SAM and BAM Files Explained: Flags, CIGAR Strings and Coverage
A practical guide to reading SAM and BAM alignments: header lines, flags, MAPQ and CIGAR, region filters and coverage, with a worked four-read example.

SAM and BAM Files Explained: Flags, CIGAR Strings and Coverage
After reads are aligned to a reference genome, the result is usually a SAM or BAM file. SAM is tab-separated text, one alignment per line; BAM holds the same records in a compressed binary form made of BGZF blocks. Learning to read them is mostly a matter of knowing what eleven columns and a few header lines mean.
The header
Header lines start with @. @HD gives the format version and sort order, @SQ lines list each reference sequence with its name (SN) and length (LN), @RG describes read groups and @PG records the programs that touched the file. The reference list matters most, because every alignment points into it.
The eleven columns
Each record has the read name, a flag, the reference name, the 1-based leftmost position, the mapping quality (MAPQ), the CIGAR string, the mate's reference and position, the template length, the sequence and the base qualities. Optional tags such as NM:i:0, the edit distance, follow.
The flag is a sum of bits. 4 means unmapped, 16 reverse strand, 256 secondary, 1024 duplicate and 2048 supplementary; paired-end data adds bits for the mate. A flag of 16 is simply a read on the reverse strand; 1024 is a read marked as a PCR or optical duplicate.
The CIGAR string describes how the read lines up. 10M is ten aligned bases. 5M2D5M means five aligned bases, two bases of the reference missing from the read, and five more aligned bases, so the read covers twelve reference positions. S is a soft clip, I an insertion and N a skipped region, as in RNA-seq splice junctions.
Worked example
The SAM and BAM Alignment Viewer was tested with a tiny file of two references, chr1 of 1,000 bases and chr2 of 500, and four reads:
- r1, flag 0, chr1 at 100, MAPQ 60, 10M
- r2, flag 16, chr1 at 105, MAPQ 30, 5M2D5M
- r3, flag 4, unmapped
- r4, flag 1024, chr2 at 10, MAPQ 60, 4M
The summary reads 4 alignments, 3 mapped, 1 duplicate and a mean MAPQ of 50, which is (60 + 30 + 60) ÷ 3, because only the three mapped reads are averaged; the unmapped read's MAPQ of 0 would otherwise drag it down.
By default the viewer leaves out unmapped, secondary, duplicate and supplementary reads, so 2 alignments match: r1 and r2. Type the region chr1:101-104 and only r1 is left, because r2 starts at 105. A region filter keeps a read if any part of its reference span overlaps the range.
Coverage from 100 to 119 shows the CIGAR at work. Positions 100 to 104 have depth 1 (r1 only). Positions 105 to 109 have depth 2, where r1 and the first five bases of r2 overlap. Positions 110 and 111 drop to 0, because those are r2's two deleted bases and r1 ended at 109. Positions 112 to 116 are back to 1 and the rest are 0.
Saved as BAM, the four records make a 272-byte file, and reading it back returns the same names, flags, positions, CIGARs, sequences and qualities.
Exporting a subset
A common job is pulling the reads from one gene or one region to share or to load into another tool. Set the region and flags, check the count, and use Export SAM or Export BAM. The BAM export is written in the original order, so sort and index it with samtools before using it in a genome browser that needs random access. Coverage CSV saves the binned depth for a chart of your own.
Common mistakes
- Opening the .bai index instead of the .bam. The index only points into the BAM; it holds no reads.
- Forgetting the default filters. Duplicates and secondary alignments are hidden until you untick them, which changes counts.
- Reading positions as 0-based. SAM positions start at 1; BED files start at 0.
- Expecting the viewer to show mismatches. Without the reference sequence, it shows alignments and depth, not variants.
- Loading a whole-genome BAM. The file is read into memory in full, so large files belong in samtools with an index.
For teaching, a few hundred reads are enough to see how flags, CIGAR operations and filters change what the coverage plot shows.



