Skip to content

Score all sequence pairs without building alignmentsJd/all pairs scores - #1166

Open
josemduarte wants to merge 2 commits into
biojava:masterfrom
josemduarte:jd/all-pairs-scores
Open

josemduarte wants to merge 2 commits into
biojava:masterfrom
josemduarte:jd/all-pairs-scores

Conversation

@josemduarte

Copy link
Copy Markdown
Contributor

Supersedes #1161, building on @mikepapadim's work there now that #1164 is merged.

What

Alignments.getAllPairsScores and the pairwise stage of getMultipleSequenceAlignment built a full NeedlemanWunsch or SmithWaterman alignment for every pair, with a traceback matrix, only to read its score, or its number of identical positions and length.

The new package-private AllPairsScores encodes each sequence once with PairwiseStatisticsAligner (#1164) and aligns every pair with it, in linear memory and with no traceback. This applies to GLOBAL, LOCAL, GLOBAL_IDENTITIES (the MSA default) and LOCAL_IDENTITIES scoring.

  • The scores are those of NeedlemanWunsch/SmithWaterman. Identicals and length are those of the alignment they return, ties included.
  • The scorers handed to the guide tree have the same score, max and min as the aligners' (or FractionalIdentityScorer's). So distances, the guide tree and the multiple alignment are unchanged.
  • Pairs run on the ConcurrencyTools pool, one task per query sequence.
  • Any other input takes the existing path: other scorer types, a single sequence, an empty sequence, mixed compound sets, positive gap penalties, or compounds outside the matrix's compound set.

Performance

getAllPairsScores on all pairs of 100 related proteins of about 300 residues, with BLOSUM62, affine gaps (10/1) and 10 cores. Times are steady-state medians in ms, each run in a fresh JVM, with identical checksums:

Before (after #1164) After Speed-up
GLOBAL 995 370 2.7×
LOCAL 1080 460–490 2.3×
GLOBAL_IDENTITIES 1030 370–390 2.7×
LOCAL_IDENTITIES 1120 470–560 2.2×

Cold first calls are 5–15% slower than these numbers in both columns.

#MMMM had its own score-only copy of the recurrences. That copy is about 2× faster than PairwiseStatisticsAligner for plain GLOBAL/LOCAL, but it would have been a third copy alongside AlignerHelper and PairwiseStatisticsAligner, and it couldn't serve the *_IDENTITIES types. This PR keeps a single kernel. Single-threaded numbers for all three variants are in the comments on #MMMM.

Tests

AllPairsScoresTest checks the score, max, min, distance and similarity of every pair against the aligners, for all four types. The cases cover:

  • random and related proteins
  • rare and ambiguous compounds
  • very different lengths
  • DNA with NUC.4.4
  • six gap penalties (affine and linear) and three matrices
  • local pairs with no positive cell
  • a list with an empty sequence, which takes the old path
  • the resulting multiple alignment

mvn test in biojava-alignment passes: 152 tests, 1 skipped, which was already skipped before this PR.

🤖 Generated with Claude Code

mikepapadim and others added 2 commits November 23, 2026 21:57
Alignments.getAllPairsScores, and the pairwise stage of
getMultipleSequenceAlignment, built a full NeedlemanWunsch or SmithWaterman
alignment for every pair, with a traceback matrix, only to read its score or
its number of identical positions and length.

AllPairsScores encodes each sequence once with PairwiseStatisticsAligner and
aligns every pair with it, in linear memory and with no traceback. Its score
is that of NeedlemanWunsch or SmithWaterman, and its number of identical
positions and length are those of the alignment they return, ties included.
So GLOBAL, LOCAL, GLOBAL_IDENTITIES and LOCAL_IDENTITIES scoring all take
this path, and the scorers it returns for the guide tree have the same
scores, maximum and minimum scores as the aligners' (or
FractionalIdentityScorer's), hence the same distances and the same multiple
alignment. GLOBAL_IDENTITIES is the default of getMultipleSequenceAlignment.
Pairs run on the ConcurrencyTools pool, one task per query sequence.

Other scorer types, a single sequence, an empty sequence, mixed compound
sets, positive gap penalties or compounds outside the compound set of the
substitution matrix take the existing path.

On 10 cores, all pairs of 100 related proteins of about 300 residues with
BLOSUM62 and affine gaps take about 370 ms for GLOBAL and GLOBAL_IDENTITIES
and 460-490 ms for LOCAL and LOCAL_IDENTITIES, against 1000-1120 ms with the
aligners.

AllPairsScoresTest checks score, maximum, minimum, distance and similarity of
every pair against the aligners for the four types, with random and related
proteins, rare compounds, very different lengths, DNA, six gap penalties and
three matrices, local pairs with no positive cell, and the multiple
alignment.

Co-authored-by: Jose Duarte <jose.m.duarte@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

2 participants