Inferensys

Glossary

Sequence Alignment Map (SAM)

A tab-delimited text format for storing biological sequences aligned to a reference genome, containing read sequences, quality scores, and a compact idiosyncratic gapped alignment report string.
QA engineer performing AI quality assurance on laptop, test results visible, casual technical debugging session.
FILE FORMAT

What is Sequence Alignment Map (SAM)?

The Sequence Alignment Map (SAM) format is a tab-delimited text format for storing biological sequences aligned to a reference genome.

A Sequence Alignment Map (SAM) file is a tab-delimited text format for storing nucleotide sequences aligned against a reference genome. Each alignment line contains 11 mandatory fields recording the read name, bitwise flag, reference position, mapping quality, and a CIGAR string that compactly encodes the alignment's match, insertion, and deletion operations.

The format also stores the aligned read sequence and per-base quality scores, enabling variant detection and visualization. While human-readable, SAM files are voluminous; the compressed binary equivalent, BAM, provides efficient storage and random access. SAM serves as the universal interchange format between short-read aligners like BWA and downstream analysis tools.

SEQUENCE ALIGNMENT MAP

Frequently Asked Questions

Clear, technical answers to common questions about the SAM format, its structure, and its role in genomic variant discovery pipelines.

A Sequence Alignment Map (SAM) file is a tab-delimited text format for storing biological sequences aligned to a reference genome. It consists of a header section containing metadata and a reference sequence dictionary, followed by an alignment section where each line represents a single sequencing read's alignment. Each alignment record contains 11 mandatory fields including the read name (QNAME), a bitwise flag (FLAG), the reference sequence name (RNAME), the 1-based leftmost mapping position (POS), a mapping quality score (MAPQ), a compact CIGAR string describing the alignment, and the read sequence and quality scores. The format was defined by the SAM/BAM specification maintained by the SAMtools community and serves as the universal interchange format between alignment algorithms and downstream analysis tools like variant callers.

ALIGNMENT STORAGE FORMATS

SAM vs BAM vs CRAM: Format Comparison

A technical comparison of the three primary formats for storing biological sequence alignment data, from uncompressed text to reference-based compression.

FeatureSAMBAMCRAM

Full Name

Sequence Alignment Map

Binary Alignment Map

Compressed Reference-oriented Alignment Map

File Type

Tab-delimited text

Binary (BGZF compressed)

Binary (custom block compression)

Human Readable

Indexed Random Access

Compression Ratio vs SAM

1:1 (baseline)

3-5:1

10-30:1

Reference Genome Required for Decompression

Lossless Read Sequence Storage

Primary Use Case

Pipeline debugging, manual inspection

Production storage, variant calling input

Long-term archival, large cohort storage

Sequence Alignment/Map Specification

Key Features of the SAM Format

The SAM format is the universal currency of aligned sequencing data. Its design balances human readability with a compact, machine-parseable structure for storing nucleotide sequences aligned to a reference genome.

02

11 Mandatory Alignment Fields

Each alignment line consists of 11 tab-delimited fields, providing a complete description of a read's placement. Key fields include:

  • QNAME: The query template name, linking paired reads.
  • FLAG: A bitwise flag encoding properties like read paired, mapped in proper pair, and strand.
  • RNAME and POS: The reference sequence name and 1-based leftmost mapping position.
  • MAPQ: The Mapping Quality score, a Phred-scaled probability that the alignment is incorrect.
  • CIGAR: A compact idiosyncratic gapped alignment report string summarizing matches, insertions, deletions, and clipping operations.
  • SEQ and QUAL: The original read sequence and its per-base quality scores.
03

Bitwise FLAG Decoding

The second field in a SAM record is a single integer that encodes up to 12 binary properties of the read alignment. This bitwise FLAG is the sum of individual flags, each a power of 2. For example, a read that is paired (0x1), mapped in a proper pair (0x2), and is the first mate (0x40) has a FLAG of 99. Common flags include:

  • 0x4: Segment unmapped
  • 0x8: Next segment unmapped
  • 0x10: Read reverse strand
  • 0x400: Read is a PCR or optical duplicate Decoding the FLAG is essential for filtering reads by strand, mapping status, or duplicate designation.
04

CIGAR String Operations

The CIGAR string is a compact representation of the alignment's fine-grained structure. It alternates between integer lengths and single-letter operation codes. Core operations include:

  • 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 on the reference (intron)
  • S: Soft clipping (bases at read ends not aligned but present in SEQ)
  • H: Hard clipping (bases removed from SEQ) A CIGAR of 150M indicates a perfect 150bp match, while 50M1I99M describes a single-base insertion at position 51.
05

Optional Tags for Rich Metadata

Beyond the 11 mandatory fields, SAM records carry optional key-value tags in the format TAG:TYPE:VALUE. These tags store platform-specific and analysis-derived metadata. Critical tags include:

  • NM:i: Edit distance to the reference, counting mismatches and indels.
  • MD:Z: A string detailing mismatching positions and deleted reference bases, used for variant discovery.
  • AS:i: Alignment score from the aligner.
  • RG:Z: The read group identifier, linking back to the header's @RG record.
  • X? and Y?: Reserved prefixes for end-user or tool-specific annotations.
06

Paired-End Mate Coordination

SAM natively handles paired-end sequencing by storing each mate on a separate line, linked by a shared QNAME. The FLAG field distinguishes the first mate (0x40) from the second (0x80). Fields RNEXT, PNEXT, and TLEN describe the mate's alignment. RNEXT is the mate's reference name (= if same chromosome), PNEXT is its 1-based position, and TLEN is the observed template length—the signed distance between the outermost mapped bases. A negative TLEN indicates the second mate maps to the reverse strand, enabling structural variant detection through insert size discordance.

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.