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,
    precursor_charge: int,
    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 ion.

  • precursor_charge

    the charge of the precursor ion.

  • 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.

  • precursor_charge (int) –

    the charge of the precursor ion.

  • 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,
    precursor_charge: int,
    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.
        precursor_charge: the charge of the precursor ion.
        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.precursor_charge = precursor_charge
    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

precursor_charge instance-attribute

precursor_charge = precursor_charge

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.precursor_charge,
            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

to_dict

to_dict() -> dict[str, Any]

Convert the Spectrum object to a dictionary for exporting purpose.

Returns:

  • dict[str, Any]

    A dictionary containing containing the following key-value pairs:

    • "spectrum_id" (str): The unique identifier of the spectrum.
    • "num_strains_with_spectrum" (int): The number of strains associated with the spectrum.
    • "precursor_mz" (float): The precursor m/z value, rounded to four decimal places.
    • "rt" (float): The retention time, rounded to three decimal places.
    • "molecular_family" (str | None ): The identifier of the molecular family.
    • "gnps_id" (str | None ): The GNPS identifier.
    • "gnps_annotations" (dict[str, str]): A dictionary of GNPS annotations.
Source code in src/nplinker/metabolomics/spectrum.py
def to_dict(self) -> dict[str, Any]:
    """Convert the Spectrum object to a dictionary for exporting purpose.

    Returns:
        A dictionary containing containing the following key-value pairs:

            - "spectrum_id" (str): The unique identifier of the spectrum.
            - "num_strains_with_spectrum" (int): The number of strains associated with the spectrum.
            - "precursor_mz" (float): The precursor m/z value, rounded to four decimal places.
            - "rt" (float): The retention time, rounded to three decimal places.
            - "molecular_family" (str | None ): The identifier of the molecular family.
            - "gnps_id" (str | None ): The GNPS identifier.
            - "gnps_annotations" (dict[str, str]): A dictionary of GNPS annotations.
    """
    return {
        "spectrum_id": self.id,
        "num_strains_with_spectrum": len(self.strains),
        "precursor_mz": round(self.precursor_mz, 4),
        "rt": round(self.rt, 3),
        "molecular_family": self.family.id if self.family else None,
        "gnps_id": self.gnps_id,
        "gnps_annotations": self.gnps_annotations,
    }

to_tabular

to_tabular() -> dict[str, str]

Convert the Spectrum object to a tabular format.

Returns:

  • dict ( dict[str, str] ) –

    A dictionary representing the Spectrum object in tabular format. The keys can be treated as headers and values are strings in which tabs are removed. This dict can be exported as a TSV file.

Source code in src/nplinker/metabolomics/spectrum.py
def to_tabular(self) -> dict[str, str]:
    """Convert the Spectrum object to a tabular format.

    Returns:
        dict: A dictionary representing the Spectrum object in tabular format.
            The keys can be treated as headers and values are strings in which tabs are removed.
            This dict can be exported as a TSV file.
    """
    return {
        key: self._to_string(value).replace("\t", "    ")
        for key, value in self.to_dict().items()
    }