blib/lib/Legacy/Graphics/Browser/Synteny.pm untested

filter: Statementstmt 0.0% 0 / 483 Branchbran 0.0% 0 / 202 Conditioncond 0.0% 0 / 149 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 49 Podpod 0.0% 0 / 34 totaltotal 0.0% 0 / 917 CC 102 SCAR 52.5
CC
102
Cov
0%
zoomnav
812.0
lookup_features_from_db
182.0
unit_label
110.0
CRAP 189.6
1 package Legacy::Graphics::Browser::Synteny;
2  
3 # $Id: Synteny.pm,v 1.1.2.10 2009-08-28 08:10:03 sheldon_mckay Exp $
4  
5 use strict;
6  
7  
8 use Bio::Root::RootI;
9 use Bio::Graphics::Browser2;
10 use Legacy::Graphics::Browser;
11 use Legacy::Graphics::Browser::Util;
12 use CGI qw/:standard escape/;
13 use CGI::Toggle;
14 use CGI::Carp 'fatalsToBrowser';
15 use GD;
16 use vars qw/$VERSION @ISA/;
17 @ISA = ('Legacy::Graphics::Browser');
18  
19 $VERSION = 0.01;
20  
21 use constant DEFAULT_FINE_ZOOM => '20%';
22 use constant BUTTONSDIR => '/gbrowse2/images/buttons';
23 use constant OVERVIEW_RATIO => 0.9;
24 use constant DEBUG => 0;
25 use constant RULER_INTERVALS => 20;
26  
27 sub new {
28 my $class = shift;
29 my $self = {};
30 $self->{lookup} = {};
31 $self->{globals} = Bio::Graphics::Browser2->open_globals;
32 return bless $self;
33 }
34  
35 *segment = \&current_segment;
36  
37 sub current_segment {
38 my ( $self, $segment ) = @_;
39 $self->{current_segment} = $segment if $segment;
40 $self->{current_segment} ||= $self->{segment};
41 return $self->{current_segment};
42 }
43  
44 sub get_source {
45 my $self = shift;
46 my $source;
47 my $new_source = param('source') || param('src') || path_info();
48 $new_source =~ s!^/+!!;
49 $new_source =~ s!/+$!!;
50 # gbrowse_syn expects a list
51 if ( wantarray ) {
52 my $old_source = cookie('source')
53 unless $new_source && param('.cgifields');
54 $source = $new_source || $old_source;
55 $source ||= $self->source;
56
57 # the default, whatever it is
58 $self->source($source);
59 return ( $source, $old_source );
60 }
61 # otherwise just the source
62 else {
63 $self->source($new_source) if defined $new_source;
64 return $new_source;
65 }
66 }
67  
68 sub search_src {
69 my $self = shift;
70 my $val = shift;
71 if ($val) {
72 $self->{search_src} = $val;
73 }
74 return $self->{search_src};
75 }
76  
77  
78 # species-specific landmark examples
79 sub show_examples {
80 my $self = shift;
81 my $params = join ';', '', @_;
82 $params ||= '';
83 my $examples = $self->setting('examples') or return;
84 my @examples = shellwords($examples);
85 my $src = $self->source || $self->get_source;
86 my @urls;
87 while (@examples) {
88 my $species = shift @examples;
89 my $name = shift @examples;
90 push @urls, a({-href=>"?search_src=$species;name=$name"},"$species $name");
91 }
92
93 return b( $self->tr('Examples') ) . ': ' . join( ', ', @urls ) . ". ";
94 }
95  
96 sub toggle {
97 my $self = shift;
98 my ($state,$title,@contents) = @_;
99 toggle_section( {on=>$state},
100 "${title}_panel",
101 b($title),
102 @contents);
103 }
104  
105 sub source_menu {
106 my $self = shift;
107 my @sources = $self->sources;
108 my $show_sources = $self->setting('show sources');
109 $show_sources ||= 1;
110  
111 # default to true
112 my $sources = $show_sources && @sources > 1;
113 my $popup = popup_menu(
114 -name => 'source',
115 -values => \@sources,
116 -labels => { map { $_ => $self->description($_) } $self->sources },
117 -default => $self->source,
118 -onChange => 'document.mainform.submit()'
119 );
120 return b( $self->tr('DATA_SOURCE') ) . br
121 . ( $sources ? $popup : $self->description( $self->source ) );
122 }
123  
124 sub slidertable {
125 my $self = shift;
126 my $small_pan = shift;
127 my $buttons = $self->globals->button_url || BUTTONSDIR;
128 my $segment = $self->current_segment or fatal_error("No segment defined");
129 my $span = $small_pan ? int $segment->length/2 : $segment->length;
130 my $half_title = $self->unit_label( int $span / 2 );
131 my $full_title = $self->unit_label($span);
132 my $half = int $span / 2;
133 my $full = $span;
134 my $fine_zoom = $self->get_zoomincrement();
135 Delete($_) foreach qw(ref start stop);
136 my @lines;
137 push @lines,
138 hidden( -name => 'start', -value => $segment->start, -override => 1 );
139 push @lines,
140 hidden( -name => 'stop', -value => $segment->end, -override => 1 );
141 push @lines,
142 hidden( -name => 'ref', -value => $segment->seq_id, -override => 1 );
143 push @lines, (
144 image_button(
145 -src => "$buttons/green_l2.gif",
146 -name => "left $full",
147 -border => 0,
148 -title => "left $full_title"
149 ),
150 image_button(
151 -src => "$buttons/green_l1.gif",
152 -name => "left $half",
153 -border => 0,
154 -title => "left $half_title"
155 ),
156 ' ',
157 image_button(
158 -src => "$buttons/minus.gif",
159 -name => "zoom out $fine_zoom",
160 -border => 0,
161 -title => "zoom out $fine_zoom"
162 ),
163 ' ', $self->zoomBar, ' ',
164 image_button(
165 -src => "$buttons/plus.gif",
166 -name => "zoom in $fine_zoom",
167 -border => 0,
168 -title => "zoom in $fine_zoom"
169 ),
170 ' ',
171 image_button(
172 -src => "$buttons/green_r1.gif",
173 -name => "right $half",
174 -border => 0,
175 -title => "right $half_title"
176 ),
177 image_button(
178 -src => "$buttons/green_r2.gif",
179 -name => "right $full",
180 -border => 0,
181 -title => "right $full_title"
182 ),
183 );
184 return join( '', @lines );
185 }
186  
187 sub unit_label {
188 my ( $self, $value ) = @_;
189 my $unit = $self->setting('units') || 'bp';
190 my $divider = $self->setting('unit_divider') || 1;
191 $value /= $divider;
192 my $abs = abs($value);
193 my $label;
194 $label = $abs >= 1e9 ? sprintf( "%.4g G%s", $value / 1e9, $unit )
195 : $abs >= 1e6 ? sprintf( "%.4g M%s", $value / 1e6, $unit )
196 : $abs >= 1e3 ? sprintf( "%.4g k%s", $value / 1e3, $unit )
197 : $abs >= 1 ? sprintf( "%.4g %s", $value, $unit )
198 : $abs >= 1e-2 ? sprintf( "%.4g c%s", $value * 100, $unit )
199 : $abs >= 1e-3 ? sprintf( "%.4g m%s", $value * 1e3, $unit )
200 : $abs >= 1e-6 ? sprintf( "%.4g u%s", $value * 1e6, $unit )
201 : $abs >= 1e-9 ? sprintf( "%.4g n%s", $value * 1e9, $unit )
202 : sprintf( "%.4g p%s", $value * 1e12, $unit );
203 if (wantarray) {
204 return split ' ', $label;
205 }
206 else {
207 return $label;
208 }
209 }
210  
211 sub get_zoomincrement {
212 my $self = shift;
213 my $zoom = $self->setting('fine zoom') || DEFAULT_FINE_ZOOM;
214 $zoom;
215 }
216  
217 sub zoomBar {
218 my $self = shift;
219 my $segment = $self->current_segment;
220 my ($show) = $self->tr('Show');
221 my %seen;
222 my @ranges = grep { !$seen{$_}++ } sort { $b <=> $a } ($segment->length, $self->get_ranges());
223 my %labels = map { $_ => $show . ' ' . $self->unit_label($_) } @ranges;
224 return popup_menu(
225 -class => 'searchtitle',
226 -name => 'span',
227 -values => \@ranges,
228 -labels => \%labels,
229 -default => $segment->length,
230 -force => 1,
231 -onChange => 'document.mainform.submit()',
232 );
233 }
234  
235 sub split_labels {
236 my $self = shift;
237 map { /^(http|ftp|das)/ ? $_ : split /[+-]/ } @_;
238 }
239  
240 sub click_bar {
241 my $self = shift;
242 my $width = $self->width;
243 my @rgb = $_[0] ? @_ : qw/255 255 255/;
244 @rgb == 3 or fatal_error("usage: \$browser->click_bar(\$width,\$R,\$G,\$B)");
245 my $gd = GD::Image->new( $width, 20 );
246 my $bg = $gd->colorAllocate(@rgb);
247 $gd->fill( 1, 1, $gd );
248 my $image = $self->generate_image($gd);
249 }
250  
251  
252 sub zoomnav {
253 my ( $self, $settings ) = @_;
254 return unless $settings->{ref};
255 my $start = $settings->{start};
256 my $stop = $settings->{stop};
257 my $span = $stop - $start + 1;
258 my $divisor = $self->setting( general => 'unit_divider' ) || 1;
259 warn "before adjusting, start = $start, stop = $stop, span=$span" if DEBUG;
260 my $flip = $settings->{flip} ? -1 : 1;
261  
262 # get zoom parameters
263 my $selected_span = param('span');
264 my ($zoom) = grep {/^zoom (out|in) \S+/} param();
265 my ($nav) = grep {/^(left|right) \S+/} param();
266 my $overview_x = param('overview.x');
267 my $regionview_x = param('regionview.x');
268 my $regionview_size = $settings->{region_size};
269 my $seg_min = param('seg_min');
270 my $seg_max = param('seg_max');
271 my $segment_length = $seg_max - $seg_min + 1
272 if defined $seg_min && defined $seg_max;
273 my $zoomlevel = $self->unit_to_value($1)
274 if $zoom && $zoom =~ /((?:out|in) .+)\.[xy]/;
275 my $navlevel = $self->unit_to_value($1)
276 if $nav && $nav =~ /((?:left|right) .+)/;
277  
278 if ( defined $zoomlevel ) {
279 warn "zoom = $zoom, zoomlevel = $zoomlevel" if DEBUG;
280 my $center = int( $span / 2 ) + $start;
281 my $range = int( $span * ( 1 - $zoomlevel ) / 2 );
282 $range = 1 if $range < 1;
283 ( $start, $stop ) = ( $center - $range, $center + $range - 1 );
284 }
285 elsif ( defined $navlevel ) {
286 $start += $flip * $navlevel;
287 $stop += $flip * $navlevel;
288 }
289 elsif ( defined $overview_x && defined $segment_length ) {
290 my @overview_tracks = grep { $settings->{features}{$_}{visible} }
291 $self->config->overview_tracks;
292 my ( $padl, $padr ) = $self->overview_pad( \@overview_tracks );
293 $settings->{width} ||= 800;
294 my $overview_width = ( $settings->{width} * OVERVIEW_RATIO );
295  
296 # adjust for padding in pre 1.6 versions of bioperl
297 $overview_width -= ($padl+$padr) unless Legacy::Graphics::Panel->can('auto_pad');
298 my $click_position = $seg_min + $segment_length * ( $overview_x - $padl )
299 / $overview_width;
300 $span = $self->setting('DEFAULT_SEGMENT')
301 if $span > $self->setting('MAX_SEGMENT');
302 $start = int( $click_position - $span / 2 );
303 $stop = $start + $span - 1;
304 }
305 elsif ( defined $regionview_x ) {
306 my ( $regionview_start, $regionview_end )
307 = get_regionview_seg( $settings, $start, $stop );
308 my @regionview_tracks = grep { $settings->{features}{$_}{visible} }
309 $self->self->regionview_tracks;
310 my ( $padl, $padr ) = $self->overview_pad( \@regionview_tracks );
311 my $regionview_width
312 = ( $settings->{width} * OVERVIEW_RATIO );
313  
314 # adjust for padding in pre 1.6 versions of bioperl
315 $regionview_width -= ($padl+$padr) unless Legacy::Graphics::Panel->can('auto_pad');
316 my $click_position
317 = $regionview_size * ( $regionview_x - $padl ) / $regionview_width;
318 $span = $self->setting('DEFAULT_SEGMENT')
319 if $span > $self->setting('MAX_SEGMENT');
320 $start = int( $click_position - $span / 2 + $regionview_start );
321 $stop = $start + $span - 1;
322 }
323 elsif ($selected_span) {
324 warn "selected_span = $selected_span" if DEBUG;
325 my $center = int( ( $span / 2 ) ) + $start;
326 my $range = int( ($selected_span) / 2 );
327 $start = $center - $range;
328 $stop = $start + $selected_span - 1;
329 }
330 warn
331 "after adjusting for navlevel, start = $start, stop = $stop, span=$span"
332 if DEBUG;
333  
334 # to prevent from going off left end
335 if (defined $seg_min && $start < $seg_min) {
336 warn "adjusting left because $start < $seg_min" if DEBUG;
337 ( $start, $stop ) = ( $seg_min, $seg_min + $stop - $start );
338 }
339 # to prevent from going off right end
340 if (defined $seg_max && $stop > $seg_max) {
341 warn "adjusting right because $stop > $seg_max" if DEBUG;
342 ( $start, $stop ) = ( $seg_max - ( $stop - $start ), $seg_max );
343 }
344 # to prevent divide-by-zero errors when zoomed down to a region < 2 bp
345 $stop = $start + ($span > 4 ? $span - 1 : 4) if $stop <= $start+2;
346 warn "start = $start, stop = $stop\n" if DEBUG;
347 $divisor = 1 if $divisor =~ /[^0-9]/;;
348 $settings->{start} = $start / $divisor;
349 $settings->{stop} = $stop / $divisor;
350 }
351  
352 sub unit_to_value {
353 my ( $self, $string ) = @_;
354 my $sign = $string =~ /out|left/ ? '-' : '+';
355 my ( $value, $units ) = $string =~ /([\d.]+) ?(\S+)/;
356  
357 return unless defined $value;
358  
359 $value /= 100 if $units eq '%';
360  
361 # percentage;
362 $value *= 1000 if $units =~ /kb/i;
363 $value *= 1e6 if $units =~ /mb/i;
364 $value *= 1e9 if $units =~ /gb/i;
365 return "$sign$value";
366 }
367  
368 sub whole_segment {
369 my $self = shift;
370 die ('No Segment') unless $self->current_segment;
371 return $self->{whole_segment} if $self->{whole_segment};
372 my $segment = $self->current_segment;
373 my $factory;
374 if ($segment->can('factory')) {
375 $factory = $segment->factory;
376 }
377 else {
378 my $segments = $self->features2segments([$segment]);
379 $segment = $segments->[0];
380 $factory = $segment->factory;
381 }
382  
383
384 # the segment class has been deprecated, but we still must support it
385 my $class = eval {$segment->seq_id->class} || eval{$factory->refclass};
386 ( $self->{whole_segment} ) = $factory->segment(
387 -class => $class,
388 -name => $segment->seq_id
389 );
390 $self->{whole_segment} ||= $segment;
391
392 # just paranoia
393 return $self->{whole_segment};
394 }
395  
396 sub resize {
397 my $self = shift;
398 my $segment = $self->current_segment;
399 my $whole_segment = $self->whole_segment;
400 my $divider = $self->setting('unit_divider') || 1;
401 my $min_seg_size = $self->setting('min segment')
402 || $self->setting('MIN_SEG_SIZE');
403 $min_seg_size /= $divider;
404  
405 my ( $new_start, $new_stop, $fix ) = ( $segment->start, $segment->end, 0 );
406  
407 if ( $segment->length < $min_seg_size ) {
408 my $resize = $min_seg_size;
409 my $middle = int( ( $segment->start + $segment->end ) / 2 );
410 $new_start = $middle - int( $resize / 2 );
411 $new_stop = $middle + int( $resize / 2 );
412 $fix++;
413 }
414  
415 if ( $segment->start < $whole_segment->start ) {
416 $new_start = $whole_segment->start;
417 $fix++;
418 }
419 elsif ( $segment->start > $whole_segment->end ) {
420 $new_start = $whole_segment->end - $min_seg_size;
421 $fix++;
422 }
423  
424 if ( $segment->end > $whole_segment->end ) {
425 $new_stop = $whole_segment->end;
426 $fix++;
427 }
428 elsif ( $segment->end < $whole_segment->start ) {
429 $new_stop = $whole_segment->start + $min_seg_size;
430 $fix++;
431 }
432 # error($self->tr('Small_interval',$resize));
433 # error message return unless $fix;
434
435 $new_start = $whole_segment->start if $new_start < $whole_segment->start;
436 $new_stop = $whole_segment->end if $new_stop > $whole_segment->end;
437 my $new_seg = $segment->factory->segment(
438 -name => $segment->seq_id,
439 -start => $new_start,
440 -end => $new_stop,
441 -absolute => 1
442 );
443 $self->current_segment($new_seg);
444 }
445  
446 sub features2segments {
447 my ( $self, $features, $db ) = @_;
448 my $refclass = $self->setting('reference class') || 'Sequence';
449 $db ||= open_database();
450 my @segments = map {
451 my $version = eval { $_->isa('Legacy::SeqFeatureI') ? undef: $_->version };
452 $db->segment(
453 -class => $refclass,
454 -name => $_->ref,
455 -start => $_->start,
456 -stop => $_->end,
457 -absolute => 1,
458 defined $version ? ( -version => $version ) : ()
459 )
460 } @$features;
461 warn "segments = @segments\n" if DEBUG;
462 \@segments;
463 }
464  
465 sub get_features {
466 my ( $self, $settings, $db ) = @_;
467 $db ||= open_database();
468 unless ($db) {
469 fatal_error(
470 "ERROR: Unable to open database",
471 $self->setting('description'),
472 pre($@)
473 );
474 }
475 eval { $db->biosql->version( $settings->{version} ) };
476  
477 # if no name is specified but there is a "initial landmark" defined in the
478 # config file, then we default to that.
479 $settings->{name} ||= $self->setting('initial landmark')
480 if defined $self->setting('initial landmark') && !defined $settings->{q};
481  
482 my @features = $self->lookup_features_from_db( $db, $settings );
483  
484 # sort of hacky way to force keyword search on wildcards
485 if (defined $settings->{name} && $settings->{name} =~ /[*?]/ ){
486 my $searchterm = $settings->{name};
487 push @features, do_keyword_search($searchterm)
488 if length $searchterm > 0;
489 }
490
491 # h'mmm. Couldn't find the feature. See if it is in an uploaded file.
492 @features = $self->lookup_features_from_external_sources($settings,$settings->{name}, undef )
493 unless @features;
494  
495 return \@features;
496 }
497  
498 sub lookup_features_from_external_sources {
499 my ( $self, $settings, $searchterm ) = @_;
500 return unless my $uploads = $self->setting('UPLOADED_SOURCES');
501  
502 my @uploaded_files = map { $uploads->feature_file($_) }
503 grep { $settings->{features}{$_}{visible} } $uploads->files;
504  
505 for my $file (@uploaded_files) {
506 next unless $file->can('get_feature_by_name');
507 my @features = $file->get_feature_by_name($searchterm);
508 return @features if @features;
509 }
510 # No exact match. Try inexact match.
511 my $max_keywords = $self->setting('keyword search max')|| $self->setting('MAX_KEYWORD_RESULTS');
512  
513 for my $file (@uploaded_files) {
514 next unless $file->can('search_notes');
515 my @matches = $file->search_notes( $searchterm, $max_keywords );
516 return map {
517 my ( $feature, $description, $score ) = @$_;
518 Legacy::Graphics::Feature->new(
519 -name => $feature->display_name,
520 -type => $description,
521 -score => $score,
522 -ref => $feature->ref,
523 -start => $feature->start,
524 -end => $feature->end
525 )
526 } @matches if @matches;
527 }
528 return;
529 }
530  
531 sub lookup_features_from_db {
532 my ( $self, $db, $settings ) = @_;
533 my @segments;
534 warn
535 "lookup_features_from_db: name = $settings->{name}, ref = $settings->{ref}, start = $settings->{start}, "
536 . "stop = $settings->{stop}, version = $settings->{version} db = $db"
537 if DEBUG;
538  
539 my $divisor = $self->setting( general => 'unit_divider' ) || 1;
540 my $padding = $self->setting( general => 'landmark_padding' ) || 0;
541 my $too_many = $self->setting('TOO_MANY_SEGMENTS');
542  
543 if ( my $name = $settings->{name} ) {
544 @segments = $self->name2segments( $name, $db, $too_many );
545 }
546 elsif ( ( my $names = $settings->{q} ) && ref $settings->{q} ) {
547 warn "looking up by query: q = $names" if DEBUG;
548 my $max = $too_many / @$names;
549 @segments = map { $self->name2segments( $_, $db, $max ) } @$names;
550 }
551 elsif ( my $ref = $settings->{ref} ) {
552 my @argv = ( -name => $ref );
553 push @argv, ( -start => $settings->{start} * $divisor )
554 if defined $settings->{start};
555 push @argv, ( -end => $settings->{stop} * $divisor )
556 if defined $settings->{stop};
557 warn "looking up by @argv" if DEBUG;
558 @segments = $db->segment(@argv);
559 }
560 # expand by a bit if padding is requested
561 # THIS CURRENTLY ISN'T WORKING PROPERLY
562 if (@segments == 1 && $padding > 0 && !$settings->{name} ){
563 $segments[0] = $segments[0]->subseq( -$padding, $segments[0]->length + $padding );
564 }
565 # some segments are not going to support the absolute() method
566 # if they come out of BioPerl
567 eval {$_->absolute(1)} foreach @segments;
568 return unless @segments;
569  
570 # Filter out redundant segments; this can happen when the same basic feature
571 # ia present under several names, such as "genes" and "frameworks"
572 my %seenit;
573 my $version = eval { $_->isa('Legacy::SeqFeatureI') ? undef: $_->version };
574 $version ||= 0;
575 @segments = grep { !$seenit{ $_->seq_id, $_->start, $_->end, $version }++ }
576 @segments;
577 return @segments if @segments > 1;
578  
579 # this prevents any confusion over (ref,start,stop) and (name) addressing. $settings->{ref} = $segments[0]->seq_id;
580 $settings->{start} = $segments[0]->start / $divisor;
581 $settings->{stop} = $segments[0]->end / $divisor;
582
583 return $segments[0];
584 }
585  
586 sub do_keyword_search {
587 my ( $self, $searchterm, $db ) = @_;
588 $db ||= open_database();
589  
590 # if they wanted something specific, don't give them non-specific results.
591 return if $searchterm =~ /^[\w._-]+:/;
592  
593 # Need to untaint the searchterm. We are very lenient about
594 # what is accepted here because we wil be quote-metaing it later.
595 $searchterm =~ /([\w .,~!@\#$%^&*()-+=<>?\/]+)/;
596 $searchterm = $1;
597 my $max_keywords = $self->setting('keyword search max')
598 || $self->setting('MAX_KEYWORD_RESULTS');
599 my @matches = $db->search_notes( $searchterm, $max_keywords );
600 my @results;
601  
602 for my $r (@matches) {
603 my ( $name, $description, $score ) = @$r;
604 my ($seg) = $db->segment($name) or next;
605 push @results,
606 Legacy::Graphics::Feature->new(
607 -name => $name,
608 -class => eval { $name->class } || undef,
609 -type => $description,
610 -score => $score,
611 -ref => $seg->abs_ref,
612 -start => $seg->abs_start,
613 -end => $seg->abs_end,
614 -factory => $db
615 );
616 }
617  
618 return @results;
619 }
620  
621 sub make_cookie {
622 my $self = shift;
623 my ( $name, $val ) = @_;
624 my $cookie = cookie(
625 -name => $name,
626 -value => $val
627 );
628 return $cookie;
629 }
630  
631 sub make_centering_map {
632 my $self = shift;
633 my ($ruler,$flip,$label,$scale_map_type) = @_;
634 my @map = $label ? ($label) : ();
635  
636 return if $ruler->[3]-$ruler->[1] == 0;
637  
638 my $segment = $self->current_segment;
639 my $whole_segment = $self->whole_segment($segment);
640 my $span = $segment->length;
641 my $length = $whole_segment->length;
642 my $plength = $ruler->[3]-$ruler->[1];
643 my $scale = $length/$plength;
644 my $pl = $ruler->[-1]->panel->pad_left;
645 my $ruler_intervals = RULER_INTERVALS;
646  
647 # divide into intervals
648 my $portion = $plength/$ruler_intervals;
649 my $ref = $ruler->[0]->seq_id;
650 my $source = $self->search_src;
651  
652 for my $i (0..$ruler_intervals-1) {
653 my $j = $i + 1;
654 my $x1 = int(($portion * $i)+0.5);
655 my $x2 = int(($portion * $j)+0.5);
656 my ($start,$stop);
657 my $middle = $scale * ($x1+$x2)/2;
658 $start = int($middle - $span/2);
659 $stop = int($start + $span - 1);
660  
661 $x1 += $pl;
662 $x2 += $pl;
663  
664 my $url = "?ref=$ref;start=$start;stop=$stop;search_src=$source";
665 $url .= ";flip=1" if $flip;
666
667 push @map, join("\t",'ruler',$x1, $ruler->[2], $x2, $ruler->[4],
668 href => $url, title => 'recenter', alt => 'recenter');
669 }
670
671 return $label ? \@map : @map;
672 }
673  
674 sub page_settings {
675 my $self = shift;
676 my $key = shift;
677  
678 if (ref $key) {
679 return $self->{page_settings} = $key;
680 }
681  
682 if ($key) {
683 return $self->{page_settings}->{$key}
684 }
685  
686 return $self->{page_settings};
687 }
688  
689 sub flush_settings {
690 my $self = shift;
691 delete $self->{page_settings};
692 $self->{lookup} = {};
693 }
694  
695 sub name2hit {
696 my $self = shift;
697 return $self->_lookup('name2hit',@_);
698 }
699  
700 sub part2name {
701 my $self = shift;
702 return $self->_lookup('part2name',@_);
703 }
704  
705 sub flip {
706 my $self = shift;
707 return $self->_lookup('flip',@_);
708 }
709  
710 sub panel_flip {
711 my ($self,$key) = @_;
712 $self->{lookup}->{panel_flip} ||= {};
713 $self->{lookup}->{panel_flip}->{$key} ||= {};
714 return $self->{lookup}->{panel_flip}->{$key};
715 }
716  
717 sub _lookup {
718 my ($self,$param,$key,$val) = @_;
719 $self->{lookup}->{$param} ||= {};
720 my $hash = $self->{lookup}->{$param};
721 $hash->{$key} = $val if defined $val;
722 return $hash->{$key};
723 }
724  
725 sub print_page_top {
726 my $self = shift;
727 my $title = shift;
728 my $reset_all = shift;
729 my $alert = shift;
730 my $session = shift;
731 local $^W = 0; # to avoid a warning from CGI.pm
732  
733 my @stylesheet_headers;
734 my @stylesheets = shellwords($self->setting('stylesheet') || '/gbrowse2/css/gbrowse.css');
735 for my $ss (@stylesheets) {
736 my ($url,$media) = $ss =~ /^([^(]+)(?:\((.+)\))?/;
737 $media ||= 'all';
738 push @stylesheet_headers,CGI::Link({-rel=>'stylesheet',
739 -type=>'text/css',
740 -href=>$self->relative_path($url),
741 -media=>$media});
742 }
743  
744 my $cookie = CGI::Cookie->new(-name => $CGI::Session::NAME,
745 -value => $session->id,
746 -path => url(-absolute=>1),
747 -expires => '+1d');
748  
749 print_header(-cookie => [$cookie], -expires => 'now');
750  
751 my @args = (-title => $title,
752 -encoding=>$self->tr('CHARSET'),
753 );
754 push @args,(-head=>$self->setting('head')) if $self->setting('head');
755 push @args,(-gbrowse_images => $self->globals->button_url || '/gbrowse2/images/buttons');
756 push @args,(-gbrowse_js => $self->globals->js_url || '/gbrowse2/js');
757 push @args,(-reset_toggle => 1) if $reset_all;
758  
759 my @onload;
760 push @onload, $self->setting('onload') if $self->setting('onload');
761 push @onload, "alert('$alert')" if $alert;
762 push @onload, 'Overview.prototype.initialize()';
763  
764 # push all needed javascript files onto top of page
765 my $js = $self->globals->js_url || '/gbrowse2/js';
766 my @js;
767 push @js, qw(prototype.js balloon.config.js balloon.js);
768 push @js, qw(rubber.js overviewSelect.js);
769  
770 my @scripts = map { {src=> "$js/$_" } } @js;
771 push @args, (-script => \@scripts);
772 push @args, (-onLoad => join('; ',@onload));
773 push @args, (-head => \@stylesheet_headers);
774  
775 print start_html(@args);
776  
777 # make a sham controller to keep the GB2 js happy
778 print <<END;
779 <script type="text/javascript">
780 var Controller;
781 Controller = new Object();
782 Controller.gbrowse_syn = true;
783 Controller.update_coordinates = function (segment) {
784 document.mainform.name.value = segment;
785 document.mainform.submit();
786 };
787 </script>
788 END
789 ;
790  
791 $self->print_balloon_settings();
792 }
793  
794 sub print_balloon_settings {
795 my $self = shift;
796 my $custom_balloons = $self->setting('custom balloons');
797 my $bstyle = $self->setting('balloon style') || 'GBubble';
798 my $images = ($self->globals->balloon_url() || '/images/balloons/') . "/$bstyle";
799 my %config_values = $custom_balloons =~ /\[([^]]+)\]([^[]+)/g;
800 $config_values{'balloon'} ||= <<END;
801 images = $images
802 delayTime = 500
803 END
804  
805 my $balloon_settings;
806  
807 for my $balloon (keys %config_values) {
808 my %config = $config_values{$balloon} =~ /(\w+)\s*=\s*(\S+)/g;
809 my $img = $config{images} || "$images/balloons";
810 $balloon_settings .= <<END;
811 var $balloon = new Balloon;
812 BalloonConfig(balloon);
813 $balloon.images = '$img';
814 $balloon.balloonImage = 'balloon.png';
815 $balloon.ieImage = 'balloon_ie.png';
816 $balloon.upLeftStem = 'up_left.png';
817 $balloon.downLeftStem = 'down_left.png';
818 $balloon.upRightStem = 'up_right.png';
819 $balloon.downRightStem = 'down_right.png';
820 $balloon.closeButton = 'close.png';
821 END
822 for my $option (keys %config) {
823 next if $option eq 'images';
824 my $value = $config{$option} =~ /^[\d.-]+$/ ? $config{$option} : "'$config{$option}'";
825 $balloon_settings .= "$balloon.$option = $value;\n";
826 }
827 }
828 print "<script>\n$balloon_settings\n</script>\n";
829 }
830  
831 sub source {
832 my $self = shift;
833 my $d = $self->{source};
834 if (@_) {
835 my $source = shift;
836 $self->{source} = $source;
837 }
838 $d;
839 }
840  
841 1;