<p>You do not need to remap the files, replacing the read group information with <code>samtools</code> 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.</p>

CC BY-SA 4.0
2022-03-11T12:27:49.440Z
2022-03-11T12:27:49.440Z
Devon Ryan
<p>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.</p>
<p>In all cases, you can find more information about <a href="https://samtools.github.io/hts-specs/" rel="nofollow noreferrer">SAM/BAM and related specifications</a> on the <a href="https://samtools.github.io/hts-specs/SAMv1.pdf" rel="nofollow noreferrer">1.3 The header section</a> as they describe what is mandatory.</p>
<p>So, if you have access to FastQ files, you can add them directly during the alignment process with <code>bwa mem</code>:</p>
<ol>
<li><a href="http://bio-bwa.sourceforge.net/bwa.shtml" rel="nofollow noreferrer">bwa mem</a></li>
</ol>
<blockquote>
<p>-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]</p>
</blockquote>
<p>An example command line would be:</p>
<pre class="lang-bash prettyprint-override"><code>$bwa mem \
-M \                 // Mark shorter split hits as secondary (for Picard compatibility).
-t &quot;$cpus&quot; \         // number of threads
&quot;$genome&quot; \          // genome reference
&quot;$fastq_r1&quot; \        // R1
&quot;$fastq_r2&quot; \        // provide this line if you have R2
-R &quot;@RG\tID:1\tLB:lib1\tPL:illumina\tSM:$sampleName\tPU:runBarcode&quot; \
&gt; &quot;$sampleName&quot;.sam  // Output
</code></pre>
<ol start="2">
<li><a href="https://github.com/bwa-mem2/bwa-mem2" rel="nofollow noreferrer">bwa2 mem</a></li>
</ol>
<blockquote>
<p>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.</p>
</blockquote>
<p>The command line is exactly similar to the command line above, except that you would be using <code>bwa2</code>.</p>
<ol start="3">
<li><a href="http://www.htslib.org/doc/samtools-addreplacerg.html" rel="nofollow noreferrer">Samtools</a></li>
</ol>
<p>As you have mentioned, you can completely overwrite (default mode) the read groups already existing by doing:</p>
<pre class="lang-bash prettyprint-override"><code>samtools addreplacerg \
-r &quot;@RG\tID:1\tLB:lib1\tPL:illumina\tSM:$sampleName\tPU:runBarcode&quot; \
-m overwrite_all \         // mode to replace all
-@ 5 \                     // number of cpus
-O b \                     // output bam
-o &quot;$sampleName&quot;_RG.bam \
&quot;$sampleName&quot;.bam
</code></pre>
<ol start="4">
<li><a href="https://gatk.broadinstitute.org/hc/en-us/articles/360037226472-AddOrReplaceReadGroups-Picard-" rel="nofollow noreferrer">Picard AddOrReplaceReadGroups</a></li>
</ol>
<pre class="lang-bash prettyprint-override"><code>java -jar $picard AddOrReplaceReadGroups \     // Also available in GATK tools
I=&quot;$sampleName&quot;.bam \
OUTPUT=&quot;$sampleName&quot;_RG.bam \
RGID=1 \
RGLB=library \
RGPL=illumina \
RGPU=runBarcode \
RGSM=${sampleName} \
SORT_ORDER=coordinate
</code></pre>

CC BY-SA 4.0
2022-03-11T16:48:34.753Z
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.
gc5
CC BY-SA 4.0
2022-03-12T13:41:31.917Z
@gc5 you can only award a bounty to a single answer.
terdon
CC BY-SA 4.0
2022-03-14T03:42:10.610Z
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.
gc5
CC BY-SA 4.0
2022-03-11T10:14:10.200Z
2022-03-11T10:14:10.200Z
user324810
<p>I have a set of BAM files where the read group tags have some (default?) values, i.e.:</p>
<pre><code>@RG ID:RG0  LB:LB0  PU:PU0  SM:SM0
</code></pre>
<p>This creates issues in my downstream analyses, where multiple BAM files with the same SM tag are used.</p>
<p>Samtools provides <a href="http://www.htslib.org/doc/samtools-addreplacerg.html" rel="nofollow noreferrer">a command</a> 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 <code>ID</code> and <code>SM</code> with the sample id, which is unique for each BAM file.</p>
<p>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?</p>

CC BY-SA 4.0
2022-03-11T09:19:40.817Z
potential (not exactly?) duplicate of https://bioinformatics.stackexchange.com/q/908/57. Let me know if those answers fix your problem too.
Kamil S Jaron
CC BY-SA 4.0
2022-03-11T21:51:43.417Z
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.
gc5
<sam><samtools>
Potential side effects of replacing read group tags in BAM file
# Potential side effects of replacing read group tags in BAM file

(Asked by: username_0 on 2022-03-08)

I have a set of BAM files where the read group tags have some (default?) values, i.e.:
@RG ID:RG0  LB:LB0  PU:PU0  SM:SM0

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?

**Tags:** sam, samtools
> **Comments:**
> **username_1**: potential (not exactly?) duplicate of https://bioinformatics.stackexchange.com/q/908/57. Let me know if those answers fix your problem too.  
> **username_0**: 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.  

---

## 2 answers

### Answer by username_2 on 2022-03-11. Score: 3 | Accepted answer

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.


---

### Answer by username_3 on 2022-03-11. Score: 3

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:

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


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.

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 \
"$sampleName".bam


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} \
SORT_ORDER=coordinate

> **Comments:**
> **username_0**: 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.  
> **username_4**: @gc5 you can only award a bounty to a single answer.  
> **username_0**: 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.  

---

