Repository navigation
Stop Smith-Waterman traceback at zero-score cells - #15631
Shubham-Padkonde wants to merge 1 commit into
Conversation
🤖 Agentic AI PR Review✅ All files passed the AI review! No critical issues found. |
rahul05ranjan
left a comment
There was a problem hiding this comment.
Verified this fixes a real correctness bug, not just tidiness. traceback's docstring says it stops at a 0-score cell, but the old loop condition (while i > 0 and j > 0) kept walking after reaching the alignment start, and at a 0-score cell the diagonal branch can never match (score_function never returns 0), so it silently fell into the horizontal branch and padded the alignment with - + subject characters.
Repro against the pre-fix code: traceback(smith_waterman('AC', 'GC'), 'AC', 'GC') returns '-C\nGC', with a spurious gap and a G that isn't part of the best local alignment. With this patch it returns the correct 'C\nC'. A brute-force sweep over all query/subject pairs up to length 4 over ACGT shows the outputs differ in 26184 cases, so this affects most inputs with short or weak matches, e.g. smith_waterman docstring examples like ('AGT', 'GTC').
One thing worth double-checking while you're in here: at a 0-score cell the vertical condition score[i][j] == score[i-1][j] + gap can match (when score[i-1][j] == 2 with the default gap=-2), so the old code could also wander vertically past the alignment start. Your score[i][j] > 0 guard stops both walks, which is exactly the documented behavior — nice, minimal fix.
Describe your change
Stop Smith-Waterman traceback at the first zero-score cell, as required for local alignment. Continuing toward the matrix boundary adds unrelated prefixes and can produce an alignment whose score is below the matrix maximum.
For
query='TTACGG'andsubject='GGACCC', the matrix maximum is 2. Traceback previously returned--AC\nGGAC(score -2); it now returnsAC\nAC(score 2).Validation on free-threaded Python 3.15: existing Smith-Waterman and Needleman-Wunsch doctests pass (4 pytest items). An external check covers all 16,129 pairs of binary-alphabet strings up to length 6. Each returned alignment has the matrix's optimal score and uses substrings of the inputs. The example above fails before the fix. All applicable pre-commit hooks pass. Existing doctests remain unchanged, per the contribution guidance.
AI-assisted contribution.
Checklist