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

Make code more modular #19

Merged
merged 1 commit into from
Jan 20, 2025
Merged
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
7 changes: 4 additions & 3 deletions src/biosynonyms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
"""Code for biosynonyms."""

from .model import Synonym, grounder_from_synonyms, group_synonyms, parse_synonyms
from .resources import (
Synonym,
get_gilda_terms,
get_grounder,
get_negative_synonyms,
get_positive_synonyms,
group_synonyms,
load_unentities,
parse_synonyms,
)

__all__ = [
"Synonym",
"get_gilda_terms",
"get_grounder",
"get_negative_synonyms",
"get_positive_synonyms",
"grounder_from_synonyms",
"group_synonyms",
"load_unentities",
"parse_synonyms",
Expand Down
24 changes: 20 additions & 4 deletions src/biosynonyms/generate_owl.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""Generate OWL from the positive synonyms."""

# TODO re-implement this using pyobo.

from __future__ import annotations

import gzip
from collections import ChainMap
from pathlib import Path
Expand All @@ -10,8 +14,8 @@
from curies import Reference
from typing_extensions import Doc

from biosynonyms import Synonym, get_positive_synonyms
from biosynonyms.resources import _clean_str, group_synonyms
from biosynonyms.model import Synonym, group_synonyms
from biosynonyms.resources import get_positive_synonyms

HERE = Path(__file__).parent.resolve()
EXPORT = HERE.parent.parent.joinpath("exports")
Expand Down Expand Up @@ -116,6 +120,14 @@
"""


def _text_for_turtle(synonym: Synonym) -> str:
"""Get the text ready for an object slot in Turtle, with optional language tag."""
tt = f'"{_clean_str(synonym.text)}"'
if synonym.language:
tt += f"@{synonym.language}"
return tt


def write_owl_rdf(**kwargs: Any) -> None:
"""Write OWL RDF in a Turtle file."""
with open(TTL_PATH, "w") as file:
Expand Down Expand Up @@ -211,7 +223,7 @@ def get_axiom_str(reference: Reference, synonym: Synonym) -> str | None:
a owl:Axiom ;
owl:annotatedSource {reference.curie} ;
owl:annotatedProperty {synonym.scope.curie} ;
owl:annotatedTarget {synonym.text_for_turtle} ;
owl:annotatedTarget {_text_for_turtle(synonym)} ;
{axiom_parts_str}
] .
"""
Expand Down Expand Up @@ -243,7 +255,7 @@ def _write_owl_rdf( # noqa:C901
mains: list[str] = []
axiom_strs: list[str] = []
for synonym in synonyms:
mains.append(f"{synonym.scope.curie} {synonym.text_for_turtle}")
mains.append(f"{synonym.scope.curie} {_text_for_turtle(synonym)}")
if axiom_str := get_axiom_str(reference, synonym):
axiom_strs.append(axiom_str)

Expand All @@ -265,5 +277,9 @@ def _write_owl_rdf( # noqa:C901
file.write(dedent(axiom_str))


def _clean_str(s: str) -> str:
return s


if __name__ == "__main__":
write_owl_rdf()
17 changes: 3 additions & 14 deletions src/biosynonyms/lint.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
"""Sort the synonyms file."""

from pathlib import Path
from biosynonyms.model import lint_synonyms

from .resources import (
NEGATIVES_PATH,
POSITIVES_PATH,
_load_unentities,
sort_key,
write_unentities,
)


def _sort(path: Path) -> None:
with path.open() as file:
header, *rows = (line.strip().split("\t") for line in file)
rows = sorted(rows, key=sort_key)
with path.open("w") as file:
print(*header, sep="\t", file=file)
for row in rows:
print(*row, sep="\t", file=file)


def _main() -> None:
_sort(POSITIVES_PATH)
_sort(NEGATIVES_PATH)
lint_synonyms(POSITIVES_PATH)
lint_synonyms(NEGATIVES_PATH)
write_unentities(list(_load_unentities()))


Expand Down
Loading
Loading