10
|
1 #!/usr/bin/env python
|
|
2 import os
|
|
3 import sys
|
|
4 import subprocess
|
|
5
|
|
6 DATASETS = [
|
|
7 'artificial',
|
|
8 'artificial-samples',
|
|
9 'artificial-nofilt',
|
|
10 'real',
|
|
11 'real-mit',
|
|
12 'real-mit-s',
|
|
13 'real-nofilt',
|
|
14 ]
|
|
15 IN_EXT = '.vcf.in'
|
|
16 OUT_EXT = '.csv.out'
|
|
17 ARGS_KEY = '##comment="ARGS='
|
|
18
|
11
|
19 XML = {
|
|
20 'tests_start':' <tests>',
|
|
21 'test_start': ' <test>',
|
|
22 'input': ' <param name="input" value="tests/%s" />',
|
|
23 'param': ' <param name="%s" value="%s" />',
|
|
24 'output': ' <output name="output" file="tests/%s" />',
|
|
25 'test_end': ' </test>',
|
|
26 'tests_end': ' </tests>',
|
|
27 }
|
|
28 PARAMS = {
|
|
29 '-f':'freq',
|
|
30 '-c':'covg',
|
|
31 '-H':'header',
|
|
32 '-s':'stranded',
|
|
33 '-n':'nofilt',
|
|
34 '-r':'seed',
|
|
35 }
|
|
36 PARAM_ARG = {
|
|
37 '-f':True,
|
|
38 '-c':True,
|
|
39 '-H':False,
|
|
40 '-s':False,
|
|
41 '-n':False,
|
|
42 '-r':True,
|
|
43 }
|
|
44
|
10
|
45 def main():
|
|
46
|
11
|
47 do_print_xml = False
|
|
48 if len(sys.argv) > 1:
|
|
49 if sys.argv[1] == '-x':
|
|
50 do_print_xml = True
|
|
51 else:
|
|
52 sys.stderr.write("Error: unrecognized option '"+sys.argv[1]+"'\n")
|
|
53 sys.exit(1)
|
|
54
|
10
|
55 test_dir = os.path.dirname(os.path.relpath(sys.argv[0]))
|
|
56 if test_dir:
|
|
57 test_dir += os.sep
|
|
58
|
11
|
59 if do_print_xml:
|
|
60 print XML.get('tests_start')
|
|
61
|
10
|
62 for dataset in DATASETS:
|
|
63 infile = test_dir+dataset+IN_EXT
|
|
64 outfile = test_dir+dataset+OUT_EXT
|
|
65
|
|
66 if not os.path.exists(infile):
|
|
67 sys.stderr.write("Error: file not found: "+infile+"\n")
|
|
68 continue
|
|
69 if not os.path.exists(outfile):
|
|
70 sys.stderr.write("Error: file not found: "+outfile+"\n")
|
|
71 continue
|
|
72
|
|
73 options = read_options(infile)
|
11
|
74 if do_print_xml:
|
|
75 print_xml(infile, outfile, options, XML, PARAMS, PARAM_ARG)
|
|
76 else:
|
|
77 run_tests(infile, outfile, options)
|
|
78
|
|
79 if do_print_xml:
|
|
80 print XML.get('tests_end')
|
|
81
|
|
82
|
|
83 def run_tests(infile, outfile, options):
|
|
84 script_cmd = 'allele-counts.py '+options+' -i '+infile
|
|
85 bash_cmd = 'diff '+outfile+' <('+script_cmd+')'
|
|
86 print script_cmd
|
|
87 subprocess.call(['bash', '-c', bash_cmd])
|
|
88
|
|
89
|
|
90 def print_xml(infile, outfile, options_str, xml, params, param_arg):
|
|
91 infile = os.path.basename(infile)
|
|
92 outfile = os.path.basename(outfile)
|
|
93
|
|
94 options = options_str.split() # on whitespace
|
|
95
|
|
96 print xml.get('test_start')
|
|
97 print xml.get('input') % infile
|
|
98
|
|
99 i = 0
|
|
100 while i < len(options):
|
|
101 opt = options[i]
|
|
102 if not params.has_key(opt) or not param_arg.has_key(opt):
|
|
103 sys.stderr.write("Error: unknown option '"+opt+"' in ARGS list in file "
|
|
104 +infile+"\n")
|
|
105 sys.exit(1)
|
|
106 if param_arg[opt]:
|
|
107 i+=1
|
|
108 arg = options[i]
|
|
109 print xml.get('param') % (params[opt], arg)
|
|
110 else:
|
|
111 print xml.get('param') % (params[opt], opt)
|
|
112 i+=1
|
|
113
|
|
114 print xml.get('output') % outfile
|
|
115 print xml.get('test_end')
|
10
|
116
|
|
117
|
|
118 def read_options(infile):
|
|
119 with open(infile, 'r') as infilehandle:
|
|
120 for line in infilehandle:
|
|
121 line.strip()
|
|
122 if ARGS_KEY == line[:len(ARGS_KEY)]:
|
|
123 return line[len(ARGS_KEY):-2]
|
|
124 return ''
|
|
125
|
|
126
|
|
127 if __name__ == '__main__':
|
|
128 main() |