Skip to content

fix: handle missing Precursor (None) in spectrum parsing, annotation, and MS2PIP feature generation - #284

Open
rpinho wants to merge 1 commit into
CompOmics:mainfrom
rpinho:fix-280-missing-precursor
Open

rpinho wants to merge 1 commit into
CompOmics:mainfrom
rpinho:fix-280-missing-precursor

Conversation

@rpinho

@rpinho rpinho commented Nov 26, 2026

Copy link
Copy Markdown

Summary

When callers construct ms2rescore_rs.MS2Spectrum(identifier=..., mz=..., intensity=...) without a precursor (relying on the charge in the ProForma string), annotate_spectra() produces an AnnotatedMS2Spectrum with precursor=None. Passing those spectra into MS2PIPFeatureGenerator.add_features() then crashes inside ms2pip._spectrum_processing._to_observed_spectrum() (ms2pip 4.2.0) with AttributeError: 'NoneType' object has no attribute 'mz'. Separately, add_precursor_values() in ms2rescore/parse_spectra.py unconditionally reads ms2.precursor.{rt,im,mz} and raises AttributeError if a spectrum reader yields precursor=None.

Who hits this: Python API users passing ms2rescore_rs.MS2Spectrum objects without a Precursor (charge supplied via ProForma) to annotate_spectra() and MS2PIPFeatureGenerator.add_features() (#280).

Changes

  • ms2rescore/parse_spectra.py:
    • Add _ensure_precursor() to construct a new MS2Spectrum or AnnotatedMS2Spectrum with default Precursor() when spectrum.precursor is None, without mutating caller objects.
    • Update annotate_spectra() to normalize precursor=None inputs via _ensure_precursor() so annotated spectra are directly consumable by downstream tools such as ms2pip.correlate().
    • Update add_precursor_values() to treat ms2.precursor is None as (0.0, 0.0, 0.0) instead of crashing on ms2.precursor.rt.
  • ms2rescore/feature_generators/ms2pip.py:
    • In MS2PIPFeatureGenerator.add_features(), temporarily substitute _ensure_precursor() around ms2pip.correlate() whenever any psm.spectrum.precursor is None (zero copies when precursors are already present) and restore the original spectrum objects in a finally: block even if correlate() raises.
  • tests/test_parse_spectra.py & tests/test_feature_generators_ms2pip.py:
    • Add 10 regression tests covering add_precursor_values(), annotate_spectra() (with extended=False/True), MS2PIPFeatureGenerator.add_features() (raw, annotate_spectra, and pre-annotated precursor=None inputs), mixed explicit/missing precursors, and missing-charge controls (confirming zero fabricated charges or features).

Note: _to_observed_spectrum() itself lives in CompOmics/ms2pip, so a complementary precursor is None guard in ms2pip would also help direct ms2pip.correlate() callers.

Verification

  • With real HCD ms2pip weights, raw, annotate_spectra, and pre-annotated precursor=None inputs match all 71 MS2PIPFeatureGenerator features of precursor=Precursor() controls bit-for-bit, explicit Precursor metadata is preserved verbatim, and original spectrum objects are restored even after a forced exception inside correlate().
  • Non-slow unit test suite passes (120 passed, 4 deselected vs 110 passed, 4 deselected on main), along with ruff check (on ms2rescore/ and both touched test files) and mypy on the modified modules.
  • Found and written with AI agents; a separate cross-family AI referee independently reproduced MS2PIPFeatureGenerator: _spectrum_processing._to_observed_spectrum() should handle missing Precursor #280 and verified the patch, real-weight feature parity, exception restoration, and test/lint suites before this PR.

Fixes #280

🤖 Generated with Claude Code

https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

MS2PIPFeatureGenerator: _spectrum_processing._to_observed_spectrum() should handle missing Precursor

1 participant