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

Greetings all,

Let me start by saying I know how to add read groups to a BAM file. What I am looking for is the easiest way to do it. This problem has been around for a while so I though I would see what neat tricks people are using.

Thanks

bam gatk samtools bwa

4 answers

In my opinion the easy way to do is using AddOrReplaceReadGroups in Picard:

What parameter would you use for these options (mandatory) in AddOrReplaceReadGroups

LB=String    Read Group Library  Required.
PU=String    Read Group platform unit (eg. run barcode)  Required.
SM=String    Read Group sample name  Required.

See this.

The library information of each read group is kept in rg.txt

perl -e 'print "@RG\tID:ga\tSM:hs\tLB:ga\tPL:Illumina\n@RG\tID:454\tSM:hs\tLB:454\tPL:454\n"' > rg.txt
samtools merge -rh rg.txt - ga.bam 454.bam | samtools rmdup - - | samtools rmdup -s - aln.bam

Some discussion over here.

Python: Adding Read Group (@RG) tags to BAM or SAM files:

How can I tell if my BAM file has read group and sample information

Related post: Read Group In Sam/Bam Files: What Do They Exactly Describe?

Could you please explain these two commands.

Can i use the same rg.txt in my libraries. Or i need to replace them. Not sure by what ?

In second command, why there are two bam ?

I think this solution is invalid.

  1. you need to escape @ character, so it would be at least:

    perl -e 'print "\@RG\tID:ga\tSM:hs\tLB:ga\tPL:Illumina\n\@RG\tID:454\tSM:hs\tLB:454\tPL:454\n"' > rg.txt
    
  2. The second problem is that the line defined only the header and -r flag will assign readgroups by filenames - in this case ga and 454. Related question I asked to find a neat solution is here

Thanks for the note on escaping the @ character...was really confused

I know this question is years ago, but I just want to add that there is bamaddrg as part of bamtools that has really straightforward usage: http://github.com/ekg/bamaddrg

Should samtools be able to do this: http://www.htslib.org/doc/samtools-addreplacerg.html

Log in to answer this question.