Skip to content

Commit

Permalink
Add synced reader region tests, and move no-index tests
Browse files Browse the repository at this point in the history
Add some tests to exercise the --regions / --targets synced reader
options.  Currently this only includes tests for the chromosomes
with [:-] characters in the name, but it could be expanded easily
to do others.  Test files have been borrowed from pull request
samtools/bcftools#1938.

Move the synced reader no-index tests from test-bcf-sr.pl to
test.pl.  The former isn't a good place for them as it gets
called 10 times, but the no-index test only needs to run once.
It also allows the code running the test to be simplified a bit.

Also fix the exit code on test-bcf-sr.pl failure from -1 to 1.

Co-authored-by: Petr Danecek <[email protected]>
  • Loading branch information
2 people authored and jkbonfield committed Jun 27, 2023
1 parent 28a8082 commit e86126b
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 59 deletions.
9 changes: 9 additions & 0 deletions test/bcf-sr/weird-chr-names.1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
##fileformat=VCFv4.3
##FILTER=<ID=PASS,Description="All filters passed">
##reference=ref.fa
##contig=<ID=1>
##contig=<ID=1:1>
##contig=<ID=1:1-1>
#CHROM POS ID REF ALT QUAL FILTER INFO
1 1 . C T . . .
1 2 . C T . . .
8 changes: 8 additions & 0 deletions test/bcf-sr/weird-chr-names.2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
##fileformat=VCFv4.3
##FILTER=<ID=PASS,Description="All filters passed">
##reference=ref.fa
##contig=<ID=1>
##contig=<ID=1:1>
##contig=<ID=1:1-1>
#CHROM POS ID REF ALT QUAL FILTER INFO
1 1 . C T . . .
9 changes: 9 additions & 0 deletions test/bcf-sr/weird-chr-names.3.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
##fileformat=VCFv4.3
##FILTER=<ID=PASS,Description="All filters passed">
##reference=ref.fa
##contig=<ID=1>
##contig=<ID=1:1>
##contig=<ID=1:1-1>
#CHROM POS ID REF ALT QUAL FILTER INFO
1:1 1 . C T . . .
1:1 2 . C T . . .
8 changes: 8 additions & 0 deletions test/bcf-sr/weird-chr-names.4.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
##fileformat=VCFv4.3
##FILTER=<ID=PASS,Description="All filters passed">
##reference=ref.fa
##contig=<ID=1>
##contig=<ID=1:1>
##contig=<ID=1:1-1>
#CHROM POS ID REF ALT QUAL FILTER INFO
1:1 1 . C T . . .
9 changes: 9 additions & 0 deletions test/bcf-sr/weird-chr-names.5.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
##fileformat=VCFv4.3
##FILTER=<ID=PASS,Description="All filters passed">
##reference=ref.fa
##contig=<ID=1>
##contig=<ID=1:1>
##contig=<ID=1:1-1>
#CHROM POS ID REF ALT QUAL FILTER INFO
1:1-1 1 . C T . . .
1:1-1 2 . C T . . .
8 changes: 8 additions & 0 deletions test/bcf-sr/weird-chr-names.6.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
##fileformat=VCFv4.3
##FILTER=<ID=PASS,Description="All filters passed">
##reference=ref.fa
##contig=<ID=1>
##contig=<ID=1:1>
##contig=<ID=1:1-1>
#CHROM POS ID REF ALT QUAL FILTER INFO
1:1-1 1 . C T . . .
12 changes: 12 additions & 0 deletions test/bcf-sr/weird-chr-names.vcf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
##fileformat=VCFv4.3
##reference=ref.fa
##contig=<ID=1>
##contig=<ID=1:1>
##contig=<ID=1:1-1>
#CHROM POS ID REF ALT QUAL FILTER INFO
1 1 . C T . . .
1 2 . C T . . .
1:1 1 . C T . . .
1:1 2 . C T . . .
1:1-1 1 . C T . . .
1:1-1 2 . C T . . .
60 changes: 1 addition & 59 deletions test/test-bcf-sr.pl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

my $opts = parse_params();
run_test($opts);
test_no_index($opts);

exit;

Expand All @@ -58,7 +57,7 @@ sub error
" -v, --verbose \n",
" -h, -?, --help This help message\n",
"\n";
exit -1;
exit 1;
}
sub parse_params
{
Expand Down Expand Up @@ -572,60 +571,3 @@ sub pairing_score
}
return (1<<(28+$min)) + $cnt;
}

