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

Add functionality for plotting mass errors #46

Closed RalfG closed 1 year ago

RalfG commented 1 year ago

This PR adds a function to spectrum_utils.plot to plot MS2 mass errors as a bubble plot:

import spectrum_utils.plot as sup
sup.mass_errors(observed_spec)

dcf7a1be-c974-4e26-9159-3c44fbcc4840

Ultimately, my goal was to be able to generate a plot similar to the ones produced by IPSA / USE, which is now possible with some additional wrapper code:

import matplotlib.pyplot as plt
import spectrum_utils.plot as sup

fig, (top_ax, middle_ax, bottom_ax) = plt.subplots(
    *(3,1),
    figsize=(12, 8),
    sharex=True,
    gridspec_kw={'height_ratios': [1, 0.5,1 ]}
)

sup.spectrum(observed_spec, ax=top_ax)
sup.mass_errors(observed_spec, ax=middle_ax)
sup.spectrum(predicted_spec, mirror_intensity=True, ax=bottom_ax)

top_ax.set_xlabel('')
middle_ax.set_xlabel('')

c9ac17a6-e334-49cf-85c8-a568ed885258

However, since all spectrum_utils.plot functions operate at the ax-level (not figure-level), I'm not sure if you would prefer to implement such a function or not.

Of note: I also changed the sup.spectrum ylim to 1.1 to avoid overflow of the peak annotation labels, which I very often had. This commit can be easily reverted if you do not like it.

Let me know what you think!

RalfG commented 1 year ago

I added the function for a figure-level mirror spectrum with bubble plot as well. However, I'm not sure what to call the function. Currently, I named it full_mirror, but I'm open to suggestions for something more descriptive. Both bubble plot and bottom spectrum are optional, and I tried to define some sensible default dimensions for the figure depending on whether there is a bottom spectrum or not.

codecov[bot] commented 1 year ago

Codecov Report

Merging #46 (3d7a96f) into main (86c623b) will decrease coverage by 4.37%. The diff coverage is 4.49%.

:exclamation: Current head 3d7a96f differs from pull request most recent head f86e36e. Consider uploading reports for the commit f86e36e to get more accurate results

@@            Coverage Diff             @@
##             main      #46      +/-   ##
==========================================
- Coverage   70.58%   66.21%   -4.37%     
==========================================
  Files           7        7              
  Lines         962     1036      +74     
==========================================
+ Hits          679      686       +7     
- Misses        283      350      +67     
Impacted Files Coverage Δ
spectrum_utils/plot.py 0.00% <0.00%> (ø)
spectrum_utils/utils.py 70.00% <66.66%> (-5.00%) :arrow_down:

... and 2 files with indirect coverage changes