Skip to content

Commit 01b5a92

Browse files
committed
Add more pyopenms helper tests
1 parent 03ac596 commit 01b5a92

5 files changed

Lines changed: 614 additions & 82 deletions

File tree

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ pysmiles = "^2.0.0"
3939
numba = "^0.61.2"
4040
numba-stats = "^1.10.1"
4141
optuna = "^4.3.0"
42+
ms-deisotope = "^0.0.60"
43+
pyopenms = "^3.1.0"
4244

4345
[tool.poetry.group.dev.dependencies]
4446
jupyterlab = "^4.4.3"

tests/test_deisotoping.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# test deisotoping and isotope generation
2+
3+
import numpy as np
4+
import pytest
5+
6+
from vimms.Chemicals import Isotopes, Adducts
7+
from vimms.Common import Formula, ADDUCT_TERMS, POSITIVE, PROTON_MASS
8+
from vimms.Deisotoping import Deisotoper
9+
from vimms.MassSpecUtils import adduct_transformation
10+
11+
12+
def test_isotope_distribution_multi_element():
13+
formula = Formula("C10H16N2O2S")
14+
isotopes = Isotopes(formula)
15+
peaks = isotopes.get_isotopes(total_proportion=0.99)
16+
17+
proportions = [peak[1] for peak in peaks]
18+
mzs = [peak[0] for peak in peaks]
19+
20+
assert len(peaks) > 1
21+
assert np.isclose(sum(proportions), 1.0, atol=1e-6)
22+
assert all(mzs[i] < mzs[i + 1] for i in range(len(mzs) - 1))
23+
24+
25+
def test_isotope_distribution_chlorine_m2_peak():
26+
formula = Formula("C5H10Cl2")
27+
isotopes = Isotopes(formula)
28+
peaks = isotopes.get_isotopes(total_proportion=0.99)
29+
30+
mono_mz = peaks[0][0]
31+
deltas = [mz - mono_mz for mz, _, _ in peaks[1:]]
32+
33+
assert any(np.isclose(delta, 1.997, atol=0.01) for delta in deltas)
34+
35+
36+
def test_deisotoper_recovers_mono():
37+
formula = Formula("C10H16N2O2S")
38+
isotopes = Isotopes(formula)
39+
adducts = Adducts(formula, adduct_prior_dict={POSITIVE: {"M+H": 1.0}})
40+
adduct_name = adducts.get_adducts()[POSITIVE][0][0]
41+
mul, add = ADDUCT_TERMS[adduct_name]
42+
43+
peaks = []
44+
for mz, proportion, _ in isotopes.get_isotopes(total_proportion=0.99):
45+
adducted_mz = adduct_transformation(mz, mul, add)
46+
peaks.append((adducted_mz, proportion * 1e5))
47+
48+
deisotoper = Deisotoper(ppm_tolerance=10.0, max_charge=1, min_isotopes=2)
49+
clusters = deisotoper.deisotope(peaks)
50+
51+
assert len(clusters) == 1
52+
expected_mz = formula.mass + PROTON_MASS
53+
assert np.isclose(clusters[0].monoisotopic_mz, expected_mz, atol=1e-3)
54+
55+
56+
def test_deisotoper_handles_m_plus_2_only():
57+
peaks = [(100.0, 1e5), (101.997, 6e4)]
58+
deisotoper = Deisotoper(ppm_tolerance=10.0, max_charge=1, min_isotopes=2)
59+
clusters = deisotoper.deisotope(peaks)
60+
61+
assert len(clusters) == 1
62+
assert np.isclose(clusters[0].monoisotopic_mz, 100.0, atol=1e-6)
63+
64+
65+
def test_pyopenms_deisotope_helper():
66+
pytest.importorskip("pyopenms")
67+
from vimms.Deisotoping import deisotope_with_pyopenms
68+
69+
peaks = [(100.0, 1e5), (101.003355, 5e4), (102.00671, 2e4)]
70+
_, mzs, intensities = deisotope_with_pyopenms(peaks, min_isopeaks=2, max_isopeaks=3)
71+
72+
assert len(mzs) <= len(peaks)
73+
assert len(mzs) == len(intensities)
74+
75+
76+
def test_pyopenms_deadduct_helper():
77+
pytest.importorskip("pyopenms")
78+
import pyopenms as oms
79+
80+
from vimms.Deisotoping import deadduct_with_pyopenms
81+
82+
fmap = oms.FeatureMap()
83+
feature = oms.Feature()
84+
feature.setMZ(100.0)
85+
feature.setIntensity(1e5)
86+
fmap.push_back(feature)
87+
88+
output = deadduct_with_pyopenms(fmap, adducts=["[M+H]+"])
89+
assert output.size() >= 1
90+
91+
92+
def test_pyopenms_deadduct_multiple_features():
93+
pytest.importorskip("pyopenms")
94+
import pyopenms as oms
95+
96+
from vimms.Deisotoping import deadduct_with_pyopenms
97+
98+
fmap = oms.FeatureMap()
99+
for mz, intensity in [(100.0, 1e5), (122.989218, 4e4)]:
100+
feature = oms.Feature()
101+
feature.setMZ(mz)
102+
feature.setIntensity(intensity)
103+
fmap.push_back(feature)
104+
105+
output = deadduct_with_pyopenms(fmap, adducts=["[M+H]+", "[M+Na]+"])
106+
assert output.size() >= 1
107+
108+
109+
def test_pyopenms_deisotope_empty_peaks():
110+
pytest.importorskip("pyopenms")
111+
from vimms.Deisotoping import deisotope_with_pyopenms
112+
113+
spectrum, mzs, intensities = deisotope_with_pyopenms([])
114+
assert spectrum is None
115+
assert mzs.size == 0
116+
assert intensities.size == 0

0 commit comments

Comments
 (0)