|
| 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) |
0 commit comments