This is a test version of Biostars. For the public version, visit https://www.biostars.org.
calling BWA mem from within python

Dear all,

I would like to call BWA mem from within a python script. This works, but I cannot specify an output filename. The output is printed to stdout, but off course this is not what I want. From within BWA I should use '>' but this is not working from within python. This is the call function that I'm using:

aln_value=call([script_dir+"/software/bwa","mem", "-t 4", "-M", script_dir+"/ref/hg19.navin", args.fastq_file ],stderr=ERR_LOG,stdout=OUT_LOG)

Is there a simple alternative that allows me to specify the outputfile from within the python script?

Thanks , Lien

bwa mem python

You can build the command as a string in python however you want, then run the command using os.system:

import os
cmd = "bwa mem -t 4 -M " + script_dir + "/ref/hg19.navin " + fastq_file + " > output"
os.system(cmd)

Thanks, this worked for me (Python 3.7). It seems that BWA doesn't work properly with os.subprocess(), but instead os.system() works perfectly. The bug about os.subprocess() should be something with output handling, I'm not sure.

1 answer

Take a look at subprocess

This would be the ideal option as it would let you rebuild shell style pipelines in python. Something like bwa mem .... | samtools view ...

Log in to answer this question.