|
import requests |
|
import os |
|
|
|
def get_umls_api_key(): |
|
return os.getenv("UMLS_API_KEY") |
|
|
|
def get_bioportal_api_key(): |
|
return os.getenv("BIOPORTAL_API_KEY") |
|
|
|
def umls_concept_lookup(term): |
|
key = get_umls_api_key() |
|
if not key: |
|
return [{"error": "UMLS API key not set."}] |
|
url = f"https://uts-ws.nlm.nih.gov/rest/search/current?string={term}&apiKey={key}" |
|
try: |
|
response = requests.get(url, timeout=10) |
|
results = response.json().get("result", {}).get("results", []) |
|
return [ |
|
{ |
|
"name": r.get("name"), |
|
"ui": r.get("ui"), |
|
"rootSource": r.get("rootSource") |
|
} |
|
for r in results[:8] |
|
] |
|
except Exception as e: |
|
return [{"error": f"UMLS error: {e}"}] |
|
|
|
def bioportal_concept_lookup(term): |
|
key = get_bioportal_api_key() |
|
if not key: |
|
return [{"error": "BioPortal API key not set."}] |
|
url = f"https://data.bioontology.org/search?q={term}&apikey={key}" |
|
try: |
|
response = requests.get(url, timeout=10) |
|
matches = response.json().get("collection", []) |
|
return [ |
|
{ |
|
"prefLabel": m.get("prefLabel"), |
|
"ontology": m.get("links", {}).get("ontology"), |
|
"id": m.get("@id") |
|
} |
|
for m in matches[:8] |
|
] |
|
except Exception as e: |
|
return [{"error": f"BioPortal error: {e}"}] |
|
|