Skip to content

Commit 2a6418e

Browse files
authored
Add get_primary_reference (#439)
Add `pyobo.get_primary_reference()` function to complement `pyobo.get_primary_curie()`
1 parent fc7e3ae commit 2a6418e

4 files changed

Lines changed: 28 additions & 4 deletions

File tree

src/pyobo/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
get_obsolete,
3838
get_primary_curie,
3939
get_primary_identifier,
40+
get_primary_reference,
4041
get_properties,
4142
get_properties_df,
4243
get_property,
@@ -139,6 +140,7 @@
139140
"get_ontology",
140141
"get_primary_curie",
141142
"get_primary_identifier",
143+
"get_primary_reference",
142144
"get_properties",
143145
"get_properties_df",
144146
"get_property",

src/pyobo/api/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
get_id_to_alts,
66
get_primary_curie,
77
get_primary_identifier,
8+
get_primary_reference,
89
)
910
from .combine import get_literal_mappings_subset
1011
from .edges import get_edges, get_edges_df, get_graph
@@ -105,6 +106,7 @@
105106
"get_ontology",
106107
"get_primary_curie",
107108
"get_primary_identifier",
109+
"get_primary_reference",
108110
"get_priority_curie",
109111
"get_properties",
110112
"get_properties_df",

src/pyobo/api/alts.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"get_id_to_alts",
2121
"get_primary_curie",
2222
"get_primary_identifier",
23+
"get_primary_reference",
2324
]
2425

2526
logger = logging.getLogger(__name__)
@@ -61,13 +62,13 @@ def get_alts_to_id(prefix: str, **kwargs: Unpack[GetOntologyKwargs]) -> Mapping[
6162
}
6263

6364

64-
def get_primary_curie(
65+
def get_primary_reference(
6566
prefix: str | curies.Reference | curies.ReferenceTuple,
6667
identifier: str | None = None,
6768
/,
6869
**kwargs: Unpack[GetOntologyKwargs],
69-
) -> str | None:
70-
"""Get the primary curie for an entity."""
70+
) -> curies.ReferenceTuple | None:
71+
"""Get the primary reference for an entity."""
7172
reference = _get_pi(prefix, identifier)
7273
try:
7374
primary_identifier = get_primary_identifier(reference, **kwargs)
@@ -76,7 +77,20 @@ def get_primary_curie(
7677
raise
7778
# this happens on invalid prefix. maybe revise?
7879
return None
79-
return f"{reference.prefix}:{primary_identifier}"
80+
return curies.ReferenceTuple(reference.prefix, primary_identifier)
81+
82+
83+
def get_primary_curie(
84+
prefix: str | curies.Reference | curies.ReferenceTuple,
85+
identifier: str | None = None,
86+
/,
87+
**kwargs: Unpack[GetOntologyKwargs],
88+
) -> str | None:
89+
"""Get the primary curie for an entity."""
90+
reference = get_primary_reference(prefix, identifier, **kwargs)
91+
if reference is None:
92+
return None
93+
return reference.curie
8094

8195

8296
def get_primary_identifier(

tests/test_api.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
get_name_by_curie,
2020
get_primary_curie,
2121
get_primary_identifier,
22+
get_primary_reference,
2223
)
2324
from pyobo.mocks import get_mock_id_alts_mapping, get_mock_id_name_mapping
2425
from pyobo.ner import get_grounder
@@ -102,6 +103,11 @@ def test_get_primary_by_curie(self, _, __):
102103
primary_curie = get_primary_curie("go:0001071")
103104
self.assertIsNotNone(primary_curie)
104105
self.assertEqual("go:0003700", primary_curie)
106+
107+
primary_reference = get_primary_reference("go:0001071")
108+
self.assertIsNotNone(primary_reference)
109+
self.assertEqual(ReferenceTuple("go", "0003700"), primary_reference)
110+
105111
self.assertIsNone(get_name_by_curie("go:0001071", upgrade_identifier=False))
106112

107113
# if set explicitly to true, then it will do it eagerly

0 commit comments

Comments
 (0)