annotate ezBAMQC/src/htslib/test/test.pl @ 14:744987262771

Uploaded
author cshl-bsr
date Wed, 30 Mar 2016 12:15:03 -0400
parents dfa3745e5fd8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
1 #!/usr/bin/env perl
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
2 #
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
3 # Copyright (C) 2012-2013 Genome Research Ltd.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
4 #
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
5 # Author: Petr Danecek <pd3@sanger.ac.uk>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
6 #
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
8 # of this software and associated documentation files (the "Software"), to deal
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
9 # in the Software without restriction, including without limitation the rights
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
11 # copies of the Software, and to permit persons to whom the Software is
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
12 # furnished to do so, subject to the following conditions:
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
13 #
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
14 # The above copyright notice and this permission notice shall be included in
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
15 # all copies or substantial portions of the Software.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
16 #
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
20 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
22 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
23 # DEALINGS IN THE SOFTWARE.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
24
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
25 use strict;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
26 use warnings;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
27 use Carp;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
28 use FindBin;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
29 use lib "$FindBin::Bin";
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
30 use Getopt::Long;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
31 use File::Temp qw/ tempfile tempdir /;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
32
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
33 my $opts = parse_params();
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
34
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
35 test_vcf_api($opts,out=>'test-vcf-api.out');
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
36 test_vcf_sweep($opts,out=>'test-vcf-sweep.out');
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
37
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
38 print "\nNumber of tests:\n";
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
39 printf " total .. %d\n", $$opts{nok}+$$opts{nfailed};
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
40 printf " passed .. %d\n", $$opts{nok};
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
41 printf " failed .. %d\n", $$opts{nfailed};
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
42 print "\n";
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
43
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
44 exit ($$opts{nfailed} > 0);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
45
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
46 #--------------------
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
47
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
48 sub error
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
49 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
50 my (@msg) = @_;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
51 if ( scalar @msg ) { confess @msg; }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
52 print
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
53 "About: samtools/htslib consistency test script\n",
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
54 "Usage: test.pl [OPTIONS]\n",
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
55 "Options:\n",
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
56 " -r, --redo-outputs Recreate expected output files.\n",
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
57 " -t, --temp-dir <path> When given, temporary files will not be removed.\n",
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
58 " -h, -?, --help This help message.\n",
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
59 "\n";
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
60 exit 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
61 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
62 sub parse_params
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
63 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
64 my $opts = { keep_files=>0, nok=>0, nfailed=>0 };
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
65 my $help;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
66 Getopt::Long::Configure('bundling');
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
67 my $ret = GetOptions (
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
68 't|temp-dir:s' => \$$opts{keep_files},
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
69 'r|redo-outputs' => \$$opts{redo_outputs},
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
70 'h|?|help' => \$help
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
71 );
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
72 if ( !$ret or $help ) { error(); }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
73 $$opts{tmp} = $$opts{keep_files} ? $$opts{keep_files} : tempdir(CLEANUP=>1);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
74 if ( $$opts{keep_files} ) { cmd("mkdir -p $$opts{keep_files}"); }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
75 $$opts{path} = $FindBin::RealBin;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
76 $$opts{bin} = $FindBin::RealBin;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
77 $$opts{bin} =~ s{/test/?$}{};
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
78 return $opts;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
79 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
80 sub _cmd
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
81 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
82 my ($cmd) = @_;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
83 my $kid_io;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
84 my @out;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
85 my $pid = open($kid_io, "-|");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
86 if ( !defined $pid ) { error("Cannot fork: $!"); }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
87 if ($pid)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
88 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
89 # parent
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
90 @out = <$kid_io>;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
91 close($kid_io);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
92 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
93 else
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
94 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
95 # child
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
96 exec('/bin/bash', '-o','pipefail','-c', $cmd) or error("Cannot execute the command [/bin/sh -o pipefail -c $cmd]: $!");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
97 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
98 return ($? >> 8, join('',@out));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
99 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
100 sub cmd
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
101 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
102 my ($cmd) = @_;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
103 my ($ret,$out) = _cmd($cmd);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
104 if ( $ret ) { error("The command failed [$ret]: $cmd\n", $out); }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
105 return $out;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
106 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
107 sub test_cmd
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
108 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
109 my ($opts,%args) = @_;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
110 if ( !exists($args{out}) )
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
111 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
112 if ( !exists($args{in}) ) { error("FIXME: expected out or in key\n"); }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
113 $args{out} = "$args{in}.out";
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
114 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
115 my ($package, $filename, $line, $test)=caller(1);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
116 $test =~ s/^.+:://;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
117
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
118 print "$test:\n";
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
119 print "\t$args{cmd}\n";
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
120
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
121 my ($ret,$out) = _cmd("$args{cmd} 2>&1");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
122 if ( $ret ) { failed($opts,$test); return; }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
123 if ( $$opts{redo_outputs} && -e "$$opts{path}/$args{out}" )
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
124 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
125 rename("$$opts{path}/$args{out}","$$opts{path}/$args{out}.old");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
126 open(my $fh,'>',"$$opts{path}/$args{out}") or error("$$opts{path}/$args{out}: $!");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
127 print $fh $out;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
128 close($fh);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
129 my ($ret,$out) = _cmd("diff -q $$opts{path}/$args{out} $$opts{path}/$args{out}.old");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
130 if ( !$ret && $out eq '' ) { unlink("$$opts{path}/$args{out}.old"); }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
131 else
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
132 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
133 print "\tthe expected output changed, saving:\n";
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
134 print "\t old .. $$opts{path}/$args{out}.old\n";
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
135 print "\t new .. $$opts{path}/$args{out}\n";
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
136 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
137 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
138 my $exp = '';
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
139 if ( open(my $fh,'<',"$$opts{path}/$args{out}") )
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
140 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
141 my @exp = <$fh>;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
142 $exp = join('',@exp);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
143 close($fh);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
144 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
145 elsif ( !$$opts{redo_outputs} ) { failed($opts,$test,"$$opts{path}/$args{out}: $!"); return; }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
146
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
147 if ( $exp ne $out )
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
148 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
149 open(my $fh,'>',"$$opts{path}/$args{out}.new") or error("$$opts{path}/$args{out}.new");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
150 print $fh $out;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
151 close($fh);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
152 if ( !-e "$$opts{path}/$args{out}" )
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
153 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
154 rename("$$opts{path}/$args{out}.new","$$opts{path}/$args{out}") or error("rename $$opts{path}/$args{out}.new $$opts{path}/$args{out}: $!");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
155 print "\tthe file with expected output does not exist, creating new one:\n";
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
156 print "\t\t$$opts{path}/$args{out}\n";
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
157 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
158 else
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
159 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
160 failed($opts,$test,"The outputs differ:\n\t\t$$opts{path}/$args{out}\n\t\t$$opts{path}/$args{out}.new");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
161 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
162 return;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
163 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
164 passed($opts,$test);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
165 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
166 sub failed
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
167 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
168 my ($opts,$test,$reason) = @_;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
169 $$opts{nfailed}++;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
170 if ( defined $reason ) { print "\n\t$reason"; }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
171 print "\n.. failed ...\n\n";
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
172 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
173 sub passed
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
174 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
175 my ($opts,$test) = @_;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
176 $$opts{nok}++;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
177 print ".. ok\n\n";
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
178 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
179 sub is_file_newer
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
180 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
181 my ($afile,$bfile) = @_;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
182 my (@astat) = stat($afile) or return 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
183 my (@bstat) = stat($bfile) or return 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
184 if ( $astat[9]>$bstat[9] ) { return 1 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
185 return 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
186 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
187
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
188
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
189 # The tests --------------------------
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
190
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
191 sub test_vcf_api
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
192 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
193 my ($opts,%args) = @_;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
194 test_cmd($opts,%args,cmd=>"$$opts{path}/test-vcf-api $$opts{tmp}/test-vcf-api.bcf");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
195 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
196
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
197 sub test_vcf_sweep
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
198 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
199 my ($opts,%args) = @_;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
200 test_cmd($opts,%args,cmd=>"$$opts{path}/test-vcf-sweep $$opts{tmp}/test-vcf-api.bcf");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
201 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
202