Reading CIGAR Strings
CIGAR (Compact Idiosyncratic Gapped Alignment Report) is an alignment-state string stored in each record of a SAM/BAM alignment file. Column 6 of a line compactly records how a read aligns to the reference sequence. If POS says “where,” CIGAR says “in what shape”: which portions align, where insertions and deletions occur, and which ends are clipped.
Question for this reference: How does a string such as
40M1523N36Mdescribe one read’s alignment?
CIGAR is one column of a SAM/BAM record. For all 11 columns, FLAG, and tags, see the SAM/BAM reference.
1. String structure: length + operator
Section titled “1. String structure: length + operator”A CIGAR string concatenates length + operator pairs from left to right. For example, 40M1523N36M is read as three pieces: 40M, 1523N, and 36M. Read them in order from the left, which is the read’s 5’ end.
40M: 40 bp align to the reference1523N: skip 1,523 bp of the reference, an intron36M: another 36 bp align to the reference
This read is therefore 76 bp long, while its alignment spans 1,599 bp of the reference. The intervening 1,523 bp are absent from the read because they are an intron. This pattern is very common in RNA-seq.
2. The nine operators
Section titled “2. The nine operators”The key distinction is whether an operator consumes bases from the read (query), the reference, or both. Once you know these two columns, you can calculate any CIGAR string.
| Operator | Meaning | Consumes query | Consumes ref |
|---|---|---|---|
| M | Aligned, either match or mismatch | ✅ | ✅ |
| = | Aligned and bases match | ✅ | ✅ |
| X | Aligned but bases mismatch | ✅ | ✅ |
| I | Insertion present in the read but absent from the reference | ✅ | |
| D | Deletion: present in the reference but absent from the read | ✅ | |
| N | Skipped reference region, usually an intron | ✅ | |
| S | Soft clip: an unaligned end retained in SEQ | ✅ | |
| H | Hard clip: removed and absent from SEQ | ||
| P | Padding for multiple alignment, rare |
Remember two rules.
- The sum of operators that consume the query (M, I, S, =, X) equals the SEQ length.
- The sum of operators that consume the reference (M, D, N, =, X) equals the reference span covered by the read.
🧩 M does not mean “match”: M only means that positions in the read and reference were aligned. It does not guarantee that their bases are identical. Most aligners, including STAR and BWA, write both matches and mismatches as M. The
=andXoperators distinguish true matches from mismatches, but few tools emit them. In practice, check theNMtag rather than CIGAR for the mismatch count.
3. N versus D: both skip the reference
Section titled “3. N versus D: both skip the reference”Both N and D denote a reference region absent from the read, but they mean different things.
- D (deletion): a few reference bases are missing from this read. It is usually short, on the scale of an indel.
- N (skipped): the alignment intentionally skips part of the reference. In RNA-seq, an N appears when a read aligns across an exon-intron-exon boundary. It is commonly hundreds to tens of thousands of base pairs long.
Thus, 50M3D26M is a read with a 3 bp deletion, while 40M1523N36M is a splice read crossing a 1,523 bp intron. This N is what makes results from splice-aware aligners such as STAR and HISAT2 the “intron-spanning alignments” described in Alignment concepts. The intron itself corresponds to exon boundaries recorded in the GTF.
4. Soft clips and hard clips
Section titled “4. Soft clips and hard clips”If an end of a read does not align to the reference, that portion is clipped. POS refers to the first aligned base, excluding any clip.
- S (soft clip): clipped bases remain in the SEQ column, so they count toward the SEQ length.
- H (hard clip): clipped bases are removed from SEQ too. Hard clips occur mainly in supplementary alignments, where a read is split across multiple locations.
For example, 5S71M clips the first 5 bp and aligns the remaining 71 bp. Its SEQ is still 76 bp long, 5 + 71. Adapter remnants or low-quality ends can appear as soft clips here. For adapters and trimming, see the trimming reference.
5. Reading real strings
Section titled “5. Reading real strings”A CIGAR of 76M for the first line of a paired-end read is simple. Here are several practical examples:
| CIGAR | Interpretation | SEQ length | ref span |
|---|---|---|---|
76M | 76 bp aligned as-is; mismatches may still exist | 76 | 76 |
5S71M | First 5 bp soft-clipped, 71 bp aligned | 76 | 71 |
50M2I24M | 2 bp insertion in the middle | 76 | 74 |
50M3D26M | 3 bp deletion in the middle | 76 | 79 |
40M1523N36M | Splice read crossing a 1,523 bp intron | 76 | 1599 |
30M500N20M2I24M | Splice followed by a 2 bp insertion | 76 | 574 |
Use the last row to verify the reference span. The reference-consuming operators are M, D, N, =, and X, so 30M (30) + 500N (500) + 20M (20) + 24M (24) = 574. The 2I does not consume the reference and is excluded. The SEQ length is the sum of query-consuming operators: 30M + 20M + 2I + 24M = 76.
💡 To inspect CIGAR directly: open alignments from a particular region with
samtools view file.bam chr21:33426891-33427000. Column 6 is CIGAR. For a gene where you suspect splicing, check whether N matches the expected intron length and whether I or D clusters at a particular position. Together with counting through FLAG filters insamtools view -c, this is a basic alignment-debugging technique.
Summary
Section titled “Summary”CIGAR compresses the shape of one read’s alignment from left to right as length+operator pairs. Instead of memorizing all nine operators at first, remember three points.
- M means aligned, not matched. Use the
NMtag for the actual mismatch count. - N skips an intron in a splice alignment; D is a short deletion. RNA-seq splice reads contain N.
- The sum of query-consuming operators is the SEQ length, and the sum of reference-consuming operators is the reference span.
Continue with Reading one SAM/BAM line for the context of a complete SAM record and Finding read positions for the concept of splice alignment.