pyms.Spectrum

Classes to model Mass Spectra and Scans.

Classes:

CompositeMassSpectrum(mass_list, intensity_list)

Represents a composite mass spectrum.

MassSpectrum(mass_list, intensity_list)

Models a binned mass spectrum.

Scan(mass_list, intensity_list)

Generic object for a single Scan's raw data.

Data:

_C

Invariant TypeVar bound to pyms.Spectrum.CompositeMassSpectrum.

_M

Invariant TypeVar bound to pyms.Spectrum.MassSpectrum.

_S

Invariant TypeVar bound to pyms.Spectrum.Scan.

Functions:

array_as_numeric(array)

Convert the given numpy array to a numeric data type.

normalize_mass_spec(mass_spec[, ...])

Normalize the intensities in the given Mass Spectrum to values between 0 and max_intensity, which by default is 100.0.

class CompositeMassSpectrum(mass_list, intensity_list)[source]

Bases: MassSpectrum

Represents a composite mass spectrum.

Parameters:
Author:

Dominic Davis-Foster

Methods:

__copy__()

Returns a copy of the object.

__eq__(other)

Return whether this object is equal to another object.

__len__()

Returns the length of the object.

crop([min_mz, max_mz, inplace])

Crop the Mass Spectrum between the given mz values.

dump(file_name[, protocol])

Dumps an object to a file through pickle.dump().

from_dict(dictionary)

Create a Scan from a dictionary.

from_jcamp(file_name)

Create a MassSpectrum from a JCAMP-DX file.

from_mz_int_pairs(mz_int_pairs)

Construct a MassSpectrum from a list of (m/z, intensity) tuples.

from_spectra(spectra)

Construct a CompositeMassSpectrum from multiple MassSpectrum objects.

get_intensity_for_mass(mass)

Returns the intensity for the given mass.

get_mass_for_intensity(intensity)

Returns the mass for the given intensity.

icrop([min_index, max_index, inplace])

Crop the Mass Spectrum between the given indices.

iter_peaks()

Iterate over the peaks in the mass spectrum.

n_largest_peaks(n)

Returns the indices of the n largest peaks in the Mass Spectrum.

Attributes:

intensity_list

Returns a copy of the intensity list.

mass_list

Returns a list of the masses.

mass_spec

Returns the intensity list.

max_mass

Returns the maximum m/z value in the spectrum.

min_mass

Returns the minimum m/z value in the spectrum.

size

The number of mass spectra combined to create this composite spectrum.

__copy__()

Returns a copy of the object.

Return type:

Scan

__eq__(other)

Return whether this object is equal to another object.

Parameters:

other (Any) – The other object to test equality with.

Return type:

bool

__len__()

Returns the length of the object.

Authors:

Andrew Isaac, Qiao Wang, Vladimir Likic

Return type:

int

crop(min_mz=None, max_mz=None, inplace=False)

Crop the Mass Spectrum between the given mz values.

Parameters:
  • min_mz (Optional[float]) – The minimum mz for the new mass spectrum. Default None.

  • max_mz (Optional[float]) – The maximum mz for the new mass spectrum. Default None.

  • inplace (bool) – Whether the cropping should be applied this instance or to a copy (default behaviour). Default False.

Return type:

~_M

Returns:

The cropped Mass Spectrum

dump(file_name, protocol=3)

Dumps an object to a file through pickle.dump().

Parameters:
  • file_name (Union[str, Path, PathLike]) – Filename to save the dump as.

  • protocol (int) – The pickle protocol to use. Default 3.

Authors:

Vladimir Likic, Dominic Davis-Foster (pathlib and pickle protocol support)

classmethod from_dict(dictionary)

Create a Scan from a dictionary.

The dictionary’s keys must match the arguments taken bt the class’s constructor.

Parameters:

dictionary (Mapping)

Return type:

~_S

classmethod from_jcamp(file_name)

Create a MassSpectrum from a JCAMP-DX file.

Parameters:

file_name (Union[str, Path, PathLike]) – Path of the file to read.

