This repository was archived by the owner on Mar 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun-eval
executable file
·186 lines (166 loc) · 6.44 KB
/
run-eval
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Std;
my %opts = ();
getopts('b:p:o:l:L:t:s:', \%opts);
# check path
my $exepath = $0 =~/^\S+\/[^\/\s]+/? $0 : &which($0);
my $root = $0 =~/^(\S+)\/[^\/\s]+/? $1 : undef;
$root = $exepath =~/^(\S+)\/[^\/\s]+/? $1 : undef if !defined($root);
die "ERROR: failed to locate the root directory\n" if !defined($root);
die("Usage: run-eval [options] <truth.vcf> <test.vcf>
Options:
-o STR output prefix [auto]
-b FILE confident regions in BED [null]
-p FILE homopolymer positions generated by 'seqtk hrun' [null]
-t FILE list of contig name and length [null]
-l INT min INDEL length [0]
-L INT max INDEL length [inf]
-s DIR RTG's SDF reference directory (generated by 'rtg format') [null]
") if @ARGV < 2;
# infer prefix
my $prefix;
if (defined $opts{o}) {
$prefix = $opts{o};
} elsif ($ARGV[1] =~ /\.vcf(\.gz?)$/) {
$prefix = $ARGV[1];
$prefix =~ s/\.vcf(\.gz?)$//;
$prefix .= ".re";
}
die "ERROR: failed to infer the prefix for output. Please specify -o.\n" unless defined($prefix);
# test ##contig in VCF header
my @ctg_truth = &test_contig($ARGV[0]);
my @ctg_test = &test_contig($ARGV[1]);
die "ERROR: failed to find ##contig lines in both VCFs. Please specify -t.\n" if (@ctg_truth == 0 && @ctg_test == 0);
my $have_ctg = 0;
if (@ctg_test == 0 && @ctg_truth != 0) {
&print_ctg(\@ctg_truth, "$prefix.ctg");
$have_ctg = 1;
} elsif (@ctg_test != 0 && @ctg_truth == 0) {
&print_ctg(\@ctg_test, "$prefix.ctg");
$have_ctg = 1;
}
$opts{t} = "$prefix.ctg" unless defined($opts{t});
open(OUT, ">$prefix.eval") || die;
warn "Evaluating positional accuracy...\n";
my $de_opt = "";
$de_opt .= " -l $opts{l}" if defined($opts{l});
$de_opt .= " -L $opts{L}" if defined($opts{L});
$de_opt .= " -b $opts{b}" if defined($opts{b});
$de_opt .= " -p $opts{p}" if defined($opts{p});
system("($root/k8 $root/hapdip.js distEval $de_opt $ARGV[0] $ARGV[1] > $prefix.de) 2>> $prefix.log");
system("($root/k8 $root/hapdip.js distEval -e $de_opt $ARGV[0] $ARGV[1] | $root/htsbox bgzip > $prefix.p_err.bed.gz) 2>> $prefix.log");
open(FH, "$prefix.de") || die;
while (<FH>) {
s/^distEval/positional/;
print OUT $_;
}
close(FH);
if (defined $opts{s}) {
unless (-f "$prefix.truth.vcf.gz") {
warn "Normalizing the truth VCF...\n";
my $ot = @ctg_truth? '' : "-t $opts{t}";
my $op = defined($opts{p})? "-p $opts{p}" : "";
my $cmd = "($root/bgt atomize -S $ot $ARGV[0] | $root/k8 $root/hapdip.js atompost $op /dev/stdin | $root/htsbox bgzip > $prefix.truth.vcf.gz) 2>> $prefix.log";
system($cmd);
}
system("$root/htsbox tabix -fpvcf $prefix.truth.vcf.gz");
unless (-f "$prefix.test.vcf.gz") {
warn "Normalizing the test VCF...\n";
my $ot = @ctg_test? '' : "-t $opts{t}";
my $op = defined($opts{p})? "-p $opts{p}" : "";
my $cmd = "($root/bgt atomize -S $ot $ARGV[1] | $root/k8 $root/hapdip.js atompost $op /dev/stdin | $root/htsbox bgzip > $prefix.test.vcf.gz) 2>> $prefix.log";
system($cmd);
}
system("$root/htsbox tabix -fpvcf $prefix.test.vcf.gz");
unless (-d "$prefix.a") {
warn "Evaluating allelic accuracy...\n";
my $cmd = "($root/rtg vcfeval -t $opts{s} -b $prefix.truth.vcf.gz -c $prefix.test.vcf.gz -o $prefix.a --squash-ploidy) 2>> $prefix.log";
system($cmd);
}
unless (-d "$prefix.g") {
warn "Evaluating genotypic accuracy...\n";
my $cmd = "($root/rtg vcfeval -t $opts{s} -b $prefix.truth.vcf.gz -c $prefix.test.vcf.gz -o $prefix.g) 2>> $prefix.log";
system($cmd);
}
warn "Counting...\n";
my $cnt_opt = '';
$cnt_opt .= " -l $opts{l}" if defined($opts{l});
$cnt_opt .= " -L $opts{L}" if defined($opts{L});
$cnt_opt .= " -b $opts{b}" if defined($opts{b});
$cnt_opt .= " -p 1" if defined($opts{p});
my @a_tp0 = &get_cnt($root, $cnt_opt, "$prefix.a/tp-baseline.vcf.gz");
my @a_tp1 = &get_cnt($root, $cnt_opt, "$prefix.a/tp.vcf.gz");
my @a_fn = &get_cnt($root, $cnt_opt, "$prefix.a/fn.vcf.gz", "$prefix.a_fn.vcf.gz");
my @a_fp = &get_cnt($root, $cnt_opt, "$prefix.a/fp.vcf.gz", "$prefix.a_fp.vcf.gz");
my @g_tp0 = &get_cnt($root, $cnt_opt, "$prefix.g/tp-baseline.vcf.gz");
my @g_tp1 = &get_cnt($root, $cnt_opt, "$prefix.g/tp.vcf.gz");
my @g_fn = &get_cnt($root, $cnt_opt, "$prefix.g/fn.vcf.gz", "$prefix.g_fn.vcf.gz");
my @g_fp = &get_cnt($root, $cnt_opt, "$prefix.g/fp.vcf.gz", "$prefix.g_fp.vcf.gz");
print OUT "allelic\tSNP\tFN\t$a_fn[0]\n";
print OUT "allelic\tSNP\tFP\t$a_fp[0]\n";
print OUT "allelic\tSNP\t%FNR\t", sprintf("%.2f", 100 * $a_fn[0] / ($a_fn[0] + $a_tp0[0])), "\n";
print OUT "allelic\tSNP\t%FDR\t", sprintf("%.2f", 100 * $a_fp[0] / ($a_fp[0] + $a_tp1[0])), "\n";
print OUT "allelic\tINDEL\tFN\t$a_fn[1]\n";
print OUT "allelic\tINDEL\tFP\t$a_fp[1]\n";
print OUT "allelic\tINDEL\t%FNR\t", sprintf("%.2f", 100 * $a_fn[1] / ($a_fn[1] + $a_tp0[1])), "\n";
print OUT "allelic\tINDEL\t%FDR\t", sprintf("%.2f", 100 * $a_fp[1] / ($a_fp[1] + $a_tp1[1])), "\n";
print OUT "genotypic\tSNP\tFN\t$g_fn[0]\n";
print OUT "genotypic\tSNP\tFP\t$g_fp[0]\n";
print OUT "genotypic\tSNP\t%FNR\t", sprintf("%.2f", 100 * $g_fn[0] / ($g_fn[0] + $g_tp0[0])), "\n";
print OUT "genotypic\tSNP\t%FDR\t", sprintf("%.2f", 100 * $g_fp[0] / ($g_fp[0] + $g_tp1[0])), "\n";
print OUT "genotypic\tINDEL\tFN\t$g_fn[1]\n";
print OUT "genotypic\tINDEL\tFP\t$g_fp[1]\n";
print OUT "genotypic\tINDEL\t%FNR\t", sprintf("%.2f", 100 * $g_fn[1] / ($g_fn[1] + $g_tp0[1])), "\n";
print OUT "genotypic\tINDEL\t%FDR\t", sprintf("%.2f", 100 * $g_fp[1] / ($g_fp[1] + $g_tp1[1])), "\n";
}
close(OUT);
sub which {
my $file = shift;
my $path = (@_)? shift : $ENV{PATH};
return if (!defined($path));
foreach my $x (split(":", $path)) {
$x =~ s/\/$//;
return "$x/$file" if (-x "$x/$file");
}
return;
}
sub test_contig {
my $fn = shift;
my @ctg = ();
open(FH, $fn =~ /\.gz$/? "gzip -dc $fn |" : $fn) || die;
while (<FH>) {
last unless /^#/;
my ($id, $len);
if (/^##contig/) {
$id = $1 if /ID=([^\s,]+)/;
$len = $1 if /length=(\d+)/;
}
push(@ctg, [$id, $len]) if defined($id) && defined($len);
}
close(FH);
return @ctg;
}
sub print_ctg {
my $ctg = shift;
my $fn = shift;
open(FH, ">$fn") || die;
for (@$ctg) {
print FH "$_->[0]\t$_->[1]\n";
}
close(FH);
}
sub get_cnt {
my $root = shift;
my $cnt_opt = shift;
my $fn = shift;
my $out = shift;
my @cnt;
open(FH, "$root/k8 $root/hapdip.js atomcnt $cnt_opt $fn |") || die;
$_ = <FH>;
@cnt = ($1, $2) if (/^(\d+)\s+(\d+)/);
close(FH);
system("$root/k8 $root/hapdip.js atomcnt $cnt_opt -P $fn | $root/htsbox bgzip > $out") if defined($out);
return @cnt;
}