Skip to content

fix(flashlfq): bound each peak window to one charge state - #1130

Open
trishorts wants to merge 1 commit into
smith-chem-wisc:PeakEnvelopeOutputfrom
trishorts:fix/peak-window-per-charge-state
Open

fix(flashlfq): bound each peak window to one charge state#1130
trishorts wants to merge 1 commit into
smith-chem-wisc:PeakEnvelopeOutputfrom
trishorts:fix/peak-window-per-charge-state

Conversation

@trishorts

Copy link
Copy Markdown
Contributor

@Alexander-Sol — this targets PeakEnvelopeOutput, so it lands inside #1122 rather than alongside it. It's the one blocking item from my review there; the other four findings are design calls I've left to you.

The problem

ChromatographicPeak.IsotopicEnvelopes holds one envelope per scan per charge state — that is what NumChargeStatesObserved counts. PeakWindow.Create took a single Min/Max of IndexedPeak.M across all of them, so a peptide traced at more than one charge got one window spanning the gap between the charge states.

Measured on PeakEnvelopeOutput before this change, with EGFQVADGPLYR (1350.66 Da) at z=2 (676.3 m/z) and z=3 (451.2 m/z), over a synthetic MS1 with one peak every 0.5 m/z:

NumChargeStatesObserved = 2
Window    = 450.700 - 676.800 m/z   (width 226.1 Da)
PeakCount = 1356 over 3 scans       (452 peaks/scan)
TSV rows for this ONE peak = 1356

[same peptide, single charge] width 1.0 Da, PeakCount = 6, rows = 6

226x the rows, and essentially none of that 226 Da is co-elution around the precursor — it's the rest of the spectrum. On real Orbitrap MS1 data (10-50k peaks over 350-1500 m/z) the output is effectively unbounded. I suspect the 200 Da window you benchmarked in the PR description is exactly this rather than a pathological case.

The change

  • PeakWindow.Create now takes the chargeState whose envelopes bound the window, and only those envelopes set MinMz/MaxMz, the scan span, and the peakfinding flags.
  • PeakWindow.CreateForEachChargeState yields one window per traced charge state, ascending.
  • PeakWindow.ChargeState is exposed, and the Peak Charge column now carries the window's charge state rather than Apex?.ChargeState ?? 0.
  • WritePeakWindows writes every charge state of a peak under that peak's single peakId, distinguished by Peak Charge. The correspondence with QuantifiedPeaks that the comment there promises is unchanged.
  • ApexRetentionTime is now the apex of the window's own charge state. The peak's overall apex can belong to a different charge state whose scan lies outside the window entirely, which would have put an out-of-range RT in the RT Apex column.
  • Peak RT Start / Peak RT Apex / Peak RT End renamed to Window RT *, since they describe the window's scan span and that span is now per charge state.

Tests

Your 13 tests still pass, updated for the new signature. Two added:

  • TestPeakWindowIsBoundedPerChargeState — the regression above. Builds the two-charge peak over a spectrum with a peak every 0.5 m/z, asserts two windows in ascending charge order, each exactly 2 * mzExpansion wide and centred on its own m/z, and that the total peak count stays under 50 where the old bounding box swept in ~1356.
  • TestPeakWindowApexIsPerChargeState — z=2 apexes in the first scan and z=3 in the last; asserts each window reports its own apex rather than the peak's.

TestWritePeakWindows additionally asserts every row's Peak Charge is a charge the peak was actually traced at, and that every traced charge state contributed rows.

Also added BuildMultiChargePeak, so a test can build the peak FlashLFQ actually produces for a multiply-charged peptide; BuildPeak now delegates to it and is unchanged for callers.

Verification

Take it, adapt it, or close it — whichever is least friction for you.

A ChromatographicPeak carries one isotopic envelope per scan per charge
state, so taking Min/Max of IndexedPeak.M across all of them produced a
single window spanning the gap between the charge states rather than a
window around the peak. For EGFQVADGPLYR traced at z=2 (676.3 m/z) and
z=3 (451.2 m/z) that is a 226 Da window whose contents are the rest of
the spectrum, and 226x the rows of the single-charge case.

PeakWindow.Create now takes the charge state whose envelopes bound the
window, and CreateForEachChargeState yields one window per charge state
in ascending order. WritePeakWindows writes them all under the peak's
single id, told apart by the Peak Charge column, so the correspondence
with QuantifiedPeaks is unchanged.

ApexRetentionTime is now the apex of the window's own charge state: the
peak's overall apex can belong to another charge state whose scan lies
outside the window. Peak RT Start/Apex/End are renamed Window RT * to
match what they have always measured.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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