Skip to content

Commit

Permalink
Add remaining annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Jan 16, 2025
1 parent 89079a4 commit f82b9dd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
14 changes: 10 additions & 4 deletions src/pyobo/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,15 +587,21 @@ def _handle_xref(
annotations = [Annotation(v.has_dbxref, p) for p in provenance]

if macro_config is not None:
# TODO handle annotations here
if xref.prefix in macro_config.treat_xrefs_as_equivalent:
return term.append_equivalent(xref)
return term.append_equivalent(xref, annotations=annotations)
elif object_property := macro_config.treat_xrefs_as_genus_differentia.get(xref.prefix):
# TODO how to add annotations here?
if annotations:
logger.warning(
"[%s] unable to add provenance to xref upgraded to intersection_of: %s",
term.reference.curie,
xref,
)
return term.append_intersection_of(xref).append_intersection_of(object_property)
elif predicate := macro_config.treat_xrefs_as_relationship.get(xref.prefix):
return term.append_relationship(predicate, xref)
return term.append_relationship(predicate, xref, annotations=annotations)
elif xref.prefix in macro_config.treat_xrefs_as_is_a:
return term.append_parent(xref)
return term.append_parent(xref, annotations=annotations)

# TODO this is not what spec calls for, maybe
# need a flag in macro config for this
Expand Down
34 changes: 27 additions & 7 deletions src/pyobo/struct/struct_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ def _property_resolve(
"TypeDef": v.subproperty_of,
}

stanza_type_to_eq_prop: dict[StanzaType, Reference] = {
"Term": v.equivalent_class,
"Instance": v.owl_same_as,
"TypeDef": v.equivalent_property,
}


class Stanza:
"""A high-level class for stanzas."""
Expand Down Expand Up @@ -172,11 +178,13 @@ def _append_annotation(
def append_equivalent(
self,
reference: ReferenceHint,
*,
annotations: Iterable[Annotation] | None = None,
) -> Self:
"""Append an equivalent class axiom."""
reference = _ensure_ref(reference)
self.append_relationship(v.equivalent_class, reference)
return self
return self.append_relationship(
stanza_type_to_eq_prop[self.type], reference, annotations=annotations
)

def append_xref(
self,
Expand Down Expand Up @@ -216,18 +224,26 @@ def _prepare_mapping_annotations(
if confidence is not None:
yield Annotation.float(v.mapping_has_confidence, confidence)

def append_parent(self, reference: ReferenceHint) -> Self:
def append_parent(
self,
reference: ReferenceHint,
*,
annotations: Iterable[Annotation] | None = None,
) -> Self:
"""Add a parent to this entity."""
reference = _ensure_ref(reference)
if reference not in self.parents:
self.parents.append(reference)
self._extend_annotations(stanza_type_to_prop[self.type], reference, annotations)
return self

def append_intersection_of(
self,
/,
reference: ReferenceHint | tuple[ReferenceHint, ReferenceHint],
r2: ReferenceHint | None = None,
*,
annotations: Iterable[Annotation] | None = None,
) -> Self:
"""Append an intersection of."""
if r2 is not None:
Expand All @@ -245,9 +261,13 @@ def append_union_of(self, reference: ReferenceHint) -> Self:
self.union_of.append(_ensure_ref(reference))
return self

def append_equivalent_to(self, reference: ReferenceHint) -> Self:
def append_equivalent_to(
self, reference: ReferenceHint, *, annotations: Iterable[Annotation] | None = None
) -> Self:
"""Append to the "equivalent to" list."""
self.equivalent_to.append(_ensure_ref(reference))
reference = _ensure_ref(reference)
self.equivalent_to.append(reference)
self._extend_annotations(stanza_type_to_eq_prop[self.type], reference, annotations)
return self

def _iterate_intersection_of_obo(self, *, ontology_prefix: str) -> Iterable[str]:
Expand Down Expand Up @@ -551,7 +571,7 @@ def _iter_edges(self) -> Iterable[tuple[Reference, Reference]]:
yield from self._iter_parents()
yield from self._iter_intersections()
for equivalent_to in self.equivalent_to:
yield v.equivalent_class, equivalent_to
yield stanza_type_to_eq_prop[self.type], equivalent_to

# The following are "annotation" properties
for subset in self.subsets:
Expand Down

0 comments on commit f82b9dd

Please sign in to comment.