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?
If OP needed python for some reason, could achieve this with shell=True in Popen, such as: