Spaces:
Sleeping
Sleeping
Update genesis/api_clients/bioportal_api.py
Browse files
genesis/api_clients/bioportal_api.py
CHANGED
@@ -1,73 +1,89 @@
|
|
1 |
# genesis/api_clients/bioportal_api.py
|
2 |
import os
|
3 |
import requests
|
4 |
-
from typing import
|
5 |
|
6 |
-
BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY")
|
7 |
BIOPORTAL_BASE = "https://data.bioontology.org"
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
}
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
"""
|
15 |
-
Search BioPortal for
|
|
|
16 |
"""
|
17 |
-
params = {
|
18 |
"q": term,
|
19 |
"pagesize": max_results
|
20 |
-
}
|
21 |
-
if
|
22 |
-
params["
|
23 |
-
|
24 |
-
r = requests.get(f"{BIOPORTAL_BASE}/search", params=params
|
25 |
r.raise_for_status()
|
26 |
data = r.json()
|
27 |
-
|
28 |
results = []
|
29 |
-
for
|
30 |
results.append({
|
31 |
-
"
|
32 |
-
"
|
33 |
-
"
|
34 |
-
"
|
35 |
-
"
|
36 |
-
"cui": item.get("cui", None),
|
37 |
-
"semantic_types": item.get("semanticType", [])
|
38 |
})
|
39 |
return results
|
40 |
|
41 |
-
def get_term_details(
|
42 |
"""
|
43 |
-
Get detailed
|
44 |
"""
|
45 |
-
|
46 |
-
r = requests.get(
|
47 |
-
f"{BIOPORTAL_BASE}/ontologies/{ontology_acronym}/classes/{encoded_term_id}",
|
48 |
-
headers=HEADERS
|
49 |
-
)
|
50 |
r.raise_for_status()
|
51 |
return r.json()
|
52 |
|
53 |
-
def
|
54 |
"""
|
55 |
-
|
|
|
56 |
"""
|
57 |
-
params = {
|
58 |
-
|
59 |
-
"include": "mappings"
|
60 |
-
}
|
61 |
-
r = requests.get(f"{BIOPORTAL_BASE}/search", params=params, headers=HEADERS)
|
62 |
r.raise_for_status()
|
63 |
data = r.json()
|
64 |
-
|
65 |
mappings = []
|
66 |
-
for
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
return mappings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# genesis/api_clients/bioportal_api.py
|
2 |
import os
|
3 |
import requests
|
4 |
+
from typing import Dict, List, Optional
|
5 |
|
6 |
+
BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY")
|
7 |
BIOPORTAL_BASE = "https://data.bioontology.org"
|
8 |
|
9 |
+
if not BIOPORTAL_API_KEY:
|
10 |
+
raise EnvironmentError("Missing BIOPORTAL_API_KEY in environment variables.")
|
|
|
11 |
|
12 |
+
# -------------------------
|
13 |
+
# CORE HELPERS
|
14 |
+
# -------------------------
|
15 |
+
def _auth_params(params: Dict) -> Dict:
|
16 |
+
"""Attach BioPortal API key."""
|
17 |
+
params["apikey"] = BIOPORTAL_API_KEY
|
18 |
+
return params
|
19 |
+
|
20 |
+
def search_term(term: str, ontologies: Optional[List[str]] = None, max_results: int = 10) -> List[Dict]:
|
21 |
"""
|
22 |
+
Search BioPortal for ontology terms.
|
23 |
+
`ontologies` is optional list like ["GO", "CHEBI", "NCBITAXON"]
|
24 |
"""
|
25 |
+
params = _auth_params({
|
26 |
"q": term,
|
27 |
"pagesize": max_results
|
28 |
+
})
|
29 |
+
if ontologies:
|
30 |
+
params["ontologies"] = ",".join(ontologies)
|
31 |
+
|
32 |
+
r = requests.get(f"{BIOPORTAL_BASE}/search", params=params)
|
33 |
r.raise_for_status()
|
34 |
data = r.json()
|
35 |
+
|
36 |
results = []
|
37 |
+
for collection in data.get("collection", []):
|
38 |
results.append({
|
39 |
+
"prefLabel": collection.get("prefLabel"),
|
40 |
+
"ontology": collection.get("links", {}).get("ontology"),
|
41 |
+
"definition": collection.get("definition"),
|
42 |
+
"cui": collection.get("cui"),
|
43 |
+
"uri": collection.get("@id")
|
|
|
|
|
44 |
})
|
45 |
return results
|
46 |
|
47 |
+
def get_term_details(term_uri: str) -> Dict:
|
48 |
"""
|
49 |
+
Get detailed metadata for a term given its URI.
|
50 |
"""
|
51 |
+
params = _auth_params({})
|
52 |
+
r = requests.get(term_uri, params=params)
|
|
|
|
|
|
|
53 |
r.raise_for_status()
|
54 |
return r.json()
|
55 |
|
56 |
+
def get_mappings(term_uri: str) -> List[Dict]:
|
57 |
"""
|
58 |
+
Get cross-ontology mappings for a term.
|
59 |
+
Useful for linking to UMLS, NCBI, ChEMBL.
|
60 |
"""
|
61 |
+
params = _auth_params({})
|
62 |
+
r = requests.get(f"{term_uri}/mappings", params=params)
|
|
|
|
|
|
|
63 |
r.raise_for_status()
|
64 |
data = r.json()
|
65 |
+
|
66 |
mappings = []
|
67 |
+
for m in data:
|
68 |
+
mappings.append({
|
69 |
+
"source": m.get("classes", [{}])[0].get("links", {}).get("ontology"),
|
70 |
+
"target": m.get("classes", [{}])[-1].get("links", {}).get("ontology"),
|
71 |
+
"target_uri": m.get("classes", [{}])[-1].get("@id"),
|
72 |
+
"target_label": m.get("classes", [{}])[-1].get("prefLabel")
|
73 |
+
})
|
74 |
return mappings
|
75 |
+
|
76 |
+
def find_related_terms(term: str, ontologies: Optional[List[str]] = None) -> List[Dict]:
|
77 |
+
"""
|
78 |
+
Find related ontology terms using BioPortal search + mappings.
|
79 |
+
"""
|
80 |
+
results = search_term(term, ontologies=ontologies)
|
81 |
+
related = []
|
82 |
+
for r in results:
|
83 |
+
mappings = get_mappings(r["uri"])
|
84 |
+
related.append({
|
85 |
+
"term": r["prefLabel"],
|
86 |
+
"uri": r["uri"],
|
87 |
+
"mappings": mappings
|
88 |
+
})
|
89 |
+
return related
|