I have a set of BAM files where the read group tags have some (default?) values, i.e.:


This creates issues in my downstream analyses, where multiple BAM files with the same SM tag are used.

Samtools provides a command to replace the read group tag. However, I am not sure if there are possible side-effects that I should be aware of, and I might need to remap the BAM file after this replacement. I want to replace ID and SM with the sample id, which is unique for each BAM file.

Do I need to remap and/or run additional steps, or replacing the RG tags should be sufficient to update the BAM files in a consistent way?

  • $\begingroup$ potential (not exactly?) duplicate of bioinformatics.stackexchange.com/q/908/57. Let me know if those answers fix your problem too. $\endgroup$ Mar 11, 2022 at 9:19
  • $\begingroup$ Thanks @KamilSJaron, as indicated above, my question was mainly concerned on potential side effects of using samtools to modify the header - and if remapping was needed. Therefore, even if the question you linked might be interesting, it is not exactly a duplicate of that. Thanks for your help, though. $\endgroup$
    – gc5
    Mar 11, 2022 at 21:51

2 Answers 2


You do not need to remap the files, replacing the read group information with samtools is sufficient to deal with this. When you update your pipeline, have it use the sample name to construct the read group information. That way you won't run into this problem again.


I assume you are using GATK software tools for downstream analysis. If that's the case, then you will most likely encounter issues due to missing information on read groups. I believe that GATK can't differentiate between read groups if they are all similar.

In all cases, you can find more information about SAM/BAM and related specifications on the 1.3 The header section as they describe what is mandatory.

So, if you have access to FastQ files, you can add them directly during the alignment process with bwa mem:

  1. bwa mem

-R STR Complete read group header line. ’\t’ can be used in STR and will be converted to a TAB in the output SAM. The read group ID will be attached to every read in the output. An example is ’@RG\tID:foo\tSM:bar’. [null]

An example command line would be:

$bwa mem \
-M \                 // Mark shorter split hits as secondary (for Picard compatibility).
-t "$cpus" \         // number of threads
"$genome" \          // genome reference
"$fastq_r1" \        // R1
"$fastq_r2" \        // provide this line if you have R2
-R "@RG\tID:1\tLB:lib1\tPL:illumina\tSM:$sampleName\tPU:runBarcode" \
> "$sampleName".sam  // Output
  1. bwa2 mem

Bwa-mem2 is the next version of the bwa-mem algorithm in bwa. It produces alignment identical to bwa and is ~1.3-3.1x faster depending on the use-case, dataset and the running machine.

The command line is exactly similar to the command line above, except that you would be using bwa2.

  1. Samtools

As you have mentioned, you can completely overwrite (default mode) the read groups already existing by doing:

samtools addreplacerg \
-r "@RG\tID:1\tLB:lib1\tPL:illumina\tSM:$sampleName\tPU:runBarcode" \
-m overwrite_all \         // mode to replace all
-@ 5 \                     // number of cpus
-O b \                     // output bam
-o "$sampleName"_RG.bam \
  1. Picard AddOrReplaceReadGroups
java -jar $picard AddOrReplaceReadGroups \     // Also available in GATK tools
I="$sampleName".bam \
OUTPUT="$sampleName"_RG.bam \
RGID=1 \
RGLB=library \
RGPL=illumina \
RGPU=runBarcode \
RGSM=${sampleName} \
  • 1
    $\begingroup$ Thank you for your detailed answer. My question was actually more limited to: do I need to remap or update the bam files after having modified the read group with samtools? Therefore, I decided to use the other answer. I will however award the bounty to both once it is possible for me to do it. $\endgroup$
    – gc5
    Mar 11, 2022 at 16:48
  • $\begingroup$ @gc5 you can only award a bounty to a single answer. $\endgroup$
    – terdon
    Mar 12, 2022 at 13:41
  • 1
    $\begingroup$ Hi @terdon, yes. Although I meant to award a second bounty for the help. I will be awarding it in 23 hours when it is possible for me to do it. $\endgroup$
    – gc5
    Mar 14, 2022 at 3:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.