Repository navigation
[FEATURE] EnzymaticDigestion: one SimpleCleavageRule parser shared with FragmentIndex; the hand path of tokenize_() for every simple rule - #10810
Conversation
…Index; the hand path of tokenize_() for every simple rule FragmentIndex's SimpleCleavageRule / parseSimpleCleavageRule() (a residue set, optionally one residue that blocks the cleavage) move into EnzymaticDigestion as the nested struct SimpleCleavageRule with a static parse(). EnzymaticDigestion parses the rule once per enzyme (constructor and setEnzyme(); ProteaseDigestion::setEnzyme(name) now goes through the base setter) and keeps it next to re_. tokenize_() applies it by hand for every enzyme whose pattern it covers (Trypsin, Trypsin/P, Lys-C, Arg-C, Chymotrypsin, Glu-C, ...: 21 of the 30 patterns of Enzymes.xml), not only for the two literal tryptic strings; the other patterns keep the regular expression. FragmentIndex's regex-free digest uses the same type. EnzymaticDigestion_test: a section for parse() (accepted and rejected patterns), and the equivalence section now compares, for every enzyme of ProteaseDB with a simple rule, the hand path against the regex path of tokenize_() on the same pattern (countInternalCleavageSites, digestUnmodified and every isValidProduct window, over the three specificities). Answers review point 1 of OpenMS#10807. Co-Authored-By: Claude Fable 5.1 <[email protected]>
timosachsenberg
left a comment
There was a problem hiding this comment.
looks good to me
timosachsenberg
left a comment
There was a problem hiding this comment.
AI-generated initial review
This is an automated, AI-generated first review. It looks only for high- and medium-priority issues (no nits) and checks whether the PR has tests and measurements. Please double-check anything before acting on it; a maintainer will follow up. I reviewed f2e8c26a against the merge base 576abb53 on develop.
What I ran: I did not build OpenMS: a full vcpkg build is not feasible in this 4-core container. Instead:
- Standalone harness: I copied develop's and this PR's
tokenize_()bodies and the PR'sSimpleCleavageRule::parse()verbatim into one program, built with g++ 13-O2against Boost.Regex 1.83, and compiled each regex asRegularExpression::Impldoes (boost::regex(pattern), default flags). I ran it on all 30 distinctRegExvalues inEnzymes.xmland on UniProt human reviewed (20,431 proteins, 11.4M residues). - Code trace: I checked every writer of
enzyme_/re_and the FragmentIndex call site. - Merge check:
git merge-treeagainst current develop (995044a5, after #10740 rewrote much ofFragmentIndex.cpp) is clean. The merged file has no leftover reference to the removed anonymous-namespace parser. - CI: when I looked,
header-set-compatibilitywas green, build-and-test on Linux (g++, clang, ARM) and Windows was running, and macOS was queued.
Verdict: I found no high- or medium-priority issue. The PR does what points 1 and 2 of the #10807 review suggested. The output is unchanged in every case I tried, and the speed gain for the newly covered enzymes is large (numbers below).
Checked and found sound
-
Equivalence with the regex path.
parse()accepts 21 of the 30 patterns, as the description says. The nine it rejects are(),(?<=W),(?<=[A-Z]), the four Asp-N/Lys-N lookaheads,(?<=[DBX])(?=[DBX])and(?<=[HKRX][PX])(?!P). For all 21:- Random sequences: 20,000 sequences of length 0–24 per pattern, over an alphabet rich in the rule's residues and its blocking residue. I tried every
startin [−2, n] and everyendin [−1, n+2], leaving out reversed ranges, which are UB on the regex path and which no caller passes. That gives 79M cases. PR vs regex: 0 mismatches. PR vs develop: 0 mismatches. - Human proteome: all full-protein tokenizations, plus 93.7M
isValidProduct()-stylepos-2 .. end+2windows (every fully specific product with up to 2 missed cleavages). 0 mismatches.
- Random sequences: 20,000 sequences of length 0–24 per pattern, over an alphabet rich in the rule's residues and its blocking residue. I tried every
-
The cached rule follows the enzyme. Every path that sets
enzyme_now also setssimple_rule_:- the constructor (initializer order matches the declaration order);
EnzymaticDigestion::setEnzyme();ProteaseDigestion::setEnzyme(name), which now delegates;RNaseDigestion::setEnzyme(), which already called the base;- the defaulted copy constructor and assignment, which copy the member.
No other code assigns
enzyme_orre_. -
The by-name pitfall is pinned by an existing test (checked by reading, not by running). In
ProteaseDigestion_test, the same object goes fromTrypsin/P(L388) toglutamyl endopeptidaseby name (L594). L601,isValidProduct(prot, 6, 8, true, true, false)under SPEC_SEMI, expectstrue. With a stale Trypsin/P rule, neither 6 nor 14 would be a site and the call would returnfalse. So reverting theProteaseDigestion.cpphunk should make that test fail. -
FragmentIndex: the gating condition (SPEC_FULL, not unspecific, pattern parses) and
digestSimpleCleavage()are unchanged apart from the type andcleavesBetween(). -
Export: a nested
struct OPENMS_DLLAPIinside an exported class has a precedent (ProForma::ConversionIssue). The Windows job will confirm it.
Tests
- Good:
- The new
parse()section covers the accepted shapes and 16 rejected ones. - The equivalence section now loops over every parseable
ProteaseDBenzyme. A subclass clears the cached rule, so both paths oftokenize_()stay covered. This closes the coverage gap from point 2 of the #10807 review. - The section also asserts which enzymes take which path, so a parser change that silently moves an enzyme to the other path would fail.
- The new
- Author's local run:
ctest -R "EnzymaticDigestion|FragmentIndex|ProSE|DigestionEnzyme|ProteaseDigestion"passed 244/244 on macOS. - I did not run the OpenMS tests myself, since I could not build here. The harness results above go further than the class test: they cover all
(start, end)combinations and a real proteome.
Measurements
- Gap: the PR gives no timing for this change. The change preserves output, but its stated purpose is speed and less contention for the 19 newly covered patterns.
- My standalone measurement of
tokenize_()(same harness, human proteome, best of 3, 4 cores; system Boost 1.83, not OpenMS's vcpkg Boost):- Full proteins: the
digestUnmodified()/ProteaseDigestion::digest()call shape. - Windows:
isValidProduct()'spos-2 .. end+2call for each fully specific product of 7–40 residues (the PeptideIndexer shape).
- Full proteins: the
| enzyme (pattern) | threads | full proteins develop → PR | windows develop → PR |
|---|---|---|---|
Lys-C (?<=[KX])(?!P) |
1 | 446 → 21 ms (21x) | 412 → 28 ms (15x) |
| 4 | 239 → 6.4 ms (38x) | 486 → 6.7 ms (72x) | |
Arg-C (?<=[RX])(?!P) |
1 | 358 → 17 ms (21x) | 333 → 23 ms (15x) |
| 4 | 212 → 6.6 ms (32x) | 445 → 7.3 ms (61x) | |
Chymotrypsin (?<=[FYWLJX])(?!P) |
1 | 730 → 37 ms (20x) | 568 → 43 ms (13x) |
| 4 | 510 → 12 ms (42x) | 582 → 19 ms (30x) | |
V8-E (?<=[EZX])(?!P) |
1 | 513 → 25 ms (20x) | 406 → 27 ms (15x) |
| 4 | 250 → 7.3 ms (34x) | 403 → 9.8 ms (41x) | |
TrypChymo (?<=[FYWLJKRX])(?!P) |
1 | 830 → 56 ms (15x) | 632 → 42 ms (15x) |
| 4 | 914 → 18 ms (50x) | 673 → 14 ms (50x) | |
Trypsin (?<=[KRX])(?!P) (hand path on develop already) |
1 | 75 → 35 ms (2.2x) | 85 → 37 ms (2.3x) |
| 4 | 17 → 7.5 ms (2.3x) | 21 → 12 ms (1.8x) |
- All 19 newly covered patterns:
- 1 thread: 13–34x on full proteins (median 19x) and 10–15x on windows (median 14x).
- 4 threads: 32–66x on full proteins (median 43x) and 29–72x on windows (median 47x).
- Contention: the numbers support the PR's explanation. On the window workload the regex path got slower with 4 threads than with 1 for 12 of the 19 patterns (0.66–1.21x scaling). The hand path scaled 2.2–4.5x.
- Trypsin: it is not slower. It is about 2x faster than develop's literal-character path, presumably because of the table lookup and the dropped
re_.str()copy. - Optional: this is
tokenize_()alone. One end-to-end timing would show what users gain, for example PeptideIndexer on a Lys-C or Chymotrypsin idXML, or a semi-specific ProSE run, where FragmentIndex falls back to the library digest and so totokenize_(). A sentence in the description noting that everyProteaseDigestionuser with these enzymes benefits, not only ProSE, would also help.
Generated by Claude Code
Follow-up to #10807, answering point 1 of @timosachsenberg's review: the code base had two hand-written copies of the same cleavage-rule semantics, and the regex-free tokenizer applied only to the two literal tryptic patterns.
What moved
SimpleCleavageRuleand its parser leave FragmentIndex's anonymous namespace and become the nested structEnzymaticDigestion::SimpleCleavageRule(OPENMS_DLLAPI) with a staticparse(): a set of residues after which the enzyme cleaves, optionally one residue that blocks the cleavage, i.e. every pattern of the form(?<=[letters])or(?<=[letters])(?!L).EnzymaticDigestionparses the rule once per enzyme and keeps it next tore_(std::optional<SimpleCleavageRule> simple_rule_), set in the constructor and insetEnzyme().ProteaseDigestion::setEnzyme(const std::string&)used to assignenzyme_andre_directly; it now goes through the base setter, so the cached rule follows the enzyme there too (the pitfall the review named).FragmentIndex::generatePeptides()uses the shared type; its regex-free digest is unchanged otherwise.Effect
tokenize_()takes the hand path for every enzyme whose pattern the parser covers: Trypsin, Trypsin/P, Lys-C, Lys-C/P, Arg-C, Arg-C/P, Chymotrypsin, Chymotrypsin/P, Glu-C, glutamyl endopeptidase, PepsinA, CNBr, elastase, TrypChymo, ... (21 of the 30 patterns inEnzymes.xml, and a custom entry that spells trypsin as(?<=[KR])(?!P)). The nine others ((),(?<=[A-Z]), the Asp-N lookahead rules,(?<=[HKRX][PX])(?!P),(?<=[DBX])(?=[DBX]), ...) keep the regular expression. The per-callre_.str()copy is gone from the hand path. Output is unchanged: for a given pattern the tokens are the same as before, only computed without boost.Tests
EnzymaticDigestion_test:SimpleCleavageRule::parse(): the accepted shapes (Trypsin, Trypsin/P, Lys-C) with the residues they cleave after and block on, and sixteen rejected patterns (no cleavage,(?<=W),(?<=[A-Z]), lower case, unbalanced brackets, two blocking residues, lookahead rules, the two-residue lookbehind, ...);ProteaseDBwhose pattern parses:countInternalCleavageSites(),digestUnmodified()and everyisValidProduct()window of random sequences rich in the rule's residues, across the three specificities, hand path against the regex path oftokenize_()on the same pattern (a test-local subclass clears the cached rule, so the comparison no longer depends on respelling the regex). It also asserts which enzymes take which path.FragmentIndex_test,ProSEAlgorithm_test,ProteaseDigestion_testand the TOPP ProSE tests cover the FragmentIndex side and the by-name setter.Local (macOS, AppleClang 21, Release):
ctest -R "EnzymaticDigestion|FragmentIndex|ProSE|DigestionEnzyme|ProteaseDigestion"passes 244/244, including the two new sections.🤖 Generated with Claude Code
Summary by CodeRabbit