A BAM file (Binary Alignment Map) is the compressed, binary representation of sequence alignment data stored in a SAM file (Sequence Alignment Map). It retains the same information—read sequences, quality scores, and a CIGAR string detailing alignment operations—but uses BGZF compression to dramatically reduce storage footprint while preserving the ability to index and query specific genomic regions without decompressing the entire file.
Glossary
BAM File

What is a BAM File?
The compressed binary version of the Sequence Alignment Map format, enabling efficient storage and high-speed random access to aligned sequencing reads.
Random access is achieved through a companion BAI index file, which maps genomic coordinates to virtual file offsets within the compressed blocks. This architecture allows variant callers like DeepVariant and genome browsers to rapidly retrieve all reads overlapping a locus of interest, making BAM the universal interchange format for high-throughput sequencing data in production bioinformatics pipelines.
Key Features of the BAM Format
The BAM format is the compressed, indexable binary representation of aligned sequencing data, enabling high-performance genomic analysis at scale.
Binary Compression Efficiency
BAM files achieve 60-80% size reduction compared to their SAM text equivalents through BGZF block compression. This block-level gzip compression allows each block to be decompressed independently, enabling random access without decompressing the entire file. The compression preserves all original alignment data, including read sequences, quality scores, and CIGAR strings, while dramatically reducing storage costs for large-scale sequencing projects.
Indexed Random Access
A companion BAI index file enables high-speed random access to any genomic region without scanning the entire BAM. The index stores binned reference coordinates mapping genomic positions to virtual file offsets. Tools like samtools view chr1:1000000-2000000 use this index to jump directly to reads overlapping the requested locus, making BAM the standard format for interactive genome browsers and targeted variant inspection.
Flag-Based Read Filtering
Each aligned read carries a bitwise FLAG field encoding 12 boolean properties in a single integer:
- 0x1: Read is paired
- 0x2: Proper pair alignment
- 0x4: Read unmapped
- 0x8: Mate unmapped
- 0x40: First in pair
- 0x80: Second in pair
- 0x100: Secondary alignment
- 0x200: Fails quality checks
- 0x400: PCR duplicate
- 0x800: Supplementary alignment
Bitwise operations enable rapid filtering of reads by alignment status, strand, or duplicate state.
Auxiliary Tag Metadata
BAM supports extensible per-read metadata through optional auxiliary tags stored as key:type:value triplets. Common tags include:
- NM: Edit distance to reference
- MD: Mismatch position string
- AS: Alignment score
- XS: Suboptimal alignment score
- RG: Read group identifier
- BC: Barcode sequence
These tags carry quality metrics, alignment scores, and sample multiplexing information critical for downstream variant calling and deduplication.
Read Group and Sample Multiplexing
BAM's read group header structure organizes reads by sample, library, platform unit, and sequencing run. Each read group contains metadata fields:
- ID: Unique group identifier
- SM: Sample name
- LB: Library preparation
- PL: Platform/technology
- PU: Platform unit (flowcell lane)
This structure enables multi-sample joint analysis, batch effect correction, and traceability from variant calls back to original sequencing runs.
Coordinate-Sorted Ordering
BAM files are typically coordinate-sorted by reference sequence name and position, enabling efficient streaming algorithms for:
- Pileup generation for variant callers like DeepVariant
- Coverage depth calculation across contiguous regions
- Merging multiple BAMs from different libraries
- Duplicate marking using positional clustering
Coordinate sorting is a prerequisite for most downstream analysis tools and is enforced by the SAM specification for indexed files.
Frequently Asked Questions
Clear, technical answers to the most common questions about the Binary Alignment Map format, its structure, and its role in high-throughput genomic analysis pipelines.
A BAM file (Binary Alignment Map) is the compressed, binary representation of sequence alignment data, serving as the industry-standard format for storing aligned sequencing reads against a reference genome. It is generated by converting the human-readable SAM (Sequence Alignment Map) format using the samtools view -bS command or directly by aligners like BWA-MEM and STAR. The binary structure employs BGZF (Blocked GNU Zip Format) compression, which allows the file to be compressed while maintaining indexability. A companion BAI (BAM index) file enables high-speed random access, allowing tools to rapidly query reads overlapping a specific genomic locus without decompressing the entire file. This architecture is critical for performance in variant calling pipelines, where algorithms like DeepVariant need to fetch pileup data for millions of candidate loci efficiently.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
BAM vs. SAM vs. CRAM
A technical comparison of the three primary file formats used for storing aligned sequencing reads, highlighting differences in compression, random access, and data preservation.
| Feature | BAM | SAM | CRAM |
|---|---|---|---|
Full Name | Binary Alignment Map | Sequence Alignment Map | Compressed Reference-oriented Alignment Map |
File Type | Binary (BGZF compressed) | Plain text (tab-delimited) | Binary (custom codec) |
Human Readable | |||
Storage Footprint (vs. SAM) | ~10-15% of original size | 100% (baseline) | ~3-5% of original size |
Random Access | |||
Reference Genome Dependency | Optional (for indexing only) | None | Mandatory (required for compression) |
Lossless Data Preservation | |||
Streaming Support |
Related Terms
Understanding the BAM file format requires familiarity with its uncompressed predecessor, the indexing mechanism that enables fast random access, and the core alignment data structures it contains.
CIGAR String Encoding
A compact, idiosyncratic string stored within each alignment record that summarizes the sequence of operations required to align a read to the reference. Each operation is represented by a length and a single-letter code:
- M: Alignment match (can be a sequence match or mismatch)
- I: Insertion in the read relative to the reference
- D: Deletion from the reference
- N: Skipped region (intron)
- S: Soft-clipped bases (not part of the alignment)
- H: Hard-clipped bases (removed from the read)
The CIGAR string
150Mindicates a perfect 150-base alignment, while20S130Mindicates 20 soft-clipped bases followed by 130 matching bases.
Mapping Quality (MAPQ)
A Phred-scaled probability stored in every BAM record indicating the confidence that the read is correctly placed at its reported genomic position. A MAPQ of 30 means a 1 in 1,000 chance of incorrect mapping; a MAPQ of 0 indicates a multi-mapping read with no unique best placement. This field is critical for variant calling pipelines, where reads with low MAPQ are typically filtered out to prevent false positive variant calls caused by mismapped reads in repetitive regions.
Flag Field Bitmask
A 12-bit integer stored in each BAM record that encodes multiple boolean properties of the alignment in a single value. Each bit represents a specific flag:
- 0x1: Read is paired
- 0x2: Read mapped in proper pair
- 0x4: Read unmapped
- 0x10: Read on reverse strand
- 0x40: First in pair
- 0x80: Second in pair
- 0x100: Secondary alignment
- 0x400: Read fails quality checks
Tools like
samtools flagstatdecode these bitmasks to generate alignment summary statistics.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us