Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function to write GOC scores to a file #485

Open
wants to merge 1 commit into
base: feature/gene_tree_benchmark_new
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions scripts/pipeline/orthology_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +724 to +725
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about

Suggested change
msg = f"Could not open a file '{output_file}' for writing."
raise OSError(msg) from e
print(f"Could not open a file '{output_file}' for writing.")
sys.exit(1)

Instead of raising OSError after having excepted it? I gather that the purpose of excepting and then raising it is to print the msg for the user - I think this would achieve the same with less pain?



def calculate_goc_scores():
"""Docstring"""

Expand Down
31 changes: 31 additions & 0 deletions src/python/tests/test_orthology_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 3 additions & 0 deletions src/test_data/flatfiles/orth_benchmark/goc_no_permission.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gene_1_id gene_2_id goc_score
11 35 0

6 changes: 6 additions & 0 deletions src/test_data/flatfiles/orth_benchmark/goc_scores.txt
Original file line number Diff line number Diff line change
@@ -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