forked from emreg00/toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_medi.py
164 lines (147 loc) · 5.85 KB
/
parse_medi.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
import os, cPickle, parse_drugbank
def main():
name = "Mesna" # Acarbose
#mapping_file = "/home/emre/data/indication/medi/MEDI_01212013_UMLS.csv"
mapping_file = "/home/emre/data/indication/medi/MEDI_01212013.csv"
#name_to_cui_and_confidences = get_medi_mapping_old(mapping_file)
name_to_icd_and_confidences = get_medi_mapping(mapping_file)
print name_to_icd_and_confidences[name]
return
def get_disease_specific_drugs(drug_to_diseases, phenotype_to_mesh_id, cutoff=1):
disease_to_drugs = {}
mesh_id_to_phenotype = {}
for phenotype, mesh_id in phenotype_to_mesh_id.items():
mesh_id_to_phenotype[mesh_id] = phenotype
for drugbank_id, diseases in drug_to_diseases.iteritems():
for phenotype, dui, val in diseases:
if val >= cutoff:
if dui in mesh_id_to_phenotype: # In the disease data set
disease = mesh_id_to_phenotype[dui].lower()
disease_to_drugs.setdefault(disease, set()).add(drugbank_id)
return disease_to_drugs
def get_drug_disease_mapping(name_to_icd_and_confidences, name_to_drug, synonym_to_drug, icd_to_mesh_ids, mesh_id_to_name, dump_file):
"""
name_to_icd_and_confidences mapping from MEDI
name_to_drug & synonym mapping from DrugBank
icd_to_mesh_ids mapping from DO
mesh_id_to_name mapping from UMLS (MSH | MH)
"""
if dump_file is not None and os.path.exists(dump_file):
drug_to_diseases = cPickle.load(open(dump_file))
return drug_to_diseases
drug_to_diseases = {} # (mesh_term, mesh_id, association score)
for name, values in name_to_icd_and_confidences.iteritems():
#if name != "Acarbose":
# continue
# Get drugbank id from name in the label
drugbank_id, drugbank_name = parse_drugbank.get_drugbank_id_from_name(name, name_to_drug, synonym_to_drug)
if drugbank_id is None:
continue
#if drugbank_id != "DB00284":
# continue
#print "%s\t%s\t%s" % (drugbank_name, drugbank_id, name)
for icd, val in values:
icd = icd.split(".")[0]
#print icd, val, icd in icd_to_mesh_ids
if icd in icd_to_mesh_ids:
dui_list = icd_to_mesh_ids[icd]
#print dui_list
for dui in dui_list:
if dui not in mesh_id_to_name:
continue
phenotype = mesh_id_to_name[dui].lower()
#print dui, phenotype
drug_to_diseases.setdefault(drugbank_id, set()).add((phenotype, dui, val))
#else:
# ICD9 ids are problematic - consider name matching
if dump_file is not None:
cPickle.dump(drug_to_diseases, open(dump_file, 'w'))
return drug_to_diseases
def get_drug_disease_mapping_from_cui(name_to_cui_and_confidences, name_to_drug, synonym_to_drug, concept_id_to_mesh_id, mesh_id_to_name, dump_file):
"""
name_to_cui_and_confidences mapping from MEDI
name_to_drug & synonym mapping from DrugBank
concept & mesh_id mapping from UMLS (MSH | MH)
"""
if dump_file is not None and os.path.exists(dump_file):
drug_to_diseases = cPickle.load(open(dump_file))
return drug_to_diseases
drug_to_diseases = {} # (mesh_term, mesh_id, association score)
for name, values in name_to_cui_and_confidences.iteritems():
# Get drugbank id from name in the label
drugbank_id, drugbank_name = parse_drugbank.get_drugbank_id_from_name(name, name_to_drug, synonym_to_drug)
if drugbank_id is None:
continue
print "%s\t%s\t%s" % (drugbank_name, drugbank_id, name)
for cui, val in values:
#print cui, val, cui in concept_id_to_mesh_id
if cui in concept_id_to_mesh_id:
dui = concept_id_to_mesh_id[cui]
phenotype = mesh_id_to_name[dui]
drug_to_diseases.setdefault(drugbank_id, set()).add((phenotype, dui, val))
if dump_file is not None:
cPickle.dump(drug_to_diseases, open(dump_file, 'w'))
return drug_to_diseases
def get_medi_mapping(mapping_file, textual_indication=False):
"""
RXCUI_IN,DRUG_DESC,ICD9,INDICATION_DESCRIPTION,MENTIONEDBYRESOURCES,HIGHPRECISIONSUBSET,POSSIBLE_LABEL_USE
44,Mesna,599.7,Hematuria,1,0,0
"""
name_to_icd_and_confidences = {}
f = open(mapping_file)
header = f.readline()
for line in f:
words = line.strip().split(",")
name = words[1]
icd = words[2]
if textual_indication:
icd = words[3]
in_hps = words[5]
confidence = 0.5
if in_hps == "1":
confidence = 1
name_to_icd_and_confidences.setdefault(name, []).append((icd, confidence))
f.close()
return name_to_icd_and_confidences
def get_medi_mapping_umls(mapping_file):
"""
RXCUI_IN,STR,CUI,ATC,NDDF,CODE,HSP
44,mesna,C0000294,R05CB05,2678,595,1
"""
name_to_icd_and_confidences = {}
f = open(mapping_file)
header = f.readline()
for line in f:
words = line.strip().split(",")
name = words[1]
icd = words[-2]
in_hps = words[-1]
confidence = 0.5
if in_hps == "1":
confidence = 1
name_to_icd_and_confidences.setdefault(name, []).append((icd, confidence))
f.close()
return name_to_icd_and_confidences
def get_medi_mapping_old(mapping_file, textual_indication=False):
"""
RXCUI_IN,DRUG_DESC,UMLS_CUI,ICD9,INDICATION_DESCRIPTION,MENTIONEDBYRESOURCES,HIGHPRECISIONSUBSET,POSSIBLE_LABEL_USE
44,Mesna,C0018965,599.7,Hematuria,1,0,0
"""
name_to_cui_and_confidences = {}
f = open(mapping_file)
header = f.readline()
for line in f:
words = line.strip().split(",")
name = words[1]
cui = words[2]
if textual_indication:
cui = words[3]
in_hps = words[6]
confidence = 0.5
if in_hps == "1":
confidence = 1
name_to_cui_and_confidences.setdefault(name, []).append((cui, confidence))
f.close()
return name_to_cui_and_confidences
if __name__ == "__main__":
main()