Summarize alignment stats with samtools flagstat
Read mapping rate, duplicate counts, and per-chromosome read totals from a BAM in seconds with samtools flagstat and idxstats.
Prerequisites
- samtools installed (v1.x)
- A coordinate-sorted BAM file (sorted.bam)
- A BAM index (sorted.bam.bai) for the idxstats step
1Get the flag summary and mapping rate
Run flagstat to tally every read by its SAM flags. The one line to watch is mapped — the percentage next to it is your overall mapping rate, and duplicates tells you how many reads were flagged as PCR/optical duplicates.
samtools flagstat sorted.bamExpected output
1965432 + 0 in total (QC-passed reads + QC-failed reads)
0 + 0 secondary
0 + 0 supplementary
18342 + 0 duplicates
1958201 + 0 mapped (99.63% : N/A)
1965432 + 0 paired in sequencing
982716 + 0 read1
982716 + 0 read2
1946890 + 0 properly paired (99.06% : N/A)
1954120 + 0 with itself and mate mapped
4081 + 0 singletons (0.21% : N/A)
0 + 0 with mate mapped to a different chr
0 + 0 with mate mapped to a different chr (mapQ>=5)2Count mapped reads per chromosome
idxstats reads the BAM index and prints one row per reference sequence: name, length, mapped read-segments, and unmapped read-segments. To get a genome-wide mapped total, sum the third column with `samtools idxstats sorted.bam | awk '{s+=$3} END {print s}'`.
samtools idxstats sorted.bamExpected output
chr1 248956422 195234 12
chr2 242193529 189122 9
chr3 198295559 156340 7
chr4 190214555 142887 5
chrX 156040895 98765 4
chrM 16569 84120 0
* 0 0 7231Troubleshooting
idxstats prints only a single line: `* 0 0 0`
The BAM has no index, so samtools cannot read per-reference counts. Build one with `samtools index sorted.bam` (the BAM must be coordinate-sorted first), then re-run idxstats.
You need insert-size, error-rate, or base-quality detail that flagstat does not report
Run `samtools stats sorted.bam | grep ^SN` to print the summary-numbers block, which includes insert size average, error rate, mismatches, and average quality.