Skip to content

Commit b0c9cf1

Browse files
authored
Merge pull request #379 from pymzml/feature/chromatogram_access
Addition of features to handle chromatograms
2 parents 40cbdd6 + a66a9c9 commit b0c9cf1

18 files changed

+1313
-26
lines changed

docs/source/pymzml_spec.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ Chromatogram
3030
:exclude-members: __repr__, __str__
3131

3232

33-
MS_Spectrum
33+
MsData
3434
-----------
3535

36-
.. autoclass:: pymzml.spec.MS_Spectrum
36+
.. autoclass:: pymzml.msdata.MsData
3737
:members:
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import os
5+
import sys
6+
import pymzml
7+
8+
9+
def main(mzml_file):
10+
"""
11+
Example script demonstrating how to access both spectra and chromatograms
12+
using pymzML.
13+
14+
Usage:
15+
./access_spectra_and_chromatograms.py <path_to_mzml_file>
16+
"""
17+
print("Initializing Reader...")
18+
# Initialize with skip_chromatogram=False to include chromatograms during iteration
19+
run = pymzml.run.Reader(mzml_file, skip_chromatogram=False)
20+
21+
# Access the first spectrum using indexing (traditional way)
22+
print("\nAccessing first spectrum using indexing (run[0]):")
23+
try:
24+
spectrum = run[0]
25+
print(f"Spectrum ID: {spectrum.ID}")
26+
print(f"MS Level: {spectrum.ms_level}")
27+
print(f"Retention Time: {spectrum.scan_time_in_minutes()} minutes")
28+
print(f"Number of peaks: {len(spectrum.peaks('raw'))}")
29+
except Exception as e:
30+
print(f"Error accessing spectrum: {e}")
31+
32+
# Access the first spectrum using the new get_spectrum method
33+
print("\nAccessing first spectrum using get_spectrum(0):")
34+
try:
35+
spectrum = run.get_spectrum(0)
36+
print(f"Spectrum ID: {spectrum.ID}")
37+
print(f"MS Level: {spectrum.ms_level}")
38+
print(f"Retention Time: {spectrum.scan_time_in_minutes()} minutes")
39+
print(f"Number of peaks: {len(spectrum.peaks('raw'))}")
40+
except Exception as e:
41+
print(f"Error accessing spectrum: {e}")
42+
43+
# Access the TIC chromatogram using string identifier
44+
print("\nAccessing TIC chromatogram using run['TIC']:")
45+
try:
46+
chromatogram = run["TIC"]
47+
print(f"Chromatogram ID: {chromatogram.ID}")
48+
print(f"Number of data points: {len(chromatogram.peaks())}")
49+
50+
# Print the first few data points
51+
print("\nFirst 5 data points (time, intensity):")
52+
for i, (time, intensity) in enumerate(chromatogram.peaks()):
53+
if i >= 5:
54+
break
55+
print(f" {time:.4f}, {intensity:.2f}")
56+
except Exception as e:
57+
print(f"Error accessing chromatogram: {e}")
58+
59+
# Access the first chromatogram using the new get_chromatogram method
60+
print("\nAccessing first chromatogram using get_chromatogram(0):")
61+
try:
62+
chromatogram = run.get_chromatogram(0)
63+
print(f"Chromatogram ID: {chromatogram.ID}")
64+
print(f"Number of data points: {len(chromatogram.peaks())}")
65+
66+
# Print the first few data points
67+
print("\nFirst 5 data points (time, intensity):")
68+
for i, (time, intensity) in enumerate(chromatogram.peaks()):
69+
if i >= 5:
70+
break
71+
print(f" {time:.4f}, {intensity:.2f}")
72+
except Exception as e:
73+
print(f"Error accessing chromatogram: {e}")
74+
75+
# Demonstrate iterating through all items (spectra and chromatograms)
76+
print("\nIterating through first few items (spectra and chromatograms):")
77+
count = 0
78+
for item in run:
79+
if count >= 5:
80+
break
81+
if isinstance(item, pymzml.spec.Spectrum):
82+
print(f" Spectrum {item.ID}, MS level {item.ms_level}, RT {item.scan_time_in_minutes():.2f} min")
83+
elif hasattr(item, 'time') and hasattr(item, 'i'):
84+
print(f" Chromatogram {item.ID}, {len(item.peaks())} data points")
85+
count += 1
86+
87+
print("\nDone!")
88+
89+
90+
if __name__ == "__main__":
91+
if len(sys.argv) < 2:
92+
print(main.__doc__)
93+
print("Please provide a path to an mzML file.")
94+
sys.exit(1)
95+
96+
mzml_file = sys.argv[1]
97+
if not os.path.exists(mzml_file):
98+
print(f"Error: File '{mzml_file}' not found.")
99+
sys.exit(1)
100+
101+
main(mzml_file)

pymzml/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2525
SOFTWARE.
2626
"""
27-
__all__ = ["run", "spec", "obo", "minimum", "plot", "file_classes"]
27+
__all__ = ["run", "spec", "chromatogram", "obo", "minimum", "plot", "file_classes"]
2828

2929
import sys
3030

@@ -39,7 +39,9 @@
3939
# Imports of individual modules
4040
import pymzml.run
4141
import pymzml.spec
42+
import pymzml.chromatogram
4243
from pymzml.spec import MSDecoder
44+
from pymzml.chromatogram import Chromatogram
4345
import pymzml.obo
4446
import pymzml.plot
4547
import pymzml.utils

0 commit comments

Comments
 (0)