blib/conf/plugins/PrimerDesigner.pm untested

filter: Statementstmt 0.0% 0 / 788 Branchbran 0.0% 0 / 184 Conditioncond 0.0% 0 / 167 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 85 Podpod 0.0% 0 / 44 totaltotal 0.0% 0 / 1268 CC 93 SCAR 42.9
CC
93
Cov
0%
configure_form
306.0
make_centering_map
210.0
unit_label
110.0
CRAP 72.8
1 # $Id: PrimerDesigner.pm,v 1.11 2007-03-31 14:33:36 sheldon_mckay Exp $
2  
3 =head1 NAME
4  
5 Bio::Graphics::Browser2::Plugin::PrimerDesigner -- a plugin to design PCR primers with primer3
6  
7 =head1 SYNOPSIS
8  
9 This module is not used directly
10  
11 =head1 DESCRIPTION
12  
13 PrimerDesigner.pm uses the Bio::PrimerDesigner API for primer3 to design
14 PCR primers for features or target coordinates in gbrowse.
15  
16 =head1 PRIMER3
17
18 Compile a primer3 (v. 0.9 or later) binary executable for your
19 OS and copy it to the default path usr/local/bin with the name primer3.
20 Source code for primer3 can be obtained from
21 http://frodo.wi.mit.edu/primer3/primer3_code.html.
22  
23 =head1 Designing Primers
24  
25 =head2 Targeting a feature or coordinate
26  
27 The target for PCR primer design is selected by clicking on an image map.
28 For aggregate features such as gene models, etc, there is a mousover menu
29 to select the individual part of the whole feature
30  
31  
32 =head2 Design Paramaters
33  
34 The Provided set of reasonable default primer attributes will work in most
35 cases. Product size will vary by target feature size. A suggested PCR
36 product size range is calculated based on the selected feature. If this field
37 is left blank, a series of increasing PCR product sizes is cycled until
38 products big enough to flank the target feature are found. This will not
39 necessarily find the best primers, just the first ones that produce a big
40 enough product to flank the target. If the primers are flagged as low quality,
41 more optimal optimal primers may be found by specifying a specific size-range.
42  
43 =head1 Bio::Graphics::Browser
44  
45 This plugin contains an additional package Bio::Graphics::Browser2::faux.
46 This class inherits from Bio::Graphics::Browser. Its purpose is to
47 keep the Bio::Graphics::Browser funtionality and configuration data
48 while overriding image_map-related funtions required for this plugin.
49  
50 =head1 TO-DO
51  
52 Add support for ePCR-based scanning for false priming
53  
54 =head1 FEEDBACK
55  
56 See the GMOD website for information on bug submission http://www.gmod.org.
57  
58 =head1 AUTHOR - Sheldon McKay
59  
60 Email mckays@cshl.edi
61  
62 =head1 SEE ALSO
63  
64 Bio::PrimerDesigner (www.cpan.org)
65 primer3 (http://frodo.wi.mit.edu/primer3/primer3_code.html)
66  
67 =cut
68 package Bio::Graphics::Browser2::Plugin::PrimerDesigner;
69  
70 use strict;
71 use Data::Dumper;
72 use POSIX qw/isdigit/;
73 use Bio::Graphics::Panel;
74 use Bio::PrimerDesigner;
75 use Bio::PrimerDesigner::Tables;
76 use Bio::Graphics::Browser2::Plugin;
77 use Bio::Graphics::Browser2::Util;
78 #use Bio::Graphics::Browser; #No longer available in Gbrowse 2
79 use Bio::Graphics::Feature;
80 use Bio::Graphics::FeatureFile;
81 use CGI qw/:standard escape/;
82 use CGI::Pretty 'html3';
83 use CGI::Carp 'fatalsToBrowser';
84 use CGI::Toggle;
85 use Math::Round 'nearest';
86 use Carp qw/cluck/;
87  
88 use constant BINARY => 'primer3';
89 use constant BINPATH => '/usr/local/bin';
90 use constant METHOD => 'local';
91 use constant IMAGE_PAD => 25;
92 use constant MAXRANGE => 300;
93 use constant IMAGEWIDTH => 800;
94 use constant DEFAULT_SEG_SIZE => 10000;
95 use constant STYLE => '/gbrowse2/css/gbrowse.css';
96  
97 #use vars '@ISA';
98 use vars qw/@ISA $PNG $CALL/;
99  
100 @ISA = qw/Bio::Graphics::Browser2::Plugin/;
101  
102  
103 # Arg, modperl
104 END {
105
106 CGI::Delete_all();
107 }
108  
109  
110  
111 sub name {
112 'PCR primers';
113 }
114  
115 sub description {
116  
117 p( "This plugin uses PRIMER3 to pick PCR primers to amplify selected "
118 . "features or sequences."
119 . " This plugin was written by Sheldon McKay (mckays\@cshl.edu)" );
120  
121 }
122  
123 sub type {
124 'dumper';
125 }
126  
127 sub verb {
128  
129 #Remove the Configure button when designing primers. The plugin doesn't need
130 #the option anyway, and it will only confuse people if left on the page.
131  
132 #$CALL keeps track of how many times the module is run so it doesn't attach
133 #multiple events. Otherwise, the javascript will run about 3 times.
134  
135 #This is a cheat. Since there's no way to make javascript calls from ajax,
136 #code gets inserted here instead because GBrowse runs this no matter what.
137 #unless ($CALL) {
138  
139 #my $detail_select_menu = detail_select_menu();
140  
141 #print <<JS;
142  
143  
144 #<script type="text/javascript">
145  
146 #var oPrimerDesigner = { };
147  
148 #oPrimerDesigner.removeConfigureButton = function () {
149 #var plugin_select = document.getElementById('plugin');
150 #var config_button = document.getElementsByName('plugin_action')[0];
151  
152 #var i = plugin_select.selectedIndex;
153 #if (plugin_select.options[i].text == 'Design PCR primers')
154 #config_button.style.display = 'none';
155  
156 #plugin_select.onchange = function () {
157  
158 #var i = this.selectedIndex;
159 #if (this.options[i].text == 'Design PCR primers') {
160 #config_button.style.display = 'none';
161 #} else {
162 #config_button.style.display = 'inline';
163 #}
164  
165 #}
166  
167 #}
168 #if (window.addEventListener) { // W3C standard
169 #window.addEventListener('load', oPrimerDesigner.removeConfigureButton, false); // NB **not** 'onload'
170 #}
171 #else if (window.attachEvent) { // Microsoft
172 #window.attachEvent('onload', oPrimerDesigner.removeConfigureButton);
173 #}
174 #</script>
175 #JS
176 #$CALL++;
177 #}
178  
179 'Design';
180 }
181  
182 sub mime_type {
183  
184 'text/html';
185 }
186  
187 sub is_zoom {
188 return param('span') unless param('configured');
189 }
190  
191 sub reconfigure {
192 my $self = shift;
193 my $conf = $self->configuration;
194  
195 $conf->{size_range} = undef;
196 $conf->{target} = undef;
197 $conf->{lb} = undef;
198 $conf->{rb} = undef;
199  
200 my $target = $self->config_param('target');
201 my $lb = $self->config_param('lb');
202 my $rb = $self->config_param('rb');
203  
204 if ($lb && $rb) {
205 my $min_size = $rb - $lb + 40;
206 my $max_size = $min_size + MAXRANGE;
207  
208 # round to nearest 50 bp
209 $conf->{size_range} = join '-', map {$_||=50} nearest(50, $min_size, $max_size);
210  
211  
212 # make sure target is within the selected region
213 if (!$target || $target < $lb || $target > $rb) {
214 $target = int( ($lb+$rb)/2 );
215 }
216 }
217  
218 $conf->{target} = $target;
219 $conf->{lb} = $lb;
220 $conf->{rb} = $rb;
221 $conf->{span} = is_zoom;
222 $conf->{name} = $self->config_param('name');
223 $self->configuration($conf);
224 }
225  
226 sub my_url {
227 my $self = shift;
228 my $url = $self->{url};
229 return $url if $url;
230 $url = self_url();
231 $url =~ s/\?.+//;
232 return $self->{url} = $url;
233 }
234  
235 sub configure_form {
236 my $self = shift;
237 my ($segment,$target,$lb,$rb,$feats) = @_;
238  
239  
240 #($segment) = @{ $self->segments } unless $segment;
241 ($segment) = $self->segments unless $segment;
242 $segment ||= fatal_error("This plugin requires a sequence region");
243 my $browser = $self->browser_config;
244 my $conf = $self->configuration;
245  
246 my $no_buttons = 1 if !($lb || $rb) || $feats;
247
248 # make sure the target is not stale for the initial config
249 delete $conf->{target} if !($lb || $rb);
250  
251 my @feature_types = $self->selected_features;
252 my @args = ( -types => \@feature_types );
253
254 $target ||= $self->focus($segment);
255 $rb ||= $target;
256 $lb ||= $target;
257  
258 # primer design params
259 my $atts = $self->primer3_params($lb,$rb) unless $no_buttons;
260  
261 my $table_width = IMAGEWIDTH + 50;
262  
263 my ( $image, $map, $zoom_menu )
264 = $self->new_segment_map( $segment, $feats, $lb, $rb );
265  
266 my $message = '';
267  
268 my $start = $segment->start;
269 my $end = $segment->end;
270 my $ref = $segment->ref;
271 my $name = $conf->{name} || "$ref:$start..$end";
272  
273 my $length = unit_label( $segment->length );
274  
275 my $html = h2("Showing $length from $ref, positions $start to $end");
276  
277 $html .= hidden( -name => 'plugin', -value => 'PrimerDesigner' )
278 . hidden( -name => 'plugin_action', -value => 'Go' )
279 . hidden( -name => 'ref', -value => $segment->ref )
280 . hidden( -name => 'start', -value => $segment->start )
281 . hidden( -name => 'stop', -value => $segment->stop );
282 $html .= hidden( -name => $self->config_name('lb'), -value => $lb) if $lb;
283 $html .= hidden( -name => $self->config_name('rb'), -value => $rb) if $rb;
284 $html .= hidden( -name => $self->config_name('target'), -value => $target) if $target;
285  
286 my $map_text = $self->map_header;
287  
288 my $on = 1 unless $feats;
289 my $no_target = li("There currently is no target region selected.")
290 if ($rb - $lb) < 3;
291 #my $no_target = li("Click and drag on the ruler to select a PCR target.");
292 my $has_buttons = li("The size of potential PCR products can be adjusted via the 'Product size range' option below")
293 unless $no_buttons;
294 my $flanked = $no_target ? 'red line' : 'shaded region';
295 my $boundaries = li("The boundaries of the shaded target region can be adjusted by clicking on the lower scalebar")
296 unless $no_target;
297 my $click_feat = $no_target ? li("Click on a sequence feature to select")
298 : li("Click on a different sequence feature to change the selection");
299
300 my $zone =
301 $self->toggle( { on => $on, override => 0 },
302 'Targetting information',
303 font( {-size => -1},
304 ul( $no_target,
305 li("PCR primers will flank the $flanked."),
306 $click_feat,
307 $boundaries,
308 $has_buttons
309 ) )
310 )
311 . br;
312  
313  
314 $html .= table(
315 { -style => "width:${table_width}px" },
316 Tr(
317 { -class => 'searchtitle' },
318 [
319 #NML: map text does nothing at the moment. not even sure what it's supposed to do
320 #It doesn't appear in the functional 1.7 version
321 #th($map_text) .
322 th($zoom_menu),
323 td( { -class => 'searchbody', -colspan => 2 }, $image . br),
324 td( { -class => 'searchbody', -colspan => 2}, $zone )
325 ]
326 )
327 );
328  
329  
330 unless ($no_buttons) {
331 my @col1 = grep {/Primer|Tm|Product/} keys %$atts;
332 my @col2 = grep { !/Primer|Tm|Product/ } keys %$atts;
333  
334 @col1 = (
335 ( grep { $atts->{$_} =~ /Opt\./ } @col1 ),
336 ( grep { $atts->{$_} !~ /Opt\./ } @col1 )
337 );
338
339 my @rows = ( td( { -colspan => 4 }, h3($message) ),
340 td( { -colspan => 4 }, hr ) );
341
342 for ( 0 .. 4 ) {
343 push @rows, td(
344 [ $col1[$_], $atts->{ $col1[$_] }, $col2[$_], $atts->{ $col2[$_] } ] );
345 }
346
347 $html .= table( { -style => "width:${table_width}px" }, Tr( \@rows ) );
348 $html .= br
349 . submit( -name => 'configured', -value => 'Design Primers' )
350 . '&nbsp;'
351 . reset('Reset Parameters')
352 . '&nbsp;'
353 . $self->back_button;
354 }
355
356 (my $action = self_url()) =~ s/\?.+//;
357 $html = start_form(
358 -method => 'POST',
359 -name => 'mainform',
360 -action => $action
361 ).
362 $html
363 . end_form;
364 #die Dumper $html;
365  
366 # if this is the first config, exit before form and buttons
367 # are printed by gbrowse
368 if ($no_buttons && !$feats) {
369 my $style = $browser->setting('stylesheet') || STYLE;
370 print start_html( -style => $style, -title => 'PCR Primers'),
371 $html, $map, $browser->footer;
372 exit;
373 }
374  
375  
376  
377 #my $form = textfield(-name => $self->config_name('foo'),
378 #-value => $conf->{foo})
379 #.
380 #textfield(-name => $self->config_name('bar'),
381 #-value => $conf->{bar});
382 #return $form;
383  
384 return $feats ? ($html,$map) : $html.$map;
385 }
386  
387 sub map_header {
388 my $recenter = a(
389 { -href => '#',
390 -title => 'Click the top scale-bar to recenter the image'
391 },
392 'recenter'
393 );
394 my $select_t = a(
395 { -href => '#',
396 -title => 'Click a sequence feature below to select a target'
397 },
398 'select a PCR target'
399 );
400  
401 return "Click on the map to $recenter or $select_t";
402 }
403  
404 sub dump {
405 my ( $self, $segment ) = @_;
406 my $conf = $self->configuration;
407 #print Dumper $conf;
408 $self->reconfigure;
409  
410 #print Dumper $conf;
411 # dumpers provide their own headers, so make sure boiler plate
412 # stuff is included
413 #my $style_sheet = $self->browser_config->setting('stylesheet') || STYLE;
414 my $style_sheet = $self->browser_config->plugin_setting('stylesheet') || STYLE;
415 print start_html( -style => $style_sheet, -title => 'PCR Primers' );
416 print $self->browser_config->header_html;
417  
418 # reset off-scale target if required
419 delete $conf->{target} if $conf->{target}
420 && ($conf->{target} > $segment->end - 1000 || $conf->{target} < $segment->start + 1000);
421 delete $conf->{lb} if $conf->{lb}
422 && ($conf->{lb} > $segment->end - 1000 || $conf->{lb} < $segment->start);
423 delete $conf->{rb} if $conf->{rb}
424 && ($conf->{rb} < $segment->start + 1000 || $conf->{rb} > $segment->end);
425 delete $conf->{target} unless $conf->{lb} && $conf->{rb};
426 #print Dumper $conf;
427  
428 my $target = $self->focus($segment);
429 my $lb = $conf->{lb} || $target;
430 my $rb = $conf->{rb} || $target;
431  
432 # check for a zoom request
433 my $segment_size = $self->is_zoom;
434  
435 # Make room if target region is too close to the ends
436 my ($new_start,$new_end);
437 if ($rb >= $segment->end - 500) {
438 $new_end = $rb + 500;
439 }
440 if ($lb <= $segment->start + 500) {
441 $new_start = $lb - 500;
442 }
443 #print 'dump 1: '.$segment->start.' '.$segment->end.'<br>';
444  
445 # NML: this is causing the panel to zoom beyond the segment boundaries
446 #if ($new_start || $new_end) {
447 #$segment = $self->database->segment( -name => $segment->ref,
448 #-start => ($new_start || $segment->start),
449 #-end => ($new_end || $segment->end) );
450 #$segment_size = $segment->length;
451 #}
452  
453 #NML: kludgy fix to get details panel to draw at an appropriate length
454 #Otherwise, it spills past the browser window
455 $self->renderer->set_details_multiplier(1);
456  
457 my $maxrange = MAXRANGE;
458 print <<JS;
459 <script type="text/javascript">
460  
461 var oPrimerDesigner = { };
462  
463 oPrimerDesigner.removePanButtons = function () {
464  
465 /*Get rid of panleft and panright buttons */
466 /*until I can figure out a way to get them working*/
467 var imgs = document.getElementsByTagName('img');
468  
469 for (i=0; i<imgs.length; i++) {
470 if (imgs[i].className == 'panleft'
471 || imgs[i].className == 'panright'
472 ) {
473 imgs[i].style.display='none';
474 }
475 }
476 }
477  
478 oPrimerDesigner.scroll = function (direction,length_units) {
479  
480 oPrimerDesigner.unselectRegion();
481  
482 var plugin = document.getElementsByName('plugin')[0].value;
483  
484 var detail_start = parseInt(Controller.segment_info.detail_start)
485 var detail_stop = parseInt(Controller.segment_info.detail_stop)
486  
487 var overview_start = Controller.segment_info.overview_start;
488 var overview_stop = Controller.segment_info.overview_stop;
489  
490 var view_length =
491 (
492 parseInt(Controller.segment_info.detail_stop)
493 - parseInt(Controller.segment_info.detail_start)
494 )
495 / parseFloat(Controller.segment_info.details_mult);
496  
497 var bits = Math.round(length_units*view_length);
498  
499 var action = direction + ' ' + bits;
500 var mainform = document.getElementsByName('mainform')[0];
501  
502 var newStart, newStop;
503  
504 if (direction == 'left') {
505 newStart = Math.round(detail_start - bits);
506 newStop = Math.round(detail_stop - bits);
507 if (newStart < overview_start) {
508 newStart = overview_start;
509 newStop = view_length;
510 newStop = Math.round(overview_start + view_length);
511 }
512 }
513 else if (direction == 'right') {
514 newStart = Math.round(detail_start + bits);
515 newStop = Math.round(detail_stop + bits);
516 if (newStop > overview_stop) {
517 newStart = Math.round(overview_stop - view_length);
518 newStop = overview_stop;
519 }
520  
521 }
522 else {
523 if (direction == 'zoom') {
524  
525 var detail_length = detail_stop - detail_start;
526 var value = detail_length * length_units;
527  
528 var addMargin = Math.round((value - detail_length)/2);
529  
530 newStart = Math.round(detail_start - addMargin);
531 newStop = Math.round(detail_stop + addMargin);
532  
533 }
534 else if (direction == 'set span') {
535 var value = length_units*1;
536 var addMargin = Math.floor((value - (detail_stop - detail_start))/2);
537  
538 newStart = Math.round(detail_start - addMargin);
539 newStop = Math.round(detail_stop + addMargin);
540  
541 }
542  
543 if ((newStop - newStart) > (overview_stop - overview_start)) {
544 newStart = overview_start;
545 newStop = overview_stop;
546 }
547  
548 if (newStart < overview_start ) {
549 newStart = overview_start;
550 newStop = newStart + value - 1;
551 }
552 else if (newStop > overview_stop ) {
553 newStart = overview_stop - value + 1;
554 newStop = overview_stop;
555 }
556 }
557  
558 oPrimerDesigner.createNewFormElement(mainform, "view_start", newStart);
559 oPrimerDesigner.createNewFormElement(mainform, "view_stop", newStop);
560  
561 mainform.action = mainform.action
562 + '?plugin='+plugin+';action=Go;view_start='+newStart+';view_stop='+newStop;
563  
564 mainform.submit();
565  
566  
567 }
568  
569 oPrimerDesigner.designPrimers = function () {
570 //var mainform = document.getElementsByName('mainform')[0];
571 //var conf = oPrimerDesigner.createNewFormElement(mainform, "configured", "Design Primers");
572  
573 //mainform.setAttribute("target", "_blank");
574 //mainform.submit();
575  
576 /*Remove these after submit or else will generate a bug where user*/
577 /*tries to scroll or zoom without closing rubber band menu first, */
578 /*causing form to unintentionally be resubmitted */
579 //mainform.removeChild(conf);
580 //mainform.removeAttribute("target");
581 }
582  
583 oPrimerDesigner.selectRegion = function () {
584  
585 SelectArea.prototype.cancelRubber();
586  
587 var size_range = document.getElementById('product_size_range');
588  
589 var plugin = document.getElementsByName('plugin')[0].value;
590  
591 var select_start = currentSelectArea.selectSequenceStart;
592 var select_end = currentSelectArea.selectSequenceEnd;
593  
594 var min_size = select_end - select_start + 40;
595 var max_size = min_size + $maxrange;
596  
597 var x = [];
598 var sizes = [min_size, max_size];
599 for (i=0;i < sizes.length;i++) {
600 var n = sizes[i];
601 x.push(Math.ceil(sizes[i]/50.0) * 50);
602  
603 }
604  
605 var lb = document.getElementsByName(plugin+'.lb')[0]
606 var rb = document.getElementsByName(plugin+'.rb')[0];
607 var target = document.getElementsByName(plugin+'.target')[0];
608  
609  
610 oPrimerDesigner.lb = lb.value;
611 oPrimerDesigner.rb = rb.value;
612 oPrimerDesigner.target = target.value;
613 oPrimerDesigner.size_range = size_range.value;
614  
615 size_range.value = x[0]+'-'+x[1];
616  
617 lb.value = select_start;
618 rb.value = select_end;
619  
620 if (target.value == undefined || target.value < select_start || target.value > select_end) {
621 target.value = parseInt( (select_start+select_end)/2 );
622 }
623  
624 var mainform = document.getElementsByName('mainform')[0];
625 mainform.submit();
626 }
627  
628 oPrimerDesigner.unselectRegion = function () {
629  
630 var plugin = document.getElementsByName('plugin')[0].value;
631 var size_range = document.getElementById('product_size_range');
632  
633 if (oPrimerDesigner.size_range != undefined) {
634 size_range.value = oPrimerDesigner.size_range;
635 }
636  
637 if (oPrimerDesigner.lb != undefined) {
638 document.getElementsByName(plugin+'.lb')[0].value = oPrimerDesigner.lb;
639 }
640 if (oPrimerDesigner.rb != undefined) {
641 document.getElementsByName(plugin+'.rb')[0].value = oPrimerDesigner.rb;
642 }
643 if (oPrimerDesigner.target != undefined) {
644 document.getElementsByName(plugin+'.target')[0].value = oPrimerDesigner.target;
645 }
646  
647 SelectArea.prototype.cancelRubber();
648 }
649  
650 if (window.addEventListener) { // W3C standard
651 window.addEventListener('load', oPrimerDesigner.removePanButtons, false); // NB **not** 'onload'
652 }
653 else if (window.attachEvent) { // Microsoft
654 window.attachEvent('onload', oPrimerDesigner.removePanButtons);
655 }
656  
657 </script>
658 JS
659 #if (param('configured') ) {
660 #print 'param configured<br>';
661 #}
662 #if ($self->get_primer3_params()) {
663 #print 'got primer3 params<br>';
664 #} else {
665 #print Dumper param();
666 #}
667 #if (param('configured') && $self->get_primer3_params()) {
668 #print Dumper $conf;
669 #}
670  
671  
672 # design the primers if required
673 $self->design_primers( $segment, $lb, $rb)
674 if param('configured') && $self->get_primer3_params();
675  
676  
677 # or print the config form
678 print $self->configure_form($segment,$target,$lb,$rb);
679  
680 my $render = $self->renderer;
681  
682 my $segment_info = $render->segment_info_object;
683  
684 my $div = $render->render_search_form_objects();
685 $div = join('', split("\n", $div));
686 $div =~ s/'/\\'/g;
687  
688 my $segment_info_string =
689 join(',', map {
690 my $val = $segment_info->{$_};
691 $val = isdigit($val) ? $val : "\'$val\'";
692 "'$_': $val"
693 } (keys %$segment_info) );
694  
695 #my $detail_select_menu = detail_select_menu();
696  
697 print <<JS;
698  
699  
700 <script type="text/javascript">
701  
702  
703 Controller.segment_info = { $segment_info_string };
704  
705 /*helper function to create a form*/
706 oPrimerDesigner.getNewSubmitForm = function (){
707 var submitForm = document.createElement("form");
708 document.body.appendChild(submitForm);
709 submitForm.method = "post";
710 return submitForm;
711 }
712  
713 /*helper function to add elements to the form*/
714 oPrimerDesigner.createNewFormElement = function (inputForm, elementName, elementValue){
715 var newElement = document.createElement('input');
716 newElement.name = elementName;
717 newElement.type = 'hidden';
718 newElement.value= elementValue
719 inputForm.appendChild(newElement);
720 return newElement;
721 }
722  
723 /*Create submit form to get rubberband working*/
724 //oPrimerDesigner.submitForm = oPrimerDesigner.getNewSubmitForm();
725 //oPrimerDesigner.createNewFormElement(oPrimerDesigner.submitForm, "force_submit", "0");
726 //oPrimerDesigner.createNewFormElement(oPrimerDesigner.submitForm, "plugin_action", "Go");
727  
728 //oPrimerDesigner.createNewFormElement(oPrimerDesigner.submitForm, "plugin", document.getElementsByName('plugin')[0].value);
729 //oPrimerDesigner.submitForm.style.display = 'none';
730  
731 //var div = document.createElement('div');
732 //div.innerHTML = '$div';
733 //div.setAttribute('id', 'search_form_objects');
734 //oPrimerDesigner.submitForm.appendChild(div);
735  
736 //document.searchform = oPrimerDesigner.submitForm;
737  
738 oPrimerDesigner.details_panel = document.getElementById('primer_panel');
739  
740 if (window.addEventListener) { // W3C standard
741 oPrimerDesigner.details_panel.addEventListener('mouseup', oPrimerDesigner.selectRegion, false);
742 }
743 else if (window.attachEvent) { // Microsoft
744 oPrimerDesigner.details_panel.attachEvent('onmouseup', oPrimerDesigner.selectRegion);
745 }
746  
747 </script>
748 JS
749  
750  
751  
752 }
753  
754 sub detail_select_menu {
755  
756 <<DETAILS;
757 <div id="detailSelectMenu">
758 <table style="width:100%">
759 <tr>
760 <th style="background:lightgrey;cell-padding:5">
761 SELECTION
762 <a style="right:0px;position:absolute;color:blue;cursor:pointer"
763 onclick="oPrimerDesigner.unselectRegion()">
764 [X]
765 </a>
766 </th>
767 </tr>
768 <tr>
769 <td>
770 <a style="color:blue;cursor:pointer" onclick="oPrimerDesigner.designPrimers()" target="_BLANK">
771 Design primers for this region
772 </a>
773 </td>
774 </tr>
775 <tr>
776 <td>
777 <a style="color:blue;cursor:pointer" onclick="SelectArea.prototype.clearAndSubmit()">
778 Zoom in
779 </a>
780 </td>
781 </tr>
782 <tr>
783 <td>
784 <a style="color:blue;cursor:pointer" onclick="SelectArea.prototype.clearAndRecenter()">
785 Recenter on this region
786 </a>
787 </td>
788 </tr>
789 </table>
790 </div>
791 DETAILS
792  
793 }
794  
795 sub design_primers {
796 my ( $self, $segment, $lb, $rb ) = @_;
797 my $conf = $self->configuration;
798 my %atts = $self->get_primer3_params($lb,$rb);
799 my $target = $self->focus($segment);
800 my $tlength = $rb - $lb || 1;
801 my $offset = $segment->start - 1;
802 my $tstart = $lb - $offset;
803 my $exclude = join ',', $tstart, $tlength if $tlength > 1;
804  
805 $tstart += int(($rb - $lb)/2);
806 my $ptarget = join ',', $tstart,1;
807
808 # make the segment a manageable size
809 if (!$ptarget && $segment->length > DEFAULT_SEG_SIZE) {
810 $segment = $self->refocus($segment, $target, DEFAULT_SEG_SIZE);
811 }
812  
813 my $dna = $segment->seq;
814 if ( ref $dna && $dna->can('seq') ) {
815 $dna = $dna->seq;
816 }
817 elsif ( ref $dna ) {
818 fatal_error(
819 "Unsure what to do with object $dna. I was expecting a sequence string"
820 )
821 }
822 elsif ( !$dna ) {
823 fatal_error("There is no DNA sequence in the database");
824 }
825  
826 # unless a product size range range is specified, just keep looking
827 # until we find some primers that flank the target
828 my $size_range = $conf->{size_range} || join ' ', qw/
829 100-300 301-400 401-500 501-600 601-700 701-800 801-900
830 901-1000 1001-1200 1201-1400 1401-1600 1601-1800 1801-2000
831 2001-2400 2401-2600 2601-2800 2801-3200 3201-3600 3601-4000/;
832  
833 $atts{seq} = $dna;
834 $atts{id} = $segment->ref;
835 $atts{target} = $ptarget;
836 $atts{excluded} = $exclude if $exclude;
837 $atts{PRIMER_PRODUCT_SIZE_RANGE} = $size_range;
838  
839 # get a PCR object
840 my $pcr = Bio::PrimerDesigner->new( program => BINARY,
841 method => METHOD );
842 $pcr or fatal_error(pre(Bio::PrimerDesigner->error));
843  
844 my $binpath = BINPATH;
845 my $method = $binpath =~ /http/i ? 'remote' : METHOD;
846  
847 if ( $method eq 'local' && $binpath ) {
848 $pcr->binary_path($binpath) or fatal_error(pre($pcr->error));
849 }
850 else {
851 $pcr->url($binpath) or fatal_error(pre($pcr->error));
852 }
853  
854 my $res = $pcr->design(%atts) or fatal_error(pre($pcr->error));
855  
856 $self->primer_results( $res, $segment, $lb, $rb );
857 }
858  
859 sub primer_results {
860 my ( $self, $res, $segment, $lb, $rb ) = @_;
861 my $conf = $self->configuration;
862 my $target = $self->focus($segment);
863 my $offset = $segment->start;
864 my $ref = $segment->ref;
865 my $num = grep {/^\d+$/} keys %$res;
866
867 my $raw_output = pre($res->raw_output);
868 $raw_output =~ s/^(SEQUENCE=\w{25}).+$/$1... \(truncated for display only\)/m;
869  
870 # Give up if primer3 failed
871 fatal_error("No primers found:".pre($raw_output)) unless $res->left;
872  
873 my @attributes = qw/ left right startleft startright tmleft tmright
874 qual lqual rqual leftgc rightgc lselfany lselfend rselfany rselfend/;
875
876 my ( @rows, @feats );
877
878 my $text = "This value should be less than 1 for best results but don\'t worry too much";
879 #my $Primer_Pair_Quality = 'Primer_Pair_Quality '.a( { -href => 'javascript:void(0)', -title => $text}, '[?]');
880 my $Primer_Pair_Quality = 'Primer_Pair_Quality '.a( { -href => "javascript:alert(\"$text\")" }, '[?]');
881 my $spacer = td( {-width => 25}, '&nbsp;');
882
883 for my $n ( 1 .. $num ) {
884 my %r;
885 for (@attributes) {
886 $r{$_} = $res->$_($n);
887 }
888 next unless $r{left};
889  
890 $r{prod} = $r{startright} - $r{startleft};
891 $r{startleft} += $offset;
892 $r{startright} += $offset;
893  
894 for (qw/ qual lqual rqual /) {
895 $r{$_} =~ s/^(\S{6}).+/$1/;
896  
897 # low primer pair quality warning
898 if ( $r{$_} > 1 ) {
899 my $msg = quality_warning();
900 $msg = "alert('$msg')";
901 $r{$_} = a(
902 { -href => 'javascript:void(0)',
903 -title => 'Low quality warning',
904 -onclick => $msg
905 },
906 b( font( { -color => 'red' }, $r{$_} ) )
907 );
908  
909 }
910 }
911  
912 push @feats,
913 Bio::Graphics::Feature->new(
914 -start => $r{startleft}-20,
915 -stop => $r{startright}+20,
916 -type => 'Primer',
917 -name => "PCR primer set $n" );
918  
919 push @rows,
920 Tr(
921 [
922 $spacer .
923 th(
924 { -class => 'searchtitle', -align => 'left' },
925 [ qw/Set Primer/, "Sequence (5'->3')", qw/Tm %GC Coord Quality Product/, $Primer_Pair_Quality ]
926 ),
927 $spacer .
928 td(
929 [ $n, 'left', $r{left}, $r{tmleft},
930 $r{leftgc}, $r{startleft}, $r{lqual}, '&nbsp;',
931 '&nbsp;'
932 ]
933 ),
934 $spacer .
935 td(
936 [ '&nbsp;', 'right', $r{right}, $r{tmright},
937 $r{rightgc}, $r{startright}, $r{rqual}, $r{prod},
938 $r{qual}
939 ]
940 ),
941 $spacer .
942 td(
943 { -colspan => 9 },
944 $self->toggle( {on => 0, override => 1},
945 "PRIMER3-style report for set $n",
946 primer3_report( $self, $segment, $res, \%r )).br
947 )
948 ]
949 );
950 }
951  
952 my $featurefile = Bio::Graphics::FeatureFile->new();
953 my $options = {
954 bgcolor => 'red',
955 glyph => 'primers',
956 height => 10,
957 label => 1
958 };
959  
960 $featurefile->set('primers',glyph=>'primers');
961 $featurefile->set('primers',label=>'1');
962 $featurefile->set('primers',height=>'10');
963 $featurefile->set('primers',bgcolor=>'red');
964  
965  
966 #$featurefile->add_type( 'Primers' => $options );
967  
968 for my $f (@feats) {
969 $featurefile->add_feature( $f => 'Primers' );
970 }
971  
972 my $width = IMAGEWIDTH;
973 my $back = Tr( $spacer . td( { -colspan => 9,}, $self->back_button ));
974 unshift @rows, $back if @rows > 3;
975  
976 my $tlength = $rb - $lb;
977 my ($config_html, $map) = $self->configure_form($segment,$target,$lb,$rb,$featurefile);
978  
979 unshift @rows, Tr( [ $spacer . td(h1({-align => 'center'},"Predicted PCR primers ") ),
980 $spacer . td($config_html) ] );
981  
982 print table(
983 { -style => "width:900px" },
984 [ @rows,
985 Tr( $spacer . td( { -colspan => 9, -class => 'searchtitle' },
986 $self->toggle( {on => 0, override => 1}, 'PRIMER3 raw output', $raw_output))
987 ),
988 $back
989 ]
990 ), $map;
991 exit(0);
992 }
993  
994 # GENERATE A PRIMER_3-STYLE REPORT
995 # contributed by Russell Smithies
996 # russell.smithies@agresearch.co.nz
997 sub primer3_report {
998 my $self = shift;
999 my $sub_segment = shift;
1000 my $sub_res = shift;
1001 my %sub_r = %{ shift @_ };
1002 my @target = split( /\,/, $sub_res->TARGET );
1003 my $start = $sub_segment->start;
1004 my $end = $sub_segment->end;
1005 my $ref = $sub_segment->ref;
1006  
1007 # tweak the names to be coords for the target rather than the displayed region
1008 my $start_name = $start + $target[0];
1009 my $end_name = $end + $target[0] + $target[1];
1010 my $name = "$ref:$start_name..$end_name";
1011  
1012 my $offset;
1013 if ( ( $sub_r{startright} - $start ) < length( $sub_res->SEQUENCE ) ) {
1014 $offset = 100;
1015 }
1016 else {
1017 $offset = 0;
1018 }
1019  
1020 # trim this much off the front of the displayed sequence to keep it a reasonable size
1021 my $trunc = $sub_r{startleft} - $start - $offset;
1022  
1023 my $rs;
1024 $rs = "<pre>";
1025 $rs .= "\n\n";
1026 $rs .= "No mispriming library specified\n";
1027 $rs .= "Using 1-based sequence positions\n\n";
1028  
1029 # set width of name field
1030 my $max_name_length = length( $name . '|RIGHT ' );
1031 $rs .= sprintf(
1032 sprintf( "%s ", '%-' . $max_name_length . 's' )
1033 . " %5s %5s %4s %5s %5s %4s %-30s\n",
1034 'OLIGO', 'start', 'len', 'tm', 'gc%', 'any', '3\'', 'seq', );
1035 $rs .= sprintf(
1036 sprintf( "%s ", '%-' . $max_name_length . 's' )
1037 . " %5d %5d %4s %5s %5s %4s %-30s\n",
1038 $name . '|LEFT', $sub_r{startleft} - $start - $trunc,
1039 length( $sub_r{left} ), $sub_r{tmleft},
1040 $sub_r{leftgc}, $sub_r{lselfany},
1041 $sub_r{lselfend}, $sub_r{left}
1042 );
1043 $rs .= sprintf(
1044 sprintf( "%s ", '%-' . $max_name_length . 's' )
1045 . " %5d %5d %4s %5s %5s %4s %-30s\n",
1046 $name . '|RIGHT', $sub_r{startright} - $start - $trunc,
1047 length( $sub_r{right} ), $sub_r{tmright},
1048 $sub_r{rightgc}, $sub_r{rselfany},
1049 $sub_r{rselfend}, $sub_r{right}
1050 );
1051 $rs .= "\n";
1052 $rs .= sprintf( "PRODUCT SIZE : %-4d\n", $sub_r{prod} );
1053 $rs .= sprintf( "TARGET REGION : %s\n", "$ref:$start_name..$end_name" );
1054 $rs .= sprintf(
1055 "TARGETS (start\, len)\*: %d\,%d\n",
1056 $target[0] - $trunc,
1057 $target[1]
1058 );
1059 $rs .= "\n";
1060  
1061 # mark the primers and target on the alignments track
1062 my $sub_alignments .= " " x ( $sub_r{startleft} - $start - $trunc );
1063  
1064 # left primer
1065 $sub_alignments .= ">" x length( $sub_r{left} );
1066 $sub_alignments .= " " x ( $target[0] - length($sub_alignments) - $trunc );
1067  
1068 # target area
1069 $sub_alignments .= "*" x $target[1];
1070 $sub_alignments
1071 .= " " x ( $sub_r{startright} - $start - length($sub_alignments) -
1072 length( $sub_r{right} ) - $trunc + 1 );
1073  
1074 # right primer
1075 $sub_alignments .= "<" x length( $sub_r{right} );
1076  
1077 my $dna = $sub_res->SEQUENCE;
1078  
1079 # trim displayed sequence
1080 $dna = substr( $dna, $trunc );
1081 $dna = substr( $dna, 0, ( $sub_r{prod} + $offset + $offset ) );
1082  
1083 # hack to place alignment track below sequence
1084 $dna =~ s/(.{1,60})/$1;/g;
1085 my @dna_bits = split( /;/, $dna );
1086 $sub_alignments =~ s/(.{1,60})/$1;/g;
1087 my @alignment_bits = split( /;/, $sub_alignments );
1088  
1089 my $i = 0;
1090  
1091 # print sequence and alignments
1092 while ( $i <= $#dna_bits ) {
1093 $alignment_bits[$i] ||= '';
1094 $rs .= sprintf( "%3d %s\n", ( $i * 60 + 1 ), $dna_bits[$i] );
1095 $rs .= " " . $alignment_bits[$i] . "\n";
1096 $rs .= "\n";
1097 $i++;
1098 }
1099 $rs .= "</pre>";
1100 return $rs;
1101 }
1102  
1103 sub unit_label {
1104 my $value = shift;
1105 $value >= 1e9 ? sprintf( "%.4g Gbp", $value / 1e9 )
1106 : $value >= 1e6 ? sprintf( "%.4g Mbp", $value / 1e6 )
1107 : $value >= 1e3 ? sprintf( "%.4g kbp", $value / 1e3 )
1108 : sprintf( "%.4g bp", $value );
1109 }
1110  
1111 sub new_segment_map {
1112 my ( $self, $segment, $feats, $lb, $rb ) = @_;
1113  
1114 my $render = $self->renderer;
1115  
1116 my $seg = $render->region->seg;
1117  
1118 #my @tracks = grep !/overview/, $self->selected_tracks;
1119  
1120 my $config = $self->browser_config;
1121 my $zoom_levels = $config->setting('zoom levels') || '1000 10000 100000 200000';
1122 my @zoom_levels = split /\s+/, $zoom_levels;
1123 my %zoom_labels;
1124 for my $zoom (@zoom_levels) {
1125 $zoom_labels{$zoom} = $render->unit_label($zoom);
1126 }
1127  
1128 my $zoom_menu = $self->zoom_menu($segment);
1129
1130 # if the primer design is done, zoom in to the PCR products
1131 my $target;
1132  
1133 my @extra_args;
1134  
1135 if ($feats) {
1136  
1137 $zoom_menu = '';
1138  
1139
1140 my @feature_types = $feats->types;
1141  
1142 for my $type (@feature_types) {
1143 my $features = $feats->features($type);
1144 my %options = $feats->style($type);
1145 }
1146
1147  
1148 push @extra_args, ('tracks', $feats);
1149  
1150 $target = $self->focus($segment);
1151 my ($longest)
1152 = map {$_->length} sort { $b->length <=> $a->length } $feats->features('Primers');
1153 $segment = $self->refocus( $segment, $target, $longest+2000 );
1154  
1155 }
1156 else {
1157  
1158 $target = $self->focus($segment);
1159 }
1160  
1161 my $postgrid_callback = sub {
1162 my $gd = shift;
1163 my $panel = shift;
1164 my $left = $panel->pad_left;
1165 my $top = $panel->top;
1166 my $bottom = $panel->bottom;
1167  
1168 my ($mstart, $mend) = $panel->location2pixel($target, $target+1);
1169 my ($hstart, $hend) = $panel->location2pixel($lb,$rb);
1170  
1171 # first shaded
1172 #if ($feats) {
1173 unless ( $hend-$hstart < 2 ) {
1174 $gd->filledRectangle( $left + $hstart,
1175 $top, $left + $hend,
1176 $bottom, $panel->translate_color('lightgrey'));
1177 }
1178 #}
1179  
1180 # then the red center line
1181 $gd->filledRectangle( $left + $mstart,
1182 $top, $left + $mend,
1183 $bottom, $panel->translate_color('red'));
1184 };
1185  
1186 push @extra_args, ('postgrid', $postgrid_callback);
1187  
1188  
1189 #my $panel = $render->render_panel($segment,\%args);
1190 my $panel = $render->render_panel($segment,\@extra_args);
1191
1192 return ($panel, ' ', $zoom_menu);
1193  
1194 }
1195  
1196  
1197 # center the segment on the target coordinate
1198 sub refocus {
1199 my ( $self, $segment, $target, $window ) = @_;
1200 my $db = $self->database;
1201 my ($whole_seq) = $db->segment( $segment->ref );
1202 my $abs_end = $whole_seq->end;
1203  
1204 $window ||= $self->configuration->{span} || $segment->length;
1205  
1206 my $half = int( $window / 2 + 0.5 );
1207 $target = int( $target + 0.5 );
1208  
1209 # We must not fall of the ends of the ref. sequence
1210 my $nstart = $target < $half ? 1 : $target - $half;
1211 my $nend = $target + $half - 1;
1212 $nend = $abs_end if $nend > $abs_end;
1213  
1214 ($segment) = $db->segment(
1215 -name => $segment->ref,
1216 -start => $nstart,
1217 -end => $nend );
1218 return $segment;
1219 }
1220  
1221 sub _target {
1222 my $segment = shift;
1223 my $span = abs( $segment->end - $segment->start );
1224 return int( $span / 2 + 0.5 ) + $segment->start;
1225 }
1226  
1227 # find the target
1228 sub focus {
1229 my ( $self, $segment ) = @_;
1230 my $conf = $self->configuration;
1231 my $target;
1232  
1233 if ( $target = $conf->{target} ) {
1234 return $target;
1235 }
1236  
1237 return $conf->{target} = _target($segment);
1238 }
1239  
1240 # slurp the BOULDER_IO params
1241 sub get_primer3_params {
1242 my $self = shift;
1243  
1244 return %{ $self->{atts} } if $self->{atts};
1245  
1246 for ( grep {/PRIMER_/} param() ) {
1247 $self->{atts}->{$_} = param($_) if param($_);
1248 param( $_, '' );
1249 }
1250  
1251 return %{ $self->{atts} } if $self->{atts};
1252 }
1253  
1254 # form elements stolen and modified from the primer3 website
1255 sub primer3_params {
1256 my $self = shift;
1257 my $conf = $self->configuration;
1258 my $target = shift;
1259  
1260 my $help = 'http://frodo.wi.mit.edu/cgi-bin/primer3/primer3_www_help.cgi';
1261 my $msg = "Format xxx-xxx\\nBy default, the smallest "
1262 . "product size to flank the feature will be selected\\n"
1263 . "Use this option to force a particular amplicon size and.or "
1264 . "reduce computation time";
1265  
1266 my $sr = $conf->{size_range} || '';
1267  
1268 my %table = (
1269 b(qq(<a name="PRIMER_NUM_RETURN_INPUT" target="_new" href="$help\#PRIMER_NUM_RETURN">
1270 Primer sets:</a>)
1271 ),
1272 qq(<input type="text" size="4" name="PRIMER_NUM_RETURN" value="3">),
1273 b(qq(<a name="PRIMER_OPT_SIZE_INPUT" target="_new" href="$help\#PRIMER_SIZE">
1274 Primer Size</a>)
1275 ),
1276 qq(Min. <input type="text" size="4" name="PRIMER_MIN_SIZE" value="18">
1277 Opt. <input type="text" size="4" name="PRIMER_OPT_SIZE" value="20">
1278 Max. <input type="text" size="4" name="PRIMER_MAX_SIZE" value="27">),
1279 b(qq(<a name="PRIMER_OPT_TM_INPUT" target="_new" href="$help\#PRIMER_TM">
1280 Primer Tm</a>)
1281 ),
1282 qq(Min. <input type="text" size="4" name="PRIMER_MIN_TM" value="57.0">
1283 Opt. <input type="text" size="4" name="PRIMER_OPT_TM" value="60.0">
1284 Max. <input type="text" size="4" name="PRIMER_MAX_TM" value="63.0">),
1285 b(qq(<a name="PRIMER_PRODUCT_SIZE_RANGE" href="javascript:void(0)"
1286 onclick="alert('$msg')">Product size range:</a>)
1287 ),
1288 qq(<input type="text" size="8" id="product_size_range" name="PRIMER_PRODUCT_SIZE_RANGE" value=$sr>),
1289 b(qq(<a name="PRIMER_MAX_END_STABILITY_INPUT" target="_new" href="$help\#PRIMER_MAX_END_STABILITY">
1290 Max 3\' Stability:</a>)
1291 ),
1292 qq(<input type="text" size="4" name="PRIMER_MAX_END_STABILITY" value="9.0">),
1293 b(qq(<a name="PRIMER_PAIR_MAX_MISPRIMING_INPUT" target="_new" href="$help\#PRIMER_PAIR_MAX_MISPRIMING">
1294 Pair Max Mispriming:</a>)
1295 ),
1296 qq(<input type="text" size="4" name="PRIMER_PAIR_MAX_MISPRIMING" value="24.00">),
1297 b(qq(<a name="PRIMER_GC_PERCENT_INPUT" target="_new" href="$help\#PRIMER_GC_PERCENT">
1298 Primer GC%</a>)
1299 ),
1300 qq(Min. <input type="text" size="4" name="PRIMER_MIN_GC" value="20.0">
1301 Opt. <input type="text" size="4" name="PRIMER_OPT_GC_PERCENT" value="">
1302 Max. <input type="text" size="4" name="PRIMER_MAX_GC" value="80.0">),
1303 b(qq(<a name="PRIMER_SELF_ANY_INPUT" target="_new" href="$help\#PRIMER_SELF_ANY">
1304 Max Self Complementarity:</a>)
1305 ),
1306 qq(<input type="text" size="4" name="PRIMER_SELF_ANY" value="8.00">),
1307 b(qq(<a name="PRIMER_SELF_END_INPUT" target="_new" href="$help\#PRIMER_SELF_END">
1308 Max 3\' Self Complementarity:</a>)
1309 ),
1310 qq(<input type="text" size="4" name="PRIMER_SELF_END" value="3.00">),
1311 b(qq(<a name="PRIMER_MAX_POLY_X_INPUT" target="_new" href="$help\#PRIMER_MAX_POLY_X">
1312 Max Poly-X:</a>)
1313 ),
1314 qq(<input type="text" size="4" name="PRIMER_MAX_POLY_X" value="5">)
1315 );
1316 return \%table;
1317 }
1318  
1319 sub toggle {
1320 my $self = shift;
1321 my ($state,$section_head,@body) = @_;
1322 #my ($label) = $self->browser_config->tr($section_head) || $section_head;
1323 my ($label) = $self->renderer->tr($section_head) || $section_head;
1324 #return $self->renderer->toggle_section($state,$label,b($label),@body);
1325 return $self->toggle_section($state,$label,b($label),@body);
1326 }
1327  
1328  
1329 #Gbrowse's toggle function isn't displaying the $show_ctrl html for
1330 #PrimerDesigner when hidden. When a section is minimized, everything
1331 #including the header disappears making it impossible to re-toggle
1332 #This fixes the problem.
1333 {
1334 no warnings 'redefine';
1335 sub toggle_section {
1336  
1337 my $self = shift;
1338 my %config = ref $_[0] eq 'HASH' ? %{shift()} : ();
1339 my ($name,$section_title,@section_body) = @_;
1340  
1341 my $visible = $config{on};
1342  
1343 my $buttons = $self->renderer->data_source->button_url;
1344 my $plus = "$buttons/plus.png";
1345 my $minus = "$buttons/minus.png";
1346 my $break = div({-id=>"${name}_break",
1347 -style=>$visible ? 'display:none' : 'display:block'
1348 },'&nbsp;');
1349  
1350 my $show_ctl = div({-id=>"${name}_show",
1351 -class=>'ctl_hidden',
1352 -style=>$visible ? 'display:none' : 'display:inline',
1353 -onClick=>"visibility('$name',1)"
1354 },
1355 img({-src=>$plus,-alt=>'+'}).'&nbsp;'.span({-class=>'tctl'},$section_title));
1356  
1357 my $js = <<"JS";
1358  
1359 var d = document.getElementById('$name').style.display;
1360 var i = this.childNodes[1];
1361 if (d == 'inline' || d == 'block') {
1362 document.getElementById('$name').style.display = 'none';
1363 i.src = '$plus';
1364 } else {
1365 document.getElementById('$name').style.display = 'inline';
1366 i.src = '$minus';
1367 }
1368  
1369 JS
1370  
1371 my $hide_ctl = div({-id=>"${name}_hide",
1372 -class=>'ctl_visible',
1373 -style=>$visible ? 'display:inline' : 'display:none',
1374 #-onClick=>"visibility('$name',0)"
1375 -onClick=> $js
1376 },
1377 img({-src=>$minus,-alt=>'-'}).'&nbsp;'.span({-class=>'tctl',-id=>"${name}_title"},$section_title));
1378 my $content = div({-id => $name,
1379 -style=>$visible ? 'display:inline' : 'display:none',
1380 -class => 'el_visible'},
1381 @section_body);
1382 my @result = $config{nodiv} ? (div({-style=>'float:left'},
1383 $show_ctl.$hide_ctl),$content)
1384 :$config{tight}? (div({-style=>'float:left;position:absolute;z-index:10'},
1385 $show_ctl.$hide_ctl).$break,$content)
1386 : div($show_ctl.$hide_ctl,$content);
1387 return wantarray ? @result : "@result";
1388 }
1389 }
1390  
1391 sub quality_warning {
1392 my $msg = <<END;
1393 Primer-pair penalty (quality score) warning.
1394 BREAK
1395 For best results, a primer-pair should have a quality
1396 score < 1.
1397 BREAK
1398 The score for the pair is the the sum of the scores
1399 for each individual primer.
1400 BREAK
1401 If the high score is due to a departure from optimal primer
1402 GC-content or Tm, the primers are probably OK.
1403 Otherwise, more optimal primers can often be obtained
1404 by adjusting the design parameters (especially
1405 the product size range).
1406 END
1407 $msg =~ s/\n/ /gm;
1408 $msg =~ s/BREAK/\\n/g;
1409  
1410 return $msg;
1411 }
1412  
1413 sub zoom_menu {
1414 my $self = shift;
1415 my $segment = shift;
1416 #my $render = $self->renderer($segment);
1417 my $render = $self->renderer;
1418 return $render->slidertable($segment);
1419 }
1420  
1421 sub renderer {
1422 my $self = shift;
1423 my $segment = shift;
1424 my $config = $self->browser_config || {};
1425 my $render = $self->{render};
1426 if ($render) {
1427 $render->current_segment($segment);
1428 return $render;
1429 }
1430
1431  
1432 my $globals = Bio::Graphics::Browser2->open_globals;
1433  
1434 $self->{render} = PrimerDesigner::Render->new($config);
1435  
1436  
1437  
1438 $self->{render} = bless $segment, 'PrimerDesigner::Render';
1439  
1440 $self->{render}->current_segment($segment);
1441 return $self->{render};
1442 }
1443  
1444 sub back_button {
1445 my $url = shift->my_url;
1446 button( -onclick => "window.location='$url'",
1447 -name => 'Return to Browser' );
1448 }
1449  
1450 sub fatal_error {
1451 #my $self = shift;
1452 print CGI::header('text/plain'),"@_\n";
1453 exit 0;
1454 }
1455  
1456  
1457 1;
1458  
1459  
1460 # A package to override some Bio::Graphics::Browser2::Render
1461 # image mapping methods
1462 #package Bio::Graphics::Browser2::faux;
1463 package PrimerDesigner::Render;
1464 use CGI qw/:standard unescape/;
1465 use warnings;
1466 use strict;
1467 use Carp qw/cluck/;
1468 use Bio::Root::Storable;
1469 use Data::Dumper;
1470  
1471 use vars '@ISA';
1472  
1473 # controls the resolution of the recentering map
1474 use constant RULER_INTERVALS => 100;
1475 use constant DEFAULT_SEG_SIZE => 10000;
1476 use constant DEFAULT_FINE_ZOOM => '20%';
1477 use constant BUTTONSDIR => '/gbrowse/images/buttons';
1478 use constant OVERVIEW_RATIO => 0.9;
1479 use constant DEBUG => 0;
1480 use constant DEFAULT_RANGES => q(100 500 1000 5000 10000 25000 100000 200000 400000);
1481  
1482 @ISA = qw/Bio::Graphics::Browser2::Render::HTML/;
1483  
1484 sub new {
1485 my $class = shift;
1486 my $browser = shift;
1487 my %browser_data = %{$browser}; # just the config data, not the object
1488 return bless \%browser_data, $class;
1489 }
1490  
1491  
1492 sub error {
1493 '';
1494 }
1495  
1496 sub make_feat_link {
1497 my $self = shift;
1498 my $feat = shift;
1499 my ($start, $end ) = @_;
1500 my $fref = $feat->ref;
1501 my $fstart = $feat->start;
1502 my $fend = $feat->stop;
1503 $start ||= $fstart;
1504 $end ||= $fend;
1505  
1506 # segment >= DEFAULT_SEG_SIZE
1507 my $padding = int((DEFAULT_SEG_SIZE - $feat->length)/2) + 1;
1508 my ($pad) = sort {$b<=>$a} 1000, $padding;
1509  
1510 $start -= $pad;
1511 $end += $pad;
1512  
1513 my $p = 'PrimerDesigner';
1514 my $url = "?plugin=$p;plugin_action=Go;ref=$fref;start=$start;stop=$end;";
1515 $url .= "$p.lb=$fstart;$p.rb=$fend";
1516
1517 return $url;
1518 }
1519  
1520 sub make_map {
1521 my $self = shift;
1522 my ( $boxes, $centering_map, $panel ) = @_;
1523 my $map = qq(\n<map name="hmap" id="hmap">\n);
1524  
1525 my $topruler = shift @$boxes;
1526 $map .= $self->make_centering_map($topruler);
1527  
1528 my $bottomruler = pop @$boxes;
1529 $map .= $self->make_boundary_map($bottomruler);
1530  
1531 my @link_sets;
1532 my $link_set_idx = 0;
1533  
1534 for my $box (@$boxes) {
1535 my ( $feat, $x1, $y1, $x2, $y2, $track ) = @$box;
1536 next unless $feat->can('primary_tag');
1537 next if $feat->primary_tag eq 'Primer';
1538 my $fclass = $feat->class || 'feature';
1539 my $fname = $feat->name || 'unnamed';
1540 my $fstart = $feat->start;
1541 my $fend = $feat->stop;
1542 my $pl = $panel->pad_left;
1543 my $half = int(($topruler->[5]->length/2) + 0.5);
1544  
1545 my $link = $self->make_feat_link( $feat );
1546 my $href = qq{href="$link"};
1547  
1548 # give each subfeature its own link
1549 my @parts = $feat->sub_SeqFeature if $feat->can('sub_SeqFeature');
1550 if ( @parts > 1 ) {
1551 my $last_end;
1552 for my $part (sort {$a->start <=> $b->start} @parts) {
1553 my $pstart = $part->start;
1554 my $pend = $part->end;
1555 my $ptype = lc $part->primary_tag;
1556  
1557 my $no_overlap = 0;
1558 # intervals between parts select the whole (aggregate) feature
1559 $last_end ||= $pend;
1560 if ($pstart > $last_end) {
1561 my $istart = $last_end + 1;
1562 my $iend = $pstart - 1;
1563 my ($ix1,$ix2) = map { $_ + $pl } $panel->location2pixel( $istart, $iend );
1564  
1565 # skip it if the box will be less than 2 pixels wide
1566 if ($ix2 - $ix1 > 1) {
1567 my $title = qq{title="select $fclass $fname"};
1568 $map .= qq(<area shape="rect" coords="$ix1,$y1,$ix2,$y2" $href $title/>\n);
1569 $no_overlap = $ix2;
1570 }
1571 }
1572  
1573 my ( $px1, $px2 ) = map { $_ + $pl } $panel->location2pixel( $pstart, $pend );
1574 $px1++ if $px1 == $no_overlap;
1575  
1576 my $phref = $self->make_feat_link( $part, $pstart, $pend );
1577 $phref = qq{href="$phref"};
1578 my $title = qq{title="select this $ptype"};
1579 $map .= qq(<area shape="rect" coords="$px1,$y1,$px2,$y2" $phref $title/>\n);
1580  
1581 $last_end = $pend;
1582 }
1583 }
1584 else {
1585 my $title = qq{title="select $fclass $fname"};
1586 $map .= qq(<area shape="rect" coords="$x1,$y1,$x2,$y2" $href $title/>\n);
1587 }
1588 }
1589  
1590 $map .= "</map>\n";
1591  
1592 return $map;
1593 }
1594  
1595 sub make_centering_map {
1596 my $self = shift;
1597 my $ruler = shift;
1598 my $bottom = shift; # true if this is the lower scale-bar
1599  
1600 my ( $rfeat, $x1, $y1, $x2, $y2, $track ) = @$ruler;
1601  
1602 my $rlength = $x2 - $x1 or return;
1603 my $length = $rfeat->length;
1604 my $start = $rfeat->start;
1605 my $stop = $rfeat->stop;
1606 my $panel = $track->panel;
1607 my $pl = $panel->pad_left;
1608 my $middle;
1609  
1610 if ($bottom) {
1611 $middle = param('PrimerDesigner.target');
1612 $middle ||= int(($start+$stop)/2 + 0.5);
1613 }
1614  
1615 # divide into RULER_INTERVAL intervals
1616 my $portion = $length / RULER_INTERVALS;
1617 my $rportion = $rlength / RULER_INTERVALS;
1618  
1619 my $ref = $rfeat->seq_id;
1620 my $source = $self->source;
1621 my $plugin = 'PrimerDesigner';
1622 my $offset = $start - int( $length / 2 );
1623  
1624 my @lines;
1625  
1626 while (1) {
1627 my $end = $offset + $length;
1628 my $center = $offset + $length/2;
1629 my $sstart = $center - $portion/2;
1630 my $send = $center + $portion/2;
1631
1632 $_ = int $_ for ($start,$end,$center,$sstart,$send);
1633  
1634 my ( $X1, $X2 )
1635 = map { $_ + $pl } $panel->location2pixel( $sstart, $send );
1636  
1637 # fall of the end...
1638 last if $center >= $stop + ($length / 2);
1639  
1640 my ($url,$title_text);
1641  
1642 my $p = 'PrimerDesigner';
1643 my $rb = param("$p.rb");
1644 $rb = $1 if $rb && $rb =~ /\=(\d+)/;
1645 my $lb = param("$p.lb");
1646 $lb = $1 if $lb && $lb =~ /\=(\d+)/;
1647 my $target = param("$p.target");
1648
1649 # left side of the lower ruler
1650 if ($middle && $sstart <= $middle) {
1651 $url = "?ref=$ref;start=$start;stop=$stop;plugin=$plugin;plugin_action=Go;$p.lb=$center;";
1652 $url .= "$p.rb=$rb;" if $rb;
1653 $url .= "$p.target=$target;" if $target;
1654 $url = qq(href="$url");
1655 $title_text = "set left target boundary to $center";
1656 }
1657 # right side of the lower ruler
1658 elsif ($middle) {
1659 $url = "?ref=$ref;start=$start;stop=$stop;plugin=$plugin;plugin_action=Go;$p.rb=$center";
1660 $url .= ";$p.lb=$lb" if $lb;
1661 $url .= "$p.target=$target;" if $target;
1662 $url = qq(href="$url");
1663 $title_text = "set right target boundary to $center";
1664 }
1665 # top ruler
1666 else {
1667 $url = "?ref=$ref;start=$offset;stop=$end;plugin=$plugin;plugin_action=Go;";
1668  
1669 # We can retain an off-center target if it is still reasonable
1670 if ($target && $target > $offset + 1000 && $target < $end - 1000 ) {
1671 $url .= "$p.target=$target;";
1672 }
1673 if ($lb && $lb > $offset + 500) {
1674 $url .= "$p.lb=$lb;";
1675 }
1676 if ($rb && $rb < $end - 500) {
1677 $url .= "$p.rb=$rb;";
1678 }
1679  
1680 $url = qq(href="$url");
1681 $title_text = "recenter at $center";
1682 }
1683 my $map_line
1684 = qq(<area shape="rect" coords="$X1,$y1,$X2,$y2" $url );
1685 $map_line .= qq(title="$title_text" alt="recenter" />\n);
1686 push @lines, $map_line;
1687  
1688 $offset += int $portion;
1689 }
1690  
1691 return join '', @lines;
1692 }
1693  
1694 sub make_boundary_map {
1695 my $self = shift;
1696 $self->make_centering_map(@_, 1);
1697 }
1698  
1699 sub current_segment {
1700 my $self = shift;
1701 my $segment = shift;
1702 return $self->{segment} = $segment if $segment;
1703 return $self->{segment};
1704 }
1705  
1706 sub unit_label {
1707 my ( $self, $value ) = @_;
1708 my $unit = $self->setting('units') || 'bp';
1709 my $divider = $self->setting('unit_divider') || 1;
1710 $value /= $divider;
1711 my $abs = abs($value);
1712 my $label;
1713 $label = $abs >= 1e9 ? sprintf( "%.4g G%s", $value / 1e9, $unit )
1714 : $abs >= 1e6 ? sprintf( "%.4g M%s", $value / 1e6, $unit )
1715 : $abs >= 1e3 ? sprintf( "%.4g k%s", $value / 1e3, $unit )
1716 : $abs >= 1 ? sprintf( "%.4g %s", $value, $unit )
1717 : $abs >= 1e-2 ? sprintf( "%.4g c%s", $value * 100, $unit )
1718 : $abs >= 1e-3 ? sprintf( "%.4g m%s", $value * 1e3, $unit )
1719 : $abs >= 1e-6 ? sprintf( "%.4g u%s", $value * 1e6, $unit )
1720 : $abs >= 1e-9 ? sprintf( "%.4g n%s", $value * 1e9, $unit )
1721 : sprintf( "%.4g p%s", $value * 1e12, $unit );
1722 if (wantarray) {
1723 return split ' ', $label;
1724 }
1725 else {
1726 return $label;
1727 }
1728 }
1729  
1730 sub slidertable {
1731 my $self = shift;
1732 my $state = $self->state;
1733 my $segment = shift;
1734  
1735 # try to avoid reopening the database -- recover segment
1736 # and whole segment lengths from our stored state if available
1737 my $span = $state->{view_stop} - $state->{view_start} + 1;
1738 my $max = $self->thin_whole_segment->length;
1739  
1740 my $buttonsDir = $self->data_source->button_url;
1741  
1742 my $half_title = $self->data_source->unit_label(int $span/2);
1743 my $full_title = $self->data_source->unit_label($span);
1744 my $half = int $span/2;
1745 my $full = $span;
1746 my $fine_zoom = $self->get_zoomincrement();
1747  
1748 my $fine_point = do { no warnings 'numeric'; $fine_zoom/100 };
1749  
1750 my $show = $self->translate('Show').' ';
1751  
1752 my @lines =
1753 (image_button(
1754 -src => "$buttonsDir/green_l2.gif",
1755 -name=>"left $full",
1756 -title => "left $full_title",
1757 #-onClick => "Controller.scroll('left', 1)"
1758 -onClick => "oPrimerDesigner.scroll('left', 1)"
1759 ),
1760 '&nbsp;',
1761 image_button(-src=>"$buttonsDir/green_l1.gif",-name=>"left $half",
1762 -title=>"left $half_title",
1763 #-onClick => "Controller.scroll('left', 0.5)"
1764 -onClick => "oPrimerDesigner.scroll('left', 0.5)"
1765 ),
1766 '&nbsp;',
1767 image_button(-src=>"$buttonsDir/mminus.png",
1768 -name=>"zoom out $fine_zoom",
1769 -style=>'background-color: transparent',
1770 -title=>"zoom out $fine_zoom",
1771 #-onClick => "Controller.update_coordinates(this.name)"
1772 -onClick => "oPrimerDesigner.scroll('zoom', $fine_point+1)"
1773 ),
1774 '&nbsp;',
1775 $self->zoomBar($span,$max,$show),
1776 #$self->zoomBar($segment),
1777 '&nbsp;',
1778 image_button(-src=>"$buttonsDir/mplus.png",
1779 -name=>"zoom in $fine_zoom",
1780 -style=>'background-color: transparent',
1781 -title=>"zoom in $fine_zoom",
1782 #-onClick => "Controller.update_coordinates(this.name)",
1783 -onClick => "oPrimerDesigner.scroll('zoom',1-$fine_point)"
1784 ),
1785 '&nbsp;',
1786 image_button(-src=>"$buttonsDir/green_r1.gif",-name=>"right $half",
1787 -title=>"right $half_title",
1788 #-onClick => "Controller.scroll('right', 0.5)"
1789 -onClick => "oPrimerDesigner.scroll('right', 0.5)"
1790 ),
1791 '&nbsp;',
1792 image_button(-src=>"$buttonsDir/green_r2.gif",-name=>"right $full",
1793 -title=>"right $full_title",
1794 #-onClick => "Controller.scroll('right', 1)"
1795 -onClick => "oPrimerDesigner.scroll('right', 1)"
1796 ),
1797 '&nbsp;',
1798 );
1799  
1800 my $str = join('', @lines);
1801 return span({-id=>'span'},$str);
1802  
1803 }
1804  
1805 sub get_zoomincrement {
1806 my $self = shift;
1807 my $zoom = $self->setting('fine zoom') || DEFAULT_FINE_ZOOM;
1808 $zoom;
1809 }
1810  
1811 sub zoomBar {
1812 my $self = shift;
1813 my ($length,$max,$item_label) = @_;
1814 $item_label ||= '';
1815  
1816 my %seen;
1817 my @r = sort {$a<=>$b} $self->data_source->get_ranges();
1818 $max *= $self->data_source->unit_divider;
1819  
1820 my @ranges = grep {!$seen{$self->data_source->unit_label($_)}++ && $_<=$max} sort {$b<=>$a} @r,$length;
1821 my %labels = map {$_=>$item_label.$self->data_source->unit_label($_)} @ranges;
1822 return popup_menu(-class => 'searchtitle',
1823 -name => 'span',
1824 -values => \@ranges,
1825 -labels => \%labels,
1826 -default => $length,
1827 -force => 1,
1828 -onChange => "oPrimerDesigner.scroll('set span',this.options[this.selectedIndex].value)",
1829 );
1830 }
1831  
1832 #NML: holdovers from Bio::Graphics::Browser
1833 #need to see if there are contemporary versions in new modules
1834 sub fatal_error {
1835 #my $self = shift;
1836 print CGI::header('text/plain'),"@_\n";
1837 exit 0;
1838 }
1839  
1840 sub render_panel {
1841  
1842 my $render = shift;
1843 my $segment = shift;
1844 #my $postgrid = shift;
1845 my $extra_args = shift;
1846 my @panels;
1847  
1848 my $region = $render->region;
1849 my $features = $region->features;
1850 my $title = $render->generate_title($features);
1851 my $output;
1852 my @post_load = $render->get_post_load_functions;
1853  
1854 $output .= $render->render_html_start($title,@post_load);
1855 $output .= $render->render_busy_signal;
1856  
1857 push @panels, $output;
1858  
1859 #my %extra_args = @$extra_args;
1860 #my $postgrid = $extra_args{'postgrid'};
1861  
1862 my $scale_bar_html = $render->scale_bar( $segment, 'detail', $extra_args );
1863  
1864 push @panels,
1865 div({-id=>'primer_panel', -style=>'padding:12px;position:relative'},
1866 $scale_bar_html,
1867 $render->render_detailview_panels($segment)
1868 );
1869  
1870 my $main_page = join('',@panels);
1871  
1872 my $tracks = $render->render_tracks_section;
1873  
1874 return $render->sender_tabbed_pages($main_page,$tracks);
1875  
1876 return $main_page;
1877  
1878 }
1879  
1880 sub sender_tabbed_pages {
1881 my $self = shift;
1882 my ($main_html,$tracks_html,$community_tracks_html,$custom_tracks_html,$settings_html,) = @_;
1883 my $uses_database = $self->user_tracks->database;
1884
1885 my $main_title = $self->translate('MAIN_PAGE');
1886 my $tracks_title = $self->translate('SELECT_TRACKS');
1887 my $community_tracks_title = $self->translate('COMMUNITY_TRACKS_PAGE') if $uses_database;
1888 my $custom_tracks_title = $self->translate('CUSTOM_TRACKS_PAGE');
1889 my $settings_title = $self->translate('SETTINGS_PAGE');
1890  
1891 my $html = '';
1892 $html .= div({-id=>'tabbed_section', -class=>'tabbed'},
1893 #div({-id=>'tabbed_menu',-class=>'tabmenu'},
1894 span({style=>'display:none', id=>'main_page_select'}, $main_title),
1895 span({style=>'display:none', id=>'track_page_select'}, $tracks_title),
1896 #$uses_database? span({id=>'community_tracks_page_select'}, $community_tracks_title) : "",
1897 #span({id=>'custom_tracks_page_select'}, $custom_tracks_title),
1898 #span({id=>'settings_page_select'}, $settings_title),
1899 #),
1900 div({-id=>'main_page', -class=>'tabbody'}, $main_html),
1901 #div({-id=>'track_page', -class=>'tabbody'}, $tracks_html),
1902 #$uses_database?div({-id=>'community_tracks_page',-class=>'tabbody'}, $community_tracks_html) : "",
1903 #div({-id=>'custom_tracks_page', -class=>'tabbody'}, $custom_tracks_html),
1904 #div({-id=>'settings_page', -class=>'tabbody'}, $settings_html),
1905 );
1906 return $html;
1907 }
1908  
1909 1;