- next if ($t[2] ne '*');
- if (!$is_ref) {
- next if ($t[3] eq '*/*');
- next if ($t[5] == 0);
- }
- next if ($t[7] > $opts{D});
- # calculate indel score
- my $score = $t[5];
- $score += $opts{s} * $t[10] if ($t[8] ne '*');
- $score += $opts{s} * $t[11] if ($t[9] ne '*');
- @$curr = ($t[0], $t[1], $score, $_);
- my $do_swap = 1;
- if (defined $last->[0]) {
- if ($curr->[0] eq $last->[0] && $last->[1] + $opts{m} > $curr->[1]) {
- $do_swap = 0 if ($last->[2] > $curr->[2]);
- } else { # then print
- print $last->[3];
+ next if (uc($t[2]) eq uc($t[3]) || $t[3] eq '*/*'); # skip non-var sites
+ # clear the out-of-range elements
+ while (@staging) {
+ # Still on the same chromosome and the first element's window still affects this position?
+ last if ($staging[0][3] eq $t[0] && $staging[0][4] + $staging[0][2] + $max_dist >= $t[1]);
+ varFilter_aux(shift(@staging), $opts{p}); # calling a function is a bit slower, not much
+ }
+ my ($flt, $score) = (0, -1);
+ # first a simple filter
+ if ($t[7] < $opts{d}) {
+ $flt = 2;
+ } elsif ($t[7] > $opts{D}) {
+ $flt = 3;
+ }
+ if ($t[2] eq '*') { # an indel
+ if ($opts{i} && $opts{i}>$t[5]) { $flt = 8; }
+ }
+ elsif ($opts{S} && $opts{S}>$t[5]) { $flt = 7; } # SNP
+
+ # site dependent filters
+ my $len=0;
+ if ($flt == 0) {
+ if ($t[2] eq '*') { # an indel
+ # If deletion, remember the length of the deletion
+ my ($a,$b) = split(m{/},$t[3]);
+ my $alen = length($a) - 1;
+ my $blen = length($b) - 1;
+ if ( $alen>$blen )
+ {
+ if ( substr($a,0,1) eq '-' ) { $len=$alen; }
+ }
+ elsif ( substr($b,0,1) eq '-' ) { $len=$blen; }
+
+ $flt = 1 if ($t[6] < $opts{q});
+ # filtering SNPs
+ if ($t[5] >= $opts{G}) {
+ for my $x (@staging) {
+ # Is it a SNP and is it outside the SNP filter window?
+ next if ($x->[0] >= 0 || $x->[4] + $x->[2] + $ow < $t[1]);
+ $x->[1] = 5 if ($x->[1] == 0);
+ }
+ }
+ # calculate the filtering score (different from indel quality)
+ $score = $t[5];
+ $score += $opts{s} * $t[10] if ($t[8] ne '*');
+ $score += $opts{s} * $t[11] if ($t[9] ne '*');
+ # check the staging list for indel filtering
+ for my $x (@staging) {
+ # Is it a SNP and is it outside the gap filter window
+ next if ($x->[0] < 0 || $x->[4] + $x->[2] + $ol < $t[1]);
+ if ($x->[0] < $score) {
+ $x->[1] = 6;
+ } else {
+ $flt = 6; last;
+ }
+ }
+ } else { # a SNP
+ $flt = 1 if ($t[6] < $opts{Q});
+ # check adjacent SNPs
+ my $k = 1;
+ for my $x (@staging) {
+ ++$k if ($x->[0] < 0 && $x->[4] + $x->[2] + $oW >= $t[1] && ($x->[1] == 0 || $x->[1] == 4 || $x->[1] == 5));
+ }
+ # filtering is necessary
+ if ($k > $opts{N}) {
+ $flt = 4;
+ for my $x (@staging) {
+ $x->[1] = 4 if ($x->[0] < 0 && $x->[4] + $x->[2] + $oW >= $t[1] && $x->[1] == 0);
+ }
+ } else { # then check gap filter
+ for my $x (@staging) {
+ next if ($x->[0] < 0 || $x->[4] + $x->[2] + $ow < $t[1]);
+ if ($x->[0] >= $opts{G}) {
+ $flt = 5; last;
+ }
+ }
+ }