Spaces:
Sleeping
Sleeping
Update genesis/api_clients/bioportal_api.py
Browse files
genesis/api_clients/bioportal_api.py
CHANGED
@@ -3,71 +3,71 @@ import os
|
|
3 |
import requests
|
4 |
from typing import List, Dict, Optional
|
5 |
|
6 |
-
#
|
7 |
BIOPORTAL_BASE = "https://data.bioontology.org"
|
8 |
-
BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY") # You must add this to Hugging Face secrets or .env
|
9 |
-
|
10 |
-
if not BIOPORTAL_API_KEY:
|
11 |
-
raise EnvironmentError("BIOPORTAL_API_KEY is missing. Add it to your environment variables or Hugging Face secrets.")
|
12 |
|
13 |
HEADERS = {
|
14 |
"Authorization": f"apikey token={BIOPORTAL_API_KEY}"
|
15 |
}
|
16 |
|
17 |
-
def
|
18 |
"""
|
19 |
-
Search BioPortal for ontology
|
20 |
-
Optionally limit search to specific ontologies like 'GO', 'CHEBI', 'DOID', etc.
|
21 |
"""
|
22 |
params = {
|
23 |
-
"q":
|
24 |
"pagesize": max_results
|
25 |
}
|
26 |
-
if
|
27 |
-
params["
|
28 |
|
29 |
-
r = requests.get(f"{BIOPORTAL_BASE}/search",
|
30 |
r.raise_for_status()
|
31 |
data = r.json()
|
32 |
|
33 |
results = []
|
34 |
for item in data.get("collection", []):
|
35 |
results.append({
|
36 |
-
"
|
37 |
-
"definition": item.get("definition"
|
38 |
"synonyms": item.get("synonym", []),
|
39 |
"ontology": item.get("links", {}).get("ontology"),
|
40 |
-
"
|
41 |
-
"cui": item.get("cui",
|
42 |
-
"
|
43 |
})
|
44 |
return results
|
45 |
|
46 |
-
|
47 |
-
def get_term_details(term_uri: str) -> Dict:
|
48 |
"""
|
49 |
-
|
50 |
"""
|
51 |
-
|
52 |
-
r = requests.get(
|
|
|
|
|
|
|
53 |
r.raise_for_status()
|
54 |
return r.json()
|
55 |
|
56 |
-
|
57 |
-
def find_related_terms(term_uri: str) -> List[Dict]:
|
58 |
"""
|
59 |
-
|
60 |
"""
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
63 |
r.raise_for_status()
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
3 |
import requests
|
4 |
from typing import List, Dict, Optional
|
5 |
|
6 |
+
BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY") # From your Hugging Face secrets or .env
|
7 |
BIOPORTAL_BASE = "https://data.bioontology.org"
|
|
|
|
|
|
|
|
|
8 |
|
9 |
HEADERS = {
|
10 |
"Authorization": f"apikey token={BIOPORTAL_API_KEY}"
|
11 |
}
|
12 |
|
13 |
+
def search_ontology(term: str, ontology: Optional[str] = None, max_results: int = 10) -> List[Dict]:
|
14 |
"""
|
15 |
+
Search BioPortal for a given term, optionally within a specific ontology.
|
|
|
16 |
"""
|
17 |
params = {
|
18 |
+
"q": term,
|
19 |
"pagesize": max_results
|
20 |
}
|
21 |
+
if ontology:
|
22 |
+
params["ontology"] = ontology
|
23 |
|
24 |
+
r = requests.get(f"{BIOPORTAL_BASE}/search", params=params, headers=HEADERS)
|
25 |
r.raise_for_status()
|
26 |
data = r.json()
|
27 |
|
28 |
results = []
|
29 |
for item in data.get("collection", []):
|
30 |
results.append({
|
31 |
+
"label": item.get("prefLabel"),
|
32 |
+
"definition": item.get("definition"),
|
33 |
"synonyms": item.get("synonym", []),
|
34 |
"ontology": item.get("links", {}).get("ontology"),
|
35 |
+
"iri": item.get("@id"),
|
36 |
+
"cui": item.get("cui", None),
|
37 |
+
"semantic_types": item.get("semanticType", [])
|
38 |
})
|
39 |
return results
|
40 |
|
41 |
+
def get_term_details(ontology_acronym: str, term_id: str) -> Dict:
|
|
|
42 |
"""
|
43 |
+
Get detailed information about a specific term in an ontology.
|
44 |
"""
|
45 |
+
encoded_term_id = term_id.replace(":", "%3A").replace("/", "%2F")
|
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 map_to_ontologies(term: str) -> List[Dict]:
|
|
|
54 |
"""
|
55 |
+
Search multiple ontologies for mappings of a term.
|
56 |
"""
|
57 |
+
params = {
|
58 |
+
"q": term,
|
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 item in data.get("collection", []):
|
67 |
+
for mapping in item.get("mappings", []):
|
68 |
+
mappings.append({
|
69 |
+
"source": mapping.get("source"),
|
70 |
+
"target": mapping.get("target"),
|
71 |
+
"relation": mapping.get("relation")
|
72 |
+
})
|
73 |
+
return mappings
|