Spaces:
Sleeping
Sleeping
File size: 2,287 Bytes
5b6700c 325ee75 7bfdf8c 701c750 7bfdf8c 67ef408 7bfdf8c 701c750 5755f5b 701c750 67ef408 701c750 7bfdf8c 67ef408 701c750 7bfdf8c 701c750 f96a85c 7bfdf8c f96a85c 325ee75 f96a85c 701c750 325ee75 701c750 f96a85c 5b6700c 701c750 c37fc6d 701c750 c37fc6d 701c750 f96a85c 7bfdf8c 325ee75 701c750 325ee75 701c750 325ee75 701c750 325ee75 701c750 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# genesis/api_clients/bioportal_api.py
import os
import requests
from typing import List, Dict, Optional
BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY") # From your Hugging Face secrets or .env
BIOPORTAL_BASE = "https://data.bioontology.org"
HEADERS = {
"Authorization": f"apikey token={BIOPORTAL_API_KEY}"
}
def search_ontology(term: str, ontology: Optional[str] = None, max_results: int = 10) -> List[Dict]:
"""
Search BioPortal for a given term, optionally within a specific ontology.
"""
params = {
"q": term,
"pagesize": max_results
}
if ontology:
params["ontology"] = ontology
r = requests.get(f"{BIOPORTAL_BASE}/search", params=params, headers=HEADERS)
r.raise_for_status()
data = r.json()
results = []
for item in data.get("collection", []):
results.append({
"label": item.get("prefLabel"),
"definition": item.get("definition"),
"synonyms": item.get("synonym", []),
"ontology": item.get("links", {}).get("ontology"),
"iri": item.get("@id"),
"cui": item.get("cui", None),
"semantic_types": item.get("semanticType", [])
})
return results
def get_term_details(ontology_acronym: str, term_id: str) -> Dict:
"""
Get detailed information about a specific term in an ontology.
"""
encoded_term_id = term_id.replace(":", "%3A").replace("/", "%2F")
r = requests.get(
f"{BIOPORTAL_BASE}/ontologies/{ontology_acronym}/classes/{encoded_term_id}",
headers=HEADERS
)
r.raise_for_status()
return r.json()
def map_to_ontologies(term: str) -> List[Dict]:
"""
Search multiple ontologies for mappings of a term.
"""
params = {
"q": term,
"include": "mappings"
}
r = requests.get(f"{BIOPORTAL_BASE}/search", params=params, headers=HEADERS)
r.raise_for_status()
data = r.json()
mappings = []
for item in data.get("collection", []):
for mapping in item.get("mappings", []):
mappings.append({
"source": mapping.get("source"),
"target": mapping.get("target"),
"relation": mapping.get("relation")
})
return mappings
|