annotate refinery_test_tool.py @ 4:ccf0e1ca6fe9 draft

Add 2-6 test tool.
author hackdna
date Mon, 13 May 2013 14:49:03 -0400
parents f725e3820fb4
children a410fb6c43fa
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:
3
f725e3820fb4 Add 2-1 and 2-2 tools. Code cleanup.
hackdna
parents: 0
diff changeset
32 out_file.write("Output file name: " + out_file.name + '\n\n')
0
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
33 out_file.write(input)
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
34 except IOError as e:
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
35 cleanup(args)
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
36 parser.error(e)
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
37 else:
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
38 cleanup(args)
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
39
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
40
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
41 def check_fail(p_fail):
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
42 '''Determine success/failure state given a probability of failure
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
43
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
44 '''
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
45 random.seed()
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
46 if random.random() < p_fail:
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
47 return True
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
48 else:
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
49 return False
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
50
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
51
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
52 def read_files(file_list):
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
53 '''Read files from disk into a string
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
54
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
55 '''
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
56 str = ''
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
57 for in_file in file_list:
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
58 str += "Input file name: " + in_file.name + '\n'
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
59 str += in_file.read()
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
60 return str
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
61
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
62
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
63 def cleanup(args):
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
64 '''Close all open file handles
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
65
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
66 '''
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
67 file_list = []
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
68 if args.input_files:
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
69 file_list.extend(args.input_files)
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
70 if args.output_files:
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
71 file_list.extend(args.output_files)
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
72 for fh in file_list:
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
73 try:
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
74 fh.close()
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
75 except AttributeError:
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
76 continue
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
77
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
78
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
79 def quit(message, args):
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
80 '''Exit and optionally write to stdout and/or stderr
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
81
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
82 '''
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
83 if args.stdout:
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
84 sys.stdout.write(message + '\n')
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
85 if args.stderr:
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
86 sys.stderr.write(message + '\n')
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
87 sys.exit(args.exit_code)
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
88
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
89
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
90 if __name__ == '__main__':
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
91 version = "%(prog)s 0.1"
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
92 description = "Test tool for running workflows on Galaxy platform from Refinery"
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
93 parser = argparse.ArgumentParser(description=description, version=version)
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
94
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
95 parser.add_argument('-i', '--in-file', dest='input_files', nargs='+',
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
96 type=file, metavar='INPUT_FILE', required=True,
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
97 help='name of the input file')
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
98 parser.add_argument('-o', '--out-file', dest='output_files', nargs='+',
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
99 type=argparse.FileType('w'), metavar='OUTPUT_FILE',
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
100 required=True, help='name of the output file')
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
101 parser.add_argument('-e', '--exit_code', type=int, default=0,
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
102 help='code to return on exit, default: %(default)s')
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
103 parser.add_argument('--stdout', action='store_true',
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
104 help='write a message to stdout')
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
105 parser.add_argument('--stderr', action='store_true',
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
106 help='write a message to stderr')
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
107 parser.add_argument('-p', '--p-fail', type=float, default=0.0,
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
108 help='probability of execution failure, default: %(default)s')
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
109 parser.add_argument('-s', '--sleep', dest='seconds', type=int, default=0,
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
110 metavar='SECONDS',
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
111 help='number of seconds to sleep, default: %(default)s')
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
112
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
113 # check argument values for errors
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
114 try:
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
115 args = parser.parse_args()
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
116 except IOError as e:
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
117 parser.error(e)
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
118
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
119 if args.exit_code < 0 or args.exit_code > 255:
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
120 cleanup(args)
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
121 parser.error("Exit code value must be between 0 and 255")
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
122
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
123 if args.p_fail < 0.0 or args.p_fail > 1.0:
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
124 cleanup(args)
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
125 parser.error("Probability value must be between 0.0 and 1.0 inclusive")
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
126
417f332cf4ef Initial upload
hackdna
parents:
diff changeset
127 main(args)