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.

Practise right here

Every command box on this page has a Run button that sends it to a real Linux terminal in your browser, with a practice sample.bam already loaded (simulated single-end reads on a 10,000-base chromosome, so flagstat shows no paired reads). No install, no account.

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 aligned part of 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" is an alternative placement for a read that could map to several places. "Supplementary" is different: it is one piece of a split read whose parts align to different places, for example across a structural-variant breakpoint. (BWA-MEM normally reports a read that fits several places as one record with MAPQ 0, not as secondary records.) 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)
Hhard-clip: read ends removed from the stored sequence (common in split reads)

So 100M means all 100 bases aligned with no gaps or clipping (mismatches are still possible; the NM tag counts them); 50M2D48M means 50 aligned, then 2 reference bases are deleted, then 48 more aligned; 10S90M means the first 10 bases were soft-clipped (kept in the read but not aligned) 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 good-quality reads agreeing is convincing, as long as they are not duplicates and map confidently (good MAPQ). 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 is split: its first half aligns to chromosome 2 and its second half to chromosome 9. How does the BAM show the second piece?
Right. Pieces of one split (chimeric) read are supplementary alignments, and they can point to structural variants such as a translocation. Secondary alignments are something else: alternative placements of a whole read that fits several places.
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.

Sources & further reading

  1. Li H, Handsaker B, Wysoker A, et al. The Sequence Alignment/Map format and SAMtools. Bioinformatics 25: 2078-2079, 2009. (FLAG, MAPQ and CIGAR) doi:10.1093/bioinformatics/btp352
  2. Danecek P, Bonfield JK, Liddle J, et al. Twelve years of SAMtools and BCFtools. GigaScience 10: giab008, 2021. doi:10.1093/gigascience/giab008

The official SAM/BAM format specification is the SAMv1 spec.
Last reviewed: October 2026.

Next in Track 2

Variant calling: from FASTQ to VCF →

Practice terminal powered by sandbox.bio (runs in your browser; your commands stay on your computer).