-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter_vcf.ss.pl
executable file
·136 lines (127 loc) · 2.61 KB
/
filter_vcf.ss.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
#!/usr/bin/perl
use strict;
use warnings;
my $vcf_file = $ARGV[0];
my $dp = $ARGV[1] || 20;
my $imf = $ARGV[2] || 0.85;
my $af1 = $ARGV[3] || 0.90;
my $rdp4 = $ARGV[4] || 0.50;
my $filter_vcf = filter_vcf($vcf_file);
annotate($filter_vcf);
sub annotate {
my $file = shift;
my $prefix = '';
if ($file =~ /(\S+).vcf/) {
$prefix = $1;
} else {
$prefix = 'filtered';
}
print STDERR "Annotating VCF ...\n";
system("snpeff.ss $file > $prefix.ann.vcf");
unlink $file;
unlink "snpEff_genes.txt";
unlink "snpEff_summary.html";
}
sub filter_vcf {
my ($file) = @_;
print STDERR "Filtering VCF ...\n";
open (VCF, $file) or die "Cannot open VCF file: $file, $!\n";
my $prefix = '';
if ($file =~ /(\S+).vcf/) {
$prefix = $1;
} else {
$prefix = 'filtered';
}
open (FVCF, ">$prefix.filtered.vcf") or die "Cannot open file: $prefix.vcf, $!\n";
while (<VCF>) {
chomp;
next if /^\#/;
my @w = split /\t/;
my $info = $w[7];
my $var = undef;
$w[7] = '';
if (is_indel($info)) {
$var = parse_indel($info);
next if ($var->{'DP'} < $dp);
next if ($var->{'IMF'} < $imf);
$w[7] = 'INDEL;IDV=' . $var->{'IDV'} . ';IMF=' . $var->{'IMF'} . ';';
} else {
$var = parse_snv($info);
next if ($var->{'DP'} < $dp);
next if ($var->{'AF1'} < $af1);
my @dp4 = split /\,/, $var->{'DP4'};
my $ratio = sum(@dp4)/$var->{'DP'};
next if ($ratio < $rdp4);
}
$w[7] .= 'DP=' . $var->{'DP'} . ';AF1=' . $var->{'AF1'} . ';DP4=' . $var->{'DP4'};
my $vcf_line = join "\t", @w;
print FVCF $vcf_line . "\n";
}
close VCF;
close FVCF;
return "$prefix.filtered.vcf";
}
sub is_indel {
my $info = shift;
if ($info =~ /INDEL/) {
return 1;
} else {
return 0;
}
}
sub parse_indel {
my $info = shift;
my %indel = ();
my @f = split /\;/, $info;
$indel{'IDV'} = 0;
$indel{'IMF'} = 0;
$indel{'DP'} = 0;
$indel{'AF1'} = 0;
$indel{'DP4'} = undef;
foreach my $item (@f) {
if ($item =~ /IDV\=(\S+)/) {
$indel{'IDV'} = $1;
}
if ($item =~ /IMF\=(\S+)/) {
$indel{'IMF'} = $1;
}
if ($item =~ /DP\=(\S+)/) {
$indel{'DP'} = $1;
}
if ($item =~ /AF1\=(\S+)/) {
$indel{'AF1'} = $1;
}
if ($item =~ /DP4\=(\S+)/) {
$indel{'DP4'} = $1;
}
}
return \%indel;
}
sub parse_snv {
my $info = shift;
my %snv = ();
my @f = split /\;/, $info;
$snv{'DP'} = 0;
$snv{'AF1'} = 0;
$snv{'DP4'} = undef;
foreach my $item (@f) {
if ($item =~ /DP\=(\S+)/) {
$snv{'DP'} = $1;
}
if ($item =~ /AF1\=(\S+)/) {
$snv{'AF1'} = $1;
}
if ($item =~ /DP4\=(\S+)/) {
$snv{'DP4'} = $1;
}
}
return \%snv;
}
sub sum {
my @array = @_;
my $sum = 0;
foreach my $num (@array) {
$sum += $num;
}
return $sum;
}