Inferensys

Glossary

BAM File

The compressed binary version of the Sequence Alignment Map (SAM) format, enabling efficient storage and high-speed random access to aligned sequencing reads.
Developer working on RAG retrieval system, document chunks visible on screen, technical workspace with code editor.
BINARY ALIGNMENT MAP

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.

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.

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.

Binary Alignment Essentials

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.

01

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.

02

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.

03

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.

04

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.

05

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.

06

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.

BAM FILE ESSENTIALS

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.

ALIGNMENT FORMAT COMPARISON

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.

FeatureBAMSAMCRAM

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

Prasad Kumkar

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.