is there a pythonic way to merge a set of bam files?
is there a pythonic way to merge a set of bam files?
I figured out the answer to this one. I am writing down it here for future me and all other curious people.
import pysam
list_of_bamfiles = [f.bam,f2.bam]
pysam.merge("-f","-o","output.bam",*list_of_bamfile)
# the * before the list unpack the list of arguments
Please read the manual: https://pysam.readthedocs.io/en/latest/usage.html#using-samtools-commands-within-python
Commands available in samtools are available as simple function calls. Command line options are provided as arguments. For example:
pysam.sort("-o", "output.bam", "ex1.bam")
corresponds to the command line:
samtools sort -o output.bam ex1.bam
Then lookup the samtools merge manual page: https://www.htslib.org/doc/samtools-merge.html to see what -f
and -o
mean.
Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Why does it need to be pythonic? There are far more straightforward command line ways to do this.
Thanks Ram. I am aware of the straightforward ways. I am just curious if I can do this with python.
Got it. We've seen a lot of users that pick tech first, task later so this sort of awareness is not always present. Glad you figured out a solution, it always helps understand the underlying challenges.