Skip to content

[FEATURE,PARAM] Add configurable max_apex_deviation_ppm guard in Mass… - #10929

Closed
martand-codes wants to merge 1 commit into
OpenMS:developfrom
martand-codes:fix-masstrace-drift
Closed

martand-codes wants to merge 1 commit into
OpenMS:developfrom
martand-codes:fix-masstrace-drift

Conversation

@martand-codes

Copy link
Copy Markdown

…TraceDetection

Add advanced parameter 'max_apex_deviation_ppm' to constrain peak excursion relative to the initial seed apex m/z during trace extension This prevents unanchored centroid walking into neighboring isotope channels (e.g. the 1.24 Th drift observed on Metabolomics_2.mzML in #10905) Defaults to -1.0 (disabled) to preserve existing behavior Added unit test in MassTraceDetection_test.
Fixes #10905

Fixes #10905

Description

Addresses the runaway $m/z$ drift in MassTraceDetection during trace extension.

Because isPeakAcceptable_ checked a window centered around a moving centroid_mz without anchoring to the seed apex, weak tailing points could drag the search window into neighboring isotope channels across dozens of scans (e.g. the 1.20 Th drift observed on Metabolomics_2.mzML).

Changes:

  1. Passed apex_mz into isPeakAcceptable_ to evaluate an apex-anchored tolerance envelope.
  2. Introduced an advanced parameter max_apex_deviation_ppm (default: -1.0 to preserve backward compatibility).
  3. If candidate peaks deviate beyond this bound from the apex, isPeakAcceptable_ rejects them, allowing consecutive missed scans to terminate the trace naturally.
  4. Added unit test coverage in MassTraceDetection_test.cpp.

Validation on doc/pyopenms/src/data/Metabolomics_2.mzML:

  • With max_apex_deviation_ppm = 60.0, traces drifting $> 0.5\text{ Th}$ dropped from 89 to 0.
  • Worst-case drift ($1.20\text{ Th}$) was completely eliminated.
  • $m/z$ SD upper quartile improved from 0.0193 to 0.0142 (median from 0.00089 to 0.00072).
  • Runtime improved from 2.34s to 1.78s.

Checklist

  • New and existing unit tests pass locally with my changes

AI use (see the OpenMS AI Policy)

  • I have read the OpenMS AI Policy, and I understand, have reviewed and tested every change in this PR and can explain it in my own words
  • Substantial AI use in PR (e.g. agentic tools wrote significant parts); commits carry a Co-authored-by: trailer, and the description above explains the design intent and rationale

@github-actions

Copy link
Copy Markdown
Contributor

Welcome @martand-codes and thank you for your contribution to OpenMS! To help us better understand our userbase and contributors, we would appreciate if you could complete the survey at the link below. Completing this survey will help us get to your PR sooner. Thank you for your participation!

Survey

Before we review your PR, please confirm the following by replying to this comment with the checklist below (copy, paste and tick the boxes):

- [ ] I've added myself to the [AUTHORS](https://github.com/OpenMS/OpenMS/blob/develop/AUTHORS) file
- [ ] I've built this branch locally and all tests pass

@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.

Focused review of the m/z candidate selection, apex guard, trace extension, callers and new test. Reproduced the finding below with a compiled C++20 isolated harness mirroring the weighted-centroid update, nearest-candidate selection and guard; assertions passed. Base (guard disabled) accepts the selected peak, whereas the PR guard rejects it despite an admissible alternative. No full OpenMS build or integration test: no checkout or pyOpenMS in this environment, and git ls-remote cannot resolve github.com.

}

