This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Merging Bam Files

I have two BAM files that have a different sample in read group (@RG) section. I would like the sample to be the same. How can I quickly modify the sample of the read group in a BAM file, so the merge BAM file has a single sample?

bam

3 answers

Try this command on bam file where you want to change the sample name. Oldname is the present sample name and the Newname is the correct sample name. Once done with this step then you can use Picard MergeSam to merge your files. This command is similar to Pierre but is a single step process.

samtools view -H $BAM | sed "s/SM:Oldname/SM:Newname/" | samtools reheader - $BAM

I would dump the header

samtools  view -H the1.bam > header.txt

edit the header and change the sample in the header for each group-id

and re-insert the header:

samtools reheader header.txt the1.bam

and then merge the1.bam and the2.bam with picard MergeSamFiles

Is there an API that I can use to accomplish the task to renaming the sample in the header?

Use picard tools AddOrReplaceReadGroups

I attempted that. It replaces all the headers with a single header. I only want to map the sample to a constant sample.

Can I merge bam files from paired end and single end sequencing data together?

  1. You should post things like this as new questions rather than answers on previous questions.
  2. Yes, you can do that.

Log in to answer this question.