This is a test version of Biostars. For the public version, visit https://www.biostars.org.
reheadering, merging and sorting on different bam file

Hi,

Imagine that I have 20 bam files, which I want to change their header then merge them and then sort them! So I just wondering if these two scenarios make different output :

  1. reheader (each of them) -> merge all -> sort the output
  2. merge all -> reheader the output -> sort

All bam files have the same read group, and I want to add sample name to each RG field

@RG     ID:1120

Thanks

bam sequence

1 answer

It shouldn't make any difference, but what I'd suggest doing is using samtools merge and passing the header you want to apply to the merged file through the -h argument. That will simplify your workflow. samtools merge does expect your input to be already sorted though. If your input is indeed sorted, then that will allow you to accomplish your goal with a single command.

For example, put the header you want in a file called header.sam:

samtools merge -h header.sam merged.bam 1.bam 2.bam 3.bam ... 20.bam

What if my bam files are not sorted, what actually does samtools merge do then? because I've just run samtools merge with unsorted bam files.

many thanks!

Log in to answer this question.