Advanced Plotting
This contains an overview of how to generate plots directly from the plotting library.
Warning
Plotting library interface is currently unstable. Please be aware that future changes may break the current interface!
MassDash is also capable of generating many different other plot types. Since these plots are more complex, plotting is more involved.
In general to create a plot we have to fetch the data
Fetch the data in either a
TransitionGrouporFeatureMapobject, depending on our plottype.Initiate a
PlotConfigInitiate a Plotter object (e.g.
InterativePlotterInteractiveTwoDimensionalPlotter,InteractiveThreeDimensionalPlotter) with thePlotConfigCall the
plotmethod with theTransitionGrouporFeatureMapobject.Show the plot (bokeh or plotly backend depending on the plot)
Here is an example of how we can plot a chromatogram directly using the plotting library.
1. Fetch the Target Data
First, lets load our a sample TransitionGroup for plotting.
[3]:
from massdash.loaders import MzMLDataLoader
from massdash.structs import TargetedDIAConfig
# Initate TargetedDIAConfig and set parameters
extraction_config = TargetedDIAConfig()
extraction_config.im_window = 0.2
extraction_config.rt_window = 50
extraction_config.mz_tol = 20
# Initiate loader object
loader = MzMLDataLoader(dataFiles="mzml/ionMobilityTest.mzML",
rsltsFile="osw/ionMobilityTest.osw")
# fetch transitionGroup for target peptide
transitionGroup = loader.loadTransitionGroups("AFVDFLSDEIK", 2, extraction_config)['ionMobilityTest']
Initializing valid scores for selection
[2024-10-10 08:03:40,127] MzMLDataAccess - INFO - Opening mzml/ionMobilityTest.mzML file...: Elapsed 0.1749250888824463 ms
[2024-10-10 08:03:40,129] MzMLDataAccess - INFO - There are 50 spectra and 0 chromatograms.
[2024-10-10 08:03:40,130] MzMLDataAccess - INFO - There are 25 MS1 spectra and 25 MS2 spectra.
If the above code block does not look familliar please visit the Loading Spectrum Data page.
2. Initiate the PlotConfig Object
A PlotConfig object is required to initiate any plotting object.
General Configurations
- class massdash.plotting.PlotConfig
A class representing the configuration for a plot.
- include_ms1
A flag indicating whether to include MS1 data.
- Type:
bool
- include_ms2
A flag indicating whether to include MS2 data.
- Type:
bool
- num_plot_columns
The number of columns to be displayed in the plot.
- Type:
int
- hide_legends
A flag indicating whether to hide the legends in the plot.
- Type:
bool
- title
The title of the plot.
- Type:
str
- subtitle
The subtitle of the plot.
- Type:
str
- x_axis_label
The label for the x-axis.
- Type:
str
- y_axis_label
The label for the y-axis.
- Type:
str
- smoothing_dict
A dictionary containing the parameters for smoothing the data. e.g. {‘type’: ‘sgolay’, ‘sgolay_polynomial_order’: 3, ‘sgolay_frame_length’: 11} e.g. {‘type’: ‘none’}
- Type:
dict
- x_range
The range of values to be displayed on the x-axis.
- Type:
tuple
- y_range
The range of values to be displayed on the y-axis.
- Type:
tuple
- scale_intensity
A flag indicating whether to scale the intensity of the data.
- Type:
bool
- aggregate_mslevels
A flag indicating whether to aggregate the data within an MS level.
- Type:
bool
- type_of_heatmap
The type of heatmap to be displayed. (Only used for InteractiveTwoDimensionalPlotter)
- Type:
str
- type_of_3d_plot
The type of 3D plot to be displayed. (Only used for InteractiveThreeDimensionalPlotter)
- Type:
str
- type_of_comparison
The type of comparison to be displayed. (Only used for InteractivePlotter)
- Type:
str
- context
The context in which the plot is being displayed. valid: “streamlit”, “jupyter”
- Type:
str
- normalization_dict
A dictionary containing the parameters for normalizing the data (2D heatmap only)
- Type:
dict
For this example chromatogram we will use the following configuration set below
[4]:
from massdash.plotting import PlotConfig
plotConfig = PlotConfig()
plotConfig.include_ms1 = True
plotConfig.include_ms2 = True
plotConfig.aggregate_mslevels = True
plotConfig.smoothing_dict = dict(type='sgolay',sgolay_polynomial_order=3, sgolay_frame_length=11)
plotConfig.title = "Test Chromatogram"
3. Initiate the Plotting Object
[5]:
from massdash.plotting import InteractivePlotter
plotter = InteractivePlotter(plotConfig)
4. Plot
[6]:
fig = plotter.plot(transitionGroup)
plotter.show()
[7]:
plotConfig.smoothing_dict
[7]:
{'type': 'sgolay', 'sgolay_polynomial_order': 3, 'sgolay_frame_length': 11}
Add Transition Group Features to the Plot
To add TransitionGroupFeatures to the plot we can use the plot_boundaries method
[8]:
# fetch transition group features
transitionGroupFeatures = loader.loadTransitionGroupFeatures("AFVDFLSDEIK", 2)['ionMobilityTest']
[9]:
fig = plotter.plot(transitionGroup, transitionGroupFeatures)
plotter.show()