Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 49 ยท Genomics Pipelines

Read Mapping, SAM and BAM

Understand how reads are mapped to a reference genome, how to read SAM and BAM records including FLAG, CIGAR and MAPQ, and how to check coverage and mapping quality.

You now hold hundreds of millions of reads and one reference genome. The next job sounds simple: for every read, find where in the genome it came from. Doing that naively would mean comparing each read with every position, billions of comparisons per read. Mapping software gets the same answer in milliseconds by building an index of the reference first.

This lesson explains how that indexing works, then teaches you to read the standard output format, SAM (and its compressed twin BAM), field by field. By the end you should be able to look at a line of SAM and say where the read landed, whether it was paired, which strand it was on, and how confident the mapper was.

Reads stacked above a reference bar with a coverage curve; three of five reads covering one position show a red mismatch.
Caption: a repeated mismatch in several overlapping reads suggests a variant; a lone mismatch is more likely a sequencing error.

What you will learn

  • How an index lets a mapper find a read's location without scanning the genome.
  • The eleven required SAM fields and how to read FLAG, CIGAR and MAPQ.
  • What sorting, indexing, duplicate marking and BAM compression do.
  • Which summary numbers tell you a mapping run was healthy.

Key terms

Mapping (alignment): finding the reference position that best matches a read.

SAM / BAM / CRAM: text, compressed binary and reference-compressed formats for alignments.

FLAG: a number whose bits describe a read: paired, reversed, duplicate and so on.

CIGAR: a compact string such as 20M2I30M that describes how a read aligns.

MAPQ: a Phred-scaled estimate that the read was placed at the wrong location.

How a mapper finds a needle in a 3-billion-letter haystack

Almost every short-read mapper follows the seed-and-extend strategy.

  1. Build an index of the reference once. Older tools stored a hash table of short substrings (k-mers) and where they occur. Bowtie2 and BWA use the FM-index, built from the Burrows-Wheeler transform of the genome. It is a compressed suffix array: a human genome index fits in a few gigabytes and can answer "where does this exact substring occur?" in time proportional to the substring's length, not the genome's. Minimap2, popular for long reads, samples minimizers (the smallest k-mer in each window) to keep the index small and fast.
  2. Seed. Cut the read into short pieces and look each one up. Pieces that match exactly give candidate locations.
  3. Extend. For each candidate, run a more careful alignment around the seed, usually a form of dynamic programming (Smith-Waterman, Needleman-Wunsch) that can place mismatches and small insertions or deletions.
  4. Score and choose. Pick the best location, and estimate how much better it is than the runner-up.

Paired-end reads help: both mates must land near each other, on opposite strands, at roughly the expected insert size. A read from a repeat can be rescued when its mate sits in unique sequence.

BWA-MEM and its faster rewrite BWA-MEM2 are the usual choice for short DNA reads; Bowtie2 is another common choice; minimap2 handles long reads and assemblies. RNA-seq needs splice-aware mappers because a read can span an intron, which the next chapter covers.

The SAM format

SAM (Sequence Alignment/Map) is a tab-separated text format. A header of lines beginning with @ records the reference sequences (@SQ), the sample (@RG) and the programs that touched the file (@PG). After the header, each line describes one alignment with eleven required fields:

# Field Meaning
1 QNAME read name, copied from the FASTQ
2 FLAG bitwise flags (paired, strand, duplicate, ...)
3 RNAME reference sequence name, such as chr1
4 POS 1-based position of the leftmost aligned base
5 MAPQ mapping quality
6 CIGAR alignment description
7 RNEXT reference name of the mate (= if the same)
8 PNEXT position of the mate
9 TLEN observed fragment length
10 SEQ read bases (as aligned to the forward strand)
11 QUAL base qualities

After the eleven fields come optional tags such as NM:i:1 (edit distance) or RG:Z:sample1. A coordinate system note that trips up everyone at least once: SAM and VCF positions are 1-based, while BED files and most programming languages' arrays are 0-based.

A reverse-strand read is stored reverse-complemented, so that SEQ always reads in the same direction as the reference. The qualities are reversed with it.

FLAG: eleven yes/no answers in one number

FLAG adds up powers of two, one per property:

Bit value Meaning
1 read is paired
2 mapped in a proper pair
4 read unmapped
8 mate unmapped
16 read maps to the reverse strand
32 mate maps to the reverse strand
64 first read of the pair
128 second read of the pair
256 secondary alignment
512 fails quality checks
1024 PCR or optical duplicate
2048 supplementary alignment

So a properly paired forward read that is the first mate has 1 + 2 + 32 + 64 = 99 (its mate is on the reverse strand), and the mate itself is 1 + 2 + 16 + 128 = 147. Seeing the pair 99 and 147 in a file is a good sign; you will spot them everywhere. Another normal pair is 83 and 163, where the first read is the one on the reverse strand.

CIGAR: the alignment in a sentence

CIGAR is a run-length code of operations along the read. The operations you will meet are:

