Python Subprocesses With Bwa And Samtools
1
0
Entering edit mode
12.0 years ago
dan79 ▴ 90

I am running a bwa sampe which automatically outputs a sam file, but I want to save disk space as well so I am trying to pipe its output through samtools and output a bamfile. This is my implementation in python. But I am not sure what I am doing incorrectly, could anyone point me in the right direction? Here is the code I have so far.

cmd1 = ['bwa', 'sampe', reference, sai1, sai2, fastq1, fastq2]
cmd2 = ['samtools', 'view', '-bS']
p1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p2 = subprocess.Popen(cmd2, stdin=p1.stdout, stdout=open('file.bam', 'w'))

The output is just the samtools view usage, then it looks like its doing something because the cursor doesn't go back to the two arrows it just blinks. I don't think it should show the samtools view usage if I was doing it correctly, any suggestions?

bwa samtools python • 7.0k views
ADD COMMENT
0
Entering edit mode
12.0 years ago
Ying W ★ 4.2k

you might try to do it in shell instead of python:

bwa sampe reference.fasta tmp1.sai tmp2.sai tmp-1.fq tmp-2.fq | samtools view -Shu - | samtools sort - myfile

I used -u instead of -b since that would make the sorting faster but you would save space (instead of time) if you use -b. headers are often needed in some downstream applications so I included them with -h, the - means read from stdin, think of it as /dev/stdin

ADD COMMENT
0
Entering edit mode

If OP needed python for some reason, could achieve this with shell=True in Popen, such as:

Popen(('bwa sampe reference.fasta tmp1.sai tmp2.sai tmp-1.fq tmp-2.fq | samtools view -Shu - | samtools sort - myfile'), shell=True))
ADD REPLY

Login before adding your answer.

Traffic: 3111 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6