-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathSMEDrugRepurposingFisher_15.py
313 lines (278 loc) · 12.8 KB
/
SMEDrugRepurposingFisher_15.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
# solves the SME workflow #1: drug repurposing based on rare diseases
import os
import sys
import argparse
# PyCharm doesn't play well with relative imports + python console + terminal
try:
from code.reasoningtool import ReasoningUtilities as RU
except ImportError:
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import ReasoningUtilities as RU
import FormatOutput
import networkx as nx
try:
from QueryCOHD import QueryCOHD
except ImportError:
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
try:
from QueryCOHD import QueryCOHD
except ImportError:
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'kg-construction'))
from QueryCOHD import QueryCOHD
from COHDUtilities import COHDUtilities
import SimilarNodesInCommon
import CustomExceptions
import numpy as np
import fisher_exact
import NormGoogleDistance
NormGoogleDistance = NormGoogleDistance.NormGoogleDistance()
# TODO: Temp file path names etc
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'MLDrugRepurposing/FWPredictor'))
import predictor
p = predictor.predictor(model_file=os.path.join(os.path.dirname(os.path.abspath(__file__)), 'MLDrugRepurposing/FWPredictor/LogModel.pkl'))
p.import_file(None, graph_file=os.path.join(os.path.dirname(os.path.abspath(__file__)), 'MLDrugRepurposing/FWPredictor/rel_max.emb.gz'), map_file=os.path.join(os.path.dirname(os.path.abspath(__file__)), 'MLDrugRepurposing/FWPredictor/map.csv'))
class SMEDrugRepurposingFisher:
def __init__(self):
None
@staticmethod
def answer(disease_id, use_json=False, num_show=25):
num_input_disease_symptoms = 15 # number of representative symptoms of the disease to keep
num_omim_keep = 15 # number of genetic conditions to keep
num_protein_keep = 15 # number of implicated proteins to keep
num_pathways_keep = 15 # number of pathways to keep
num_pathway_proteins_selected = 15 # number of proteins enriched for the above pathways to select
num_drugs_keep = 2*num_show # number of drugs that target those proteins to keep
num_paths = 2 # number of paths to keep for each drug selected
# Initialize the response class
response = FormatOutput.FormatResponse(6)
response.response.table_column_names = ["disease name", "disease ID", "drug name", "drug ID", "confidence"]
# get the description of the disease
disease_description = RU.get_node_property(disease_id, 'name')
# Find symptoms of disease
# symptoms = RU.get_one_hop_target("disease", disease_id, "phenotypic_feature", "has_phenotype")
# symptoms_set = set(symptoms)
(symptoms_dict, symptoms) = RU.top_n_fisher_exact([disease_id], "disease", "phenotypic_feature", rel_type="has_phenotype", n=num_input_disease_symptoms)
symptoms_set = set(symptoms)
# check for an error
if not symptoms_set:
error_message = "I found no phenotypic_features for %s." % disease_description
if not use_json:
print(error_message)
return
else:
error_code = "NoPathsFound"
response = FormatOutput.FormatResponse(3)
response.add_error_message(error_code, error_message)
response.print()
return
# Find diseases enriched for that phenotype
path_type = ["gene_mutations_contribute_to", "protein", "participates_in", "pathway", "participates_in",
"protein", "physically_interacts_with", "chemical_substance"]
(genetic_diseases_dict, genetic_diseases_selected) = RU.top_n_fisher_exact(symptoms, "phenotypic_feature",
"disease", rel_type="has_phenotype",
n=num_omim_keep, curie_prefix="OMIM",
on_path=path_type,
exclude=disease_id)
if not genetic_diseases_selected:
error_message = "I found no diseases connected to phenotypes of %s." % disease_description
if not use_json:
print(error_message)
return
else:
error_code = "NoPathsFound"
response = FormatOutput.FormatResponse(3)
response.add_error_message(error_code, error_message)
response.print()
return
# find the most representative proteins in these diseases
path_type = ["participates_in", "pathway", "participates_in",
"protein", "physically_interacts_with", "chemical_substance"]
(implicated_proteins_dict, implicated_proteins_selected) = RU.top_n_fisher_exact(genetic_diseases_selected,
"disease", "protein",
rel_type="gene_mutations_contribute_to",
n=num_protein_keep,
on_path=path_type)
if not implicated_proteins_selected:
error_message = "I found no proteins connected to diseases connected to phenotypes of %s." % disease_description
if not use_json:
print(error_message)
return
else:
error_code = "NoPathsFound"
response = FormatOutput.FormatResponse(3)
response.add_error_message(error_code, error_message)
response.print()
return
# find enriched pathways from those proteins
path_type = ["participates_in", "protein", "physically_interacts_with", "chemical_substance"]
(pathways_selected_dict, pathways_selected) = RU.top_n_fisher_exact(implicated_proteins_selected, "protein",
"pathway", rel_type="participates_in",
n=num_pathways_keep, on_path=path_type)
if not pathways_selected:
error_message = "I found no pathways connected to proteins connected to diseases connected to phenotypes of %s." % disease_description
if not use_json:
print(error_message)
return
else:
error_code = "NoPathsFound"
response = FormatOutput.FormatResponse(3)
response.add_error_message(error_code, error_message)
response.print()
return
# find proteins enriched for those pathways
path_type = ["physically_interacts_with", "chemical_substance"]
(pathway_proteins_dict, pathway_proteins_selected) = RU.top_n_fisher_exact(pathways_selected, "pathway",
"protein",
rel_type="participates_in",
n=num_pathway_proteins_selected,
on_path=path_type)
if not pathway_proteins_selected:
error_message = "I found no proteins connected to pathways connected to proteins connected to diseases connected to phenotypes of %s." % disease_description
if not use_json:
print(error_message)
return
else:
error_code = "NoPathsFound"
response = FormatOutput.FormatResponse(3)
response.add_error_message(error_code, error_message)
response.print()
return
# find drugs enriched for targeting those proteins
(drugs_selected_dict, drugs_selected) = RU.top_n_fisher_exact(pathway_proteins_selected, "protein",
"chemical_substance",
rel_type="physically_interacts_with",
n=num_drugs_keep)
if not drugs_selected:
error_message = "I found no drugs connected toproteins connected to pathways connected to proteins connected to diseases connected to phenotypes of %s." % disease_description
if not use_json:
print(error_message)
return
else:
error_code = "NoPathsFound"
response = FormatOutput.FormatResponse(3)
response.add_error_message(error_code, error_message)
response.print()
return
path_type = ["disease", "has_phenotype", "phenotypic_feature", "has_phenotype", "disease",
"gene_mutations_contribute_to", "protein", "participates_in", "pathway", "participates_in",
"protein", "physically_interacts_with", "chemical_substance"]
g = RU.get_subgraph_through_node_sets_known_relationships(path_type,
[[disease_id], symptoms, genetic_diseases_selected,
implicated_proteins_selected, pathways_selected,
pathway_proteins_selected, drugs_selected],
directed=True)
graph_weight_tuples = []
for drug in drugs_selected:
# get the relevant subgraph from this drug back to the input disease
node_types = ["disease", "phenotypic_feature", "disease", "protein", "pathway", "protein",
"chemical_substance"]
drug_pathway_protein_neighbors = RU.one_hope_neighbors_of_type(g, drug, 'protein', 'R')
drug_pathway_neighbors = set()
for protein in drug_pathway_protein_neighbors:
drug_pathway_neighbors.update(RU.one_hope_neighbors_of_type(g, protein, 'pathway', 'R'))
drug_protein_neighbors = set()
for pathway in drug_pathway_neighbors:
drug_protein_neighbors.update(RU.one_hope_neighbors_of_type(g, pathway, 'protein', 'L'))
drug_disease_neighbors = set()
for protein in drug_protein_neighbors:
drug_disease_neighbors.update(RU.one_hope_neighbors_of_type(g, protein, 'disease', 'R'))
drug_phenotype = set()
for disease in drug_disease_neighbors:
drug_phenotype.update(RU.one_hope_neighbors_of_type(g, disease, 'phenotypic_feature', 'R'))
g2 = RU.get_subgraph_through_node_sets_known_relationships(path_type,
[[disease_id], drug_phenotype,
drug_disease_neighbors,
drug_protein_neighbors, drug_pathway_neighbors,
drug_pathway_protein_neighbors, [drug]],
directed=False)
drug_id_old_curie = drug.replace("CHEMBL.COMPOUND:CHEMBL", "ChEMBL:")
# Machine learning probability of "treats"
prob = p.prob_single(drug_id_old_curie, disease_id)
if not prob:
prob = -1
else:
prob = prob[0]
graph_weight_tuples.append((g, prob, drug))
# sort by the path weight
graph_weight_tuples.sort(key=lambda x: x[1], reverse=True)
# print out the results
if not use_json:
num_shown = 0
for graph, weight, drug_id in graph_weight_tuples:
num_shown += 1
if num_shown > num_show:
break
drug_description = RU.get_node_property(drug_id, "name", node_label="chemical_substance")
drug_id_old_curie = drug_id.replace("CHEMBL.COMPOUND:CHEMBL", "ChEMBL:")
# Machine learning probability of "treats"
prob = p.prob_single(drug_id_old_curie, disease_id)
if not prob:
prob = -1
else:
prob = prob[0]
print("%s %f %f" % (drug_description, weight, prob))
else:
# add the neighborhood graph
response.add_neighborhood_graph(g.nodes(data=True), g.edges(data=True))
response.response.table_column_names = ["disease name", "disease ID", "drug name", "drug ID", "path weight",
"drug disease google distance",
"ML probability drug treats disease"]
num_shown = 0
for graph, weight, drug_id in graph_weight_tuples:
num_shown += 1
if num_shown > num_show:
break
drug_description = RU.get_node_property(drug_id, "name", node_label="chemical_substance")
drug_id_old_curie = drug_id.replace("CHEMBL.COMPOUND:CHEMBL", "ChEMBL:")
# Machine learning probability of "treats"
prob = p.prob_single(drug_id_old_curie, disease_id)
if not prob:
prob = -1
else:
prob = prob[0]
confidence = prob
# Google distance
gd = NormGoogleDistance.get_ngd_for_all([drug_id, disease_id], [drug_description, disease_description])
# populate the graph
res = response.add_subgraph(graph.nodes(data=True), graph.edges(data=True),
"The drug %s is predicted to treat %s." % (
drug_description, disease_description), confidence,
return_result=True)
res.essence = "%s" % drug_description # populate with essence of question result
row_data = [] # initialize the row data
row_data.append("%s" % disease_description)
row_data.append("%s" % disease_id)
row_data.append("%s" % drug_description)
row_data.append("%s" % drug_id)
row_data.append("%f" % weight)
row_data.append("%f" % gd)
row_data.append("%f" % prob)
res.row_data = row_data
response.print()
@staticmethod
def describe():
output = "Answers questions of the form: 'What are some potential treatments for $disease?'" + "\n"
# TODO: subsample disease nodes
return output
def main():
parser = argparse.ArgumentParser(description="Answers questions of the form: 'What are some potential treatments for $disease?'",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-d', '--disease', type=str, help="disease curie ID", default="OMIM:603903")
parser.add_argument('-j', '--json', action='store_true', help='Flag specifying that results should be printed in JSON format (to stdout)', default=False)
parser.add_argument('--describe', action='store_true', help='Print a description of the question to stdout and quit', default=False)
parser.add_argument('--num_show', type=int, help='Maximum number of results to return', default=25)
# Parse and check args
args = parser.parse_args()
disease_id = args.disease
use_json = args.json
describe_flag = args.describe
num_show = args.num_show
# Initialize the question class
Q = SMEDrugRepurposingFisher()
if describe_flag:
res = Q.describe()
print(res)
else:
Q.answer(disease_id, use_json=use_json, num_show=num_show)
if __name__ == "__main__":
main()