-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathumls_utils.py
495 lines (462 loc) · 22.5 KB
/
umls_utils.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import logging as log
from Authentication import *
import requests
import json
import argparse
import time
## TODO - allow this to be passed via command line or environment variable
UMLS_API_TOKEN = 'NOT-A-REAL-TOKEN-ASDF-QWERTY'
UMLS_API_TOKEN = 'e4dcd9c2-cafd-4dee-a90d-760476c64fac'
last_auth_time = None
last_auth_client = None
def init_authentication( api_key ):
global last_auth_time , last_auth_client
global auth_client
now_time = time.time()
## Re-authorize every thirty minutes
if( last_auth_time is None or
( now_time - last_auth_time ) > 30 * 60 ):
if( last_auth_time is None ):
log.debug( 'Authenticating with the UTS server' )
else:
log.debug( 'Re-authenticating with the UTS server' )
last_auth_time = time.time()
auth_client = Authentication( api_key )
last_auth_client = auth_client
else:
auth_client = last_auth_client
###################################
#get TGT for our session
###################################
return auth_client
########################################################################
##
########################################################################
def search_umls( auth_client , version , identifier , source ,
input_type = 'sourceUi' ,
return_type = 'concept' ):
log.debug( 'call to search_umls( ... , {} , {} , {} , {} )'.format( identifier ,
source ,
input_type ,
return_type ) )
auth_client = init_authentication( UMLS_API_TOKEN )
tgt = auth_client.gettgt()
uri = "https://uts-ws.nlm.nih.gov"
content_endpoint = "/rest/search/current?string="+str(identifier) + \
"&inputType="+str(input_type) + \
"&returnIdType="+str(return_type) + \
"&searchType=exact&sabs="+str(source)
##log( content_endpoint )
##ticket is the only parameter needed for this call - paging does not come into play because we're only asking for one Json object
query = {'ticket':auth_client.getst(tgt)}
r = requests.get(uri+content_endpoint,params=query)
r.encoding = 'utf-8'
items = json.loads(r.text)
if( 'error' in items ):
log.error( 'Query failed due to reported error: {}'.format( items[ 'error' ] ) )
return None
if( 'result' not in items ):
return None
jsonData = items["result"]
##uncomment the print statment if you want the raw json output, or you can just look at the documentation :=)
#https://documentation.uts.nlm.nih.gov/rest/concept/index.html#sample-output
#https://documentation.uts.nlm.nih.gov/rest/source-asserted-identifiers/index.html#sample-output
##log( json.dumps(items, indent = 4) )
############################
classType = jsonData["classType"]
if( len( jsonData["results"] ) > 1 ):
log.debug( 'Multiple matches. Only using the last for {}'.format( source ) )
name = None
cui = None
for inner_results in jsonData["results"]:
name = inner_results[ "name" ]
cui = inner_results["ui"]
##
if( cui == None ):
print (json.dumps(items, indent = 4))
return( cui )
def get_cui( auth_client , version , identifier , source ):
log.debug( 'call to get_cui( ... , {} , ... )'.format( identifier ) )
return( search_umls( auth_client , version , identifier , source ,
input_type = 'sourceUi' ,
return_type = 'concept' ) )
def get_concept_id( auth_client , version , identifier , source ):
log.debug( 'call to get_concept_id( ... , {} , ... )'.format( identifier ) )
return( search_umls( auth_client , version , identifier , source ,
input_type = 'concept' ,
return_type = 'sourceUi' ) )
def get_atoms( auth_client , version , identifier , source ):
log.debug( 'call to get_atoms( ... , {} , ... )'.format( identifier ) )
current_page = 1
last_page = 1
tgt = auth_client.gettgt()
uri = "https://uts-ws.nlm.nih.gov"
content_endpoint = "/rest/content/current/CUI/"+str(identifier) + "/atoms?" + \
"sabs=" + str( source ) + \
"&ttys=PT,HT"
##log( 'Content Endpoint\n\n{}\n'.format( content_endpoint ) )
codes_list = []
############################
while( current_page <= last_page ):
##content_endpoint = "/rest/search/current?string=" + str(identifier) + "inputType=sourceUi&pageNumber=1"
##ticket is the only parameter needed for this call - paging does not come into play because we're only asking for one Json object
query = { 'ticket' : auth_client.getst(tgt) , 'pageNumber' : current_page }
r = requests.get(uri+content_endpoint,params=query)
r.encoding = 'utf-8'
#log( 'Text\n\n{}\n'.format( r.text ) )
try:
items = json.loads(r.text)
except ValueError as e :
current_page +=1
continue
if( 'error' in items ):
log.error( 'Query failed due to reported error: {}'.format( items[ 'error' ] ) )
break
if( 'result' not in items ):
continue
jsonData = items["result"]
##uncomment the print statment if you want the raw json output, or you can just look at the documentation :=)
#https://documentation.uts.nlm.nih.gov/rest/concept/index.html#sample-output
#https://documentation.uts.nlm.nih.gov/rest/source-asserted-identifiers/index.html#sample-output
##log( 'JSON Dump\n\n{}\n'.format( json.dumps(items, indent = 4) ) )
if( current_page == 1 ):
last_page = items[ "pageCount" ]
##log( 'Page {} of {}'.format( current_page , last_page ) )
##
##log( 'Inner Results\n\n' )
for inner_results in jsonData:
source_code_url = inner_results[ 'code' ].split( '/' )
source_code = source_code_url[ len( source_code_url ) - 1 ]
codes_list.append( source_code )
##log( source_code )
current_page +=1
return( codes_list )
def get_family_tree( auth_client , version , identifier ,
relation_type , root_source = 'SNOMEDCT_US' ):
log.debug( 'call to get_family_tree( ... , {} , ... )'.format( identifier ) )
current_page = 1
last_page = 1
auth_client = init_authentication( UMLS_API_TOKEN )
tgt = auth_client.gettgt()
uri = "https://uts-ws.nlm.nih.gov"
content_endpoint = "/rest/content/current/source/" + str( root_source ) + "/"+str(identifier) + "/" + str( relation_type )
##print( '{}'.format( content_endpoint ) )
atoms_set = set()
############################
while( current_page <= last_page ):
##content_endpoint = "/rest/search/current?string=" + str(identifier) + "inputType=sourceUi&pageNumber=1"
##ticket is the only parameter needed for this call - paging does not come into play because we're only asking for one Json object
query = { 'ticket' : auth_client.getst(tgt) , 'pageNumber' : current_page }
r = requests.get(uri+content_endpoint,params=query)
r.encoding = 'utf-8'
##print( r.text )
try:
items = json.loads(r.text)
except ValueError as e :
current_page +=1
continue
if( 'error' in items ):
log.error( 'Query failed due to reported error: {}'.format( items[ 'error' ] ) )
break
if( 'result' not in items ):
continue
jsonData = items["result"]
if( jsonData is None ):
return( atoms_set )
##uncomment the print statment if you want the raw json output, or you can just look at the documentation :=)
#https://documentation.uts.nlm.nih.gov/rest/concept/index.html#sample-output
#https://documentation.uts.nlm.nih.gov/rest/source-asserted-identifiers/index.html#sample-output
##log( json.dumps(items, indent = 4) )
if( current_page == 1 ):
last_page = items[ "pageCount" ]
##log( 'Page {} of {}'.format( current_page , last_page ) )
##
for inner_results in jsonData:
atomic_ui = inner_results[ "ui" ]
##log( '\t{}'.format( relation_label ) )
##if( target_relation_label is not None and
## relation_label != target_relation_label ):
## continue
##name = inner_results[ "relatedIdName" ]
##cui_url = inner_results[ "relatedId" ]
##cui = cui_url.split( '/' )[ -1 ]
##cui_dict[ cui ] = name
atoms_set.add( atomic_ui )
##log( '\t{}'.format( atomic_ui ) )
current_page +=1
return( atoms_set )
def get_parents( auth_client , version , identifier , source ,
atoms = [] ):
log.debug( 'call to get_parents( ... , {} , ... )'.format( identifier ) )
if( atoms == [] ):
atoms = get_atoms( auth_client , version , identifier ,
source = source )
parent_atoms = set()
parent_cuis = set()
for atomic_ui in atoms:
for parent in get_family_tree( auth_client , version , #'9468002' , #'A10134087' ,
atomic_ui ,
relation_type = 'parents' ):
parent_atoms.add( parent )
for parent_aui in parent_atoms:
cui = get_cui( auth_client , version , parent_aui , source )
parent_cuis.add( cui )
return( parent_cuis )
########################################################################
##
########################################################################
def get_cuis_atom( auth_client , version , identifier , atom_type ):
log.debug( 'call to get_cuis_atom( ... , {} , {} )'.format( identifier , atom_type ) )
current_page = 1
last_page = 1
auth_client = init_authentication( UMLS_API_TOKEN )
tgt = auth_client.gettgt()
uri = "https://uts-ws.nlm.nih.gov"
atom_string = ''
if( atom_type != '' ):
atom_string = "/atoms{}".format( str( atom_type ) )
content_endpoint = "/rest/content/current/CUI/"+str(identifier) + atom_string
##print( '{}'.format( content_endpoint ) )
if( atom_type == '/preferred' ):
all_atoms = None
else:
all_atoms = set()
############################
while( current_page <= last_page ):
##content_endpoint = "/rest/search/current?string=" + str(identifier) + "inputType=sourceUi&pageNumber=1"
##ticket is the only parameter needed for this call - paging does not come into play because we're only asking for one Json object
query = { 'ticket' : auth_client.getst(tgt) , 'pageNumber' : current_page }
r = requests.get(uri+content_endpoint,params=query)
r.encoding = 'utf-8'
##log( r.text )
try:
items = json.loads(r.text)
except ValueError as e :
current_page +=1
continue
if( 'error' in items ):
log.error( 'Query failed due to reported error: {}'.format( items[ 'error' ] ) )
break
if( 'result' not in items ):
continue
jsonData = items["result"]
##uncomment the print statment if you want the raw json output, or you can just look at the documentation :=)
#https://documentation.uts.nlm.nih.gov/rest/concept/index.html#sample-output
#https://documentation.uts.nlm.nih.gov/rest/source-asserted-identifiers/index.html#sample-output
##log( json.dumps(items, indent = 4) )
if( current_page == 1 ):
last_page = items[ "pageCount" ]
if( last_page > 1 ):
log.debug( 'Page {} of {}'.format( current_page , last_page ) )
##
if( atom_type == '/preferred' ):
name = jsonData[ "name" ]
##log( '{}\t{}'.format( str( identifier ) , name ) )
return( name )
elif( atom_type == '' ):
semantic_types = jsonData[ 'semanticTypes' ]
for sem_type in semantic_types:
tui = sem_type[ 'uri' ].split( '/' )[ -1 ]
return( tui )
else:
for inner_results in jsonData:
name = inner_results[ "name" ]
all_atoms.add( name )
##log( '{}\t{}'.format( identifier , name ) )
current_page +=1
return( all_atoms )
def get_cuis_preferred_atom( auth_client , version , identifier ):
log.debug( 'call to get_cui_preferred_atom( . , {} , {} )'.format( version , identifier ) )
return( get_cuis_atom( auth_client , version , identifier , atom_type = '/preferred' ) )
def get_cuis_eng_atom( auth_client , version , identifier ):
log.debug( 'call to get_cui_eng_atom( . , {} , {} )'.format( version , identifier ) )
return( get_cuis_atom( auth_client , version , identifier , atom_type = '?language=ENG' ) )
def get_typed_relation( auth_client , version , identifier , target_relation_type , target_relation_label ):
log.debug( 'call to get_typed_relation( ... , {} , {} , {} )'.format( identifier , target_relation_type , target_relation_label ) )
current_page = 1
last_page = 1
auth_client = init_authentication( UMLS_API_TOKEN )
tgt = auth_client.gettgt()
uri = "https://uts-ws.nlm.nih.gov"
content_endpoint = "/rest/content/current/CUI/"+str(identifier) + "/" + str(target_relation_type)
##log( '{}'.format( content_endpoint ) )
cui_dict = {}
############################
while( current_page <= last_page ):
##content_endpoint = "/rest/search/current?string=" + str(identifier) + "inputType=sourceUi&pageNumber=1"
##ticket is the only parameter needed for this call - paging does not come into play because we're only asking for one Json object
query = { 'ticket' : auth_client.getst(tgt) , 'pageNumber' : current_page }
r = requests.get(uri+content_endpoint,params=query)
r.encoding = 'utf-8'
##log( '{}'.format( r.text ) )
try:
items = json.loads(r.text)
except ValueError as e :
current_page +=1
continue
if( 'error' in items ):
log.error( 'Query failed due to reported error: {}'.format( items[ 'error' ] ) )
break
if( 'result' not in items ):
continue
jsonData = items[ "result" ]
##uncomment the print statment if you want the raw json output, or you can just look at the documentation :=)
#https://documentation.uts.nlm.nih.gov/rest/concept/index.html#sample-output
#https://documentation.uts.nlm.nih.gov/rest/source-asserted-identifiers/index.html#sample-output
##log( '{}'.format( json.dumps(items, indent = 4) ) )
if( current_page == 1 ):
last_page = items[ "pageCount" ]
##log( 'Page {} of {}'.format( current_page , last_page ) )
##
for inner_results in jsonData:
relation_label = inner_results[ "relationLabel" ]
##log( '\t{}'.format( relation_label ) )
if( target_relation_label is not None and
relation_label != target_relation_label ):
continue
name = inner_results[ "relatedIdName" ]
cui_url = inner_results[ "relatedId" ]
cui = cui_url.split( '/' )[ -1 ]
cui_dict[ cui ] = name
current_page +=1
return( cui_dict )
def get_rbs( auth_client , version , identifier ):
log.debug( 'call to get_rbs( ... , {} )'.format( identifier ) )
return( get_typed_relation( auth_client , version , identifier ,
target_relation_type = 'relations' ,
target_relation_label = 'RB' ) )
def get_rns( auth_client , version , identifier ):
log.debug( 'call to get_rns( ... , {} )'.format( identifier ) )
return( get_typed_relation( auth_client , version , identifier ,
target_relation_type = 'relations' ,
target_relation_label = 'RN' ) )
def get_ros( auth_client , version , identifier ):
log.debug( 'call to get_ros( ... , {} )'.format( identifier ) )
return( get_typed_relation( auth_client , version , identifier ,
target_relation_type = 'relations' ,
target_relation_label = 'RO' ) )
########################################################################
##
########################################################################
def get_all_umls_descendants( auth_client , head_cui ,
concepts ,
exclude_list , already_included = None ):
descendant_cuis = get_rbs( auth_client , 'current' , head_cui )
log.debug( 'Grabbed RBs. descendant cui n = {}'.format( len( descendant_cuis ) ) )
include_list = []
for descendant_cui in descendant_cuis:
if( descendant_cui in exclude_list or
descendant_cui in concepts or
( already_included is not None and
descendant_cui in already_included ) ):
continue
include_list.append( descendant_cui )
include_list += get_all_umls_descendants( auth_client , descendant_cui ,
concepts ,
exclude_list ,
include_list )
return include_list
def get_all_snomed_descendants( auth_client , head_concept_id ,
exclude_list ):
descendant_concept_ids = get_family_tree( auth_client , 'current' ,
head_concept_id ,
relation_type = 'children' )
include_list = []
for descendant_concept_id in descendant_concept_ids:
descendant_cui = get_cui( auth_client , 'current' , descendant_concept_id , 'SNOMEDCT_US' )
if( descendant_cui in exclude_list ):
continue
include_list.append( descendant_cui )
include_list += get_all_snomed_descendants( auth_client , descendant_concept_id ,
exclude_list )
return include_list
########################################################################
##
########################################################################
def get_first_umls_children( auth_client , head_cui ,
exclude_list , get_grandchildren = False ):
descendant_cuis = get_rbs( auth_client , 'current' , head_cui )
include_list = []
for descendant_cui in descendant_cuis:
if( descendant_cui in exclude_list ):
continue
include_list.append( descendant_cui )
if( get_grandchildren ):
include_list += get_first_umls_children( auth_client , descendant_cui ,
exclude_list , False )
return include_list
def get_first_rxnorm_ancestors( auth_client , head_concept_id ):
descendant_concept_ids = get_family_tree( auth_client , 'current' ,
head_concept_id ,
relation_type = 'ancestors' ,
root_source = 'RXNORM' )
include_list = []
for descendant_concept_id in descendant_concept_ids:
descendant_cui = get_cui( auth_client , 'current' , descendant_concept_id , 'RXNORM' )
include_list.append( descendant_cui )
return include_list
########################################################################
##
########################################################################
def get_rxcui_umls_cui( rxcui_str ):
base_uri = 'https://rxnav.nlm.nih.gov/REST/'
content_endpoint = "rxcui/" + rxcui_str + "/property.json?propName=UMLSCUI"
##log( '{}{}'.format( base_uri , content_endpoint ) )
#query = {'ticket':auth_client.getst(tgt)}
r = requests.get( base_uri + content_endpoint )#,params=query)
r.encoding = 'utf-8'
items = json.loads(r.text)
all_cuis = set()
if( 'propConceptGroup' not in items or
items[ 'propConceptGroup' ] is None or
'propConcept' not in items[ 'propConceptGroup' ] ):
return( all_cuis )
##log.error( '{}\n---------------\n'.format( items ) )
groupData = items[ "propConceptGroup" ][ "propConcept" ]
##log( '{}\n---------------\n{}'.format( items , groupData ) )
for group in groupData:
umls_cui = group[ 'propValue' ]
#add_dictionary_entry( umls_cui , name )
all_cuis.add( umls_cui )
return all_cuis
def get_rxclass_members( rxclass_str ):
base_uri = 'https://rxnav.nlm.nih.gov/REST/'
relaSrc = 'ATC'
if( rxclass_str == 'D009294' ):
relaSrc = 'MESH'
##
content_endpoint = "rxclass/classMembers.json?classId=" + rxclass_str + "&relaSource=" + relaSrc
#log( '{}{}'.format( base_uri , content_endpoint ) )
#query = {'ticket':auth_client.getst(tgt)}
r = requests.get( base_uri + content_endpoint )#,params=query)
r.encoding = 'utf-8'
#log( '{}\n---------------\n'.format( r ) )
items = json.loads(r.text)
all_cuis = set()
groupData = items[ "drugMemberGroup" ][ "drugMember" ]
##log( '{}\n---------------\n{}'.format( items , groupData ) )
for group in groupData:
if( 'minConcept' in group ):
concept = group[ "minConcept" ]
rx_cui = concept[ 'rxcui' ]
name = concept[ 'name' ]
all_cuis.add( rx_cui )
return all_cuis
########################################################################
##
########################################################################
if __name__ == "__main__":
auth_client = init_authentication( UMLS_API_TOKEN )
##log( '{}'.format( get_cuis_preferred_atom( auth_client , 'current' , 'C0000731' ) ) )
#print( '{}'.format( get_cuis_atom( auth_client , 'current' , 'C0553968' , '' ) ) )
#print( '{}'.format( get_cuis_atom( auth_client , 'current' , 'C0553968' , '/preferred' ) ) )
##print( '{}'.format( get_cuis_atom( auth_client , 'current' , 'C0553968' , '?language=ENG' ) ) )
print( '{}'.format( get_typed_relation( auth_client , 'current' , 'C0232491' , 'relations' , 'RB' ) ) )
#print( '{}'.format( get_cuis_atom( auth_client , 'current' , 'C0019699' , '' ) ) )
#print( '{}'.format( get_concept_id( auth_client , 'current' , 'C0019699' , 'SNOMEDCT_US' ) ) )
#print( '{}'.format( get_concept_id( auth_client , 'current' , '165816005' , 'SNOMEDCT_US' ) ) )
##print( '{}'.format( get_cui( auth_client , 'current' , 'C0019699' , '' ) ) )
#print( '{}'.format( get_family_tree( auth_client , 'current' , '165816005' , 'parents' ) ) )
##print( '{}'.format( get_family_tree( auth_client , 'current' , '165816005' , 'CHD' ) ) )
##log( '{}'.format( get_cuis_atom( auth_client , 'current' , 'C0000731' ,
## atom_type = '?language=ENG' ) ) )