forked from z0on/2bRAD_denovo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcf2map.pl
executable file
·283 lines (257 loc) · 7.86 KB
/
vcf2map.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
#!/usr/bin/env perl
my $usage="
vcf2map.pl : replaces scaffold coordinates in the vcf file with their approximate locations
in the linkage map. Scaffolds not pinned by the map are discarded.
The input VCF file is assumed to be sorted by scaffold.
Arguments:
vcf=[filename]
map=[filename] : table where third column is linkage group, fourth column is map position,
fifth column is the scaffold ID, and sixth column is scaffold position.
cmmb=3 : recombination rate (centimorgans per megabase). Set to 0 to estimate
from data on a per-scaffold basis (be sure to check with
MapAnchoring_tests.R that the results are reasonable)
mindist=10000 : Minimum base distance to calculate recombination rate (cM/Mb)
Outputs three files mapAnchored_[input vcf filename] :
.vcf: recoded sorted vcf
.rec: inferred ecombination rate across genome, cM/Mb
.tab: table of new to old coordinates:
chromosome chrom.coordinate scaffold scaff.coordinate
.rec: table of inferred recombination rates (cM/Mb - tends to be super noisy though):
linkageGroup scaffoldSegment meanCoordinate recombRate
Example:
vcf2map.pl vcf=gatkFinal2digitifera.vcf map=oldMap.tab
Mikhail Matz, matz\@utexas.edu
May 2015
";
# to calculate means:
use List::Util qw(sum);
sub mean {
return sum(@_)/@_;
}
my $vcf="";
my $mapfile="";
my $cmmb=3;
#my $rescale=0;
my $mindist=2500;
if ("@ARGV"=~/vcf=(\S+)/) { $vcf=$1; } else { die $usage;}
if ("@ARGV"=~/map=(\S+)/) { $mapfile=$1; } else { die $usage;}
if ("@ARGV"=~/cmmb=(\S+)/) { $cmmb=$1; }
if ("@ARGV"=~/mindist=(\S+)/) { $mindist=$1; }
#if ("@ARGV"=~/rescale/) { $rescale=1; }
open MAP, $mapfile or die "cannot open map table $map\n";
my %map={};
my %lg={};
my %coord={};
my %lgcount={};
my @lgs=();
while (<MAP>){
chop;
my @line=split("\t",$_);
if (" @{$map{$line[4]}} "!~/$line[3]/) {
push @{$lg{$line[4]}},$line[2];
#warn "lgs for $line[4]: @{$lg{$line[4]}}\n";
push @lgs, $line[2] unless (" @lgs "=~/ $line[2] /);
$lgcount{$line[4]}{$line[2]}++;
push @{$map{$line[4]}}, $line[3] ;
push @{$coord{$line[4]}}, $line[5];
}
}
close MAP;
my @scaffolds=keys %lg;
# extracting consistently mapped scaffold segments:
# for multi-marker scaffolds, these are regions with at least two consecutive markers
# mapping to the same linkage group
my %onelg={};
my $onemap={};
my $onecoord={};
my %splits={};
my %lg2scaff={};
my $singletons=0;
foreach my $s (@scaffolds){
next if ($s=~/HASH/);
if (@{$lg{$s}}==1) {
my $sgl=$s.${$lg{$s}}[0];
$onelg{$sgl}=${$lg{$s}}[0];
push @{$lg2scaff{$l}},$sgl;
push @{$onemap{$sgl}},${$map{$s}}[0];
push @{$onecoord{$sgl}},${$coord{$s}}[0];
push @{$splits{$s}},$sgl;
$singletons++;
}
else {
my @goodlgs=();
for(my $i=0; my $l=${$lg{$s}}[$i]; $i++){
if ($l == ${$lg{$s}}[$i-1]) {
push @goodlgs,$l unless (" @goodlgs "=~/ $l /);
}
}
if (@goodlgs==0) {
#warn "\t\tambiguous mapping for $s: @{$lg{$s}}\n";
next;
}
foreach my $gl (@goodlgs) {
my $sgl=$s.$gl;
push @{$splits{$s}},$sgl;
for(my $i=0; my $l=${$lg{$s}}[$i]; $i++){
if ($l eq $gl) {
$onelg{$sgl}=$l;
push @{$lg2scaff{$l}},$sgl;
push @{$onemap{$sgl}},${$map{$s}}[$i];
push @{$onecoord{$sgl}},${$coord{$s}}[$i];
}
}
#warn "$sgl: @{$lg{$s}} => $onelg{$sgl}: @{$onecoord{$sgl}}\n";
}
}
}
@scaffolds=keys %onelg;
warn "\n",$#scaffolds+1," scaffolds\n",$singletons," of them are singletons\n";
# calculating recombination rates and scaffold orientations
# in multi-marker scaffolds, require monotonously increasing marker base coordinates
my %direction={};
my %meanmap={};
my @allrecs=();
foreach my $s (@scaffolds){
next if ($s=~/HASH/);
#warn "\n---------------------\n$s: @{$onemap{$s}} : @{$onecoord{$s}}\n";
if (@{$onemap{$s}}==1) {
$direction{$s}=1;
$meanmap{$s}=${$onemap{$s}}[0];
}
else {
my $mid=$#{$onemap{$s}}/2;
my $beg=0;
my $end=0;
my $all=0;
my $i=0;
my $monotone=1;
my $prevchange=0;
my @ratios;
for ($i=0;$i<@{$onemap{$s}};$i++){
if ($i>0 and $monotone) {
my $change=${$onecoord{$s}}[$i]-${$onecoord{$s}}[$i-1];
my $changem=${$onemap{$s}}[$i]-${$onemap{$s}}[$i-1];
if (abs($change)>$mindist){
if ($change*$prevchange<0 ) {
$monotone=0;
#warn "\tSHIFT $i change:$change;prev:$prevchange mono:$monotone\n";
}
else {
#warn "\t\t$i change:$change;prev:$prevchange prod:",$change*$prevchange," mono:$monotone\n";
$prevchange=$change;
push @ratios,abs($changem/$change);
}
}
}
$meanmap{$s}+=${$onemap{$s}}[$i];
if ($i<$mid) {$beg+=${$onecoord{$s}}[$i];}
elsif($i>$mid) {$end+=${$onecoord{$s}}[$i];}
}
$meanmap{$s}=sprintf("%.4f",$meanmap{$s}/@{$onemap{$s}});
if ($beg<=$end){$direction{$s}=1;}
else {$direction{$s}=-1;}
if ($prevchange and $monotone) {
$rec{$s}=mean(@ratios)*1e+6;
push @allrecs,@ratios;
#warn ">>>monotonous: @ratios MEAN:",mean(@ratios),"\n";
}
}
#warn "\t\tmean:$meanmap{$s} direction:$direction{$s}\n";
}
my $gr=mean(@allrecs)*1e+6;
@allrecs=sort{$a<=>$b} @allrecs;
my $grmed=$allrecs[sprintf("%.0f",@allrecs/2)]*1e+6;
warn "\nmean cM/Mb:\t$gr\nmedian cM/Mb:\t$grmed\n(",scalar(@allrecs)," comparisons)\n\n";
if ($cmmb!=0) { $grmed=$cmmb;}
warn "using cM/Mb = $grmed\n";
my $outvcf="mapAnchored_".$vcf;
open OUTV, ">$outvcf" or die "cannot create output vcf file $outvcf\n";
open VCF, $vcf or die "cannot open vcf file $vcf\n";
# re
my %data={};
my %tab={};
my $chr="";
my $add=0;
my $seen={};
while (<VCF>) {
if ($_=~/^#/) { print {OUTV} $_ and next;}
(my $s,my $co,my @rest)=split(/\t/,$_);
next unless @{$splits{$s}}>0;
my $nearest=1e+9;
my $spot=0;
for (my $i=0; my $sgl=${$splits{$s}}[$i];$i++){
my $c=mean(@{$onecoord{$sgl}});
if (abs($co-$c)<$nearest) {
$split=$sgl;
$nearest=abs($co-$c);
}
}
my $scale=$grmed;
# if ($rec{$split} and $rescale){
#warn "using native rec.rate for $split: $rec{$split}\n";
# $scale=$rec{$split};
# }
#if (!$direction{$s}) { warn "no dir: $s $meanmap{$split} @{$onecoord{$split}}\n";}
my $loc=sprintf("%.0f",1e+6*$meanmap{$split}/$scale+$direction{$split}*($co-sprintf("%.0f",mean(@{$onecoord{$split}}))));
#warn "$s: co:$co; meanmap:$meanmap{$split}; meanCoord:",mean(@{$onecoord{$split}}),"; loc:$loc\n";
my $ind=1e+10*$onelg{$split}+$loc;
if ($seen{$ind}) {
warn "$split $co repeated index:$ind mm:$meanmap{$split} @{$onecoord{$split}} $loc\n$tab{$ind}\n";
$loc+=1;
$ind+=1;
}
if ($seen{$ind}) {
warn "\t$split $co repeated index:$ind mm:$meanmap{$split} @{$onecoord{$split}} $loc\n\t$tab{$ind}\n";
$loc+=1;
$ind+=1;
}
$seen{$ind}++;
$data{$ind}=join("\t",@rest);
$data{$ind}="chr$onelg{$split}\t$loc\t".$data{$ind};
$tab{$ind}="chr$onelg{$split}\t$loc\t$s\t$co\n";
#warn "$s:$co: $onelg{$split} @{$onecoord{$split}} $loc\n";
}
my @indx=sort {$a <=> $b } keys %data;
my $count=0;
my $chr="";
my $add=0;
my $ch="";
my $l=0;
my @rest=();
foreach my $i (@indx) {
next if ($i=~/HASH/);
($ch,$l,@rest)=split(/\t/,$data{$i});
if ($ch ne $chr){
$chr=$ch;
$add=0-$l;
#warn "new chrom: $ch add: $add\n";
}
$l+=$add;
$data{$i}=$ch."\t".$l."\t".join("\t",@rest);
(my $cht, my $lt,my @restt)=split("\t",$tab{$i});
$tab{$i}=$ch."\t".$l."\t".join("\t",@restt);
$count++;
}
warn "\n$count variants anchored\n\n";
my $outtab=$outvcf;
$outtab=~s/\.vcf/\.tab/;
open OUTT, ">$outtab" or die "cannot create output tab file $outtab\n";
foreach my $i (@indx) {
next if ($i=~/HASH/);
print {OUTV} $data{$i};
print {OUTT} $tab{$i};
}
close OUTV;
my $outtab=$outvcf;
$outtab=~s/\.vcf/\.rec/;
open OUTT, ">$outtab" or die "cannot create output tab file $outtab\n";
foreach my $lg (@lgs){
my %seen={};
my @scafs =sort { $meanmap{$a} <=> $meanmap{$b} } @{$lg2scaff{$lg}};
#warn "lg$lg @scafs\n";
foreach my $s (@scafs){
next if ($s=~/HASH/ or $seen{$s} or !$rec{$s});
print {OUTT} "$lg\t$s\t",sprintf("%.0f",1e+6*$meanmap{$s}/$cmmb),"\t$rec{$s}\n";
$seen{$s}=1;
}
}