-
Notifications
You must be signed in to change notification settings - Fork 4
/
get_template.pl
432 lines (389 loc) · 11.6 KB
/
get_template.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/bin/perl -w
use strict;
use warnings;
use Getopt::Long;
use Data::Dumper;
use FindBin qw($Bin $Script);
use File::Basename qw(basename dirname);
require "$Bin/path.pm";
require "$Bin/common.pm";
require "$Bin/snp.pm";
my $BEGIN_TIME=time();
my $version="2.0";
#######################################################################################
# ------------------------------------------------------------------
# GetOptions
# ------------------------------------------------------------------
our $REF_GRCh37;
our $REF_GRCh37_SNP;
our $SAMTOOLS;
my ($ftarget,$fkey,$outdir);
my $Max_Dis = 20;
my $vcf;
my $fref = $REF_GRCh37;
my $fref_snp;
my $Extend = 200;
my $UD_devide;
my $die_check;
GetOptions(
"help|?" =>\&USAGE,
"i:s"=>\$ftarget,
"r:s"=>\$fref,
"s:s"=>\$fref_snp,
"k:s"=>\$fkey,
"dieCk:s"=>\$die_check,
"UD_devide:s"=>\$UD_devide,
"md:s"=>\$Max_Dis,
"et:s"=>\$Extend,
"od:s"=>\$outdir,
) or &USAGE;
&USAGE unless ($ftarget and $fkey);
$outdir||="./";
`mkdir $outdir` unless (-d $outdir);
$outdir=AbsolutePath("dir",$outdir);
$fref_snp = defined $fref_snp? $fref_snp: $fref;
my $Max_Group_Len = $Max_Dis;
my $Min_Dis_Detect = 0;
my %target_start;
my %target_end;
my $check_success=1;
my $ftype;
{
my $head = `head -1 $ftarget`;
chomp $head;
my @unit = split /\s+/, $head;
if($unit[2]=~/^\d+$/ && $unit[2]-$unit[1]<1000 && $unit[2]-$unit[1]>=0){
if($unit[5]=~/ID=/){
$ftype = "Target";
}else{
die "Wrong SNP file!\n";
}
}else{
$ftype ="VCF";
}
}
my %target;
my %idcheck;
open(T, $ftarget) or die $!;
while (<T>){
chomp;
next if(/^$/);
my ($chr, $s, $e, $ref, $alt, $info);
my ($id, $gene, $gstrand, $cpos);
if($ftype eq "VCF"){
#13 19685715 rs564281463 G GA 100 PASS AC=2263;AF=0.451877;AN=5008;
($chr, $s, $id, $ref, $alt)=split;
# $e=$s+length($ref)-1;
($s, $e, $ref, $alt) = &convert_StartEndRefAlt($s, $ref, $alt);
}else{
($chr, $s, $e, $ref, $alt, $info)=split;
#10 43609948 43609948 T C ID=COSM966;GENE=RET;STRAND=+;CDS=c.1900T>C;AA=p.C634R;CNT=13;SOURCE=50Gene
($id)=$info=~/ID=(\S+?);/;
}
if(!defined $s || !defined $e || $e-$s>100){
print "Wrong: ref base $ref($chr:$s-$e) is not right! Target: $_\n";
$check_success=0;
next;
}
if(!defined $id || $id eq "" || $id eq "-" || $id eq "NA"){
$id=join("_", $chr, $s);
}
## check ref base
if($ref ne "-"){
my $rinfo = `$SAMTOOLS faidx $fref $chr:$s-$e`;
my (undef, @seq)=split /\n/, $rinfo;
my $seq = join("", @seq);
$seq = uc($seq);
if($ref ne $seq){
print "Wrong: ref base $ref($chr:$s-$e $seq) of $id is not right!\n";
$check_success=0;
next;
}
}
$idcheck{$id}++;
push @{$target_start{$chr}{$s}},[$id];
push @{$target_end{$chr}{$e}}, [$id];
@{$target{$id}}=($s, $e, $chr, $ref, $alt);
}
close (T);
#### check id
#foreach my $id(keys %idcheck){
# if($idcheck{$id}>1){
# print STDERR "Repeat ID: $id, please correct and run again!\n";
# die;
# }
#}
if($check_success==0 && $die_check){
print "ref base check failed!\n";
die;
}
my %out;
if(defined $UD_devide){
open ($out{"U"}, ">$outdir/$fkey.template_U.fa") or die $!;
open ($out{"D"}, ">$outdir/$fkey.template_D.fa") or die $!;
open ($out{"Us"}, ">$outdir/$fkey.template_U_snp.fa") or die $!;
open ($out{"Ds"}, ">$outdir/$fkey.template_D_snp.fa") or die $!;
}else{
open ($out{"U"}, ">$outdir/$fkey.template.fa") or die $!;
open ($out{"Us"}, ">$outdir/$fkey.template_snp.fa") or die $!;
}
my %tem_target;
my %tseq;
foreach my $chr(sort {$a cmp $b} keys %target_start){
my @merge_region;
my @merge_id = ();
my $start;
my $end;
my $last;
my @s_sort = sort {$a <=>$b} keys %{$target_start{$chr}};
my @e_sort = sort {$a <=>$b} keys %{$target_end{$chr}};
my @s_group = &grouping_by_pos($Max_Dis, $Max_Group_Len, \@s_sort);
my @e_group = &grouping_by_pos($Max_Dis, $Max_Group_Len, \@e_sort);
## get template seq .fa
get_template_seq(\%tem_target, \%tseq, \@s_group, $target_start{$chr}, $chr, "Start");
get_template_seq(\%tem_target, \%tseq, \@e_group, $target_end{$chr}, $chr, "End");
}
## check target template-U and template-D are consistant, maybe not when indel is too long
foreach my $oid(sort{$a cmp $b}keys %target){
my $tidU = $tem_target{"tar"}{$oid}{"U"};
my $tidD = $tem_target{"tar"}{$oid}{"D"};
next if($tidU eq $tidD);
my @idsU=split /\|/, $tidU;
my @idsD=split /\|/, $tidD;
my %idsU;
foreach my $id(@idsU){
$idsU{$id}=1;
}
my @gr;
my $tidnew=$oid;
foreach my $id(@idsD){
if(exists $idsU{$id}){
push @gr, $id;
$tem_target{"tar"}{$id}{"U"}=$tidnew;
$tem_target{"tar"}{$id}{"D"}=$tidnew;
delete $tem_target{"tem"}{$tidU}{"U"}{$id};
delete $tem_target{"tem"}{$tidD}{"D"}{$id};
$tem_target{"tem"}{$tidnew}{"U"}{$id}=1;
$tem_target{"tem"}{$tidnew}{"D"}{$id}=1;
}
}
@{$tseq{$tidnew}{"U"}}=@{$tseq{$tidU}{"U"}};
@{$tseq{$tidnew}{"D"}}=@{$tseq{$tidD}{"D"}};
}
foreach my $tid(sort {$a cmp $b} keys %{$tem_target{"tem"}}){
my @oidU=sort{$a cmp $b} keys %{$tem_target{"tem"}{$tid}{"U"}};
my @oidD=sort{$a cmp $b} keys %{$tem_target{"tem"}{$tid}{"D"}};
next if(scalar @oidU==0);
#my $tidU=join("|", @oidU);
#my $tidD=join("|", @oidD);
my $tidU=$oidU[0];
my $tidD=$oidD[0];
if($tidU ne $tidD){
die "U target ids $tidU are not consitant with D target ids $tidD!\n";
}
my @s;
my @e;
foreach my $id(@oidU){
push @s, $target{$id}->[0];
push @e, $target{$id}->[1];
}
my @ssort = sort{$a<=>$b}@s;
my @esort = sort{$a<=>$b}@e;
my ($Us, $Ue) = ($Min_Dis_Detect, $ssort[-1]-$ssort[0]+$Min_Dis_Detect);
my ($Ds, $De) = ($Min_Dis_Detect, $esort[-1]-$esort[0]+$Min_Dis_Detect);
my ($pos_infoU, $seqU, $seqUs)=@{$tseq{$tid}{"U"}};
my ($pos_infoD, $seqD, $seqDs)=@{$tseq{$tid}{"D"}};
my $id_str = join(";", @oidU);
print {$out{"U"}} ">$tidU-U\tXS:i:$Us\tXE:i:$Ue\tXP:Z:$pos_infoU\tXI:Z:$id_str\n";
print {$out{"U"}} $seqU,"\n";
print {$out{"U"}} ">$tidU-D\tXS:i:$Ds\tXE:i:$De\tXP:Z:$pos_infoD\tXI:Z:$id_str\n";
print {$out{"U"}} $seqD,"\n";
print {$out{"Us"}} ">$tidU-U\tXS:i:$Us\tXE:i:$Ue\tXP:Z:$pos_infoU\tXI:Z:$id_str\n";
print {$out{"Us"}} $seqUs,"\n";
print {$out{"Us"}} ">$tidU-D\tXS:i:$Ds\tXE:i:$De\tXP:Z:$pos_infoD\tXI:Z:$id_str\n";
print {$out{"Us"}} $seqDs,"\n";
}
close($out{"U"});
close($out{"Us"});
if(defined $UD_devide){
close($out{"D"});
close($out{"Ds"});
}
#######################################################################################
print STDOUT "\nDone. Total elapsed time : ",time()-$BEGIN_TIME,"s\n";
#######################################################################################
# ------------------------------------------------------------------
# sub function
# ------------------------------------------------------------------
sub get_template_seq{
my ($atemp_target, $aseq, $agroups, $atarget, $chr, $mode)=@_;
my @groups = @{$agroups};
for(my $i=0; $i<@groups; $i++){
my @ids;
for(my $j=0; $j<@{$groups[$i]}; $j++){
if(!exists $atarget->{$groups[$i][$j]}){
print $groups[$i][$j],"\n";
die;
}
my @target_info = @{$atarget->{$groups[$i][$j]}};
for (my $k=0; $k<@target_info; $k++){
push @ids, $target_info[$k][0];
}
}
## get id
my $pos = $groups[$i][0];
my $pos_end = $groups[$i][-1];
my ($start, $end);
my $flank;
if ($mode eq "Start"){
$end = $pos-$Min_Dis_Detect;
$start = $end - $Extend;
if($start <0){
$start=0;
}
$flank = "U";
}else{
$start = $pos_end + $Min_Dis_Detect;
$end = $start + $Extend;
$flank = "D";
}
## get seq
my $seq=&faidx_seq($fref, $chr, $start, $end);
my $seq_snp=&faidx_seq($fref_snp, $chr, $start, $end);
$seq_snp=&seq_snp_add_target($seq_snp, $atarget, $chr, $start, $end); #add targetspot snp
my $strand = "+";
if($flank eq "U"){## targetspot is on the start of sequence
$seq = &revcom($seq);
$seq_snp = &revcom($seq_snp);
$strand = "-";
}
my $pos_info = $strand."$chr:$start-$end";
## store
my $tid = join("|", @ids);
foreach my $id(@ids){
$atemp_target->{"tem"}{$tid}{$flank}{$id}=1;
$atemp_target->{"tar"}{$id}{$flank}=$tid;
}
@{$aseq->{$tid}{$flank}}=($pos_info, $seq, $seq_snp);
}
}
sub seq_snp_add_target{
my ($seq_snp, $atarget, $chr, $start, $end)=@_;
my @unit = split //, $seq_snp;
my @pos;
foreach my $id(keys %target){
my ($s, $e, $c, $r, $a)=@{$target{$id}};
if($c eq $chr && $s<=$end && $e>=$start){
&sequence_convert_snp(\@unit, $s-$start+1, $r, $a); ## convert @unit directly
}
}
my $seq_new = join("", @unit);
return $seq_new;
}
sub faidx_seq{
my ($fref, $chr, $start, $end, $flank)=@_;
my $seq_info = `$SAMTOOLS faidx $fref $chr:$start-$end`;
my @line = split /\n/, $seq_info;
shift @line;
my $seq = join("", @line);
return ($seq);
}
sub grouping_by_pos{
my ($Max_Dis, $Max_Group_Len, $apos)=@_;
my @pos=@{$apos};
my $last;
my @final_group;
my @group;
for(my $i=0; $i<@pos+1; $i++){
if (!defined $last || ($i!=@pos && $pos[$i]-$last<=$Max_Dis)){
push @group, $pos[$i];
$last = $pos[$i];
next;
}
### handle with @group
my $len = $group[-1] - $group[0];
if ($len <= $Max_Group_Len){
push @final_group, [@group];
}else{
#print join("\t", @group),"\n";
my @groups= &break_group(@group);
#print Dumper @groups;
push @final_group, @groups;
}
### init
if($i!=@pos){
@group = ();
push @group, $pos[$i];
$last = $pos[$i];
}
}
return @final_group;
}
sub break_group{
my @group = @_;
my @final_group;
my $max_dis = 0;
for(my $k=0; $k<@group-1; $k++){
if($group[$k+1] - $group[$k] > $max_dis){
$max_dis = $group[$k+1] - $group[$k];
}
}
my @group_new1;
my @group_new2 = @group;
for(my $k=0; $k<@group-1; $k++){
if($group[$k+1] - $group[$k] < $max_dis){
push @group_new1, $group[$k];
shift @group_new2;
}elsif($group[$k+1] - $group[$k] == $max_dis){
push @group_new1, $group[$k];
shift @group_new2;
last;
}
}
## break
if($group_new1[-1]-$group_new1[0]>$Max_Group_Len){
my @grs = &break_group(@group_new1);
push @final_group, @grs;
}else{
push @final_group, [@group_new1];
}
if($group_new2[-1]-$group_new2[0]>$Max_Group_Len){
my @grs = &break_group(@group_new2);
push @final_group, @grs;
}else{
push @final_group, [@group_new2];
}
return @final_group;
}
sub USAGE {#
my $usage=<<"USAGE";
Program:
Version: $version
Contact:zeng huaping<huaping.zeng\@genetalks.com>
###input file format:
format 1:
chr7 107350577 107350577 A G ID=SLC26A4_2168A_G;GENE=SLC26A4;STRAND=+;CDS=c.2168A>G;AA=p.H723R;NM=NM_000441.1
chr7 107323898 107323898 A G ID=SLC26A4_919-2A_G;GENE=SLC26A4;STRAND=+;CDS=c.919-2A>G;AA=;NM=NM_000441.1
chr13 20763691 20763691 - C ID=GJB2_35dupG;GENE=GJB2;STRAND=-;CDS=c.35dupG;AA=p.V13fs;NM=NM_004004.5
chr13 20763421 20763422 AT - ID=GJB2_299delAT;GENE=GJB2;STRAND=-;CDS=c.299_300del;AA=p.H100fs;NM=NM_004004.5
format 2: like first 5 columns of vcf
chr1 10247 rs796996180 T C
chr1 10248 rs148908337 A T
chr1 10249 rs774211241 AAC A
chr1 10250 rs199706086 A C
Usage:
Options:
-i <file> Input file, forced
-r <file> Input ref file, [$fref]
-s <file> Input ref file containing snps, optional
-k <str> Key of output file, forced
--dieC die when ref base check failed
--UD_devide output U and D template file separately
-md <int> max distance permissible in one target spots cluster between two adjacent target spots , [$Max_Dis]
-et <int> extend length of UD flank of one target spot or target spots cluster , [$Extend]
-od <dir> Dir of output file, default ./
-h Help
USAGE
print $usage;
exit;
}