Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update plotting tutorials #427

Merged
merged 5 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions docs/source/user_guide/fragment_spectrum_generation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ which you could plot with :py:meth:`pyopenms.plotting.plot_spectrum`, automatica

import matplotlib.pyplot as plt
from pyopenms.plotting import plot_spectrum
import matplotlib.pyplot as plt

plot_spectrum(spec1)
plt.show()
Expand Down Expand Up @@ -122,10 +121,6 @@ which you can again visualize with:
.. code-block:: python
:linenos:

import matplotlib.pyplot as plt
from pyopenms.plotting import plot_spectrum
import matplotlib.pyplot as plt

plot_spectrum(spec2, annotate_ions=False)
plt.show()

Expand Down
Binary file modified docs/source/user_guide/img/DFPIANGER_theo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/source/user_guide/img/DFPIANGER_theo_full.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file added docs/source/user_guide/img/nlargest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/source/user_guide/img/spec_alignment_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/source/user_guide/img/spec_alignment_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/user_guide/img/threshold_mower.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/user_guide/img/window_mower.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
114 changes: 110 additions & 4 deletions docs/source/user_guide/ms_data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ We can also visualize our mass spectrum from before using the :py:func:`~.plot_s

import matplotlib.pyplot as plt
from pyopenms.plotting import plot_spectrum
import matplotlib.pyplot as plt

plot_spectrum(spectrum)
plt.show()
Expand Down Expand Up @@ -639,8 +638,8 @@ But first, we will load some test data:
oms.MzMLFile().load("test.mzML", inp)


Filtering Mass Spectra by :term`MS` Level
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Filtering Mass Spectra by MS Level
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We will filter the data from ``test.mzML`` file by only retaining
mass spectra that are not :term:`MS1` spectra
Expand Down Expand Up @@ -707,4 +706,111 @@ Similarly we could only retain peaks above a certain
intensity or keep only the top N peaks in each mass spectrum.

For more advanced filtering tasks pyOpenMS provides special algorithm classes.
We will take a closer look at some of them in the algorithm section.
We will take a closer look at some of them in the next section.


Filtering Mass Spectra with TOPP Tools
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We can also use predefined TOPP tools to filter our data. First we need to load in the data:

.. code-block:: python
:linenos:

import matplotlib.pyplot as plt
from pyopenms.plotting import plot_spectrum, mirror_plot_spectrum

gh = "https://raw.githubusercontent.com/OpenMS/pyopenms-docs/master"
urlretrieve(
gh + "/src/data/YIC(Carbamidomethyl)DNQDTISSK.mzML", "observed.mzML"
)

exp = oms.MSExperiment()
# Load mzML file and obtain spectrum for peptide YIC(Carbamidomethyl)DNQDTISSK
oms.MzMLFile().load("observed.mzML", exp)

# Get first spectrum
spectra = exp.getSpectra()
observed_spectrum = spectra[0]

The :py:class:`~.WindowMower` tool can be used to remove peaks in a sliding or jumping window. The window size,
number of highest peaks to keep and move type can be set with a :py:class:`~.Param` object

.. code-block:: python
:linenos:

from copy import deepcopy

window_mower_filter = oms.WindowMower()

# Copy the original spectrum
mowed_spectrum = deepcopy(observed_spectrum)

# Set parameters
params = oms.Param()
# Defines the m/z range of the sliding window
params.setValue("windowsize", 100.0, "")
# Defines the number of highest peaks to keep in the sliding window
params.setValue("peakcount", 1, "")
# Defines the type of window movement: jump (window size steps) or slide (one peak steps)
params.setValue("movetype", "jump", "")

# Apply window mowing
window_mower_filter.setParameters(params)
window_mower_filter.filterPeakSpectrum(mowed_spectrum)

# Visualize the resulting data together with the original spectrum
mirror_plot_spectrum(observed_spectrum, mowed_spectrum)
plt.show()

.. image:: img/window_mower.png


Noise can be easily removed with :py:class:`~.ThresholdMower` by setting a threshold value for the intensity of peaks
and cutting off everything below.

.. code-block:: python
:linenos:

# Copy spectrum
threshold_mower_spectrum = deepcopy(observed_spectrum)

threshold_mower_filter = oms.ThresholdMower()

# Set parameters
params = oms.Param()
params.setValue("threshold", 20.0, "")

# Apply threshold mowing
threshold_mower_filter.setParameters(params)
threshold_mower_filter.filterPeakSpectrum(threshold_mower_spectrum)

mirror_plot_spectrum(observed_spectrum, threshold_mower_spectrum)
plt.show()

.. image:: img/threshold_mower.png


We can also use e.g. :py:class:`~.NLargest` to keep only the N highest peaks in a spectrum.

.. code-block:: python
:linenos:

# Copy spectrum
nlargest_spectrum = deepcopy(observed_spectrum)

nlargest_filter = oms.NLargest()

# Set parameters
params = oms.Param()
params.setValue("n", 4, "")

# Apply N-Largest filter
nlargest_filter.setParameters(params)
nlargest_filter.filterPeakSpectrum(nlargest_spectrum)

mirror_plot_spectrum(observed_spectrum, nlargest_spectrum)
plt.show()
# Two peaks are overlapping, so only three peaks are really visible in the plot

.. image:: img/nlargest.png
4 changes: 2 additions & 2 deletions docs/source/user_guide/query_msexperiment_massql.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Query :py:class:`~.MSExperiment` with MassQL
============================================
Query MSExperiment with MassQL
==============================

MassQL is a powerful, SQL-like query language for mass spectrometry data.
For further information visit the `MassQL documentation
Expand Down
10 changes: 3 additions & 7 deletions docs/source/user_guide/spectrum_alignment.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@

import matplotlib.pyplot as plt
from pyopenms.plotting import mirror_plot_spectrum
import matplotlib.pyplot as plt

mirror_plot_spectrum(
observed_spectrum,
Expand Down Expand Up @@ -123,15 +122,12 @@
.. code-block:: python
:linenos:

import matplotlib.pyplot as plt
from pyopenms.plotting import mirror_plot_spectrum
import matplotlib.pyplot as plt

match_peaks_observed, match_peaks_theoretical = list(zip(*alignment))
mirror_plot_spectrum(
observed_spectrum,
theo_spectrum,
alignment=alignment,
spectrum_bottom_kws={"annotate_ions": False},
spectrum_top_kws={"matched_peaks": match_peaks_theoretical},
spectrum_bottom_kws={"annotate_ions": False, "matched_peaks": match_peaks_observed}

Check warning on line 130 in docs/source/user_guide/spectrum_alignment.rst

View workflow job for this annotation

GitHub Actions / linting

[blacken-docs] reported by reviewdog 🐶 Raw Output: docs/source/user_guide/spectrum_alignment.rst:130:- spectrum_bottom_kws={"annotate_ions": False, "matched_peaks": match_peaks_observed} docs/source/user_guide/spectrum_alignment.rst:130:+ spectrum_bottom_kws={ docs/source/user_guide/spectrum_alignment.rst:131:+ "annotate_ions": False, docs/source/user_guide/spectrum_alignment.rst:132:+ "matched_peaks": match_peaks_observed, docs/source/user_guide/spectrum_alignment.rst:133:+ },
)
plt.show()

Expand Down
Loading