Skip to content

[FIX] Chromatogram extraction: count the first and last peak of a spectrum exactly once - #10673

Merged
timosachsenberg merged 1 commit into
OpenMS:developfrom
timosachsenberg:fix/tophat-extraction-edges
Oct 20, 2026
Merged

timosachsenberg merged 1 commit into
OpenMS:developfrom
timosachsenberg:fix/tophat-extraction-edges

Conversation

@timosachsenberg

@timosachsenberg timosachsenberg commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

ChromatogramExtractorAlgorithm::extract_value_tophat (both the plain and the ion mobility overload) summed the peaks inside the m/z window incorrectly at the edges of a spectrum. This is the shared OpenSWATH extractor used by OpenSwathWorkflow, OpenDIA and OpenSwathChromatogramExtractor. The first-centroid case is the one noted in the OpenDIA plan (#10661); the double count at the end of the spectrum is new.

  • First peak dropped. When the walk to the left reached the first peak after more than one step, the loop stopped at mz_start without adding it. The special case for the first peak only covered a single step.
  • Last peak counted twice. When the target m/z lay beyond the last peak, mz_it == mz_end. The last peak was added as the current peak and again by the walk to the left.

One spectrum, window 1 Th (±0.5), same result for both overloads:

Peaks (m/z : intensity) Target Before After
100.0:1, 100.1:10, 100.2:100, 100.3:1000 100.25 1110 1111
100.0:1, 100.1:10, 100.2:100 100.3 210 111
100.0:5 100.1 10 5

The walk to the left now visits every peak down to and including the first one. The current peak is only added if the target is not past the end of the spectrum. The walk to the right is unchanged.

Both cases only occur at the first or last peak of a spectrum, so the effect on real data should be small. The extracted values are still a change in behaviour and now match the documented sum over the window.

Tests

  • New [EXTRA] section in ChromatogramExtractorAlgorithm_test covering the cases above for both overloads. It failed before the fix (1110, 210, 10) and passes now.
  • Four existing expectations encoded the first-peak bug and are corrected. The first peak (400.0, intensity 8) is inside those windows: 400.05 ± 0.1 Th and 500 ppm give 8408 instead of 8400, 400.1 at 500 ppm (±0.100025) gives 9008 instead of 9000, and the IM case gives 4108 instead of 4100. Their comment formulas came from the Python generator, where the first intensity is 0, while the C++ array uses 8.
  • No checked-in TOPP output changes. I compared all 32 chromatogram outputs written by the OpenSWATH/MRM TOPP tests against their references, and they are identical at 1e-6 relative.
  • Full local ctest passes: 2,987/2,987 tests, built with GUI and Thermo RAW off and LP_SOLVER=COIN.

Checklist

  • Make sure that you are listed in the AUTHORS file
  • I have commented my code, particularly in hard-to-understand areas
  • New and existing unit tests pass locally with my changes
  • Updated or added python bindings for changed or new classes (Tick if no updates were necessary.)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved chromatogram extraction near spectrum boundaries.
    • Corrected handling of first and last peaks for both standard and ion-mobility extraction.
    • Ensured boundary peaks are included when they fall within the requested m/z range.
  • Tests

    • Added regression coverage for single-peak spectra, out-of-range targets, boundary peaks, and ion-mobility filtering.
    • Updated expected integrated intensities to reflect corrected peak inclusion.

…ctrum exactly once

ChromatogramExtractorAlgorithm::extract_value_tophat (with and without ion
mobility) summed the peaks inside the m/z window incorrectly at the edges of a
spectrum:

- The first peak was dropped when the walk to the left reached it after more
  than one step: the loop stopped at mz_start without adding it, and the
  special case for the first peak only covered a single step.
- When the target m/z lay beyond the last peak, the last peak was added twice:
  once as the current peak and once more by the walk to the left.

The walk to the left now visits every peak down to and including the first
one, and the current peak is only added if the target is not past the end of
the spectrum.

Four existing test expectations encoded the first bug: the first peak (400.0,
intensity 8) lies inside those windows, so the sums are 8408/4108/9008, not
8400/4100/9000. None of the chromatograms written by the OpenSWATH and MRM
TOPP tests change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f8d360c6-d4aa-4aba-829c-addbcacf5472
📥 Commits

Reviewing files that changed from the base of the PR and between 2b8ceb0 and 91b4313.

📒 Files selected for processing: 2
  • src/openms/source/ANALYSIS/OPENSWATH/ChromatogramExtractorAlgorithm.cpp
  • src/tests/class_tests/openms/source/ChromatogramExtractorAlgorithm_test.cpp

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

📝 Walkthrough

The top-hat extraction overloads now check the current peak and traverse left within the m/z window. The ion-mobility overload retains ion-mobility filtering. Updated expectations and regression tests cover boundary peaks, targets beyond spectrum bounds, and single-peak spectra.

Changes: Top-hat extraction

Layer / File(s) Summary
Peak traversal and regression coverage
src/openms/source/ANALYSIS/OPENSWATH/ChromatogramExtractorAlgorithm.cpp, src/tests/class_tests/openms/source/ChromatogramExtractorAlgorithm_test.cpp
Both overloads conditionally add the current peak and walk left while peaks remain within the m/z window. The ion-mobility overload filters accepted peaks by ion mobility. Updated expectations include boundary peaks, and regression tests cover spectrum bounds and single-peak spectra.

Priority: ⬇️ Low

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 91b43

The boundary-peak fix and regression tests are ready to merge after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and specifically describes the main fix: counting the first and last spectrum peaks exactly once during chromatogram extraction.
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.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · 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

A rabbit checks the peak in sight,
Then hops left through the window right.
The first peak joins the measured sum,
While bounds keep extra peaks from some.
One lone peak gets counted too,
In mobility’s filter, it passes through.

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

@timosachsenberg
timosachsenberg merged commit 110a409 into OpenMS:develop Oct 20, 2026
13 checks passed
singjc pushed a commit to singjc/OpenMS that referenced this pull request Oct 21, 2026
…ctrum exactly once (OpenMS#10673)

ChromatogramExtractorAlgorithm::extract_value_tophat (with and without ion
mobility) summed the peaks inside the m/z window incorrectly at the edges of a
spectrum:

- The first peak was dropped when the walk to the left reached it after more
  than one step: the loop stopped at mz_start without adding it, and the
  special case for the first peak only covered a single step.
- When the target m/z lay beyond the last peak, the last peak was added twice:
  once as the current peak and once more by the walk to the left.

The walk to the left now visits every peak down to and including the first
one, and the current peak is only added if the target is not past the end of
the spectrum.

Four existing test expectations encoded the first bug: the first peak (400.0,
intensity 8) lies inside those windows, so the sums are 8408/4108/9008, not
8400/4100/9000. None of the chromatograms written by the OpenSWATH and MRM
TOPP tests change.

Co-authored-by: Claude Opus 5.5 <[email protected]>
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