Skip to main content
v2026.11,772 entries · CC-BY 4.0

What Is a BAM File? Format, Structure, and Tools

A BAM file is the compressed, indexed binary counterpart to a SAM alignment file. What it is, how it is structured, and the samtools commands used to work with it.

Written and maintained by CASRAI Editorial Board

Last updated

What a BAM file is

A BAM (Binary Alignment Map) file is the compressed, binary counterpart to a SAM (Sequence Alignment Map) file. Both formats store the same information — a set of sequencing reads and, for each one, where and how it aligns to a reference genome — but SAM is a human-readable, tab-delimited text format, and BAM is its binary, compressed serialization. Aligners such as BWA, Bowtie2, HISAT2 and STAR write SAM as their native output; a BAM file is what that SAM gets converted into once a pipeline needs to store, sort, index or move the data efficiently.

The two formats are defined by a single specification, maintained by the HTS-specs working group (the group behind samtools): the SAM/BAM format specification. Every field that appears in a SAM file has an exact binary equivalent in BAM, so no alignment information is lost in the conversion — BAM is not a summary or a subset of SAM, it is the same data encoded differently.

Why alignments are stored as BAM instead of plain SAM

Genome and exome sequencing runs routinely produce tens to hundreds of millions of aligned reads. Keeping that volume of data as plain text is expensive on three fronts, and BAM addresses each one directly:

  • Size. BAM uses BGZF (Blocked GNU Zip Format) compression — ordinary gzip compression applied in independent, fixed-size blocks rather than as one continuous stream. A whole-genome SAM file that runs to tens of gigabytes as text typically compresses to a fraction of that size as BAM.
  • Sortability. A BAM file is normally sorted by leftmost mapping coordinate (reference sequence, then position), which is what every downstream tool — variant callers, coverage calculators, duplicate markers — expects as input. Coordinate order is also what makes indexing possible.
  • Indexable random access. Because BGZF compresses in independent blocks, a separate index file can record the compressed byte offset of each block. That lets a tool jump directly to the reads overlapping a given genomic region — say, one exon, or one gene — without decompressing or scanning the rest of the file. Plain-text SAM has no equivalent capability; every query is a linear scan.

This is also why BAM, not SAM, is the format almost every aligner, variant caller and genome browser expects as input in practice: SAM’s readability is useful for spot-checking a handful of records, but every stage of a real pipeline downstream of alignment operates on sorted, indexed BAM.

Basic structure: header plus alignment records

A BAM file (and the SAM file it corresponds to) has exactly two parts, in this order:

1. The header

The header is a set of tab-delimited metadata lines, each beginning with an @ tag. The ones that appear in essentially every real file are:

  • @HD — file-level metadata: the SAM specification version and the sort order (SO:coordinate, SO:queryname, or SO:unsorted).
  • @SQ — one line per reference sequence the reads were aligned against, giving its name (SN) and length (LN). A human whole-genome BAM has one @SQ line per chromosome/contig.
  • @RG — read group definitions: sample ID, library, sequencing platform, and other metadata tying a batch of reads back to its sequencing run. Variant callers such as GATK require a valid @RG line.
  • @PG — a record of each program that has processed the file, in order (aligner, sorter, deduplicator), forming an audit trail of the file’s provenance.

2. Alignment records

Every line after the header is one alignment record: one sequencing read (or one part of a split/chimeric read) mapped to a position in the reference, or flagged as unmapped. Each record has 11 mandatory tab-delimited fields, in fixed order:

  1. QNAME — the read/template name.
  2. FLAG — a bitwise integer encoding read properties (paired, mapped, reverse-strand, secondary alignment, duplicate, and so on).
  3. RNAME — the reference sequence (chromosome/contig) the read aligns to.
  4. POS — the 1-based leftmost mapping position on that reference.
  5. MAPQ — mapping quality: a Phred-scaled estimate (typically 0–60, aligner-dependent) of the probability the mapping position is wrong. Higher is more confident; a MAPQ of 0 or 1 usually means the read mapped equally well to more than one location.
  6. CIGAR — a compact string describing how the read aligns base-by-base against the reference, using operation codes such as M (alignment match, which includes mismatches), I/D (insertion/deletion relative to the reference), N (a skipped reference region, e.g. an intron in RNA-seq), S/H (soft/hard clipping), and =/X (exact match/mismatch, an optional finer-grained alternative to plain M). A CIGAR of 76M means 76 consecutive bases align to the reference with no gaps.
  7. RNEXT — the reference name of the read’s mate, for paired-end data.
  8. PNEXT — the mate’s mapping position.
  9. TLEN — the observed template (insert) length spanning both mates.
  10. SEQ — the read’s base-call sequence.
  11. QUAL — the per-base quality scores, in the same Phred-scaled encoding used in FASTQ.

