Q&A-4Parse alignments without coordinate errors.
Answer
Core answer. Assume SAM/BAM (spec SAMv1, hts-specs 1.6), POS is 1-based and CIGAR offsets are 0-based within the alignment. Walk two pointers, a query pointer over SEQ and a reference pointer starting at POS-1, and advance each per operation. M, I, S, =, X consume query; M, D, N, =, X consume reference; H and P consume neither. Get that table right and coordinates fall out; get it wrong and every downstream interval is shifted.
Mechanism. The reference pointer only moves on ops that occupy reference bases. I inserts query bases with no reference advance; D and N skip reference bases with no query advance (N is the spliced gap, D a deletion). S is present in SEQ but not aligned; H is not in SEQ at all, so len(SEQ) equals query-consuming length only when there is no H. The 1-based reference end is POS + ref_span - 1.
import re
Q = set("MIS=X") # consume query
R = set("MDN=X") # consume reference
def spans(cigar):
q = r = 0
for n, op in re.findall(r"(\d+)([MIDNSHP=X])", cigar):
n = int(n)
if op in Q: q += n
if op in R: r += n
return q, r
spans("3S8M2I4M") # (17, 12) -- Python 3.11Worked trace. POS=1000, CIGAR=3S8M2I4M:
| op | query ptr | ref ptr (0-based) |
|---|---|---|
| start | 0 | 999 |
| 3S | 3 | 999 |
| 8M | 11 | 1007 |
| 2I | 13 | 1007 |
| 4M | 17 | 1011 |
Query length 17, reference span 12, 1-based end 1000+12-1 = 1011. Converting to BED (0-based, half-open): start = POS-1 = 999, end = 999+12 = 1011.
Trade-offs. M means alignment match: it may hide mismatches. If you need identity or variant calls, require =/X (minimap2 --eqx; BWA-MEM does not emit them by default) or read the MD tag. Cost: =/X roughly doubles CIGAR length and inflates BAM. Criterion: coordinates and coverage only → M is fine; per-base identity → =/X or MD. For malformed CIGAR, choose reject-versus-repair deliberately: a validation gate should reject and fail the batch; a long-running pipeline should quarantine the record and continue, because silently clamping a bad CIGAR corrupts every downstream interval. Never repair without logging.
Failure modes. Off-by-one: mixing 1-based POS with 0-based CIGAR offsets shifts intervals by one base. Detect by asserting ref_end == POS + ref_span - 1 on every record. Treating M as exact match inflates identity: detect by comparing against NM/MD. Ignoring 0x10 (reverse strand): SEQ is reverse-complemented relative to the reference, but POS still names the leftmost reference base, so do not flip coordinates. Detect by checking SEQ against the reference. Double-counting 0x100 secondary alignments inflates coverage; 0x800 supplementary records are split parts of one read and should be combined (see the SA tag). Dropping supplementary records loses split alignments entirely. H present but query length computed from len(SEQ) mismatches: assert query_span == len(SEQ) only when no H.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.