Spectrum alignment#
OpenMS provides several ways to find matching peaks between two spectra. The most basic one SpectrumAlignment returns a list of matching peak indices between a query and target spectrum. In this example, we take an observed (measured) spectrum and align a theoretical spectrum to it.
First we load a (chemically modified) peptide:
1from urllib.request import urlretrieve
2from pyopenms import *
3
4gh = "https://raw.githubusercontent.com/OpenMS/pyopenms-docs/master"
5urlretrieve(
6 gh + "/src/data/YIC(Carbamidomethyl)DNQDTISSK.mzML", "observed.mzML"
7)
8
9exp = MSExperiment()
10# Load mzML file and obtain spectrum for peptide YIC(Carbamidomethyl)DNQDTISSK
11MzMLFile().load("observed.mzML", exp)
12
13# Get first spectrum
14spectra = exp.getSpectra()
15observed_spectrum = spectra[0]
Now we generate the theoretical spectrum of that peptide:
1tsg = TheoreticalSpectrumGenerator()
2theo_spectrum = MSSpectrum()
3p = tsg.getParameters()
4p.setValue("add_y_ions", "true")
5p.setValue("add_b_ions", "true")
6p.setValue("add_metainfo", "true")
7tsg.setParameters(p)
8peptide = AASequence.fromString("YIC(Carbamidomethyl)DNQDTISSK")
9tsg.getSpectrum(theo_spectrum, peptide, 1, 2)
Now we can plot the observed and theoretical spectrum as a mirror plot:
1from pyopenms.plotting import mirror_plot_spectrum
2
3mirror_plot_spectrum(
4 observed_spectrum,
5 theo_spectrum,
6 spectrum_bottom_kws={"annotate_ions": False},
7)
8plt.show()
which produces
Now we want to find matching peaks between observed and theoretical spectrum.
1alignment = []
2spa = SpectrumAlignment()
3p = spa.getParameters()
4# use 0.5 Da tolerance (Note: for high-resolution data we could also use ppm by setting the is_relative_tolerance value to true)
5p.setValue("tolerance", 0.5)
6p.setValue("is_relative_tolerance", "false")
7spa.setParameters(p)
8# align both spectra
9spa.getSpectrumAlignment(alignment, theo_spectrum, observed_spectrum)
The alignment contains a list of matched peak indices. We can simply inspect matching peaks with:
1# Print matching ions and mz from theoretical spectrum
2print("Number of matched peaks: " + str(len(alignment)))
3t = []
4for theo_idx, obs_idx in alignment:
5 ion_name = theo_spectrum.getStringDataArrays()[0][theo_idx].decode()
6 ion_charge = theo_spectrum.getIntegerDataArrays()[0][theo_idx]
7 t.append(
8 [
9 ion_name,
10 str(ion_charge),
11 str(theo_spectrum[theo_idx].getMZ()),
12 str(observed_spectrum[obs_idx].getMZ()),
13 ]
14 )
15print(tabulate(t, headers=["ion", "charge", "theo. m/z", "observed m/z"]))
Number of matched peaks: 16
ion charge theo. m/z observed m/z
----- -------- ----------- --------------
y2+ 1 234.145 234.123
y5++ 2 268.158 268.105
b2+ 1 277.155 277.246
y3+ 1 321.177 321.297
y4+ 1 434.261 434.288
b3+ 1 437.185 437.291
y5+ 1 535.309 535.189
b4+ 1 552.212 552.338
b9++ 2 562.24 562.421
y10++ 2 584.251 584.412
y11++ 2 640.793 640.954
The mirror plot can also be used to visualize the aligned spectrum:
1from pyopenms.plotting import mirror_plot_spectrum
2
3mirror_plot_spectrum(
4 observed_spectrum,
5 theo_spectrum,
6 alignment=alignment,
7 spectrum_bottom_kws={"annotate_ions": False},
8)
9plt.show()
which produces