Spaces:
Sleeping
Sleeping
Update genesis/api_clients/bioportal_api.py
Browse files
genesis/api_clients/bioportal_api.py
CHANGED
@@ -1,81 +1,78 @@
|
|
1 |
# genesis/api_clients/bioportal_api.py
|
2 |
-
import os
|
3 |
import requests
|
|
|
4 |
from urllib.parse import quote
|
5 |
|
6 |
-
BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY")
|
7 |
BIOPORTAL_BASE_URL = "https://data.bioontology.org"
|
8 |
|
9 |
if not BIOPORTAL_API_KEY:
|
10 |
-
raise ValueError("
|
11 |
-
|
12 |
|
13 |
-
|
14 |
-
"""
|
15 |
-
|
16 |
-
"""
|
17 |
-
headers = {"Authorization": f"apikey token={BIOPORTAL_API_KEY}"}
|
18 |
-
url = f"{BIOPORTAL_BASE_URL}{endpoint}"
|
19 |
-
res = requests.get(url, headers=headers, params=params)
|
20 |
-
res.raise_for_status()
|
21 |
-
return res.json()
|
22 |
|
23 |
|
24 |
-
def
|
25 |
"""
|
26 |
-
Search for biomedical
|
|
|
|
|
|
|
|
|
|
|
27 |
"""
|
28 |
params = {
|
29 |
-
"q":
|
30 |
-
"pagesize":
|
31 |
}
|
32 |
-
if
|
33 |
-
params["
|
34 |
-
return _bioportal_get("/search", params)
|
35 |
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
def get_term_details(ontology_acronym: str, term_id: str):
|
38 |
-
"""
|
39 |
-
Retrieve details for a specific ontology term.
|
40 |
-
"""
|
41 |
-
encoded_term_id = quote(term_id, safe="")
|
42 |
-
return _bioportal_get(f"/ontologies/{ontology_acronym}/classes/{encoded_term_id}")
|
43 |
|
44 |
-
|
45 |
-
def map_to_standard_ontology(term: str, ontologies: list = None):
|
46 |
"""
|
47 |
-
|
48 |
"""
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
"ontology": top_hit.get("links", {}).get("ontology"),
|
55 |
-
"definition": top_hit.get("definition"),
|
56 |
-
"synonyms": top_hit.get("synonym", []),
|
57 |
-
"cui": top_hit.get("@id")
|
58 |
-
}
|
59 |
-
return None
|
60 |
|
61 |
|
62 |
-
def
|
63 |
"""
|
64 |
-
|
65 |
"""
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
return mapped
|
74 |
|
75 |
|
76 |
-
def
|
77 |
"""
|
78 |
-
|
79 |
"""
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# genesis/api_clients/bioportal_api.py
|
|
|
2 |
import requests
|
3 |
+
import os
|
4 |
from urllib.parse import quote
|
5 |
|
6 |
+
BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY") # Store this in Hugging Face secrets
|
7 |
BIOPORTAL_BASE_URL = "https://data.bioontology.org"
|
8 |
|
9 |
if not BIOPORTAL_API_KEY:
|
10 |
+
raise ValueError("BIOPORTAL_API_KEY not found in environment variables.")
|
|
|
11 |
|
12 |
+
HEADERS = {
|
13 |
+
"Authorization": f"apikey token={BIOPORTAL_API_KEY}"
|
14 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
|
17 |
+
def search_concept(term: str, ontology: str = None, max_results: int = 10):
|
18 |
"""
|
19 |
+
Search for a biomedical concept across BioPortal ontologies.
|
20 |
+
|
21 |
+
Args:
|
22 |
+
term (str): The search term (e.g., "BRCA1", "glioblastoma").
|
23 |
+
ontology (str): Optional ontology acronym (e.g., "NCIT", "SNOMEDCT").
|
24 |
+
max_results (int): Maximum number of results.
|
25 |
"""
|
26 |
params = {
|
27 |
+
"q": term,
|
28 |
+
"pagesize": max_results
|
29 |
}
|
30 |
+
if ontology:
|
31 |
+
params["ontology"] = ontology
|
|
|
32 |
|
33 |
+
url = f"{BIOPORTAL_BASE_URL}/search"
|
34 |
+
response = requests.get(url, headers=HEADERS, params=params)
|
35 |
+
response.raise_for_status()
|
36 |
+
return response.json().get("collection", [])
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
def get_concept_details(ontology_acronym: str, concept_id: str):
|
|
|
40 |
"""
|
41 |
+
Fetch detailed information about a concept given ontology acronym and concept ID.
|
42 |
"""
|
43 |
+
encoded_id = quote(concept_id, safe="")
|
44 |
+
url = f"{BIOPORTAL_BASE_URL}/ontologies/{ontology_acronym}/classes/{encoded_id}"
|
45 |
+
response = requests.get(url, headers=HEADERS)
|
46 |
+
response.raise_for_status()
|
47 |
+
return response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
|
50 |
+
def get_ontologies():
|
51 |
"""
|
52 |
+
List all available ontologies in BioPortal.
|
53 |
"""
|
54 |
+
url = f"{BIOPORTAL_BASE_URL}/ontologies"
|
55 |
+
response = requests.get(url, headers=HEADERS)
|
56 |
+
response.raise_for_status()
|
57 |
+
return [
|
58 |
+
{"acronym": o.get("acronym"), "name": o.get("name"), "description": o.get("description")}
|
59 |
+
for o in response.json()
|
60 |
+
]
|
|
|
61 |
|
62 |
|
63 |
+
def map_term_across_ontologies(term: str, ontologies: list = None):
|
64 |
"""
|
65 |
+
Map a term across multiple ontologies to unify synonyms and cross-references.
|
66 |
"""
|
67 |
+
mappings = {}
|
68 |
+
target_ontologies = ontologies or [o["acronym"] for o in get_ontologies()]
|
69 |
+
|
70 |
+
for ont in target_ontologies:
|
71 |
+
results = search_concept(term, ontology=ont, max_results=1)
|
72 |
+
if results:
|
73 |
+
mappings[ont] = {
|
74 |
+
"label": results[0].get("prefLabel"),
|
75 |
+
"uri": results[0].get("@id"),
|
76 |
+
"synonyms": results[0].get("synonym", []),
|
77 |
+
}
|
78 |
+
return mappings
|