-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprimer_evaluation.pl
428 lines (388 loc) · 15 KB
/
primer_evaluation.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
#!/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/common.pm";
require "$Bin/product.pm";
require "$Bin/self_lib.pm";
require "$Bin/path.pm";
require "$Bin/io.pm";
my $BEGIN_TIME=time();
my $version="1.0.0";
#######################################################################################
# ------------------------------------------------------------------
# GetOptions
# ------------------------------------------------------------------
my ($foligo,$fbound,$fprobe,$fkey,$outdir);
our $REF_GRCh37;
my $fdatabases = $REF_GRCh37;
my $ptype = "face-to-face";
my $PCRsize=1000;
my $min_eff=0.00001;
my $min_tm_spec=45;
my $opt_tm = 60;
my $opt_tm_probe = 70;
my $max_prodn=50;
my $thread = 10;
my $etype="SinglePlex";
my ($AllEvalue, $OutAllProduct, $Methylation, $NoSpecificity);
my $range = "120,160,80,200";
GetOptions(
"help|?" =>\&USAGE,
"io:s"=>\$foligo,
"ib:s"=>\$fbound,
"id:s"=>\$fdatabases,
"k:s"=>\$fkey,
"tp:s"=>\$ptype,
"ep:s"=>\$etype,
"AllEvalue:s"=>\$AllEvalue,
"OutAllProduct:s"=>\$OutAllProduct,
"Methylation:s"=>\$Methylation,
"NoSpecificity:s"=>\$NoSpecificity,
"rd:s"=>\$range,
"tm:s"=>\$opt_tm,
"tmb:s"=>\$opt_tm_probe,
"sz:s"=>\$PCRsize,
"mp:s"=>\$max_prodn,
"stm:s"=>\$min_tm_spec,
"me:s"=>\$min_eff,
"td:s"=>\$thread,
"od:s"=>\$outdir,
) or &USAGE;
&USAGE unless ($foligo and $fkey);
$outdir||="./";
`mkdir $outdir` unless (-d $outdir);
$outdir=AbsolutePath("dir",$outdir);
my @lendif=(0,4,0,8); ## tm diff between F and R primer
my @tmdif=(0,3,0,6); ## tm diff between F and R primer
my @rdis=split /,/, $range;
my ($mind, $maxd)=($rdis[2], $rdis[3]);
my ($fulls_pos, $fulls_dis, $fulls_lend, $fulls_tmd, $fulls_prod)=(20,30,10,10,30);
my ($w1, $w2, $wr, $wp)=(0.3, 0.3, 0.4, 0);
my $fevalue;
my $ftype;
my $info = `tail -1 $foligo`;
chomp $info;
my $col = scalar(split /\s+/, $info);
my $idx;
if($col ==2){
$ftype="Common";
$idx = 0;
}elsif($col==24 || $col==23){
$ftype="Evalue";
$idx = 3;
$fevalue = $foligo;
}
if(!defined $fbound){
### evalue single oligo
if($ftype ne "Common"){
die "Wrong file type: must be Common(2column: id seq) when not defined -ib!\n";
}
my $ftemplate = "$outdir/$fkey.template.fa";
`less $foligo|perl -ne '\''{chomp; \@a=split; print \">\$a[0]\\n\$a[1]\\n\";}'\'' >$ftemplate`;
my $cmd = "perl $Bin/oligo_evaluation.pl --nohead -p $foligo -t $ftemplate -d $fdatabases -thread $thread -stm $min_tm_spec --NoFilter -k $fkey -maxtime 100000000 -od $outdir";
if(defined $Methylation){
$cmd .= " --Methylation";
}
if(defined $NoSpecificity){
$cmd .= " --NoSpecificity";
}
&Run($cmd);
$fevalue = "$outdir/$fkey.evaluation.out";
$fbound = "$outdir/$fkey.bound.info";
}else{
if($ftype ne "Evalue"){
die "Wrong file type: must be Evalue type(24column) when defined -ib!\n";
}
}
my %record;
my $is_probe=0;
open(I, $foligo) or die $!;
while(<I>){
chomp;
next if(/^$/ || /^#/);
my @unit=split /\s+/, $_;
my $id=$unit[$idx];
my ($tid, $type)=$id=~/(\S+)-([12FRP])/;
if($type eq "P"){
$is_probe=1;
}
$record{$id}=0;
}
close(I);
if($is_probe){
($fulls_pos, $fulls_dis, $fulls_lend, $fulls_tmd, $fulls_prod)=(40,20,10,10,20);
($w1, $w2, $wr, $wp)=(0.25, 0.25, 0.25, 0.25);
}
### evalue combination oligos
my %bound;
my %product;
my %productp;
my @all_alignments = ();
if(!defined $NoSpecificity){
my %boundm;
&SHOW_TIME("#Read in Bound file");
open(B, $fbound) or die $!;
while(<B>){
chomp;
my ($id, $strand, $chr, $pos5, $seq, $tm, $end3_base, $mvisual)=split /\t/, $_;
next if(!exists $record{$id});
$record{$id}=1;
my ($tid, $type)=$id=~/(\S+)-([12FRP])/;
if(!defined $tid || !defined $type){
die "Wrong oligo ID $id: Primer ID must end by -F/R/1/2, eg. xxx-F, xxx-R, xxx-1, xxx-2; Probe ID must end by -P, eg. xxx-P!\n";
}
my $len = length $seq;
my $pos3=$strand eq "+"? $pos5+$len-1: $pos5-$len+1;
my $a = [$chr, $strand, $pos3, $pos5, $tm, $end3_base, $mvisual, $tid, $type, undef, undef, undef, undef];
# no-use eff eff_tm eff_end
push @{$bound{$type}{$tid}}, $a;
push @all_alignments, $a;
}
close(B);
foreach my $id(keys %record){
if($record{$id}==0){
print "Warn: $id not exist bound info and cannot evalue its products!\n";
}
}
my @tps = sort {$a cmp $b} keys %bound;
&SHOW_TIME("#Evalue");
&caculate_product_evaluation(\@all_alignments, $ptype, \%product, \%record, $PCRsize, $opt_tm, $min_tm_spec, $mind, $maxd, $min_eff, @all_alignments*@all_alignments, $etype, $AllEvalue, $tps[0]);
if($is_probe==1){#Probe
foreach my $tid(keys %{$bound{"P"}}){
# probe num on products
&probe_bounds_on_products($tid."-P", $bound{"P"}{$tid}, $product{$tid}{$tid}, \%productp, $PCRsize, $opt_tm_probe, $min_tm_spec+8); #my ($id, $abound, $aprod, $aresult)=@_;
}
}
}
### output final.result
if($ftype eq "Common"){ ## score
open(I, $fevalue) or die $!;
my @ids;
my %info;
while(<I>){
chomp;
next if(/^$/ || /^#/);
my ($abase, $afeature, $ameth, $aspec, $bnumtm)=&read_evaluation_info($idx, $_, $Methylation, 1); #spec: ($bnum,$btm,$binfo)
my ($id, $seq, $len) =@{$abase}[$idx..($idx+2)];
my ($tid,$tp)=$id=~/(\S+)-([12FRP])$/;
$tp=~s/F/1/;
$tp=~s/R/2/;
@{$info{$tid}{$tp}}=($abase, $afeature, $ameth, $aspec, $bnumtm);
push @ids, $id;
}
close(I);
if(defined $OutAllProduct){
open(P, ">$outdir/$fkey.final.pair.product") or die $!;
}
open(O, ">$outdir/$fkey.final.result") or die $!;
print O "##ScoreOligo: total score | ". &score_des("Primer", $Methylation)."\n";
if($is_probe==1){
print O "##ScoreOligo(Probe) : total score | ". &score_des("Probe", $Methylation)."\n";
}
print O &final_evalue_head($Methylation, $NoSpecificity)."\n";
#### score
my %score_rel;
my %score_info;
my %prod_info;
foreach my $tid(sort {$a cmp $b} keys %info){
my ($abase1, $afeature1, $ameth1, $aspec1, $bnumtm1) = @{$info{$tid}{1}};
my ($abase2, $afeature2, $ameth2, $aspec2, $bnumtm2) = @{$info{$tid}{2}};
my ($id1, $seq1, $len1) =@{$abase1}[$idx..($idx+2)];
my ($id2, $seq2, $len2) =@{$abase2}[$idx..($idx+2)];
## score oligo
my ($tid,$tp1)=$id1=~/(\S+)-([12FRP])$/;
my ($s1, $s2, $score_info1, $score_info2);
if(defined $Methylation){
($s1, $score_info1)=&primer_meth_score($opt_tm, $len1, @{$afeature1}, @{$ameth1}, $bnumtm1);
($s2, $score_info2)=&primer_meth_score($opt_tm, $len2, @{$afeature2}, @{$ameth2}, $bnumtm2);
}else{
($s1, $score_info1)=&primer_oligo_score($opt_tm, $len1, @{$afeature1}, $bnumtm1);
($s2, $score_info2)=&primer_oligo_score($opt_tm, $len2, @{$afeature2}, $bnumtm2);
}
@{$score_info{$id1}}=($s1, $score_info1);
@{$score_info{$id2}}=($s2, $score_info2);
## score for relation
my ($sp, $score_infop)=(0, "");
my $spos = $fulls_pos;
if(exists $info{$tid}{"P"}){
my ($abase, $afeature, $ameth, $aspec, $bnumtm) = @{$info{$tid}{"P"}};
my ($id, $seq, $len) =@{$abase}[$idx..($idx+2)];
my ($is_G5, $CGd) = &G_content($seq);
if(defined $Methylation){
($sp, $score_infop)=&probe_meth_score($opt_tm_probe, $len, @{$afeature}, @{$ameth}, $bnumtm, $is_G5, $CGd);
}else{
($sp, $score_infop)=&probe_oligo_score($opt_tm_probe, $len, @{$afeature}, $bnumtm, $is_G5, $CGd);
}
@{$score_info{$id}}=($sp, $score_infop);
my ($pnum, $apeff, $apinfos)=&get_highest_bound($productp{$id}, 1000000, "Eff");
my $pos=$apinfos->[0]=~/^(\S+)\//;
$spos=int(&score_single($pos, $fulls_pos, (1, 5, 0, 10))+0.5);
my ($peffs, $pinfos);
if($pnum!~/\+/ && $pnum<=3){
$peffs = join(",", @{$apeff});
$pinfos = join(";", @{$apinfos});
}else{
$peffs = join(",", @{$apeff}[0..2]);
$pinfos = join(";", @{$apinfos}[0..2]);
}
@prod_info{$id}=($pnum, $peffs, $pinfos);
if(defined $OutAllProduct){
print P ">$id\t$pnum\n";
my @peff=@{$apeff};
my @pinfo=@{$apinfos};
for(my $i=0; $i<@{$apeff}; $i++){
print P join("\t", $apinfos->[$i], $apeff->[$i]),"\n";
}
}
}
# score for tm diff
my $tmdif=abs($afeature1->[0] - $afeature2->[0]);
#print "tmdif:\t", join("\t", $tmdif, $fulls_tmd, @tmdif),"\n";
my $stmd=int(&score_single($tmdif, $fulls_tmd, @tmdif)+0.5);
# score for len diff
my $lendif=abs($len2-$len1);
#print "lendif:\t", join("\t", $lendif, $fulls_lend, @lendif),"\n";
my $slend=int(&score_single($lendif, $fulls_lend, @lendif)+0.5);
## product
my $sprod=$fulls_prod;
my $dis;
if(!defined $NoSpecificity){
my ($pnum, $apeff, $apinfos);
($pnum, $apeff, $apinfos)=&get_highest_bound($product{$tid}{$tid}, 1000000, "Eff");
my ($peffs, $pinfos);
if($pnum!~/\+/ && $pnum<=3){
$peffs = join(",", @{$apeff});
$pinfos = join(";", @{$apinfos});
}else{
$peffs = join(",", @{$apeff}[0..2]);
$pinfos = join(";", @{$apinfos}[0..2]);
}
@{$prod_info{$tid}}=($pnum, $peffs, $pinfos);
# print "prod:\t", join("\t", $pnum, $peffs, $pinfos),"\n";
$sprod = &bound_score($pnum, $peffs, $fulls_prod, "Eff");
($dis) = $apinfos->[0]=~/^(\S+)\//;
if(defined $OutAllProduct){
print P ">$tid\t$pnum\n";
my @peff=@{$apeff};
my @pinfo=@{$apinfos};
for(my $i=0; $i<@{$apeff}; $i++){
print P join("\t", $apinfos->[$i], $apeff->[$i]),"\n";
}
}
}
# score for dis
#print "dis:\t", join("\t", $dis, $fulls_dis, @rdis),"\n";
my $sdis=int(&score_single($dis, $fulls_dis, @rdis)+0.5);
my $srel=$spos+$slend+$stmd+$sdis+$sprod;
my $stotal = $s1*$w1 + $s2*$w2 + $srel*$wr + $sp*$wp;
$stotal = sprintf("%.1f", $stotal);
@{$score_info{$tid}}=($stotal, join(",", $spos, $sdis, $slend, $stmd, $sprod));
}
foreach my $id(@ids){
my ($tid,$tp)=$id=~/(\S+)-([12FRP])$/;
$tp=~s/F/1/;
$tp=~s/R/2/;
my ($abase, $afeature, $ameth, $aspec, $bnumtm) = @{$info{$tid}{$tp}};
my @prods = @{$prod_info{$tid}};
if($tp eq "P"){
@prods=@{$prod_info{$id}};
}
print O join("\t", @{$abase}, @{$score_info{$tid}}, join("|",@{$score_info{$id}}), @{$afeature}, @{$ameth},@prods),"\n";
}
close(I);
close(O);
if(defined $OutAllProduct){
close(P);
}
}
## output cross product
if($etype eq "MultiPlex" && !defined $NoSpecificity){
open(C, ">$outdir/$fkey.final.cross.product") or die $!;
foreach my $tid1(sort {$a cmp $b} keys %product){
foreach my $tid2(sort {$a cmp $b} keys %{$product{$tid1}}){
my ($tid1_t)=$tid1=~/(\S+)-[UDP]-/;
my ($tid2_t)=$tid2=~/(\S+)-[UDP]-/;
$tid1_t = defined $tid1_t? $tid1_t: $tid1;
$tid2_t = defined $tid2_t? $tid2_t: $tid2;
next if($tid1_t eq $tid2_t);
my ($pnum, $apeff, $apinfos)=&get_highest_bound($product{$tid1}{$tid2}, 1000000, "Eff");
print C ">$tid1\_$tid2\t$pnum\n";
my @peff=@{$apeff};
my @pinfo=@{$apinfos};
for(my $i=0; $i<@{$apeff}; $i++){
print C join("\t", $apeff->[$i], $apinfos->[$i]),"\n";
}
}
}
close(C);
}
#######################################################################################
print STDOUT "\nDone. Total elapsed time : ",time()-$BEGIN_TIME,"s\n";
#######################################################################################
# ------------------------------------------------------------------
# sub function
# ------------------------------------------------------------------
sub USAGE {#
my $usage=<<"USAGE";
Program:
Version: $version
Contact:zeng huaping<huaping.zeng\@genetalks.com>
-io: oligo file can be two format as following, ID must like: xxx-F, xxx-R, xxx-P, xxx-1, xxx-2.
Common Format:
ID1-F AAAAAAAAAAAA
ID1-P AAAAAAAAAAAAAAAA
ID1-R AAAAAAAAAAAA
ID2-1 AAAAAAAAAAAA
ID2-P AAAAAAAAAAAAAAAA
ID2-2 AAAAAAAAAAAA
Evalue Format: xxx.final.result
#Chr Start Strand ID Seq Len Dis2Target ProductSize ScoreTotal ScorePair ScoreOligo Tm GC Hairpin DimerType DimerSize EndANum EndStability SNP Poly OligoBound BoundNum HighestTm HighestInfo
chr1 100379124 - rs113994132-B1-1 TTTTTCTGTTCCACTCATCATATGAGA 27 0 162 286 25,11,10,20,30 41|3,10,0,7,10,-5,5,0 58.90 0.333 55.89 7.91 20.73 1 -6.7 0D1:TTTTTCTGTTCCACTCATCATATGAGE 22T5 10|58.84,49.32,48.69 1 1.00 162/chr1,P1,-100379124,|||||||||||||||||||||||||||,58.84,P2,+100378962,|||||||||||||||||||||||||||||,59.58
chr1 100379097 - rs113994132-B1-P CCTTTATAGCCTTTCCTGAAAAATGACATAAGACATGGTA 40 40 0 286 30 63.5|1,3,10,5,10,10,3,7 66.39 0.350 30.67 -18.20 -6.10 2 0 NA 17A5,26T3,35T3 15|65.97,50.26,49.55 1 0.86 27/chr1,-100379097,||||||||||||||||||||||||||||||||||||||||,65.97:chr1,P1,-100379124,|||||||||||||||||||||||||||,58.84,P2,+100378962,|||||||||||||||||||||||||||||,59.58
-rd: distance range of pair primers, (best_min, best_max, min, max) separted by ",", example:
face-to-face: |---> P1 x dis_range: 120,160,80,200
(SNP) x P2 <---|
face-to-face: P1 |---> x dis_range:120,160,80,200
(Region) x <---| P2
________________
back-to-back: x <---| P2 dis_range:5,10,0,15
P1 |---> x (Overlap between p1 and p2: dis > 0)
__________
back-to-back: x <---| P2 dis_range:-50,-40,-60,-30
P1 |---> x (No overlap between p1 and p2: dis < 0)
_______________
Nested: P2 --->| x dis_range(P2-P1):-15,-10,-30,-3
P1 --->| x (dis < 0)
Usage:
Options:
-io <file> Input oligo file, Common or Evalue format(as shown in the above), forced
when -ib is given, must be Evalue format.
when -ib not given, must be Common format.
-ib <file> Input oligo's bounds file, evalue oligos when not given, optional
-id <files> Input database files separated by "," to evalue specificity, [$fdatabases]
-k <str> Key of output file, forced
-tp <str> primer type, "face-to-face", "back-to-back", "Nested", ["face-to-face"]
-ep <str> evalue type, "SinglePlex", "MultiPlex", ["SinglePlex"]
-mp <int> maximum products number to be caculated, to reduce running time. [$max_prodn]
--Methylation Design methylation oligos
--NoSpecificity Not evalue specificity
--AllEvalue Evalue all possible products, not only F<->R, contain F<->F and R<->R.
--OutAllProduct Output All Product Info.
-td <int> thread in bwa, [$thread]
-od <dir> Dir of output file, default ./
Caculating efficiency options:
-sz <int> max PCR fragment size, [$PCRsize]
-tm <int> optimal tm of primer, [$opt_tm]
-tmb <int> optimal tm of probe, [$opt_tm_probe]
-rd <str> distance range of pair primers, (best_min, best_max, min, max) separted by ",", [$range]
-stm <int> min tm to amplify when caculate specifity, [$min_tm_spec]
-me <float> min efficiency to consider a product, [$min_eff]
-h Help
USAGE
print $usage;
exit;
}