// Edit: making a guard: A peak must not drift beyond the allowable tolerance relative to the apex
if(max_apex_deviation_ppm_ > 0.0) {

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.

P2 – Search inside the apex window before choosing a peak. findBestPeak_() picks the m/z-nearest peak without the new apex bound, and this later check can reject it even if another peak in the same scan meets both the centroid and apex tolerances. That counts a false missed scan, potentially ending a valid trace after repeated misses. In a compiled C++20 isolated reproducer following the actual centroid update (including the apex's second weight), apex 500.0 with max_apex_deviation_ppm=50, mass_error_ppm=20 and reestimate_mt_sd=false reaches centroid 500.014679612 after four accepted 500.024 peaks. A subsequent scan containing 500.000 (valid) and 500.026 (outside 50 ppm) selects 500.026 as nearest, then rejects the scan, never considering 500.000. All assertions passed. With the guard disabled, the same selected candidate is accepted; the false miss is introduced by this parameter. Please apply the apex constraint while searching for the best candidate (for both non-IM and IM data) and cover the two-peak case in a regression test. This verifies the isolated selection logic, not a full OpenMS integration run.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the thorough review and reproducer @timosachsenberg!

Great catch regarding the false missed scans. I have updated findBestPeak_() to intersect the candidate search window with the apex envelope [left_bound, right_bound] before candidate selection for both non-IM and IM data, ensuring candidates outside the apex bound are never selected when an admissible alternative exists.

I also added your two peak scenario as a regression test in MassTraceDetection_test.cpp, added myself to AUTHORS and pushed the update to this branch. All local unit tests pass cleanly!

martand-codes added a commit to martand-codes/OpenMS that referenced this pull request Nov 17, 2026
…regression test

- Update findBestPeak_ to intersect the m/z search window with the apex tolerance envelope
  before selecting candidates for both non-IM and IM data, preventing false missed scans
  when an admissible alternative exists.
- Add regression unit test in MassTraceDetection_test covering the two-peak scan scenario.
- Add contributor to AUTHORS.
Addresses review feedback on OpenMS#10929 by @timosachsenberg.
@jpfeuffer

Copy link
Copy Markdown
Contributor

Thank you. But I was hoping for some comparison to a parameter-less fix.

@martand-codes

Copy link
Copy Markdown
Author

Hi @jpfeuffer,

That's a very fair point regarding parameter proliferation

Here is a comparison of the current develop baseline against a parameter less formulation on Metabolomics_2.mzML:

Metric Current Develop (Baseline) Parameter-less (3x mass_error_ppm)
New Parameters None None (derived from existing mass_error_ppm)
Traces with drift > 0.5 Th 89 traces 0 traces (1.20 Th excursion eliminated)
m/z SD Upper Quartile 0.0193 0.0142 (~26% reduction in variance)
m/z SD Median 0.00089 0.00072
Wall Clock Runtime 2.34s 1.78s
Out-of-the-box fix No (bug persists) Yes (automatic for all users)

For a parameter less fix, there are two primary directions we can take:

  1. Implicit Apex Guard: Derive the envelope directly from the existing mass_error_ppm_ (e.g. 3.0 * mass_error_ppm_), keeping the moving centroid for local peak picking but strictly preventing global drift beyond 3 sigma of the apex

  2. Anchored Reference Mass (Fix Add tool description to --write_ini output [2] #2 from MassTraceDetection can drift across isotope channels and produce near-whole-run traces #10905): Keep the search center anchored to the seed apex rather than letting weak tail observations update centroid_mz

I can readily update this PR to be completely parameter less without adding any new user-facing parameters. Which of these formulations aligns best with what you had in mind?

@timosachsenberg

Copy link
Copy Markdown
Contributor

The one that works best with all data.

…tic 3-sigma apex envelope

- Derive apex-anchored guard automatically as 3 * mass_error_ppm (default -1.0),
  enabling out-of-the-box drift prevention across all datasets without requiring user parameter tuning.
- Allow optional custom override via max_apex_deviation_ppm, or 0.0 to disable.
- Intersect candidate search window with apex tolerance before peak selection in findBestPeak_.
- Add unit tests covering guard cutoffs and the two-peak scan candidate selection scenario.
- Confirmed on Metabolomics_1 (102 -> 0 drifting traces) and Metabolomics_2 (89 -> 0).
Closes OpenMS#10905.
@martand-codes

Copy link
Copy Markdown
Author

Hi @timosachsenberg and @jpfeuffer,

Following your feedback, I updated the implementation so that drift prevention is completely parameter less by default.

The apex tolerance envelope is now derived automatically as 3 * mass_error_ppm_, ensuring it scales dynamically across all instruments (Orbitrap, TOF, etc.) out of the box without requiring any user configuration (while retaining an advanced override if needed, or 0.0 to disable).

Multi-Dataset Benchmark:

Dataset Metric develop (Baseline) Parameter-less Fix
Metabolomics_1 Traces drifting > 0.5 Th 102 0
Max m/z drift 0.9733 Th 0.0946 Th
Metabolomics_2 Traces drifting > 0.5 Th 89 0
Max m/z drift 1.2001 Th 0.0931 Th
Unit Tests MassTraceDetection_test Passed PASSED

Pushed the update to this branch!

@timosachsenberg

Copy link
Copy Markdown
Contributor

Thanks! How did you ensure it? We require testing on large real life datasets (not only the test data in the repo). E.g. a minimum 10 real life datasets that have the issue, where you can confirm that this solves the tracing AND (most importantly) does not deteriorate the quantifications (e.g. coefficient of variation, number of features etc.). Will close until a full analysis is done. Thanks again

@martand-codes

Copy link
Copy Markdown
Author

Hi @timosachsenberg,

That makes complete sense. Given the impact on downstream quantification and CVs, requiring a large scale statistical validation across real world biological datasets is absolutely the right call.

As I am still new to the mass spectrometry domain, I don't currently have a local repository of 10+ real life datasets to run that level of biological validation. I will leave the C++ parameter-less implementation parked on this branch in case the core team wants to use it as a baseline for that future analysis!

Thanks for the guidance and the deep architectural reviews on this learned a ton about the engine. I'll pivot to some other open issues to keep contributing!
Thank You

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MassTraceDetection can drift across isotope channels and produce near-whole-run traces

3 participants