Skip to content

refine: pair each Pass-2 decoy with its anchor peptide - #100

Merged
ypriverol merged 1 commit into
mainfrom
fix/refine-decoy-balance
Oct 3, 2026
Merged

ypriverol merged 1 commit into
mainfrom
fix/refine-decoy-balance

Conversation

@ypriverol

@ypriverol ypriverol commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Problem

--refine Pass 2 searches each confident peptide as its own mini-protein. Its decoy was the whole sequence reversed, which breaks the target/decoy pairing for single peptides:

  • The C-terminal K/R moves to the front. PEPKTIDEK → KEDITKPEP: the full-length decoy needs one more missed cleavage than its target, so under the default limit of one it is never generated.
  • An N-terminal Q/E moves to the C-terminus, so pyro-Glu candidates exist only on the target side.
  • The decoy pieces that are generated (EDITKPEP, PEPK…) sit at other masses and rarely compete with a modified target for the same spectrum.

Fix

Each Pass-2 decoy keeps its anchor's first and last residue and reverses (or shuffles, under --decoy-strategy shuffle) only the interior: PEPKTIDEK → PEDITKPEK. Cleavage pattern and N-terminal modification eligibility now match the target. The [REFINE] log line also reports Pass-2 target/decoy candidate counts so an imbalance is visible.

New test anchored_decoys_keep_termini_and_pair_full_length_with_targets asserts equal full-length target/decoy forms per anchor (missed cleavage, N-terminal Q/E, internal K); it fails on the old code.

Measured (Astral HYE, --refine, Percolator 3.7.1)

main this PR
Pass-2 candidates, targets / decoys 213,012 / 281,055 213,024 / 214,255
Pass-2 PSM rows in PIN, targets / decoys 571,662 / 222,368 391,319 / 382,581
PSMs at 1% q, total (seed 1) 61,096 50,013
— Pass-2 (modified) 22,842 9,879
— Pass-1 38,254 40,134

On main, Pass-2 targets outnumbered decoys 2.6 : 1 at the PSM level, so Percolator accepted modified PSMs against too few decoys. With paired decoys, accepted Pass-2 PSMs fall 57%; Pass-1 rises because fewer spurious modified PSMs displace correct unmodified ones. Earlier --refine gains measured on main were inflated by this (entrapment cannot see Pass-2, so it never caught it).

Seeds 2–3 and a no---refine control are running; I'll add them here.

Known remaining issue (not fixed here)

On main, of 16,610 accepted deamidated PSMs only 18% have a precursor residual near 0 after removing whole isotope steps; 54% sit at +12 to +26 mDa, where an unmodified peptide picked one isotope high lands (1.0034 − 0.9840 = 19 mDa). Balanced decoys cannot model that artefact, so it needs its own guard (an isotope-residual check or fragment-ion localization for deamidation). Follow-up.

Summary by CodeRabbit

  • Bug Fixes
    • Peptide-anchored decoys now retain the target peptide’s first and last residues while applying the selected strategy to the interior, preserving full-length target/decoy pairing. This also applies to anchors with missed cleavages and eligible N-terminal pyro-Glu.
  • Refactor
    • Refinement logs now report target and decoy candidate counts separately.

…mini)

Pass 2 searches each confident peptide as its own mini-protein and decoyed
it by reversing the whole sequence. That moves the C-terminal K/R to the
front: PEPKTIDEK becomes KEDITKPEP, whose full-length form needs one more
missed cleavage than its target and is never generated under the default
limit. Reversal also moves an N-terminal Q/E to the C-terminus, so
pyro-Glu candidates existed only on the target side. The decoy pieces
that were generated sit at the wrong masses and rarely compete with a
modified target.

Decoys now keep the anchor's first and last residue and reverse (or
shuffle) only the interior. On Astral the Pass-2 PSM rows go from
571,662 targets / 222,368 decoys to 391,319 / 382,581. The [REFINE] log
line now reports the Pass-2 target/decoy candidate counts.
@qodo-code-review

Copy link
Copy Markdown

