-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquery_drugbank.py
64 lines (54 loc) · 1.86 KB
/
query_drugbank.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
import json
from tqdm import tqdm
import csv
from fuzzywuzzy import fuzz
import re
field_name = "prescription"
with open(f"mimic_{field_name}_name.json", 'r') as f:
data = json.load(f)
returned_data = {}
visit = 0
success = 0
for (idx, name) in tqdm(data.items()):
if field_name == "prescription":
idx, name = name, idx
print(idx, name)
returned_data[idx] = name
with open("drugbank_drugs_info.csv", 'r') as f:
f.readline()
data = {}
reader = csv.reader(f, delimiter = ',')
for line in reader:
drug_title = line[3]
dbid = line[10]
drug_name = line[11]
desc = line[13]
data[drug_title] = {"drugbank": dbid, "description": desc}
data[drug_name] = {"drugbank": dbid, "description": desc}
print(len(data))
def clean_string(s):
# Remove "NEO*IV*"
s = s.replace("NEO*IV*", "")
s = s.replace("*NF*", "")
s = re.sub(r'\d+(\.\d+)?%', '', s)
# Remove content within brackets and the brackets themselves
s = re.sub(r'\(.*?\)', '', s)
return s.strip()
save_data = {}
for idx in tqdm(returned_data):
name = returned_data[idx]
score_max = -1
name = clean_string(name)
for drug_name in data:
score = fuzz.ratio(name, clean_string(drug_name))
if score > score_max:
best_name = drug_name
best_def = data[drug_name]["description"].split("\n")[0]
score_max = score
if score_max >= 80:
print(name, best_name, best_def, score_max)
save_data[idx] = {"name": returned_data[idx], "drugbank_name": best_name, "def": best_def, "score": score_max}
else:
save_data[idx] = {"name": returned_data[idx], "drugbank_name": "", "def": "", "score": ""}
with open(f"mimic_{field_name}_name_drugbank.json", 'w') as f_out:
json.dump(save_data, f_out, indent = 2)