Re: [Samtools-help] SAM to BAM

278 views
Skip to first unread message

Mic

unread,
Jan 2, 2012, 6:52:23 PM1/2/12
to pysam-us...@googlegroups.com
Hello,
is it a way to do it with pysam?

Thank you in advance.

On Mon, Oct 17, 2011 at 11:15 PM, Tim Fennell <tfen...@broadinstitute.org> wrote:
And if you're not tied to pysam you can use Picard's sort sam to do it all in one pass:
       java -Xmx2g -jar SortSam.jar I=your.sam O=your.bam CREATE_INDEX=true

-t

On Oct 17, 2011, at 9:06 AM, Peter Cock wrote:

> On Mon, Oct 17, 2011 at 1:11 PM, Mic <mict...@gmail.com> wrote:
>> Hello,
>> Is there a way to convert SAM file to sorted BAM file and generate also BAI
>> file with pysam?
>> Thank you in advance.
>
> With samtools at the command line,
>
> samtools view -b -S example.sam | samtools sort - example
> samtools index example.bam
>
> I know you can easy call samtools from pysam, not sure if you
> can do the pipe trick to avoid extra steps:
>
> samtools view -b -S example.sam > example_unsorted
> samtools sort example_unsorted.bam example
> rm example_unsorted.bam
> samtools index example.bam
>
> Peter
>
> ------------------------------------------------------------------------------
> All the data continuously generated in your IT infrastructure contains a
> definitive record of customers, application performance, security
> threats, fraudulent activity and more. Splunk takes this data and makes
> sense of it. Business sense. IT sense. Common sense.
> http://p.sf.net/sfu/splunk-d2d-oct
> _______________________________________________
> Samtools-help mailing list
> Samtoo...@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/samtools-help


Brett Bowman

unread,
Jun 18, 2012, 12:39:58 PM6/18/12
to pysam-us...@googlegroups.com
I am also curious about this.

I'm using PySam to QC some very large BAM files, but I'm starting to get some SAMs as well.  The problem is the unsorted SAMs take a lot longer to access, so I'd like to convert them to BAM and sort them as a pre-processing step.

I've tried pysam.view(), but it says the -o option is disabled.  In addition, since its within Python I can't use a command-line redirect '>' to do it either.

Is this currently possible?  If so how.

-Brett
Thank you in advance.

Ben Schiller

unread,
Jun 18, 2012, 1:39:45 PM6/18/12
to pysam-us...@googlegroups.com

As a workaround, I do -ofoo to save to foo, but it can fail on some systems, so use carefully

Ben

Brett Bowman

unread,
Jun 18, 2012, 3:34:25 PM6/18/12
to pysam-us...@googlegroups.com
Sweet, that works.  Thanks Ben.

Are there any plans for when/if a non-workaround option will be made available?

/b/

Ben Schiller

unread,
Jun 18, 2012, 3:50:53 PM6/18/12
to pysam-us...@googlegroups.com
Andreas would know. I forget why it was disabled in the first place -- I think it may have been an issue with Windows or Mac? You can remove the check and recompile your own copy if you want. It's in pysam/csamtools.pyx line 2867 (in the current hg source):

if "-o" in args: raise ValueError("option -o is forbidden in samtools view")

So if you need, just add a comment (#) and delete the csamtools.c file (don't commit that to the upstream code, obviously)

I went a different route and wrapped the command line samtools because I wanted to pipe things more easily. You can look at my align.py script if you want https://github.com/benjschiller/seriesoftubes . Note: the PolledPipe class I wrote does not support Windows.

Ben

Mic

unread,
Jun 20, 2012, 12:26:17 AM6/20/12
to pysam-us...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages