def plot_peaks(ax: Axes, peak_list: List[Peak.Peak], label: str = "Peaks", style: str = 'o') -> List[Line2D]: """ Plots the locations of peaks as found by PyMassSpec. :param ax: The axes to plot the peaks on :param peak_list: List of peaks to plot :param label: label for plot legend. :param style: The marker style. See `https://matplotlib.org/3.1.1/api/markers_api.html` for a complete list :return: A list of Line2D objects representing the plotted data. """ if not is_peak_list(peak_list): raise TypeError("'peak_list' must be a list of Peak objects") time_list = [] height_list = [] if "line" in style.lower(): lines = [] for peak in peak_list: lines.append(ax.axvline(x=peak.rt, color="lightgrey", alpha=0.8, linewidth=0.3)) return lines else: for peak in peak_list: time_list.append(peak.rt) height_list.append(sum(peak.mass_spectrum.intensity_list)) # height_list.append(peak.height) # print(peak.height - sum(peak.mass_spectrum.intensity_list)) # print(sum(peak.mass_spectrum.intensity_list)) return ax.plot(time_list, height_list, style, label=label)
def store_peaks(peak_list: Peak, file_name: Union[str, pathlib.Path], protocol=1): """ Store the list of peak objects :param peak_list: A list of peak objects :type peak_list: list of :class:`pyms.Peaks.Class.Peak` :param file_name: File name to store peak list :type file_name: str or os.PathLike :param protocol: :type protocol: :author: Andrew Isaac :author: Dominic Davis-Foster (type assertions and pathlib support) """ if not is_peak_list(peak_list): raise TypeError("'peak_list' must be a list of Peak objects") if not is_path(file_name): raise TypeError("'file_name' must be a string or a PathLike object") file_name = prepare_filepath(file_name) fp = file_name.open('wb') pickle.dump(peak_list, fp, protocol) fp.close()
def __init__(self, expr_code: str, peak_list: Sequence[Peak]): if not isinstance(expr_code, str): raise TypeError("'expr_code' must be a string") if not is_peak_list(peak_list): raise TypeError("'peak_list' must be a list of Peak objects") self._expr_code = expr_code self._peak_list = list(peak_list)
def __init__(self, expr_code, peak_list): """ Models an experiment """ if not isinstance(expr_code, str): raise TypeError("'expr_code' must be a string") if not is_peak_list(peak_list): raise TypeError("'peak_list' must be a list of Peak objects") self._expr_code = expr_code self._peak_list = peak_list
def rel_threshold(pl: Sequence[Peak], percent: float = 2, copy_peaks: bool = True) -> List[Peak]: """ Remove ions with relative intensities less than the given relative percentage of the maximum intensity. :param pl: :param percent: Threshold for relative percentage of intensity. :default percent: ``2%`` :param copy_peaks: Whether the returned peak list should contain copies of the peaks. :return: A new list of Peak objects with threshold ions. :author: Andrew Isaac, Dominic Davis-Foster (type assertions) """ # noqa: D400 peak_list = pl if not is_peak_list(peak_list): raise TypeError("'pl' must be a list of Peak objects") if not is_number(percent): raise TypeError("'percent' must be a number > 0") if percent <= 0: raise ValueError("'percent' must be a number > 0") if copy_peaks: peak_list = copy.deepcopy(peak_list) new_peak_list = [] for peak in peak_list: ms = peak.mass_spectrum # if ms is None: # raise ValueError("The peak has no mass spectrum.") ia = ms.mass_spec # assume max(ia) big so /100 1st cutoff = (max(ia) / 100.0) * float(percent) for i in range(len(ia)): if ia[i] < cutoff: ia[i] = 0 ms.mass_spec = ia peak.mass_spectrum = ms new_peak_list.append(peak) return new_peak_list
def num_ions_threshold(pl: List, n: int, cutoff: float, copy_peaks: bool = True) -> List: """ Remove Peaks where there are less than a given number of ion intensities above the given threshold :param pl: A list of Peak objects :type pl: list :param n: Minimum number of ions that must have intensities above the cutoff :type n: int :param cutoff: The minimum intensity threshold :type cutoff: int or float :param copy_peaks: Whether a the returned peak list should contain copies of the peaks. Default ``False`` :type copy_peaks: bool, optional :return: A new list of Peak objects :rtype: list :author: Andrew Isaac, Dominic Davis-Foster (type assertions) """ if not is_peak_list(pl): raise TypeError("'pl' must be a list of Peak objects") if not isinstance(n, int): raise TypeError("'n' must be an integer") if not isinstance(cutoff, Number): raise TypeError("'cutoff' must be a Number") if copy_peaks: pl = copy.deepcopy(pl) new_pl = [] for p in pl: ms = p.mass_spectrum ia = ms.mass_spec ions = 0 for i in range(len(ia)): if ia[i] >= cutoff: ions += 1 if ions >= n: new_pl.append(p) return new_pl
def rel_threshold(pl: List, percent: float = 2, copy_peaks: bool = True) -> List: """ Remove ions with relative intensities less than the given relative percentage of the maximum intensity. :param pl: A list of Peak objects :type pl: list :param percent: Threshold for relative percentage of intensity. Default ``2%`` :type percent: float, optional :param copy_peaks: Whether a the returned peak list should contain copies of the peaks. Default ``False`` :type copy_peaks: bool, optional :return: A new list of Peak objects with threshold ions :rtype: list :author: Andrew Isaac, Dominic Davis-Foster (type assertions) """ if not is_peak_list(pl): raise TypeError("'pl' must be a list of Peak objects") if not isinstance(percent, (int, float)): raise TypeError("'percent' must be a number > 0") if percent <= 0: raise ValueError("'percent' must be a number > 0") if copy_peaks: pl = copy.deepcopy(pl) new_pl = [] for p in pl: ms = p.mass_spectrum ia = ms.mass_spec # assume max(ia) big so /100 1st cutoff = (max(ia) / 100.0) * float(percent) for i in range(len(ia)): if ia[i] < cutoff: ia[i] = 0 ms.mass_spec = ia p.mass_spectrum = ms new_pl.append(p) return new_pl
def num_ions_threshold( pl: Sequence[Peak], n: int, cutoff: float, copy_peaks: bool = True, ) -> List[Peak]: """ Remove Peaks where there are fewer than ``n`` ions with intensities above the given threshold. :param pl: :param n: Minimum number of ions that must have intensities above the cutoff. :param cutoff: The minimum intensity threshold. :param copy_peaks: Whether the returned peak list should contain copies of the peaks. :return: A new list of Peak objects. :author: Andrew Isaac, Dominic Davis-Foster (type assertions) """ if not is_peak_list(pl): raise TypeError("'pl' must be a list of Peak objects") if not isinstance(n, int): raise TypeError("'n' must be an integer") if not is_number(cutoff): raise TypeError("'cutoff' must be a number") if copy_peaks: pl = copy.deepcopy(pl) new_pl = [] for p in pl: ms = p.mass_spectrum ia = ms.mass_spec ions = 0 for i in range(len(ia)): if ia[i] >= cutoff: ions += 1 if ions >= n: new_pl.append(p) return new_pl
def plot_peaks(ax, peak_list, label="Peaks", style="o"): """ Plots the locations of peaks as found by PyMassSpec. :param ax: The axes to plot the peaks on :type ax: matplotlib.axes.Axes :param peak_list: List of peaks to plot :type peak_list: :class:`list` of :class:`pyms.Peak.Class.Peak` objects :param label: label for plot legend. Default ``Peaks`` :type label: str, optional :param style: The marker style. See `https://matplotlib.org/3.1.1/api/markers_api.html` for a complete list :type style: str :return: A list of Line2D objects representing the plotted data. :rtype: list of :class:`matplotlib.lines.Line2D` """ if not is_peak_list(peak_list): raise TypeError("'peak_list' must be a list of Peak objects") time_list = [] height_list = [] if "line" in style.lower(): lines = [] for peak in peak_list: lines.append( ax.axvline(x=peak.rt, color="lightgrey", alpha=0.8, linewidth=0.3)) return lines else: for peak in peak_list: time_list.append(peak.rt) height_list.append(sum(peak.get_mass_spectrum().intensity_list)) # height_list.append(peak.height) # print(peak.height - sum(peak.get_mass_spectrum().intensity_list)) # print(sum(peak.mass_spectrum.intensity_list)) return ax.plot(time_list, height_list, style, label=label)