Skip to content

Adopt upstream #5365: identity distance with skip_letters - #292

Merged
dbolser merged 6 commits into
mainfrom
identity-skip-letters
Nov 9, 2026
Merged

dbolser merged 6 commits into
mainfrom
identity-skip-letters

Conversation

@dbolser

@dbolser dbolser commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Adopts biopython/biopython#5365 by Angus Barlow, which fixes biopython/biopython#5332. Both upstream commits are cherry-picked with -x, so they keep their author. Rebase-merge, do not squash.

The defect

DistanceCalculator with the identity model counted matches only over the columns where neither letter is in skip_letters, but divided by len(seq1), which counts every column. So with an explicit skip_letters, every skipped column made the distance larger. With skip_letters=("-", "*"):

pair before after
AC--GT / AC--GT 0.333 0.0
AC--GT / AC--GA 0.5 0.25 (same as ACGT / ACGA)
AC--GT / ACTTGT 0.333 0.0

These numbers hold for a MultipleSeqAlignment. They also hold for a Bio.Align.Alignment whenever another row has letters in the gap columns, as ACTTGT does, which is the usual case in a real multiple alignment. An Alignment was spared only the columns that are gaps in every row, because Align.read drops them. So a two-row Alignment of just AC--GT and AC--GT was already 0.

