-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGAP.py
executable file
·219 lines (179 loc) · 8.25 KB
/
GAP.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
#!/usr/bin/env python2.7
import sys
import os
import argparse
import logging
from System import GAPipeline
# Define the available platform modules
available_plat_modules = {
"Google": "GooglePlatform",
"Hardac": "SlurmPlatform",
}
def configure_argparser(argparser_obj):
def platform_type(arg_string):
value = arg_string.capitalize()
if value not in available_plat_modules:
err_msg = "%s is not a valid platform! " \
"Please view usage menu for a list of available platforms" % value
raise argparse.ArgumentTypeError(err_msg)
return available_plat_modules[value]
def file_type(arg_string):
"""
This function check both the existance of input file and the file size
:param arg_string: file name as string
:return: file name as string
"""
if not os.path.exists(arg_string):
err_msg = "%s does not exist!! " \
"Please provide a correct file!!" % arg_string
raise argparse.ArgumentTypeError(err_msg)
return arg_string
# Path to sample set config file
argparser_obj.add_argument("--input",
action="store",
#type=argparse.FileType('r'),
type=file_type,
dest="sample_set_config",
required=True,
help="Path to config file containing input files "
"and information for one or more samples.")
# Path to sample set config file
argparser_obj.add_argument("--name",
action="store",
type=str,
dest="pipeline_name",
required=True,
help="Descriptive pipeline name. Will be appended to final output dir. Should be unique across runs.")
# Path to pipeline graph config file
argparser_obj.add_argument("--pipeline_config",
action='store',
#type=argparse.FileType('r'),
type=file_type,
dest='graph_config',
required=True,
help="Path to config file defining "
"pipeline graph and tool-specific input.")
# Path to resources config file
argparser_obj.add_argument("--res_kit_config",
action='store',
#type=argparse.FileType('r'),
type=file_type,
dest='res_kit_config',
required=True,
help="Path to config file defining "
"the resources used in the pipeline.")
# Path to platform config file
argparser_obj.add_argument("--plat_config",
action='store',
#type=argparse.FileType('r'),
type=file_type,
dest='platform_config',
required=True,
help="Path to config file defining "
"platform where pipeline will execute.")
# Name of the platform module
available_plats = "\n".join(["%s (as module '%s')" % item for item in available_plat_modules.iteritems()])
argparser_obj.add_argument("--plat_name",
action='store',
type=platform_type,
dest='platform_module',
required=True,
help="Platform to be used. Possible values are:\n%s" % available_plats,)
# Verbosity level
argparser_obj.add_argument("-v",
action='count',
dest='verbosity_level',
required=False,
default=0,
help="Increase verbosity of the program."
"Multiple -v's increase the verbosity level:\n"
"0 = Errors\n"
"1 = Errors + Warnings\n"
"2 = Errors + Warnings + Info\n"
"3 = Errors + Warnings + Info + Debug")
# Final output dir
argparser_obj.add_argument("-o", "--output_dir",
action='store',
type=str,
dest="final_output_dir",
required=True,
help="Absolute path to the final output directory.")
def configure_logging(verbosity):
# Setting the format of the logs
FORMAT = "[%(asctime)s] %(levelname)s: %(message)s"
# Configuring the logging system to the lowest level
logging.basicConfig(level=logging.DEBUG, format=FORMAT, stream=sys.stderr)
# Defining the ANSI Escape characters
BOLD = '\033[1m'
DEBUG = '\033[92m'
INFO = '\033[94m'
WARNING = '\033[93m'
ERROR = '\033[91m'
END = '\033[0m'
# Coloring the log levels
if sys.stderr.isatty():
logging.addLevelName(logging.ERROR, "%s%s%s%s%s" % (BOLD, ERROR, "GAP_ERROR", END, END))
logging.addLevelName(logging.WARNING, "%s%s%s%s%s" % (BOLD, WARNING, "GAP_WARNING", END, END))
logging.addLevelName(logging.INFO, "%s%s%s%s%s" % (BOLD, INFO, "GAP_INFO", END, END))
logging.addLevelName(logging.DEBUG, "%s%s%s%s%s" % (BOLD, DEBUG, "GAP_DEBUG", END, END))
else:
logging.addLevelName(logging.ERROR, "GAP_ERROR")
logging.addLevelName(logging.WARNING, "GAP_WARNING")
logging.addLevelName(logging.INFO, "GAP_INFO")
logging.addLevelName(logging.DEBUG, "GAP_DEBUG")
# Setting the level of the logs
level = [logging.ERROR, logging.WARNING, logging.INFO, logging.DEBUG][verbosity]
logging.getLogger().setLevel(level)
def configure_import_paths():
# Get the directory of the executable
exec_dir = sys.path[0]
# Add the modules paths to the python path
sys.path.insert(1, os.path.join(exec_dir, "Modules/Tools/"))
sys.path.insert(1, os.path.join(exec_dir, "Modules/Splitters/"))
sys.path.insert(1, os.path.join(exec_dir, "Modules/Mergers/"))
# Add the available platforms to the python path
for plat in available_plat_modules:
sys.path.insert(1, os.path.join(exec_dir, "System/Platform/%s" % plat))
def main():
# Configure argparser
argparser = argparse.ArgumentParser(prog="GAP")
configure_argparser(argparser)
# Parse the arguments
args = argparser.parse_args()
# Configure logging
configure_logging(args.verbosity_level)
# Configuring the importing locations
configure_import_paths()
# Create pipeline object
pipeline = GAPipeline(pipeline_id=args.pipeline_name,
graph_config=args.graph_config,
resource_kit_config=args.res_kit_config,
sample_data_config=args.sample_set_config,
platform_config=args.platform_config,
platform_module=args.platform_module,
final_output_dir=args.final_output_dir)
# Initialize variables
err = True
err_msg = None
try:
# Load the pipeline components
pipeline.load()
# Validated pipeline inputs and configuration
pipeline.validate()
# Run the pipeline
pipeline.run()
# Indicate that pipeline completed successfully
err = False
except BaseException as e:
logging.error("Pipeline failed!")
logging.error("Pipeline failure error:\n%s" % e.message)
err_msg = e.message
pipeline.save_progress()
raise
finally:
# Generate pipeline run report
pipeline.publish_report(err=err, err_msg=err_msg)
# Clean up the pipeline. Only remove temporary output if pipeline completed successfully.
pipeline.clean_up()
if __name__ == "__main__":
main()