Skip to content

Commit 2d685e2

Browse files
committed
Up
1 parent ebbe759 commit 2d685e2

3 files changed

Lines changed: 52 additions & 14 deletions

File tree

src/bioregistry/resource_manager.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
record_accumulator,
4949
sanitize_model,
5050
)
51+
from .schema.struct import OlsVersion
5152
from .schema_utils import (
5253
_collections_from_path,
5354
_contexts_from_path,
@@ -1559,21 +1560,17 @@ def get_bioportal_iri(self, prefix: str, identifier: str) -> str | None:
15591560
>>> manager.get_bioportal_iri("chebi", "24431")
15601561
'https://bioportal.bioontology.org/ontologies/CHEBI/?p=classes&conceptid=http://purl.obolibrary.org/obo/CHEBI_24431'
15611562
"""
1562-
bioportal_prefix = self.get_mapped_prefix(prefix, "bioportal")
1563-
if bioportal_prefix is None:
1563+
resource = self.get_resource(prefix)
1564+
if resource is None:
15641565
return None
1565-
obo_link = self.get_obofoundry_iri(prefix, identifier)
1566-
if obo_link is not None:
1567-
return f"https://bioportal.bioontology.org/ontologies/{bioportal_prefix}/?p=classes&conceptid={obo_link}"
1568-
return None
1566+
return resource.get_bioportal_iri(identifier)
15691567

1570-
def get_ols_iri(self, prefix: str, identifier: str) -> str | None:
1568+
def get_ols_iri(self, prefix: str, identifier: str, *, version: OlsVersion = "3") -> str | None:
15711569
"""Get the OLS URL if possible."""
1572-
ols_prefix = self.get_mapped_prefix(prefix, "ols")
1573-
obo_iri = self.get_obofoundry_iri(prefix, identifier)
1574-
if ols_prefix is None or obo_iri is None:
1570+
resource = self.get_resource(prefix)
1571+
if resource is None:
15751572
return None
1576-
return f"https://www.ebi.ac.uk/ols4/ontologies/{ols_prefix}/terms?iri={obo_iri}"
1573+
return resource.get_ols_iri(identifier, version=version)
15771574

15781575
def get_formatted_iri(self, metaprefix: str, prefix: str, identifier: str) -> str | None:
15791576
"""Get an IRI using the format in the metaregistry.

src/bioregistry/schema/struct.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@
9191
"and IRI specification (https://www.ietf.org/rfc/rfc3987.txt) for more information."
9292
)
9393

94+
OlsVersion: TypeAlias = Literal["3", "4"]
95+
9496
X = TypeVar("X")
9597

9698
#: A controlled vocabulary of domains.
@@ -1953,6 +1955,27 @@ def get_ols_uri_format(self) -> str | None:
19531955
return None
19541956
return f"{ols_url_prefix}$1"
19551957

1958+
def get_ols_iri(self, identifier: str, *, version: OlsVersion = "3") -> str | None:
1959+
"""Get the OLS URL for the given local unique identifier, if possible."""
1960+
ols_prefix = self.get_ols_prefix()
1961+
if ols_prefix is None:
1962+
return None
1963+
if rdf_uri := self.get_rdf_uri(identifier):
1964+
ols_version = "ols4" if version == "4" else "ols"
1965+
return (
1966+
f"https://www.ebi.ac.uk/{ols_version}/ontologies/{ols_prefix}/terms?iri={rdf_uri}"
1967+
)
1968+
return None
1969+
1970+
def get_bioportal_iri(self, identifier: str) -> str | None:
1971+
"""Get the Bioportal URL for the given local unique identifier, if possible."""
1972+
bioportal_prefix = self.get_mapped_prefix("bioportal")
1973+
if bioportal_prefix is None:
1974+
return None
1975+
if rdf_uri := self.get_rdf_uri(identifier):
1976+
return f"https://bioportal.bioontology.org/ontologies/{bioportal_prefix}/?p=classes&conceptid={rdf_uri}"
1977+
return None
1978+
19561979
def get_rrid_uri_format(self) -> str | None:
19571980
"""Get the RRID URI format.
19581981

tests/test_struct/test_struct.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,47 @@ def test_get_ols_uri_prefix(self) -> None:
3636
resource = Resource(
3737
prefix=internal_prefix,
3838
mappings={"ols": external_prefix},
39-
ols={"prefix": external_prefix},
4039
rdf_uri_format=rdf_uri_format,
4140
)
4241
self.assertEqual(external_prefix, resource.get_ols_prefix())
4342
self.assertEqual(
4443
f"https://www.ebi.ac.uk/ols/ontologies/{external_prefix}/terms?iri={rdf_uri_prefix}",
4544
resource.get_ols_uri_prefix(),
4645
)
46+
self.assertEqual(
47+
f"https://www.ebi.ac.uk/ols/ontologies/{external_prefix}/terms?iri={rdf_uri_prefix}0000001",
48+
resource.get_ols_iri("0000001"),
49+
)
4750

4851
# test shortcut for obofoundry
4952
resource = Resource(
5053
prefix=internal_prefix,
5154
mappings={"ols": external_prefix, "obofoundry": external_prefix},
52-
ols={"prefix": external_prefix},
5355
obofoundry={"prefix": external_prefix},
5456
)
5557
self.assertEqual(external_prefix, resource.get_ols_prefix())
56-
self.assertEqual(external_prefix, resource.get_ols_prefix())
5758
self.assertEqual(
5859
f"https://www.ebi.ac.uk/ols/ontologies/{external_prefix}/terms?iri=http://purl.obolibrary.org/obo/{external_prefix.upper()}_",
5960
resource.get_ols_uri_prefix(),
6061
)
6162

63+
def test_get_bioportal(self) -> None:
64+
"""Test getting OLS URI prefix."""
65+
internal_prefix = "test"
66+
external_prefix = "tset"
67+
rdf_uri_prefix = "https://example.com/test/"
68+
rdf_uri_format = f"{rdf_uri_prefix}$1"
69+
70+
resource = Resource(
71+
prefix=internal_prefix,
72+
mappings={"bioportal": external_prefix},
73+
rdf_uri_format=rdf_uri_format,
74+
)
75+
self.assertEqual(
76+
f"https://bioportal.bioontology.org/ontologies/{external_prefix}/?p=classes&conceptid={rdf_uri_prefix}0000001",
77+
resource.get_bioportal_iri("0000001"),
78+
)
79+
6280
def test_record_accumulator(self) -> None:
6381
"""Test record accumulator."""
6482
resource = Resource(prefix="test")

0 commit comments

Comments
 (0)