-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbioport-batch.py
More file actions
68 lines (59 loc) · 1.81 KB
/
Copy pathbioport-batch.py
File metadata and controls
68 lines (59 loc) · 1.81 KB
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
import requests
import json
import argparse
import csv
# search an array of dictionaries
def findterm(term, a):
o = list(filter(lambda a:a['icd'] == term, a))
if len(o) == 0:
return False
return True
term = ""
termCache = []
colhdr = ['code', 'desc', 'icd']
url = "http://data.bioontology.org/search"
querystring = {"q":term}
headers = {
'authorization': "apikey token=[ADD YOUR TOKEN HERE]",
'cache-control': "no-cache",
'postman-token': "e9e97c86-ecba-cca7-4213-3933e49f3e09"
}
print('Searching...')
with open("comorbid.csv") as csvfile:
reader = csv.DictReader(csvfile)
title = reader.fieldnames
cnt = 0
for row in reader:
dec = ""
for k,v in row.items():
lastChar = v[-2:] # just check last two chars
val = v[:len(v)-2]
if lastChar:
if lastChar[-1:] == '0': # if it is a zero only ignore it
dec = "." +lastChar[0] # add decimal to last 1 digit
else:
dec = "." + lastChar # add decimal to last 2 digits
if val:
term = val + dec # add decimal for ICD-9
#if term not in termCache:
r = findterm(term, termCache)
#print(r)
if r is False: # if not in the cache then search
querystring = {"q":term}
response = requests.request("GET", url, headers=headers, params=querystring) # search bioportal
j = json.loads(response.text)
col = j["collection"]
if col:
d = {'code': v, 'desc':col[0]["prefLabel"], 'icd':term}
#print(d)
termCache.append(d)
#cnt = cnt+1
#if cnt == 5:
# break
print('building out csv...')
with open("comorbid-out.csv", "w") as out:
writer = csv.DictWriter(out, fieldnames=colhdr, dialect='excel', lineterminator='\n')
writer.writeheader()
for data in termCache:
writer.writerow(data)
#print(response.text)