forked from ding-lab/somaticwrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_mutect1.7.pl
176 lines (145 loc) · 3.93 KB
/
filter_mutect1.7.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
#!/usr/bin/perl
## tumor >= 5% and normal <=2%
### mutect1.7 filtering ###
use strict;
use warnings;
die unless @ARGV == 6;
### samtools ##
my ($samtools,$f_m,$f_filter_out,$mincov_t,$mincov_n,$minvaf)=@ARGV;
#my $f_m=$run_dir."/mutect/mutect.raw.vcf";
#my $f_filter_out=$run_dir."/mutect/mutect.filtered.vcf";
### minimum vaf for tumor 0.05, column 10 ###
## maximum vaf for normal 0.02, column 11 ###
### get the bam path ##
my @temp=split(/\//,$f_m);
my $path_d="/";
for(my $i=1;$i<scalar @temp-2; $i++)
{
$path_d.=$temp[$i]."/";
}
my $f_bam_n=$path_d.$temp[-3].".N.bam";
my $f_bam_t=$path_d.$temp[-3].".T.bam";
## read absolute path ##
#my $f_bam_n_abs=`readlink -f $f_bam_n`;
#my $f_bam_t_abs=`readlink -f $f_bam_t`;
#chomp($f_bam_n_abs);
#chomp($f_bam_t_abs);
#print $f_bam_n_abs,"\n";
#print $f_bam_t_abs,"\n";
#my $last_bam_n_abs=(split(/\//,$f_bam_n_abs))[-1];
#my $last_bam_t_abs=(split(/\//,$f_bam_t_abs))[-1];
my $sn_n;
my $sn_t;
#foreach my $l (`$samtools view -H $f_bam_n`)
# {
# my $ltr=$l;
# chomp($ltr);
# if($ltr=~/^\@RG/) {
# my @temp2=split("\t",$ltr);
# $sn_n=(split(/\:/,$temp2[-1]))[1];
# last;
# }
# }
#foreach my $l (`$samtools view -H $f_bam_t`)
# {
# my $ltr=$l;
# chomp($ltr);
# if($ltr=~/^\@RG/) {
# my @temp2=split("\t",$ltr);
# $sn_t=(split(/\:/,$temp2[-1]))[1];
# last;
# }
# }
foreach my $l (`$samtools view -H $f_bam_n`)
{
my $ltr=$l;
chomp($ltr);
if($ltr=~/^\@RG/) {
my @temp2=split("\t",$ltr);
for(my $i=0;$i<scalar @temp2;$i++)
{
if($temp2[$i]=~/^SM/)
{
$sn_n=(split(/\:/,$temp2[$i]))[1];
last;
}
}
}
}
foreach my $l (`$samtools view -H $f_bam_t`)
{
my $ltr=$l;
chomp($ltr);
if($ltr=~/^\@RG/) {
my @temp2=split("\t",$ltr);
for(my $i=0;$i<scalar @temp2;$i++)
{
if($temp2[$i]=~/^SM/)
{
$sn_t=(split(/\:/,$temp2[$i]))[1];
last;
}
}
}
}
#print $sn_n,"\t",$sn_t,"\n";
#<STDIN>;
my $min_vaf_somatic=$minvaf;
my $max_vaf_germline=0.02;
#my $min_coverage=$mincov;
my $tumor_normal_order=-1;
open(IN,"<$f_m");
open(OUT,">$f_filter_out");
while(<IN>)
{
my $l=$_;
chomp($l);
if($l=~/^#/) { #print OUT $l,"\n";
if($l=~/^#CHROM/) {
my @temphead=split("\t",$l);
print OUT $temphead[0];
for(my $i=1;$i<=8;$i++)
{
print OUT "\t",$temphead[$i];
}
if($temphead[9] eq $sn_t && $temphead[10] eq $sn_n) { print OUT "\t","TUMOR","\t","NORMAL","\n"; $tumor_normal_order=1; }
if($temphead[9] eq $sn_n && $temphead[10] eq $sn_t) { print OUT "\t","NORMAL","\t","TUMOR","\n"; $tumor_normal_order=0; }
}
else { print OUT $l,"\n"; }
}
else {
#print $tumor_normal_order,"\n";
#<STDIN>;
my @temp=split("\t",$l);
if($tumor_normal_order==-1) { last; }
my $tumor=$temp[9];
my $normal=$temp[10];
if($tumor_normal_order==0) {
$tumor=$temp[10];
$normal=$temp[9];
}
my @tempt=split(":",$tumor);
my @tempn=split(":",$normal);
my @readt=split(",",$tempt[1]);
my @readn=split(",",$tempn[1]);
#print $l,"\n";
#print $readn[0], "\t", $readn[1],"\n";
#<STDIN>;
#print $readt[0], "\t", $readt[1],"\n";
#### readn[0]: read count for ref allele in normal ##
### readn[1]: read count for alt allele in normal ##
#### readt[0]: read count for ref allele in tumor ##
### readt[1]: read count for alt allele in tumor ##
my $tot_n=$readn[0]+$readn[1];
my $tot_t=$readt[0]+$readt[1];
if($tot_n==0 || $tot_t==0) { next; }
my $vaf_t=$readt[1]/$tot_t;
my $vaf_n=$readn[1]/$tot_n;
#print $vaf_t,"\t",$vaf_n,"\n";
## apply filtering ##
if($temp[6] eq "PASS" && $vaf_t>=$min_vaf_somatic && $vaf_n<=$max_vaf_germline && $tot_n>=$mincov_n && $tot_t>=$mincov_t)
{
print OUT $l,"\n";
}
}
}