annotate refinery_test_tool.py @ 10:ca9a5aa3a59a draft default tip

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