Before you start
- You should have a sorted, indexed BAM ready; if not, work through the samtools survival kit first.
- Any term new? The Glossary has it.
Learning objectives
By the end of this lesson you will be able to: read the key columns of a SAM/BAM record, understand what the FLAG, MAPQ, and CIGAR fields tell you, and check the coverage (depth) at a position so you know whether to trust what you see.
Making a BAM file readable
You aligned your reads and got a BAM file. Open it with samtools view and it looks like a wall of numbers and letters. It is not random: each line is one read, and a handful of columns tell you everything you need. Learn those columns and a BAM stops being scary.
1The columns that matter
Every aligned read is one row. These are the fields you will actually read:
| Column | What it tells you |
|---|---|
| QNAME | The read's name (same name on both mates of a pair) |
| FLAG | A code packing yes/no facts about the read (see below) |
| RNAME, POS | Which chromosome, and the 1-based position where the read starts |
| MAPQ | How confident the aligner is that the read is in the right place |
| CIGAR | How the read lines up: matches, insertions, deletions, clips |
# peek at a few reads (the header is hidden without -h)
samtools view sample.bam | head
2FLAG: a yes/no checklist packed into one number
The FLAG looks like a meaningless number (like 99 or 147), but it is really a set of yes/no switches added together: is the read paired? did the pair map properly? is it unmapped? is it on the reverse strand? is it a duplicate? You rarely decode it by hand, you ask samtools.
# explain what a flag value means in plain English samtools flags 99 # -> PAIRED,PROPER_PAIR,MREVERSE,READ1 # count reads, broken down by flag (mapped, duplicates, etc.) samtools flagstat sample.bam
The flags you will meet mostThe useful handful
"Properly paired" means both mates mapped the way the aligner expected, a good sign. "Unmapped" reads did not align. "Duplicate" marks PCR/optical copies (from the markdup step). "Secondary/supplementary" are extra alignments for a read that maps in more than one place. flagstat totals all of these for you.
3MAPQ: can you trust where the read landed?
MAPQ is the mapping quality: higher means the aligner is more sure the read belongs exactly here. A MAPQ near 0 is a warning, it usually means the read maps equally well to several places (a repeat), so you cannot trust its position. Many analyses filter to keep only confidently placed reads, for example samtools view -q 20.
4CIGAR: how the read lines up
The CIGAR string is a tiny recipe for how the read matches the reference. The common letters:
| Letter | Meaning |
|---|---|
| M | aligned to the reference (a match or a mismatch) |
| I | insertion: bases in the read that are not in the reference |
| D | deletion: reference bases missing from the read |
| S | soft-clip: read ends that did not align (often low quality) |
| N | skipped region (in RNA-seq, a read spanning an intron) |
So 100M means all 100 bases aligned cleanly; 50M2D48M means 50 aligned, then 2 reference bases are deleted, then 48 more aligned; 10S90M means the first 10 bases were clipped off and 90 aligned.
5Coverage: how many reads back up a position
Coverage (or depth) is simply how many reads stack over a given position. It is the number that tells you whether to believe a variant: one read suggesting a change is noise; thirty reads agreeing is convincing. Always sanity-check the depth before trusting a call.
# per-position depth, and a quick coverage summary per chromosome
samtools depth sample.bam | head
samtools coverage sample.bam
Check your understanding
samtools flags decodes it into plain English.N usually mean in RNA-seq?-q 20.10S90M. What happened?