text: user1771: 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?
  
  user4413: 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
  
  
  user77: 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.
  
original_text: 1771: <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>
   4413: <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>
   77: <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>
  