bittremieux / spectrum_utils

Python package for efficient mass spectrometry data processing and visualization
https://spectrum-utils.readthedocs.io/
Apache License 2.0
130 stars 21 forks source link

how to import mass data pymzml package #36

Closed jianghudaoshi closed 1 year ago

jianghudaoshi commented 1 year ago

sorry to bother you, its an amazing tools to visualize mass data, but i don't know how to use spectrum_utils to process mzml data imported by pymzml package. i'm not good at computer science. desperately need your help, thanks!

bittremieux commented 1 year ago

Here's some example code to plot the first MS/MS spectrum encountered in an mzML file read with pymzml:

import matplotlib.pyplot as plt
import pymzml
import spectrum_utils.plot as sup
import spectrum_utils.spectrum as sus

filename = "my_filename.mzML"
for spec in pymzml.run.Reader(filename):
    if spec.ms_level == 2:
        spectrum = sus.MsmsSpectrum(
            f"scan_nr={spec.ID}",
            spec.selected_precursors[0]["mz"],
            spec.selected_precursors[0]["charge"],
            spec.mz,
            spec.i,
        )
        break

fig, ax = plt.subplots(figsize=(12, 6))
sup.spectrum(spectrum, grid=False, ax=ax)
ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)
plt.savefig("pymzml.png", bbox_inches="tight", dpi=300, transparent=True)
plt.close()