-
Notifications
You must be signed in to change notification settings - Fork 0
/
codes_by_concept.py
63 lines (50 loc) · 2.64 KB
/
codes_by_concept.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
#This script returns codes based on an input file of UMLS CUIs, where each line in txt file is a separate CUI.
#If no results are found for a specific CUI, this will be noted in output and output file.
#Each set of results for a CUI is separated in the output file with '***'.
#USAGE: python codes_by_concept.py -k YOUR_API_KEY -i cuis.txt -o output.txt -s SNOMEDCT_US
import requests
import argparse
parser = argparse.ArgumentParser(description='process user given parameters')
parser.add_argument('-k', '--apikey', required = True, dest = 'apikey', help = 'enter api key from your UTS Profile')
parser.add_argument('-v', '--version', required = False, dest='version', default = 'current', help = 'enter version example-2015AA')
parser.add_argument('-o', '--outputfile', required = True, dest = 'outputfile', help = 'enter a name for your output file')
parser.add_argument('-s', '--sabs', required = True, dest='sabs',help = 'enter a comma-separated list of vocabularies without spaces, like MSH,SNOMEDCT_US')
parser.add_argument('-i', '--inputfile', required = True, dest = 'inputfile', help = 'enter a name for your input file')
args = parser.parse_args()
apikey = args.apikey
version = args.version
outputfile = args.outputfile
inputfile = args.inputfile
sabs = args.sabs
base_uri = 'https://uts-ws.nlm.nih.gov'
cui_list = []
with open(inputfile, encoding='utf-8') as f:
for line in f:
if line.isspace() is False:
cui1 = line.strip()
cui_list.append(cui1)
else:
continue
with open(outputfile, 'w', encoding='utf-8') as o:
for cui in cui_list:
page = 0
o.write('SEARCH CUI: ' + cui + '\n' + '\n')
while True:
page += 1
path = '/search/'+version
query = {'apiKey':apikey, 'string':cui, 'sabs':sabs, 'returnIdType':'code', 'pageNumber':page}
output = requests.get(base_uri+path, params=query)
output.encoding = 'utf-8'
#print(output.url)
outputJson = output.json()
results = (([outputJson['result']])[0])['results']
if len(results) == 0:
if page == 1:
print('No results found for ' + cui +'\n')
o.write('No results found.' + '\n' + '\n')
break
else:
break
for item in results:
o.write('Name: ' + item['name'] + '\n' + 'UI: ' + item['ui'] + '\n' + 'Source Vocabulary: ' + item['rootSource'] + '\n' + 'URI: ' + item['uri'] + '\n' + '\n')
o.write('***' + '\n' + '\n')