Skill Tracer
Skills › Scientific Agent Skills › pyopenms › signal_processing.md

signal_processing.md

113 lines · skills/pyopenms/references/signal_processing.md at 5f2bc03

Open on GitHub

Most marks come from matching words in the file, so treat them as pointers.

1# Signal processing (pyOpenMS 3.6.0)
2

Signal processing (pyOpenMS 3.6.0)

Raw lines 1 to 2
3## Decide what the signal represents
4
5Use `spec.getType()` and acquisition/conversion provenance. Its enum is
6`ms.SpectrumSettings.SpectrumType.PROFILE`, `CENTROID`, or `UNKNOWN`.
7`isSorted()` tests m/z ordering only. `getType(True)` can estimate type from the
8signal when metadata is insufficient, but an estimate is not acquisition proof.
9
10`process_spectra.py` applies smoothing, picking, normalization, S/N filtering,
11then intensity thresholding to selected MS levels; chromatograms pass through.
12It rejects centroid spectra for smoothing/picking and requires
13`--assume-profile` for independently verified unknown-type spectra.
14The native tests check a synthetic Gaussian profile, selected-level isolation,
15metadata/peak-array retention and writeback of filtered spectra.
17```bash
18python scripts/process_spectra.py profile.mzML picked.mzML --ms-level 1 --pick
19python scripts/process_spectra.py spectra.mzML scaled.mzML --ms-level 2 --normalize to_one --threshold 0.01
20```
22These are file-dependent templates. Parameters need representative instrument
23QC; smoothing can distort narrow peaks. `PeakPickerHiRes` is for well sampled
24high-resolution profile peaks. Do not repeatedly centroid already picked data.

Decide what the signal represents

Use spec.getType() and acquisition/conversion provenance. Its enum is ms.SpectrumSettings.SpectrumType.PROFILE, CENTROID, or UNKNOWN. isSorted() tests m/z ordering only. getType(True) can estimate type from the signal when metadata is insufficient, but an estimate is not acquisition proof.

process_spectra.py applies smoothing, picking, normalization, S/N filtering, then intensity thresholding to selected MS levels; chromatograms pass through. It rejects centroid spectra for smoothing/picking and requires --assume-profile for independently verified unknown-type spectra. The native tests check a synthetic Gaussian profile, selected-level isolation, metadata/peak-array retention and writeback of filtered spectra.

python scripts/process_spectra.py profile.mzML picked.mzML --ms-level 1 --pick
python scripts/process_spectra.py spectra.mzML scaled.mzML --ms-level 2 --normalize to_one --threshold 0.01

These are file-dependent templates. Parameters need representative instrument QC; smoothing can distort narrow peaks. PeakPickerHiRes is for well sampled high-resolution profile peaks. Do not repeatedly centroid already picked data.

Raw lines 3 to 25
Runs code python scripts/process_spectra.py :18
26## API patterns
28```python
29import pyopenms as ms
30import numpy as np
31spec = ms.MSSpectrum()
32spec.setType(ms.SpectrumSettings.SpectrumType.PROFILE)
33mz = np.linspace(499.9, 500.1, 101)
34spec.set_peaks((mz, 1000 * np.exp(-0.5 * ((mz - 500) / 0.01) ** 2)))
35spec.sortByPosition()
36picker = ms.PeakPickerHiRes()
37picked = ms.MSSpectrum()
38picker.pick(spec, picked)
39assert len(picked) == 1
40assert abs(picked[0].getMZ() - 500) < 0.001
41```
43`GaussFilter.filter(spec)` uses `gaussian_width` in m/z units for a
44spectrum; `use_ppm_tolerance` selects `ppm_tolerance` instead. Chromatogram
45widths use seconds. The 3.6 `filterSpectrum` compatibility alias is broken for these two smoothers;
46use their native `filter(spec)` method. `SavitzkyGolayFilter` uses a point-count `frame_length` and
47`polynomial_order`; validate sampling regularity and window size.
48`PeakPickerIterative` has `signal_to_noise_`, `peak_width`, and
49`nr_iterations_` parameters; this alternative was signature/parameter checked,
50not benchmarked on low-resolution instrument data.
52Normalization (`Normalizer.filterSpectrum`) supports `to_one` and `to_TIC`.
53Thresholds afterwards are on normalized intensities; a threshold of 10 after
54unit-TIC normalization removes all nonnegative peaks. Within-scan normalization
55usually destroys the abundance information needed for label-free quantification.
57For filtering, use `spec.select(indices)` to keep float/integer/string data
58arrays synchronized. `set_peaks()` alone does not filter associated arrays.
59Spectra read with `getSpectrum`, indexing or iteration may be copies: explicitly
60replace the full spectrum list with `exp.setSpectra(processed)`.