Authors:

Qiao Wang, Andrew Isaac, Vladimir Likic, David Kainer, Dominic Davis-Foster

Return type:

~_M

classmethod from_mz_int_pairs(mz_int_pairs)

Construct a MassSpectrum from a list of (m/z, intensity) tuples.

Parameters:

mz_int_pairs (Sequence[Tuple[float, float]])

Return type:

~_M

classmethod from_spectra(spectra)[source]

Construct a CompositeMassSpectrum from multiple MassSpectrum objects.

If no MassSpectrum objects are given an empty CompositeMassSpectrum is returned.

Parameters:

spectra (Iterable[MassSpectrum])

Return type:

~_C

get_intensity_for_mass(mass)

Returns the intensity for the given mass.

Parameters:

mass (float)

Return type:

float

get_mass_for_intensity(intensity)

Returns the mass for the given intensity. If more than one mass has the given intensity, the first mass is returned.

Parameters:

intensity (float)

Return type:

float

icrop(min_index=0, max_index=-1, inplace=False)

Crop the Mass Spectrum between the given indices.

Parameters:
  • min_index (int) – The minimum index for the new mass spectrum. Default 0.

  • max_index (int) – The maximum index for the new mass spectrum. Default -1.

  • inplace (bool) – Whether the cropping should be applied this instance or to a copy (default behaviour). Default False.

Return type:

~_M

Returns:

The cropped Mass Spectrum

property intensity_list

Returns a copy of the intensity list.

Authors:

Qiao Wang, Andrew Isaac, Vladimir Likic

Return type:

List

iter_peaks()

Iterate over the peaks in the mass spectrum.

Return type:

Iterator[Tuple[float, float]]

property mass_list

Returns a list of the masses.

Authors:

Qiao Wang, Andrew Isaac, Vladimir Likic

Return type:

List[float]

property mass_spec

Returns the intensity list.

Authors:

Qiao Wang, Andrew Isaac, Vladimir Likic

Return type:

List

property max_mass

Returns the maximum m/z value in the spectrum.

Author:

Andrew Isaac

Return type:

Optional[float]

property min_mass

Returns the minimum m/z value in the spectrum.

Author:

Andrew Isaac

Return type:

Optional[float]

n_largest_peaks(n)

Returns the indices of the n largest peaks in the Mass Spectrum.

Parameters:

n (int) – The number of peaks to return the indices for.

Return type:

List[int]

size = 1

Type:    int

The number of mass spectra combined to create this composite spectrum.

class MassSpectrum(mass_list, intensity_list)[source]

Bases: Scan

Models a binned mass spectrum.

Parameters:
Authors:

Andrew Isaac, Qiao Wang, Vladimir Likic, Dominic Davis-Foster

Methods:

__copy__()

Returns a copy of the object.

__eq__(other)

Return whether this object is equal to another object.

__len__()

Returns the length of the object.

crop([min_mz, max_mz, inplace])

Crop the Mass Spectrum between the given mz values.

dump(file_name[, protocol])

Dumps an object to a file through pickle.dump().

from_dict(dictionary)

Create a Scan from a dictionary.

from_jcamp(file_name)

Create a MassSpectrum from a JCAMP-DX file.

from_mz_int_pairs(mz_int_pairs)

Construct a MassSpectrum from a list of (m/z, intensity) tuples.

get_intensity_for_mass(mass)

Returns the intensity for the given mass.

get_mass_for_intensity(intensity)

Returns the mass for the given intensity.

icrop([min_index, max_index, inplace])

Crop the Mass Spectrum between the given indices.

iter_peaks()

Iterate over the peaks in the mass spectrum.

n_largest_peaks(n)

Returns the indices of the n largest peaks in the Mass Spectrum.

Attributes:

intensity_list

Returns a copy of the intensity list.

mass_list

Returns a list of the masses.

mass_spec

Returns the intensity list.

max_mass

Returns the maximum m/z value in the spectrum.

min_mass

Returns the minimum m/z value in the spectrum.

