diff --git a/scripts/pipeline/orthology_benchmark.py b/scripts/pipeline/orthology_benchmark.py index 678687a8016..ef48de54198 100755 --- a/scripts/pipeline/orthology_benchmark.py +++ b/scripts/pipeline/orthology_benchmark.py @@ -694,6 +694,37 @@ def calculate_goc_genomes(core_name1: str, core_name2: str, gtf_dir: str, res_di return goc +def write_goc_scores(goc_scores: List[Tuple[str, str, float]], output_file: str) -> None: + """Writes (appends) calculated GOC scores for putative orthologs to a specified file. + + Format: gene1_id \t gene2_id \t goc_score \n. If the file does not exist, the function creates it. + + Args: + goc_scores: A list with putative orthologs and corresponding GOC scores. + output_file: Path to the output file. + + Raises: + OSError: If `output_file` does not exist and cannot be created. If `output_file` exists but cannot be + opened for writing. + + """ + if not os.path.exists(output_file): + try: + with open(output_file, "a") as file_handler: + file_handler.write("gene_1_id\tgene_2_id\tgoc_score\n") + except OSError as e: + msg = f"Could not create a file '{output_file}' for writing." + raise OSError(msg) from e + + try: + with open(output_file, "a") as file_handler: + for i in goc_scores: + file_handler.write(f"{i[0]}\t{i[1]}\t{i[2]}\n") + except OSError as e: + msg = f"Could not open a file '{output_file}' for writing." + raise OSError(msg) from e + + def calculate_goc_scores(): """Docstring""" diff --git a/src/python/tests/test_orthology_benchmark.py b/src/python/tests/test_orthology_benchmark.py index 0089f48110e..483f732c8d7 100644 --- a/src/python/tests/test_orthology_benchmark.py +++ b/src/python/tests/test_orthology_benchmark.py @@ -681,3 +681,34 @@ def test_calculate_goc_genomes() -> None: "species_species1_core_51_104_3", "species_species2_core_51_104_5", test_files_dir, orthofinder_res, 2, 3 ) == exp_out + + +def test_write_goc_scores(tmp_dir: Path) -> None: + """Tests :func:`orthology_benchmark.write_goc_scores()`. + + Args: + tmp_dir:Unit test temp directory (fixture). + + """ + out_file = tmp_dir / "test_goc_out.txt" + orthology_benchmark.write_goc_scores([("11", "35", 0), ("21", "35", 50), ("22", "36", 50)], out_file) + orthology_benchmark.write_goc_scores([("23", "31", 50), ("23", "38", 50)], out_file) + + # pylint: disable-next=no-member + test_files_dir = pytest.files_dir / "orth_benchmark" # type: ignore[attr-defined, operator] + assert file_cmp(out_file, test_files_dir / "goc_scores.txt") + + +def test_write_goc_scores_errors() -> None: + """Tests :func:`orthology_benchmark.write_goc_scores()` when it fails to write to an output file. + """ + scores = [("23", "31", 50), ("23", "38", 50)] + + with raises(OSError, match=r"Could not create a file '/compara/goc.txt' for writing."): + orthology_benchmark.write_goc_scores(scores, "/compara/goc.txt") + + # pylint: disable-next=no-member + test_dir = pytest.files_dir / "orth_benchmark" # type: ignore[attr-defined, operator] + test_file = test_dir / "goc_no_permission.txt" + with raises(OSError, match=fr"Could not open a file '{test_file}' for writing."): + orthology_benchmark.write_goc_scores(scores, test_file) diff --git a/src/test_data/flatfiles/orth_benchmark/goc_no_permission.txt b/src/test_data/flatfiles/orth_benchmark/goc_no_permission.txt new file mode 100644 index 00000000000..936113871eb --- /dev/null +++ b/src/test_data/flatfiles/orth_benchmark/goc_no_permission.txt @@ -0,0 +1,3 @@ +gene_1_id gene_2_id goc_score +11 35 0 + diff --git a/src/test_data/flatfiles/orth_benchmark/goc_scores.txt b/src/test_data/flatfiles/orth_benchmark/goc_scores.txt new file mode 100644 index 00000000000..bdf760396e8 --- /dev/null +++ b/src/test_data/flatfiles/orth_benchmark/goc_scores.txt @@ -0,0 +1,6 @@ +gene_1_id gene_2_id goc_score +11 35 0 +21 35 50 +22 36 50 +23 31 50 +23 38 50