Skip to content
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
6 changes: 3 additions & 3 deletions src/pyobo/api/alts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .utils import _get_pi, get_version_from_kwargs
from ..constants import GetOntologyKwargs, check_should_cache, check_should_force
from ..getters import get_ontology
from ..identifier_utils import wrap_norm_prefix
from ..identifier_utils import Reference, wrap_norm_prefix
from ..utils.cache import cached_multidict
from ..utils.path import CacheArtifact, get_cache_path

Expand Down Expand Up @@ -67,7 +67,7 @@ def get_primary_reference(
identifier: str | None = None,
/,
**kwargs: Unpack[GetOntologyKwargs],
) -> curies.ReferenceTuple | None:
) -> Reference | None:
"""Get the primary reference for an entity."""
reference = _get_pi(prefix, identifier)
try:
Expand All @@ -77,7 +77,7 @@ def get_primary_reference(
raise
# this happens on invalid prefix. maybe revise?
return None
return curies.ReferenceTuple(reference.prefix, primary_identifier)
return Reference(prefix=reference.prefix, identifier=primary_identifier)


def get_primary_curie(
Expand Down
2 changes: 1 addition & 1 deletion src/pyobo/api/hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
from .properties import get_literal_properties
from .utils import _get_pi
from ..constants import GetOntologyKwargs
from ..identifier_utils import Reference
from ..struct import has_member, has_part, is_a, member_of, part_of
from ..struct.reference import Reference
from ..struct.struct_utils import ReferenceHint, _ensure_ref

__all__ = [
Expand Down
3 changes: 1 addition & 2 deletions src/pyobo/api/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
check_should_use_tqdm,
)
from ..getters import get_ontology
from ..identifier_utils import wrap_norm_prefix
from ..struct.reference import Reference
from ..identifier_utils import Reference, wrap_norm_prefix
from ..struct.struct_utils import ReferenceHint, _ensure_ref
from ..utils.cache import cached_df
from ..utils.path import CacheArtifact, get_cache_path, get_relation_cache_path
Expand Down
31 changes: 11 additions & 20 deletions src/pyobo/api/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"""Utilities for high-level API."""

import warnings

import curies
from bioregistry import NormalizedNamableReference as Reference
from curies import ReferenceTuple

from ..identifier_utils import Reference
from ..utils.ver import (
VersionError,
get_version,
Expand All @@ -24,21 +22,14 @@


def _get_pi(
prefix: str | curies.Reference | ReferenceTuple, identifier: str | None = None, /
reference: str | curies.Reference | ReferenceTuple, _unused: str | None = None, /
) -> Reference:
if isinstance(prefix, ReferenceTuple | curies.Reference):
if identifier is not None:
raise ValueError("unexpected non-none value passed as second positional argument")
return Reference(prefix=prefix.prefix, identifier=prefix.identifier)
if isinstance(prefix, str) and identifier is None:
return Reference.from_curie(prefix)
if identifier is None:
raise ValueError(
"prefix was given as a string, so an identifier was expected to be passed as a string as well"
)
warnings.warn(
"Passing a prefix and identifier as seperate arguments is deprecated. Please pass a curies.Reference or curies.ReferenceTuple in the first positional-only argument instead.",
DeprecationWarning,
stacklevel=4, # this is 4 since this is (always?) called from inside a decorator
)
return Reference(prefix=prefix, identifier=identifier)
if _unused is not None:
raise ValueError("unexpected non-none value passed as second positional argument")
if isinstance(reference, Reference):
return reference
if isinstance(reference, ReferenceTuple | curies.Reference):
return Reference.from_reference(reference)
if isinstance(reference, str):
return Reference.from_curie(reference)
raise TypeError(f"unexpected type {type(reference)}")
4 changes: 2 additions & 2 deletions src/pyobo/cli/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from ..constants import GetOntologyKwargs

if TYPE_CHECKING:
from curies import Reference
import curies

__all__ = [
"lookup",
Expand Down Expand Up @@ -306,7 +306,7 @@ def descendants(curie: str, **kwargs: Unpack[GetOntologyKwargs]) -> None:


def _list_curies(
references: Iterable[Reference] | None, **kwargs: Unpack[GetOntologyKwargs]
references: Iterable[curies.Reference] | None, **kwargs: Unpack[GetOntologyKwargs]
) -> None:
if not references:
return
Expand Down
2 changes: 2 additions & 0 deletions src/pyobo/identifier_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
NotCURIEError,
ParseError,
ParseValidationError,
Reference,
UnparsableIRIError,
UnregisteredPrefixError,
_is_valid_identifier,
Expand All @@ -24,6 +25,7 @@
"NotCURIEError",
"ParseError",
"ParseValidationError",
"Reference",
"UnparsableIRIError",
"UnregisteredPrefixError",
"_is_valid_identifier",
Expand Down
3 changes: 2 additions & 1 deletion src/pyobo/identifier_utils/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

import bioregistry
import click
from bioregistry import NormalizedNamableReference as Reference
from bioregistry.constants import FailureReturnType
from curies.preprocessing import BlocklistError, PreprocessingConverter
from curies_processing import get_rules
from pydantic import ValidationError
from typing_extensions import Doc

from .reference import Reference
from .relations import ground_relation

__all__ = [
Expand All @@ -24,6 +24,7 @@
"NotCURIEError",
"ParseError",
"ParseValidationError",
"Reference",
"UnparsableIRIError",
"UnregisteredPrefixError",
"_parse_str_or_curie_or_uri_helper",
Expand Down
5 changes: 5 additions & 0 deletions src/pyobo/identifier_utils/reference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Defines a PyOBO reference."""

from bioregistry import NormalizedNamableReference as Reference

__all__ = ["Reference"]
11 changes: 6 additions & 5 deletions src/pyobo/identifier_utils/relations/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
from pathlib import Path

import requests
from bioregistry import NormalizedNamedReference
from tqdm import tqdm

from ..reference import Reference

__all__ = [
"get_normalized_label",
"ground_relation",
Expand Down Expand Up @@ -65,7 +66,7 @@ def _norm(s: str) -> str:
return s.replace(" ", "").replace("_", "").replace(":", "").lower()


def ground_relation(s: str) -> NormalizedNamedReference | None:
def ground_relation(s: str) -> Reference | None:
"""Ground a string to a RO property."""
return get_lookups().get(_norm(s))

Expand All @@ -82,14 +83,14 @@ def get_normalized_label(curie_or_uri: str) -> str | None:


@lru_cache(1)
def get_lookups() -> Mapping[str, NormalizedNamedReference]:
def get_lookups() -> Mapping[str, Reference]:
"""Get lookups for relation ontology properties."""
d = {}
for record in json.loads(PATH.read_text()):
prefix, identifier, label = record["prefix"], record["identifier"], record["label"]
d[_norm(label)] = NormalizedNamedReference(prefix=prefix, identifier=identifier, name=label)
d[_norm(label)] = Reference(prefix=prefix, identifier=identifier, name=label)
for s in record.get("synonyms", []):
d[_norm(s)] = NormalizedNamedReference(prefix=prefix, identifier=identifier, name=label)
d[_norm(s)] = Reference(prefix=prefix, identifier=identifier, name=label)
return d


Expand Down
2 changes: 1 addition & 1 deletion src/pyobo/ner/normalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ def ground(
# can be type annotated with the right reference
return Reference.from_reference(match.reference)
if strict_match:
raise ValueError
raise ValueError(f"no match found for query: {query} against prefixes: {prefix}")
return None
10 changes: 3 additions & 7 deletions src/pyobo/struct/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
"""Data structures for OBO."""

from .reference import (
OBOLiteral,
Reference,
Referenced,
_parse_str_or_curie_or_uri,
default_reference,
)
from .reference import OBOLiteral, Referenced, _parse_str_or_curie_or_uri, default_reference
from .struct import (
CHARLIE_TERM,
HUMAN_TERM,
Expand Down Expand Up @@ -41,6 +35,7 @@
transcribes_to,
translates_to,
)
from ..identifier_utils import Reference

__all__ = [
"CHARLIE_TERM",
Expand All @@ -50,6 +45,7 @@
"OBOLiteral",
"Obo",
"Reference",
"Reference",
"Referenced",
"Stanza",
"StanzaType",
Expand Down
3 changes: 2 additions & 1 deletion src/pyobo/struct/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
from rdflib import XSD

from . import vocabulary as pv
from .reference import OBOLiteral, Reference, _parse_datetime
from .reference import OBOLiteral, _parse_datetime
from .struct import get_iris
from ..identifier_utils import Reference

if TYPE_CHECKING:
from .reference import Referenced
Expand Down
2 changes: 1 addition & 1 deletion src/pyobo/struct/obo/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import bioregistry
import networkx as nx
from bioregistry import NormalizedNamableReference as Reference
from curies import ReferenceTuple
from curies.preprocessing import BlocklistError
from curies.vocabulary import SynonymScope
Expand Down Expand Up @@ -45,6 +44,7 @@
from ...identifier_utils import (
NotCURIEError,
ParseError,
Reference,
UnparsableIRIError,
_is_valid_identifier,
_parse_str_or_curie_or_uri_helper,
Expand Down
2 changes: 1 addition & 1 deletion src/pyobo/struct/obo/reader_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
from collections.abc import Mapping, Sequence

import click
from bioregistry import NormalizedNamableReference as Reference
from curies import ReferenceTuple
from curies import vocabulary as v

from pyobo.identifier_utils import Reference
from pyobo.struct.reference import (
OBOLiteral,
_obo_parse_identifier,
Expand Down
2 changes: 1 addition & 1 deletion src/pyobo/struct/obograph/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from typing import cast

import obographs as og
from curies import Reference
from obographs import StandardizedGraph, StandardizedMeta

from pyobo.constants import TypeDefType
from pyobo.identifier_utils import Reference

__all__ = [
"assert_graph_equal",
Expand Down
7 changes: 5 additions & 2 deletions src/pyobo/struct/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,21 @@
import curies
import dateutil.parser
import pytz
from bioregistry import NormalizedNamableReference as Reference
from curies import ReferenceTuple
from curies import vocabulary as v
from curies.preprocessing import BlocklistError

from ..identifier_utils import (
NotCURIEError,
ParseError,
Reference,
UnparsableIRIError,
_is_valid_identifier,
_parse_str_or_curie_or_uri_helper,
)

__all__ = [
"OBOLiteral",
"Reference",
"Referenced",
"default_reference",
"get_preferred_curie",
Expand Down Expand Up @@ -158,9 +157,13 @@ def default_reference(prefix: str, identifier: str, name: str | None = None) ->

>>> default_reference("chebi", "conjugate_base_of")
Reference(prefix="obo", identifier="chebi#conjugate_base_of", name=None)

>>> default_reference("CHEBI", "conjugate_base_of")
Reference(prefix="obo", identifier="chebi#conjugate_base_of", name=None)
"""
if not identifier.strip():
raise ValueError("default identifier is empty")
prefix = bioregistry.normalize_prefix(prefix) or prefix.lower()
return Reference(prefix="obo", identifier=f"{prefix}#{identifier}", name=name)


Expand Down
5 changes: 3 additions & 2 deletions src/pyobo/struct/skos/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from curies import Converter, Reference
import curies
from curies import Converter
from rdflib import Graph, Node, URIRef

from pyobo.struct import Obo
Expand All @@ -29,7 +30,7 @@ def write_skos(
graph.serialize(path, format=format or "ttl")


def _expand_rdflib(converter: Converter, reference: Reference) -> URIRef:
def _expand_rdflib(converter: Converter, reference: curies.Reference) -> URIRef:
import rdflib

return rdflib.URIRef(converter.expand_reference(reference, strict=True))
Expand Down
16 changes: 6 additions & 10 deletions src/pyobo/struct/skos/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@

import curies
import rdflib
from bioregistry import NormalizedNamableReference, NormalizedNamedReference
from curies import Reference
from curies import vocabulary as v
from rdflib import DCTERMS, RDF, RDFS, SKOS, VANN, Graph, Node, URIRef
from tqdm import tqdm

from pyobo import Annotation
from pyobo.identifier_utils import get_converter
from pyobo.identifier_utils import Reference, get_converter
from pyobo.struct import Obo, Term, build_ontology
from pyobo.struct.vocabulary import has_source

Expand Down Expand Up @@ -73,12 +71,10 @@ def _get_scheme_object_literal(p: Node) -> str | None:
raise ValueError(f"no prefix given nor found using {VANN.preferredNamespacePrefix}")

root_terms = [
NormalizedNamableReference.from_reference(
converter.parse_uri(str(subject), strict=True).to_pydantic()
)
Reference.from_reference(converter.parse_uri(str(subject), strict=True).to_pydantic())
for subject in graph.objects(scheme, SKOS.hasTopConcept)
]
terms: dict[Reference, Term] = {}
terms: dict[curies.Reference, Term] = {}
for concept in tqdm(
graph.subjects(RDF.type, SKOS.Concept),
desc=f"[{prefix}] SKOS concepts to OWL",
Expand Down Expand Up @@ -129,10 +125,10 @@ def _get_scheme_object_literal(p: Node) -> str | None:
)


def _cleanup_narrow_matches(terms: dict[Reference, Term]) -> None:
def _cleanup_narrow_matches(terms: dict[curies.Reference, Term]) -> None:
for parent in terms.values():
for child in parent.get_property_objects(v.narrow_match):
if not isinstance(child, Reference):
if not isinstance(child, curies.Reference):
continue
if child not in terms:
continue
Expand Down Expand Up @@ -174,7 +170,7 @@ def get_term(
labels = _literal_objects(graph, node, SKOS.prefLabel, language_priority)
definitions = _literal_objects(graph, node, SKOS.definition, language_priority)
term = Term(
reference=NormalizedNamedReference(
reference=Reference(
prefix=reference_tuple.prefix,
identifier=reference_tuple.identifier,
name=labels[0] if labels else None,
Expand Down
Loading
Loading