Spaces:
Sleeping
Sleeping
Update genesis/api_clients/umls_api.py
Browse files- genesis/api_clients/umls_api.py +83 -62
genesis/api_clients/umls_api.py
CHANGED
@@ -1,105 +1,126 @@
|
|
1 |
# genesis/api_clients/umls_api.py
|
2 |
import os
|
3 |
import requests
|
4 |
-
from typing import
|
5 |
|
6 |
-
UMLS_API_KEY = os.getenv("UMLS_API_KEY")
|
7 |
-
|
|
|
8 |
|
9 |
-
|
10 |
-
raise EnvironmentError("Missing UMLS_API_KEY in environment variables.")
|
11 |
|
12 |
# -------------------------
|
13 |
-
#
|
14 |
# -------------------------
|
15 |
-
def
|
16 |
"""
|
17 |
-
Get
|
18 |
"""
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
25 |
return r.headers.get("location")
|
26 |
|
27 |
-
def
|
28 |
"""
|
29 |
-
|
30 |
"""
|
31 |
-
|
|
|
32 |
r.raise_for_status()
|
33 |
return r.text
|
34 |
|
35 |
# -------------------------
|
36 |
-
#
|
37 |
# -------------------------
|
38 |
-
def
|
39 |
"""
|
40 |
-
Search
|
41 |
"""
|
42 |
-
tgt =
|
43 |
-
|
44 |
|
45 |
params = {
|
46 |
"string": term,
|
47 |
-
"ticket":
|
48 |
"pageSize": page_size
|
49 |
}
|
50 |
-
r =
|
51 |
r.raise_for_status()
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
for
|
56 |
-
|
57 |
-
"
|
58 |
-
"
|
59 |
-
"rootSource":
|
60 |
})
|
61 |
-
return
|
62 |
|
63 |
-
|
|
|
|
|
|
|
64 |
"""
|
65 |
-
|
66 |
"""
|
67 |
-
tgt =
|
68 |
-
|
69 |
|
70 |
-
|
71 |
-
r = requests.get(f"{UMLS_BASE}/rest/content/current/CUI/{cui}", params=params)
|
72 |
r.raise_for_status()
|
73 |
-
return r.json()
|
74 |
|
75 |
-
|
|
|
|
|
|
|
76 |
"""
|
77 |
-
|
78 |
-
relation_type: 'RO' (related other), 'RQ' (related and qualified), etc.
|
79 |
"""
|
80 |
-
tgt =
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
r = requests.get(
|
85 |
-
f"{UMLS_BASE}/rest/content/current/CUI/{cui}/relations",
|
86 |
-
params=params
|
87 |
-
)
|
88 |
r.raise_for_status()
|
89 |
-
data = r.json()
|
90 |
|
|
|
91 |
related = []
|
92 |
-
for rel in
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
})
|
99 |
return related
|
100 |
|
101 |
-
|
|
|
|
|
|
|
102 |
"""
|
103 |
-
|
104 |
"""
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# genesis/api_clients/umls_api.py
|
2 |
import os
|
3 |
import requests
|
4 |
+
from typing import List, Dict
|
5 |
|
6 |
+
UMLS_API_KEY = os.getenv("UMLS_API_KEY") # Your UMLS API key from NIH UTS
|
7 |
+
UMLS_AUTH_ENDPOINT = "https://utslogin.nlm.nih.gov/cas/v1/api-key"
|
8 |
+
UMLS_BASE = "https://uts-ws.nlm.nih.gov/rest"
|
9 |
|
10 |
+
session = requests.Session()
|
|
|
11 |
|
12 |
# -------------------------
|
13 |
+
# UMLS Authentication
|
14 |
# -------------------------
|
15 |
+
def get_umls_ticket_granting_ticket() -> str:
|
16 |
"""
|
17 |
+
Get the Ticket Granting Ticket (TGT) for UMLS API authentication.
|
18 |
"""
|
19 |
+
if not UMLS_API_KEY:
|
20 |
+
raise ValueError("UMLS API key not found in environment variables.")
|
21 |
+
|
22 |
+
params = {"apikey": UMLS_API_KEY}
|
23 |
+
r = session.post(UMLS_AUTH_ENDPOINT, data=params)
|
24 |
+
r.raise_for_status()
|
25 |
+
|
26 |
+
# The TGT is returned in the Location header of the POST response
|
27 |
return r.headers.get("location")
|
28 |
|
29 |
+
def get_umls_service_ticket(tgt: str) -> str:
|
30 |
"""
|
31 |
+
Use TGT to request a one-time Service Ticket (ST).
|
32 |
"""
|
33 |
+
params = {"service": "http://umlsks.nlm.nih.gov"}
|
34 |
+
r = session.post(tgt, data=params)
|
35 |
r.raise_for_status()
|
36 |
return r.text
|
37 |
|
38 |
# -------------------------
|
39 |
+
# Search UMLS Concepts
|
40 |
# -------------------------
|
41 |
+
def search_umls(term: str, page_size: int = 5) -> List[Dict]:
|
42 |
"""
|
43 |
+
Search the UMLS Metathesaurus for a given term and return related CUIs.
|
44 |
"""
|
45 |
+
tgt = get_umls_ticket_granting_ticket()
|
46 |
+
st = get_umls_service_ticket(tgt)
|
47 |
|
48 |
params = {
|
49 |
"string": term,
|
50 |
+
"ticket": st,
|
51 |
"pageSize": page_size
|
52 |
}
|
53 |
+
r = session.get(f"{UMLS_BASE}/search/current", params=params)
|
54 |
r.raise_for_status()
|
55 |
+
|
56 |
+
results = r.json().get("result", {}).get("results", [])
|
57 |
+
concepts = []
|
58 |
+
for res in results:
|
59 |
+
concepts.append({
|
60 |
+
"name": res.get("name"),
|
61 |
+
"cui": res.get("ui"),
|
62 |
+
"rootSource": res.get("rootSource")
|
63 |
})
|
64 |
+
return concepts
|
65 |
|
66 |
+
# -------------------------
|
67 |
+
# Get UMLS Concept Details
|
68 |
+
# -------------------------
|
69 |
+
def get_concept_details(cui: str) -> Dict:
|
70 |
"""
|
71 |
+
Fetch full details for a UMLS concept (definitions, atoms, semantic types).
|
72 |
"""
|
73 |
+
tgt = get_umls_ticket_granting_ticket()
|
74 |
+
st = get_umls_service_ticket(tgt)
|
75 |
|
76 |
+
r = session.get(f"{UMLS_BASE}/content/current/CUI/{cui}", params={"ticket": st})
|
|
|
77 |
r.raise_for_status()
|
78 |
+
return r.json().get("result", {})
|
79 |
|
80 |
+
# -------------------------
|
81 |
+
# Get Related Concepts
|
82 |
+
# -------------------------
|
83 |
+
def get_related_concepts(cui: str) -> List[Dict]:
|
84 |
"""
|
85 |
+
Fetch related concepts for a given CUI.
|
|
|
86 |
"""
|
87 |
+
tgt = get_umls_ticket_granting_ticket()
|
88 |
+
st = get_umls_service_ticket(tgt)
|
89 |
+
|
90 |
+
r = session.get(f"{UMLS_BASE}/content/current/CUI/{cui}/relations", params={"ticket": st})
|
|
|
|
|
|
|
|
|
91 |
r.raise_for_status()
|
|
|
92 |
|
93 |
+
relations = r.json().get("result", [])
|
94 |
related = []
|
95 |
+
for rel in relations:
|
96 |
+
related.append({
|
97 |
+
"relatedCUI": rel.get("relatedId"),
|
98 |
+
"relationLabel": rel.get("relationLabel"),
|
99 |
+
"relatedName": rel.get("relatedIdName")
|
100 |
+
})
|
|
|
101 |
return related
|
102 |
|
103 |
+
# -------------------------
|
104 |
+
# Map Term to Related Network
|
105 |
+
# -------------------------
|
106 |
+
def umls_network(term: str) -> Dict:
|
107 |
"""
|
108 |
+
Build a semantic network for a term: CUI, details, and related concepts.
|
109 |
"""
|
110 |
+
concepts = search_umls(term)
|
111 |
+
network = []
|
112 |
+
|
113 |
+
for concept in concepts:
|
114 |
+
cui = concept["cui"]
|
115 |
+
details = get_concept_details(cui)
|
116 |
+
related = get_related_concepts(cui)
|
117 |
+
network.append({
|
118 |
+
"concept": concept,
|
119 |
+
"details": details,
|
120 |
+
"related": related
|
121 |
+
})
|
122 |
+
|
123 |
+
return {
|
124 |
+
"term": term,
|
125 |
+
"network": network
|
126 |
+
}
|