# origin: Programming+ demo playground # status: unverified; `corpus.py verify` checks it on the pinned compiler # probe: Smith-Waterman local alignment with affine gaps (Gotoh): a read with a 2-letter deletion, scored two ways; three tables allocated, filled, traced back, and freed at their last use # mode: both # args: -O0 --debug-level=line-tables # expect-stdout: read GATCAGGT against reference TTGATTACAGGTCC # expect-stdout: 1. align with affine gaps # expect-stdout: + alloc H (best score ending here) 15x9 # expect-stdout: + alloc E (ends in a gap in the reference) 15x9 # expect-stdout: + alloc F (ends in a gap in the read) 15x9 # expect-stdout: - free F (ends in a gap in the read) # expect-stdout: - free E (ends in a gap in the reference) # expect-stdout: - free H (best score ending here) # expect-stdout: affine gaps: opening a gap costs 5, each further letter 1 # expect-stdout: score 18 | reference letters 3 to 12 # expect-stdout: reference GATTACAGGT # expect-stdout: ||| ||||| # expect-stdout: read GAT--CAGGT # expect-stdout: 2. align the same read with linear gaps # expect-stdout: + alloc H (best score ending here) 15x9 # expect-stdout: + alloc E (ends in a gap in the reference) 15x9 # expect-stdout: + alloc F (ends in a gap in the read) 15x9 # expect-stdout: - free F (ends in a gap in the read) # expect-stdout: - free E (ends in a gap in the reference) # expect-stdout: - free H (best score ending here) # expect-stdout: linear gaps: every gap letter costs 2 # expect-stdout: score 20 | reference letters 3 to 12 # expect-stdout: reference GATTACAGGT # expect-stdout: || | ||||| # expect-stdout: read GA-T-CAGGT # expect-stdout: the models disagree: one 2-letter deletion, or two 1-letter deletions? # Far below any real score: a gap state that cannot have been reached yet. comptime NEG_INF = -1_000_000 def cell(diagonal: Int, gap_in_reference: Int, gap_in_read: Int) -> Int: """The Smith-Waterman recurrence for one cell: the best of the three ways in, but never below 0, because a local alignment may start afresh anywhere.""" var best: Int = 0 if diagonal > best: best = diagonal if gap_in_reference > best: best = gap_in_reference if gap_in_read > best: best = gap_in_read return best def larger(a: Int, b: Int) -> Int: if a > b: return a return b @fieldwise_init struct Scoring(Copyable, Movable): """How an alignment is scored. With gap_open == gap_extend every gap letter costs the same (linear gaps); with gap_open below gap_extend one long gap is cheaper than several short ones (affine gaps), which is what real insertions and deletions look like. """ var name: String var match_score: Int var mismatch_score: Int var gap_open: Int var gap_extend: Int def pair(self, same: Bool) -> Int: if same: return self.match_score return self.mismatch_score struct Grid(Movable): """A dynamic-programming table that owns its memory, row by row in one List, and says when that memory is allocated and freed.""" var name: String var cols: Int var cells: List[Int] def __init__(out self, name: String, rows: Int, cols: Int, start: Int): self.name = name self.cols = cols self.cells = List[Int]() for _ in range(rows * cols): self.cells.append(start) print(" + alloc", name, String(rows) + "x" + String(cols)) def __init__(out self, *, deinit move: Self): self.name = move.name^ self.cols = move.cols self.cells = move.cells^ def __deinit__(deinit self): print(" - free ", self.name) def at(self, i: Int, j: Int) -> Int: return self.cells[i * self.cols + j] def put(mut self, i: Int, j: Int, value: Int): self.cells[i * self.cols + j] = value @fieldwise_init struct Alignment(Copyable, Movable): """The best local alignment: its score, where it sits in the reference, and the three printed rows (reference, match marks, read).""" var score: Int var start: Int var end: Int var top: String var marks: String var bottom: String def align(reference: String, seq_read: String, s: Scoring) -> Alignment: """Smith-Waterman local alignment with affine gaps (Gotoh's three tables). H[i][j]: the best score of an alignment ending at reference letter i and read letter j. E[i][j]: the same, but ending in a gap in the reference (a letter of the read left over). F[i][j]: the same, but ending in a gap in the read (a letter of the reference skipped). """ var n = reference.count_codepoints() var m = seq_read.count_codepoints() var h = Grid("H (best score ending here)", n + 1, m + 1, 0) var e = Grid("E (ends in a gap in the reference)", n + 1, m + 1, NEG_INF) var f = Grid("F (ends in a gap in the read)", n + 1, m + 1, NEG_INF) # fill: every cell from its three neighbours var best = 0 var best_i = 0 var best_j = 0 for i in range(1, n + 1): for j in range(1, m + 1): e.put( i, j, larger( e.at(i, j - 1) + s.gap_extend, h.at(i, j - 1) + s.gap_open ), ) f.put( i, j, larger( f.at(i - 1, j) + s.gap_extend, h.at(i - 1, j) + s.gap_open ), ) var same = reference[codepoint=i - 1] == seq_read[codepoint=j - 1] var diagonal = h.at(i - 1, j - 1) + s.pair(same) var here = cell(diagonal, e.at(i, j), f.at(i, j)) h.put(i, j, here) if here > best: best = here best_i = i best_j = j # traceback: walk back from the best cell, switching tables where a gap opened var top = String("") var marks = String("") var bottom = String("") var i = best_i var j = best_j var table = 0 # 0: in H, 1: in E, 2: in F while i > 0 and j > 0: if table == 0: if h.at(i, j) == 0: break var same = reference[codepoint=i - 1] == seq_read[codepoint=j - 1] if h.at(i, j) == h.at(i - 1, j - 1) + s.pair(same): top = String(reference[codepoint=i - 1]) + top bottom = String(seq_read[codepoint=j - 1]) + bottom if same: marks = "|" + marks else: marks = "." + marks i -= 1 j -= 1 elif h.at(i, j) == e.at(i, j): table = 1 else: table = 2 elif table == 1: top = "-" + top marks = " " + marks bottom = String(seq_read[codepoint=j - 1]) + bottom if e.at(i, j) == h.at(i, j - 1) + s.gap_open: table = 0 j -= 1 else: top = String(reference[codepoint=i - 1]) + top marks = " " + marks bottom = "-" + bottom if f.at(i, j) == h.at(i - 1, j) + s.gap_open: table = 0 i -= 1 return Alignment(best, i + 1, best_i, top^, marks^, bottom^) def report(name: String, a: Alignment): print(" " + name) print(" score", a.score, "| reference letters", a.start, "to", a.end) print(" reference ", a.top) print(" ", a.marks) print(" read ", a.bottom) def main(): # A sequencing read that lost two letters (TA) of the reference: a 2-letter deletion. var reference = "TTGATTACAGGTCC" var read = "GATCAGGT" print("read", read, "against reference", reference) var affine = Scoring( "affine gaps: opening a gap costs 5, each further letter 1", 3, -2, -5, -1, ) var linear = Scoring("linear gaps: every gap letter costs 2", 3, -2, -2, -2) print("1. align with affine gaps") var first = align(reference, read, affine) report(affine.name, first) print("2. align the same read with linear gaps") var second = align(reference, read, linear) report(linear.name, second) if first.top == second.top and first.bottom == second.bottom: print("the two models agree") else: print( "the models disagree: one 2-letter deletion, or two 1-letter" " deletions?" )