Skip to content

Commit 72f45c0

Browse files
committed
Update
1 parent b2fb7f4 commit 72f45c0

3 files changed

Lines changed: 69 additions & 25 deletions

File tree

src/bioontologies/constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
"one": "http://purl.obolibrary.org/obo/ONE",
1616
"ons": "https://raw.githubusercontent.com/enpadasi/Ontology-for-Nutritional-Studies/master/ons.owl",
1717
"ontie": "https://ontology.iedb.org/ontology/ontie.owl",
18+
"aro": "http://purl.obolibrary.org/obo/antibiotic_resistance.owl",
19+
"http://purl.obolibrary.org/obo/iao/cdt.owl": "cdt",
20+
"http://purl.obolibrary.org/obo/clo/clo_merged.owl": "clo",
21+
"http://purl.obolibrary.org/obo/clyh/clyh.owl": "clyh",
1822
}
1923

2024
IRI_TO_PREFIX = {v: k for k, v in CANONICAL.items()}

src/bioontologies/obograph.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
from collections.abc import Iterable, Mapping
1313
from functools import lru_cache
1414
from pathlib import Path
15-
from typing import Any, Literal
15+
from typing import TYPE_CHECKING, Any, Literal
1616

1717
import bioregistry
18+
import curies
1819
import pandas as pd
1920
from bioregistry import NormalizedNamableReference as Reference
2021
from bioregistry import manager
@@ -25,6 +26,9 @@
2526
from .constants import CANONICAL, IRI_TO_PREFIX
2627
from .relations import get_normalized_label, ground_relation, label_norm
2728

29+
if TYPE_CHECKING:
30+
import networkx as nx
31+
2832
__all__ = [
2933
"Definition",
3034
"Edge",
@@ -55,7 +59,7 @@ def standardize(self) -> Self:
5559
"""Standardize the data in this class."""
5660
raise NotImplementedError
5761

58-
def raise_on_unstandardized(self):
62+
def raise_on_unstandardized(self) -> None:
5963
"""Raise an exception if standarization has not occurred."""
6064
if not self.standardized:
6165
raise ValueError
@@ -215,7 +219,7 @@ class Meta(BaseModel, StandardizeMixin):
215219
subsets: list[str] | None = None
216220
xrefs: list[Xref] | None = None
217221
synonyms: list[Synonym] | None = None
218-
comments: list | None = None
222+
comments: list[str] | None = None
219223
version: str | None = None
220224
properties: list[Property] | None = Field(None, alias="basicPropertyValues")
221225
deprecated: bool = False
@@ -303,7 +307,7 @@ def from_parsed(
303307
)
304308

305309

306-
def _help_get_properties(self, predicate_iris: str | list[str]) -> list[str]:
310+
def _help_get_properties(self: Graph | Node, predicate_iris: str | list[str]) -> list[str]:
307311
if not self.meta:
308312
return []
309313
if isinstance(predicate_iris, str):
@@ -330,12 +334,16 @@ class Node(BaseModel, StandardizeMixin):
330334
@property
331335
def prefix(self) -> str | None:
332336
"""Get the prefix for the node if it has been standardized."""
333-
return self.reference and self.reference.prefix
337+
if self.reference:
338+
return self.reference.prefix
339+
return None
334340

335341
@property
336342
def identifier(self) -> str | None:
337343
"""Get the identifier for the node if it has been standardized."""
338-
return self.reference and self.reference.identifier
344+
if self.reference is not None:
345+
return self.reference.identifier
346+
return None
339347

340348
def standardize(self) -> Self:
341349
"""Ground the node to a standard prefix and luid based on its id (URI)."""
@@ -536,17 +544,23 @@ def roots(self) -> list[str]:
536544
@property
537545
def license(self) -> str | None:
538546
"""Get the license of the ontology."""
539-
return self._get_property("http://purl.org/dc/terms/license")
547+
return self._get_property(
548+
["http://purl.org/dc/terms/license", "http://purl.org/dc/elements/1.1/license"]
549+
)
540550

541551
@property
542552
def title(self) -> str | None:
543553
"""Get the title of the ontology."""
544-
return self._get_property("http://purl.org/dc/elements/1.1/title")
554+
return self._get_property(
555+
["http://purl.org/dc/terms/title", "http://purl.org/dc/elements/1.1/title"]
556+
)
545557

546558
@property
547559
def description(self) -> str | None:
548560
"""Get the license of the ontology."""
549-
return self._get_property("http://purl.org/dc/elements/1.1/description")
561+
return self._get_property(
562+
["http://purl.org/dc/terms/description", "http://purl.org/dc/elements/1.1/description"]
563+
)
550564

551565
@property
552566
def version_iri(self) -> str | None:
@@ -643,9 +657,9 @@ def standardize(
643657

644658
return self
645659

646-
def _standardize_prefix(self):
660+
def _standardize_prefix(self) -> None:
647661
if not self.id:
648-
return
662+
return None
649663
if self.id in IRI_TO_PREFIX:
650664
self.prefix = IRI_TO_PREFIX[self.id]
651665
elif self.id.startswith("http://purl.obolibrary.org/obo/"):
@@ -656,10 +670,12 @@ def _standardize_prefix(self):
656670
self.id.removeprefix("http://purl.obolibrary.org/obo/")
657671
.removesuffix(suffix)
658672
.removesuffix("_import")
673+
.removesuffix("_merged")
659674
)
660675
if prefix != bioregistry.normalize_prefix(prefix):
661676
tqdm.write(f"could not guess prefix from {self.id}")
662677
return
678+
# FIXME check if clyh/clyh
663679
self.prefix = prefix
664680
return
665681

@@ -696,7 +712,9 @@ def get_xrefs(self) -> list[tuple[Reference, Reference, Reference]]:
696712
rv.append((node.reference, xref.predicate, xref.value))
697713
return rv
698714

699-
def _get_edge_predicate_label(self, edge: Edge, ctn, require_label: bool = False) -> str:
715+
def _get_edge_predicate_label(
716+
self, edge: Edge, ctn: Mapping[str, str], require_label: bool = False
717+
) -> str:
700718
if edge.predicate:
701719
label = get_normalized_label(edge.predicate.curie)
702720
if label:
@@ -870,7 +888,7 @@ def get_curie_to_name(self) -> Mapping[str, str]:
870888
node.curie: node.name for node in self.nodes if node.name and node.reference is not None
871889
}
872890

