Q&A-5Derive an alignment recurrence.
Answer
Assumptions. Global alignment (Needleman–Wunsch) of A = a₁…a_m against B = b₁…b_n, linear gap penalty: a run of k gap columns costs k·d, d > 0. Substitution score s(a_i, b_j) from a fixed matrix: match +1, mismatch −1 in the trace below. Affine gaps come after.
Subproblem. F(i, j) = the maximum score over all alignments of the prefixes A[1..i] and B[1..j].
Derivation. Take any alignment of A[1..i] and B[1..j] and look at its last column. That column is one of exactly three things: a_i paired with b_j; a_i paired with a gap; or b_j paired with a gap. There is no fourth option, and the three are mutually exclusive, so the alignments partition into three disjoint classes. Deleting the last column from a class-1 member leaves an alignment of A[1..i−1] and B[1..j−1], worth at best F(i−1, j−1) + s(a_i, b_j). Class 2 leaves an alignment of A[1..i−1] and B[1..j] plus one gap column: F(i−1, j) − d. Class 3: F(i, j−1) − d. Best of the three:
F(i,j) = max( F(i−1,j−1) + s(a_i,b_j), F(i−1,j) − d, F(i,j−1) − d )
Boundaries. F(0,0) = 0; F(i,0) = −i·d; F(0,j) = −j·d. The boundary is not cosmetic: setting F(i,0) = 0 makes end gaps free, which is semi-global alignment, a different problem.
Traceback. Store a pointer per cell to whichever term attained the max (ties: diagonal first). Walk from (m,n) to (0,0); each step emits one column.
Worked trace. A = AC, B = AGC, match +1, mismatch −1, d = 2.
| F | j=0 | A | G | C |
|---|---|---|---|---|
| i=0 | 0 | −2 | −4 | −6 |
| A | −2 | 1 | −1 | −3 |
| C | −4 | −1 | 0 | 0 |
F(2,3) = max(F(1,2)+s(C,C) = −1+1 = 0, F(1,3)−2 = −5, F(2,2)−2 = −2) = 0. Traceback: (2,3)←(1,2) diagonal, (1,2)←(1,1) left, (1,1)←(0,0) diagonal. Alignment A: A - C, B: A G C, score 1 − 2 + 1 = 0 = F(2,3).
Affine gaps. A run of k gaps costing o + k·e (o open, e extend) breaks the recurrence: F(i,j) cannot tell whether the gap it just opened is length 1 or 10. Gotoh's fix is three matrices: M ends with a_i against b_j, Ix with a_i against a gap, Iy with b_j against a gap:
M(i,j) = max(M(i−1,j−1), Ix(i−1,j−1), Iy(i−1,j−1)) + s(a_i,b_j) Ix(i,j) = max(M(i−1,j) − o − e, Ix(i−1,j) − e) Iy(i,j) = max(M(i,j−1) − o − e, Iy(i,j−1) − e)
Ix opens only from M, never from Iy: allowing that lets a gap switch strands without a substitution, which no alignment does.
Trade-offs. Linear: one matrix, O(mn) time, O(min(m,n)) space with Hirschberg. Affine: three matrices, ~3× constant factor, same asymptotics, and the right model whenever an indel is one event (sequencing reads, protein homologs). Local alignment (Smith–Waterman) adds a 0 term to the max and zeroes every boundary; use it when the sequences share only a domain.
Failure modes.
- Wrong base case (F(i,0)=0): end gaps become free. Detect by aligning two sequences with no shared residue: global must return a negative score.
- Illegal gap switching (Ix opened from Iy): double counts. Detect by brute-force enumerating all alignments for m,n ≤ 8 and comparing scores.
- Score–traceback mismatch: recompute the traceback's score and compare to F(m,n); any disagreement means the pointer table is stale.
- Missing state: affine scoring with only F(i,j). Detect by comparing against Gotoh on a pair with a 5-residue indel: the single-matrix version mis-penalizes it.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.