Skip to content

[FIX] PeptideIndexer: check enzyme specificity on the unmodified protein sequence (fixes #1793) - #10190

Merged
timosachsenberg merged 7 commits into
developfrom
claude/hopeful-cray-y4wecx
Oct 17, 2026
Merged

timosachsenberg merged 7 commits into
developfrom
claude/hopeful-cray-y4wecx

Conversation

@timosachsenberg

@timosachsenberg timosachsenberg commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #1793 — the root cause named in that issue's title is still present on develop.

With IL_equivalent, PeptideIndexing replaced every L (and J) by I in place in the protein sequence, and then handed that substituted string to EnzymaticDigestion::isValidProduct(). Cleavage sites were therefore determined on the substituted sequence, which silently destroys sites for enzymes that cleave after L but not after I (and invents sites where the protein had an I):

prot = proteins.chunkAt(i).sequence;
StringUtils::substitute(prot, 'L', 'I');          // clobbers the original
...
search(ac_trie, ac_state, prot, prot, 0, ...);    // same string used as 'full_prot'
  -> func_threads.validate(full_prot, ...)
     -> enzyme.isValidProduct(seq_prot, ...)      // regex runs on the substituted seq

Demonstrated with Chymotrypsin's own regex from Enzymes.xml and one of the peptides from the issue report:

protein (original)    : AAGGL DTAYDDLGNSGHF SSGGAA   cleavage sites [0, 5, 9, 12, 18]
protein (L->I subst.) : AAGGI DTAYDDIGNSGHF SSGGAA   cleavage sites [0,       9,     18]
fully-specific on ORIGINAL    : True   <- correct
fully-specific on SUBSTITUTED : False  <- what was computed  => "Unmatched peptide"

Affected enzymes in Enzymes.xml (those with I/L/J in their cleavage regex): Chymotrypsin, Chymotrypsin/P, TrypChymo, PepsinA, PepsinA + P, leukocyte elastase, elastase-trypsin-chymotrypsin.

The same string also supplied the flanking residues, so aa_before/aa_after were reported as I wherever the protein really had an L or J — wrong for every enzyme, trypsin included.

Fix

The substitution now goes into a separate per-thread buffer used for the Aho-Corasick search only; the original sequence is passed to search() as full_prot and drives both the enzyme filter and the flanking residues. Since the substitution preserves length, hit positions carry over unchanged.

std::string prot;     // original ('*' removed) -> enzyme specificity + flanking residues
std::string prot_IL;  // I/L-substituted copy; only filled when IL_equivalent_ is set

if (IL_equivalent_)
{ // single pass: copy and map 'L'/'J' to 'I' in one go (hot loop over all proteins)
  prot_IL.resize(prot.size());
  std::transform(prot.begin(), prot.end(), prot_IL.begin(), [](const char c) { return (c == 'L' || c == 'J') ? 'I' : c; });
}
const std::string& prot_search = IL_equivalent_ ? prot_IL : prot;

Memory impact is negligible: the original protein sequences are already resident in FASTAContainer's double-buffered chunk cache, and this only adds one scratch string per thread (amortised to the longest protein seen, so single-digit MB even at high thread counts, against a ~400 MB resident FASTA cache). When IL_equivalent is off — the default — prot_IL stays empty and prot_search aliases prot, so nothing extra is allocated on the default path.

This also removes the guard that rejected IL_equivalent for Chymotrypsin/Chymotrypsin/P/TrypChymo with an InvalidParameter exception. It was never a fix, and it was unsound twice over: it covered only 3 of the 7 affected enzymes, and it compared the raw enzyme:name parameter, so it never triggered for the default enzyme:name=auto where the enzyme comes from the idXML — which is why the problem kept resurfacing (see the 2022 comment on #1793).

Second commit: a pre-existing gap this uncovers

Once the enzyme filter sees the protein unsubstituted, a J in the database reaches the cleavage rules as J. Six of the seven enzymes above list J explicitly; elastase-trypsin-chymotrypsin did not:

Chymotrypsin                   (?<=[FYWLJX])(?!P)      J
Chymotrypsin/P                 (?<=[FYWLJX])           J
PepsinA / PepsinA + P          (?<=[FLJX])             J
TrypChymo                      (?<=[FYWLJKRX])(?!P)    J
leukocyte elastase             (?<=[ALIJVX])(?!P)      J
elastase-trypsin-chymotrypsin  (?<=[ALIVKRWFYX])(?!P)  -- missing

Its own RegExDescription reads "cuts after A,L,I(or J),V,K,R,W,F,Y unless followed by P", so the regex simply contradicted its documented intent. A peptide preceded by J was rejected as non-specific — already the case before this PR whenever IL_equivalent was off; IL_equivalent=true happened to mask it by rewriting J to I first. Adding J to the class fixes both paths.

The rule is defined twice: share/OpenMS/CHEMISTRY/Enzymes.xml is what ProteaseDB loads at runtime (reverting only that file makes the new test fail), and BuiltInProteaseDataProvider.cpp is the compiled-in fallback. Both are updated. CometAdapter.cpp also carries the class in Comet's own enzyme-file format and is deliberately left alone.

Regression tests

New [EXTRA] section in PeptideIndexing_test.cpp, both parts run with IL_equivalent off and on:

  1. Chymotrypsin, specificity=full, aaa_max=0:
Protein1 = AAGGL·DTAYDDLGNSGHF·SSGGAA Protein2 = AAGGW·DTAYDDIGNSGHF·SSGGAA
IL_equivalent=false matched not matched
IL_equivalent=true matched (dropped before this fix) matched (I/L equivalence still works)

plus aa_before == 'L' (not 'I'). Protein2 is a control: valid under either sequence, so it isolates the enzyme filter from the I/L matching feature itself.

  1. elastase-trypsin-chymotrypsin with Protein3 = GGG·J·DTAYDDSGNSGGGG — a J flank, expected to match under both settings, with aa_before == 'J'.

Both are verified non-vacuous by reverting each fix independently and watching the corresponding assertions fail (accessions.count(...): got '0', expected '1') — the Unmatched peptide symptom from #1793.

No reference outputs needed regenerating: TOPP_PeptideIndexer_10 is the only test using -IL_equivalent and it passes against its checked-in reference, and no test or .ini stores the enzyme regex (the .ini files list the enzyme by name only).

Also

PeptideIndexing.h claimed that IL_equivalent + write_protein_sequences stores both sequence versions, "roughly equivalent to the size of the FASTA database file itself". That was already stale — write_protein_sequence re-reads the original entry via proteins.readAt() — so the paragraph now describes what actually happens.

Checklist

  • Make sure that you are listed in the AUTHORS file
  • Add relevant changes and new features to the CHANGELOG file
  • I have commented my code, particularly in hard-to-understand areas
  • New and existing unit tests pass locally with my changes — PeptideIndexing_test, ProteaseDB_test, EnzymaticDigestion_test, ProteaseDigestion_test, AhoCorasickAmbiguous_test, PeptideEvidence_test and all 27 TOPP_PeptideIndexer_* tests: 33/33 pass (Ubuntu 24.04, g++ 13, Release)
  • Updated or added python bindings for changed or new classes (Tick if no updates were necessary.) — PeptideIndexing is bound in src/pyOpenMS/bindings/bind_misc.cpp, but no binding change is needed: this is a behaviour fix with no API change, and the binding docstring does not restate the I/L paragraph edited in PeptideIndexing.h

🤖 Generated with Claude Code

https://claude.ai/code/session_014cwvmMpsfDb9mD7JwivuuR

Summary by CodeRabbit

  • Bug Fixes
    • Enzyme specificity checks and reported flanking residues now reflect the original protein sequence when I/L-equivalent searching is enabled.
    • I/L-equivalent searching can now be used with Chymotrypsin and TrypChymo enzymes.
    • Elastase-Trypsin-Chymotrypsin cleavage rules now recognize J alongside I/L.

…ein sequence

With 'IL_equivalent', PeptideIndexing replaced every 'L' (and 'J') by 'I' in the
protein sequence in-place, and then handed that substituted string to
EnzymaticDigestion::isValidProduct(). Cleavage sites were therefore determined on
the substituted sequence, which silently destroys sites for enzymes that cleave
after 'L' but not after 'I' (and invents sites where the protein had an 'I').
Affected enzymes in Enzymes.xml: Chymotrypsin, Chymotrypsin/P, TrypChymo,
PepsinA, PepsinA + P, leukocyte elastase and elastase-trypsin-chymotrypsin.
The same string also supplied the flanking residues, so 'aa_before'/'aa_after'
were reported as 'I' wherever the protein really had an 'L' or 'J'.

The substitution is now done into a separate per-thread buffer which is used for
the Aho-Corasick search only; the original sequence is passed to search() as
'full_prot' and drives both the enzyme filter and the flanking residues. Since
the substitution preserves length, hit positions carry over unchanged. When
'IL_equivalent' is off, the buffer stays empty and the search string aliases the
original, so nothing extra is allocated on the default path.

This also removes the guard which rejected 'IL_equivalent' for Chymotrypsin,
Chymotrypsin/P and TrypChymo: it covered only three of the seven affected
enzymes, and it compared the raw 'enzyme:name' parameter, so it never triggered
for the default 'auto' where the enzyme comes from the idXML.

Adds a regression test with a chymotryptic peptide preceded by 'L' in the
database, which was dropped by the enzyme filter before this change, plus a
second protein carrying 'I' instead of 'L' to confirm I/L-equivalent matching
still works and that the flanking residues are reported unsubstituted.

Fixes #1793

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014cwvmMpsfDb9mD7JwivuuR
…gle-pass I/L substitution

Review follow-up to the PeptideIndexer I/L fix.

'elastase-trypsin-chymotrypsin' was the only one of the seven enzymes cleaving
after I/L whose regex omitted 'J' -- "(?<=[ALIVKRWFYX])(?!P)" -- although its own
RegExDescription says it "cuts after A,L,I(or J),V,K,R,W,F,Y". A peptide preceded
by 'J' in the database was therefore rejected as non-specific. With
'-IL_equivalent' this was masked, because 'J' was rewritten to 'I' in-place before
the cleavage sites were determined; now that the enzyme filter sees the protein
unsubstituted, the omission is exposed for that path too. Adding 'J' to the
character class fixes both, and matches the six sibling enzymes.

The rule is defined twice: Enzymes.xml is what ProteaseDB loads at runtime (a test
reverting only that file fails), BuiltInProteaseDataProvider.cpp is the
compiled-in fallback. Both are updated. CometAdapter's own enzyme table is a
separate Comet-side format and is deliberately left alone.

Also builds the I/L-substituted search copy in a single std::transform pass
instead of a copy plus two std::replace scans, since this runs for every protein,
and includes <algorithm> directly rather than relying on a transitive header.

The regression test gains a 'J'-flanked case covering both IL_equivalent settings;
it fails on both without the regex fix.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014cwvmMpsfDb9mD7JwivuuR
…Size

count_j_proteins counted database proteins containing 'J' so the run could warn
that they eat into the 'aaa_max' budget. As a uint16_t it wrapped silently at
65536, and since the warning is guarded by 'if (count_j_proteins)' the message
was not merely wrong but absent for any exact multiple of 65536.

Verified with a generated FASTA of exactly 65536 'J'-bearing proteins: before,
PeptideIndexer printed no 'J' warning at all; after, it reports
"found 65536 protein sequences in your database containing the amino acid 'J'".

Pre-existing, unrelated to the I/L specificity fix, but in the same function and
directly adjacent to the reworked 'J' handling; included at the reviewer's
request.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014cwvmMpsfDb9mD7JwivuuR
@timosachsenberg
timosachsenberg force-pushed the claude/hopeful-cray-y4wecx branch from 6049e67 to 0514ebc Compare September 18, 2026 17:46
@OpenMS OpenMS deleted a comment from coderabbitai Bot Sep 19, 2026
@timosachsenberg

Copy link
Copy Markdown
Contributor Author

@cbielow ready for review

@timosachsenberg

Copy link
Copy Markdown
Contributor Author

@copilot resolve the merge conflicts in this pull request

Copilot AI commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

develop is already merged into this branch in 3323d60 (a two-parent merge commit), and the working tree has no unresolved conflicts. No additional conflict-resolution changes were needed.

@timosachsenberg

Copy link
Copy Markdown
Contributor Author

@cbielow looks correct to me.

@OpenMS OpenMS deleted a comment from coderabbitai Bot Oct 7, 2026

@timosachsenberg timosachsenberg left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This moves the I/L(/J) substitution into a per-thread copy that only the Aho-Corasick search sees. Enzyme specificity and flanking residues now come from the original sequence, and 'J' is added to the elastase-trypsin-chymotrypsin rule. I checked:

  • Index alignment: '*' is stripped before the copy and the transform is 1:1, so search positions match. X positions are identical in both strings, so the chunk offsets computed on prot_search are valid offsets into prot.
  • Performance: there are no per-hit copies (full_prot is a reference to the thread-local prot). The IL path makes one pass into a reused buffer instead of two in-place passes; the default path is unchanged.
  • Semantics: N-terminal Met clipping (sequence[0]=='M'), X/B/Z handling and decoy labelling are unaffected. Every regex with L also has J, so 'J' reaching the regex now behaves consistently.
  • Tests fail without the fix (guard exception / count 0 for Protein1).

CI is green and the PR merges cleanly. Only the CHANGELOG needs work (inline).


Generated by Claude Code

Comment thread CHANGELOG Resolved

Copy link
Copy Markdown
Contributor Author

Merged the current develop and fixed the CHANGELOG review point in a5059ef. Both PeptideIndexer entries now sit in the 3.7.0 section under TOPP tools → Changes → PeptideIndexer instead of the released 3.6.0 section. The PR's diff against develop is otherwise unchanged.


Generated by Claude Code

@OpenMS OpenMS deleted a comment from coderabbitai Bot Oct 16, 2026
@timosachsenberg
timosachsenberg merged commit 89cbde6 into develop Oct 17, 2026
32 checks passed
@timosachsenberg
timosachsenberg deleted the claude/hopeful-cray-y4wecx branch October 17, 2026 08:40
timosachsenberg pushed a commit that referenced this pull request Oct 19, 2026
…#10301, #10611, #10606, #10608, #10609, #10617, #10610, #10578 ProSE Percolator rescoring, #10628)

Only CHANGELOG conflicted; both entries are kept. Develop's changes to FalseDiscoveryRate,
IDScoreGetterSetter, ConsensusMapMergerAlgorithm, FileInfo and ProSE work on the established
classes, which this branch keeps as they are.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B51nRFoHvZuVXD7cT8XZav
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.

EnzymaticDigestion::isValidProduct is run after I/L replacement in PeptideIndexer

3 participants