The scoring-matrix branch already left skipped columns out of its total. skip_letters was added in 2017 (f25c05203, for biopython/biopython#491) so that identity could skip gaps the way the other models do. That commit changed how matches were counted but not the total, so this looks like an oversight. The default identity model skips no letters and gives the same numbers as before. Biopython 1.88 and upstream master give the same wrong numbers as fork main; upstream has not merged the PR.

Commits

Rebased onto main at 8f9e0ffec.

  1. 4e894b440b (upstream): the fix, its regression test, and the author's CONTRIB.rst line. The only conflict was in NEWS.rst. The upstream hunk added a contributor list to upstream's in-progress 1.89 section, which this tree keeps as upstream history, so I left that section unchanged, as was done for Write PDBIO TER records within 80 columns and with their own serial biopython/biopython#5391. Main already has the same CONTRIB.rst line in the same place, from the adoption of Fix protein_scale for window 1 and even window sizes biopython/biopython#5363. This commit and the next together leave CONTRIB.rst unchanged.
  2. 69921cb009 (upstream): moves the CONTRIB.rst line into alphabetical order. On this base, that removes the second copy that the first commit adds.
  3. Correction: a new test. The PR's test reads its alignments only with AlignIO, so it never passes a Bio.Align.Alignment. Its identical gapped rows would not have helped there, because Align.read drops the columns that are gaps in every row. The new test puts a gap in one row only and checks both alignment classes. This commit also corrects the comment above the loop (it said no letters were skipped) and cites the issue as biopython/biopython#5332. Its commit message gives the column-dropping as the reason the PR's test missed Alignment; the ADOPTED.md entry gives the correct reason.
  4. NEWS paragraph at the top of the fork's 1.89 section.
  5. ADOPTED.md entry: what was taken and what was left behind, plus the related upstream PRs Fix identity distances with skipped alignment columns biopython/biopython#5401 and fix(phylo): exclude skipped positions from the identity distance maximum biopython/biopython#5333 and why they were not taken.
  6. Wording fixes to the NEWS paragraph and the ADOPTED.md entry. Identical gapped rows were wrong in an Alignment too, the reason the PR's test missed Alignment is now the correct one, and the entry notes the CONTRIB.rst overlap.

Verified

  • With only Bio/Phylo/TreeConstruction.py reverted to main, exactly the two new tests fail (5 failures, counting subtests). Everything else in test_TreeConstruction.py, test_Consensus.py and test_ProtParam.py passes. With the fix, all pass: 71 tests and 65 subtests.
  • The examples above, run with Biopython 1.88, upstream master, fork main and this branch, using both alignment classes and a third row ACTTGT.
  • Full suite on Python 3.12, one pytest process per module: 4868 passed, 75 skipped, 3 failed, and one module timed out. All four are online tests against live services, and this branch changes only Bio/Phylo/TreeConstruction.py:
    • test_mmtf_online: the MMTF host does not resolve.
    • test_UniProt: the live response has an extra field.
    • test_KEGG_online: KEGG returned HTTP 400.
    • test_NCBI_qblast: timed out after 300 s.
      The first three fail the same way on main. I did not run test_NCBI_qblast on main.
  • pre-commit run --files on every changed file: all hooks pass.
  • The licence box on Fix DistanceCalculator identity model with skip_letters biopython/biopython#5365 is ticked ([X]). The PR has no comments or reviews as of 2026-11-09, and DistanceCalculator identity model inflates the distance when skip_letters is used biopython/biopython#5332 is still open.

Not checked: other Python versions or platforms (this is a pure-Python change), and a local Sphinx build (rstcheck and doc8 pass on the NEWS text).

Known and left for a separate fix: when a scoring-matrix model such as blosum62 is given a Bio.Align.Alignment with a letter outside the matrix, it raises AttributeError instead of the intended ValueError. The error message reads seq1.id, but Alignment rows are plain strings. Biopython 1.88 and fork main do the same, and this PR does not touch that code.

🤖 Generated with Claude Code

barlowa124 and others added 6 commits November 9, 2026 23:07
DistanceCalculator._pairwise scored only non-skipped positions but used
len(seq1) as the maximum, so gap columns inflated the reported distance:
identical sequences with gaps returned > 0 and the identity branch
disagreed with the scoring-matrix branch on the same data.

Count only non-skipped aligned positions in max_score, matching the set
the score is computed over. The default identity path (empty
skip_letters) is unchanged.

Closes #5332.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit 4e894b440b17037b2ddeadc5445a926af47628a2)
(cherry picked from commit 69921cb0094ee534a02116e493dbf4036bed2ff0)
The adopted test uses identical gapped rows, and Align.read drops the
columns that are gaps in every row, so it never exercised skipping with a
Bio.Align.Alignment. The new test puts a gap in one row only and checks
both alignment classes; AC--GT against ACTTGT gave 1/3 before the fix
and now gives 0.

Also correct the comment above the identity loop, which said no letters
were skipped (stale since skip_letters was added in 2017), and cite the
upstream issue as biopython/biopython#5332, since #5332 in this
repository would be a different number.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The examples said identical rows AC--GT were 0.333 apart "in a
MultipleSeqAlignment", which reads as though a Bio.Align.Alignment was
spared. It was not: with a third row ACTTGT filling the gap columns, every
pair was 0.333 apart in either class. An Alignment escapes only the
columns that are gaps in every row, which Align.read drops.

ADOPTED.md also gave the wrong reason why the pull request's test never
covered an Alignment: it reads only with AlignIO. The column dropping is
why #5401's Alignment test passed without the fix. After the rebase onto
main, the #5363 adoption already has the author's CONTRIB.rst line, so
the two cherry-picked commits leave that file unchanged; say so.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@dbolser

dbolser commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator Author

Codex review (CLI)

There is nothing worth fixing in this PR. I checked the identity denominator change, unchanged default behavior, custom skipped letters, all-skipped inputs, both alignment classes, public signatures/keywords, and the NEWS/PR explanation. Both cherry-picked commits preserve the upstream author's name, email and author date; the fix also retains its co-author attribution.

Validation on Python 3.12: the scratch-installed PR passes all 71 tests in test_TreeConstruction.py, test_Consensus.py and test_ProtParam.py. Substituting only TreeConstruction.py from base 8f9e0ffec yields exactly five failures across the two new regression tests. An additional 349,525 equal-length sequence cases matched the intended identity formula and preserved the old distances whenever no columns were skipped. No checkout files were modified.

@dbolser
dbolser merged commit 6b40d52 into main Nov 9, 2026
55 of 56 checks passed
@dbolser
dbolser deleted the identity-skip-letters branch November 9, 2026 23:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DistanceCalculator identity model inflates the distance when skip_letters is used

2 participants