873-
def get_networkx(self):
891+
def get_networkx(self) -> nx.MultiDiGraph:
874892
"""Get a networkx multi-directional graph."""
875893
import networkx as nx
876894

@@ -902,7 +920,7 @@ def write_warned(path: str | Path) -> None:
902920

903921

904922
@lru_cache(1)
905-
def _get_converter():
923+
def _get_converter() -> curies.Converter:
906924
return bioregistry.manager.get_converter(include_prefixes=True)
907925

908926

@@ -933,7 +951,7 @@ def _get_reference(s: str, *, debug: bool = False) -> Reference | None: # noqa:
933951
if reference is not None:
934952
return reference
935953
if s not in WARNED:
936-
logger.warning("could not parse OBO internal relation: %s", s)
954+
logger.debug("could not parse OBO internal relation: %s", s)
937955
WARNED[s] += 1
938956

939957
if "upload.wikimedia.org" in s:

src/bioontologies/robot.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
from typing import Any, Literal
1818

1919
import bioregistry
20+
import click
2021
import pystow
2122
import requests
2223
from pystow.utils import download, name_from_url
24+
from tqdm import tqdm
2325

2426
from .obograph import Graph, GraphDocument
2527

@@ -178,7 +180,8 @@ def get_obograph_by_prefix(
178180
json_path: None | str | Path = None,
179181
cache: bool = False,
180182
check: bool = True,
181-
reason: bool = True,
183+
reason: bool = False,
184+
merge: bool = False,
182185
) -> ParseResults:
183186
"""Get an ontology by its Bioregistry prefix."""
184187
if prefix != bioregistry.normalize_prefix(prefix):
@@ -194,7 +197,7 @@ def get_obograph_by_prefix(
194197
msg = f"[{prefix}] could not parse JSON from {json_iri}: {e}"
195198
messages.append(msg)
196199
GETTER_MESSAGES.append(msg)
197-
logger.warning(msg)
200+
tqdm.write(click.style(msg, fg="red"))
198201
else:
199202
return parse_results
200203

@@ -211,17 +214,26 @@ def get_obograph_by_prefix(
211214
path = os.path.join(d, name_from_url(iri))
212215
download(iri, path=path)
213216
parse_results = convert_to_obograph_local(
214-
path, json_path=json_path, from_iri=iri, check=check
217+
path,
218+
json_path=json_path,
219+
from_iri=iri,
220+
check=check,
221+
merge=merge,
222+
reason=reason,
215223
)
216224
else:
217225
parse_results = convert_to_obograph_remote(
218-
iri, json_path=json_path, check=check, reason=reason
226+
iri,
227+
json_path=json_path,
228+
check=check,
229+
reason=reason,
230+
merge=merge,
219231
)
220232
except (subprocess.CalledProcessError, KeyError):
221233
msg = f"[{prefix}] could not parse {label} from {iri}"
222234
messages.append(msg)
223235
GETTER_MESSAGES.append(msg)
224-
logger.warning(msg)
236+
tqdm.write(click.style(msg, fg="red"))
225237
continue
226238
else:
227239
# stick all messages before
@@ -237,6 +249,8 @@ def convert_to_obograph_local(
237249
json_path: None | str | Path = None,
238250
from_iri: str | None = None,
239251
check: bool = True,
252+
reason: bool = False,
253+
merge: bool = False,
240254
) -> ParseResults:
241255
"""Convert a local OWL/OBO file to an OBO Graph JSON object.
242256
@@ -255,7 +269,13 @@ def convert_to_obograph_local(
255269
output from the ROBOT conversion program
256270
"""
257271
return convert_to_obograph(
258-
input_path=path, input_flag="-i", json_path=json_path, from_iri=from_iri, check=check
272+
input_path=path,
273+
input_flag="-i",
274+
json_path=json_path,
275+
from_iri=from_iri,
276+
check=check,
277+
reason=reason,
278+
merge=merge,
259279
)
260280

261281

@@ -265,6 +285,7 @@ def convert_to_obograph_remote(
265285
json_path: None | str | Path = None,
266286
check: bool = True,
267287
reason: bool = True,
288+
merge: bool = True,
268289
) -> ParseResults:
269290
"""Convert a remote OWL/OBO file to an OBO Graph JSON object.
270291
@@ -290,6 +311,7 @@ def convert_to_obograph_remote(
290311
input_is_iri=True,
291312
check=check,
292313
reason=reason,
314+
merge=merge,
293315
)
294316

295317

@@ -301,9 +323,9 @@ def convert_to_obograph(
301323
input_is_iri: bool = False,
302324
extra_args: list[str] | None = None,
303325
from_iri: str | None = None,
304-
merge: bool = True,
326+
merge: bool = False,
305327
check: bool = True,
306-
reason: bool = True,
328+
reason: bool = False,
307329
debug: bool = False,
308330
) -> ParseResults:
309331
"""Convert a local OWL file to a JSON file.
@@ -402,7 +424,7 @@ def correct_raw_json(graph_document_raw) -> None:
402424
return graph_document_raw
403425

404426

405-
def _clean_raw_meta(element):
427+
def _clean_raw_meta(element: dict[str, Any]) -> None:
406428
meta = element.get("meta")
407429
if not meta:
408430
return
@@ -494,7 +516,7 @@ def convert(
494516
output_path: str | Path,
495517
input_flag: Literal["-i", "-I"] | None = None,
496518
*,
497-
merge: bool = True,
519+
merge: bool = False,
498520
fmt: str | None = None,
499521
check: bool = True,
500522
reason: bool = False,

0 commit comments

Comments
 (0)