-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTOBIml.py
executable file
·141 lines (124 loc) · 3.87 KB
/
TOBIml.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
#!/usr/bin/env python
import argparse
import os
import sys
import subprocess
def get_arg():
prog_description = """TOBIv1.2:
Tumor Only Boosting Identification of Driver Mutations.
Machine learning step."""
parser = argparse.ArgumentParser(description = prog_description)
parser.add_argument(
"step",
choices = ['preprocess','machinelearning'],
help = "preprocess: preprocessing step; machinelearning: machine learning step"
)
parser.add_argument(
'--input',
help = "[REQUIRED] input file"
)
parser.add_argument(
'--output',
help = "[REQUIRED] output file"
)
parser.add_argument(
'--somatic',
help = "[REQUIRED] formatted file containing somatic variants"
)
parser.add_argument(
'--log',
help = 'Optional argument to specify a log to pipe stdout and stderr to'
)
parser.add_argument(
'--check_missed',
help = '[PP ARG] checking which mutations in important genes are missed by filtering'
)
parser.add_argument(
'--suffix',
help = '[ML ARG] a label specific to this particular run (e.g. <date>_<disease>)'
)
parser.add_argument(
"--vcftype",
choices = ['default','TCGA'],
default = "default",
help="Specifies vcf type specically for TCGA filtering"
)
parser.add_argument(
'--train_size',
help = '[ML ARG] number of patients you want in the training set.',
)
parser.add_argument(
'--verbose',
action = "store_true",
default = False,
help = "verbose flag"
)
if len(sys.argv)==1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
return args
def check_main_args(args):
if args.input == None:
sys.exit("[ERROR]: Missing required '--inputdir' argument.")
if args.output == None:
sys.exit("[ERROR]: Missing required '--output' argument.")
if args.somatic == None:
sys.exit("[ERROR]: Missing required '--somatic' argument.")
def check_ml_args(args):
if args.suffix == None:
sys.exit("[ERROR]: Missing required '--suffix' argument.")
if args.train_size == None:
sys.exit("[ERROR]: Missing required '--train_size' argument.")
def pp_cmdgen(args,source_dir):
if args.log == None:
pipe = ""
else:
pipe = ">> " + args.log + " 2>&1"
cmd = source_dir +"/machine_learning/pre_processing.R " \
+ args.input + " " \
+ args.output + " " \
+ args.somatic + " " \
+ source_dir + " " \
+ args.vcftype + " " \
+ pipe
if args.verbose:
print(cmd)
return cmd
def ml_cmdgen(args,source_dir):
if args.log == None:
pipe = ""
else:
pipe = ">> " + args.log + " 2>&1"
cmd = source_dir + "/machine_learning/machine_learning.R " \
+ args.input + " " \
+ args.output + " " \
+ args.somatic + " " \
+ args.suffix + " " \
+ args.train_size + " " \
+ source_dir + " " \
+ args.vcftype + " " \
+ pipe
if args.verbose:
print(cmd)
return cmd
def runShellCmd(cmd):
proc = subprocess.Popen(
cmd,
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
if proc.wait() != 0:
sys.exit("[ERROR] process '" + cmd + "' terminated with non-zero exit code")
def main():
args = get_arg()
source_dir = os.path.dirname(os.path.realpath(__file__))
check_main_args(args)
#preprocessing
if args.step == "preprocess":
runShellCmd(pp_cmdgen(args,source_dir))
#machine learning
if args.step == "machinelearning":
check_ml_args(args)
runShellCmd(ml_cmdgen(args,source_dir))
if __name__ == "__main__":
main()