forked from z0on/2bRAD_denovo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recalibrateSNPs.pl
executable file
·445 lines (398 loc) · 11.6 KB
/
recalibrateSNPs.pl
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/usr/bin/perl
my $usage="
recalibrateSNPs.pl :
Non-parametric variant quality recalibration based on INFO fields
in the set of variants that are reproducibly genotyped among replicates
(output of replicatesMatch.pl)
This script is for de novo pipeline. Use recalibrateSNPs_gatk.pl for
reference-based pipeline.
Three parameters (INFO items) are assessed for each SNP:
- total coverage depth (DP)
- strand bias (SB)
- allele bias (AB)
- SNP position withn tag (TP)
For AB and SB, low values are deemed bad and high ones are good;
for DP and TP, both low and high values are considered bad, the best values
being around the median.
The script computes the product of quantiles for different INFO fields, determines
the quantiles of the result within the 'true' set (JOINT quantiles),
and then computes the new quality scores in the main vcf file.
These scores are supposed to correspond to the probability (x100) that the
SNPs comes from the same distribution as the 'true' SNPs.
Output:
- Recalibrated VCF (printed to STDOUT) with QUAL field replaced by the new score
minus 0.1 (for easy filtering)
- a table (printed to STDERR) showing the \"gain\" at each recalibrated quality
score setting, which is excess variants removed when filtering at this quality score.
For example, if 40% of all variants are removed at the quality score 15, the gain
is 40 - 15 = 35% (i.e., in addition to removing 15% of the 'true' variants,
additional 35% of the dataset, likely corresponding to wrong variants, is removed).
The optimal filtering score is the one giving maximum gain. Try different combinations
of the four possible filters to find the one maximizing the gain.
Arguments:
vcf=[file name] : vcf file to be recalibrated
true=[file name] : vcf file that is subset of the above, with SNPs that are considered
true because they show matching and polymorphic genotypes in replicates
(replicatesMatch.pl polyonly=1).
-nodp : do not use DP
-noab : do not use AB
-nosb : do not use SB
-notp : do not use TP
Example:
recalibrateSNPs.pl vcf=cdh_alltags.ul_Vhap_count10_ab10_sb10_clip0.vcf \
true=vqsr.vhap.vcf -nosb -notp >denovo.vhap.recal.vcf
Mikhail Matz, matz\@utexas.edu July 2013 - September 2014
";
my $vcf;
my $true;
my $nodp=0;
my $nosb=0;
my $noab=0;
my $notp=0;
if ("@ARGV"=~/vcf=(\S+)/) { $vcf=$1;}
else { die $usage; }
if ("@ARGV"=~/true=(\S+)/) { $true=$1;}
else { die $usage; }
if ("@ARGV"=~/-noab/) { $noab=1;}
if ("@ARGV"=~/-nosb/) { $nosb=1;}
if ("@ARGV"=~/-nodp/) { $nodp=1;}
if ("@ARGV"=~/-notp/) { $notp=1;}
if($nodp+$nosb+$noab+$notp==4) { die "no fields left to filter!\n";}
open TR, $true or die "cannot open the true set $true\n";
my @dp=();
my @ab=();
my @sb=();
my @tp=();
my @dline=();
my @iline=();
while (<TR>) {
next if ($_=~/^#/);
@dline=split("\t",$_);
@iline=split(";",$dline[7]);
foreach my $i (@iline){
if ($i=~/AB=(\d+)/) { push @ab, $1;}
elsif ($i=~/SB=(\d+)/) { push @sb, $1;}
elsif ($i=~/DP=(\d+)/) { push @dp, $1;}
elsif ($i=~/TP=(\d+)/) { push @tp, $1;}
}
}
close TR;
@ab=sort {$a <=> $b} @ab;
@sb=sort {$a <=> $b} @sb;
@dp=sort {$a <=> $b} @dp;
@tp=sort {$a <=> $b} @tp;
my $total=$#dp+1;
my %abq;
my %sbq;
my %dpq;
my %dpq2;
$abq{1}=$ab[sprintf("%.0f",$total*0.01)];
$sbq{1}=$sb[sprintf("%.0f",$total*0.01)];
$abq{5}=$ab[sprintf("%.0f",$total*0.05)];
$sbq{5}=$sb[sprintf("%.0f",$total*0.05)];
$abq{10}=$ab[sprintf("%.0f",$total*0.1)];
$sbq{10}=$sb[sprintf("%.0f",$total*0.1)];
$abq{15}=$ab[sprintf("%.0f",$total*0.15)];
$sbq{15}=$sb[sprintf("%.0f",$total*0.15)];
$abq{20}=$ab[sprintf("%.0f",$total*0.2)];
$sbq{20}=$sb[sprintf("%.0f",$total*0.2)];
$abq{30}=$ab[sprintf("%.0f",$total*0.3)];
$sbq{30}=$sb[sprintf("%.0f",$total*0.3)];
$abq{40}=$ab[sprintf("%.0f",$total*0.4)];
$sbq{40}=$sb[sprintf("%.0f",$total*0.4)];
$abq{50}=$ab[sprintf("%.0f",$total*0.5)];
$sbq{50}=$sb[sprintf("%.0f",$total*0.5)];
$abq{60}=$ab[sprintf("%.0f",$total*0.6)];
$sbq{60}=$sb[sprintf("%.0f",$total*0.6)];
$abq{70}=$ab[sprintf("%.0f",$total*0.7)];
$sbq{70}=$sb[sprintf("%.0f",$total*0.7)];
$abq{80}=$ab[sprintf("%.0f",$total*0.8)];
$sbq{80}=$sb[sprintf("%.0f",$total*0.8)];
$abq{90}=$ab[sprintf("%.0f",$total*0.9)];
$sbq{90}=$sb[sprintf("%.0f",$total*0.9)];
$dpq{1}=$dp[sprintf("%.0f",$total*0.005)];
$dpq2{1}=$dp[sprintf("%.0f",$total*0.995)];
$dpq{5}=$dp[sprintf("%.0f",$total*0.025)];
$dpq2{5}=$dp[sprintf("%.0f",$total*0.975)];
$dpq{10}=$dp[sprintf("%.0f",$total*0.05)];
$dpq2{10}=$dp[sprintf("%.0f",$total*0.95)];
$dpq{15}=$dp[sprintf("%.0f",$total*0.075)];
$dpq2{15}=$dp[sprintf("%.0f",$total*0.925)];
$dpq{20}=$dp[sprintf("%.0f",$total*0.1)];
$dpq2{20}=$dp[sprintf("%.0f",$total*0.9)];
$dpq{30}=$dp[sprintf("%.0f",$total*0.15)];
$dpq2{30}=$dp[sprintf("%.0f",$total*0.85)];
$dpq{40}=$dp[sprintf("%.0f",$total*0.2)];
$dpq2{40}=$dp[sprintf("%.0f",$total*0.8)];
$dpq{50}=$dp[sprintf("%.0f",$total*0.25)];
$dpq2{50}=$dp[sprintf("%.0f",$total*0.75)];
$dpq{60}=$dp[sprintf("%.0f",$total*0.3)];
$dpq2{60}=$dp[sprintf("%.0f",$total*0.7)];
$dpq{70}=$dp[sprintf("%.0f",$total*0.35)];
$dpq2{70}=$dp[sprintf("%.0f",$total*0.65)];
$dpq{80}=$dp[sprintf("%.0f",$total*0.4)];
$dpq2{80}=$dp[sprintf("%.0f",$total*0.6)];
$dpq{90}=$dp[sprintf("%.0f",$total*0.45)];
$dpq2{90}=$dp[sprintf("%.0f",$total*0.55)];
$tpq{1}=$tp[sprintf("%.0f",$total*0.005)];
$tpq2{1}=$tp[sprintf("%.0f",$total*0.995)];
$tpq{5}=$tp[sprintf("%.0f",$total*0.025)];
$tpq2{5}=$tp[sprintf("%.0f",$total*0.975)];
$tpq{10}=$tp[sprintf("%.0f",$total*0.05)];
$tpq2{10}=$tp[sprintf("%.0f",$total*0.95)];
$tpq{15}=$tp[sprintf("%.0f",$total*0.075)];
$tpq2{15}=$tp[sprintf("%.0f",$total*0.925)];
$tpq{20}=$tp[sprintf("%.0f",$total*0.1)];
$tpq2{20}=$tp[sprintf("%.0f",$total*0.9)];
$tpq{30}=$tp[sprintf("%.0f",$total*0.15)];
$tpq2{30}=$tp[sprintf("%.0f",$total*0.85)];
$tpq{40}=$tp[sprintf("%.0f",$total*0.2)];
$tpq2{40}=$tp[sprintf("%.0f",$total*0.8)];
$tpq{50}=$tp[sprintf("%.0f",$total*0.25)];
$tpq2{50}=$tp[sprintf("%.0f",$total*0.75)];
$tpq{60}=$tp[sprintf("%.0f",$total*0.3)];
$tpq2{60}=$tp[sprintf("%.0f",$total*0.7)];
$tpq{70}=$tp[sprintf("%.0f",$total*0.35)];
$tpq2{70}=$tp[sprintf("%.0f",$total*0.65)];
$tpq{80}=$tp[sprintf("%.0f",$total*0.4)];
$tpq2{80}=$tp[sprintf("%.0f",$total*0.6)];
$tpq{90}=$tp[sprintf("%.0f",$total*0.45)];
$tpq2{90}=$tp[sprintf("%.0f",$total*0.55)];
print STDERR "\nAB quantiles:\n";
foreach my $q (sort {$a <=> $b} keys %abq){
print STDERR "$q\t$abq{$q}\n";
}
print STDERR "\nSB quantiles:\n";
foreach my $q (sort {$a <=> $b} keys %sbq){
print STDERR "$q\t$sbq{$q}\n";
}
print STDERR "\nDP quantiles:\n";
foreach my $q (sort {$a <=> $b} keys %dpq){
print STDERR "$q\t$dpq{$q}\t$dpq2{$q}\n";
}
print STDERR "\nTP quantiles:\n";
foreach my $q (sort {$a <=> $b} keys %tpq){
print STDERR "$q\t$tpq{$q}\t$tpq2{$q}\n";
}
my @joint=();
my %jq={};
open TR, $true;
while(<TR>){
if ($_=~/^#/) {
next;
}
@dline=split("\t",$_);
@iline=split(";",$dline[7]);
my $abQ=0;
my $sbQ=0;
my $dpQ=0;
my $tpQ=0;
foreach my $i (@iline){
if ($i=~/AB=(\d+)/) {
if ($noab){
$abQ=100;
next;
}
my $abt=$1;
foreach my $abv (sort {$a <=> $b} keys %abq) {
if ($abt<$abq{$abv}) {
$abQ=$abv;
last;
}
}
if (!$abQ){$abQ=100;}
}
elsif ($i=~/SB=(\d+)/) {
if ($nosb){
$sbQ=100;
next;
}
my $sbt=$1;
foreach my $sbv (sort {$a <=> $b} keys %sbq) {
if ($sbt<$sbq{$sbv}) {
$sbQ=$sbv;
last;
}
}
if (!$sbQ){$sbQ=100;}
}
elsif ($i=~/DP=(\d+)/) {
if ($nodp){
$dpQ=100;
next;
}
my $dpt=$1;
foreach my $dpv (sort {$a <=> $b} keys %dpq) {
if ($dpt<$dpq{$dpv} || $dpt>$dpq2{$dpv}) {
$dpQ=$dpv;
last;
}
}
if (!$dpQ){
$dpQ=100;
}
# if ($dpQ>50) { $dpQ=100-$dpQ;}
}
elsif ($i=~/TP=(\d+)/) {
if ($notp){
$tpQ=100;
next;
}
my $tpt=$1;
foreach my $tpv (sort {$a <=> $b} keys %tpq) {
if ($tpt<$tpq{$tpv} || $tpt>$tpq2{$tpv}) {
$tpQ=$tpv;
last;
}
}
if (!$tpQ){
$tpQ=100;
}
# if ($dpQ>50) { $dpQ=100-$dpQ;}
}
}
push @joint, sprintf("%.5f",($dpQ/100)*($tpQ/100)*($abQ/100)*($sbQ/100));
}
close TR;
@joint=sort {$a <=> $b} @joint;
$jq{1}=$joint[sprintf("%.0f",$total*0.01)];
$jq{5}=$joint[sprintf("%.0f",$total*0.05)];
$jq{10}=$joint[sprintf("%.0f",$total*0.1)];
$jq{15}=$joint[sprintf("%.0f",$total*0.15)];
$jq{20}=$joint[sprintf("%.0f",$total*0.2)];
$jq{30}=$joint[sprintf("%.0f",$total*0.3)];
$jq{40}=$joint[sprintf("%.0f",$total*0.4)];
$jq{50}=$joint[sprintf("%.0f",$total*0.5)];
$jq{60}=$joint[sprintf("%.0f",$total*0.6)];
$jq{70}=$joint[sprintf("%.0f",$total*0.7)];
$jq{80}=$joint[sprintf("%.0f",$total*0.8)];
$jq{90}=$joint[sprintf("%.0f",$total*0.9)];
print STDERR "\nJOINT quantiles:\n";
foreach my $q (sort {$a <=> $b} keys %jq){
next if ($q=~/HASH/);
print STDERR "$q\t$jq{$q}\n";
}
my $ones=0;
my $fives=0;
my $tens=0;
my $fifteens=0;
my $twenties=0;
my $thirties=0;
my $total2=0;
open VCF, $vcf or die "cannot open vcf $vcf\n";
while(<VCF>){
if ($_=~/^#/) {
print $_;
next;
}
$total2++;
@dline=split("\t",$_);
@iline=split(";",$dline[7]);
my $abQ=0;
my $sbQ=0;
my $dpQ=0;
my $jQ=0;
foreach my $i (@iline){
if ($i=~/AB=(\d+)/) {
if ($noab){
$abQ=100;
next;
}
my $abt=$1;
foreach my $abv (sort {$a <=> $b} keys %abq) {
if ($abt<$abq{$abv}) {
$abQ=$abv;
last;
}
}
if (!$abQ){$abQ=100;}
}
elsif ($i=~/SB=(\d+)/) {
if ($nosb){
$sbQ=100;
next;
}
my $sbt=$1;
foreach my $sbv (sort {$a <=> $b} keys %sbq) {
if ($sbt<$sbq{$sbv}) {
$sbQ=$sbv;
last;
}
}
if (!$sbQ){$sbQ=100;}
}
elsif ($i=~/DP=(\d+)/) {
if ($nodp){
$dpQ=100;
next;
}
my $dpt=$1;
foreach my $dpv (sort {$a <=> $b} keys %dpq) {
if ($dpt<$dpq{$dpv} || $dpt>$dpq2{$dpv}) {
$dpQ=$dpv;
last;
}
}
if (!$dpQ){
$dpQ=100;
}
# if ($dpQ>50) { $dpQ=100-$dpQ;}
}
elsif ($i=~/TP=(\d+)/) {
if ($notp){
$tpQ=100;
next;
}
my $tpt=$1;
foreach my $tpv (sort {$a <=> $b} keys %tpq) {
if ($tpt<$tpq{$tpv} || $tpt>$tpq2{$tpv}) {
$tpQ=$tpv;
last;
}
}
if (!$tpQ){
$tpQ=100;
}
# if ($dpQ>50) { $dpQ=100-$dpQ;}
}
}
my $jt=sprintf("%.5f",($dpQ/100)*($tpQ/100)*($abQ/100)*($sbQ/100));
#warn "\t$jt\n";
foreach my $jv (sort {$a <=> $b} keys %jq) {
if ($jt<$jq{$jv}) {
$jQ=$jv;
last;
}
}
if (!$jQ){$jQ=100;}
if ($jQ==1) { $ones++;}
elsif ($jQ==5) { $fives++;}
elsif ($jQ==10) { $tens++;}
elsif ($jQ==15) { $fifteens++;}
elsif ($jQ==20) { $twenties++;}
elsif ($jQ==30) { $thirties++;}
$jQ=$jQ-0.1;
$dline[5]=$jQ;
push @iline,"SBQ=".$sbQ;
push @iline,"ABQ=".$abQ;
push @iline,"DPQ=".$dpQ;
push @iline,"TPQ=".$tpQ;
$dline[7]=join(";",@iline);
print join("\t",@dline);
}
my $oness=sprintf("%.2f",100*$ones/$total2);
my $fivess=sprintf("%.2f",100*($ones+$fives)/$total2);
my $tenss=sprintf("%.2f",100*($ones+$fives+$tens)/$total2);
my $fifteenss=sprintf("%.2f",100*($ones+$fives+$tens+$fifteens)/$total2);
my $twentiess=sprintf("%.2f",100*($ones+$fives+$tens+$fifteens+$twenties)/$total2);
my $thirtiess=sprintf("%.2f",100*($ones+$fives+$tens+$fifteens+$twenties+$thirties)/$total2);
print STDERR "
------------------------
$oness%\tat qual <1 (",sprintf("%.2f",$oness-1),"% gain)
$fivess%\tat qual <5 (",sprintf("%.2f",$fivess-5),"% gain)
$tenss%\tat qual <10 (",sprintf("%.2f",$tenss-10),"% gain)
$fifteenss%\tat qual <15 (",sprintf("%.2f",$fifteenss-15),"% gain)
$twentiess%\tat qual <20 (",sprintf("%.2f",$twentiess-20),"% gain)
$thirtiess%\tat qual <30 (",sprintf("%.2f",$thirtiess-30),"% gain)
------------------------
";