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))
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?
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
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))
Log in to answer this question.