Skip to content

Stop Smith-Waterman traceback at zero-score cells - #15631

Open
Shubham-Padkonde wants to merge 1 commit into
TheAlgorithms:masterfrom
Shubham-Padkonde:fix/smith-waterman-traceback-stop
Open

Shubham-Padkonde wants to merge 1 commit into
TheAlgorithms:masterfrom
Shubham-Padkonde:fix/smith-waterman-traceback-stop

Conversation

@Shubham-Padkonde

Copy link
Copy Markdown

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests?
  • Documentation 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' and subject='GGACCC', the matrix maximum is 2. Traceback previously returned --AC\nGGAC (score -2); it now returns AC\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

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass.

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Oct 23, 2026
@Salaam7867

Copy link
Copy Markdown

🤖 Agentic AI PR Review

✅ All files passed the AI review! No critical issues found.

@rahul05ranjan rahul05ranjan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants