|
| 1 | +""" |
| 2 | +Tests for MSExperiment chromatogram integration. |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +import numpy as np |
| 7 | +import pandas as pd |
| 8 | +import pyopenms as oms |
| 9 | + |
| 10 | +from openms_python import Py_MSExperiment, Py_MSChromatogram |
| 11 | + |
| 12 | + |
| 13 | +def test_msexperiment_chromatogram_count(): |
| 14 | + """Test chromatogram count properties.""" |
| 15 | + exp = Py_MSExperiment() |
| 16 | + |
| 17 | + # Initially empty |
| 18 | + assert exp.nr_chromatograms == 0 |
| 19 | + assert exp.chromatogram_count == 0 |
| 20 | + |
| 21 | + # Add a chromatogram |
| 22 | + chrom = Py_MSChromatogram(oms.MSChromatogram()) |
| 23 | + exp.add_chromatogram(chrom) |
| 24 | + |
| 25 | + assert exp.nr_chromatograms == 1 |
| 26 | + assert exp.chromatogram_count == 1 |
| 27 | + |
| 28 | + |
| 29 | +def test_msexperiment_add_chromatogram(): |
| 30 | + """Test adding chromatograms to experiment.""" |
| 31 | + exp = Py_MSExperiment() |
| 32 | + |
| 33 | + # Create chromatogram from DataFrame |
| 34 | + df = pd.DataFrame({ |
| 35 | + 'rt': [10.0, 20.0, 30.0], |
| 36 | + 'intensity': [100.0, 200.0, 150.0] |
| 37 | + }) |
| 38 | + chrom = Py_MSChromatogram.from_dataframe(df, mz=445.12, name="XIC 445.12") |
| 39 | + |
| 40 | + # Add it |
| 41 | + exp.add_chromatogram(chrom) |
| 42 | + |
| 43 | + assert exp.nr_chromatograms == 1 |
| 44 | + |
| 45 | + # Add another |
| 46 | + chrom2 = Py_MSChromatogram.from_dataframe(df, mz=500.0, name="XIC 500.0") |
| 47 | + exp.add_chromatogram(chrom2) |
| 48 | + |
| 49 | + assert exp.nr_chromatograms == 2 |
| 50 | + |
| 51 | + |
| 52 | +def test_msexperiment_get_chromatogram(): |
| 53 | + """Test getting chromatograms by index.""" |
| 54 | + exp = Py_MSExperiment() |
| 55 | + |
| 56 | + # Add chromatograms |
| 57 | + df1 = pd.DataFrame({'rt': [10.0, 20.0], 'intensity': [100.0, 200.0]}) |
| 58 | + chrom1 = Py_MSChromatogram.from_dataframe(df1, mz=445.12, name="First") |
| 59 | + exp.add_chromatogram(chrom1) |
| 60 | + |
| 61 | + df2 = pd.DataFrame({'rt': [30.0, 40.0], 'intensity': [150.0, 250.0]}) |
| 62 | + chrom2 = Py_MSChromatogram.from_dataframe(df2, mz=500.0, name="Second") |
| 63 | + exp.add_chromatogram(chrom2) |
| 64 | + |
| 65 | + # Get them back |
| 66 | + retrieved1 = exp.get_chromatogram(0) |
| 67 | + assert retrieved1.mz == pytest.approx(445.12) |
| 68 | + assert retrieved1.name == "First" |
| 69 | + assert len(retrieved1) == 2 |
| 70 | + |
| 71 | + retrieved2 = exp.get_chromatogram(1) |
| 72 | + assert retrieved2.mz == pytest.approx(500.0) |
| 73 | + assert retrieved2.name == "Second" |
| 74 | + assert len(retrieved2) == 2 |
| 75 | + |
| 76 | + |
| 77 | +def test_msexperiment_chromatograms_iteration(): |
| 78 | + """Test iterating over chromatograms.""" |
| 79 | + exp = Py_MSExperiment() |
| 80 | + |
| 81 | + # Add multiple chromatograms |
| 82 | + mz_values = [445.12, 500.0, 550.25] |
| 83 | + for mz in mz_values: |
| 84 | + df = pd.DataFrame({'rt': [10.0, 20.0, 30.0], 'intensity': [100.0, 200.0, 150.0]}) |
| 85 | + chrom = Py_MSChromatogram.from_dataframe(df, mz=mz) |
| 86 | + exp.add_chromatogram(chrom) |
| 87 | + |
| 88 | + # Iterate and check |
| 89 | + retrieved_mzs = [] |
| 90 | + for chrom in exp.chromatograms(): |
| 91 | + assert isinstance(chrom, Py_MSChromatogram) |
| 92 | + assert len(chrom) == 3 |
| 93 | + retrieved_mzs.append(chrom.mz) |
| 94 | + |
| 95 | + assert len(retrieved_mzs) == 3 |
| 96 | + np.testing.assert_array_almost_equal(retrieved_mzs, mz_values) |
| 97 | + |
| 98 | + |
| 99 | +def test_msexperiment_repr_with_chromatograms(): |
| 100 | + """Test string representation includes chromatogram count.""" |
| 101 | + exp = Py_MSExperiment() |
| 102 | + |
| 103 | + # Add a spectrum |
| 104 | + spec = oms.MSSpectrum() |
| 105 | + spec.setRT(10.0) |
| 106 | + spec.setMSLevel(1) |
| 107 | + spec.set_peaks(([100.0, 200.0], [50.0, 100.0])) |
| 108 | + exp._experiment.addSpectrum(spec) |
| 109 | + |
| 110 | + # Initially no chromatograms |
| 111 | + repr_str = repr(exp) |
| 112 | + assert "chromatograms" not in repr_str |
| 113 | + |
| 114 | + # Add a chromatogram |
| 115 | + df = pd.DataFrame({'rt': [10.0, 20.0], 'intensity': [100.0, 200.0]}) |
| 116 | + chrom = Py_MSChromatogram.from_dataframe(df, mz=445.12) |
| 117 | + exp.add_chromatogram(chrom) |
| 118 | + |
| 119 | + repr_str = repr(exp) |
| 120 | + assert "chromatograms=1" in repr_str |
| 121 | + |
| 122 | + |
| 123 | +def test_msexperiment_add_native_chromatogram(): |
| 124 | + """Test adding native pyOpenMS chromatogram.""" |
| 125 | + exp = Py_MSExperiment() |
| 126 | + |
| 127 | + # Create native chromatogram |
| 128 | + native_chrom = oms.MSChromatogram() |
| 129 | + native_chrom.set_peaks(([10.0, 20.0, 30.0], [100.0, 200.0, 150.0])) |
| 130 | + product = native_chrom.getProduct() |
| 131 | + product.setMZ(445.12) |
| 132 | + native_chrom.setProduct(product) |
| 133 | + |
| 134 | + # Add it directly |
| 135 | + exp.add_chromatogram(native_chrom) |
| 136 | + |
| 137 | + assert exp.nr_chromatograms == 1 |
| 138 | + |
| 139 | + # Retrieve and verify |
| 140 | + retrieved = exp.get_chromatogram(0) |
| 141 | + assert retrieved.mz == pytest.approx(445.12) |
| 142 | + assert len(retrieved) == 3 |
| 143 | + |
| 144 | + |
| 145 | +def test_msexperiment_empty_chromatograms(): |
| 146 | + """Test experiment with no chromatograms.""" |
| 147 | + exp = Py_MSExperiment() |
| 148 | + |
| 149 | + # Should not raise error |
| 150 | + assert exp.nr_chromatograms == 0 |
| 151 | + |
| 152 | + # Should return empty iterator |
| 153 | + chroms = list(exp.chromatograms()) |
| 154 | + assert len(chroms) == 0 |
0 commit comments