sub test_no_index {
my ($opts) = @_;

my $vcfdir = "$FindBin::Bin/bcf-sr";
if ($^O =~ /^msys/) {
$vcfdir = `cygpath -w $vcfdir`;
$vcfdir =~ s/\r?\n//;
$vcfdir =~ s/\\/\\\\/g;
}

# Positive test
open(my $fh, '>', "$$opts{tmp}/no_index_1.txt")
|| error("$$opts{tmp}/no_index_1.txt : $!");
print $fh "$vcfdir/merge.noidx.a.vcf\n";
print $fh "$vcfdir/merge.noidx.b.vcf\n";
print $fh "$vcfdir/merge.noidx.c.vcf\n";
close($fh) || error("$$opts{tmp}/no_index_1.txt : $!");

my $cmd = "$FindBin::Bin/test-bcf-sr --no-index -p all $$opts{tmp}/no_index_1.txt > $$opts{tmp}/no_index_1.out 2> $$opts{tmp}/no_index_1.err";
my ($ret) = _cmd($cmd);
if ($ret) {
error("The command failed [$ret]: $cmd\n");
}

if ($^O =~ /^msys/) {
cmd("diff --strip-trailing-cr $vcfdir/merge.noidx.abc.expected.out $$opts{tmp}/no_index_1.out");
} else {
cmd("cmp $vcfdir/merge.noidx.abc.expected.out $$opts{tmp}/no_index_1.out");
}

# Check bad input detection

my @bad_file_tests = (["out-of-order header",
["merge.noidx.a.vcf", "merge.noidx.hdr_order.vcf"]],
["out-of-order records",
["merge.noidx.a.vcf", "merge.noidx.rec_order.vcf"]],
["out-of-order records",
["merge.noidx.rec_order.vcf", "merge.noidx.a.vcf"]]);
my $count = 2;
foreach my $test_params (@bad_file_tests) {
my ($badness, $inputs) = @$test_params;
open($fh, '>', "$$opts{tmp}/no_index_$count.txt")
|| error("$$opts{tmp}/no_index_$count.txt : $!");
foreach my $input (@$inputs) {
print $fh "$vcfdir/$input\n";
}
close($fh) || error("$$opts{tmp}/no_index_$count.txt : $!");

$cmd = "$FindBin::Bin/test-bcf-sr --no-index -p all $$opts{tmp}/no_index_$count.txt > $$opts{tmp}/no_index_$count.out 2> $$opts{tmp}/no_index_$count.err";
my ($ret) = _cmd($cmd);
if ($ret == 0) {
error("Failed to detect $badness: $cmd\n");
}
$count++;
}
}
109 changes: 109 additions & 0 deletions test/test.pl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
test_vcf_sweep($opts,out=>'test-vcf-sweep.out');
test_vcf_various($opts);
test_bcf_sr_sort($opts);
test_bcf_sr_no_index($opts);
test_bcf_sr_range($opts);
test_command($opts,cmd=>'test-bcf-translate -',out=>'test-bcf-translate.out');
test_convert_padded_header($opts);
test_rebgzip($opts);
Expand Down Expand Up @@ -1033,6 +1035,113 @@ sub test_bcf_sr_sort
}
}

sub test_bcf_sr_no_index {
my ($opts) = @_;

my $test = "test_bcf_sr_no_index";

my $vcfdir = "$$opts{path}/bcf-sr";

# Positive test
test_cmd($opts, out => "bcf-sr/merge.noidx.abc.expected.out",
cmd => "$$opts{path}/test-bcf-sr --no-index -p all --args $vcfdir/merge.noidx.a.vcf $vcfdir/merge.noidx.b.vcf $vcfdir/merge.noidx.c.vcf 2> $$opts{tmp}/no_index_1.err");

# Check bad input detection

my @bad_file_tests = (["out-of-order header",
["merge.noidx.a.vcf", "merge.noidx.hdr_order.vcf"]],
["out-of-order records",
["merge.noidx.a.vcf", "merge.noidx.rec_order.vcf"]],
["out-of-order records",
["merge.noidx.rec_order.vcf", "merge.noidx.a.vcf"]]);
my $count = 2;
foreach my $test_params (@bad_file_tests) {
my ($badness, $inputs) = @$test_params;
my @ins = map { "$vcfdir/$_" } @$inputs;

my $cmd = "$$opts{path}/test-bcf-sr --no-index -p all --args @ins > $$opts{tmp}/no_index_$count.out 2> $$opts{tmp}/no_index_$count.err";
print "$test:\n\t$cmd (expected fail)\n";
my ($ret) = _cmd($cmd);
if ($ret == 0) {
failed($opts, $test, "Failed to detect $badness: $cmd\n");
} else {
passed($opts, $test);
}
$count++;
}
}

