mioXpektron.plotting.plot_peaks
Functions
|
Overlay spectra with two colors (Cancer vs Control) inferred from file names. |
Classes
|
Class for plotting overlapping peaks from multiple spectra files. |
|
Configuration for PlotPeaks class. |
- class mioXpektron.plotting.plot_peaks.PlotPeaksConfig(data_dir, file_pattern='*.txt', mz_min=0.0, mz_max=1000.0, norm_tic=False, bin_width=0.001, alpha=0.18, show_median=True, show_group_cumulative=True, figsize=(10, 6), cumulative_figsize=(10, 4), color_map=None, save_fig=False, save_path='output_files/plots')[source]
Bases:
objectConfiguration for PlotPeaks class.
- Parameters:
data_dir (str) – Directory containing spectra files.
file_pattern (str, default “*.txt”) – Glob pattern for matching spectrum files.
mz_min (float, default 0.0) – Minimum m/z value for the plotting window.
mz_max (float, default 1000.0) – Maximum m/z value for the plotting window.
norm_tic (bool, default False) – If True, normalize intensities by total ion count.
bin_width (float, default 0.001) – Bin width for interpolation grid.
alpha (float, default 0.18) – Transparency for individual spectra lines.
show_median (bool, default True) – If True, overlay median curves on the plot.
show_group_cumulative (bool, default True) – If True, create cumulative intensity plot.
figsize (tuple, default (10, 6)) – Figure size for overlay plot.
cumulative_figsize (tuple, default (10, 4)) – Figure size for cumulative plot.
color_map (dict, optional) – Dictionary mapping group names to colors.
save_fig (bool, default False) – If True, save figures as PDF files.
save_path (str, default "../output_files/plots") – Directory path where PDF files will be saved.
- class mioXpektron.plotting.plot_peaks.PlotPeaks(config=None)[source]
Bases:
objectClass for plotting overlapping peaks from multiple spectra files.
Features: - Load and group spectra by inferred labels (Cancer/Control/Unknown) - Overlay individual spectra with customizable transparency - Plot per-group median curves - Plot cumulative intensity by group - Flexible configuration through PlotPeaksConfig
Example:
>>> config = PlotPeaksConfig( ... data_dir="data/spectra", ... mz_min=100.0, ... mz_max=200.0, ... norm_tic=True, ... save_fig=True, ... save_path="../output_files/plots" ... ) >>> plotter = PlotPeaks(config) >>> plotter.load_data() >>> plotter.plot_overlay() >>> plotter.plot_cumulative()
- __init__(config=None)[source]
Initialize PlotPeaks.
- Parameters:
config (PlotPeaksConfig, optional) – Configuration object. If None, must set attributes manually.
- set_group_inference(func)[source]
Set custom group inference function.
- Parameters:
func (callable) – Function that takes a file path and returns group label.
- static load_window(file_path, mz_min, mz_max, norm_tic=False)[source]
Read one spectrum and return (m/z, intensity) in the requested window.
- Parameters:
file_path (str) – Path to a tab or comma-separated spectrum with columns for m/z and intensity. Column names are case-insensitive and support variations: - m/z: “mz”, “m/z”, “M/Z”, “MZ”, “Mz” - intensity: “intensity”, “Intensity”, “INTENSITY”, “int”, “Int”
mz_min (float) – Inclusive m/z window to extract.
mz_max (float) – Inclusive m/z window to extract.
norm_tic (bool, default False) – If True, normalize intensities by total ion count (sum to 1).
- Returns:
mz (np.ndarray)
inten (np.ndarray) – Intensities scaled by 1e6 (to keep values readable on plots).
- Return type:
- load_data()[source]
Load all files matching the pattern and group them by inferred labels.
- Raises:
RuntimeError – If no files match the pattern.
- Return type:
None
- get_group_counts()[source]
Get counts of spectra per group.
- Returns:
Dictionary with group names as keys and counts as values.
- Return type:
- plot_overlay(ax=None, show=True)[source]
Plot overlapping spectra with optional median curves.
- Parameters:
ax (matplotlib.axes.Axes, optional) – Axes to plot on. If None, creates new figure.
show (bool, default True) – If True, call plt.show() at the end.
- Returns:
The figure object.
- Return type:
- plot_cumulative(ax=None, show=True)[source]
Plot cumulative intensity curves by group.
- Parameters:
ax (matplotlib.axes.Axes, optional) – Axes to plot on. If None, creates new figure.
show (bool, default True) – If True, call plt.show() at the end.
- Returns:
The figure object.
- Return type:
- Parameters:
config (PlotPeaksConfig | None)
- mioXpektron.plotting.plot_peaks.plot_overlapping_peaks(data_dir, file_pattern, mz_min, mz_max, norm_tic=False, alpha=0.18, bin_width=0.001, show_median=True, show_group_cumulative=True)[source]
Overlay spectra with two colors (Cancer vs Control) inferred from file names.
DEPRECATED: This function is maintained for backwards compatibility. Use PlotPeaks class for new code.
- Parameters:
data_dir (str) – Directory containing spectra.
mz_min (float) – m/z window to visualize.
mz_max (float) – m/z window to visualize.
norm_tic (bool, default False) – Normalize each spectrum by its TIC prior to plotting.
alpha (float, default 0.18) – Line transparency for individual spectra.
bin_width (float, default 0.001) – Common grid step for interpolation (used for medians/cumulative plots).
show_median (bool, default True) – If True, overlay per-group median curves (thicker lines).
show_group_cumulative (bool, default True) – If True, plot per-group cumulative intensity curves on a separate figure.
Notes
Group detection is based on substrings in filenames: “_CC” (Cancer), “_CT” (Control).
Files without these markers are labeled “Unknown” and plotted in grey.
Examples
>>> # New recommended approach >>> config = PlotPeaksConfig( ... data_dir="data/spectra", ... mz_min=100.0, ... mz_max=200.0 ... ) >>> plotter = PlotPeaks(config) >>> plotter.load_data() >>> plotter.plot_all()