-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.py
executable file
·457 lines (347 loc) · 15.6 KB
/
run.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#!/usr/local/bin/python3
# This script is intended to be the one-stop-shop for running any of our algorithms/tests, on any valid dataset.
# It presents the user with a clean command line menu interface for selecting the algorithm/validation method they want to run, as well as the datasets to use.
from signal import signal, SIGINT
import subprocess
import platform
import sys
import os
try:
from termcolor import colored, cprint
termcolorMissing = False
except ImportError:
termcolorMissing = True
def cprint(m, c):
print(m)
def colored(m, c):
return m
# Interface Utility Functions
def resetScreen():
if platform.system() == "Windows":
os.system("cls")
else:
os.system("clear")
def sigint_handler(signalReceived, frame):
# Handle any cleanup here
print('\nExiting..\n')
exit(0)
def checkDependencies():
print("\nChecking for required libraries...")
if termcolorMissing:
print("Module termcolor not found. Install termcolor python module for color.")
error = False
for lib in ['networkx', 'numpy', 'scipy', 'matplotlib', 'requests']:
error = checkPipLibrary(lib) or error
if error:
choice = input("\nWould you like to try to automatically install missing dependencies? (Y/n) >>")
if choice.upper() != "Y":
print("\nExiting..\n")
sys.exit(0)
else:
print("\nInstalling dependencies..\n")
subprocess.Popen(["python3", "Scripts/setup.py"]).wait()
print("\nExiting script.. please restart it.\n")
sys.exit(0)
else:
cprint("Success.", "green")
print("\nChecking for datasets..\n")
dgf = get_disease_gene_files()
ppif = get_ppi_data_files()
if len(ppif) == 0:
cprint("No PPI network found.", "red")
choice = input("Would you like to automatically download the newest one from String-DB.org? (Y/n) >>")
if choice.upper() != "Y":
print("\nExiting..\n")
sys.exit(0)
else:
print("Installing String dataset..")
p = subprocess.Popen(["bash", "Scripts/download-human-dataset.sh"])
p.wait()
if p.returncode != 0:
cprint("Something went wrong. Exiting.", "red")
sys.exit(1)
else:
print("\nString data successfully installed.\n")
if len(dgf) == 0:
print("\n There are no disease-gene files installed. Disease gene files must be in the Data/ directory and must include '.diseasegenes' in the file name.")
print("Exiting..")
sys.exit(0)
cprint("Success.", "green")
def checkPipLibrary(lib):
print("checking for {0}".format(lib), end=" ")
error = False
try:
__import__(lib)
except ImportError:
print(colored("missing", "red"))
error = True
if not error:
print(colored("yes", "green"))
return error
# Selection/Execution Functions
def get_files_in_directory(path):
return [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
def get_ppi_data_files():
ppiDataFiles = []
for f in get_files_in_directory("Data/"):
if 'ppi' in f.split('.'):
ppiDataFiles.append("Data/" + f)
return ppiDataFiles
def get_disease_gene_files(t="diseasegenes"):
diseaseGeneFiles = []
for f in get_files_in_directory("Data/"):
if t in f.split('.'):
diseaseGeneFiles.append("Data/" + f)
return diseaseGeneFiles
def select_dataset():
resetScreen()
print("\n\nSelect the PPI network dataset you'd like to analyze:\n\n")
datasets = {}
for i, f in enumerate(get_ppi_data_files(), start=1):
datasets[i] = f
print(("\t- " + colored("{0}", "cyan") + ": {1}").format(i, f))
print("\n\n\tNot seeing your data file? Make sure it is in the Data/ directory and has '.ppi' somewhere in its name.\n\n")
choice = 0
while choice == 0 or choice > len(datasets):
try:
choice = int(input("Select a dataset: >>"))
except ValueError:
cprint("please enter a number", "red")
if choice > len(datasets):
cprint("number must be between 1 and {0}".format(len(datasets)), "red")
choice = 0
return datasets[choice]
def select_disease_gene_file():
resetScreen()
print("\n\nSelect the disease gene file you'd like to use:\n\n")
diseaseGeneFiles = {}
for i, f in enumerate(get_disease_gene_files(), start=1):
diseaseGeneFiles[i] = f
print(("\t- " + colored("{0}", "cyan") + ": {1}").format(i, f))
print("\n\n\tNot seeing your data file? Make sure it is in the Data/ directory and has '.diseasegenes' somewhere in its name.\n\n")
choice = 0
while choice == 0 or choice > len(diseaseGeneFiles):
try:
choice = int(input("Select a disease gene file: >>"))
except ValueError:
cprint("please enter a number", "red")
if choice > len(diseaseGeneFiles):
cprint("number must be between 1 and {0}".format(len(diseaseGeneFiles)), "red")
choice = 0
return diseaseGeneFiles[choice]
def select_program():
resetScreen()
print("\n\nSelect the task you'd like to perform:\n\n")
print("\t- " + colored("1", "cyan") + ": Run an algorithm")
print("\t- " + colored("2", "cyan") + ": Validation test")
print("\n\n")
programs = {
1:"algorithm",
2:"validation"
}
choice = 0
while choice == 0:
try:
choice = int(input("Select a task: >>"))
except ValueError:
cprint("please enter a number", "red")
if choice > 2:
cprint("number must be 1 or 2", "red")
choice = 0
return programs[choice]
def select_algorithm(all=False):
resetScreen()
print("\n\nSelect the algorithm you'd like to run:\n\n")
cprint("---ALGORITHMS---\n", "green")
print("\t- " + colored("1", "cyan") + ": Diffusion kernel")
print("\t- " + colored("2", "cyan") + ": PageRank")
print("\t- " + colored("3", "cyan") + ": Random walk with restart")
if all:
print("\t- " + colored("4", "cyan") + ": All algorithms")
print("\n\n")
algorithms = {
1:"Algorithms/DiffusionKernel.py",
2:"Algorithms/PageRank.py",
3:"Algorithms/RandomWalk.py"
}
if all:
algorithms[4] = "All"
choice = 0
while choice == 0:
try:
choice = int(input("Select an algorithm: >>"))
except ValueError:
cprint("please enter a number", "red")
if choice > len(algorithms):
cprint("number must be between 1 and {0}".format(len(algorithms)), "red")
choice = 0
if algorithms[choice] == "Algorithms/DiffusionKernel.py":
numeric = select_beta_value()
if algorithms[choice] == "Algorithms/PageRank.py":
numeric = select_pr_beta_value()
if algorithms[choice] == "Algorithms/RandomWalk.py":
numeric = select_rwr_r_value()
if algorithms[choice] == "All":
numeric = select_rwr_r_value(all=True)
return algorithms[choice], numeric
def select_prior_bias():
resetScreen()
print("\n\nPageRank allows you to use a non-uniform prior bias vector.\nSelect the prior bias file you'd like to use:\n\n")
priorBiasFiles = {}
priorBiasFiles[1] = "None"
print(("\t- " + colored("1", "cyan") + ": None"))
for i, f in enumerate(get_disease_gene_files(t="priors"), start=2):
priorBiasFiles[i] = f
print(("\t- " + colored("{0}", "cyan") + ": {1}").format(i, f))
print("\n\n\tNot seeing your data file? Make sure it is in the Data/ directory and has '.priors' somewhere in its name.\n\n")
choice = 0
while choice == 0 or choice > len(priorBiasFiles):
try:
choice = int(input("Select a prior bias file: >>"))
except ValueError:
cprint("please enter a number", "red")
if choice > len(priorBiasFiles):
cprint("number must be between 1 and {0}".format(
len(priorBiasFiles)), "red")
choice = 0
return priorBiasFiles[choice]
def select_beta_value():
resetScreen()
print("\nDiffusion kernel allows you to specify a beta value that controls the spread of the algorithm through the graph.\nA value of 0 prioritizes the disease genes (center) highest, larger values increase the influence of further nodes.")
print("\nPlease enter a beta value between " + colored("0", "cyan") + " and " + colored("2", "cyan") + ".")
choice = float("inf")
while choice < 0 or choice > 2:
try:
choice = float(input("Select beta: >>"))
except ValueError:
cprint("please enter a decimal number between 0 and 2", "red")
continue
if choice < 0 or choice > 2:
cprint("please enter a decimal number between 0 and 2", "red")
choice = float("inf")
return choice
def select_pr_beta_value():
resetScreen()
print("\nPageRank allows you to specify a beta value that sets the probability of restarting from a known disease gene.\nA value of 0 means the algorithm will never 'restart', while a value of 1 means that the algorithm will only ever visit known disease genes. (always restart)\nWe have found through ROC analysis that an r value around .4 yields the best results.")
print("\nPlease enter a beta value between " + colored("0", "cyan") + " and " + colored("1", "cyan") + ".")
choice = float("inf")
while choice < 0 or choice > 1:
try:
choice = float(input("Select beta: >>"))
except ValueError:
cprint("please enter a decimal number between 0 and 1", "red")
continue
if choice < 0 or choice > 1:
cprint("please enter a decimal number between 0 and 1", "red")
choice = float("inf")
return choice
def select_rwr_r_value(all=False):
resetScreen()
print("\nRandom Walk with Restart allows you to specify an R value that sets the probability of restarting from a known disease gene.\nA value of 0 means the algorithm will never 'restart', while a value of 1 means that the algorithm will only ever visit known disease genes. (always restart)\nWe have found through ROC analysis that an r value around .4 yields the best results.")
if all:
print(colored("\n\t-- ", "red") + "leave-one-out validation will use this value as the numeric value for all algorithms.")
print("\nPlease enter an R value between " + colored("0", "cyan") + " and " + colored("1", "cyan") + ".")
choice = float("inf")
while choice < 0 or choice > 1:
try:
choice = float(input("Select R: >>"))
except ValueError:
cprint("please enter a decimal number between 0 and 1", "red")
continue
if choice < 0 or choice > 1:
cprint("please enter a decimal number between 0 and 1", "red")
choice = float("inf")
return choice
def select_validation():
resetScreen()
print("\n\nSelect the validation method you'd like to use:\n\n")
cprint("---VALIDATION---\n", "green")
print("\t- " + colored("1", "cyan") + ": Area under ROC curve")
print("\t- " + colored("2", "cyan") + ": Leave one out cross validation")
print("\n\n")
validations = {
1:"Validation/areaUnderROC.py",
2:"Validation/leaveOneOut.py"
}
choice = 0
while choice == 0 or choice > len(validations):
try:
choice = int(input("Select a validation method: >>"))
except ValueError:
cprint("please enter a number", "red")
if choice > len(validations):
cprint("number must be between 1 and {0}".format(len(validations)), "red")
choice = 0
return validations[choice]
def select_output_file():
resetScreen()
print("\n----OUTPUT----\n")
print("Please enter a name for your output file - this file will appear in the Results directory.\n\n")
name = input("Name: >>").strip()
return "Results/" + name
def main():
# Initialization
signal(SIGINT, sigint_handler)
resetScreen()
# Resource demand warning
print("----Disease Gene Prioritization Script----")
print(colored("\nWarning:", "red"), "This script eats up a lot of resources!\nDo not run without at least 32GB of RAM, and a multi-core processor will make your life better.")
input(colored("\nPress enter to continue, ctrl+c to quit: >>", "green"))
#Check for dependencies
checkDependencies()
# Get user selections for algorithm/validation they want to run
program = select_program()
if program == "algorithm":
algorithm, numeric = select_algorithm()
ppiDataset = select_dataset()
diseaseGeneFile = select_disease_gene_file()
if algorithm == "Algorithms/PageRank.py":
priorBiasFile = select_prior_bias()
if priorBiasFile == "None":
priorBiasFile = diseaseGeneFile
if program == "validation":
validation = select_validation()
if validation == "Validation/leaveOneOut.py":
algorithm, numeric = select_algorithm(all=True)
ppiDataset = select_dataset()
diseaseGeneFile = select_disease_gene_file()
else:
ppiDataset = select_dataset()
# Create output file:
if program == "algorithm":
outputFile = select_output_file() + ".csv"
elif validation == "Validation/leaveOneOut.py":
outputFile = select_output_file() + ".txt"
# Confirm user selections
resetScreen()
if program == "algorithm":
cprint("----Algorithm----", "green")
print((colored("\nRunning:\t\t", "yellow") + "{0}" + colored("\n on dataset:\t\t", "yellow") + "{1}" + colored("\n using disease genes:\t", "yellow") + "{2}").format(algorithm, ppiDataset, diseaseGeneFile))
if algorithm == "Algorithms/PageRank.py":
print((colored(" prior bias file:\t", "yellow") + "{0}").format(priorBiasFile))
print(colored("\nSaving results to:\t", "yellow") + outputFile)
input(colored("\nPress enter to continue (ctrl+c to cancel)..", "green"))
if program == "validation":
cprint("----Validation Test----", "green")
if validation == "Validation/leaveOneOut.py":
print((colored("\nRunning:\t\t", "yellow") + "{0}" + colored("\n on dataset:\t\t", "yellow") + "{1}" + colored("\n using disease genes:\t", "yellow") + "{2}" + colored("\n\nValidating with:\t", "yellow") + "{3}").format(algorithm, ppiDataset, diseaseGeneFile, validation))
print(colored("\nSaving results to:\t", "yellow") + outputFile)
input(colored("\nPress enter to continue (ctrl+c to cancel)..", "green"))
else:
cprint("\nGenerating area under ROC curves", "green")
print(colored("\nSaving results to:\t", "yellow") + "Results folder")
input(colored("\nPress enter to continue (ctrl+c to cancel)..", "green"))
# Run stuff
if program == "algorithm":
cmd = "python3 {0} {1} {2} {3} {4}".format(algorithm, ppiDataset, diseaseGeneFile, numeric, outputFile)
if algorithm == "Algorithms/PageRank.py":
cmd = "python3 {0} {1} {2} {3} {4} {5}".format(algorithm, ppiDataset, diseaseGeneFile, priorBiasFile, numeric, outputFile)
os.system(cmd)
elif validation == "Validation/leaveOneOut.py":
cmd = "python3 {0} {1} {2} {3} {4} {5}".format(validation, algorithm, ppiDataset, diseaseGeneFile, numeric, outputFile)
os.system(cmd)
else:
cmd = "python3 {0} {1}".format(validation, ppiDataset)
os.system(cmd)
if __name__ == "__main__":
main()