To calculate the total number of reads in a bam file:
$samtools view -c mapping.bam 119244250
To calculate only mapped reads:
$samtools view -c -F 4 mapping.bam 103642171
Here “-F 4” is the flag used in the sam format. Similarly we can use “-f 4” to get the number of unmapped reads.
$samtools view -f4 mapping.bam 15602079
The sum of the mapped and unmapped reads equals the total reads:
$echo "103642171 + 15602079" |bc 119244250