API patterns

import pyopenms as ms
import numpy as np
spec = ms.MSSpectrum()
spec.setType(ms.SpectrumSettings.SpectrumType.PROFILE)
mz = np.linspace(499.9, 500.1, 101)
spec.set_peaks((mz, 1000 * np.exp(-0.5 * ((mz - 500) / 0.01) ** 2)))
spec.sortByPosition()
picker = ms.PeakPickerHiRes()
picked = ms.MSSpectrum()
picker.pick(spec, picked)
assert len(picked) == 1
assert abs(picked[0].getMZ() - 500) < 0.001

GaussFilter.filter(spec) uses gaussian_width in m/z units for a spectrum; use_ppm_tolerance selects ppm_tolerance instead. Chromatogram widths use seconds. The 3.6 filterSpectrum compatibility alias is broken for these two smoothers; use their native filter(spec) method. SavitzkyGolayFilter uses a point-count frame_length and polynomial_order; validate sampling regularity and window size. PeakPickerIterative has signal_to_noise_, peak_width, and nr_iterations_ parameters; this alternative was signature/parameter checked, not benchmarked on low-resolution instrument data.

Normalization (Normalizer.filterSpectrum) supports to_one and to_TIC. Thresholds afterwards are on normalized intensities; a threshold of 10 after unit-TIC normalization removes all nonnegative peaks. Within-scan normalization usually destroys the abundance information needed for label-free quantification.

For filtering, use spec.select(indices) to keep float/integer/string data arrays synchronized. set_peaks() alone does not filter associated arrays. Spectra read with getSpectrum, indexing or iteration may be copies: explicitly replace the full spectrum list with exp.setSpectra(processed).

Raw lines 26 to 61
62## Other algorithms and alignment
64`ThresholdMower`, `WindowMower`, `NLargest`, and `MorphologicalFilter` expose
65parameters through `getParameters()`; inspect their installed `filterSpectrum`
66signatures before use. Their particular noise/baseline assumptions are not
67validated by a successful call. For `SpectraMerger`, block-wise merging uses
68`block_method:*` options; setting `average_gaussian:*` while calling
69`mergeSpectraBlockWise` does not configure Gaussian averaging.
71`Deisotoper.deisotopeAndSingleCharge` acts on a sorted centroided spectrum;
72record tolerance units, charge range, isotope count, and whether intensity is
73summed or masses are converted to singly charged ions. This is not calibration.
74For feature deconvolution use a `FeatureMap` and `FeatureDeconvolution.compute`
75with output map/groups/edges; there is no `potential_charge_states` parameter.
77For RT alignment, call `aligner.setReference(reference_map)`,
78`aligner.align(sample_map, trafo)`, then
79`MapAlignmentTransformer().transformRetentionTimes(sample_map, trafo, True)`.
80See the feature reference and bundled alignment script. Retention alignment
81does not correct mass errors.

Other algorithms and alignment

ThresholdMower, WindowMower, NLargest, and MorphologicalFilter expose parameters through getParameters(); inspect their installed filterSpectrum signatures before use. Their particular noise/baseline assumptions are not validated by a successful call. For SpectraMerger, block-wise merging uses block_method:* options; setting average_gaussian:* while calling mergeSpectraBlockWise does not configure Gaussian averaging.

