diff --git a/src/pyobo/api/alts.py b/src/pyobo/api/alts.py index ee90519c..2d46ab04 100644 --- a/src/pyobo/api/alts.py +++ b/src/pyobo/api/alts.py @@ -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 @@ -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: @@ -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( diff --git a/src/pyobo/api/hierarchy.py b/src/pyobo/api/hierarchy.py index df0649ef..c45b0776 100644 --- a/src/pyobo/api/hierarchy.py +++ b/src/pyobo/api/hierarchy.py @@ -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__ = [ diff --git a/src/pyobo/api/relations.py b/src/pyobo/api/relations.py index a5338d1d..de086c5c 100644 --- a/src/pyobo/api/relations.py +++ b/src/pyobo/api/relations.py @@ -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 diff --git a/src/pyobo/api/utils.py b/src/pyobo/api/utils.py index 3bbf01d2..e5e6c646 100644 --- a/src/pyobo/api/utils.py +++ b/src/pyobo/api/utils.py @@ -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, @@ -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)}") diff --git a/src/pyobo/cli/lookup.py b/src/pyobo/cli/lookup.py index a91aad03..4bee8d6f 100644 --- a/src/pyobo/cli/lookup.py +++ b/src/pyobo/cli/lookup.py @@ -21,7 +21,7 @@ from ..constants import GetOntologyKwargs if TYPE_CHECKING: - from curies import Reference + import curies __all__ = [ "lookup", @@ -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 diff --git a/src/pyobo/identifier_utils/__init__.py b/src/pyobo/identifier_utils/__init__.py index 8333bfdc..e0790d21 100644 --- a/src/pyobo/identifier_utils/__init__.py +++ b/src/pyobo/identifier_utils/__init__.py @@ -8,6 +8,7 @@ NotCURIEError, ParseError, ParseValidationError, + Reference, UnparsableIRIError, UnregisteredPrefixError, _is_valid_identifier, @@ -24,6 +25,7 @@ "NotCURIEError", "ParseError", "ParseValidationError", + "Reference", "UnparsableIRIError", "UnregisteredPrefixError", "_is_valid_identifier", diff --git a/src/pyobo/identifier_utils/api.py b/src/pyobo/identifier_utils/api.py index 3c56f0c7..58975f97 100644 --- a/src/pyobo/identifier_utils/api.py +++ b/src/pyobo/identifier_utils/api.py @@ -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__ = [ @@ -24,6 +24,7 @@ "NotCURIEError", "ParseError", "ParseValidationError", + "Reference", "UnparsableIRIError", "UnregisteredPrefixError", "_parse_str_or_curie_or_uri_helper", diff --git a/src/pyobo/identifier_utils/reference.py b/src/pyobo/identifier_utils/reference.py new file mode 100644 index 00000000..1c548244 --- /dev/null +++ b/src/pyobo/identifier_utils/reference.py @@ -0,0 +1,5 @@ +"""Defines a PyOBO reference.""" + +from bioregistry import NormalizedNamableReference as Reference + +__all__ = ["Reference"] diff --git a/src/pyobo/identifier_utils/relations/api.py b/src/pyobo/identifier_utils/relations/api.py index f851ced9..01f735de 100644 --- a/src/pyobo/identifier_utils/relations/api.py +++ b/src/pyobo/identifier_utils/relations/api.py @@ -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", @@ -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)) @@ -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 diff --git a/src/pyobo/ner/normalizer.py b/src/pyobo/ner/normalizer.py index d3d7e54f..18f6dd7a 100644 --- a/src/pyobo/ner/normalizer.py +++ b/src/pyobo/ner/normalizer.py @@ -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 diff --git a/src/pyobo/struct/__init__.py b/src/pyobo/struct/__init__.py index 7924d96e..f1723bee 100644 --- a/src/pyobo/struct/__init__.py +++ b/src/pyobo/struct/__init__.py @@ -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, @@ -41,6 +35,7 @@ transcribes_to, translates_to, ) +from ..identifier_utils import Reference __all__ = [ "CHARLIE_TERM", @@ -50,6 +45,7 @@ "OBOLiteral", "Obo", "Reference", + "Reference", "Referenced", "Stanza", "StanzaType", diff --git a/src/pyobo/struct/functional.py b/src/pyobo/struct/functional.py index a115d395..751eec10 100644 --- a/src/pyobo/struct/functional.py +++ b/src/pyobo/struct/functional.py @@ -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 diff --git a/src/pyobo/struct/obo/reader.py b/src/pyobo/struct/obo/reader.py index aa2fe48a..2dcc4c26 100644 --- a/src/pyobo/struct/obo/reader.py +++ b/src/pyobo/struct/obo/reader.py @@ -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 @@ -45,6 +44,7 @@ from ...identifier_utils import ( NotCURIEError, ParseError, + Reference, UnparsableIRIError, _is_valid_identifier, _parse_str_or_curie_or_uri_helper, diff --git a/src/pyobo/struct/obo/reader_utils.py b/src/pyobo/struct/obo/reader_utils.py index e2d4cbae..4b24f527 100644 --- a/src/pyobo/struct/obo/reader_utils.py +++ b/src/pyobo/struct/obo/reader_utils.py @@ -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, diff --git a/src/pyobo/struct/obograph/utils.py b/src/pyobo/struct/obograph/utils.py index 88965a95..eb249367 100644 --- a/src/pyobo/struct/obograph/utils.py +++ b/src/pyobo/struct/obograph/utils.py @@ -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", diff --git a/src/pyobo/struct/reference.py b/src/pyobo/struct/reference.py index 805734f6..0f77633b 100644 --- a/src/pyobo/struct/reference.py +++ b/src/pyobo/struct/reference.py @@ -14,7 +14,6 @@ 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 @@ -22,6 +21,7 @@ from ..identifier_utils import ( NotCURIEError, ParseError, + Reference, UnparsableIRIError, _is_valid_identifier, _parse_str_or_curie_or_uri_helper, @@ -29,7 +29,6 @@ __all__ = [ "OBOLiteral", - "Reference", "Referenced", "default_reference", "get_preferred_curie", @@ -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) diff --git a/src/pyobo/struct/skos/export.py b/src/pyobo/struct/skos/export.py index 4879f1e8..2c6ca560 100644 --- a/src/pyobo/struct/skos/export.py +++ b/src/pyobo/struct/skos/export.py @@ -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 @@ -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)) diff --git a/src/pyobo/struct/skos/reader.py b/src/pyobo/struct/skos/reader.py index 51677411..f90e3628 100644 --- a/src/pyobo/struct/skos/reader.py +++ b/src/pyobo/struct/skos/reader.py @@ -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 @@ -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", @@ -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 @@ -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, diff --git a/src/pyobo/struct/struct.py b/src/pyobo/struct/struct.py index 0f630ad8..d27c98ff 100644 --- a/src/pyobo/struct/struct.py +++ b/src/pyobo/struct/struct.py @@ -34,7 +34,6 @@ from . import vocabulary as v from .reference import ( OBOLiteral, - Reference, Referenced, _reference_list_tag, comma_separate_references, @@ -73,6 +72,7 @@ TypeDefType, get_semantic_mapping_metadata, ) +from ..identifier_utils import Reference from ..utils.cache import write_gzipped_graph from ..utils.io import multidict, write_iterable_tsv from ..utils.path import ( @@ -466,7 +466,7 @@ def get_species( if species.prefix == prefix: return species if strict: - raise ValueError + raise ValueError(f"no species found with prefix {prefix}") return None def extend_relationship(self, typedef: ReferenceHint, references: Iterable[Reference]) -> None: diff --git a/src/pyobo/struct/struct_utils.py b/src/pyobo/struct/struct_utils.py index 699d8b26..ff678735 100644 --- a/src/pyobo/struct/struct_utils.py +++ b/src/pyobo/struct/struct_utils.py @@ -20,7 +20,6 @@ from . import vocabulary as v from .reference import ( OBOLiteral, - Reference, Referenced, comma_separate_references, default_reference, @@ -34,6 +33,7 @@ from ..identifier_utils import ( NotCURIEError, ParseError, + Reference, _is_valid_identifier, _parse_str_or_curie_or_uri_helper, ) diff --git a/src/pyobo/struct/typedef.py b/src/pyobo/struct/typedef.py index 031077de..0e099f58 100644 --- a/src/pyobo/struct/typedef.py +++ b/src/pyobo/struct/typedef.py @@ -7,8 +7,9 @@ from curies import ReferenceTuple from . import vocabulary as v -from .reference import Reference, default_reference +from .reference import default_reference from .struct import TypeDef +from ..identifier_utils import Reference from ..resources.ro import load_ro __all__ = [ diff --git a/src/pyobo/struct/vocabulary.py b/src/pyobo/struct/vocabulary.py index 1e607159..dd172157 100644 --- a/src/pyobo/struct/vocabulary.py +++ b/src/pyobo/struct/vocabulary.py @@ -5,7 +5,7 @@ import curies from curies import vocabulary as _v -from .reference import Reference +from ..identifier_utils import Reference __all__ = [ "equivalent_class", diff --git a/src/pyobo/utils/path.py b/src/pyobo/utils/path.py index 8e3e3f13..01a6b7d8 100644 --- a/src/pyobo/utils/path.py +++ b/src/pyobo/utils/path.py @@ -8,8 +8,8 @@ from pathlib import Path from typing import TYPE_CHECKING, Any +import curies import pandas as pd -from curies import Reference from pystow import VersionHint from pystow.utils.download import DownloadKwargs from typing_extensions import Unpack @@ -166,7 +166,7 @@ def get_cache_path( def get_relation_cache_path( ontology: str, - reference: Reference, + reference: curies.Reference, *, version: str | None = None, ) -> Path: diff --git a/tests/test_api.py b/tests/test_api.py index 52bb6126..094d64be 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -16,8 +16,8 @@ from sssom_pydantic.testing import assert_semantic_mapping_equal import pyobo -from pyobo import Reference as PyOBOReference from pyobo import ( + Reference, default_reference, get_name, get_name_by_curie, @@ -97,34 +97,40 @@ def test_get_primary(self, _: Any, __: Any) -> None: self.assertIsNotNone(primary_id) self.assertEqual("0003700", primary_id) self.assertIsNone(get_name(ReferenceTuple("go", "0001071"), upgrade_identifier=False)) - name = get_name(ReferenceTuple("go", "0001071"), upgrade_identifier=True) - self.assertIsNotNone(name) - self.assertEqual("DNA-binding transcription factor activity", name) + self.assertEqual( + "DNA-binding transcription factor activity", + get_name(ReferenceTuple("go", "0001071"), upgrade_identifier=True), + ) @mock_id_alts_mapping @mock_id_names_mapping def test_get_primary_by_curie(self, _: Any, __: Any) -> None: """Test upgrading an obsolete CURIE.""" - primary_curie = get_primary_curie("go:0001071") + secondary_curie = "go:0001071" + expected_primary_curie = "go:0003700" + + primary_curie = get_primary_curie(secondary_curie) self.assertIsNotNone(primary_curie) - self.assertEqual("go:0003700", primary_curie) + self.assertEqual(expected_primary_curie, primary_curie) - primary_reference = get_primary_reference("go:0001071") + primary_reference = get_primary_reference(secondary_curie) self.assertIsNotNone(primary_reference) - self.assertEqual(ReferenceTuple("go", "0003700"), primary_reference) + self.assertEqual(Reference.from_curie(expected_primary_curie), primary_reference) - self.assertIsNone(get_name_by_curie("go:0001071", upgrade_identifier=False)) + self.assertIsNone(get_name_by_curie(secondary_curie, upgrade_identifier=False)) # if set explicitly to true, then it will do it eagerly - name = get_name_by_curie("go:0001071", upgrade_identifier=True) - self.assertIsNotNone(name) - self.assertEqual("DNA-binding transcription factor activity", name) + self.assertEqual( + "DNA-binding transcription factor activity", + get_name_by_curie(secondary_curie, upgrade_identifier=True), + ) # if not set, or left as default, then it will do it only if there's no # original name - name = get_name_by_curie("go:0001071", upgrade_identifier=None) - self.assertIsNotNone(name) - self.assertEqual("DNA-binding transcription factor activity", name) + self.assertEqual( + "DNA-binding transcription factor activity", + get_name_by_curie(secondary_curie, upgrade_identifier=None), + ) @mock_id_alts_mapping @mock_id_names_mapping @@ -134,15 +140,12 @@ def test_already_primary(self, _: Any, __: Any) -> None: self.assertIsNotNone(primary_id) self.assertEqual("0003700", primary_id) name = get_name(ReferenceTuple("go", "0003700")) - self.assertIsNotNone(name) self.assertEqual("DNA-binding transcription factor activity", name) name = get_name(ReferenceTuple("go", "0003700")) - self.assertIsNotNone(name) self.assertEqual("DNA-binding transcription factor activity", name) name = get_name(curies.Reference(prefix="go", identifier="0003700")) - self.assertIsNotNone(name) self.assertEqual("DNA-binding transcription factor activity", name) @mock_id_alts_mapping @@ -157,12 +160,11 @@ def test_get_primary_on_reference(self, _: Any, __: Any) -> None: @mock_id_names_mapping def test_already_primary_by_curie(self, _: Any, __: Any) -> None: """Test when you give a primary CURIE.""" - primary_curie = get_primary_curie("go:0003700") - self.assertIsNotNone(primary_curie) - self.assertEqual("go:0003700", primary_curie) - name = get_name_by_curie("go:0003700") - self.assertIsNotNone(name) - self.assertEqual("DNA-binding transcription factor activity", name) + for curie in ["go:0003700", "GO:0003700"]: + primary_reference = get_primary_reference(curie) + self.assertEqual(Reference.from_curie(curie), primary_reference) + name = get_name_by_curie(curie) + self.assertEqual("DNA-binding transcription factor activity", name) @mock_id_alts_mapping @mock_id_names_mapping @@ -183,11 +185,11 @@ def test_api(self) -> None: """ tr1 = default_reference(TEST_P1, "r1") td1 = TypeDef(reference=tr1) - r1 = PyOBOReference(prefix=TEST_P1, identifier="1", name="test name") - r2 = PyOBOReference(prefix=TEST_P1, identifier="2") - r3 = PyOBOReference(prefix=TEST_P1, identifier="3") - r2_1 = PyOBOReference(prefix=TEST_P2, identifier="X") - r2_2 = PyOBOReference(prefix=TEST_P2, identifier="Y") + r1 = Reference(prefix=TEST_P1, identifier="1", name="test name") + r2 = Reference(prefix=TEST_P1, identifier="2") + r3 = Reference(prefix=TEST_P1, identifier="3") + r2_1 = Reference(prefix=TEST_P2, identifier="X") + r2_2 = Reference(prefix=TEST_P2, identifier="Y") syn1 = "ttt1" t1 = Term(reference=r1).append_alt(r2).append_synonym(syn1).append_xref(r2_1) t1.append_comment("test comment") @@ -261,12 +263,12 @@ def test_api(self) -> None: ) ) self.assertEqual(3, len(semantic_mappings)) - subject_source = PyOBOReference(prefix="bioregistry", identifier=r1.prefix) + subject_source = Reference(prefix="bioregistry", identifier=r1.prefix) expected_semantic_mappings = [ SemanticMapping( subject=r1, subject_type=_v.owl_class, - predicate=PyOBOReference.from_reference(_v.alternative_term), + predicate=Reference.from_reference(_v.alternative_term), object=r2, justification=_v.unspecified_matching_process.without_name(), source=subject_source, @@ -275,7 +277,7 @@ def test_api(self) -> None: SemanticMapping( subject=r1, subject_type=_v.owl_class, - predicate=PyOBOReference.from_reference(_v.has_dbxref), + predicate=Reference.from_reference(_v.has_dbxref), object=r2_1, justification=_v.unspecified_matching_process.without_name(), source=subject_source, @@ -284,7 +286,7 @@ def test_api(self) -> None: SemanticMapping( subject=r3, subject_type=_v.owl_class, - predicate=PyOBOReference.from_reference(_v.exact_match), + predicate=Reference.from_reference(_v.exact_match), object=r2_2, justification=_v.unspecified_matching_process.without_name(), source=subject_source, @@ -300,13 +302,13 @@ def test_api(self) -> None: LiteralMapping( text="ttt1", reference=r1, - predicate=PyOBOReference.from_reference(_v.has_related_synonym), + predicate=Reference.from_reference(_v.has_related_synonym), source=TEST_P1, ), LiteralMapping( text="test name", reference=r1, - predicate=PyOBOReference.from_reference(_v.has_label), + predicate=Reference.from_reference(_v.has_label), source=TEST_P1, ), ] diff --git a/tests/test_obo_reader/test_get.py b/tests/test_obo_reader/test_get.py index 52517f43..a1f5a02b 100644 --- a/tests/test_obo_reader/test_get.py +++ b/tests/test_obo_reader/test_get.py @@ -272,11 +272,11 @@ def test_get_node_relations(self) -> None: self.assertIsNotNone(target) self.assertIsInstance(target, Reference) - self.assertEqual(("chebi", "29228"), target.pair) + self.assertEqual(ReferenceTuple("chebi", "29228"), target.pair) self.assertIsNotNone(typedef) self.assertIsInstance(typedef, Reference) - self.assertEqual(("obo", "chebi#is_conjugate_base_of"), typedef.pair) + self.assertEqual(ReferenceTuple("obo", "chebi#is_conjugate_base_of"), typedef.pair) class TestGet(unittest.TestCase): @@ -299,6 +299,7 @@ def test_get_id_alts_mapping(self) -> None: alt_id: CHEBI:14384 """ id_alts_mapping = self.ontology.get_id_alts_mapping() + self.assertNotEqual({}, id_alts_mapping, msg="alternate ID dictionary unexpectedly empty") self.assertNotIn("C00462", id_alts_mapping) self.assertIn("16042", id_alts_mapping, msg="halide anion alt_id fields not parsed") self.assertEqual({"5605", "14384"}, set(id_alts_mapping["16042"])) @@ -308,4 +309,4 @@ def test_typedefs(self) -> None: xx = default_reference("chebi", "has_major_microspecies_at_pH_7_3") td = self.ontology._index_typedefs() self.assertIn(xx.pair, td) - self.assertIn(ReferenceTuple("ro", "0018033"), td) + self.assertIn(ReferenceTuple("ro", "0018033"), set(td)) diff --git a/tests/test_obo_reader/test_reader.py b/tests/test_obo_reader/test_reader.py index c9eedcff..0b92ec2b 100644 --- a/tests/test_obo_reader/test_reader.py +++ b/tests/test_obo_reader/test_reader.py @@ -1416,8 +1416,5 @@ def test_get_grounder(self) -> None: """) r1 = Reference(prefix="CHEBI", identifier="16236", name="ethanol") grounder = ontology.get_grounder() - match = grounder.get_best_match("Ethanol") - self.assertIsNotNone(match) - if match is None: - raise ValueError + match = grounder.get_best_match("Ethanol", strict=True) self.assertEqual(r1, match.reference) diff --git a/tests/test_struct/test_obo/test_struct_term.py b/tests/test_struct/test_obo/test_struct_term.py index f77f2155..c8d2c559 100644 --- a/tests/test_struct/test_obo/test_struct_term.py +++ b/tests/test_struct/test_obo/test_struct_term.py @@ -78,33 +78,6 @@ def test_invalid_prefix(self) -> None: with self.assertRaises(BioregistryError): Nope() - def test_reference_validation(self) -> None: - """Test validation of prefix.""" - with self.assertRaises(ValueError): - Reference(prefix="nope", identifier="also_nope") - - # For when we want to do regex checking - # with self.assertRaises(ValueError): - # Reference(prefix="go", identifier="nope") - # with self.assertRaises(ValueError): - # Reference(prefix="go", identifier="12345") - - r1 = Reference(prefix="go", identifier="1234567") - self.assertEqual("go", r1.prefix) - self.assertEqual("1234567", r1.identifier) - - r2 = Reference(prefix="GO", identifier="1234567") - self.assertEqual("go", r2.prefix) - self.assertEqual("1234567", r2.identifier) - - r3 = Reference(prefix="go", identifier="GO:1234567") - self.assertEqual("go", r3.prefix) - self.assertEqual("1234567", r3.identifier) - - r4 = Reference(prefix="GO", identifier="GO:1234567") - self.assertEqual("go", r4.prefix) - self.assertEqual("1234567", r4.identifier) - def test_synonym_typedef(self) -> None: """Test synonym type definition serialization.""" r1 = Reference(prefix="OMO", identifier="0003012", name="acronym") diff --git a/tests/test_struct/test_obo/test_typedef.py b/tests/test_struct/test_obo/test_typedef.py index d72b89be..eac30726 100644 --- a/tests/test_struct/test_obo/test_typedef.py +++ b/tests/test_struct/test_obo/test_typedef.py @@ -5,16 +5,11 @@ from textwrap import dedent import bioregistry -from bioregistry import NormalizedNamedReference from curies import vocabulary as v from pyobo import Obo, Reference, default_reference from pyobo.struct.reference import OBOLiteral -from pyobo.struct.struct import ( - Synonym, - TypeDef, - build_ontology, -) +from pyobo.struct.struct import Synonym, TypeDef, build_ontology from pyobo.struct.typedef import ( exact_match, has_contributor, @@ -318,9 +313,7 @@ def test_9_synonym(self) -> None: typedef = TypeDef( reference=REF, - synonyms=[ - Synonym("bears role", type=NormalizedNamedReference.from_reference(v.previous_name)) - ], + synonyms=[Synonym("bears role", type=Reference.from_reference(v.previous_name))], ) self.assert_obo_stanza( """\ @@ -343,7 +336,7 @@ def test_9_synonym(self) -> None: synonyms=[ Synonym( "bears role", - type=NormalizedNamedReference.from_reference(v.previous_name), + type=Reference.from_reference(v.previous_name), specificity="EXACT", ) ], @@ -388,7 +381,7 @@ def test_11_property_value(self) -> None: typedef = TypeDef( reference=REF, properties={ - has_contributor.reference: [NormalizedNamedReference.from_reference(v.charlie)], + has_contributor.reference: [Reference.from_reference(v.charlie)], has_inchi.reference: [OBOLiteral.string("abc")], }, ) diff --git a/tests/test_utils/test_utils.py b/tests/test_utils/test_utils.py index 2dcd2955..4987b5ce 100644 --- a/tests/test_utils/test_utils.py +++ b/tests/test_utils/test_utils.py @@ -2,7 +2,7 @@ import unittest -from curies import Reference +import curies from pyobo.identifier_utils import ( NotCURIEError, @@ -21,7 +21,7 @@ def assert_pair( ) -> None: """Test a pair is parsed properly.""" xx = _parse_str_or_curie_or_uri_helper(curie, ontology_prefix=ontology_prefix) - if not isinstance(xx, Reference): + if not isinstance(xx, curies.Reference): raise self.fail() self.assertEqual(expected, xx.pair)