How to merge set of bam files into one?
1
0
Entering edit mode
22 months ago
mrj ▴ 180

is there a pythonic way to merge a set of bam files?

merge bam python pysam • 1.8k views
ADD COMMENT
3
Entering edit mode

Why does it need to be pythonic? There are far more straightforward command line ways to do this.

ADD REPLY
2
Entering edit mode

Thanks Ram. I am aware of the straightforward ways. I am just curious if I can do this with python.

ADD REPLY
1
Entering edit mode

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.

ADD REPLY
5
Entering edit mode
22 months ago
mrj ▴ 180

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
ADD COMMENT
0
Entering edit mode

Thanks for sharing this! What do the first two arguments ("-f","-o") stand for?

ADD REPLY
0
Entering edit mode

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.

ADD REPLY

Login before adding your answer.

Traffic: 2338 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