Spaces:
Sleeping
Sleeping
Update genesis/api_clients/bioportal_api.py
Browse files
genesis/api_clients/bioportal_api.py
CHANGED
@@ -5,29 +5,49 @@ import requests
|
|
5 |
BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY")
|
6 |
BIOPORTAL_BASE = "https://data.bioontology.org"
|
7 |
|
8 |
-
|
|
|
|
|
|
|
9 |
"""
|
10 |
-
Search BioPortal for
|
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":
|
19 |
"apikey": BIOPORTAL_API_KEY,
|
20 |
-
"pagesize":
|
21 |
}
|
22 |
res = requests.get(url, params=params)
|
23 |
res.raise_for_status()
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
5 |
BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY")
|
6 |
BIOPORTAL_BASE = "https://data.bioontology.org"
|
7 |
|
8 |
+
if not BIOPORTAL_API_KEY:
|
9 |
+
raise ValueError("Missing BIOPORTAL_API_KEY in environment variables")
|
10 |
+
|
11 |
+
def search_terms(query: str, limit: int = 10):
|
12 |
"""
|
13 |
+
Search BioPortal for a term across all ontologies.
|
|
|
14 |
"""
|
|
|
|
|
|
|
15 |
url = f"{BIOPORTAL_BASE}/search"
|
16 |
params = {
|
17 |
+
"q": query,
|
18 |
"apikey": BIOPORTAL_API_KEY,
|
19 |
+
"pagesize": limit
|
20 |
}
|
21 |
res = requests.get(url, params=params)
|
22 |
res.raise_for_status()
|
23 |
+
return res.json().get("collection", [])
|
24 |
+
|
25 |
+
def get_term_details(ontology_acronym: str, term_id: str):
|
26 |
+
"""
|
27 |
+
Get details about a specific term from a specific ontology.
|
28 |
+
"""
|
29 |
+
url = f"{BIOPORTAL_BASE}/ontologies/{ontology_acronym}/classes/{term_id}"
|
30 |
+
params = {"apikey": BIOPORTAL_API_KEY}
|
31 |
+
res = requests.get(url, params=params)
|
32 |
+
res.raise_for_status()
|
33 |
+
return res.json()
|
34 |
+
|
35 |
+
def list_ontologies():
|
36 |
+
"""
|
37 |
+
List all available ontologies in BioPortal.
|
38 |
+
"""
|
39 |
+
url = f"{BIOPORTAL_BASE}/ontologies"
|
40 |
+
params = {"apikey": BIOPORTAL_API_KEY}
|
41 |
+
res = requests.get(url, params=params)
|
42 |
+
res.raise_for_status()
|
43 |
+
return res.json()
|
44 |
|
45 |
+
def get_ontology_details(acronym: str):
|
46 |
+
"""
|
47 |
+
Get metadata for a specific ontology.
|
48 |
+
"""
|
49 |
+
url = f"{BIOPORTAL_BASE}/ontologies/{acronym}"
|
50 |
+
params = {"apikey": BIOPORTAL_API_KEY}
|
51 |
+
res = requests.get(url, params=params)
|
52 |
+
res.raise_for_status()
|
53 |
+
return res.json()
|