Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Jan 6, 2025
1 parent a9935ea commit 622c2bf
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/pyobo/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ def from_obonet(
_data_version=data_version,
_root_terms=root_terms,
terms=terms,
# TODO add subsetdefs
# TODO add extra properties
)


Expand Down
5 changes: 5 additions & 0 deletions src/pyobo/struct/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ class OBOLiteral(NamedTuple):
value: str
datatype: Reference

@classmethod
def string(cls, value: str) -> OBOLiteral:
"""Get a string literal."""
return cls(value, Reference(prefix="xsd", identifier="string"))


AxiomsHint = Mapping[
tuple[Reference, Reference | OBOLiteral], Sequence[tuple[Reference, Reference | OBOLiteral]]
Expand Down
10 changes: 9 additions & 1 deletion src/pyobo/struct/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,8 @@ class Obo:

subsetdefs: ClassVar[list[tuple[Reference, str]] | None] = None

property_values: ClassVar[list[tuple[Reference, Reference | OBOLiteral]] | None] = None

def __post_init__(self):
"""Run post-init checks."""
if self.ontology is None:
Expand Down Expand Up @@ -1135,7 +1137,7 @@ def _iterate_properties(self) -> Iterable[str]:
for predicate, value in self._iterate_property_pairs():
match value:
case OBOLiteral():
end = f"{obo_escape_slim(value.value)} {reference_escape(value.datatype, ontology_prefix=self.ontology)}"
end = f"\"{obo_escape_slim(value.value)}\" {reference_escape(value.datatype, ontology_prefix=self.ontology)}"
case Reference():
end = reference_escape(value, ontology_prefix=self.ontology)
yield f"property_value: {reference_escape(predicate, ontology_prefix=self.ontology)} {end}"
Expand Down Expand Up @@ -1168,6 +1170,10 @@ def _iterate_property_pairs(self) -> Iterable[tuple[Reference, Reference | OBOLi
for root_term in self.root_terms or []:
yield has_ontology_root_term.reference, root_term

# Extras
if self.property_values:
yield from self.property_values

def _index_typedefs(self) -> Mapping[ReferenceTuple, TypeDef]:
return ChainMap(
{t.pair: t for t in self.typedefs or []},
Expand Down Expand Up @@ -2012,6 +2018,7 @@ def make_ad_hoc_ontology(
_idspaces: Mapping[str, str] | None = None,
_root_terms: list[Reference] | None = None,
_subsetdefs: list[tuple[Reference, str]] | None = None,
_property_values: list[tuple[Reference, Reference | OBOLiteral]] | None = None,
*,
terms: list[Term] | None = None,
) -> Obo:
Expand All @@ -2029,6 +2036,7 @@ class AdHocOntology(Obo):
idspaces = _idspaces
root_terms = _root_terms
subsetdefs = _subsetdefs
property_values = _property_values

def __post_init__(self):
self.date = _date
Expand Down
35 changes: 35 additions & 0 deletions tests/test_struct_obo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from textwrap import dedent

from pyobo import default_reference
from pyobo.struct.reference import OBOLiteral
from pyobo.struct.struct import Obo, make_ad_hoc_ontology
from pyobo.struct.typedef import has_license


class TestOBOHeader(unittest.TestCase):
Expand Down Expand Up @@ -148,3 +150,36 @@ def test_18_properties(self) -> None:
""",
ontology,
)

def test_18_properties_bioregistry(self) -> None:
"""Test auto-populating."""
ontology = make_ad_hoc_ontology(
_ontology="go",
)
self.assert_obo_lines(
"""\
format-version: 1.2
ontology: go
property_value: dcterms:title "Gene Ontology" xsd:string
property_value: dcterms:license "CC-BY-4.0" xsd:string
property_value: dcterms:description "The Gene Ontology project provides a controlled vocabulary to describe gene and gene product attributes in any organism." xsd:string
""",
ontology,
)

def test_18_properties_external(self) -> None:
"""Test properties."""
ontology = make_ad_hoc_ontology(
_ontology="xxx",
_property_values=[
(has_license.reference, OBOLiteral.string("CC0")),
],
)
self.assert_obo_lines(
"""\
format-version: 1.2
ontology: xxx
property_value: dcterms:license "CC0" xsd:string
""",
ontology,
)

0 comments on commit 622c2bf

Please sign in to comment.