Op Meaning Uses read bases? Uses reference bases?
M alignment match or mismatch yes yes
I insertion in the read yes no
D deletion from the read no yes
N skipped region (an intron in RNA-seq) no yes
S soft clip: bases kept in SEQ but not aligned yes no
H hard clip: bases removed from SEQ no no

Take 5S40M2I20M3D30M. The read has 5 unaligned bases at the start, 40 aligned positions, 2 extra read bases not in the reference, 20 more aligned, then 3 reference bases the read skips, then 30 aligned. The read length is 5+40+2+20+30 = 97 and it spans 40+20+3+30 = 93 reference bases. A subtle point: M does not mean "match". It means the base sits in an alignment column, whether or not the letters agree. The NM tag or the MD tag tells you about mismatches.

MAPQ

MAPQ is -10 * log10(P) where P is the mapper's estimate that the read's reported position is wrong. A read from a unique sequence typically gets the maximum (60 for BWA-MEM and minimap2). A read that fits equally well in two places gets 0 or close, because the mapper cannot tell the places apart. Filtering on MAPQ >= 20 (1% chance of a wrong placement) or >= 30 is common in variant calling. Different mappers scale MAPQ differently (Bowtie2 maxes out at 42), so do not compare values across tools, and remember that MAPQ is only an estimate under the mapper's model.

BAM, sorting and indexing

A text SAM file for a 30x human genome would be several hundred gigabytes. BAM is the same data in a binary, block-compressed form (BGZF), usually a third of the size or smaller. CRAM shrinks it further by storing only the differences from the reference, so it needs the exact reference to be read back.

Mappers write reads in the order they came off the sequencer. Most downstream tools need them sorted by coordinate and indexed, so you can jump straight to chr7:117,500,000-117,600,000 without reading the whole file. Typical commands, shown here read-only because they need the samtools program:

Terminal
bwa-mem2 mem -t 8 -R "@RG\tID:s1\tSM:sample1" ref.fa s1_R1.fastq.gz s1_R2.fastq.gz \
  | samtools sort -@ 4 -o s1.sorted.bam -
samtools index s1.sorted.bam
samtools flagstat s1.sorted.bam
samtools view -b -q 20 -F 4 s1.sorted.bam chr1:100000-200000 > region.bam

The -F 4 means "exclude flag 4", that is, drop unmapped reads. Then duplicate marking (samtools markdup or Picard MarkDuplicates) flags read pairs that start and end at identical positions, which are probably PCR copies of one molecule. Marking, not deleting, preserves the data while letting the variant caller ignore the copies.

Is this mapping healthy?

Look at a handful of numbers before going further:

  • Percentage mapped (flagstat): for a human sample against the right reference, you expect well above 95%. A low value suggests contamination or the wrong species.
  • Properly paired percentage and the insert-size distribution: a tight peak near the library's fragment length.
  • Mean depth and uniformity: what fraction of the target is covered at 10x, 20x, 30x.
  • Duplicate rate: a few percent is normal; very high values mean the library had too little material.
  • A look in IGV (the Integrative Genomics Viewer) at a few regions, because your eyes catch odd patterns that summaries average away.

Common misconceptions

"M in a CIGAR means the bases match." It means the base is aligned in that column. A mismatch is still part of an M run.

"MAPQ 60 means the variant call is right." MAPQ describes how sure the mapper is about the read's location, not about whether the read's bases are correct.

"Unmapped reads are junk." They are often contamination, adapters, or real sequence missing from the reference. In some projects those reads are the discovery.

Try it

The first example builds a toy seed-and-extend mapper: a k-mer index, votes for the read's offset, then a mismatch count. The second decodes FLAG and CIGAR by code. The third compares simulated coverage with the Poisson prediction.

Python A toy seed-and-extend mapper

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Decode FLAG and CIGAR

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Simulated coverage versus the Poisson model

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A read has FLAG 99. What does that tell you?

    99 = 1 + 2 + 32 + 64: paired, proper pair, mate on reverse strand, first in pair.
  2. 2. The CIGAR string 10S50M has a read length of 60. How many reference bases does it span?

    Soft-clipped bases (S) do not consume reference positions. Only the 50 M bases do.
  3. 3. What does a MAPQ of 0 usually mean?

    MAPQ is Phred-scaled confidence in the placement; 0 means essentially no confidence, typical of repeats.
  4. 4. Why are BAM files sorted and indexed?

    The index maps genomic regions to file offsets, which makes region queries fast.
  5. 5. SAM position 100 for a read means which of the following?

    SAM and VCF are 1-based; BED is 0-based.

Summary

  • Mappers build an index of the reference (FM-index, hash, minimizers), seed with exact matches, then extend with alignment.
  • A SAM record has eleven fields: name, FLAG, reference, 1-based position, MAPQ, CIGAR, mate info, length, bases, qualities.
  • FLAG is a sum of powers of two; CIGAR counts aligned (M), inserted (I), deleted (D), skipped (N) and clipped (S/H) bases; M does not imply a match.
  • MAPQ is Phred-scaled confidence in the placement; multi-mapping reads get 0.
  • Sort, index and mark duplicates, then check percent mapped, insert size, depth and duplicate rate.

Next: Variant Calling and VCF