Reading a BAM File: Flags, MAPQ, CIGAR & Coverage

A BAM file looks like a wall of numbers. This lesson makes it readable: the columns that matter, what the FLAG, MAPQ, and CIGAR fields mean, and how to check coverage so you know what to trust.

🟢 Beginner ⏱️ ~30 min 💻 Command line

Before you start

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:

ColumnWhat it tells you
QNAMEThe read's name (same name on both mates of a pair)
FLAGA code packing yes/no facts about the read (see below)
RNAME, POSWhich chromosome, and the 1-based position where the read starts
MAPQHow confident the aligner is that the read is in the right place
CIGARHow 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:

LetterMeaning
Maligned to the reference (a match or a mismatch)
Iinsertion: bases in the read that are not in the reference
Ddeletion: reference bases missing from the read
Ssoft-clip: read ends that did not align (often low quality)
Nskipped 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.

reference depth here = 4
Coverage at a position is the number of reads covering it. More reads in agreement means a more trustworthy call.
# per-position depth, and a quick coverage summary per chromosome
samtools depth sample.bam | head
samtools coverage sample.bam

Check your understanding

What does the FLAG field of a read encode?
Right. The FLAG is a bitwise code; samtools flags decodes it into plain English.
In a CIGAR string, what does N usually mean in RNA-seq?
Correct. N is a skipped reference region, which in RNA-seq is a read crossing an intron between two exons.
A read has a MAPQ of 0. What does that most likely mean?
Right. Low MAPQ flags ambiguous placement (often a repeat). Many pipelines filter these out with something like -q 20.
A read's CIGAR is 10S90M. What happened?
Correct. S is a soft-clip: those bases are present in the read but not aligned, usually low-quality ends.
You see a variant supported by reads at a position with a depth of 1. Should you trust it?
Exactly. Coverage is how you judge confidence: one read is noise, many agreeing reads is evidence.
Next in Track 2

Variant calling: from FASTQ to VCF →