Skip to content

Commit

Permalink
Rename function
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Nov 25, 2024
1 parent e95ead4 commit c6c09c9
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/pyobo/api/alts.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def get_primary_curie(
**kwargs: Unpack[GetOntologyKwargs],
) -> str | None:
"""Get the primary curie for an entity."""
reference = Reference.from_curie(curie, strict=kwargs.get("strict", True))
reference = Reference.from_curie_or_uri(curie, strict=kwargs.get("strict", True))
if reference is None:
return None
primary_identifier = get_primary_identifier(reference, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion src/pyobo/cli/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def relations(
else:
echo_df(relations_df)
else:
relation_reference = Reference.from_curie(relation, strict=False)
relation_reference = Reference.from_curie_or_uri(relation, strict=False)
if relation_reference is None:
click.secho(f"not a valid curie: {relation}", fg="red")
raise sys.exit(1)
Expand Down
41 changes: 28 additions & 13 deletions src/pyobo/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ def from_obonet(graph: nx.MultiDiGraph, *, strict: bool = True) -> Obo:

node_xrefs = list(
iterate_node_xrefs(
prefix=prefix, data=data, strict=strict, ontology_prefix=ontology_prefix
prefix=prefix,
data=data,
strict=strict,
ontology_prefix=ontology_prefix,
node=reference,
)
)
xrefs, provenance = [], []
Expand Down Expand Up @@ -322,7 +326,7 @@ def iterate_graph_synonym_typedefs(
# assume it's a default reference
yield SynonymTypeDef(reference=default_reference(ontology_prefix, sid, name=name))
else:
reference = Reference.from_curie(
reference = Reference.from_curie_or_uri(
sid, name=name, strict=strict, ontology_prefix=ontology_prefix
)
if reference is not None:
Expand Down Expand Up @@ -350,7 +354,7 @@ def iterate_graph_typedefs(
logger.debug("[%s] typedef %s is missing a name", graph.graph["ontology"], curie)

if ":" in curie:
reference = Reference.from_curie(
reference = Reference.from_curie_or_uri(
curie, name=name, strict=strict, ontology_prefix=ontology_prefix
)
else:
Expand All @@ -361,7 +365,9 @@ def iterate_graph_typedefs(

xrefs = []
for curie in typedef.get("xref", []):
_xref = Reference.from_curie(curie, strict=strict, ontology_prefix=ontology_prefix)
_xref = Reference.from_curie_or_uri(
curie, strict=strict, ontology_prefix=ontology_prefix
)
if _xref:
xrefs.append(_xref)
yield TypeDef(reference=reference, xrefs=xrefs)
Expand Down Expand Up @@ -498,7 +504,7 @@ def _parse_trailing_ref_list(
):
rest = rest.lstrip("[").rstrip("]")
return [
Reference.from_curie(
Reference.from_curie_or_uri(
curie.strip(), strict=strict, node=node, ontology_prefix=ontology_prefix
)
for curie in rest.split(",")
Expand Down Expand Up @@ -564,7 +570,7 @@ def _handle_prop(
# if the value doesn't start with a quote, we're going to
# assume that it's a reference
if not value_type.startswith('"'):
obj_reference = Reference.from_curie(
obj_reference = Reference.from_curie_or_uri(
value_type, strict=strict, ontology_prefix=ontology_prefix, node=node
)
if obj_reference is None:
Expand All @@ -589,7 +595,7 @@ def _handle_prop(
if not datatype:
return LiteralProperty(prop_reference, value, Reference(prefix="xsd", identifier="string"))

datatype_reference = Reference.from_curie(
datatype_reference = Reference.from_curie_or_uri(
datatype, strict=strict, ontology_prefix=ontology_prefix, node=node
)
if datatype_reference is None:
Expand Down Expand Up @@ -617,7 +623,9 @@ def _get_prop(
elif ":" not in prop:
return default_reference(ontology_prefix, prop)
else:
return Reference.from_curie(prop, strict=strict, node=node, ontology_prefix=ontology_prefix)
return Reference.from_curie_or_uri(
prop, strict=strict, node=node, ontology_prefix=ontology_prefix
)


def iterate_node_parents(
Expand All @@ -629,7 +637,7 @@ def iterate_node_parents(
) -> Iterable[Reference]:
"""Extract parents from a :mod:`obonet` node's data."""
for parent_curie in data.get("is_a", []):
reference = Reference.from_curie(
reference = Reference.from_curie_or_uri(
parent_curie, strict=strict, ontology_prefix=ontology_prefix, node=node
)
if reference is None:
Expand All @@ -643,7 +651,7 @@ def iterate_node_alt_ids(
) -> Iterable[Reference]:
"""Extract alternate identifiers from a :mod:`obonet` node's data."""
for curie in data.get("alt_id", []):
reference = Reference.from_curie(
reference = Reference.from_curie_or_uri(
curie, strict=strict, node=node, ontology_prefix=ontology_prefix
)
if reference is not None:
Expand Down Expand Up @@ -678,7 +686,7 @@ def iterate_node_relationships(
relation.curie,
)

target = Reference.from_curie(
target = Reference.from_curie_or_uri(
target_curie, strict=strict, ontology_prefix=ontology_prefix, node=node
)
if target is None:
Expand All @@ -689,7 +697,12 @@ def iterate_node_relationships(


def iterate_node_xrefs(
*, prefix: str, data: Mapping[str, Any], strict: bool = True, ontology_prefix: str | None
*,
prefix: str,
data: Mapping[str, Any],
strict: bool = True,
ontology_prefix: str | None,
node: Reference,
) -> Iterable[Reference]:
"""Extract xrefs from a :mod:`obonet` node's data."""
for xref in data.get("xref", []):
Expand All @@ -708,6 +721,8 @@ def iterate_node_xrefs(
continue
xref = _xref_split[0]

yv = Reference.from_curie(xref, strict=strict, ontology_prefix=ontology_prefix)
yv = Reference.from_curie_or_uri(
xref, strict=strict, ontology_prefix=ontology_prefix, node=node
)
if yv is not None:
yield yv
2 changes: 1 addition & 1 deletion src/pyobo/sources/complexportal.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def _parse_xrefs(s) -> list[tuple[Reference, str]]:
xref_curie = _clean_intenz(xref_curie)

try:
reference = Reference.from_curie(xref_curie)
reference = Reference.from_curie_or_uri(xref_curie)
except ValueError:
logger.warning("can not parse CURIE: %s", xref_curie)
continue
Expand Down
4 changes: 2 additions & 2 deletions src/pyobo/sources/conso.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def iter_terms() -> Iterable[Term]:

synonyms_df = ensure_df(PREFIX, url=SYNONYMS_URL)
synonyms_df["reference"] = synonyms_df["reference"].map(
lambda s: [Reference.from_curie(s)] if pd.notna(s) and s != "?" else [],
lambda s: [Reference.from_curie_or_uri(s)] if pd.notna(s) and s != "?" else [],
)
synonyms_df["specificity"] = synonyms_df["specificity"].map(
lambda s: "EXACT" if pd.isna(s) or s == "?" else s
Expand Down Expand Up @@ -71,7 +71,7 @@ def iter_terms() -> Iterable[Term]:
curie = curie.strip()
if not curie:
continue
reference = Reference.from_curie(curie)
reference = Reference.from_curie_or_uri(curie)
if reference is not None:
provenance.append(reference)
identifier = row["Identifier"]
Expand Down
2 changes: 1 addition & 1 deletion src/pyobo/sources/famplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def get_terms(version: str, force: bool = False) -> Iterable[Term]:
reference = Reference(prefix=PREFIX, identifier=entity, name=entity)
definition, provenance = id_to_definition.get(entity, (None, None))
provenance_reference = (
Reference.from_curie(provenance) if isinstance(provenance, str) else None
Reference.from_curie_or_uri(provenance) if isinstance(provenance, str) else None
)
term = Term(
reference=reference,
Expand Down
2 changes: 1 addition & 1 deletion src/pyobo/sources/flybase.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def get_terms(version: str, force: bool = False) -> Iterable[Term]:
for hgnc_curie in human_orthologs.get(identifier, []):
if not hgnc_curie or pd.isna(hgnc_curie):
continue
hgnc_ortholog = Reference.from_curie(hgnc_curie)
hgnc_ortholog = Reference.from_curie_or_uri(hgnc_curie)
if hgnc_ortholog is None:
tqdm.write(f"[{PREFIX}] {identifier} had invalid ortholog: {hgnc_curie}")
else:
Expand Down
4 changes: 2 additions & 2 deletions src/pyobo/sources/uniprot/uniprot.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def iter_terms(version: str | None = None) -> Iterable[Term]:
# FIXME this needs a different relation than enables
# see https://github.com/biopragmatics/pyobo/pull/168#issuecomment-1918680152
enables,
cast(Reference, Reference.from_curie(rhea_curie, strict=True)),
cast(Reference, Reference.from_curie_or_uri(rhea_curie, strict=True)),
)

if bindings:
Expand All @@ -136,7 +136,7 @@ def iter_terms(version: str | None = None) -> Iterable[Term]:
if part.startswith("/ligand_id"):
curie = part.removeprefix('/ligand_id="').rstrip('"')
binding_references.add(
cast(Reference, Reference.from_curie(curie, strict=True))
cast(Reference, Reference.from_curie_or_uri(curie, strict=True))
)
for binding_reference in sorted(binding_references):
term.annotate_object(molecularly_interacts_with, binding_reference)
Expand Down
2 changes: 1 addition & 1 deletion src/pyobo/sources/uniprot/uniprot_ptm.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def _parse(i, lines: Iterable[tuple[str, str]]) -> Term | None:
if line.startswith(y):
line = x + line[len(y) :]

ref = Reference.from_curie(line.replace("; ", ":"))
ref = Reference.from_curie_or_uri(line.replace("; ", ":"))
if ref:
term.append_xref(ref)
else:
Expand Down
2 changes: 1 addition & 1 deletion src/pyobo/sources/zfin.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def get_terms(force: bool = False, version: str | None = None) -> Iterable[Term]
for hgnc_id in human_orthologs.get(identifier, []):
term.append_relationship(orthologous, Reference(prefix="hgnc", identifier=hgnc_id))
for mgi_curie in mouse_orthologs.get(identifier, []):
mouse_ortholog = Reference.from_curie(mgi_curie)
mouse_ortholog = Reference.from_curie_or_uri(mgi_curie)
if mouse_ortholog:
term.append_relationship(orthologous, mouse_ortholog)
for flybase_id in fly_orthologs.get(identifier, []):
Expand Down
2 changes: 1 addition & 1 deletion src/pyobo/struct/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def bioregistry_link(self) -> str:
return f"https://bioregistry.io/{self.curie}"

@classmethod
def from_curie( # type:ignore[override]
def from_curie_or_uri(
cls,
curie: str,
name: str | None = None,
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 @@ -190,7 +190,7 @@ def _ensure_ref(
if not ontology_prefix:
raise ValueError(f"can't parse reference: {reference}")
return default_reference(ontology_prefix, reference)
_rv = Reference.from_curie(reference, strict=True, ontology_prefix=ontology_prefix)
_rv = Reference.from_curie_or_uri(reference, strict=True, ontology_prefix=ontology_prefix)
if _rv is None:
raise ValueError(f"[{ontology_prefix}] unable to parse {reference}")
return _rv
Expand Down
7 changes: 4 additions & 3 deletions tests/test_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ def test_typedefs(self):

def test_iter_filtered_relations(self):
"""Test getting filtered relations w/ upgrade."""
term_reference = Reference(prefix="chebi", identifier="17051")
reference = default_reference("chebi", "is_conjugate_base_of")
object_reference = Reference(prefix="chebi", identifier="29228")
for inp in [
reference.curie,
reference,
Expand All @@ -303,6 +305,5 @@ def test_iter_filtered_relations(self):
for term, target in self.ontology.iterate_filtered_relations(inp)
)
self.assertNotEqual(0, len(rr))
term = Reference.from_curie("chebi:17051")
self.assertIn(term, rr)
self.assertIn(Reference.from_curie("chebi:29228"), rr[term])
self.assertIn(term_reference, rr)
self.assertIn(object_reference, rr[term_reference])
2 changes: 1 addition & 1 deletion tests/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
LYSINE_DEHYDROGENASE_ACT = Reference(
prefix="GO", identifier="0050069", name="lysine dehydrogenase activity"
)
RO_DUMMY = TypeDef(reference=Reference.from_curie("RO:1234567"))
RO_DUMMY = TypeDef(reference=Reference(prefix="RO", identifier="1234567"))
CHARLIE = Reference(prefix="orcid", identifier="0000-0003-4423-4370")


Expand Down

0 comments on commit c6c09c9

Please sign in to comment.