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"