ⓘ Qodo reviews are paused because the subscription is no longer active. Ask your workspace admin to reactivate the subscription to resume reviews. Manage billing

@coderabbitai

coderabbitai Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 6a19bd04-801a-45d4-805b-614e4e6630b1
📥 Commits

Reviewing files that changed from the base of the PR and between 627ba30 and 84ddaa5.

📒 Files selected for processing (1)
  • crates/search/src/refinement.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

Peptide-anchored decoys now preserve target endpoints while applying the selected strategy to sequence interiors. A test checks full-length target and decoy candidate parity. Refinement summaries report target and decoy candidate counts separately.

Changes

Peptide-anchored decoy refinement

Layer / File(s) Summary
Anchored decoy generation and parity
crates/search/src/refinement.rs
Decoys are generated from sequence interiors, then the target’s first and last residues are restored for sequences of at least two residues. A test checks candidate parity for anchors with missed cleavages and N-terminal Q/E cases.
Pass-2 candidate counts
crates/search/src/refinement.rs
The refinement summary reports total Pass-2 candidates as separate target and decoy counts.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 84dda

No blocking issue was identified with anchored decoy generation or the Pass-2 count reporting. The change is mergeable after normal checks.

Architecture Summary

Architecture risk: 🟡 Medium · up to 84dda

The change affects 1 system.

Changed systems: crates

Architecture concerns
No architecture-level concerns identified.

Review details

Systems and components

  • observed — crates (service) was modified; 1 changed file maps to changed impact.

Before / after behavior

  • observed — Modified behavior in crates/search/src/refinement.rs: build_peptide_anchored_index now uses anchored_target_plus_decoy instead of generating decoys from whole mini-protein sequences. The helper generates decoys from each sequence’s interior, then restores the target’s first and last residues for sequences of length at least two; shorter sequences retain the generated sequence unchanged.
  • observed — Modified behavior in crates/search/src/refinement.rs: Adds a count of decoy candidates in the Pass-2 candidate pool for the refinement summary.
  • observed — Modified behavior in crates/search/src/refinement.rs: The refinement summary now reports total Pass-2 candidates split into target and decoy counts.
  • observed — Modified behavior in crates/search/src/refinement.rs: Adds a test that builds reverse decoys for anchors including missed-cleavage and N-terminal Q/E cases, then checks that candidate enumeration produces equal numbers of full-length target and decoy forms for every anchor.

Reliability and maintainability

  • inferred — Risk-relevant change factors for crates: blast_radius_1; blast_radius_3; direct_dependents_1; direct_dependents_3
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: pairing each Pass-2 decoy with its anchor peptide.
Docstring Coverage ✅ Passed Docstring coverage is 85.71% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 1 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ypriverol

Copy link
Copy Markdown
Contributor Author

Full results: 3 Percolator seeds + a no---refine control (Astral HYE)

PSMs at 1% q, seeds 1 / 2 / 3:

arm total Pass-2 (modified)
no --refine 41,437 / 41,323 / 41,360 —
--refine, main 61,096 / 61,679 / 61,460 22,842 / 23,210 / 23,039
--refine, this PR 50,013 / 49,913 / 49,978 9,879 / 9,835 / 9,854

The seed spread is about 1%, so the drop is not noise. With paired decoys, --refine adds about +8,600 PSMs over the no-refine control (+21%), down from +20,000 (+48%) on main.

Most of the remaining gain is still suspect. On the fixed arm, Percolator seed 1 accepts 8,042 deamidated PSMs. Their precursor mass residual after removing whole isotope steps splits like this:

  • 1,147 (14%) sit near 0, as a real deamidation would.
  • 4,869 (61%) sit at +12 to +26 mDa, where an unmodified peptide picked one isotope too high would sit.

Paired decoys cannot model that artefact. Until the follow-up guard exists, the honest --refine gain on this data is well below +21%.

Also: the fixed arm's Pass-1 count (40,134, seed 1) is about 1,300 below the no-refine control, so Pass-2 rows still displace some correct unmodified PSMs.

CI is green.

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.

1 participant