-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
import methods to load ontologies via pronto (#7)
merges #7
- Loading branch information
Showing
5 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,5 @@ repos: | |
hooks: | ||
- id: mypy | ||
args: ["--strict", "--show-error-codes"] | ||
additional_dependencies: | ||
- pronto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import logging | ||
from os import PathLike | ||
from typing import AnyStr, BinaryIO, Union | ||
|
||
from pronto import Ontology as Prontology # type: ignore [attr-defined] | ||
|
||
from nxontology import NXOntology | ||
from nxontology.exceptions import NodeNotFound | ||
|
||
|
||
def pronto_to_nxontology(onto: Prontology) -> NXOntology: | ||
""" | ||
Create an `NXOntology` from an input `pronto.Ontology`. | ||
Obsolete terms are omitted as nodes. | ||
Only is_a / subClassOf relationships are used for edges. | ||
""" | ||
nxo = NXOntology() | ||
nxo.pronto = onto # type: ignore [attr-defined] | ||
for term in onto.terms(): | ||
if term.obsolete: | ||
# obsolete was unreliable in pronto < v2.4.0 | ||
# https://github.com/althonos/pronto/issues/122 | ||
continue | ||
nxo.add_node( | ||
term.id, | ||
identifier=term.id, | ||
label=term.name, | ||
namespace=term.namespace, | ||
) | ||
for term in onto.terms(): | ||
# add subClassOf / is_a relations | ||
# https://github.com/althonos/pronto/issues/119 | ||
for child in term.subclasses(distance=1, with_self=False): | ||
try: | ||
nxo.add_edge(term.id, child.id) | ||
except NodeNotFound as e: | ||
logging.warning( | ||
f"Cannot add edge: {term.id} --> {child.id} " | ||
f"({term.name} --> {child.name}): {e}" | ||
) | ||
return nxo | ||
|
||
|
||
def from_obo_library(slug: str) -> NXOntology: | ||
""" | ||
Read ontology from <http://www.obofoundry.org/>. | ||
Delegates to [`pronto.Ontology.from_obo_library`](https://pronto.readthedocs.io/en/stable/api/pronto.Ontology.html#pronto.Ontology.from_obo_library). | ||
""" | ||
onto = Prontology.from_obo_library(slug=slug) | ||
nxo = pronto_to_nxontology(onto) | ||
nxo.graph.graph["from_obo_library"] = slug | ||
return nxo | ||
|
||
|
||
def from_file(handle: Union[BinaryIO, str, "PathLike[AnyStr]"]) -> NXOntology: | ||
""" | ||
Read ontology in OBO, OWL, or JSON (OBO Graphs) format via pronto. | ||
Arguments: | ||
handle: Either the path to a file or a binary file handle | ||
that contains a serialized version of the ontology. | ||
""" | ||
onto = Prontology(handle=handle) | ||
return pronto_to_nxontology(onto) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import pytest | ||
|
||
from nxontology.imports import from_file, from_obo_library | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"format", | ||
[ | ||
"owl", | ||
"obo", | ||
], | ||
) | ||
def test_from_obo_library_taxrank(format: str) -> None: | ||
""" | ||
http://www.obofoundry.org/ontology/taxrank.html | ||
""" | ||
slug = f"taxrank.{format}" | ||
nxo = from_obo_library(slug) | ||
(root,) = nxo.roots | ||
assert root == "TAXRANK:0000000" | ||
cultivar = nxo.node_info("TAXRANK:0000034") | ||
assert cultivar.identifier == "TAXRANK:0000034" | ||
assert cultivar.label == "cultivar" | ||
assert "TAXRANK:0000000" in cultivar.ancestors | ||
|
||
|
||
def test_from_file_go() -> None: | ||
url = "http://release.geneontology.org/2021-02-01/ontology/go-basic.json.gz" | ||
nxo = from_file(url) | ||
assert nxo.n_nodes > 20_000 | ||
# pronto < 2.4.0 marked GO:0000003 as obsolete | ||
# https://github.com/althonos/pronto/issues/122 | ||
assert "GO:0000003" in nxo.graph | ||
info = nxo.node_info("GO:0042552") | ||
assert info.identifier == "GO:0042552" | ||
assert info.label == "myelination" | ||
assert info.data["namespace"] == "biological_process" | ||
# has edge from "axon ensheathment" to "myelination" | ||
assert nxo.graph.has_edge("GO:0008366", "GO:0042552") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters