Inferensys

Glossary

CIGAR String Encoding

A compact representation within alignment files that summarizes the sequence of match, insertion, deletion, and clipping operations required to align a read to a reference genome.
Stylish WeWork-like workspace with hot desks and document wall, professional searching through enterprise knowledge base on a mounted ultrawide display, warm industrial pendants overhead.
COMPACT ALIGNMENT REPRESENTATION

What is CIGAR String Encoding?

A CIGAR (Compact Idiosyncratic Gapped Alignment Report) string is a concise, standardized notation within SAM and BAM files that summarizes the sequence of match, insertion, deletion, and clipping operations required to align a sequencing read to a reference genome.

The CIGAR string encodes the alignment trace by pairing integer lengths with single-letter operation codes (e.g., M for match/mismatch, I for insertion, D for deletion). This compact representation allows downstream variant calling tools to rapidly reconstruct the read-to-reference mapping without storing the full pairwise alignment, enabling efficient storage and high-speed parsing of massive genomic datasets.

Precise CIGAR interpretation is critical for accurate variant allele fraction calculation and indel discovery. Operations like S (soft clipping) and H (hard clipping) distinguish unaligned read segments from true deletions, preventing false positive variant calls at read edges and ensuring the correct calculation of coverage depth at candidate loci.

CIGAR STRING ENCODING

Core Operations and Properties

The CIGAR string is a compact, operational summary of how a sequencing read aligns to a reference genome. It encodes the precise sequence of matches, insertions, deletions, and clipping events, serving as the fundamental data structure for variant calling and alignment visualization.

01

Core CIGAR Operations

The CIGAR string uses a defined set of single-character operations to describe an alignment. Each operation is preceded by a length integer, forming a compact run-length encoding.

  • M (Match/Mismatch): An alignment match, which can represent a sequence match or a mismatch. The read base is aligned to the reference.
  • I (Insertion): Bases present in the read but absent from the reference genome.
  • D (Deletion): Bases present in the reference but absent from the read. This operation consumes reference bases but no read bases.
  • N (Skipped Region): Represents an intron or other large gap in the read's alignment to the reference, typically used in spliced alignments.
  • S (Soft Clipping): Read bases at the start or end that are not aligned but are retained in the Sequence Alignment Map (SAM) record's sequence field.
  • H (Hard Clipping): Read bases that are not aligned and are also removed from the SAM record's sequence field.
02

Parsing the String: `100M1I50M1D25M`

A CIGAR string is parsed by iterating through pairs of numbers and operation characters. The example 100M1I50M1D25M describes a 176-base read with a complex alignment.

  • 100M: The first 100 bases of the read align to the reference with matches or mismatches.
  • 1I: The next base in the read is an insertion relative to the reference. It consumes 1 read base and 0 reference bases.
  • 50M: The subsequent 50 read bases align to the reference.
  • 1D: A single base is deleted from the reference. It consumes 1 reference base and 0 read bases.
  • 25M: The final 25 read bases align to the reference.

This compact representation allows tools to rapidly reconstruct the exact gapped alignment without storing the full pairwise alignment matrix.

176 bp
Total Read Length
175 bp
Reference Span
03

Consumption Rules and Length Calculation

A critical property of the CIGAR string is the distinction between read-consuming and reference-consuming operations. This distinction is essential for calculating alignment length and mapping coordinates.

  • Read-Consuming Operations: M, I, S, and =/X (sequence match/mismatch) advance the position in the read sequence.
  • Reference-Consuming Operations: M, D, N, and =/X advance the position on the reference genome.
  • Non-Consuming Operations: H and P (padding) consume neither read nor reference.

The sum of lengths for read-consuming operations must equal the length of the SEQ field in the SAM record. The sum of reference-consuming operations defines the total span of the reference covered by the alignment, which is used to calculate the mapping quality and identify structural variants.

04

Extended CIGAR for Long Reads

The original CIGAR specification has been extended to support the complex alignment features of long-read sequencing technologies like PacBio and Oxford Nanopore.

  • = (Sequence Match): An exact base-for-base match, providing finer resolution than the generic M operator.
  • X (Sequence Mismatch): An explicit base substitution, allowing variant callers to immediately identify single nucleotide polymorphisms from the CIGAR string alone.
  • P (Padding): A silent deletion from a padded reference, used in multiple sequence alignments where the reference has been artificially extended.

Using = and X instead of M transforms the CIGAR string from a coarse alignment summary into a precise edit transcript, enabling rapid variant identification without re-examining the base-level alignments.

05

CIGAR in Variant Calling Pipelines

Variant callers like DeepVariant and GATK HaplotypeCaller rely heavily on the CIGAR string to construct pileup images and perform local reassembly.

  • Pileup Generation: The CIGAR string dictates which read bases are stacked at each reference position. Insertions (I) cause extra bases to be placed between reference columns, while deletions (D) create gaps.
  • Indel Realignment: Regions with clusters of I and D operations indicate potential misalignments around true indels. Tools perform local reassembly to resolve these regions.
  • Strand Bias Detection: Comparing the CIGAR operations from forward-strand reads versus reverse-strand reads at a locus can reveal systematic sequencing artifacts.

A malformed CIGAR string that violates consumption rules will cause pipeline failures, making strict validation a critical preprocessing step.

06

CIGAR vs. MD Tag

The CIGAR string is often paired with the optional MD tag in SAM/BAM files to provide a complete picture of the alignment.

  • CIGAR String: Encodes the operations (match, insert, delete) but does not, by default, specify the reference bases that were mismatched.
  • MD Tag: A string that encodes the reference bases for mismatches and deletions. For example, MD:Z:10A5^C3 indicates 10 matches, a mismatch to 'A', 5 matches, a deletion of 'C', and 3 matches.

Together, the CIGAR string and MD tag allow a tool to perfectly reconstruct the reference sequence at the aligned locus without accessing the full reference genome file, which is critical for the high-speed operation of variant callers and alignment viewers.

CIGAR STRING DECODING

Frequently Asked Questions

Clear, technically precise answers to the most common questions about the CIGAR string, its syntax, and its critical role in genomic sequence alignment.

A CIGAR string (Compact Idiosyncratic Gapped Alignment Report) is a compact, standardized representation within Sequence Alignment/Map (SAM) and Binary Alignment/Map (BAM) files that summarizes the sequence of operations required to align a sequencing read to a reference genome. It encodes the exact pattern of matches (M), insertions (I), deletions (D), skipped regions (N), soft-clipping (S), and hard-clipping (H) as a series of paired length-and-operator values. For example, the string 150M indicates a perfect 150-base-pair match, while 20M2I128M describes a read where 20 bases match, 2 bases are inserted relative to the reference, and then 128 more bases match. The CIGAR string is essential for variant calling because it precisely defines the alignment's edit distance and allows tools like DeepVariant to reconstruct the read's sequence at each genomic position.

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.