__copy__()

Returns a copy of the object.

Return type:

Scan

__eq__(other)

Return whether this object is equal to another object.

Parameters:

other (Any) – The other object to test equality with.

Return type:

bool

__len__()

Returns the length of the object.

Authors:

Andrew Isaac, Qiao Wang, Vladimir Likic

Return type:

int

crop(min_mz=None, max_mz=None, inplace=False)[source]

Crop the Mass Spectrum between the given mz values.

Parameters:
  • min_mz (Optional[float]) – The minimum mz for the new mass spectrum. Default None.

  • max_mz (Optional[float]) – The maximum mz for the new mass spectrum. Default None.

  • inplace (bool) – Whether the cropping should be applied this instance or to a copy (default behaviour). Default False.

Return type:

~_M

Returns:

The cropped Mass Spectrum

dump(file_name, protocol=3)

Dumps an object to a file through pickle.dump().

Parameters:
  • file_name (Union[str, Path, PathLike]) – Filename to save the dump as.

  • protocol (int) – The pickle protocol to use. Default 3.

Authors:

Vladimir Likic, Dominic Davis-Foster (pathlib and pickle protocol support)

classmethod from_dict(dictionary)

Create a Scan from a dictionary.

The dictionary’s keys must match the arguments taken bt the class’s constructor.

Parameters:

dictionary (Mapping)

Return type:

~_S

classmethod from_jcamp(file_name)[source]

Create a MassSpectrum from a JCAMP-DX file.

Parameters:

file_name (Union[str, Path, PathLike]) – Path of the file to read.

Authors:

Qiao Wang, Andrew Isaac, Vladimir Likic, David Kainer, Dominic Davis-Foster

Return type:

~_M

classmethod from_mz_int_pairs(mz_int_pairs)[source]

Construct a MassSpectrum from a list of (m/z, intensity) tuples.

Parameters:

mz_int_pairs (Sequence[Tuple[float, float]])

Return type:

~_M

get_intensity_for_mass(mass)[source]

Returns the intensity for the given mass.

Parameters:

mass (float)

Return type:

float

get_mass_for_intensity(intensity)[source]

Returns the mass for the given intensity. If more than one mass has the given intensity, the first mass is returned.

Parameters:

intensity (float)

Return type:

float

icrop(min_index=0, max_index=-1, inplace=False)[source]

Crop the Mass Spectrum between the given indices.

Parameters:
  • min_index (int) – The minimum index for the new mass spectrum. Default 0.

  • max_index (int) – The maximum index for the new mass spectrum. Default -1.

  • inplace (bool) – Whether the cropping should be applied this instance or to a copy (default behaviour). Default False.

Return type:

~_M

Returns:

The cropped Mass Spectrum

property intensity_list

Returns a copy of the intensity list.

Authors:

Qiao Wang, Andrew Isaac, Vladimir Likic

Return type:

List

iter_peaks()

Iterate over the peaks in the mass spectrum.

Return type:

Iterator[Tuple[float, float]]

property mass_list

Returns a list of the masses.

Authors:

Qiao Wang, Andrew Isaac, Vladimir Likic

Return type:

List[float]

property mass_spec

Returns the intensity list.

Authors:

Qiao Wang, Andrew Isaac, Vladimir Likic

Return type:

List

property max_mass

Returns the maximum m/z value in the spectrum.

Author:

Andrew Isaac

Return type:

Optional[float]

property min_mass

Returns the minimum m/z value in the spectrum.

Author:

Andrew Isaac

Return type:

Optional[float]

n_largest_peaks(n)[source]

Returns the indices of the n largest peaks in the Mass Spectrum.

Parameters:

n (int) – The number of peaks to return the indices for.

Return type:

List[int]

class Scan(mass_list, intensity_list)[source]

Bases: pymsBaseClass, MassListMixin

Generic object for a single Scan’s raw data.

Parameters:
Authors:

Andrew Isaac, Qiao Wang, Vladimir Likic, Dominic Davis-Foster

