Spaces:
Sleeping
Sleeping
Update genesis/api_clients/umls_api.py
Browse files- genesis/api_clients/umls_api.py +73 -52
genesis/api_clients/umls_api.py
CHANGED
@@ -1,73 +1,94 @@
|
|
1 |
# genesis/api_clients/umls_api.py
|
2 |
-
import os
|
3 |
import requests
|
|
|
|
|
4 |
|
5 |
-
UMLS_API_KEY = os.getenv("UMLS_API_KEY")
|
6 |
-
|
7 |
-
UMLS_API_URL = "https://uts-ws.nlm.nih.gov/rest"
|
8 |
|
9 |
if not UMLS_API_KEY:
|
10 |
-
raise ValueError("
|
11 |
|
12 |
-
|
|
|
13 |
"""
|
14 |
-
|
15 |
"""
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
return res.text.split('action="')[1].split('"')[0]
|
22 |
|
23 |
-
def
|
24 |
"""
|
25 |
-
|
26 |
"""
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
|
|
|
33 |
"""
|
34 |
-
|
35 |
"""
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
"pageNumber": page,
|
43 |
-
"pageSize": page_size
|
44 |
-
}
|
45 |
-
res = requests.get(url, params=params)
|
46 |
-
res.raise_for_status()
|
47 |
-
return res.json().get("result", {}).get("results", [])
|
48 |
|
49 |
-
def
|
50 |
"""
|
51 |
-
Get
|
52 |
"""
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
res.raise_for_status()
|
59 |
-
return res.json()
|
60 |
|
61 |
-
|
|
|
62 |
"""
|
63 |
-
|
64 |
"""
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
}
|
72 |
-
|
73 |
-
]
|
|
|
1 |
# genesis/api_clients/umls_api.py
|
|
|
2 |
import requests
|
3 |
+
import os
|
4 |
+
from urllib.parse import quote
|
5 |
|
6 |
+
UMLS_API_KEY = os.getenv("UMLS_API_KEY") # Store in Hugging Face secrets
|
7 |
+
UMLS_BASE_URL = "https://uts-ws.nlm.nih.gov/rest"
|
|
|
8 |
|
9 |
if not UMLS_API_KEY:
|
10 |
+
raise ValueError("UMLS_API_KEY not found in environment variables.")
|
11 |
|
12 |
+
# Get UMLS authentication ticket
|
13 |
+
def get_umls_ticket():
|
14 |
"""
|
15 |
+
Retrieve a UMLS authentication ticket.
|
16 |
"""
|
17 |
+
auth_url = f"https://utslogin.nlm.nih.gov/cas/v1/api-key"
|
18 |
+
response = requests.post(auth_url, data={"apikey": UMLS_API_KEY})
|
19 |
+
response.raise_for_status()
|
20 |
+
return response.text.split("<form")[0].strip()
|
21 |
+
|
|
|
22 |
|
23 |
+
def search_umls(term: str, page_size: int = 10):
|
24 |
"""
|
25 |
+
Search UMLS for a term (CUI, concept name, or synonym).
|
26 |
"""
|
27 |
+
ticket = get_umls_ticket()
|
28 |
+
encoded_term = quote(term)
|
29 |
+
url = f"{UMLS_BASE_URL}/search/current?string={encoded_term}&ticket={ticket}&pageSize={page_size}"
|
30 |
+
response = requests.get(url)
|
31 |
+
response.raise_for_status()
|
32 |
+
results = response.json().get("result", {}).get("results", [])
|
33 |
+
return [
|
34 |
+
{
|
35 |
+
"ui": r.get("ui"),
|
36 |
+
"name": r.get("name"),
|
37 |
+
"rootSource": r.get("rootSource"),
|
38 |
+
}
|
39 |
+
for r in results
|
40 |
+
if r.get("ui") != "NONE"
|
41 |
+
]
|
42 |
|
43 |
+
|
44 |
+
def get_concept_details(cui: str):
|
45 |
"""
|
46 |
+
Retrieve details for a specific UMLS concept (CUI).
|
47 |
"""
|
48 |
+
ticket = get_umls_ticket()
|
49 |
+
url = f"{UMLS_BASE_URL}/content/current/CUI/{cui}?ticket={ticket}"
|
50 |
+
response = requests.get(url)
|
51 |
+
response.raise_for_status()
|
52 |
+
return response.json().get("result", {})
|
53 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
def get_concept_synonyms(cui: str):
|
56 |
"""
|
57 |
+
Get synonyms for a given UMLS concept.
|
58 |
"""
|
59 |
+
concept = get_concept_details(cui)
|
60 |
+
synonyms = []
|
61 |
+
for atom in concept.get("atoms", []):
|
62 |
+
synonyms.append(atom.get("name"))
|
63 |
+
return list(set(synonyms))
|
|
|
|
|
64 |
|
65 |
+
|
66 |
+
def get_semantic_types(cui: str):
|
67 |
"""
|
68 |
+
Retrieve semantic types for a UMLS concept.
|
69 |
"""
|
70 |
+
ticket = get_umls_ticket()
|
71 |
+
url = f"{UMLS_BASE_URL}/content/current/CUI/{cui}/definitions?ticket={ticket}"
|
72 |
+
response = requests.get(url)
|
73 |
+
response.raise_for_status()
|
74 |
+
definitions = response.json().get("result", [])
|
75 |
+
return [d.get("rootSource") for d in definitions]
|
76 |
+
|
77 |
+
|
78 |
+
def cross_map_term(term: str):
|
79 |
+
"""
|
80 |
+
Map a term across all major UMLS vocabularies.
|
81 |
+
"""
|
82 |
+
mappings = {}
|
83 |
+
search_results = search_umls(term, page_size=5)
|
84 |
+
for result in search_results:
|
85 |
+
cui = result["ui"]
|
86 |
+
synonyms = get_concept_synonyms(cui)
|
87 |
+
semantic_types = get_semantic_types(cui)
|
88 |
+
mappings[cui] = {
|
89 |
+
"name": result["name"],
|
90 |
+
"rootSource": result["rootSource"],
|
91 |
+
"synonyms": synonyms,
|
92 |
+
"semantic_types": semantic_types,
|
93 |
}
|
94 |
+
return mappings
|
|