Skip to content

Data Models

nplinker.metabolomics

MolecularFamily

MolecularFamily(id: str)

Class to model molecular family.

Attributes:

Parameters:

  • id (str) –

    Unique id for the molecular family.

Source code in src/nplinker/metabolomics/molecular_family.py
def __init__(self, id: str):
    """Initialize the MolecularFamily.

    Args:
        id: Unique id for the molecular family.
    """
    self.id: str = id
    self.spectra_ids: set[str] = set()
    self._spectra: set[Spectrum] = set()
    self._strains: StrainCollection = StrainCollection()

id instance-attribute

id: str = id

spectra_ids instance-attribute

spectra_ids: set[str] = set()

spectra property

spectra: set[Spectrum]

Get Spectrum objects in the molecular family.

strains property

Get strains in the molecular family.

__str__

__str__() -> str
Source code in src/nplinker/metabolomics/molecular_family.py
def __str__(self) -> str:
    return (
        f"MolecularFamily(id={self.id}, #Spectrum_objects={len(self._spectra)}, "
        f"#spectrum_ids={len(self.spectra_ids)}, #strains={len(self._strains)})"
    )

__repr__

__repr__() -> str
Source code in src/nplinker/metabolomics/molecular_family.py
def __repr__(self) -> str:
    return str(self)

__eq__

__eq__(other) -> bool
Source code in src/nplinker/metabolomics/molecular_family.py
def __eq__(self, other) -> bool:
    if isinstance(other, MolecularFamily):
        return self.id == other.id
    return NotImplemented

__hash__

__hash__() -> int
Source code in src/nplinker/metabolomics/molecular_family.py
def __hash__(self) -> int:
    return hash(self.id)

__reduce__

__reduce__() -> tuple

Reduce function for pickling.

Source code in src/nplinker/metabolomics/molecular_family.py
def __reduce__(self) -> tuple:
    """Reduce function for pickling."""
    return (self.__class__, (self.id,), self.__dict__)

add_spectrum

add_spectrum(spectrum: Spectrum) -> None

Add a Spectrum object to the molecular family.

Parameters:

  • spectrum (Spectrum) –

    Spectrum object to add to the molecular family.

Source code in src/nplinker/metabolomics/molecular_family.py
def add_spectrum(self, spectrum: Spectrum) -> None:
    """Add a Spectrum object to the molecular family.

    Args:
        spectrum: `Spectrum` object to add to the molecular family.
    """
    self._spectra.add(spectrum)
    self.spectra_ids.add(spectrum.id)
    self._strains = self._strains + spectrum.strains
    # add the molecular family to the spectrum
    spectrum.family = self

detach_spectrum

detach_spectrum(spectrum: Spectrum) -> None

Remove a Spectrum object from the molecular family.

Parameters:

  • spectrum (Spectrum) –

    Spectrum object to remove from the molecular family.

Source code in src/nplinker/metabolomics/molecular_family.py
def detach_spectrum(self, spectrum: Spectrum) -> None:
    """Remove a Spectrum object from the molecular family.

    Args:
        spectrum: `Spectrum` object to remove from the molecular family.
    """
    self._spectra.remove(spectrum)
    self.spectra_ids.remove(spectrum.id)
    self._strains = self._update_strains()
    # remove the molecular family from the spectrum
    spectrum.family = None

has_strain

has_strain(strain: Strain) -> bool

Check if the given strain exists.

Parameters:

  • strain (Strain) –

    Strain object.

Returns:

  • bool

    True when the given strain exists.

Source code in src/nplinker/metabolomics/molecular_family.py
def has_strain(self, strain: Strain) -> bool:
    """Check if the given strain exists.

    Args:
        strain: `Strain` object.

    Returns:
        True when the given strain exists.
    """
    return strain in self._strains

is_singleton

is_singleton() -> bool

Check if the molecular family contains only one spectrum.

Returns:

  • bool

    True when the molecular family has only one spectrum.

Source code in src/nplinker/metabolomics/molecular_family.py
def is_singleton(self) -> bool:
    """Check if the molecular family contains only one spectrum.

    Returns:
        True when the molecular family has only one spectrum.
    """
    return len(self.spectra_ids) == 1

Spectrum

Spectrum(
    id: str,
    mz: list[float],
    intensity: list[float],
    precursor_mz: float,
    rt: float = 0,
    metadata: dict | None = None,
)

