<p>This one-liner below will work better for long reads than <code>samtools flagstat</code> in that it only counts the primary alignment for each read and <code>samtools flagstat</code> doesn't seem to calculate some stats for long reads. I have never seen <code>samtools flagstat</code> output stats on a per-reference basis, but am curious if so!</p>

<p>This answer filters out secondary and supplementary alignments for your reads (<code>-F 2304</code>) that have some alignment to both amplicon reference and just keep the best one. This might give a more accurate idea of how many reads of each amplicon are in the library.</p>

<pre><code>samtools view -F 2304 myfile.bam | awk -F <span class="math-container">$'\t' '{a[$1, $3]++} END{for (i in a) {split (i, sep, SUBSEP); print sep[1], sep[2], a[i]}}' | uniq | awk '{print($</span>2)}' | uniq -c | sort -k1 -nr
</code></pre>

<p><a href="https://askubuntu.com/questions/592273/count-the-number-of-unique-values-based-on-two-columns-in-a-spreadsheet">adapted from this.</a></p>

CC BY-SA 4.0
2018-10-30T15:08:17.377Z
2018-10-30T15:08:17.377Z
conchoecia
<p>This can be done using </p>

<pre><code>   samtools flagstat your_bam_file
</code></pre>

CC BY-SA 4.0
2018-10-30T16:02:12.010Z
I think that flagstat counts the number of alignments in the bam file.
Peter Menzel
CC BY-SA 4.0
2018-10-30T14:57:16.020Z
2018-10-30T14:57:16.020Z
Ammar Sabir Cheema
<p>The quick way to get the number of alignments on each reference is </p>

<pre><code>samtools idxstats my_bam.bam
</code></pre>

<p>Number of reads on each reference is column 3. Although, as has been pointed out, this will give you the total number of <em>alignments</em> per reference, not the total number of reads (each read might give rise to more than one alignment). That said I do tend to us this as generally I'm after a rough approximation, rather than an accurate number. </p>

<p>In theory, only one alignment for each read should be marked as primary, so the following should give you what you need quickly and at low memory usage:</p>

<pre><code>samtools view -bF 2304 my_bam.bam &gt; primary_only.bam
samtools index primary_only.bam
samtools idxstats primary_only.bam
</code></pre>

CC BY-SA 4.0
2018-10-30T18:00:03.153Z
2018-10-30T18:00:03.153Z
Ian Sudbery
<p>I have two PCR amplicons that have been multiplexed and sequenced using the nanopore minion.</p>

<p>I have aligned the fastq reads using minimap2 with a reference file containing both amplicon sequences and generated a bam file that I have viewed using IGV.</p>

<p>I am looking for a way to generate some simple summary statistics.</p>

<p>In particular, is there a way to extract the total number of fastq reads aligning to each amplicon reference from the bam file?  </p>

<phylogenetics><sam><nanopore><minion>
Total reads aligning to each reference within a bam file
# Total reads aligning to each reference within a bam file

(Asked by: username_0 on 2018-10-30)

I have two PCR amplicons that have been multiplexed and sequenced using the nanopore minion.

I have aligned the fastq reads using minimap2 with a reference file containing both amplicon sequences and generated a bam file that I have viewed using IGV.

I am looking for a way to generate some simple summary statistics.

In particular, is there a way to extract the total number of fastq reads aligning to each amplicon reference from the bam file?

**Tags:** phylogenetics, sam, nanopore, minion

---

## 3 answers

### Answer by username_4 on 2018-10-30. Score: 8 | Accepted answer

The quick way to get the number of alignments on each reference is 
samtools idxstats my_bam.bam

Number of reads on each reference is column 3. Although, as has been pointed out, this will give you the total number of alignments per reference, not the total number of reads (each read might give rise to more than one alignment). That said I do tend to us this as generally I'm after a rough approximation, rather than an accurate number. 
In theory, only one alignment for each read should be marked as primary, so the following should give you what you need quickly and at low memory usage:
samtools view -bF 2304 my_bam.bam > primary_only.bam
samtools index primary_only.bam
samtools idxstats primary_only.bam


---

### Answer by username_1 on 2018-10-30. Score: 2

This one-liner below will work better for long reads than samtools flagstat in that it only counts the primary alignment for each read and samtools flagstat doesn't seem to calculate some stats for long reads. I have never seen samtools flagstat output stats on a per-reference basis, but am curious if so!
This answer filters out secondary and supplementary alignments for your reads (-F 2304) that have some alignment to both amplicon reference and just keep the best one. This might give a more accurate idea of how many reads of each amplicon are in the library.
samtools view -F 2304 myfile.bam | awk -F $'\t' '{a[$1, $3]++} END{for (i in a) {split (i, sep, SUBSEP); print sep[1], sep[2], a[i]}}' | uniq | awk '{print($2)}' | uniq -c | sort -k1 -nr

adapted from this.


---

### Answer by username_2 on 2018-10-30. Score: 1

This can be done using 
   samtools flagstat your_bam_file

> **Comments:**
> **username_3**: I think that flagstat counts the number of alignments in the bam file.  

---

