File size: 1,435 Bytes
c85a395
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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}"}]