Skip to content

Commit

Permalink
Improve root term handling (#266)
Browse files Browse the repository at this point in the history
1. Fix bug to output correctly normalized "default" references
2. Add reading for property values with custom handling for root terms
3. Still TODO - handle reading "default" references
  • Loading branch information
cthoyt authored Jan 1, 2025
1 parent 8e5d739 commit a036f9c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/pyobo/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
)
from .struct.reference import _parse_identifier
from .struct.struct import DEFAULT_SYNONYM_TYPE, LiteralProperty, ObjectProperty
from .struct.typedef import default_typedefs
from .struct.typedef import default_typedefs, has_ontology_root_term
from .utils.misc import STATIC_VERSION_REWRITES, cleanup_version

__all__ = [
Expand Down Expand Up @@ -125,6 +125,21 @@ def from_obonet(
f"[{ontology_prefix}] slashes not allowed in data versions because of filesystem usage: {data_version}"
)

root_terms: list[Reference] = []
for t in iterate_node_properties(
graph.graph,
ontology_prefix=ontology_prefix,
upgrade=upgrade,
node=Reference(prefix="obo", identifier=ontology_prefix),
):
# TODO other custom handling can be put here
match t:
case LiteralProperty(predicate, value, datatype):
pass
case ObjectProperty(predicate, obj, _):
if predicate.pair == has_ontology_root_term.pair:
root_terms.append(obj)

#: CURIEs to typedefs
typedefs: Mapping[ReferenceTuple, TypeDef] = {
typedef.pair: typedef
Expand Down Expand Up @@ -241,7 +256,7 @@ def from_obonet(
match t:
case LiteralProperty(predicate, value, datatype):
term.annotate_literal(predicate, value, datatype)
case ObjectProperty(predicate, obj, _datatype):
case ObjectProperty(predicate, obj, _):
term.annotate_object(predicate, obj)
terms.append(term)

Expand All @@ -260,6 +275,7 @@ def from_obonet(
_synonym_typedefs=list(synonym_typedefs.values()),
_date=date,
_data_version=data_version,
_root_terms=root_terms,
terms=terms,
)

Expand Down
2 changes: 1 addition & 1 deletion src/pyobo/struct/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@ def iterate_obo_lines(
yield f'property_value: http://purl.org/dc/terms/description "{description}" xsd:string'

for root_term in self.root_terms or []:
yield f"property_value: {has_ontology_root_term.preferred_curie} {root_term.preferred_curie}"
yield f"property_value: {has_ontology_root_term.preferred_curie} {reference_escape(root_term, ontology_prefix=self.ontology)}"

for typedef in sorted(self.typedefs or []):
yield from typedef.iterate_obo_lines(ontology_prefix=self.ontology)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,21 @@ def test_sssom_axiom(self) -> None:
self.assertIsNotNone(context.contributor)
self.assertEqual("0000-0003-4423-4370", context.contributor.identifier)

def test_root(self) -> None:
"""Test root terms."""
ontology = _read("""\
ontology: go
property_value: IAO:0000700 GO:0050069
[Term]
id: GO:0050069
""")
# FIXME support default reference, like property_value: IAO:0000700 adhoc
self.assertEqual(
[Reference(prefix="GO", identifier="0050069")],
ontology.root_terms,
)


class TestVersionHandling(unittest.TestCase):
"""Test version handling."""
Expand Down

0 comments on commit a036f9c

Please sign in to comment.