-
Notifications
You must be signed in to change notification settings - Fork 1
/
windowed_similarity_sep.pl
320 lines (296 loc) · 7.65 KB
/
windowed_similarity_sep.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
use strict;
use warnings;
use Getopt::Long;
my($qvcf,$tvcf,$qName,$windowSize,$keepList,$qpass,$tpass,$outName,$regList);
my $usage = "USAGE:\nperl $0 --qin <query vcf> --tin <target vcf> --q <query name> --out <out prefix>";
GetOptions(
"qin=s" => \$qvcf,
"tin=s" => \$tvcf,
"q=s" => \$qName,
"out=s" => \$outName,
"keep=s" => \$keepList,
"bed=s" => \$regList,
"window=s" => \$windowSize,
"qpass!" => \$qpass,
"tpass!" => \$tpass,
) or die $usage;
die $usage unless(defined $qvcf and defined $tvcf and defined $qName and defined $outName);
unless(defined $windowSize){
$windowSize = 10 * 1000;
}
# input keep samples
my %hash_sample;
if(defined $keepList){
print "# Reading sample list...\n";
open(IN,"<$keepList") or die $!;
while(<IN>){
chomp;
$hash_sample{$_}{rank} = "";
}
close IN;
}
# input keep regions
my %hash_bed;
my %hash_chr;
if(defined $regList){
open(IN,"<$regList") or die $!;
while(<IN>){
chomp;
my($chr,$start,$end,$other) = split/\t/;
next if(exists $hash_bed{$chr}{$start} and $hash_bed{$chr}{$start} > $end);
$hash_bed{$chr}{$start} = $end;
}
close IN;
# merge regions
foreach my $chr(keys %hash_bed){
$hash_chr{$chr} = "";
my @starts = sort {$a <=> $b} keys %{$hash_bed{$chr}};
for(my $i = 0; $i < @starts; $i++){
my $starti = $starts[$i];
my $endi = $hash_bed{$chr}{$starti};
for(my $j = $i+1; $j < @starts; $j++){
my $startj = $starts[$j];
my $endj = $hash_bed{$chr}{$startj};
last if($startj > $endi);
if($endj > $endi){
$endi = $endj;
$hash_bed{$chr}{$starti} = $endj;
}
delete($hash_bed{$chr}{$startj});
splice(@starts,$j,1);
$j--;
}
}
}
}
# read query vcf file
my %hash_query;
my $qRank;
# chr settings
my @regions;
my @remained_chrs;
# window settings
my $chr_tmp = "";
open(IN,"<$qvcf") or die $!;
while(<IN>){
chomp;
if($_ =~ /^#CHROM/){
my(undef,undef,undef,undef,undef,undef,undef,undef,undef,@allSamples) = split/\t/;
for(my $i = 0; $i < @allSamples; $i++){
next unless($allSamples[$i] eq $qName);
$qRank = $i;
}
unless(defined $qRank){
print "#ERROR: the query sample:$qName does not exist in the query vcf file.\n";
die;
}
}
next if($_ =~ /^#/);
my($chr,$pos,$id,$ref,$alt,$qual,$filter,$info,$format,$datas_join) = split/\t/,$_,10;
if(defined $qpass){
next unless($filter eq "PASS" or $filter eq "SnpCluster");
}
# Check if the variant is a bi-allelic SNP
next unless(length($ref) == 1 and length($alt) == 1);
# Check if the position locates in the candidate regions
goto NOBED unless(defined $regList);
next unless(exists $hash_bed{$chr});
if($chr ne $chr_tmp){
@regions = ();
foreach my $start(sort {$a <=> $b} keys %{$hash_bed{$chr}}){
my $end = $hash_bed{$chr}{$start};
push @regions, "$start,$end";
}
$chr_tmp = $chr;
if(exists $hash_chr{$chr}){
delete($hash_chr{$chr});
}
@remained_chrs = keys %hash_chr;
print "\t# Reading chr:$chr\n";
if(@regions == 0){
print "\t# Skipping chr:$chr\n";
}
}
if(@remained_chrs == 0 and @regions == 0){
last;
}
if(@regions == 0){
next;
}
my $pickIt = 0;
for(my $i = 0; $i < @regions; $i++){
my($start,$end) = split/,/,$regions[$i];
if($pos < $start){
last;
}elsif($pos > $end){
splice(@regions, $i, 1);
$i--;
next;
}else{
$pickIt = 1;
last;
}
}
next unless($pickIt == 1);
NOBED:
my @alts = split/,/,$alt;
my @bases = ($ref,@alts);
my @datas = split/\t/, $datas_join;
my $qData = $datas[$qRank];
my $base;
if($qData =~ /(\d+)\/(\d+)/ and $1 == $2){
$base = $bases[$1];
}else{
next;
}
$hash_query{$chr}{$pos}{$ref}{base} = $base;
}
close IN;
# read target vcf file
open(IN,"<$tvcf") or die $!;
open(OUT,">$outName.values");
# general settings
my @allSamples;
my @tRanks;
my @tSamples;
# chr settings
@regions = ();
@remained_chrs = ();
# window settings
$chr_tmp = "";
my $window_tmp = -1;
my $qCoverNum = 0;
my @tCoverNums;
my @identiNums;
while(<IN>){
chomp;
if($_ =~ /^#CHROM/){
(undef,undef,undef,undef,undef,undef,undef,undef,undef,@allSamples) = split/\t/;
if(defined $keepList){
for(my $i = 0; $i < @allSamples; $i++){
next unless(exists $hash_sample{$allSamples[$i]});
$hash_sample{$allSamples[$i]}{rank} = $i;
push @tRanks, $i;
push @tSamples, $allSamples[$i];
}
}else{
for(my $i = 0; $i < @allSamples; $i++){
$hash_sample{$allSamples[$i]}{rank} = $i;
push @tRanks, $i;
push @tSamples, $allSamples[$i];
}
}
unless(@tRanks > 0){
print "#ERROR: no target sample found in the vcf file.\n";
die;
}
print "\t# sample number=".@tRanks."\n";
print OUT "#CHROM\tSTART\tEND\tMKNUM\t".join("\t",@tSamples)."\n";
next;
}
next if($_ =~ /^#/);
my($chr,$pos,$id,$ref,$alt,$qual,$filter,$info,$format,$datas_join) = split/\t/,$_,10;
next unless(exists $hash_query{$chr}{$pos}{$ref});
if(defined $tpass){
next unless($filter eq "PASS" or $filter eq "SnpCluster");
}
# Check if the variant is a bi-allelic SNP
next unless(length($ref) == 1 and length($alt) == 1);
# Check if the position locates in the candidate regions
goto NOBED unless(defined $regList);
next unless(exists $hash_bed{$chr});
if($chr ne $chr_tmp){
@regions = ();
if($qCoverNum > 0 and $window_tmp >= 0){
my @outDatas;
for(my $i = 0; $i < @tCoverNums; $i++){
push @outDatas, "$identiNums[$i]|$tCoverNums[$i]";
}
my $windowStart = $windowSize * $window_tmp + 1;
my $windowEnd = $windowSize * ($window_tmp + 1);
print OUT "$chr_tmp\t$windowStart\t$windowEnd\t$qCoverNum\t".join("\t",@outDatas)."\n";
$chr_tmp = $chr;
$window_tmp = -1;
$qCoverNum = 0;
@tCoverNums = ();
@identiNums = ();
}
foreach my $start(sort {$a <=> $b} keys %{$hash_bed{$chr}}){
my $end = $hash_bed{$chr}{$start};
push @regions, "$start,$end";
}
$chr_tmp = $chr;
if(exists $hash_chr{$chr}){
delete($hash_chr{$chr});
}
@remained_chrs = keys %hash_chr;
print "\t# Reading chr:$chr\n";
if(@regions == 0){
print "\t# Skipping chr:$chr\n";
}
}
if(@remained_chrs == 0 and @regions == 0){
last;
}
if(@regions == 0){
next;
}
my $pickIt = 0;
for(my $i = 0; $i < @regions; $i++){
my($start,$end) = split/,/,$regions[$i];
if($pos < $start){
last;
}elsif($pos > $end){
splice(@regions, $i, 1);
$i--;
next;
}else{
$pickIt = 1;
last;
}
}
next unless($pickIt == 1);
NOBED:
# dissect
my $windowRank = int($pos/$windowSize);
if($chr_tmp ne $chr or $window_tmp ne $windowRank){
if($window_tmp >= 0){
my @outDatas;
for(my $i = 0; $i < @tRanks; $i++){
unless(defined $identiNums[$i]){
$identiNums[$i] = 0;
}
unless(defined $tCoverNums[$i]){
$tCoverNums[$i] = 0;
}
push @outDatas, "$identiNums[$i]|$tCoverNums[$i]";
}
my $windowStart = $windowSize * $window_tmp + 1;
my $windowEnd = $windowSize * ($window_tmp + 1);
print OUT "$chr_tmp\t$windowStart\t$windowEnd\t$qCoverNum\t".join("\t",@outDatas)."\n";
}
$chr_tmp = $chr;
$window_tmp = $windowRank;
$qCoverNum = 0;
@tCoverNums = ();
@identiNums = ();
}
my @alts = split/,/,$alt;
my @bases = ($ref,@alts);
my @datas = split/\t/, $datas_join;
my $qBase = $hash_query{$chr}{$pos}{$ref}{base};
$qCoverNum++;
for(my $i = 0; $i < @tRanks; $i++){
my $tg = $datas[$tRanks[$i]];
my $tBase = "";
if($tg =~ /^(\d+)\/(\d+)/ and $1 eq $2){
$tBase = $bases[$1];
$tCoverNums[$i]++;
}
if($tBase eq $qBase){
$identiNums[$i]++;
}
}
}
close IN;
close OUT;