Skip to content

Commit e9df5f2

Browse files
authored
Merge pull request #472 from cthoyt/use-curies
Replace `prefixcommons` usages with `curies`
2 parents f7a056a + 4389336 commit e9df5f2

File tree

5 files changed

+50
-61
lines changed

5 files changed

+50
-61
lines changed

kgx/config.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import importlib
2+
from functools import lru_cache
23
from typing import Dict, Any, Optional
34
import sys
45
from os import path
@@ -10,12 +11,13 @@
1011
import json
1112
import logging
1213

14+
import curies
1315
from kgx.graph.base_graph import BaseGraph
1416

1517
config: Optional[Dict[str, Any]] = None
1618
logger: Optional[logging.Logger] = None
1719
graph_store_class: Optional[BaseGraph] = None
18-
jsonld_context_map: Dict = {}
20+
jsonld_context_map: Dict[str, Dict[str, str]] = {}
1921

2022
CONFIG_FILENAME = path.join(path.dirname(path.abspath(__file__)), "config.yml")
2123

@@ -41,6 +43,13 @@ def get_config(filename: str = CONFIG_FILENAME) -> Dict:
4143
return config
4244

4345

46+
@lru_cache
47+
def get_converter(name: str = "biolink") -> curies.Converter:
48+
"""Get contents of a JSON-LD context."""
49+
filepath = config["jsonld-context"][name] # type: ignore
50+
return curies.load_jsonld_context(filepath)
51+
52+
4453
def get_jsonld_context(name: str = "biolink"):
4554
"""
4655
Get contents of a JSON-LD context.
@@ -55,19 +64,8 @@ def get_jsonld_context(name: str = "biolink"):
5564
if name in jsonld_context_map:
5665
content = jsonld_context_map[name]
5766
else:
58-
filepath = config["jsonld-context"][name] # type: ignore
59-
if filepath.startswith("http"):
60-
try:
61-
content = requests.get(filepath).json()
62-
except ConnectionError:
63-
raise Exception(f"Unable to download JSON-LD context from {filepath}")
64-
else:
65-
if path.exists(filepath):
66-
content = json.load(open(filepath))
67-
68-
if "@context" in content:
69-
content = content["@context"]
70-
jsonld_context_map[name] = content
67+
converter = get_converter(name)
68+
jsonld_context_map[name] = converter.bimap
7169
return content
7270

7371

kgx/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ logging:
1818
format: '[%(name)s][%(filename)s][%(funcName)20s] %(levelname)s: %(message)s'
1919

2020
jsonld-context:
21-
biolink: https://raw.githubusercontent.com/biolink/biolink-model/2.2.5/context.jsonld
21+
biolink: https://raw.githubusercontent.com/biolink/biolink-model/v4.0.0/project/jsonld/biolink_model.context.jsonld
2222
monarch_context: https://raw.githubusercontent.com/prefixcommons/biocontext/master/registry/monarch_context.jsonld
23-
obo_context: https://raw.githubusercontent.com/prefixcommons/biocontext/master/registry/obo_context.jsonld
23+
obo_context: https://raw.githubusercontent.com/prefixcommons/biocontext/master/registry/obo_context.jsonld

kgx/prefix_manager.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import re
22
from typing import Dict, Optional
33

4-
import prefixcommons.curie_util as cu
4+
import curies
55
from cachetools import LRUCache, cached
66

77
from kgx.config import get_jsonld_context, get_logger
@@ -33,7 +33,8 @@ def __init__(self, url: str = None):
3333
3434
"""
3535
if url:
36-
context = cu.read_remote_jsonld_context(url)
36+
converter = curies.load_jsonld_context(url)
37+
context = converter.bimap
3738
else:
3839
context = get_jsonld_context()
3940
self.set_prefix_map(context)

kgx/utils/kgx_utils.py

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@
2121
from cachetools import LRUCache
2222
import pandas as pd
2323
import 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+
)
2732
from kgx.graph.base_graph import BaseGraph
2833

2934
curie_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+
223245
def 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

270271
def 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

tests/unit/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import docker
21
import pytest
32
from neo4j import GraphDatabase
43

@@ -17,6 +16,8 @@ def check_container():
1716
is currently running.
1817
"""
1918
try:
19+
import docker
20+
2021
client = docker.from_env()
2122
status = False
2223
try:

0 commit comments

Comments
 (0)