Skip to content

Commit

Permalink
Merge pull request #100 from Roestlab/patch/peak_boundary_int
Browse files Browse the repository at this point in the history
Patch/peak boundary int
  • Loading branch information
jcharkow authored Jan 25, 2024
2 parents aa27f47 + f27fad7 commit 7560d19
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 1,643 deletions.
54 changes: 27 additions & 27 deletions docs/python_docs/Advanced Plotting.ipynb

Large diffs are not rendered by default.

42 changes: 21 additions & 21 deletions docs/python_docs/Plotting1D.ipynb

Large diffs are not rendered by default.

319 changes: 6 additions & 313 deletions manuscript/main_figures/Figure-3-Features-Comparison.ipynb

Large diffs are not rendered by default.

990 changes: 13 additions & 977 deletions manuscript/main_figures/Figure-4-Extraction-Optimization.ipynb

Large diffs are not rendered by default.

288 changes: 3 additions & 285 deletions manuscript/main_figures/Figure-5-Peak-Picking-Demonstration.ipynb

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions massdash/loaders/GenericLoader.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,7 @@ def plotChromatogram(self,
plotter = InteractivePlotter(pc)

# Plot the chromatogram data
print(transitionGroup)
fig = plotter.plot(transitionGroup)

# Add boundaries to the plot
fig = plotter.add_peak_boundaries(fig, transitionGroupFeatures)
fig = plotter.plot(transitionGroup, transitionGroupFeatures)

show(fig)

Expand Down
11 changes: 10 additions & 1 deletion massdash/plotting/GenericPlotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def __init__(self):
self.type_of_comparison = "retention time vs ion mobility"
self.context = "streamlit" # or "jupyter"
self.normalization_dict = {'type':'none'} # or {'type':'equalization', 'bins': (int)}
self.ms_level_str = 'ms1ms2'

def __str__(self):
return f"{'-'*8} PlotConfig {'-'*8}\ninclude_ms1: {self.include_ms1}\ninclude_ms2: {self.include_ms2}\nnum_plot_columns: {self.num_plot_columns}\ntitle: {self.title}\nsubtitle: {self.subtitle}\nx_axis_label: {self.x_axis_label}\ny_axis_label: {self.y_axis_label}\nsmoothing_dict: {self.smoothing_dict}\nx_range: {self.x_range}\ny_range: {self.y_range}\nscale_intensity: {self.scale_intensity}\naggregate_mslevels: {self.aggregate_mslevels}\ntype_of_heatmap: {self.type_of_heatmap}\ntype_of_3d_plot: {self.type_of_3d_plot}\ntype_of_comparison: {self.type_of_comparison}\n{'-'*30}"
Expand All @@ -71,6 +72,13 @@ def update(self, config_dict):
for key, value in config_dict.items():
if hasattr(self, key):
setattr(self, key, value)

if self.include_ms1 and self.include_ms2:
self.ms_level_str = 'ms1ms2'
elif self.include_ms1 and not self.include_ms2:
self.ms_level_str = 'ms1'
elif not self.include_ms1 and self.include_ms2:
self.ms_level_str = 'ms2'

class GenericPlotter(ABC):
"""
Expand All @@ -89,7 +97,8 @@ def __init__(self, config: PlotConfig):
self.y_range = config.y_range
self.scale_intensity = config.scale_intensity
self.normalization_dict = config.normalization_dict

self.ms_level_str = config.ms_level_str

@abstractmethod
def plot(self, transitionGroup: TransitionGroup, features: Optional[List[TransitionGroupFeature]] = None, plot_type: Literal['chromatogram', 'mobilogram', 'spectrum'] = 'chromatogram'):
pass
Expand Down
19 changes: 12 additions & 7 deletions massdash/plotting/InteractivePlotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self, config: PlotConfig, verbose: bool=False):
else:
LOGGER.setLevel("INFO")

def plot(self, transitionGroup: TransitionGroup, features: Optional[List[TransitionGroupFeature]] = None, plot_type: Literal['chromatogram', 'mobilogram', 'spectra'] = 'chromatogram') -> figure:
def plot(self, transitionGroup: TransitionGroup, features: Optional[List[TransitionGroupFeature]] = None, plot_type: Literal['chromatogram', 'mobilogram', 'spectrum'] = 'chromatogram', feature_legend_labels:Optional[List[str]] = []) -> figure:
"""
Plots the given transitionGroup using the specified plot type.
Expand All @@ -60,10 +60,10 @@ def plot(self, transitionGroup: TransitionGroup, features: Optional[List[Transit
figure: The generated plot as a Bokeh figure object.
"""
if plot_type == 'chromatogram':
plot = self.plot_chromatogram(transitionGroup, features)
plot = self.plot_chromatogram(transitionGroup, features, feature_legend_labels)
elif plot_type == 'mobilogram':
plot = self.plot_mobilogram(transitionGroup)
elif plot_type == 'spectra':
elif plot_type == 'spectrum':
plot = self.plot_spectra(transitionGroup)
else:
raise ValueError("Unsupported plot plot_type")
Expand Down Expand Up @@ -148,13 +148,18 @@ def process_chrom(self, p: figure, chrom: Chromatogram, label: str, color: str='

return line

def add_peak_boundaries(self, p: figure, features: List[TransitionGroupFeature], legend_labels:Optional[List[str]] = []) -> None:
def __add_peak_boundaries(self,
p: figure,
features: List[TransitionGroupFeature],
transitionGroup: TransitionGroup,
legend_labels:Optional[List[str]] = []) -> None:
"""
Adds peak boundaries to a Bokeh figure.
Args:
p (figure): The Bokeh figure to add the peak boundaries to.
features (List[TransitionGroupFeature]): A list of peak features to highlight on the plot.
transitionGroup (TransitionGroup): The TransitionGroup object containing precursor and transition data.
legend_labels (List[str], optional): A list of labels for the peak features. Defaults to [].
"""
if len(features) <= 8:
Expand Down Expand Up @@ -182,7 +187,7 @@ def add_peak_boundaries(self, p: figure, features: List[TransitionGroupFeature],
'bottom_int' : [0]})
else:
source = ColumnDataSource(data = {
'Intensity' : [feature.areaIntensity],
'Intensity' : [transitionGroup.max((feature.leftBoundary, feature.rightBoundary), level=self.ms_level_str)],
'leftWidth' : [feature.leftBoundary],
'rightWidth' : [feature.rightBoundary],
'ms2_mscore' : [feature.qvalue],
Expand Down Expand Up @@ -221,7 +226,7 @@ def add_peak_boundaries(self, p: figure, features: List[TransitionGroupFeature],

return p

def plot_chromatogram(self, transitionGroup: TransitionGroup, features: Optional[List[TransitionGroupFeature]]) -> figure:
def plot_chromatogram(self, transitionGroup: TransitionGroup, features: Optional[List[TransitionGroupFeature]], feature_legend_labels:Optional[List[str]] = []) -> figure:
"""
Plots a chromatogram for a given TransitionGroup.
Expand Down Expand Up @@ -323,7 +328,7 @@ def plot_chromatogram(self, transitionGroup: TransitionGroup, features: Optional

# Add peak boundaries if available
if features is not None:
p = self.add_peak_boundaries(p, features)
p = self.__add_peak_boundaries(p, features, transitionGroup, feature_legend_labels)

return p

Expand Down
8 changes: 1 addition & 7 deletions massdash/structs/TransitionGroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,6 @@ def plot(self,

plotter = InteractivePlotter(config)

plotter.plot(self)

if transitionGroupFeatures is not None:
if self.dataType == Chromatogram:
plotter.add_peak_boundaries(plotter.fig, transitionGroupFeatures)
else:
raise NotImplementedError("Peak boundaries are only implemented for chromatograms")
plotter.plot(self, transitionGroupFeatures, config.plot_type)

plotter.show()

0 comments on commit 7560d19

Please sign in to comment.