lib/Bio/PrimerDesigner/primer3.pm untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 42 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 262 CC 37 SCAR 60.9
CC
37
Cov
0%
design
812.0
check_params
30.0
request
20.0
CRAP 442.8
1 # $Id: primer3.pm,v 1.12 2005/08/23 13:51:14 smckay Exp $
2  
3 package Bio::PrimerDesigner::primer3;
4  
5 =head1 NAME
6  
7 Bio::PrimerDesigner::primer3 - An class for accessing primer3
8  
9 =head1 SYNOPSIS
10  
11 use Bio::PrimerDesigner::primer3;
12  
13 =head1 METHODS
14  
15 Methods are called using the simplifed alias for each primer3 result
16 or the raw primer3 BoulderIO key. Use the raw_output method to view the
17 raw output.
18  
19 =cut
20  
21 use strict;
22 use File::Spec::Functions 'catfile';
23 use File::Temp 'tempfile';
24 use Bio::PrimerDesigner::Remote;
25 use Bio::PrimerDesigner::Result;
26 use base 'Class::Base';
27  
28 use vars '$VERSION';
29 $VERSION = sprintf "%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/;
30  
31 # -------------------------------------------------------------------
32 sub binary_name {
33  
34 =pod
35  
36 =head2 binary_name
37  
38 Defines the binary's name on the system.
39  
40 =cut
41  
42 my $self = shift;
43 return 'primer3';
44 }
45  
46 # -------------------------------------------------------------------
47 sub check_params {
48  
49 =pod
50  
51 =head2 check_params
52  
53 Make sure we have required primer3 arguments.
54  
55 =cut
56  
57 my ( $self, $args ) = @_;
58  
59 if (
60 defined $args->{'PRIMER_LEFT_INPUT'} &&
61 defined $args->{'PRIMER_RIGHT_INPUT'}
62 ) {
63 return $self->error(
64 "Number of sets must be set to 1 for defined primers"
65 ) if $args->{'PRIMER_NUM_RETURN'} > 1;
66  
67 return $self->error("Sequence input is missing")
68 unless defined $args->{'SEQUENCE'};
69 }
70 else {
71 return $self->error("Required design paramaters are missing") unless
72 defined $args->{'PRIMER_SEQUENCE_ID'} &&
73 defined $args->{'SEQUENCE'} &&
74 defined $args->{'PRIMER_PRODUCT_SIZE_RANGE'};
75 }
76  
77 return 1;
78 }
79  
80 # -------------------------------------------------------------------
81 sub design {
82  
83 =pod
84  
85 =head2 design
86  
87 Build the primer3 config file, run primer3, then parse the results.
88 Expects to be passed a hash of primer3 input options. Returns an object
89 that can be used to call result methods.
90  
91 =cut
92  
93 my ( $self, $method, $loc, $args ) = @_;
94  
95 return $self->error('No arguments for design method' ) unless $args;
96
97 #
98 # Unalias incoming parameters if required.
99 #
100 my %aliases = $self->list_aliases;
101 my %lookup = reverse %aliases;
102 while ( my ( $k, $v ) = each %$args ) {
103 next if exists $aliases{ $k };
104 my $alias = $lookup{ $k } or return $self->error("No alias for '$k'");
105 delete $args->{ $k };
106 $args->{ $alias } = $v;
107 }
108  
109 #
110 # Check that everything required is present.
111 #
112 $self->check_params( $args ) or return $self->error;
113  
114 #
115 # Send request to designer.
116 #
117 my @data = $self->request( $method, $loc, $args );
118
119 #
120 # abort on empty or undefined data array
121 #
122 return '' unless @data && @data > 1;
123  
124 my $output = '';
125 my $count = 1;
126 my $result = Bio::PrimerDesigner::Result->new;
127  
128 for ( @data ) {
129 $output .= $_;# unless /^SEQUENCE=/;
130  
131 # save raw output into the results hash
132 my ($key, $value) = /(.+)=(.+)/;
133 $result->{$count}->{$key} = $value if $key && $value;
134  
135 # save aliased output
136 $result->{$count}->{'qual'} = $1
137 if /R_PAIR_PENALT\S+=(\S+)/ || /R_PAIR_QUAL\S+=(\S+)/;
138 $result->{$count}->{'left'} = $1
139 if /R_LEFT\S+SEQUENC\S+=(\S+)/;
140 $result->{$count}->{'right'} = $1
141 if /R_RIGHT\S+SEQUENC\S+=(\S+)/;
142 $result->{$count}->{'startleft'} = $1
143 if /R_LEFT_?\d*=(\d+),\d+/;
144 $result->{$count}->{'startright'} = $1
145 if /R_RIGHT_?\d*=(\d+),\d+/;
146 $result->{$count}->{'lqual'} = $1
147 if /R_LEFT\S*_PENALT\S+=(\S+)/ || /R_LEFT\S*_QUAL\S+=(\S+)/;
148 $result->{$count}->{'rqual'} = $1
149 if /R_RIGHT\S*_PENALT\S+=(\S+)/ || /R_RIGHT\S*_QUAL\S+=(\S+)/;
150 $result->{$count}->{'leftgc'} = int $1
151 if /R_LEFT\S+GC_PERCEN\S+=(\S+)/;
152 $result->{$count}->{'rightgc'} = int $1
153 if /R_RIGHT\S+GC_PERCEN\S+=(\S+)/;
154 $result->{$count}->{'lselfany'} = int $1
155 if /R_LEFT\S+SELF_AN\S+=(\S+)/;
156 $result->{$count}->{'rselfany'} = int $1
157 if /R_RIGHT\S+SELF_AN\S+=(\S+)/;
158 $result->{$count}->{'lselfend'} = int $1
159 if /R_LEFT\S+SELF_EN\S+=(\S+)/;
160 $result->{$count}->{'rselfend'} = int $1
161 if /R_RIGHT\S+SELF_EN\S+=(\S+)/;
162 $result->{$count}->{'lendstab'} = int $1
163 if /R_LEFT\S+END_STABILIT\S+=(\S+)/;
164 $result->{$count}->{'rendstab'} = int $1
165 if /R_RIGHT\S+END_STABILIT\S+=(\S+)/;
166 $result->{$count}->{'pairendcomp'}= int $1
167 if /R_PAIR\S+COMPL_EN\S+=(\S+)/;
168 $result->{$count}->{'pairanycomp'}= int $1
169 if /R_PAIR\S+COMPL_AN\S+=(\S+)/;
170 $result->{$count}->{'hyb_oligo'}= lc $1
171 if /PRIMER_INTERNAL_OLIGO_SEQUENC\S+=(\S+)/;
172
173 #
174 # round up Primer Tm's
175 #
176 $result->{$count}->{'hyb_tm'}= int (0.5 + $1)
177 if /PRIMER_INTERNAL_OLIGO\S+TM=(\d+)/;
178  
179 $result->{$count}->{'tmleft'} = int (0.5 + $1)
180 if (/^PRIMER_LEFT.*_TM=(\S+)/);
181  
182 $result->{$count}->{'tmright'} = int (0.5 + $1)
183 if (/^PRIMER_RIGHT.*_TM=(\S+)/);
184  
185 #
186 # product size key means that we are at the end of each primer set
187 #
188 $result->{$count}->{'prod'} = $1 and $count++
189 if /^PRIMER_PRODUCT_SIZ\S+=(\S+)/ && !/RANGE/;
190
191 #
192 # abort if we encounter a primer3 error message
193 #
194 if (/PRIMER_ERROR/) {
195 $self->error("Some sort of primer3 error:\n$output");
196 return '';
197 }
198 }
199  
200 #
201 # save the raw primer3 output (except for input sequence -- too big)
202 #
203 $result->{1}->{'raw_output'} = $output;
204
205 return $result;
206 }
207  
208  
209 # -------------------------------------------------------------------
210 sub request {
211  
212 =pod
213  
214 =head2 request
215  
216 Figures out where the primer3 binary resides and accesses it with a
217 list of parameters for designing primers.
218  
219 =cut
220  
221 my ( $self, $method, $loc, $args ) = @_;
222 $method ||= 'remote';
223  
224 my $config = '';
225 while ( my ( $key, $value ) = each %$args ) {
226 $config .= "$key=$value\n";
227 }
228  
229 my @data = ();
230  
231 if ( $method eq 'remote' ) {
232 my $cgi = Bio::PrimerDesigner::Remote->new
233 or return $self->error('could not make remote object');
234 my $url = $loc;
235 $args->{'program'} = $self->binary_name;
236 @data = $cgi->CGI_request( $url, $args );
237 }
238 else { # "local"
239 my $path = $loc;
240 my $binary_name = $self->binary_name or return;
241 my $binary_path = catfile( $path, $binary_name );
242 return $self->error("Can't execute local binary '$binary_path'")
243 unless -x $binary_path;
244  
245 my ( $tmp_fh, $tmp_file ) = tempfile;
246 print $tmp_fh $config, "=\n";
247 close $tmp_fh;
248
249 #
250 # send the instructions to primer3 and get results
251 #
252 open RESULT_FILE, "$binary_path < $tmp_file |";
253 @data = <RESULT_FILE>;
254 close RESULT_FILE;
255 unlink $tmp_file;
256 }
257
258 if ( $self->check_results( $method, @data ) ) {
259 return @data;
260 }
261 else {
262 return '';
263 }
264 }
265  
266 # -------------------------------------------------------------------
267 sub check_results {
268  
269 =pod
270  
271 =head2 check_results
272  
273 Verify the validity of the design results.
274  
275 =cut
276  
277 my $self = shift;
278 my $method = shift;
279 my $results = join '', grep {defined} @_;
280  
281 my $thing = $method eq 'remote' ? 'URL' : 'binary';
282 my $problem = "Possible problem with the primer3 $thing";
283
284 if ( $results =~ /SEQUENCE=/m ) {
285 return 1;
286 }
287 else {
288 return $self->error("Primer design failure:\n", $problem);
289 }
290 }
291  
292 # -------------------------------------------------------------------
293 sub list_aliases {
294  
295 =pod
296  
297 =head2 list_aliases
298  
299 Prints a list of shorthand aliases for the primer3 BoulderIO
300 input format. The full input/ouput options and the aliases can be
301 used interchangeably.
302  
303 =cut
304  
305 my $self = shift;
306
307 return (
308 PRIMER_SEQUENCE_ID => 'id',
309 SEQUENCE => 'seq',
310 INCLUDED_REGION => 'inc',
311 TARGET => 'target',
312 EXCLUDED_REGION => 'excluded',
313 PRIMER_COMMENT => 'comment',
314 PRIMER_SEQUENCE_QUALITY => 'quality',
315 PRIMER_LEFT_INPUT => 'leftin',
316 PRIMER_RIGHT_INPUT => 'rightin',
317 PRIMER_START_CODON_POSITION => 'start_cod_pos',
318 PRIMER_PICK_ANYWAY => 'pickanyway',
319 PRIMER_MISPRIMING_LIBRARY => 'misprimelib',
320 PRIMER_MAX_MISPRIMING => 'maxmisprime',
321 PRIMER_PAIR_MAX_MISPRIMING => 'pairmaxmisprime',
322 PRIMER_PRODUCT_MAX_TM => 'prodmaxtm',
323 PRIMER_PRODUCT_MIN_TM => 'prodmintm',
324 PRIMER_EXPLAIN_FLAG => 'explain',
325 PRIMER_PRODUCT_SIZE_RANGE => 'sizerange',
326 PRIMER_GC_CLAMP => 'gcclamp',
327 PRIMER_OPT_SIZE => 'optpsize',
328 PRIMER_INTERNAL_OLIGO_OPT_SIZE => 'hyb_opt_size',
329 PRIMER_MIN_SIZE => 'minpsize',
330 PRIMER_MAX_SIZE => 'maxpsize',
331 PRIMER_OPT_TM => 'opttm',
332 PRIMER_MIN_TM => 'mintm',
333 PRIMER_MAX_TM => 'maxtm',
334 PRIMER_MAX_DIFF_TM => 'maxtmdiff',
335 PRIMER_MIN_GC => 'mingc',
336 PRIMER_OPT_GC_PERCENT => 'optgc',
337 PRIMER_MAX_GC => 'maxgc',
338 PRIMER_SALT_CONC => 'saltconc',
339 PRIMER_DNA_CONC => 'dnaconc',
340 PRIMER_NUM_NS_ACCEPTED => 'maxN',
341 PRIMER_SELF_ANY => 'selfany',
342 PRIMER_SELF_END => 'selfend',
343 PRIMER_DEFAULT_PRODUCT => 'sizerangelist',
344 PRIMER_MAX_POLY_X => 'maxpolyX',
345 PRIMER_LIBERAL_BASE => 'liberal',
346 PRIMER_NUM_RETURN => 'num',
347 PRIMER_FIRST_BASE_INDEX => '1stbaseindex',
348 PRIMER_MAX_END_STABILITY => 'maxendstab',
349 PRIMER_PRODUCT_OPT_TM => 'optprodtm',
350 PRIMER_PRODUCT_OPT_SIZE => 'optprodsize',
351 PRIMER_WT_TM_GT => 'wt_tm_gt',
352 PRIMER_WT_TM_LT => 'wt_tm_lt',
353 PRIMER_WT_SIZE_LT => 'wt_size_lt',
354 PRIMER_WT_SIZE_GT => 'wt_size_gt',
355 PRIMER_WT_GC_PERCENT_LT => 'wt_gc_lt',
356 PRIMER_WT_GC_PERCENT_GT => 'wt_gc_gt',
357 PRIMER_WT_COMPL_ANY => 'wt_comp_any',
358 PRIMER_WT_COMPL_END => 'wt_comp_end',
359 PRIMER_WT_NUM_NS => 'wt_numN',
360 PRIMER_WT_REP_SIM => 'wt_rep_sim',
361 PRIMER_WT_SEQ_QUAL => 'wt_seq_qual',
362 PRIMER_WT_END_QUAL => 'wt_end_qual',
363 PRIMER_WT_END_STABILITY => 'wt_end_stab',
364 PRIMER_PAIR_WT_PR_PENALTY => 'wt_pr_penalty',
365 PRIMER_PAIR_WT_DIFF_TM => 'wt_pr_tmdiff',
366 PRIMER_PAIR_WT_COMPL_ANY => 'wt_pr_comp_any',
367 PRIMER_PAIR_WT_COMPL_END => 'wt_pr_comp_end',
368 PRIMER_PAIR_WT_PRODUCT_TM_LT => 'wt_prodtm_lt',
369 PRIMER_PAIR_WT_PRODUCT_TM_GT => 'wt_prodtm_gt',
370 PRIMER_PAIR_WT_PRODUCT_SIZE_GT => 'wt_prodsize_gt',
371 PRIMER_PAIR_WT_PRODUCT_SIZE_LT => 'wt_prodsize_lt',
372 PRIMER_PAIR_WT_REP_SIM => 'wt_repsim',
373 PRIMER_PICK_INTERNAL_OLIGO => 'hyb_oligo',
374 PRIMER_LEFT_EXPLAIN => 'left_explain',
375 PRIMER_RIGHT_EXPLAIN => 'right_explain',
376 PRIMER_PAIR_EXPLAIN => 'pair_explain',
377 PRIMER_INTERNAL_OLIGO_EXPLAIN => 'hyb_explain',
378 PRIMER_INTERNAL_OLIGO_MIN_SIZE => 'hyb_min_size',
379 PRIMER_INTERNAL_OLIGO_MAX_SIZE => 'hyb_max_size',
380 );
381 }
382  
383 # -------------------------------------------------------------------
384 sub list_params {
385  
386 =pod
387  
388 =head2 list_params
389  
390 Returns a list of primer3 configuration options. primer3 will use
391 reasonable default options for most parameters.
392  
393 =cut
394  
395  
396 my $self = shift;
397  
398 return (
399 'PRIMER_SEQUENCE_ID (string, optional)',
400 'SEQUENCE (nucleotide sequence, REQUIRED)',
401 'INCLUDED_REGION (interval, optional)',
402 'TARGET (interval list, default empty)',
403 'EXCLUDED_REGION (interval list, default empty)',
404 'PRIMER_COMMENT (string, optional)',
405 'PRIMER_SEQUENCE_QUALITY (quality list, default empty)',
406 'PRIMER_LEFT_INPUT (nucleotide sequence, default empty)',
407 'PRIMER_RIGHT_INPUT (nucleotide sequence, default empty)',
408 'PRIMER_START_CODON_POSITION (int, default -1000000)',
409 'PRIMER_PICK_ANYWAY (boolean, default 0)',
410 'PRIMER_MISPRIMING_LIBRARY (string, optional)',
411 'PRIMER_MAX_MISPRIMING (decimal,9999.99, default 12.00)',
412 'PRIMER_PAIR_MAX_MISPRIMING (decimal,9999.99, default 24.00)',
413 'PRIMER_PRODUCT_MAX_TM (float, default 1000000.0)',
414 'PRIMER_PRODUCT_MIN_TM (float, default -1000000.0)',
415 'PRIMER_EXPLAIN_FLAG (boolean, default 0)',
416 'PRIMER_PRODUCT_SIZE_RANGE (size range list, default 100-300)',
417 'PRIMER_GC_CLAMP (int, default 0)',
418 'PRIMER_OPT_SIZE (int, default 20)',
419 'PRIMER_MIN_SIZE (int, default 18)',
420 'PRIMER_MAX_SIZE (int, default 27)',
421 'PRIMER_OPT_TM (float, default 60.0C)',
422 'PRIMER_MIN_TM (float, default 57.0C)',
423 'PRIMER_MAX_TM (float, default 63.0C)',
424 'PRIMER_MAX_DIFF_TM (float, default 100.0C)',
425 'PRIMER_MIN_GC (float, default 20.0%)',
426 'PRIMER_OPT_GC_PERCENT (float, default 50.0%)',
427 'PRIMER_MAX_GC (float, default 80.0%)',
428 'PRIMER_SALT_CONC (float, default 50.0 mM)',
429 'PRIMER_DNA_CONC (float, default 50.0 nM)',
430 'PRIMER_NUM_NS_ACCEPTED (int, default 0)',
431 'PRIMER_SELF_ANY (decimal,9999.99, default 8.00)',
432 'PRIMER_SELF_END (decimal 9999.99, default 3.00)',
433 'PRIMER_DEFAULT_PRODUCT (size range list, default 100-300)',
434 'PRIMER_MAX_POLY_X (int, default 5)',
435 'PRIMER_LIBERAL_BASE (boolean, default 0)',
436 'PRIMER_NUM_RETURN (int, default 5)',
437 'PRIMER_FIRST_BASE_INDEX (int, default 0)',
438 'PRIMER_MAX_END_STABILITY (float 999.9999, default 100.0)',
439 'PRIMER_PRODUCT_OPT_TM (float, default 0.0)',
440 'PRIMER_PRODUCT_OPT_SIZE (int, default 0)',
441 '',
442 '** PENALTY WEIGHTS **',
443 '',
444 'PRIMER_WT_TM_GT (float, default 1.0)',
445 'PRIMER_WT_TM_LT (float, default 1.0)',
446 'PRIMER_WT_SIZE_LT (float, default 1.0)',
447 'PRIMER_WT_SIZE_GT (float, default 1.0)',
448 'PRIMER_WT_GC_PERCENT_LT (float, default 1.0)',
449 'PRIMER_WT_GC_PERCENT_GT (float, default 1.0)',
450 'PRIMER_WT_COMPL_ANY (float, default 0.0)',
451 'PRIMER_WT_COMPL_END (float, default 0.0)',
452 'PRIMER_WT_NUM_NS (float, default 0.0)',
453 'PRIMER_WT_REP_SIM (float, default 0.0)',
454 'PRIMER_WT_SEQ_QUAL (float, default 0.0)',
455 'PRIMER_WT_END_QUAL (float, default 0.0)',
456 'PRIMER_WT_END_STABILITY (float, default 0.0)',
457 'PRIMER_PAIR_WT_PR_PENALTY (float, default 1.0)',
458 'PRIMER_PAIR_WT_DIFF_TM (float, default 0.0)',
459 'PRIMER_PAIR_WT_COMPL_ANY (float, default 0.0)',
460 'PRIMER_PAIR_WT_COMPL_END (float, default 0.0)',
461 'PRIMER_PAIR_WT_PRODUCT_TM_LT (float, default 0.0)',
462 'PRIMER_PAIR_WT_PRODUCT_TM_GT (float, default 0.0)',
463 'PRIMER_PAIR_WT_PRODUCT_SIZE_GT (float, default 0.0)',
464 'PRIMER_PAIR_WT_PRODUCT_SIZE_LT (float, default 0.0)',
465 'PRIMER_PAIR_WT_REP_SIM (float, default 0.0)',
466 );
467 }
468  
469 # -------------------------------------------------------------------
470 sub example {
471  
472 =pod
473  
474 =head2 example
475  
476 Runs a sample remote primer design job. Returns an
477 Bio::PrimerDesigner::Result object.
478  
479 =cut
480  
481 my $self = shift;
482 my $dna = $self->_example_dna;
483 my $length = length $dna;
484 my $result = $self->design(
485 'remote',
486 'dev.wormbase.org/db/seq/primer_designer.cgi',
487 {
488 num => 1,
489 seq => $dna,
490 sizerange => '100-200',
491 target => '150,10',
492 excluded => '1,30 400,' . ($length - 401),
493 id => 'test_seq'
494 }
495 ) or return $self->error("Can't get remote server call to work");
496
497 return $result;
498 }
499  
500 # -------------------------------------------------------------------
501 sub _example_dna {
502  
503 =pod
504  
505 =head2 _example_dna
506  
507 Returns an example DNA sequence.
508  
509 =cut
510  
511 my $self = shift;
512 return 'cagagttaaagagaaaactgataattttttttccatctttctcctcacttgtgaataaac' .
513 'taaacgcatttctgtggacgttccaagtgtaatatgagagttgttttcatttggaaatgc' .
514 'gggaatatattgaatcttccattagatgttcaggaatatataaatacgttgtctgctctg' .
515 'aaaattcacacggaaaatctaaaaattgtcaaattatagatttcattctcaaatgactat' .
516 'ataacattttatttttgcaatttcttttcaattaggaaacatttcaaaaagctacgttgt' .
517 'ttttcacattcaaaatgattactgtcggtgcgttcattttccgagtttttccaatttcac' .
518 'gcttgctcttcttcgtaaaaaactcgtaatttagaaattgtgtctagatcaaaaaaaaaa' .
519 'ttttctgagcaatcctgaatcaggcatgctctctaaacaactctcagatatctgagatat' .
520 'gggaagcaaattttgagaccttactagttataaaaatcattaaaaatcaacgccgacagt' .
521 'ttctcacagaaacttaaaccgaaaaatcccaacgaagacttcagctcttttttctttgaa';
522 }
523  
524 1;
525  
526 # -------------------------------------------------------------------
527  
528 =pod
529  
530 =head1 AUTHOR
531  
532 Copyright (C) 2003-2008 Sheldon McKay E<lt>mckays@cshl.eduE<gt>,
533 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
534  
535 =head1 LICENSE
536  
537 This program is free software; you can redistribute it and/or modify
538 it under the terms of the GNU General Public License as published by
539 the Free Software Foundation; version 2.
540  
541 This program is distributed in the hope that it will be useful, but
542 WITHOUT ANY WARRANTY; without even the implied warranty of
543 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
544 General Public License for more details.
545  
546 You should have received a copy of the GNU General Public License
547 along with this program; if not, write to the Free Software
548 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
549 USA.
550  
551 =head1 SEE ALSO
552  
553 Bio::PrimerDesigner::epcr.
554  
555 =cut