This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Problem with BAM file headers

Hello Everyone,

I have noticed an issue with my BAM file headers, where the @RG line is either mal-formed or is missing entirely. I think I can sed the files that are mal-formed, and add the sample names necessary to complete my further analyses in GATK. But is there a way to add an entire line to the headers?

Thanks,

sam gatk bam

2 answers

samtools addreplacerg -w -r ID:oldid -r LB:Lib -r SM:SAMPLE -O BAM -o output.bam bad.bam

Ahh, I think that's what I am looing for. Thanks!

You should be able to open and edit the header in plain text format, and you can just add it to the top of a headerless bam. Something like

samtools view -H bad.bam > bad_header.txt

<edit bad_header.txt>

samtools view bad.bam | cat good_header - | samtools view -hb - > good.bam

Hi,

I think that's close to the solution I want, but the BAM files I have are not headerless, their headers are just really poorly formatted. I simply want to add a line to the header, I think I can do what you said, but then use samtools reheader to edit the original BAM.

So:

samtools reheader edited_header.sam bad.bam > good.bam

Log in to answer this question.