Merging Bam Files
3
1
Entering edit mode
10.9 years ago
rickyflintoff ▴ 100

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 • 6.7k views
ADD COMMENT
2
Entering edit mode
10.9 years ago

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
ADD COMMENT
1
Entering edit mode
10.9 years ago

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

ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

just use awk ?

ADD REPLY
0
Entering edit mode
10.9 years ago

Use picard tools AddOrReplaceReadGroups

ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode
  1. You should post things like this as new questions rather than answers on previous questions.
  2. Yes, you can do that.
ADD REPLY

Login before adding your answer.

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