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
Thank you in advance.
As a workaround, I do -ofoo to save to foo, but it can fail on some systems, so use carefully
Ben