-
Notifications
You must be signed in to change notification settings - Fork 0
/
hetPi.pl
171 lines (146 loc) · 4.18 KB
/
hetPi.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
use strict;
use warnings;
use Getopt::Long;
my($vcf,$minmaf,$mincov,$out,$keepList,$window,$pass,$pass1);
my $usage = "USAGE:\nperl $0 --in <vcf> --out <output file>\n";
$usage .= "--keep <keep sample list>: [Optional]\n";
$usage .= "--maf: minor allele frequency [0,1], default=0 [Optional]\n";
$usage .= "--cov: minium coverage [0,1], default=0 [Optional]\n";
$usage .= "--window <window size>: default=10000 [Optional]\n";
$usage .= "--pass to keep SNPs with filter TAG [PASS] or [SnpCluster].\n";
$usage .= "--pass1 to keep SNPs with filter TAG [PASS].\n";
GetOptions(
"in=s" => \$vcf,
"out=s" => \$out,
"cov=s" => \$mincov,
"maf=s" => \$minmaf,
"window=s" => \$window,
"keep=s" => \$keepList,
"pass!" => \$pass,
"pass1!" => \$pass1,
) or die $usage;
die $usage unless(defined $vcf and defined $out);
unless(defined $mincov){
$mincov = 0;
}
unless(defined $minmaf){
$minmaf = 0;
}
unless(defined $window){
$window = 10 * 1000;
}
my %hash_keep;
if(defined $keepList){
open(IN,"<$keepList") or die $!;
while(<IN>){
chomp;
my($sample,$other) = split/\t/;
$hash_keep{$sample}{rank} = "";
}
close IN;
}
open(IN,"<$vcf") or die $!;
open(OUT,">$out");
print OUT "CHROM\tBIN_START\tBIN_END\tN_VARIANTS\tPI\n";
my @pickedRanks;
my %hash_window;
my $tmp = "";
while(<IN>){
chomp;
next if($_ =~ /^##/);
my($chr,$pos,$id,$ref,$alts_join,$qual,$filter,$info,$format,$datas_join) = split/\t/,$_,10;
# dissect samples
if($chr =~ /#CHROM/){
my @samples = split/\t/,$datas_join;
my $tail = @samples - 1;
my @pickedSamples = ();
print @pickedRanks."\n";
unless(defined $keepList){
@pickedRanks = (0..$tail);
next;
}
for(my $i = 0; $i < @samples; $i++){
next unless(exists $hash_keep{$samples[$i]});
push @pickedRanks, $i;
}
NOKEEP:
print "#Keeping ".@pickedRanks." samples from ".@samples." samples.\n";
next;
}
my $wRank = int($pos/$window);
my $wStart = $wRank * $window + 1;
my $wEnd = $wRank * $window + $window;
if($tmp ne "$chr|$wRank"){
goto EMPTY if($tmp eq "");
my($chr_tmp,$wRank_tmp) = $tmp =~ /(\S+)\|(\d+)/;
goto EMPTY unless(exists $hash_window{$chr_tmp}{$wRank_tmp});
my $wStart_tmp = $wRank_tmp * $window + 1;
my $wEnd_tmp = $wRank_tmp * $window + $window;
my $windowPi = $hash_window{$chr_tmp}{$wRank_tmp}{sum}/$window;
my $count = @{$hash_window{$chr_tmp}{$wRank_tmp}{pi}};
print OUT "$chr_tmp\t$wStart_tmp\t$wEnd_tmp\t$count\t$windowPi\n";
EMPTY:
$tmp = "$chr|$wRank";
}
# Check if the variant is a bi-allelic SNP
next unless(length($ref) == 1 and length($alts_join) == 1);
# Check if the SNP pass the filter
if(defined $pass){
next unless($filter eq "PASS" or $filter eq "SnpCluster");
}
if(defined $pass1){
next unless($filter eq "PASS");
}
my @datas = split/\t/,$datas_join;
my @pickedDatas = ();
foreach my $rank(@pickedRanks){
push @pickedDatas, $datas[$rank];
}
my($cov,$maf,$pi) = hetCovMafPi(@pickedDatas);
next unless($cov >= $mincov and $maf >= $minmaf);
push @{$hash_window{$chr}{$wRank}{pi}}, $pi;
$hash_window{$chr}{$wRank}{sum} += $pi;
}
close IN;
# output the last bin if exists
my($chr_tmp,$wRank_tmp) = $tmp =~ /(\S+)\|(\d+)/;
unless(exists $hash_window{$chr_tmp}{$wRank_tmp}){
my $wStart_tmp = $wRank_tmp * $window + 1;
my $wEnd_tmp = $wRank_tmp * $window + $window;
my $windowPi = $hash_window{$chr_tmp}{$wRank_tmp}{sum}/$window;
my $count = @{$hash_window{$chr_tmp}{$wRank_tmp}{pi}};
print OUT "$chr_tmp\t$wStart_tmp\t$wEnd_tmp\t$count\t$windowPi\n";
}
close OUT;
sub hetCovMafPi{
my @genos = @_;
my $refcount = 0;
my $altcount = 0;
my $hetcount = 0;
my $covcount = 0;
foreach my $geno(@genos){
if($geno =~ /^(\d+)\/(\d+)/){
$covcount++;
if($1 != $2){
$hetcount++;
}elsif($1 == 0){
$refcount++;
}else{
$altcount++;
}
}
}
my $cov = $covcount/@genos;
my @counts = sort {$b <=> $a} ($refcount,$hetcount,$altcount);
my $maf = ($covcount - $counts[0])/@genos;
my $refratio = 0;
my $hetratio = 0;
my $altratio = 0;
if($covcount > 0){
$refratio = $refcount/$covcount;
$hetratio = $hetcount/$covcount;
$altratio = $altcount/$covcount;
}
my $pi = $refratio * $hetratio * 1 + $altratio * $hetratio * 1 + $refratio * $altratio * 2;
return($cov,$maf,$pi);
}