Skip to content

[FEATURE] EnzymaticDigestion: one SimpleCleavageRule parser shared with FragmentIndex; the hand path of tokenize_() for every simple rule - #10810

Merged
timosachsenberg merged 1 commit into
OpenMS:developfrom
okohlbacher:claude/prose-cleavage-rule-20261103
Nov 3, 2026

Conversation

@okohlbacher

@okohlbacher okohlbacher commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

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

  • SimpleCleavageRule and its parser leave FragmentIndex's anonymous namespace and become the nested struct EnzymaticDigestion::SimpleCleavageRule (OPENMS_DLLAPI) with a static parse(): 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).
  • EnzymaticDigestion parses the rule once per enzyme and keeps it next to re_ (std::optional<SimpleCleavageRule> simple_rule_), set in the constructor and in setEnzyme(). ProteaseDigestion::setEnzyme(const std::string&) used to assign enzyme_ and re_ 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 in Enzymes.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-call re_.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:

  • a new section for 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, ...);
  • the equivalence section from [FEATURE] EnzymaticDigestion, TheoreticalSpectrumGenerator, PercolatorInfile: the core-class pieces of the ProSE speed rounds (output unchanged) #10807 now runs over every enzyme of ProteaseDB whose pattern parses: countInternalCleavageSites(), digestUnmodified() and every isValidProduct() window of random sequences rich in the rule's residues, across the three specificities, hand path against the regex path of tokenize_() 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_test and 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

  • Improvements
    • Peptide digestion now handles a broader range of enzyme cleavage rules, including rules that prevent cleavage before specific residues.
    • Enzyme patterns that do not match the supported simple-rule format continue to use the existing regular-expression handling.
  • Tests
    • Expanded coverage checks cleavage and blocking-residue behavior across supported enzyme rules and digestion specificity modes.

…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]>
@OpenMS OpenMS deleted a comment from coderabbitai Bot Nov 3, 2026

@timosachsenberg timosachsenberg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

looks good to me

@timosachsenberg timosachsenberg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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's SimpleCleavageRule::parse() verbatim into one program, built with g++ 13 -O2 against Boost.Regex 1.83, and compiled each regex as RegularExpression::Impl does (boost::regex(pattern), default flags). I ran it on all 30 distinct RegEx values in Enzymes.xml and 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-tree against current develop (995044a5, after #10740 rewrote much of FragmentIndex.cpp) is clean. The merged file has no leftover reference to the removed anonymous-namespace parser.
  • CI: when I looked, header-set-compatibility was 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 start in [−2, n] and every end in [−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()-style pos-2 .. end+2 windows (every fully specific product with up to 2 missed cleavages). 0 mismatches.
  • The cached rule follows the enzyme. Every path that sets enzyme_ now also sets simple_rule_:

    No other code assigns enzyme_ or re_.

  • The by-name pitfall is pinned by an existing test (checked by reading, not by running). In ProteaseDigestion_test, the same object goes from Trypsin/P (L388) to glutamyl endopeptidase by name (L594). L601, isValidProduct(prot, 6, 8, true, true, false) under SPEC_SEMI, expects true. With a stale Trypsin/P rule, neither 6 nor 14 would be a site and the call would return false. So reverting the ProteaseDigestion.cpp hunk should make that test fail.

  • FragmentIndex: the gating condition (SPEC_FULL, not unspecific, pattern parses) and digestSimpleCleavage() are unchanged apart from the type and cleavesBetween().

  • Export: a nested struct OPENMS_DLLAPI inside 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 ProteaseDB enzyme. A subclass clears the cached rule, so both paths of tokenize_() 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.
  • 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()'s pos-2 .. end+2 call for each fully specific product of 7–40 residues (the PeptideIndexer shape).
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 to tokenize_(). A sentence in the description noting that every ProteaseDigestion user with these enzymes benefits, not only ProSE, would also help.

Generated by Claude Code

@timosachsenberg
timosachsenberg merged commit bb5d968 into OpenMS:develop Nov 3, 2026
29 checks passed
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