What the columns in a BAM file mean
A BAM file is the compressed binary form of a SAM (Sequence Alignment/Map) file, and each record in it describes one read and where that read landed on a reference genome. Every record carries eleven mandatory columns in a fixed order.
- QNAME
- FLAG
- RNAME
- POS
- MAPQ
- CIGAR
- RNEXT
- PNEXT
- TLEN
- SEQ
- QUAL
After those come any number of optional tags in TAG:TYPE:VALUE form, which is where aligners stash edit distance, alignment score, read group, and barcodes. You cannot read the binary directly with less, so the practical entry point is samtools view, which decodes a BAM record into the tab-separated SAM text those eleven columns describe 1.
If you have had whole-genome sequencing done, the BAM (or its CRAM cousin) is the central artifact. The variant calls you receive are downstream summaries. The BAM is the evidence.
The eleven mandatory columns
Start by looking at real records. The header holds the reference sequence dictionary and the program history, and the alignments follow.
samtools view -H sample.bam | head -5
samtools view sample.bam chr7:117,559,590-117,559,700 | head -3
A single line might read like this, abbreviated:
A00123:45:HXXX:1:1101:9000:1234 99 chr7 117559590 60 151M = 117559811 372 GATC... FFF:... NM:i:1 MD:Z:75A75 AS:i:146 MQ:i:60 RG:Z:lib1
QNAME is the read name assigned by the sequencer. For paired-end data both mates share the same QNAME, which is how tools pair them. FLAG is a bitwise integer encoding up to twelve properties at once. The bits you will use most are 0x1 for paired and 0x2 for mapped in a proper pair. Then come 0x4 for unmapped, 0x10 for reverse strand and 0x40 for first in pair. The rest are 0x80 for second in pair, 0x100 for secondary alignment, 0x400 for PCR or optical duplicate and 0x800 for supplementary alignment. Run samtools flags 99 to decode any value. A value of 99 means paired, proper pair, mate reverse and first in pair.
RNAME is the reference sequence name, matching an @SQ line in the header. POS is the 1-based leftmost coordinate of the first aligned base in the text form. Inside the binary it is stored 0-based, which is a frequent source of off-by-one bugs when you write your own parser. Soft-clipped bases at the start of a read are not counted in POS.
MAPQ is a Phred-scaled estimate of the probability that the alignment position is wrong, so 60 means roughly one in a million and 0 means the aligner found equally good positions elsewhere. The ceiling is aligner-specific: BWA-MEM tops out at 60, Bowtie2 at 42, and 255 is reserved for “unavailable”. MAPQ 0 reads cluster in repeats and transposable elements, where read length and insert size determine whether a locus is mappable at all 2.
CIGAR describes how the read aligns base by base. M is an aligned base that may match or mismatch, I is an insertion into the read, and D is a deletion. S is soft clipping, where bases are present in SEQ but not aligned. H is hard clipping, where bases are removed from SEQ, and N is a skipped region. In RNA-seq, N is the intron: a CIGAR like 62M8410N89M is a read spanning a splice junction. Some pipelines emit = and X instead of M to separate matches from mismatches.
RNEXT and PNEXT give the reference name and position of the mate, with = meaning the same chromosome. TLEN is the observed template length, signed, positive for the leftmost mate of the pair. Its absolute value is your insert size, and its distribution is one of the fastest quality checks you can run on a library 3. SEQ is the read sequence on the forward reference strand, so a read with the 0x10 flag set has been reverse-complemented relative to the raw FASTQ. QUAL holds per-base Phred qualities as ASCII characters offset by 33, or a single * if absent.
The optional tags that carry the real information
The eleven columns tell you where a read sits. The tags usually tell you how much to trust it. NM:i is the edit distance to the reference. MD:Z encodes the reference bases at mismatch positions, letting you reconstruct mismatches without loading the FASTA. AS:i is the alignment score and XS:i (for BWA) is the score of the next-best alignment. A small gap between them explains a low MAPQ. SA:Z lists supplementary alignments for split reads, which is the primary signal for structural variant callers. RG:Z links the read to a read group in the header, which is what duplicate marking and base quality recalibration key on. Single-cell and UMI pipelines add CB:Z and UB:Z for cell and molecular barcodes.
How the binary is laid out
BAM is not plain gzip. The format uses BGZF, a blocked gzip variant where each block holds at most 65,536 bytes of uncompressed data and can be decompressed independently. That is what makes random access possible: the .bai or .csi index maps genomic intervals to 64-bit virtual offsets combining a block start and an offset within the block. Without the index, samtools view sample.bam chr1:1000-2000 cannot work.
Inside, the file begins with the magic bytes BAM\1. The header text follows, and then the reference dictionary. Each alignment record is a length-prefixed struct with fixed-width fields. After those fields come the read name and the CIGAR packed as 32-bit integers in the form op_len << 4 | op. Then come the sequence packed at 4 bits per base, the qualities at one byte per base, and the tags. You rarely need to parse this yourself. htslib, pysam, and the BamTools C++ API all handle it 4. Knowing the layout helps when you are debugging a truncated file or estimating storage, which for whole-genome data is a real line item 5.
CRAM goes further by storing bases as differences against the reference genome. It is typically 30 to 60 percent smaller than BAM at the same losslessness, at the cost of requiring the exact reference FASTA to decode. Keep the MD5 sums from the @SQ lines if you archive CRAM.
Reading, filtering, and viewing your own BAM
Three commands cover most of what you need day to day.
samtools flagstat sample.bam
samtools stats sample.bam > stats.txt && plot-bamstats -p out/ stats.txt
samtools view -c -f 0x400 sample.bam # duplicate reads
samtools view -b -q 20 -F 0x904 in.bam > pass.bam
That last filter is the one we reach for first. -q 20 drops ambiguous placements. -F 0x904 excludes unmapped reads, secondary alignments, and supplementary alignments. For region-specific rules, where you want high-MAPQ reads genome-wide but every read over a panel of genes, VariantBam expresses that in a rules file and avoids writing three separate BAMs 6.
For visual inspection, load the BAM plus its index into IGV and turn on “view as pairs” and soft-clip display. samtools tview works over SSH when you cannot move the file. Purpose-built viewers such as BamView and Bambino render pileups with mismatch and coverage tracks and are useful when you want a second opinion on a locus 7 8. Looking at a locus before believing a variant call is a habit worth keeping.
One boundary: nothing in a BAM interprets itself clinically. Coverage, MAPQ, and strand balance tell you whether a call is technically sound. Whether a confirmed variant means anything for your health is a question for a clinical geneticist or your physician, working from a validated clinical report.
Questions people also ask
What does BAM mean?
BAM stands for Binary Alignment Map. It is the lossless binary encoding of SAM, compressed in BGZF blocks so that regions can be retrieved without reading the whole file.
What is the difference between a SAM file and a BAM file?
They hold identical information. SAM is tab-separated text you can read with standard Unix tools, and BAM is the binary form, roughly four to five times smaller and indexable for random access. Convert with samtools view -b in.sam > out.bam.
What is the difference between BAM and CRAM?
CRAM stores read bases relative to a reference genome rather than storing them outright, which cuts file size substantially. The tradeoff is that decoding requires the exact reference used at compression time, and CRAM supports optional lossy quality binning that BAM does not.
How do I read a BAM file?
Use samtools view to stream records as text and samtools view -H for the header. In Python, pysam.AlignmentFile("sample.bam", "rb") gives you iterable records with named attributes for each field.
How do I visualize a BAM file?
Index it with samtools index, then load the BAM and .bai into IGV and navigate to a locus. For terminal-only access, samtools tview -p chr1:1000000 sample.bam ref.fa renders a pileup in place.
Footnotes
-
Peter Robinson, Peter Hansen. SAM/BAM Format. Computational Exome and Genome Analysis, 2017. https://doi.org/10.1201/9781315154770-9 ↩
-
Corinne E. Sexton, Mira V. Han. Paired-end mappability of transposable elements in the human genome. Mobile DNA, 2019. https://doi.org/10.1186/s13100-019-0172-5 ↩
-
Peter N. Robinson, Rosario M. Piro, Marten Jäger. Alignment Data: Quality Control. Computational Exome and Genome Analysis, 2017. https://doi.org/10.1201/9781315154770-11 ↩
-
Derek W. Barnett, Erik K. Garrison, Aaron R. Quinlan, et al. BamTools: a C++ API and toolkit for analyzing and managing BAM files. Bioinformatics, 2011. https://doi.org/10.1093/bioinformatics/btr174 ↩
-
Andrea Sboner, Xinmeng Mu, Dov Greenbaum, et al. The real cost of sequencing: higher than you think!. Genome Biology, 2011. https://doi.org/10.1186/gb-2011-12-8-125 ↩
-
Jeremiah Wala, Cheng-Zhong Zhang, Matthew Meyerson, et al. VariantBam: filtering and profiling of next-generational sequencing data using region-specific rules. Bioinformatics, 2016. https://doi.org/10.1093/bioinformatics/btw111 ↩
-
T. Carver, S. R. Harris, T. D. Otto, et al. BamView: visualizing and interpretation of next-generation sequencing read alignments. Briefings in Bioinformatics, 2012. https://doi.org/10.1093/bib/bbr073 ↩
-
Michael N. Edmonson, Jinghui Zhang, Chunhua Yan, et al. Bambino: a variant detector and alignment viewer for next-generation sequencing data in the SAM/BAM format. Bioinformatics, 2011. https://doi.org/10.1093/bioinformatics/btr032 ↩
Pothos builds longitudinal molecular profiles of individuals: whole-genome sequencing, single-cell RNA sequencing, and plasma proteomics, delivered as a detailed report, every data file, and stored cells. Build your profile.