# 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