Class to model MS/MS Spectrum.

Attributes:

  • id

    the spectrum ID.

  • mz

    the list of m/z values.

  • intensity

    the list of intensity values.

  • precursor_mz

    the m/z value of the precursor.

  • rt

    the retention time in seconds.

  • metadata

    the metadata of the spectrum, i.e. the header information in the MGF file.

  • gnps_annotations (dict) –

    the GNPS annotations of the spectrum.

  • gnps_id (str | None) –

    the GNPS ID of the spectrum.

  • strains (StrainCollection) –

    the strains that this spectrum belongs to.

  • family (MolecularFamily | None) –

    the molecular family that this spectrum belongs to.

  • peaks (ndarray) –

    2D array of peaks, each row is a peak of (m/z, intensity) values.

Parameters:

  • id (str) –

    the spectrum ID.

  • mz (list[float]) –

    the list of m/z values.

  • intensity (list[float]) –

    the list of intensity values.

  • precursor_mz (float) –

    the precursor m/z.

  • rt (float, default: 0 ) –

    the retention time in seconds. Defaults to 0.

  • metadata (dict | None, default: None ) –

    the metadata of the spectrum, i.e. the header information in the MGF file.

Source code in src/nplinker/metabolomics/spectrum.py
def __init__(
    self,
    id: str,
    mz: list[float],
    intensity: list[float],
    precursor_mz: float,
    rt: float = 0,
    metadata: dict | None = None,
) -> None:
    """Initialize the Spectrum.

    Args:
        id: the spectrum ID.
        mz: the list of m/z values.
        intensity: the list of intensity values.
        precursor_mz: the precursor m/z.
        rt: the retention time in seconds. Defaults to 0.
        metadata: the metadata of the spectrum, i.e. the header information
            in the MGF file.
    """
    self.id = id
    self.mz = mz
    self.intensity = intensity
    self.precursor_mz = precursor_mz
    self.rt = rt
    self.metadata = metadata or {}

    self.gnps_annotations: dict = {}
    self.gnps_id: str | None = None
    self.strains: StrainCollection = StrainCollection()
    self.family: MolecularFamily | None = None

id instance-attribute

id = id

mz instance-attribute

mz = mz

intensity instance-attribute

intensity = intensity

precursor_mz instance-attribute

precursor_mz = precursor_mz

rt instance-attribute

rt = rt

metadata instance-attribute

metadata = metadata or {}

gnps_annotations instance-attribute

gnps_annotations: dict = {}

gnps_id instance-attribute

gnps_id: str | None = None

strains instance-attribute

family instance-attribute

family: MolecularFamily | None = None

peaks cached property

peaks: ndarray

Get the peaks, a 2D array with each row containing the values of (m/z, intensity).

__str__

__str__() -> str
Source code in src/nplinker/metabolomics/spectrum.py
def __str__(self) -> str:
    return f"Spectrum(id={self.id}, #strains={len(self.strains)})"

__repr__

__repr__() -> str
Source code in src/nplinker/metabolomics/spectrum.py
def __repr__(self) -> str:
    return str(self)

__eq__

__eq__(other) -> bool
Source code in src/nplinker/metabolomics/spectrum.py
def __eq__(self, other) -> bool:
    if isinstance(other, Spectrum):
        return self.id == other.id and self.precursor_mz == other.precursor_mz
    return NotImplemented

__hash__

__hash__() -> int
Source code in src/nplinker/metabolomics/spectrum.py
def __hash__(self) -> int:
    return hash((self.id, self.precursor_mz))

__reduce__

__reduce__() -> tuple

Reduce function for pickling.

Source code in src/nplinker/metabolomics/spectrum.py
def __reduce__(self) -> tuple:
    """Reduce function for pickling."""
    return (
        self.__class__,
        (self.id, self.mz, self.intensity, self.precursor_mz, self.rt, self.metadata),
        self.__dict__,
    )

has_strain

has_strain(strain: Strain) -> bool

Check if the given strain exists in the spectrum.

Parameters:

  • strain (Strain) –

    Strain object.

Returns:

  • bool

    True when the given strain exist in the spectrum.

Source code in src/nplinker/metabolomics/spectrum.py
def has_strain(self, strain: Strain) -> bool:
    """Check if the given strain exists in the spectrum.

    Args:
        strain: `Strain` object.

    Returns:
        True when the given strain exist in the spectrum.
    """
    return strain in self.strains