Answers.Body: <p>You might have to manually strip those auxiliary tags off:</p>
  
  <pre><code>samtools view -h your.bam | grep -v "^@RG" | sed "s/\tRG:Z:[^\t]*//" | samtools view -bo your_fixed.bam -
  </code></pre>
  
  <p>The <code>sed</code> bit is searching for the aux tag and removing everything up to the next tab.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-23T14:00:33.083Z
Answers.LastActivityDate: 2017-12-23T14:00:33.083Z
Answers.UserDisplayName: Devon Ryan
Body: <p>I have problem with one my BAMs---it appears to have invalid read groups. </p>
  
  <p>Normally when I have such a problem, I remove all the read groups from the BAM header as follows:</p>
  
  <pre><code>samtools view -H your.bam | grep -v "^@RG" | samtools reheader - original.bam &gt; new.bam
  </code></pre>
  
  <p>and then I would execute <code>AddOrReplaceReadGroups</code> from Picard:</p>
  
  <p><a href="http://broadinstitute.github.io/picard/command-line-overview.html#AddOrReplaceReadGroups" rel="noreferrer">http://broadinstitute.github.io/picard/command-line-overview.html#AddOrReplaceReadGroups</a></p>
  
  <p>I have tried this, and there must still be reads with a read group in the optional tags <code>RG:Z:</code>. </p>
  
  <p>How to do I definitively remove all read groups from a BAM, including all of the reads?</p>
  
Tags: <sam><samtools><reads><picard>
Title: How to remove all BAM read groups from all reads (not just the header)?
ThreadText: # How to remove all BAM read groups from all reads (not just the header)?
  
  (Asked by: username_0 on 2017-12-23)
  
  I have problem with one my BAMs---it appears to have invalid read groups.
  
  Normally when I have such a problem, I remove all the read groups from the BAM header as follows:
  
  ```
  samtools view -H your.bam | grep -v "^@RG" | samtools reheader - original.bam > new.bam
  
  ```
  
  and then I would execute `AddOrReplaceReadGroups` from Picard:
  
  <http://broadinstitute.github.io/picard/command-line-overview.html#AddOrReplaceReadGroups>
  
  I have tried this, and there must still be reads with a read group in the optional tags `RG:Z:`.
  
  How to do I definitively remove all read groups from a BAM, including all of the reads?
  
  **Tags:** sam, samtools, reads, picard
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-12-23. Score: 7 | Accepted answer
  
  You might have to manually strip those auxiliary tags off:
  
  ```
  samtools view -h your.bam | grep -v "^@RG" | sed "s/\tRG:Z:[^\t]*//" | samtools view -bo your_fixed.bam -
  
  ```
  
  The `sed` bit is searching for the aux tag and removing everything up to the next tab.
  
  
  ---
  
  