Beyond these 11 fixed fields, a record can carry any number of optional tab-delimited tags in a TAG:TYPE:VALUE format — for example NM:i:2 (edit distance to the reference) or MD:Z:... (a string encoding mismatching positions). These tags are how aligners and downstream tools attach extra per-read annotation without changing the core format.

Working with BAM files: samtools

samtools is the reference implementation for reading, writing and manipulating SAM/BAM/CRAM files, built on the same htslib library that defines the format. Three subcommands cover most day-to-day BAM handling:

samtools view — convert and inspect

# SAM -> BAM (compress)
samtools view -b -o aligned.bam aligned.sam

# BAM -> SAM (human-readable, for inspection)
samtools view -h aligned.bam > aligned.sam

# View only reads mapped to a region of a coordinate-sorted, indexed BAM
samtools view aligned.bam chr7:140000-150000

The -b flag requests BAM output; without it, samtools view writes SAM text. The -h flag includes the header, which is normally what you want when converting back to SAM for a human to read.

samtools sort — put records in coordinate order

samtools sort -o aligned.sorted.bam aligned.bam

Sorting is a prerequisite for indexing and for almost every downstream tool. Some pipelines instead sort by read name (samtools sort -n) as an intermediate step — for example before running a tool that needs both mates of a pair adjacent to each other — but a name-sorted BAM cannot be indexed with samtools index; it has to be coordinate-sorted first.

samtools index — build the .bai companion file

samtools index aligned.sorted.bam
# produces aligned.sorted.bam.bai

This reads the coordinate-sorted BAM and writes a .bai index alongside it, recording the compressed file offset of the reads at each genomic bin. The index file is only valid for the exact BAM it was built from — if the BAM is re-sorted, re-headered, or otherwise rewritten, the old .bai is stale and must be regenerated. Tools that need random access to a specific region (a genome browser like IGV, a variant caller restricted to an interval, samtools view with a region argument as shown above) require this index to be present; without it, they either fail or fall back to a full linear scan.

A few other samtools subcommands worth knowing: samtools flagstat summarizes how many reads are mapped, paired, duplicated, and so on; samtools stats produces a fuller QC report; and samtools merge combines multiple sorted BAMs (for example, per-lane BAMs from the same sample) into one.

Frequently asked questions

Is a BAM file the same information as a SAM file?

Yes. SAM and BAM are two encodings of identical data — SAM as tab-delimited plain text, BAM as its BGZF-compressed binary equivalent. Converting SAM to BAM and back with samtools produces the same alignment records; nothing is discarded in either direction.

Can I open a BAM file in a text editor?

Not usefully. A BAM file is compressed binary, not text, so it will not display as readable content. Convert it to SAM first (samtools view -h in.bam > out.sam) to read it directly, or use a genome browser such as IGV to inspect it visually.

Do I need to sort a BAM file before indexing it?

Yes. samtools index requires the BAM to be coordinate-sorted (samtools sort with no -n flag). A name-sorted or unsorted BAM will fail to index.

What is CRAM, and how does it relate to BAM?

CRAM is a newer, reference-based alignment format defined in the same HTS-specs family. Rather than storing each read’s full sequence, CRAM can store only the differences from the reference genome, which typically compresses further than BAM at the cost of needing the matching reference sequence available to decode it. samtools reads and writes CRAM alongside SAM/BAM using the same commands (samtools view -C for CRAM output).

What does a MAPQ of 0 mean?

It means the aligner could not distinguish this position from at least one other equally good mapping location for the read — the read maps ambiguously, often because it falls in a repetitive or duplicated region of the genome. Many downstream tools filter out MAPQ 0 reads, or treat them with reduced confidence, for exactly this reason.

Related reading

For the format that comes before alignment, see FASTQ Format Explained and Phred Quality Scores. For what happens after alignment, see RNA-seq: Experimental Design Through Analysis, Differential Gene Expression Analysis and ChIP-seq: Antibody Choice, Controls and Quality Metrics. For depositing aligned or raw sequence data in a public archive, see Submitting Sequence Data to NCBI SRA and the Sequence Read Archive (SRA) dictionary entry.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · included with Regulatory Radar

Ask about What Is a BAM File? Format, Structure, and Tools

Ask CASRAI answers research-administration questions and cites the passages behind every claim — and says so when the corpus does not cover something, instead of guessing. It comes with a Regulatory Radar subscription at $29 a month, alongside the daily digest of regulatory changes and the dashboard of what changed.

150 questions a day, on this site, over the API, or inside your own tools through the CASRAI MCP server.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Regulatory Radar

Stop finding out after the fact

$29/month, cancel anytime. Daily digest updates from our analysis, a dashboard holding the same items, and a cited assistant for everything they raise.

  • Federal Register, Federal Register+, Grants.gov, Regulations.gov, NSF News, UKRI, plus CASRAI’s own published content.
  • 72,264 indexed passages, and every answer cites the ones it drew on.