0
|
1 #!/usr/bin/env python
|
|
2
|
|
3 '''
|
|
4 Test tool for running Galaxy workflows from Refinery
|
|
5
|
|
6 @author: Ilya Sytchev
|
|
7
|
|
8 Input: one or more text files
|
|
9 Output: concatenated input file(s) plus annotation
|
|
10
|
|
11 Requires Python v2.7
|
|
12
|
|
13 '''
|
|
14
|
|
15
|
|
16 import argparse
|
|
17 import random
|
|
18 import sys
|
|
19 import time
|
|
20
|
|
21
|
|
22 def main(args):
|
|
23 time.sleep(args.seconds)
|
|
24
|
|
25 if check_fail(args.p_fail):
|
|
26 cleanup(args)
|
|
27 quit("Processing failed by request", args)
|
|
28
|
|
29 input = read_files(args.input_files)
|
|
30 try:
|
|
31 for out_file in args.output_files:
|
8
|
32 if args.empty_outfile:
|
|
33 output = ''
|
|
34 else:
|
|
35 output = "Output file name: " + out_file.name + "\n\n" + input
|
|
36 out_file.write(output)
|
0
|
37 except IOError as e:
|
|
38 cleanup(args)
|
|
39 parser.error(e)
|
|
40 else:
|
|
41 cleanup(args)
|
|
42
|
|
43
|
|
44 def check_fail(p_fail):
|
|
45 '''Determine success/failure state given a probability of failure
|
|
46
|
|
47 '''
|
|
48 random.seed()
|
|
49 if random.random() < p_fail:
|
|
50 return True
|
|
51 else:
|
|
52 return False
|
|
53
|
|
54
|
|
55 def read_files(file_list):
|
|
56 '''Read files from disk into a string
|
|
57
|
|
58 '''
|
|
59 str = ''
|
|
60 for in_file in file_list:
|
5
|
61 str += "Input file name: " + in_file.name + "\n\n"
|
0
|
62 str += in_file.read()
|
|
63 return str
|
|
64
|
|
65
|
|
66 def cleanup(args):
|
|
67 '''Close all open file handles
|
|
68
|
|
69 '''
|
|
70 file_list = []
|
|
71 if args.input_files:
|
|
72 file_list.extend(args.input_files)
|
|
73 if args.output_files:
|
|
74 file_list.extend(args.output_files)
|
|
75 for fh in file_list:
|
|
76 try:
|
|
77 fh.close()
|
|
78 except AttributeError:
|
|
79 continue
|
|
80
|
|
81
|
|
82 def quit(message, args):
|
|
83 '''Exit and optionally write to stdout and/or stderr
|
|
84
|
|
85 '''
|
|
86 if args.stdout:
|
|
87 sys.stdout.write(message + '\n')
|
|
88 if args.stderr:
|
|
89 sys.stderr.write(message + '\n')
|
|
90 sys.exit(args.exit_code)
|
|
91
|
|
92
|
|
93 if __name__ == '__main__':
|
|
94 version = "%(prog)s 0.1"
|
|
95 description = "Test tool for running workflows on Galaxy platform from Refinery"
|
|
96 parser = argparse.ArgumentParser(description=description, version=version)
|
|
97
|
|
98 parser.add_argument('-i', '--in-file', dest='input_files', nargs='+',
|
|
99 type=file, metavar='INPUT_FILE', required=True,
|
|
100 help='name of the input file')
|
|
101 parser.add_argument('-o', '--out-file', dest='output_files', nargs='+',
|
|
102 type=argparse.FileType('w'), metavar='OUTPUT_FILE',
|
|
103 required=True, help='name of the output file')
|
|
104 parser.add_argument('-e', '--exit_code', type=int, default=0,
|
|
105 help='code to return on exit, default: %(default)s')
|
|
106 parser.add_argument('--stdout', action='store_true',
|
|
107 help='write a message to stdout')
|
|
108 parser.add_argument('--stderr', action='store_true',
|
|
109 help='write a message to stderr')
|
8
|
110 parser.add_argument('--empty_outfile', action='store_true',
|
|
111 help='produce empty output file(s)')
|
0
|
112 parser.add_argument('-p', '--p-fail', type=float, default=0.0,
|
|
113 help='probability of execution failure, default: %(default)s')
|
|
114 parser.add_argument('-s', '--sleep', dest='seconds', type=int, default=0,
|
|
115 metavar='SECONDS',
|
|
116 help='number of seconds to sleep, default: %(default)s')
|
|
117
|
|
118 # check argument values for errors
|
|
119 try:
|
|
120 args = parser.parse_args()
|
|
121 except IOError as e:
|
|
122 parser.error(e)
|
|
123
|
|
124 if args.exit_code < 0 or args.exit_code > 255:
|
|
125 cleanup(args)
|
|
126 parser.error("Exit code value must be between 0 and 255")
|
|
127
|
|
128 if args.p_fail < 0.0 or args.p_fail > 1.0:
|
|
129 cleanup(args)
|
|
130 parser.error("Probability value must be between 0.0 and 1.0 inclusive")
|
|
131
|
|
132 main(args)
|