Please help me find the number of mapped reads from a bam file. I was going through forums and tutorials. Looking at `samtools flagstat` resulted the following:

My total read count is **413,033**. So, I understand 453,800 to be number of alignments. Now, is it that 391,696 reads mapped? According to [**this tutorial**][1], *Yes*.

1\. `samtools flagstat file.sorted.bam`

```
453800 + 0 in total (QC-passed reads + QC-failed reads)
0 + 0 duplicates
391696 + 0 mapped (86.31%:-nan%) 0 + 0 paired in sequencing
0 + 0 read1
0 + 0 read2
0 + 0 properly paired (-nan%:-nan%)
0 + 0 with itself and mate mapped
0 + 0 singletons (-nan%:-nan%)
0 + 0 with mate mapped to a different chr
0 + 0 with mate mapped to a different chr (mapQ>=5)
```

Found another explanation at [this page][2]. It says 391,696 is the number of locations that reads mapped. A number same as flagstat is giving us above and the number of mapped reads are 413,033 same as my original fasta sequence number. Check commands below.

2\. Number of Mapped locations:

```
samtools view -F 0x04 -c file.sorted.bam
391696
```

3\. Number of Mapped reads:

```
samtools view -F 0x40 file.sorted.bam | cut -f1 | sort | uniq | wc -l
413033
```

Could I please know, which one, should I follow to find the number of reads that mapped the reference from my original fasta file? Suppose the reads are to be single end. I would thankful to your explanations.

 [1]: http://left.subtree.org/2012/04/13/counting-the-number-of-reads-in-a-bam-file/
 [2]: https://gist.github.com/davfre/8596159
1. 391696 is the number of mapped alignments in the BAM file. That includes multimappers, so it won't necessarily be the number of mapped reads.
2. This is identical to above and does the exact same thing.
3. This is the number of entries excluding those marked as read #1 in a pair. This is not what you want.

You have a single-end data set. What you want is the number of mapped alignment, excluding those marked as secondary or supplementary (i.e., multimappers or chimeric entries). Consequently, `samtools view -F 0x904 -c file.sorted.bam` should work for you. This won't work for paired-end datasets (you'd have to `samtools view -F 0x4 foo.sorted.bam | cut -f 1 | sort | uniq | wc -l` them).