Skip to content

Commit

Permalink
Clean up types
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Dec 12, 2024
1 parent a7435d2 commit e6d1ae3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
41 changes: 21 additions & 20 deletions src/pyobo/struct/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ class Term(Referenced):
default_factory=lambda: defaultdict(list)
)

_axioms: dict[tuple[Reference, Reference], list[tuple[Reference, Reference]]] = field(
_axioms: dict[tuple[Reference, Reference], list[ObjectProperty]] = field(
default_factory=lambda: defaultdict(list)
)
_str_axioms: dict[tuple[Reference, Reference], list[tuple[Reference, str]]] = field(
_str_axioms: dict[tuple[Reference, Reference], list[LiteralProperty]] = field(
default_factory=lambda: defaultdict(list)
)

Expand Down Expand Up @@ -471,13 +471,13 @@ def get_mappings(
def _get_object_axiom_target(
self, p: Reference, o: Reference, ap: Reference
) -> Reference | None:
for axiom_predicate, axiom_target in self._axioms.get((p, o), []):
for axiom_predicate, axiom_target, _ in self._axioms.get((p, o), []):
if axiom_predicate == ap:
return axiom_target
return None

def _get_str_axiom_target(self, p: Reference, o: Reference, ap: Reference) -> str | None:
for axiom_predicate, axiom_target in self._str_axioms.get((p, o), []):
for axiom_predicate, axiom_target, _ in self._str_axioms.get((p, o), []):
if axiom_predicate == ap:
return axiom_target
return None
Expand Down Expand Up @@ -578,12 +578,12 @@ def annotate_object(
def _annotate_axiom(
self, p: Reference, o: Reference, axiom: ObjectProperty | LiteralProperty
) -> None:
match axiom:
case ObjectProperty(predicate, target, _):
self._axioms[p, o].append((predicate, target))
case LiteralProperty(predicate, target, _datatype):
# TODO use datatype?
self._str_axioms[p, o].append((predicate, target))
if isinstance(axiom, ObjectProperty):
self._axioms[p, o].append(axiom)
elif isinstance(axiom, LiteralProperty):
self._str_axioms[p, o].append(axiom)
else:
raise TypeError

def set_species(self, identifier: str, name: str | None = None) -> Self:
"""Append the from_species relation."""
Expand Down Expand Up @@ -733,21 +733,22 @@ def _emit_relations(

@classmethod
def _format_trailing_modifiers(
cls, axioms: list[tuple[Reference, Reference] | tuple[Reference, str]], ontology_prefix: str
cls, axioms: list[ObjectProperty | LiteralProperty], ontology_prefix: str
) -> str:
# See https://owlcollab.github.io/oboformat/doc/GO.format.obo-1_4.html#S.1.4
# trailing modifiers can be both axioms and some other implementation-specific
# things, so split up the place where axioms are put in here
modifiers = []

for predicate, target in sorted(axioms, key=lambda p: (p[0], type(p[1]), p[1])):
if isinstance(target, Reference):
right = cls._reference(target, ontology_prefix)
else:
right = target
modifiers: list[tuple[str, str]] = []

for axiom in axioms:
match axiom:
case ObjectProperty(predicate, target, _):
right = cls._reference(target, ontology_prefix)
case LiteralProperty(predicate, target, _datatype):
right = target
modifiers.append((cls._reference(predicate, ontology_prefix), right))

inner = ", ".join(f"{key}={value}" for key, value in modifiers)
inner = ", ".join(f"{key}={value}" for key, value in sorted(modifiers))
return "{" + inner + "}"

def _emit_properties(
Expand All @@ -772,7 +773,7 @@ def _emit_object_properties(
def _trailing_modifiers(
self, predicate: Reference, value: Reference, *, ontology_prefix: str
) -> str:
axioms: list[tuple[Reference, Reference] | tuple[Reference, str]] = [
axioms: list[ObjectProperty | LiteralProperty] = [
*self._axioms.get((predicate, value), []),
*self._str_axioms.get((predicate, value), []),
]
Expand Down
26 changes: 20 additions & 6 deletions tests/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from pyobo.struct.reference import unspecified_matching
from pyobo.struct.struct import (
BioregistryError,
LiteralProperty,
ObjectProperty,
SynonymTypeDef,
Term,
TypeDef,
Expand Down Expand Up @@ -584,9 +586,10 @@ def test_default_term(self) -> None:
def test_format_axioms(self) -> None:
"""Test formatting axioms."""
axioms = [
(
ObjectProperty(
mapping_has_justification,
Reference(prefix="semapv", identifier="UnspecifiedMapping"),
None,
),
]
self.assertEqual(
Expand All @@ -595,11 +598,14 @@ def test_format_axioms(self) -> None:
)

axioms = [
(
ObjectProperty(
mapping_has_justification,
Reference(prefix="semapv", identifier="UnspecifiedMapping"),
None,
),
ObjectProperty(
has_contributor, Reference(prefix="orcid", identifier="0000-0003-4423-4370"), None
),
(has_contributor, Reference(prefix="orcid", identifier="0000-0003-4423-4370")),
]
self.assertEqual(
"{dcterms:contributor=orcid:0000-0003-4423-4370, sssom:mapping_justification=semapv:UnspecifiedMapping}",
Expand All @@ -618,13 +624,17 @@ def test_append_exact_match_axioms(self) -> None:
self.assertEqual(
{
(exact_match.reference, target): [
(mapping_has_justification.reference, unspecified_matching)
ObjectProperty(mapping_has_justification.reference, unspecified_matching, None)
]
},
dict(term._axioms),
)
self.assertEqual(
{(exact_match.reference, target): [(mapping_has_confidence.reference, "0.99")]},
{
(exact_match.reference, target): [
LiteralProperty.float(mapping_has_confidence.reference, 0.99)
]
},
dict(term._str_axioms),
)
lines = dedent("""\
Expand Down Expand Up @@ -679,7 +689,11 @@ def test_append_xref_with_axioms(self) -> None:
term = Term(LYSINE_DEHYDROGENASE_ACT)
term.append_xref(target, confidence=0.99)
self.assertEqual(
{(has_dbxref.reference, target): [(mapping_has_confidence.reference, "0.99")]},
{
(has_dbxref.reference, target): [
LiteralProperty.float(mapping_has_confidence.reference, 0.99)
]
},
dict(term._str_axioms),
)
lines = dedent("""\
Expand Down

0 comments on commit e6d1ae3

Please sign in to comment.