Body: <p>I have merged together 2 different .bam files in order to simulate sample contamination. So the reads can come from one of two samples, as shown by the read group info:</p>
  <pre><code>@RG ID:0    PL:ILLUMINA SM:LP4100018-DNA_C11_Proband    PU:HGY3WDSXX:1:none
  @RG ID:1    PL:ILLUMINA SM:LP4100018-DNA_C11_Proband    PU:HGY3WDSXX:2:none
  @RG ID:2    PL:ILLUMINA SM:LP4100018-DNA_C11_Proband    PU:HGY3WDSXX:3:none
  @RG ID:3    PL:ILLUMINA SM:LP4100018-DNA_C11_Proband    PU:HGY3WDSXX:4:none
  @RG ID:0-11EFC00B   PL:ILLUMINA SM:LP4100018-DNA_E11_Proband    PU:HGY3WDSXX:1:none
  @RG ID:1-B8A1099    PL:ILLUMINA SM:LP4100018-DNA_E11_Proband    PU:HGY3WDSXX:2:none
  @RG ID:2-330086F    PL:ILLUMINA SM:LP4100018-DNA_E11_Proband    PU:HGY3WDSXX:3:none
  @RG ID:3-7681F092   PL:ILLUMINA SM:LP4100018-DNA_E11_Proband    PU:HGY3WDSXX:4:none
  </code></pre>
  <p>I'd like to check that the correct proportion of read groups originate from each sample.</p>
  <p>Currently I am using:</p>
  <pre><code>samtools view example.bam | rev | cut -f 1 | rev &gt; output.txt
  </code></pre>
  <p>, but this is not very elegant and only works because the RG field is last in the .bam.</p>
  <p>Is there a quick way to tabulate the number of reads groups with different IDs? E.g. produce an output like:</p>
  <pre><code>ID:0 1000
  ID:1 2000
  ID:2 3000
  ...
  </code></pre>
  <p>A solution in samtools would be ideal, along the lines of the output produced in <code>samtools stats</code>.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-09-23T21:16:54.770Z
Comments.Text: Does this answer your question: https://bioinformatics.stackexchange.com/q/10559/6251 ?
Comments.UserDisplayName: Timur Shtatland
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-09-23T21:17:42.177Z
Comments.Text: Also, try [sambamba](https://github.com/biod/sambamba/wiki/%5Bsambamba-view%5D-Filter-expression-syntax).
Comments.UserDisplayName: Timur Shtatland
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-09-24T08:22:42.553Z
Comments.Text: @TimurShtatland, yes, using ``samtools view -R read_groups.txt example.bam | wc -l`` will work. thanks.
Comments.UserDisplayName: user438383
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-01-15T13:10:05.053Z
Comments.Text: Would you like to self-answer the question then? So it's easier for others to find the solution.
Comments.UserDisplayName: Kamil S Jaron
Tags: <sam><samtools>
Title: summarising read group information from a .bam file
ThreadText: # summarising read group information from a .bam file
  
  (Asked by: username_0 on 2020-09-23)
  
  I have merged together 2 different .bam files in order to simulate sample contamination. So the reads can come from one of two samples, as shown by the read group info:
  
  ```
  @RG ID:0    PL:ILLUMINA SM:LP4100018-DNA_C11_Proband    PU:HGY3WDSXX:1:none
  @RG ID:1    PL:ILLUMINA SM:LP4100018-DNA_C11_Proband    PU:HGY3WDSXX:2:none
  @RG ID:2    PL:ILLUMINA SM:LP4100018-DNA_C11_Proband    PU:HGY3WDSXX:3:none
  @RG ID:3    PL:ILLUMINA SM:LP4100018-DNA_C11_Proband    PU:HGY3WDSXX:4:none
  @RG ID:0-11EFC00B   PL:ILLUMINA SM:LP4100018-DNA_E11_Proband    PU:HGY3WDSXX:1:none
  @RG ID:1-B8A1099    PL:ILLUMINA SM:LP4100018-DNA_E11_Proband    PU:HGY3WDSXX:2:none
  @RG ID:2-330086F    PL:ILLUMINA SM:LP4100018-DNA_E11_Proband    PU:HGY3WDSXX:3:none
  @RG ID:3-7681F092   PL:ILLUMINA SM:LP4100018-DNA_E11_Proband    PU:HGY3WDSXX:4:none
  
  ```
  
  I'd like to check that the correct proportion of read groups originate from each sample.
  
  Currently I am using:
  
  ```
  samtools view example.bam | rev | cut -f 1 | rev > output.txt
  
  ```
  
  , but this is not very elegant and only works because the RG field is last in the .bam.
  
  Is there a quick way to tabulate the number of reads groups with different IDs? E.g. produce an output like:
  
  ```
  ID:0 1000
  ID:1 2000
  ID:2 3000
  ...
  
  ```
  
  A solution in samtools would be ideal, along the lines of the output produced in `samtools stats`.
  
  **Tags:** sam, samtools
  
  > **Comments:**
  > **username_1**: Does this answer your question: https://bioinformatics.stackexchange.com/q/10559/6251 ?  
  > **username_1**: Also, try [sambamba](https://github.com/biod/sambamba/wiki/%5Bsambamba-view%5D-Filter-expression-syntax).  
  > **username_0**: @username_1, yes, using ``samtools view -R read_groups.txt example.bam | wc -l`` will work. thanks.  
  > **username_2**: Would you like to self-answer the question then? So it's easier for others to find the solution.  
  
  ---
  
  ## 0 answers
  
  