sub test_bcf_sr_range {
my ($opts) = @_;

my $test = "test_bcf_sr_range";

my $vcfdir = "$$opts{path}/bcf-sr";

my @tests = (['r', '1', 'weird-chr-names.vcf', 'weird-chr-names.1.out'],
['r', '1:1-2', 'weird-chr-names.vcf', 'weird-chr-names.1.out'],
['r', '1:1,1:2', 'weird-chr-names.vcf', 'weird-chr-names.1.out'],
['r', '1:1-1', 'weird-chr-names.vcf', 'weird-chr-names.2.out'],
['r', '{1:1}', 'weird-chr-names.vcf', 'weird-chr-names.3.out'],
['r', '{1:1}:1-2', 'weird-chr-names.vcf', 'weird-chr-names.3.out'],
['r', '{1:1}:1,{1:1}:2', 'weird-chr-names.vcf', 'weird-chr-names.3.out'],
['r', '{1:1}:1-1', 'weird-chr-names.vcf', 'weird-chr-names.4.out'],
['r', '{1:1-1}', 'weird-chr-names.vcf', 'weird-chr-names.5.out'],
['r', '{1:1-1}:1-2', 'weird-chr-names.vcf', 'weird-chr-names.5.out'],
['r', '{1:1-1}:1,{1:1-1}:2', 'weird-chr-names.vcf', 'weird-chr-names.5.out'],
['r', '{1:1-1}:1-1', 'weird-chr-names.vcf', 'weird-chr-names.6.out'],
['r', '{1:1-1}-2', 'weird-chr-names.vcf', undef], # Expected failure
['t', '1', 'weird-chr-names.vcf', 'weird-chr-names.1.out'],
['t', '1:1-2', 'weird-chr-names.vcf', 'weird-chr-names.1.out'],
['t', '1:1,1:2', 'weird-chr-names.vcf', 'weird-chr-names.1.out'],
['t', '1:1-1', 'weird-chr-names.vcf', 'weird-chr-names.2.out'],
['t', '{1:1}', 'weird-chr-names.vcf', 'weird-chr-names.3.out'],
['t', '{1:1}:1-2', 'weird-chr-names.vcf', 'weird-chr-names.3.out'],
['t', '{1:1}:1,{1:1}:2', 'weird-chr-names.vcf', 'weird-chr-names.3.out'],
['t', '{1:1}:1-1', 'weird-chr-names.vcf', 'weird-chr-names.4.out'],
['t', '{1:1-1}', 'weird-chr-names.vcf', 'weird-chr-names.5.out'],
['t', '{1:1-1}:1-2', 'weird-chr-names.vcf', 'weird-chr-names.5.out'],
['t', '{1:1-1}:1,{1:1-1}:2', 'weird-chr-names.vcf', 'weird-chr-names.5.out'],
['t', '{1:1-1}:1-1', 'weird-chr-names.vcf', 'weird-chr-names.6.out'],
['t', '{1:1-1}-2', 'weird-chr-names.vcf', undef] # Expected failure
);

my $count = 0;
my %converted;
foreach my $tst (@tests) {
my ($option, $range, $in, $exp_out) = @$tst;
$count++;
if (!$converted{$in}) {
my $cmd = "$$opts{path}/test_view -b -p $$opts{tmp}/$in.bcf -x $$opts{tmp}/$in.bcf.csi $vcfdir/$in";
print "$test:\n\t$cmd\n";
my ($ret) = _cmd($cmd);
if ($ret) {
failed($opts, $test);
$converted{$in} = 'fail';
next;
} else {
passed($opts, $test);
$converted{$in} = "$$opts{tmp}/$in.bcf";
}
}
next if ($converted{$in} eq 'fail');
my $cmd = "$$opts{path}/test-bcf-sr -O vcf -o $$opts{tmp}/range_test_$count.out.vcf -$option '$range' --args $converted{$in}";
if ($exp_out) {
test_compare($opts, $cmd, "$vcfdir/$exp_out",
"$$opts{tmp}/range_test_$count.out.vcf",
fix_newlines => 1);
} else {
print "$test:\n\t$cmd (expected fail)\n";
my ($ret) = _cmd($cmd);
if ($ret) {
passed($opts, $test);
} else {
failed($opts, $test);
}
}
}
}

sub test_command
{
my ($opts, %args) = @_;
Expand Down

0 comments on commit e86126b

Please sign in to comment.