<p>Typically I use <a href="http://www.htslib.org/doc/samtools.html" rel="nofollow noreferrer">samtools</a> for operations like this.  Specifically I use <code>samtools view</code> with either <code>-r</code> or <code>-R</code> flag depending on the use case.  </p>

<pre><code>-r STR
Output alignments in read group STR [null]. Note that records with no RG tag will also be output when using this option. This behaviour may change in a future release.

-R FILE
Output alignments in read groups listed in FILE [null]. Note that records with no RG tag will also be output when using this option. This behaviour may change in a future release.
</code></pre>

CC BY-SA 4.0
2019-10-12T01:55:38.107Z
2019-10-12T01:55:38.107Z
Bioathlete
<p>This sounds more like a job for <code>samtools split</code> if you want to split out all the read groups into separate bams in one go. </p>

<p><a href="http://www.htslib.org/doc/samtools-split.html" rel="nofollow noreferrer">http://www.htslib.org/doc/samtools-split.html</a></p>

<pre><code>samtools split [options] merged.sam|merged.bam|merged.cram

Splits a file by read group.

Options:

-u FILE1
Put reads with no RG tag or an unrecognised RG tag into FILE1

-u FILE1:FILE2
As above, but assigns an RG tag as given in the header of FILE2

-f STRING
Output filename format string (see below) ["%*_%#.%."]

-v
Verbose output
</code></pre>

<p>Alternatively if you do want to proceed one read group at a time, in addition to <code>samtools view</code>, you can also do this with GATK using the PrintReads tool with the ReadGroupReadFilter as documented here:</p>

<p><a href="https://gatk.broadinstitute.org/hc/en-us/articles/360041415172-ReadGroupReadFilter" rel="nofollow noreferrer">https://gatk.broadinstitute.org/hc/en-us/articles/360041415172-ReadGroupReadFilter</a></p>

<p>The command line would look like this:</p>

<pre><code>gatk PrintReads \
   -I input.bam \
   --read-filter ReadGroupReadFilter \
   --keep-read-group &lt;RG:readgroup&gt; \
   -O output.bam
</code></pre>

<p>There's also a filter called ReadGroupBlackListReadFilter that allows you to exclude a list of readgroups.</p>

<p><a href="https://gatk.broadinstitute.org/hc/en-us/articles/360041851151-ReadGroupBlackListReadFilter" rel="nofollow noreferrer">https://gatk.broadinstitute.org/hc/en-us/articles/360041851151-ReadGroupBlackListReadFilter</a></p>

<p>The PrintReads tool doc is here: </p>

<p><a href="https://gatk.broadinstitute.org/hc/en-us/articles/360041849911-PrintReads" rel="nofollow noreferrer">https://gatk.broadinstitute.org/hc/en-us/articles/360041849911-PrintReads</a></p>

CC BY-SA 4.0
2020-04-23T15:34:43.573Z
2020-04-23T15:34:43.573Z
Geraldine_VdAuwera
<p>How could I extract reads from bam files by their read groups @RG ?, I've got one file containing all reads for 5 samples, the SM is appears NONE, So, I want to extract each sample reads by the @RG tags. </p>

CC BY-SA 4.0
2019-10-09T23:34:36.360Z
Hi and welcome, please have a think about targeting your question better. What are the genome sizes and machine? What did you try command wise?
M__
CC BY-SA 4.0
2019-10-09T23:39:49.970Z
Okey thank u for answering.
Maloki
CC BY-SA 4.0
2019-10-09T23:47:51.590Z
So, I use ion torrent to sequence 3 bactrial whole genome and 2 samples of fungi. I used 5 codebars but when I launched the plan I Forgot to check if I put the names of each samples. When I've got the ubam file I found only one file containing all the reads. So I Want To know if I could get each sample by its @RG or ID. Knowing that the SM tag appears "None".
Maloki
<sam><ngs><reads><genome-sequencing><bacteria>
Extract reads from bam files by their @RG
# Extract reads from bam files by their @RG

(Asked by: username_0 on 2019-10-09)

How could I extract reads from bam files by their read groups @RG ?, I've got one file containing all reads for 5 samples, the SM is appears NONE, So, I want to extract each sample reads by the @RG tags.

**Tags:** sam, ngs, reads, genome-sequencing, bacteria
> **Comments:**
> **username_1**: Hi and welcome, please have a think about targeting your question better. What are the genome sizes and machine? What did you try command wise?  
> **username_0**: Okey thank u for answering.  
> **username_0**: So, I use ion torrent to sequence 3 bactrial whole genome and 2 samples of fungi. I used 5 codebars but when I launched the plan I Forgot to check if I put the names of each samples. When I've got the ubam file I found only one file containing all the reads. So I Want To know if I could get each sample by its @RG or ID. Knowing that the SM tag appears "None".  

---

## 2 answers

### Answer by username_2 on 2019-10-12. Score: 4

Typically I use samtools for operations like this.  Specifically I use samtools view with either -r or -R flag depending on the use case.  
-r STR
Output alignments in read group STR [null]. Note that records with no RG tag will also be output when using this option. This behaviour may change in a future release.

-R FILE
Output alignments in read groups listed in FILE [null]. Note that records with no RG tag will also be output when using this option. This behaviour may change in a future release.


---

### Answer by username_3 on 2020-04-23. Score: 2

This sounds more like a job for samtools split if you want to split out all the read groups into separate bams in one go. 
http://www.htslib.org/doc/samtools-split.html
samtools split [options] merged.sam|merged.bam|merged.cram

Splits a file by read group.

Options:

-u FILE1
Put reads with no RG tag or an unrecognised RG tag into FILE1

-u FILE1:FILE2
As above, but assigns an RG tag as given in the header of FILE2

-f STRING
Output filename format string (see below) ["%*_%#.%."]

-v
Verbose output

Alternatively if you do want to proceed one read group at a time, in addition to samtools view, you can also do this with GATK using the PrintReads tool with the ReadGroupReadFilter as documented here:
https://gatk.broadinstitute.org/hc/en-us/articles/360041415172-ReadGroupReadFilter
The command line would look like this:
gatk PrintReads \
   -I input.bam \
   --read-filter ReadGroupReadFilter \
   --keep-read-group  \
   -O output.bam

There's also a filter called ReadGroupBlackListReadFilter that allows you to exclude a list of readgroups.
https://gatk.broadinstitute.org/hc/en-us/articles/360041851151-ReadGroupBlackListReadFilter
The PrintReads tool doc is here: 
https://gatk.broadinstitute.org/hc/en-us/articles/360041849911-PrintReads


---

