Question: Merging Bam Files With Different Headers
gravatar for Frenkiboy
7.6 years ago by
Frenkiboy250 wrote:

This might be a trivial question, but I would rather ask then make a mistake.

What is the easiest way to merge two bam files that have different headers - the headers contain the same information regarding the chromosome names and lengths, but in one file they are sorted by char (eg. chr1 chr10), and in another file by chromosome number (chr1 chr2). I know that each read references the exact chromosome name from the header, so that the reheader command should not produce correct results. The only way that I can currently think of is to convert both bam files to sam, merge them and add the header manually.

Is there a way to do this automatically?

Best regards

bam samtools • 3.4k views
ADD COMMENTlink modified 7.6 years ago by Devon Ryan97k • written 7.6 years ago by Frenkiboy250
gravatar for Devon Ryan
7.6 years ago by
Devon Ryan97k
Freiburg, Germany
Devon Ryan97k wrote:

I'm not aware of anything that will do that without going to a sam file first. The easiest solution would be:

samtools view -h file1.bam > tmp.sam
samtools view file2.bam >> tmp.sam

You could then convert the resulting file back into bam format. Alternatively, writing a program to keep everything in bam format and merging would be simple.

ADD COMMENTlink written 7.6 years ago by Devon Ryan97k
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1202 users visited in the last hour