Methods:

__copy__()

Returns a copy of the object.

__eq__(other)

Return whether this object is equal to another object.

__len__()

Returns the length of the object.

dump(file_name[, protocol])

Dumps an object to a file through pickle.dump().

from_dict(dictionary)

Create a Scan from a dictionary.

iter_peaks()

Iterate over the peaks in the mass spectrum.

Attributes:

intensity_list

Returns a copy of the intensity list.

mass_list

Returns a list of the masses.

mass_spec

Returns the intensity list.

max_mass

Returns the maximum m/z value in the spectrum.

min_mass

Returns the minimum m/z value in the spectrum.

__copy__()[source]

Returns a copy of the object.

Return type:

Scan

__eq__(other)[source]

Return whether this object is equal to another object.

Parameters:

other (Any) – The other object to test equality with.

Return type:

bool

__len__()[source]

Returns the length of the object.

Authors:

Andrew Isaac, Qiao Wang, Vladimir Likic

Return type:

int

dump(file_name, protocol=3)

Dumps an object to a file through pickle.dump().

Parameters:
  • file_name (Union[str, Path, PathLike]) – Filename to save the dump as.

  • protocol (int) – The pickle protocol to use. Default 3.

Authors:

Vladimir Likic, Dominic Davis-Foster (pathlib and pickle protocol support)

classmethod from_dict(dictionary)[source]

Create a Scan from a dictionary.

The dictionary’s keys must match the arguments taken bt the class’s constructor.

Parameters:

dictionary (Mapping)

Return type:

~_S

property intensity_list

Returns a copy of the intensity list.

Authors:

Qiao Wang, Andrew Isaac, Vladimir Likic

Return type:

List

iter_peaks()[source]

Iterate over the peaks in the mass spectrum.

Return type:

Iterator[Tuple[float, float]]

property mass_list

Returns a list of the masses.

Authors:

Qiao Wang, Andrew Isaac, Vladimir Likic

Return type:

List[float]

property mass_spec

Returns the intensity list.

Authors:

Qiao Wang, Andrew Isaac, Vladimir Likic

Return type:

List

property max_mass

Returns the maximum m/z value in the spectrum.

Author:

Andrew Isaac

Return type:

Optional[float]

property min_mass

Returns the minimum m/z value in the spectrum.

Author:

Andrew Isaac

Return type:

Optional[float]

_C = TypeVar(_C, bound=CompositeMassSpectrum)

Type:    TypeVar

Invariant TypeVar bound to pyms.Spectrum.CompositeMassSpectrum.

_M = TypeVar(_M, bound=MassSpectrum)

Type:    TypeVar

Invariant TypeVar bound to pyms.Spectrum.MassSpectrum.

_S = TypeVar(_S, bound=Scan)

Type:    TypeVar

Invariant TypeVar bound to pyms.Spectrum.Scan.

array_as_numeric(array)[source]

Convert the given numpy array to a numeric data type.

If the data in the array is already in a numeric data type no changes will be made.

If array is a python Sequence then it will first be converted to a numpy array.

Parameters:

array (Union[Sequence, ndarray])

Return type:

ndarray

normalize_mass_spec(mass_spec, relative_to=None, inplace=False, max_intensity=100)[source]

Normalize the intensities in the given Mass Spectrum to values between 0 and max_intensity, which by default is 100.0.

Parameters:
  • mass_spec (MassSpectrum) – The Mass Spectrum to normalize

  • relative_to (Optional[float]) – The largest intensity in the original data set. If not None the intensities are computed relative to this value. If None the value is calculated from the mass spectrum. This can be useful when normalizing several mass spectra to each other. Default None.

  • inplace (bool) – Whether the normalization should be applied to the MassSpectrum object given, or to a copy (default behaviour). Default False.

  • max_intensity (float) – The maximum intensity in the normalized spectrum. If omitted the range 0-100.0 is used. If an integer the normalized intensities will be integers. Default 100.

Return type:

MassSpectrum

Returns:

The normalized mass spectrum