Spaces:
Sleeping
Sleeping
Update genesis/api_clients/bioportal_api.py
Browse files
genesis/api_clients/bioportal_api.py
CHANGED
@@ -1,22 +1,33 @@
|
|
1 |
# genesis/api_clients/bioportal_api.py
|
2 |
-
import requests
|
3 |
import os
|
|
|
4 |
|
5 |
BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY")
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
1 |
# genesis/api_clients/bioportal_api.py
|
|
|
2 |
import os
|
3 |
+
import requests
|
4 |
|
5 |
BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY")
|
6 |
+
BIOPORTAL_BASE = "https://data.bioontology.org"
|
7 |
+
|
8 |
+
def search_bioportal(term: str, max_results: int = 10):
|
9 |
+
"""
|
10 |
+
Search BioPortal for ontology terms related to the given term.
|
11 |
+
Returns a list of related concept labels + IDs.
|
12 |
+
"""
|
13 |
+
if not BIOPORTAL_API_KEY:
|
14 |
+
raise ValueError("BIOPORTAL_API_KEY is missing in environment variables")
|
15 |
|
16 |
+
url = f"{BIOPORTAL_BASE}/search"
|
17 |
+
params = {
|
18 |
+
"q": term,
|
19 |
+
"apikey": BIOPORTAL_API_KEY,
|
20 |
+
"pagesize": max_results
|
21 |
+
}
|
22 |
+
res = requests.get(url, params=params)
|
23 |
+
res.raise_for_status()
|
24 |
+
data = res.json()
|
25 |
|
26 |
+
results = []
|
27 |
+
for item in data.get("collection", []):
|
28 |
+
results.append({
|
29 |
+
"label": item.get("prefLabel"),
|
30 |
+
"id": item.get("@id"),
|
31 |
+
"ontology": item.get("links", {}).get("ontology")
|
32 |
+
})
|
33 |
+
return results
|