2121from cachetools import LRUCache
2222import pandas as pd
2323import numpy as np
24- from prefixcommons .curie_util import contract_uri
25- from prefixcommons .curie_util import expand_uri
26- from kgx .config import get_logger , get_jsonld_context , get_biolink_model_schema
24+ import curies
25+
26+ from kgx .config import (
27+ get_logger ,
28+ get_jsonld_context ,
29+ get_biolink_model_schema ,
30+ get_converter ,
31+ )
2732from kgx .graph .base_graph import BaseGraph
2833
2934curie_lookup_service = None
@@ -220,12 +225,28 @@ def format_biolink_slots(s: str) -> str:
220225 return f"biolink:{ formatted } "
221226
222227
228+ MONARCH_CONVERTER = get_converter ("monarch_context" )
229+ OBO_CONVERTER = get_converter ("obo_context" )
230+ DEFAULT_CONVERTER = curies .chain ([MONARCH_CONVERTER , OBO_CONVERTER ])
231+
232+
233+ def _resolve_converter (prefix_maps : Optional [List [Dict [str , str ]]] = None ) -> curies .Converter :
234+ if not prefix_maps :
235+ return DEFAULT_CONVERTER
236+ return curies .chain ([
237+ * (
238+ curies .load_prefix_map (prefix_map )
239+ for prefix_map in prefix_maps
240+ ),
241+ DEFAULT_CONVERTER ,
242+ ])
243+
244+
223245def contract (
224- uri : str , prefix_maps : Optional [List [Dict ]] = None , fallback : bool = True
246+ uri : str , prefix_maps : Optional [List [Dict [ str , str ] ]] = None , fallback : bool = True
225247) -> str :
226248 """
227249 Contract a given URI to a CURIE, based on mappings from `prefix_maps`.
228- If no prefix map is provided then will use defaults from prefixcommons-py.
229250
230251 This method will return the URI as the CURIE if there is no mapping found.
231252
@@ -236,39 +257,19 @@ def contract(
236257 prefix_maps: Optional[List[Dict]]
237258 A list of prefix maps to use for mapping
238259 fallback: bool
239- Determines whether to fallback to default prefix mappings, as determined
240- by `prefixcommons.curie_util`, when URI prefix is not found in `prefix_maps`.
260+ Defunct option. New implementation always chains with default prefix maps.
241261
242262 Returns
243263 -------
244264 str
245265 A CURIE corresponding to the URI
246266
247267 """
248- curie = uri
249- default_curie_maps = [
250- get_jsonld_context ("monarch_context" ),
251- get_jsonld_context ("obo_context" ),
252- ]
253- if prefix_maps :
254- curie_list = contract_uri (uri , prefix_maps )
255- if len (curie_list ) == 0 :
256- if fallback :
257- curie_list = contract_uri (uri , default_curie_maps )
258- if curie_list :
259- curie = curie_list [0 ]
260- else :
261- curie = curie_list [0 ]
262- else :
263- curie_list = contract_uri (uri , default_curie_maps )
264- if len (curie_list ) > 0 :
265- curie = curie_list [0 ]
266-
267- return curie
268+ return _resolve_converter (prefix_maps ).compress (uri , passthrough = True )
268269
269270
270271def expand (
271- curie : str , prefix_maps : Optional [List [dict ]] = None , fallback : bool = True
272+ curie : str , prefix_maps : Optional [List [Dict [ str , str ] ]] = None , fallback : bool = True
272273) -> str :
273274 """
274275 Expand a given CURIE to an URI, based on mappings from `prefix_map`.
@@ -282,27 +283,15 @@ def expand(
282283 prefix_maps: Optional[List[dict]]
283284 A list of prefix maps to use for mapping
284285 fallback: bool
285- Determines whether to fallback to default prefix mappings, as determined
286- by `prefixcommons.curie_util`, when CURIE prefix is not found in `prefix_maps`.
286+ Defunct option. New implementation always chains with default prefix maps.
287287
288288 Returns
289289 -------
290290 str
291291 A URI corresponding to the CURIE
292292
293293 """
294- default_curie_maps = [
295- get_jsonld_context ("monarch_context" ),
296- get_jsonld_context ("obo_context" ),
297- ]
298- if prefix_maps :
299- uri = expand_uri (curie , prefix_maps )
300- if uri == curie and fallback :
301- uri = expand_uri (curie , default_curie_maps )
302- else :
303- uri = expand_uri (curie , default_curie_maps )
304-
305- return uri
294+ return _resolve_converter (prefix_maps ).expand (curie , passthrough = True )
306295
307296
308297_default_toolkit = None
0 commit comments