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