Skip to content

Commit

Permalink
remove class SingletonFamily
Browse files Browse the repository at this point in the history
Class MolecularFamily can be used to represent singleton molecular family.
  • Loading branch information
CunliangGeng committed Dec 14, 2023
1 parent 4031a73 commit d472eba
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/nplinker/class_info/chem_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ def _read_cf_classes(self, mne_dir):
nr_nodes = line.pop(0)
# todo: make it easier to query classes of singleton families
# if singleton family, format like '-1_spectrum-id' like canopus results
# CG: Note that the singleton families id is "singleton-" + uuid.
# Note that the singleton families id is "singleton-" + spectrum-id.
if nr_nodes == "1":
component = f"-1_{cluster}"
class_info = []
Expand Down
3 changes: 1 addition & 2 deletions src/nplinker/metabolomics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import logging
from .molecular_family import MolecularFamily
from .singleton_family import SingletonFamily
from .spectrum import GNPS_KEY
from .spectrum import Spectrum


logging.getLogger(__name__).addHandler(logging.NullHandler())

__all__ = ["MolecularFamily", "SingletonFamily", "GNPS_KEY", "Spectrum"]
__all__ = ["MolecularFamily", "GNPS_KEY", "Spectrum"]
10 changes: 0 additions & 10 deletions src/nplinker/metabolomics/singleton_family.py

This file was deleted.

2 changes: 0 additions & 2 deletions src/nplinker/nplinker.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,6 @@ def get_common_strains(
) -> dict[tuple[Spectrum | MolecularFamily, GCF], list[Strain]]:
"""Get common strains between given spectra/molecular families and GCFs.
Note that SingletonFamily objects are excluded from given molecular families.
Args:
met(Sequence[Spectrum] | Sequence[MolecularFamily]):
A list of Spectrum or MolecularFamily objects.
Expand Down
10 changes: 0 additions & 10 deletions src/nplinker/scoring/linking/data_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from nplinker.genomics.gcf import GCF
from nplinker.logconfig import LogConfig
from nplinker.metabolomics import MolecularFamily
from nplinker.metabolomics import SingletonFamily
from nplinker.metabolomics import Spectrum
from .utils import calc_correlation_matrix
from .utils import isinstance_all
Expand Down Expand Up @@ -108,8 +107,6 @@ def get_common_strains(
) -> dict[tuple[Spectrum | MolecularFamily, GCF], list[Strain]]:
"""Get common strains between given spectra/molecular families and GCFs.
Note that SingletonFamily objects are excluded from given `spectra_or_mfs`.
Args:
spectra_or_mfs(Sequence[Spectrum | MolecularFamily]):
A list of Spectrum and/or MolecularFamily objects.
Expand Down Expand Up @@ -138,8 +135,6 @@ def get_common_strains(
strain_ids = self.occurrence_gcf_strain.columns
results = {}
for obj in spectra_or_mfs:
if isinstance(obj, SingletonFamily):
continue
for gcf in gcfs:
if isinstance(obj, Spectrum):
shared_strains = strain_ids[
Expand Down Expand Up @@ -215,12 +210,7 @@ def _get_occurrence_mf_strain(
strains as columns, where index is `mf.family_id` and column name is
`strain.id`. The values are 1 if the molecular family contains the
strain and 0 otherwise.
Note that SingletonFamily objects are excluded from given `mfs`.
"""
# remove SingletonFamily objects
mfs = [mf for mf in mfs if not isinstance(mf, SingletonFamily)]

df_mf_strain = pd.DataFrame(
np.zeros((len(mfs), len(strains))),
index=[mf.family_id for mf in mfs],
Expand Down
31 changes: 0 additions & 31 deletions tests/scoring/test_data_links.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pandas as pd
import pytest
from pandas.testing import assert_frame_equal
from nplinker.metabolomics import SingletonFamily


def test_init(datalinks):
Expand Down Expand Up @@ -173,36 +172,6 @@ def test_get_common_strains_spec_mf(datalinks, spectra, mfs, gcfs, strains_list)
assert sut == expected


def test_get_common_strains_sf(datalinks, mfs, gcfs, strains_list):
"""Test get_common_strains method for input SingletonFamily."""
smf = SingletonFamily()

sut = datalinks.get_common_strains([smf], gcfs)
assert sut == {}

# the expected are same as `test_get_common_strains_mf`
mfs_mix = (*mfs[:2], smf)
sut = datalinks.get_common_strains(mfs_mix, gcfs)
expected = {
(mfs[0], gcfs[0]): [strains_list[0]],
(mfs[0], gcfs[1]): [],
(mfs[0], gcfs[2]): [strains_list[0]],
(mfs[1], gcfs[0]): [],
(mfs[1], gcfs[1]): [strains_list[1]],
(mfs[1], gcfs[2]): [strains_list[1]],
}
assert sut == expected

sut = datalinks.get_common_strains(mfs_mix, gcfs, filter_no_shared=True)
expected = {
(mfs[0], gcfs[0]): [strains_list[0]],
(mfs[0], gcfs[2]): [strains_list[0]],
(mfs[1], gcfs[1]): [strains_list[1]],
(mfs[1], gcfs[2]): [strains_list[1]],
}
assert sut == expected


def test_get_common_strains_invalid_value(datalinks, spectra, gcfs):
"""Test get_common_strains method for empty arguments."""
with pytest.raises(ValueError) as e:
Expand Down

0 comments on commit d472eba

Please sign in to comment.