Skip to content

Commit

Permalink
Update struct.py
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Nov 23, 2024
1 parent ca6df16 commit b8d2e4c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
24 changes: 15 additions & 9 deletions src/pyobo/api/hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
from .properties import get_filtered_properties_mapping
from .relations import get_filtered_relations_df
from ..identifier_utils import wrap_norm_prefix
from ..struct import TypeDef, has_member, is_a, part_of
from ..struct import has_member, is_a, part_of
from ..struct.reference import Reference
from ..struct.struct import ReferenceHint, _ensure_ref

__all__ = [
"get_ancestors",
Expand All @@ -32,8 +33,8 @@ def get_hierarchy(
*,
include_part_of: bool = True,
include_has_member: bool = False,
extra_relations: Iterable[TypeDef] | None = None,
properties: Iterable[str] | None = None,
extra_relations: Iterable[ReferenceHint] | None = None,
properties: Iterable[ReferenceHint] | None = None,
use_tqdm: bool = False,
force: bool = False,
force_process: bool = False,
Expand All @@ -58,12 +59,19 @@ def get_hierarchy(
This function thinly wraps :func:`_get_hierarchy_helper` to make it easier to work with the lru_cache mechanism.
"""
extra_relations_ = tuple(
sorted(_ensure_ref(r, ontology_prefix=prefix) for r in extra_relations or [])
)
properties_ = tuple(
sorted(_ensure_ref(prop, ontology_prefix=prefix) for prop in properties or [])
)

return _get_hierarchy_helper(
prefix=prefix,
include_part_of=include_part_of,
include_has_member=include_has_member,
extra_relations=tuple(sorted(extra_relations or [])),
properties=tuple(sorted(properties or [])),
extra_relations=extra_relations_,
properties=properties_,
use_tqdm=use_tqdm,
force=force,
force_process=force_process,
Expand All @@ -77,8 +85,8 @@ def get_hierarchy(
def _get_hierarchy_helper(
prefix: str,
*,
extra_relations: tuple[TypeDef, ...],
properties: tuple[str, ...],
extra_relations: tuple[Reference, ...],
properties: tuple[Reference, ...],
include_part_of: bool,
include_has_member: bool,
use_tqdm: bool,
Expand Down Expand Up @@ -140,8 +148,6 @@ def _get_hierarchy_helper(
rv.add_edge(f"{source_ns}:{source_id}", f"{prefix}:{target_id}", relation="part_of")

for relation in extra_relations:
if not isinstance(relation, TypeDef | Reference):
raise TypeError
relation_df = get_filtered_relations_df(
prefix=prefix,
relation=relation,
Expand Down
7 changes: 5 additions & 2 deletions src/pyobo/api/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
)
from ..getters import get_ontology
from ..identifier_utils import wrap_norm_prefix
from ..struct import TypeDef
from ..struct.reference import Reference
from ..struct.struct import ReferenceHint, _ensure_ref
from ..utils.cache import cached_df
Expand Down Expand Up @@ -102,7 +101,11 @@ def _df_getter() -> pd.DataFrame:

@wrap_norm_prefix
def get_id_multirelations_mapping(
prefix: str, typedef: TypeDef, *, use_tqdm: bool = False, **kwargs: Unpack[GetOntologyKwargs]
prefix: str,
typedef: ReferenceHint,
*,
use_tqdm: bool = False,
**kwargs: Unpack[GetOntologyKwargs],
) -> Mapping[str, list[Reference]]:
"""Get the OBO file and output a synonym dictionary."""
kwargs["version"] = get_version_from_kwargs(prefix, kwargs)
Expand Down
3 changes: 1 addition & 2 deletions src/pyobo/sources/rhea.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from pyobo.api.utils import get_version
from pyobo.struct import Obo, Reference, Term
from pyobo.struct.typedef import (
TypeDef,
enabled_by,
has_bidirectional_reaction,
has_input,
Expand Down Expand Up @@ -208,7 +207,7 @@ def iter_terms(version: str, force: bool = False) -> Iterable[Term]:
)
continue
target_reference = Reference(prefix=xref_prefix, identifier=xref_id)
if isinstance(relation, TypeDef):
if relation is not None:
terms[directional_rhea_id].append_relationship(relation, target_reference)
else:
terms[directional_rhea_id].append_xref(target_reference)
Expand Down

0 comments on commit b8d2e4c

Please sign in to comment.