mgbam commited on
Commit
7b3f817
·
verified ·
1 Parent(s): d24e0a6

Update genesis/api_clients/umls_api.py

Browse files
Files changed (1) hide show
  1. 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
- UMLS_AUTH_URL = "https://utslogin.nlm.nih.gov"
7
- UMLS_API_URL = "https://uts-ws.nlm.nih.gov/rest"
8
 
9
  if not UMLS_API_KEY:
10
- raise ValueError("Missing UMLS_API_KEY in environment variables")
11
 
12
- def get_tgt():
 
13
  """
14
- Get a Ticket Granting Ticket (TGT) for UMLS authentication.
15
  """
16
- url = f"{UMLS_AUTH_URL}/cas/v1/api-key"
17
- params = {"apikey": UMLS_API_KEY}
18
- res = requests.post(url, data=params)
19
- res.raise_for_status()
20
- # Extract TGT URL from HTML form action
21
- return res.text.split('action="')[1].split('"')[0]
22
 
23
- def get_service_ticket(tgt):
24
  """
25
- Get a Service Ticket (ST) using TGT.
26
  """
27
- params = {"service": "http://umlsks.nlm.nih.gov"}
28
- res = requests.post(tgt, data=params)
29
- res.raise_for_status()
30
- return res.text
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- def search_umls(query: str, page: int = 1, page_size: int = 10):
 
33
  """
34
- Search for a term in UMLS.
35
  """
36
- tgt = get_tgt()
37
- ticket = get_service_ticket(tgt)
38
- url = f"{UMLS_API_URL}/search/current"
39
- params = {
40
- "string": query,
41
- "ticket": ticket,
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 get_cui_details(cui: str):
50
  """
51
- Get detailed info for a CUI (Concept Unique Identifier).
52
  """
53
- tgt = get_tgt()
54
- ticket = get_service_ticket(tgt)
55
- url = f"{UMLS_API_URL}/content/current/CUI/{cui}"
56
- params = {"ticket": ticket}
57
- res = requests.get(url, params=params)
58
- res.raise_for_status()
59
- return res.json()
60
 
61
- def map_to_umls(term: str):
 
62
  """
63
- Given a term, return the matching UMLS CUI(s) and definitions.
64
  """
65
- matches = search_umls(term)
66
- return [
67
- {
68
- "name": m.get("name"),
69
- "cui": m.get("ui"),
70
- "rootSource": m.get("rootSource")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  }
72
- for m in matches if m.get("ui") != "NONE"
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