Deisotoper.deisotopeAndSingleCharge acts on a sorted centroided spectrum; record tolerance units, charge range, isotope count, and whether intensity is summed or masses are converted to singly charged ions. This is not calibration. For feature deconvolution use a FeatureMap and FeatureDeconvolution.compute with output map/groups/edges; there is no potential_charge_states parameter.

For RT alignment, call aligner.setReference(reference_map), aligner.align(sample_map, trafo), then MapAlignmentTransformer().transformRetentionTimes(sample_map, trafo, True). See the feature reference and bundled alignment script. Retention alignment does not correct mass errors.

Raw lines 62 to 82
83## Calibration: verified signature, illustrative workflow
85`InternalCalibration.calibrate(exp, reference_masses)` is not a valid API.
86First collect calibrants using `fillCalibrants`, then fit/apply a model. The
87following is a template requiring independently established ion m/z values and
88a representative run; it was not validated on instrument data:
90```python
91calibration = ms.InternalCalibration()
92locks = [ms.InternalCalibration_LockMass(500.0, 1, 1),
93 ms.InternalCalibration_LockMass(1000.0, 1, 1)]
94found, failed = calibration.fillCalibrants(exp, locks, 20.0, False, False, False)
95if found < 2:
96 raise ValueError("Insufficient calibrants; do not fit")
97ok = calibration.calibrate(exp, [1], ms.MZTrafoModel.MODELTYPE.LINEAR,
98 -1.0, False, 5.0, 5.0)
99if not ok:
100 raise ValueError("Calibration residual criteria failed")
101```
103Lock masses include adducts and require correct MS level and charge. Two points
104are merely a minimal algebraic requirement for a line; use adequate mass/RT
105coverage, independent validation ions, outlier diagnostics, isotope checks and
106before/after ppm residuals. Do not calibrate on unverified accurate-mass database
107candidates and then use the reduced error as independent identification evidence.
108Negative `rt_chunk` selects one global model; positive windows are seconds.
109R is needed only for requested calibration plot output, not this no-plot template.
111Sources: [InternalCalibration release source](https://github.com/OpenMS/OpenMS/blob/5d5cbff4053b281763a1a79bf69e81c27967cfdf/src/openms/include/OpenMS/PROCESSING/CALIBRATION/InternalCalibration.h),
112[OpenMS changes](https://openms.de/documentation/html/ChangeLog.html), and installed
1133.6.0 binding docstrings/defaults.

Calibration: verified signature, illustrative workflow

InternalCalibration.calibrate(exp, reference_masses) is not a valid API. First collect calibrants using fillCalibrants, then fit/apply a model. The following is a template requiring independently established ion m/z values and a representative run; it was not validated on instrument data:

calibration = ms.InternalCalibration()
locks = [ms.InternalCalibration_LockMass(500.0, 1, 1),
         ms.InternalCalibration_LockMass(1000.0, 1, 1)]
found, failed = calibration.fillCalibrants(exp, locks, 20.0, False, False, False)
if found < 2:
    raise ValueError("Insufficient calibrants; do not fit")
ok = calibration.calibrate(exp, [1], ms.MZTrafoModel.MODELTYPE.LINEAR,
                           -1.0, False, 5.0, 5.0)
if not ok:
    raise ValueError("Calibration residual criteria failed")

Lock masses include adducts and require correct MS level and charge. Two points are merely a minimal algebraic requirement for a line; use adequate mass/RT coverage, independent validation ions, outlier diagnostics, isotope checks and before/after ppm residuals. Do not calibrate on unverified accurate-mass database candidates and then use the reduced error as independent identification evidence. Negative rt_chunk selects one global model; positive windows are seconds. R is needed only for requested calibration plot output, not this no-plot template.

Sources: InternalCalibration release source, OpenMS changes, and installed 3.6.0 binding docstrings/defaults.

Raw lines 83 to 113