mgbam's picture
Update genesis/api_clients/bioportal_api.py
67ef408 verified
raw
history blame
966 Bytes
# genesis/api_clients/bioportal_api.py
import os
import requests
BIOPORTAL_API_KEY = os.getenv("BIOPORTAL_API_KEY")
BIOPORTAL_BASE = "https://data.bioontology.org"
def search_bioportal(term: str, max_results: int = 10):
"""
Search BioPortal for ontology terms related to the given term.
Returns a list of related concept labels + IDs.
"""
if not BIOPORTAL_API_KEY:
raise ValueError("BIOPORTAL_API_KEY is missing in environment variables")
url = f"{BIOPORTAL_BASE}/search"
params = {
"q": term,
"apikey": BIOPORTAL_API_KEY,
"pagesize": max_results
}
res = requests.get(url, params=params)
res.raise_for_status()
data = res.json()
results = []
for item in data.get("collection", []):
results.append({
"label": item.get("prefLabel"),
"id": item.get("@id"),
"ontology": item.get("links", {}).get("ontology")
})
return results