-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutilities.py
269 lines (240 loc) · 9.03 KB
/
utilities.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/python
"""
Utility functions.
"""
import time
import logging
import requests
import json
from config import settings
from Authentication import Authentication
# API-kEY FOR UMLS REST TICKET SERVICES
umls_api = settings['apis']['umls']
# UMLS REST SERVICES INITIALIZATION OF CLIENT AND TICKET
# GRANTING SERVICE TO BE USED IN ALL CASES
AuthClient = Authentication(umls_api)
tgt = AuthClient.gettgt()
# To supress some kind of warning?!
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
# logging.basicConfig(
# format="%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s",
# handlers=[
# #logging.FileHandler("%s" % settings['log_path']),
# logging.StreamHandler()
# ])
# logging.info('lala')
# # create logger
# logger = logging.getLogger()
# formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# ch = logging.StreamHandler()
# ch.setFormatter(formatter)
# logger.addHandler(ch)
# fh = logging.FileHandler(settings['log_path'])
# fh.setFormatter(formatter)
# logger.addHandler(fh)
def get_umls_ticket2(tgt=tgt, AuthClient=AuthClient, apikey=umls_api):
"""
Get a single use ticket for the UMLS REST services.
It is supposed that an Author Client and a Ticket
Granting Service have already been set-up in case
the apikey = None. If an api-key is given, create the
above needed instances and generate a new ticket.
Input:
- apikey: str,
UMLS REST services api-key. Default is None and
the already establised service is used
Output:
- string of the generated ticket
"""
# Get ticket from the already establised service
if not(tgt) and not(AuthClient):
AuthClient = Authentication(umls_api)
tgt = AuthClient.gettgt()
return AuthClient.getst(tgt)
def get_umls_ticket(apikey=None, AuthClient=AuthClient, tgt=tgt):
"""
Get a single use ticket for the UMLS REST services.
It is supposed that an Author Client and a Ticket
Granting Service have already been set-up in case
the apikey = None. If an api-key is given, create the
above needed instances and generate a new ticket.
Input:
- apikey: str,
UMLS REST services api-key. Default is None and
the already establised service is used
Output:
- string of the generated ticket
"""
# Get ticket from the already establised service
if apikey is None:
return AuthClient.getst(tgt)
else:
# Establish new Client and Ticket granting service
AuthClient = Authentication(apikey)
tgt = AuthClient.gettgt()
return AuthClient.getst(tgt)
def time_log(phrase, time_start=None):
"""
A time_logger function so as to print info with time since elapsed if wanted,
alongside with the current logging config.
"""
# If we want to also print time_elapsed
if time_start:
logging.info('%s in : %.2f seconds.' % (phrase, time.time() - time_start))
else:
logging.info('%s' % (phrase))
return 1
def get_concept_from_source(source_id, source, apikey=tgt):
"""
Function that maps an entity from another source to UMLS concepts.
Input:
- source_id: str,
string of the unique id from the source knowledge base
- source: str,
string code-name of the knowledge base, as used by the
UMLS REST services (e.g. drugbank -> DRUGBANK, MESH->MSH)
- apikey: str,
UMLS REST services api-key. Default is None and
the already establised service is used. Check get_umls_ticket
function for details
Output:
- concepts: list,
list of dictionaries representing concepts mapped to the
source_id entity. Each dictionary has keys "label", "cuid", "sem_types"
Check get_concept_from_cui for more details
"""
ticket = get_umls_ticket2()
#ticket = get_umls_ticket(apikey)
url = "https://uts-ws.nlm.nih.gov/rest/search/current"
passed = False
times = 0
while not(passed):
params = {'string': source_id, 'sabs': source, 'searchType': 'exact',
'inputType': 'sourceUi', 'ticket': ticket}
r = requests.get(url, params=params)
r.encoding = 'utf-8'
concepts = []
if r.ok:
items = json.loads(r.text)
jsonData = items["result"]
# Get cuis related to source_id
cuis = [res['ui']for res in jsonData['results']]
# Get concepts from cuis
concepts = [get_concept_from_cui(cui) for cui in cuis if cui != 'NONE']
passed = True
else:
time_log(r.url)
time_log('Error getting concept from: Source %s | ID: %s' % (source, source_id))
time_log('~'*25 + ' GETTING NEW TICKET SERVICE' + '~'*24)
ticket = get_umls_ticket2(None, None, umls_api)
times += 1
if times >= 2:
passed = True
time_log('Error getting concept from: Source %s | ID: %s' % (source, source_id))
time_log('~'*25 + ' EXITING AFTER TRYING TWICE WITH NEW TICKET ' + '~'*25)
#exit(1)
raise Exception
return concepts
def get_concept_from_cui(cui, apikey=None):
"""
Function that fetches a concept's attributes from the corresponding cui.
Input:
- cui: str,
string of cui that will be looked up
- apikey: str,
UMLS REST services api-key. Default is None and
the already establised service is used. Check get_umls_ticket
function for details
Output:
- res: dictionary,
dictionary with the concepts attributes as fetched. Specifically,
"label", "cuid"(cui) and "sem_types"(comma delimited string of
the semantic types us returned)
"""
ticket = get_umls_ticket2()
#ticket = get_umls_ticket(apikey)
url = "https://uts-ws.nlm.nih.gov/rest/content/current/CUI/" + cui
passed = False
times = 0
while not(passed):
try:
r = requests.get(url, params={'ticket': ticket}, timeout=120)
passed = True
except requests.exceptions.Timeout:
time_log('~'*25 + ' TIMEOUT ERROR 120 SECONDS'+'~'*25)
time_log('~'*25 + ' GETTING NEW TICKET SERVICE' + '~'*24)
ticket = get_umls_ticket2(None, None, umls_api)
times += 1
if times >= 2:
passed = True
time_log('Error getting concept from: CUI %s' % cui)
time_log('~'*25 + ' EXITING AFTER TRYING TWICE WITH NEW TICKET ' + '~'*25)
raise Exception
#exit(1)
r.encoding = 'utf-8'
res = {}
if r.ok:
items = json.loads(r.text)
jsonData = items["result"]
res = {'label': jsonData['name'], 'cuid': cui}
sem_types = []
# For each semantic type of the entity
for stys in jsonData["semanticTypes"]:
# Keep only the TUI code from the uri e.g.
# https://uts-ws.nlm.nih.gov/rest/semantic-network/current/TUI/T116
code_tui = stys['uri'].split('/')[-1]
# Fetch the abbreviation of this TUI code
sem_types.append(get_sem_type_abbr(code_tui))
# Comma separated string
sem_types = ",".join(sem_types)
res['sem_types'] = sem_types
else:
time_log(r.url)
time_log('Error getting concept from cui : %s' % cui)
raise ValueError
return res
def get_sem_type_abbr(code_tui, apikey=None):
"""
Function that fetches a semantic-type's abbreviation.
Input:
- code_tui: str,
string of TUI code that will be looked up
- apikey: str,
UMLS REST services api-key. Default is None and
the already establised service is used. Check get_umls_ticket
function for details
Output:
string, abbreviation of the code (e.g. "gngm")
"""
ticket = get_umls_ticket2()
#ticket = get_umls_ticket(apikey)
url = "https://uts-ws.nlm.nih.gov/rest/semantic-network/current/TUI/" + code_tui
passed = False
times = 0
while not(passed):
try:
r = requests.get(url, params={'ticket': ticket}, timeout=120)
passed = True
except requests.exceptions.Timeout:
time_log('~'*25 + ' TIMEOUT ERROR 120 SECONDS'+'~'*25)
time_log('~'*25 + ' GETTING NEW TICKET SERVICE' + '~'*24)
ticket = get_umls_ticket2(None, None, umls_api)
times += 1
if times >= 2:
passed = True
time_log('Error getting semantic type abbreviation: %s' % code_tui)
time_log('~'*25 + ' EXITING AFTER TRYING TWICE WITH NEW TICKET ' + '~'*25)
raise Exception
#exit(1)
r.encoding = 'utf-8'
res = ' '
if r.ok:
items = json.loads(r.text)
jsonData = items["result"]
res = jsonData['abbreviation']
else:
time_log(r.url)
time_log('Error getting sem-type from TUI : %s' % code_tui)
raise ValueError
return res