diff --git a/tests/class_info/__init__.py b/tests/class_info/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_chem_classes.py b/tests/class_info/test_chem_classes.py similarity index 98% rename from tests/test_chem_classes.py rename to tests/class_info/test_chem_classes.py index 0aaf1835..3556ef97 100644 --- a/tests/test_chem_classes.py +++ b/tests/class_info/test_chem_classes.py @@ -2,7 +2,7 @@ from pathlib import Path from nplinker.class_info.chem_classes import MolNetEnhancerResults -from . import DATA_DIR +from .. import DATA_DIR # TODO: No such file or directory: 'canopus.tsv' diff --git a/tests/test_class_matches.py b/tests/class_info/test_class_matches.py similarity index 100% rename from tests/test_class_matches.py rename to tests/class_info/test_class_matches.py diff --git a/tests/conftest.py b/tests/conftest.py index 101aa660..378cc94a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,21 +1,57 @@ from pathlib import Path import shutil import pytest - +from nplinker.metabolomics.metabolomics import load_spectra +from nplinker.metabolomics.metabolomics import make_families +from nplinker.metabolomics.spectrum import Spectrum +from nplinker.strain_collection import StrainCollection +from nplinker.strains import Strain from nplinker.utils import extract_archive - from . import DATA_DIR + def _unpack(archive: Path): filepath = DATA_DIR / archive outdir = DATA_DIR / filepath.stem extract_archive(filepath, outdir) return filepath, outdir + @pytest.fixture(scope="session", autouse=True) def prepare_data(): - _unpack("ProteoSAFe-METABOLOMICS-SNETS-c22f44b1-download_clustered_spectra.zip") - _unpack("ProteoSAFe-FEATURE-BASED-MOLECULAR-NETWORKING-92036537-download_cytoscape_data.zip") + _unpack( + "ProteoSAFe-METABOLOMICS-SNETS-c22f44b1-download_clustered_spectra.zip" + ) + _unpack( + "ProteoSAFe-FEATURE-BASED-MOLECULAR-NETWORKING-92036537-download_cytoscape_data.zip" + ) yield - shutil.rmtree(DATA_DIR / "ProteoSAFe-METABOLOMICS-SNETS-c22f44b1-download_clustered_spectra") - shutil.rmtree(DATA_DIR / "ProteoSAFe-FEATURE-BASED-MOLECULAR-NETWORKING-92036537-download_cytoscape_data") \ No newline at end of file + shutil.rmtree( + DATA_DIR / + "ProteoSAFe-METABOLOMICS-SNETS-c22f44b1-download_clustered_spectra") + shutil.rmtree( + DATA_DIR / + "ProteoSAFe-FEATURE-BASED-MOLECULAR-NETWORKING-92036537-download_cytoscape_data" + ) + + +@pytest.fixture +def spec_dict() -> dict[int, Spectrum]: + mgf_file = DATA_DIR / "spectra.mgf" + edges_file = DATA_DIR / "edges.pairsinfo" + return load_spectra(mgf_file, edges_file) + + +@pytest.fixture +def collection_from_file() -> StrainCollection: + filename = DATA_DIR / "strain_mappings.csv" + sut = StrainCollection() + sut.add_from_file(filename) + return sut + + +@pytest.fixture +def strain() -> Strain: + item = Strain("peter") + item.aliases = set(["dieter"]) + return item diff --git a/tests/test_aa_pred.py b/tests/genomics/test_aa_pred.py similarity index 97% rename from tests/test_aa_pred.py rename to tests/genomics/test_aa_pred.py index c03f11fa..1eadabb4 100644 --- a/tests/test_aa_pred.py +++ b/tests/genomics/test_aa_pred.py @@ -1,7 +1,7 @@ import pytest from Bio import SeqIO from nplinker.genomics.aa_pred import AntiSmash5Record, predict_aa -from . import DATA_DIR +from .. import DATA_DIR ANTISMASH_FILE = DATA_DIR / "antismash_v5_GCF_000016425.1_NC_009380.1.region017.gbk" diff --git a/tests/metabolomics/__init__.py b/tests/metabolomics/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_gnps_annotation_loader.py b/tests/metabolomics/test_gnps_annotation_loader.py similarity index 95% rename from tests/test_gnps_annotation_loader.py rename to tests/metabolomics/test_gnps_annotation_loader.py index 26ba4b6c..352be137 100644 --- a/tests/test_gnps_annotation_loader.py +++ b/tests/metabolomics/test_gnps_annotation_loader.py @@ -4,18 +4,17 @@ from nplinker.metabolomics.gnps.gnps_annotation_loader import \ GNPSAnnotationLoader from nplinker.metabolomics.spectrum import Spectrum -from . import DATA_DIR -from .test_metabolomics import spec_dict +from .. import DATA_DIR class GNPSAnnotationLoaderBuilder: def __init__(self): self._file = DATA_DIR / "ProteoSAFe-FEATURE-BASED-MOLECULAR-NETWORKING-92036537-download_cytoscape_data/DB_result/7dc5b46b50d94246a1de12ef485d0f75.tsv" - + def with_file(self, file) -> Self: self._file = file return self - + def build(self) -> GNPSAnnotationLoader: return GNPSAnnotationLoader(self._file) @@ -51,9 +50,9 @@ def test_annotations_are_equal(spec_dict: dict[int, Spectrum]): expected[x.spectrum_id] = x.gnps_annotations actual = GNPSAnnotationLoaderBuilder().with_file(annotations_file).build().get_annotations() - + for key in expected: expected_annotations = expected[key] actual_annotations = actual[key] for key_entry in expected_annotations: - assert expected_annotations[key_entry] == actual_annotations[key_entry] + assert expected_annotations[key_entry] == actual_annotations[key_entry] diff --git a/tests/test_gnps_downloader.py b/tests/metabolomics/test_gnps_downloader.py similarity index 94% rename from tests/test_gnps_downloader.py rename to tests/metabolomics/test_gnps_downloader.py index 74284531..394dcf32 100644 --- a/tests/test_gnps_downloader.py +++ b/tests/metabolomics/test_gnps_downloader.py @@ -6,14 +6,14 @@ import pytest from nplinker.metabolomics.gnps.gnps_downloader import GNPSDownloader -from . import DATA_DIR +from .. import DATA_DIR class GNPSDownloaderBuilder: def __init__(self): self._task_id = None self._download_root = gettempdir() - + def with_task_id(self, task_id: str) -> Self: self._task_id = task_id return self @@ -21,10 +21,10 @@ def with_task_id(self, task_id: str) -> Self: def with_download_root(self, download_root: Path) -> Self: self._download_root = download_root return self - + def build(self) -> GNPSDownloader: return GNPSDownloader(self._task_id, self._download_root) - + def test_has_gnps_task_id(): @@ -51,4 +51,4 @@ def test_downloads_file(tmp_path: Path, task_id, filename_expected): actual_names = actual.namelist() expected_names = [x.filename for x in expected.filelist if x.compress_size > 0] - assert all(item in actual_names for item in expected_names) \ No newline at end of file + assert all(item in actual_names for item in expected_names) diff --git a/tests/test_gnps_extractor.py b/tests/metabolomics/test_gnps_extractor.py similarity index 96% rename from tests/test_gnps_extractor.py rename to tests/metabolomics/test_gnps_extractor.py index ec186561..1b321a7f 100644 --- a/tests/test_gnps_extractor.py +++ b/tests/metabolomics/test_gnps_extractor.py @@ -8,7 +8,7 @@ import pytest from nplinker.metabolomics.gnps.gnps_extractor import GNPSExtractor from nplinker.utils import extract_archive -from . import DATA_DIR +from .. import DATA_DIR class GNPSExtractorBuilder: @@ -24,7 +24,7 @@ def with_file(self, file: Path) -> Self: def with_extract_path(self, extract_path: Path) -> Self: self._extract_path = extract_path return self - + def build(self) -> GNPSExtractor: return GNPSExtractor(self._file, self._extract_path) @@ -41,7 +41,7 @@ def _unpack(archive: Path): extract_archive(file, outdir) return file, outdir - + def test_default(): sut = GNPSExtractorBuilder().build() assert sut is not None @@ -51,7 +51,7 @@ def test_has_zipfile(): file = DATA_DIR / 'ProteoSAFe-METABOLOMICS-SNETS-c22f44b1-download_clustered_spectra.zip' sut = GNPSExtractorBuilder().with_file(file).build() actual = sut.get_data() - + expected = zipfile.ZipFile(file) numpy.testing.assert_array_equal(actual.namelist(), expected.namelist()) @@ -86,7 +86,7 @@ def test_creates_molecular_families(archive: Path, filename: str, tmp_path: Path sut = GNPSExtractorBuilder().with_file(file).with_extract_path(tmp_path).build() sut._extract_molecular_families() actual = Path(sut.get_extract_path()) / "molecular_families.pairsinfo" - + assert_extraction_success(filename, outdir, actual) @@ -100,7 +100,7 @@ def test_creates_file_mappings(archive: Path, filename: str, tmp_path: Path): sut = GNPSExtractorBuilder().with_file(file).with_extract_path(tmp_path).build() sut._extract_file_mappings() actual = Path(sut.get_extract_path()) / ("file_mappings" + str(Path(filename).suffix)) - + assert_extraction_success(filename, outdir, actual) @@ -113,5 +113,5 @@ def test_creates_annotations(archive: Path, filename: str, tmp_path: Path): sut = GNPSExtractorBuilder().with_file(file).with_extract_path(tmp_path).build() sut._extract_annotations() - actual = Path(sut.get_extract_path()) / "annotations.tsv" - assert_extraction_success(filename, outdir, actual) \ No newline at end of file + actual = Path(sut.get_extract_path()) / "annotations.tsv" + assert_extraction_success(filename, outdir, actual) diff --git a/tests/test_gnps_file_mapping_loader.py b/tests/metabolomics/test_gnps_file_mapping_loader.py similarity index 100% rename from tests/test_gnps_file_mapping_loader.py rename to tests/metabolomics/test_gnps_file_mapping_loader.py diff --git a/tests/test_gnps_format.py b/tests/metabolomics/test_gnps_format.py similarity index 98% rename from tests/test_gnps_format.py rename to tests/metabolomics/test_gnps_format.py index bcf9209e..58dc162b 100644 --- a/tests/test_gnps_format.py +++ b/tests/metabolomics/test_gnps_format.py @@ -5,7 +5,7 @@ from nplinker.metabolomics.gnps.gnps_format import gnps_format_from_file_mapping from nplinker.metabolomics.gnps.gnps_format import gnps_format_from_task_id from nplinker.metabolomics.gnps.gnps_format import gnps_format_from_archive -from . import DATA_DIR +from .. import DATA_DIR @pytest.mark.parametrize("filename, expected", [ diff --git a/tests/test_gnps_molecular_family_loader.py b/tests/metabolomics/test_gnps_molecular_family_loader.py similarity index 78% rename from tests/test_gnps_molecular_family_loader.py rename to tests/metabolomics/test_gnps_molecular_family_loader.py index 6acf0617..e8ac05a1 100644 --- a/tests/test_gnps_molecular_family_loader.py +++ b/tests/metabolomics/test_gnps_molecular_family_loader.py @@ -1,19 +1,16 @@ import os import numpy - import pytest -from nplinker.metabolomics.gnps.gnps_molecular_family_loader import GNPSMolecularFamilyLoader +from nplinker.metabolomics.gnps.gnps_molecular_family_loader import \ + GNPSMolecularFamilyLoader +from nplinker.metabolomics.metabolomics import make_families from nplinker.metabolomics.molecular_family import MolecularFamily +from .. import DATA_DIR -from .test_metabolomics import molecular_families, spec_dict -from . import DATA_DIR @pytest.fixture -def molecular_families_gnps(): - filename = os.path.join(DATA_DIR, "edges.pairsinfo") - sut = GNPSMolecularFamilyLoader(filename) - return sut.families() - +def molecular_families(spec_dict) -> list[MolecularFamily]: + return make_families(spec_dict.values()) @pytest.mark.parametrize("filename", [ os.path.join(DATA_DIR, "edges.pairsinfo"), @@ -36,10 +33,10 @@ def test_families_are_identical(spec_dict, molecular_families): x.id = i for spec_id in x.spectra_ids: x.add_spectrum(spec_dict[spec_id]) - + for x in molecular_families: for spec in x.spectra: x.spectra_ids.add(spec.spectrum_id) - numpy.testing.assert_array_equal(actual, molecular_families) \ No newline at end of file + numpy.testing.assert_array_equal(actual, molecular_families) diff --git a/tests/test_gnps_spectrum_loader.py b/tests/metabolomics/test_gnps_spectrum_loader.py similarity index 89% rename from tests/test_gnps_spectrum_loader.py rename to tests/metabolomics/test_gnps_spectrum_loader.py index 40d6143c..627c2bfa 100644 --- a/tests/test_gnps_spectrum_loader.py +++ b/tests/metabolomics/test_gnps_spectrum_loader.py @@ -1,7 +1,7 @@ import pytest from nplinker.metabolomics.gnps.gnps_spectrum_loader import GNPSSpectrumLoader -from . import DATA_DIR +from .. import DATA_DIR @pytest.mark.parametrize("file, expected", [ [DATA_DIR / "ProteoSAFe-METABOLOMICS-SNETS-c22f44b1-download_clustered_spectra/METABOLOMICS-SNETS-c22f44b1-download_clustered_spectra-main.mgf", 435], @@ -9,4 +9,4 @@ ]) def test_loads_spectra(file, expected): actual = GNPSSpectrumLoader(file).spectra() - assert len(actual) == expected \ No newline at end of file + assert len(actual) == expected diff --git a/tests/test_load_gnps.py b/tests/metabolomics/test_load_gnps.py similarity index 93% rename from tests/test_load_gnps.py rename to tests/metabolomics/test_load_gnps.py index 829eac49..f3c4961b 100644 --- a/tests/test_load_gnps.py +++ b/tests/metabolomics/test_load_gnps.py @@ -7,10 +7,8 @@ from nplinker.metabolomics.load_gnps import load_gnps from nplinker.strain_collection import StrainCollection from nplinker.utils import get_headers -from .test_metabolomics import spec_dict -from .test_strain_collection import collection_from_file -from . import DATA_DIR +from .. import DATA_DIR nodes_file = DATA_DIR / "nodes.tsv" @@ -77,4 +75,4 @@ def test_parse_mzxml_header(): headers = get_headers(str(DATA_DIR / "nodes_fbmn.csv")) hdr = headers[10] actual = _parse_mzxml_header(hdr, StrainCollection(), None, None) - assert actual is not None \ No newline at end of file + assert actual is not None diff --git a/tests/test_metabolomics.py b/tests/metabolomics/test_metabolomics.py similarity index 50% rename from tests/test_metabolomics.py rename to tests/metabolomics/test_metabolomics.py index 7025b559..61d83d59 100644 --- a/tests/test_metabolomics.py +++ b/tests/metabolomics/test_metabolomics.py @@ -1,27 +1,7 @@ -import pytest from nplinker.metabolomics.metabolomics import make_families from nplinker.metabolomics.metabolomics import load_dataset -from nplinker.metabolomics.metabolomics import load_spectra -from nplinker.metabolomics.molecular_family import MolecularFamily -from nplinker.metabolomics.spectrum import Spectrum from nplinker.strain_collection import StrainCollection -from . import DATA_DIR - - -@pytest.fixture -def spec_dict() -> dict[int, Spectrum]: - mgf_file = DATA_DIR / "spectra.mgf" - edges_file = DATA_DIR / "edges.pairsinfo" - return load_spectra(mgf_file, edges_file) - -@pytest.fixture -def spec_with_families(spec_dict) -> dict[int, Spectrum]: - make_families(spec_dict.values()) - return spec_dict - -@pytest.fixture -def molecular_families(spec_dict) -> list[MolecularFamily]: - return make_families(spec_dict.values()) +from .. import DATA_DIR def test_load_spectra(spec_dict): @@ -47,4 +27,4 @@ def test_load_dataset(): def test_make_families(spec_dict): families = make_families(spec_dict.values()) - assert len(families) == 25769 \ No newline at end of file + assert len(families) == 25769 diff --git a/tests/test_spectrum.py b/tests/metabolomics/test_spectrum.py similarity index 100% rename from tests/test_spectrum.py rename to tests/metabolomics/test_spectrum.py diff --git a/tests/pairedomics/__init__.py b/tests/pairedomics/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_downloader.py b/tests/pairedomics/test_downloader.py similarity index 99% rename from tests/test_downloader.py rename to tests/pairedomics/test_downloader.py index d4f3fc7f..ea2d7784 100644 --- a/tests/test_downloader.py +++ b/tests/pairedomics/test_downloader.py @@ -10,7 +10,7 @@ from nplinker.pairedomics.downloader import Downloader from nplinker.pairedomics.downloader import _generate_gnps_download_url from nplinker.pairedomics.downloader import _execute_download -from . import DATA_DIR +from .. import DATA_DIR @pytest.fixture @@ -80,7 +80,7 @@ def test_download_gnps_data(tmp_path): gnps_task_id = "c22f44b14a3d450eb836d607cb9521bb" sut = Downloader("MSV000079284", local_cache=tmp_path / 'actual') actual = sut._load_gnps_data(gnps_task_id) - + expected = zipfile.ZipFile(DATA_DIR / "ProteoSAFe-METABOLOMICS-SNETS-c22f44b1-download_clustered_spectra.zip") actual.extract("networkedges_selfloop/6da5be36f5b14e878860167fa07004d6.pairsinfo", tmp_path / "actual") @@ -101,4 +101,3 @@ def test_extract_metabolomics_data(tmp_path): assert (Path(sut.project_file_cache) / "networkedges_selfloop/6da5be36f5b14e878860167fa07004d6.pairsinfo").is_file() assert (Path(sut.project_file_cache) / "clusterinfosummarygroup_attributes_withIDs_withcomponentID/d69356c8e5044c2a9fef3dd2a2f991e1.tsv").is_file() assert (Path(sut.project_file_cache) / "spectra/METABOLOMICS-SNETS-c22f44b1-download_clustered_spectra-main.mgf").is_file() - diff --git a/tests/parsers/__init__.py b/tests/parsers/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_mgf.py b/tests/parsers/test_mgf.py similarity index 91% rename from tests/test_mgf.py rename to tests/parsers/test_mgf.py index 2afb5cbf..6587be64 100644 --- a/tests/test_mgf.py +++ b/tests/parsers/test_mgf.py @@ -1,6 +1,6 @@ from nplinker.parsers.mgf import LoadMGF -from . import DATA_DIR +from .. import DATA_DIR def test_load_mgf(): diff --git a/tests/scoring/__init__.py b/tests/scoring/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_data_linking_functions.py b/tests/scoring/test_data_linking_functions.py similarity index 100% rename from tests/test_data_linking_functions.py rename to tests/scoring/test_data_linking_functions.py diff --git a/tests/test_data_links.py b/tests/scoring/test_data_links.py similarity index 55% rename from tests/test_data_links.py rename to tests/scoring/test_data_links.py index dae99b6c..004db682 100644 --- a/tests/test_data_links.py +++ b/tests/scoring/test_data_links.py @@ -1,8 +1,24 @@ +import os +import pytest +from nplinker.metabolomics.gnps.gnps_molecular_family_loader import \ + GNPSMolecularFamilyLoader +from nplinker.metabolomics.metabolomics import make_families +from nplinker.metabolomics.spectrum import Spectrum from nplinker.scoring.linking.data_linking import DataLinks -from .test_metabolomics import spec_dict, spec_with_families -from .test_gnps_molecular_family_loader import molecular_families_gnps +from .. import DATA_DIR +@pytest.fixture +def spec_with_families(spec_dict) -> dict[int, Spectrum]: + make_families(spec_dict.values()) + return spec_dict + +@pytest.fixture +def molecular_families_gnps(): + filename = os.path.join(DATA_DIR, "edges.pairsinfo") + sut = GNPSMolecularFamilyLoader(filename) + return sut.families() + def test_collect_mappings_from_spectra(spec_with_families): sut = DataLinks() actual = sut._collect_mappings_from_spectra(spec_with_families.values()) @@ -25,4 +41,4 @@ def test_mappings_are_equal(spec_with_families, molecular_families_gnps): sut._collect_mappings_from_molecular_families(molecular_families_gnps) expected = sut.mapping_spec - assert actual.eq(expected).all(axis=None) \ No newline at end of file + assert actual.eq(expected).all(axis=None) diff --git a/tests/test_scoring.py b/tests/scoring/test_scoring.py similarity index 100% rename from tests/test_scoring.py rename to tests/scoring/test_scoring.py diff --git a/tests/test_annotations.py b/tests/test_annotations.py index f66b8302..90430856 100644 --- a/tests/test_annotations.py +++ b/tests/test_annotations.py @@ -1,6 +1,5 @@ from nplinker.annotations import load_annotations -from .test_metabolomics import spec_dict from . import DATA_DIR diff --git a/tests/test_strain.py b/tests/test_strain.py index 64b6458b..88cda472 100644 --- a/tests/test_strain.py +++ b/tests/test_strain.py @@ -2,12 +2,6 @@ from nplinker.strains import Strain -@pytest.fixture -def strain() -> Strain: - item = Strain("peter") - item.aliases = set(["dieter"]) - return item - def test_default(): sut = Strain("peter") assert sut.id == "peter" @@ -24,4 +18,4 @@ def test_has_alias(strain: Strain, alias: str, expected: bool): def test_add_alias(strain: Strain): strain.add_alias("test") - assert len(strain.aliases) == 2 \ No newline at end of file + assert len(strain.aliases) == 2 diff --git a/tests/test_strain_collection.py b/tests/test_strain_collection.py index bd88cc3b..043e43ba 100644 --- a/tests/test_strain_collection.py +++ b/tests/test_strain_collection.py @@ -1,9 +1,6 @@ import pytest from nplinker.strain_collection import StrainCollection from nplinker.strains import Strain -from tests import DATA_DIR - -from .test_strain import strain @pytest.fixture @@ -13,13 +10,6 @@ def collection(strain: Strain) -> StrainCollection: return sut -@pytest.fixture -def collection_from_file() -> StrainCollection: - filename = DATA_DIR / "strain_mappings.csv" - sut = StrainCollection() - sut.add_from_file(filename) - return sut - def test_default(): sut = StrainCollection() assert sut is not None @@ -73,7 +63,7 @@ def test_remove(collection: StrainCollection, strain: Strain): with pytest.raises(KeyError): collection.lookup(strain.id) - + assert strain not in collection # needs fixing, see #90