From fa1f3fc638fe4e5dac8bcacf1ece4982147ce7c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Balajti?= Date: Fri, 10 Nov 2023 11:38:58 +0100 Subject: [PATCH 1/2] replace json with model_dump --- htsinfer/htsinfer.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/htsinfer/htsinfer.py b/htsinfer/htsinfer.py index 4967a4d9..d29c89ae 100755 --- a/htsinfer/htsinfer.py +++ b/htsinfer/htsinfer.py @@ -85,7 +85,7 @@ def evaluate(self): self.get_library_stats() LOGGER.info( "Library stats determined: " - f"{self.config.results.library_stats.json()}" + f"{self.config.results.library_stats.model_dump_json()}" ) # determine library source @@ -93,7 +93,7 @@ def evaluate(self): self.config.results.library_source = self.get_library_source() LOGGER.info( "Library source determined: " - f"{self.config.results.library_source.json()}" + f"{self.config.results.library_source.model_dump_json()}" ) # determine library type @@ -106,7 +106,7 @@ def evaluate(self): LOGGER.warning(f"{type(exc).__name__}: {str(exc)}") LOGGER.info( "Library type determined: " - f"{self.config.results.library_type.json()}" + f"{self.config.results.library_type.model_dump_json()}" ) # determine read orientation @@ -119,7 +119,7 @@ def evaluate(self): LOGGER.warning(f"{type(exc).__name__}: {str(exc)}") LOGGER.info( "Read orientation determined: " - f"{self.config.results.read_orientation.json()}" + f"{self.config.results.read_orientation.model_dump_json()}" ) # determine read layout @@ -132,7 +132,7 @@ def evaluate(self): LOGGER.warning(f"{type(exc).__name__}: {str(exc)}") LOGGER.info( "Read layout determined: " - f"{self.config.results.read_layout.json()}" + f"{self.config.results.read_layout.model_dump_json()}" ) except FileProblem as exc: @@ -148,7 +148,7 @@ def evaluate(self): LOGGER.error(f"{type(exc).__name__}: {str(exc)}") # log results - LOGGER.info(f"Results: {self.config.results.json()}") + LOGGER.info(f"Results: {self.config.results.model_dump_json()}") def prepare_env(self): """Set up work environment.""" From fbd91b0b3706435e30ab79046f8230acdae1c783 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Balajti?= Date: Fri, 10 Nov 2023 13:24:52 +0100 Subject: [PATCH 2/2] feat: add org_id param #108 --- htsinfer/get_library_source.py | 11 ++-- tests/test_get_library_source.py | 90 ++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 4 deletions(-) diff --git a/htsinfer/get_library_source.py b/htsinfer/get_library_source.py index 4dd79c81..b4d7b06a 100644 --- a/htsinfer/get_library_source.py +++ b/htsinfer/get_library_source.py @@ -78,8 +78,11 @@ def evaluate(self) -> ResultsSource: # Check if library_source is provided, otherwise infer it if self.org_id is not None: source.file_1.taxon_id = self.org_id - org_name = self.get_organism_name(self.org_id, self.transcripts_file) - + org_name = self.get_organism_name( + self.org_id, + self.transcripts_file + ) + if org_name is not None: source.file_1.short_name = org_name @@ -89,8 +92,8 @@ def evaluate(self) -> ResultsSource: else: LOGGER.warning( - f"Taxon ID '{self.org_id}' not found in organism dictionary, " - "inferring source organism..." + f"Taxon ID '{self.org_id}' not found in " + "organism dictionary, inferring source organism..." ) index = self.create_kallisto_index() library_source = self.get_source( diff --git a/tests/test_get_library_source.py b/tests/test_get_library_source.py index e8e01498..5cd2df54 100644 --- a/tests/test_get_library_source.py +++ b/tests/test_get_library_source.py @@ -355,3 +355,93 @@ def test_get_organism_name_file_problem(self): test_instance.get_organism_name( taxon_id, CONFIG.args.t_file_processed ) + + def test_evaluate_org_id_is_none(self, monkeypatch, tmpdir): + """Test when self.org_id is None.""" + CONFIG.args.org_id = None + CONFIG.args.path_1_processed = FILE_MATE_1 + CONFIG.args.path_2_processed = FILE_MATE_2 + CONFIG.args.t_file_processed = FILE_TRANSCRIPTS + CONFIG.args.tmp_dir = tmpdir + CONFIG.args.out_dir = tmpdir + test_instance = GetLibSource(config=CONFIG) + + # Mock the create_kallisto_index method to return a specific result + monkeypatch.setattr( + 'htsinfer.get_library_source.GetLibSource.create_kallisto_index', + lambda *args, **kwargs: tmpdir / "kallisto.idx", + ) + + # Mock the get_source method to return a specific result + monkeypatch.setattr( + 'htsinfer.get_library_source.GetLibSource.get_source', + lambda *args, **kwargs: SOURCE_FRUIT_FLY, + ) + + result = test_instance.evaluate() + + assert result.file_1.taxon_id == SOURCE_FRUIT_FLY.taxon_id + assert result.file_1.short_name == SOURCE_FRUIT_FLY.short_name + + assert result.file_2.taxon_id == SOURCE_FRUIT_FLY.taxon_id + assert result.file_2.short_name == SOURCE_FRUIT_FLY.short_name + + def test_evaluate_org_id_not_none_no_org_name(self, monkeypatch, tmpdir): + """Test when self.org_id is not None but org_name is not found.""" + CONFIG.args.org_id = 7227 + CONFIG.args.path_1_processed = FILE_MATE_1 + CONFIG.args.path_2_processed = FILE_MATE_2 + CONFIG.args.t_file_processed = FILE_TRANSCRIPTS + CONFIG.args.tmp_dir = tmpdir + CONFIG.args.out_dir = tmpdir + test_instance = GetLibSource(config=CONFIG) + + # Mock the get_organism_name method to return None + monkeypatch.setattr( + 'htsinfer.get_library_source.GetLibSource.get_organism_name', + lambda *args, **kwargs: None, + ) + + # Mock the create_kallisto_index method to return a specific result + monkeypatch.setattr( + 'htsinfer.get_library_source.GetLibSource.create_kallisto_index', + lambda *args, **kwargs: tmpdir / "kallisto.idx", + ) + + # Mock the get_source method to return a specific result + monkeypatch.setattr( + 'htsinfer.get_library_source.GetLibSource.get_source', + lambda *args, **kwargs: SOURCE_FRUIT_FLY, + ) + + result = test_instance.evaluate() + + assert result.file_1.taxon_id == SOURCE_FRUIT_FLY.taxon_id + assert result.file_1.short_name == SOURCE_FRUIT_FLY.short_name + + assert result.file_2.taxon_id == SOURCE_FRUIT_FLY.taxon_id + assert result.file_2.short_name == SOURCE_FRUIT_FLY.short_name + + def test_evaluate_org_id_not_none_name_found(self, monkeypatch, tmpdir): + """Test when self.org_id is not None and org_name is found.""" + CONFIG.args.org_id = 7227 + CONFIG.args.path_1_processed = FILE_MATE_1 + CONFIG.args.path_2_processed = FILE_MATE_2 + CONFIG.args.t_file_processed = FILE_TRANSCRIPTS + CONFIG.args.tmp_dir = tmpdir + CONFIG.args.out_dir = tmpdir + test_instance = GetLibSource(config=CONFIG) + + # Mock the get_organism_name method to return a specific result + monkeypatch.setattr( + 'htsinfer.get_library_source.GetLibSource.get_organism_name', + lambda *args, **kwargs: "dmelanogaster", + ) + + result = test_instance.evaluate() + + assert result.file_1.taxon_id == 7227 + assert result.file_1.short_name == "dmelanogaster" + + assert result.file_2.taxon_id == 7227 + assert result.file_2.short_name == "dmelanogaster"