« blib/lib/Bio/Graphics/Browser2/DataLoader/featurefile.pm
↑ Summary
blib/lib/Legacy/Graphics/Browser/Synteny.pm »
| 1 | package Legacy::Graphics::Browser; | ||
| 2 | # $Id: Browser.pm,v 1.167.4.34.2.32.2.126 2009-09-03 17:08:10 lstein Exp $ | ||
| 3 | |||
| 4 | # This is an old version of Bio::Graphics::Browser retained for gbrowse_syn | ||
| 5 | # It is on the path to deprecation | ||
| 6 | |||
| 7 | =head1 METHODS | ||
| 8 | |||
| 9 | The remainder of this document describes the methods available to the | ||
| 10 | programmer. | ||
| 11 | |||
| 12 | =cut | ||
| 13 | |||
| 14 | use strict; | ||
| 15 | use File::Basename 'basename'; | ||
| 16 | use Bio::Graphics; | ||
| 17 | use Carp qw(carp croak cluck); | ||
| 18 | use CGI qw(img param escape unescape url div span image_button); | ||
| 19 | use CGI::Toggle 'toggle_section'; | ||
| 20 | use Digest::MD5 'md5_hex'; | ||
| 21 | use File::Path 'mkpath'; | ||
| 22 | use IO::File; | ||
| 23 | use Legacy::Graphics::Browser::I18n; | ||
| 24 | use Legacy::Graphics::Browser::Util qw(modperl_request is_safari shellwords); | ||
| 25 | use Bio::Graphics::Browser2; | ||
| 26 | |||
| 27 | require Exporter; | ||
| 28 | |||
| 29 | use vars '$VERSION','@ISA','@EXPORT'; | ||
| 30 | $VERSION = '1.17'; | ||
| 31 | |||
| 32 | @ISA = 'Exporter'; | ||
| 33 | @EXPORT = ('commas','DEFAULT_OVERVIEW_BGCOLOR'); | ||
| 34 | |||
| 35 | use constant DEFAULT_WIDTH => 800; | ||
| 36 | use constant DEFAULT_DB_ADAPTOR => 'Bio::DB::GFF'; | ||
| 37 | use constant DEFAULT_KEYSTYLE => 'bottom'; | ||
| 38 | use constant DEFAULT_EMPTYTRACKS => 'key'; | ||
| 39 | use constant RULER_INTERVALS => 20; # fineness of the centering map on the ruler | ||
| 40 | use constant TOO_MANY_SEGMENTS => 5_000; | ||
| 41 | use constant MAX_SEGMENT => 1_000_000; | ||
| 42 | use constant DEFAULT_SEGMENT => 100_000; | ||
| 43 | use constant DEFAULT_RANGES => q(100 500 1000 5000 10000 25000 100000 200000 400000); | ||
| 44 | use constant MIN_OVERVIEW_PAD => 25; | ||
| 45 | use constant PAD_OVERVIEW_BOTTOM => 5; | ||
| 46 | use constant PAD_DETAIL_SIDES => 25; | ||
| 47 | use constant DEFAULT_OVERVIEW_BGCOLOR => 'wheat'; | ||
| 48 | |||
| 49 | # amount of time to remember persistent settings | ||
| 50 | use constant REMEMBER_SOURCE_TIME => '+12M'; # 12 months | ||
| 51 | use constant REMEMBER_SETTINGS_TIME => '+1M'; # 1 month | ||
| 52 | |||
| 53 | use constant DEBUG => 0; | ||
| 54 | |||
| 55 | if( $ENV{MOD_PERL} && | ||
| 56 | exists $ENV{MOD_PERL_API_VERSION} && | ||
| 57 | $ENV{MOD_PERL_API_VERSION} >= 2) { | ||
| 58 | require Apache2::SubRequest; | ||
| 59 | require Apache2::RequestUtil; | ||
| 60 | require Apache2::ServerUtil; | ||
| 61 | } | ||
| 62 | |||
| 63 | =head2 new() | ||
| 64 | |||
| 65 | my $browser = Legacy::Graphics::Browser->new(); | ||
| 66 | |||
| 67 | Create a new Legacy::Graphics::Browser object. The object is initially | ||
| 68 | empty. This is done automatically by gbrowse. | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | sub new { | ||
| 73 | my $class = shift; | ||
| 74 | my $self = bless { },ref($class) || $class; | ||
| 75 | $self->{globals} = Bio::Graphics::Browser2->open_globals; | ||
| 76 | $self; | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | # patch to access GB2-style global config | ||
| 81 | sub globals { shift->{globals} } | ||
| 82 | |||
| 83 | =head2 url_label() | ||
| 84 | |||
| 85 | my $url_label = $browser->url_label($yucky_url); | ||
| 86 | |||
| 87 | Creates a label.alias for URL strings starting with 'http' or 'ftp'. | ||
| 88 | The last word (following a '/') in the url is used for the label. | ||
| 89 | Returns a string "url:label". | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub url_label { | ||
| 94 | my ($self,$label) = @_; | ||
| 95 | my $key; | ||
| 96 | if ($label =~ /^http|^ftp/) { | ||
| 97 | my $l = $label; | ||
| 98 | $l =~ s!^\W+//!!; | ||
| 99 | my (undef,$type) = $l =~ /\S+t(ype)?=([^;\&]+)/; | ||
| 100 | $l =~ s/\?.+//; | ||
| 101 | ($key) = grep /$_/, reverse split('/',$l); | ||
| 102 | $key = "url:$key" if $key; | ||
| 103 | $key .= ":$type" if $type; | ||
| 104 | } | ||
| 105 | return $key || $label; | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | |||
| 110 | =head2 read_configuration() | ||
| 111 | |||
| 112 | my $success = $browser->read_configuration('/path/to/gbrowse.conf'); | ||
| 113 | |||
| 114 | Parse the files in the gbrowse.conf configuration directory. This is | ||
| 115 | done automatically by gbrowse. Returns a true status code if | ||
| 116 | successful. | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub read_configuration { | ||
| 121 | my $self = shift; | ||
| 122 | my $conf_dir = shift; | ||
| 123 | my $suffix = shift || 'conf'; | ||
| 124 | |||
| 125 | $self->{conf} ||= {}; | ||
| 126 | |||
| 127 | croak("$conf_dir: not a directory") unless -d $conf_dir; | ||
| 128 | opendir(D,$conf_dir) or croak "Couldn't open $conf_dir: $!"; | ||
| 129 | my @conf_files = map { "$conf_dir/$_" } grep {/\.$suffix$/} grep {!/^\.|^#|log4perl/} readdir(D); | ||
| 130 | close D; | ||
| 131 | |||
| 132 | # try to work around a bug in Apache/mod_perl which appears when | ||
| 133 | # running under linux/glibc 2.2.1 | ||
| 134 | unless (@conf_files) { | ||
| 135 | @conf_files = glob("$conf_dir/*.$suffix"); | ||
| 136 | } | ||
| 137 | |||
| 138 | # get modification times | ||
| 139 | my %mtimes = map { $_ => (stat($_))[9] } @conf_files; | ||
| 140 | |||
| 141 | for my $file (@conf_files) { | ||
| 142 | my $basename = basename($file,".$suffix"); | ||
| 143 | next if $basename eq 'GBrowse'; # global settings -- used in main branch | ||
| 144 | $basename =~ s/^\d+\.//; | ||
| 145 | next if defined($self->{conf}{$basename}{mtime}) | ||
| 146 | && ($self->{conf}{$basename}{mtime} >= $mtimes{$file}); | ||
| 147 | my $config = Legacy::Graphics::BrowserConfig->new(-file => $file, | ||
| 148 | -safe => 1) or next; | ||
| 149 | $self->{conf}{$basename}{data} = $config; | ||
| 150 | $self->{conf}{$basename}{mtime} = $mtimes{$file}; | ||
| 151 | $self->{conf}{$basename}{path} = $file; | ||
| 152 | } | ||
| 153 | |||
| 154 | my $default_source; | ||
| 155 | for my $basename (sort keys %{$self->{conf}}) { | ||
| 156 | my $config = $self->{conf}{$basename}{data}; | ||
| 157 | $default_source ||= $basename if $config->authorized('general'); | ||
| 158 | } | ||
| 159 | |||
| 160 | |||
| 161 | $self->{source} = $default_source; | ||
| 162 | $self->{width} = DEFAULT_WIDTH; | ||
| 163 | $self->{dir} = $conf_dir; | ||
| 164 | 1; | ||
| 165 | } | ||
| 166 | |||
| 167 | =head2 $conf_dir = dir() | ||
| 168 | |||
| 169 | Returns the directory path that this config is attached to. | ||
| 170 | |||
| 171 | =cut | ||
| 172 | |||
| 173 | sub dir { | ||
| 174 | my $self = shift; | ||
| 175 | my $d = $self->{dir}; | ||
| 176 | $self->{dir} = shift if @_; | ||
| 177 | $d; | ||
| 178 | } | ||
| 179 | |||
| 180 | =head2 sources() | ||
| 181 | |||
| 182 | @sources = $browser->sources; | ||
| 183 | |||
| 184 | Returns the list of symbolic names for sources. The symbolic names | ||
| 185 | are derived from the configuration file name by: | ||
| 186 | |||
| 187 | 1) stripping off the .conf extension. | ||
| 188 | 2) removing the pattern "^\d+\." | ||
| 189 | |||
| 190 | This means that the configuration file "03.fly.conf" will have the | ||
| 191 | symbolic name "fly". | ||
| 192 | |||
| 193 | =cut | ||
| 194 | |||
| 195 | sub sources { | ||
| 196 | my $self = shift; | ||
| 197 | my $conf = $self->{conf} or return; | ||
| 198 | my @sources = keys %$conf; | ||
| 199 | |||
| 200 | # don't let unauthorized individuals see the source at all | ||
| 201 | my @authorized = grep {exists $conf->{$_}{data} && $conf->{$_}{data}->authorized('general')} @sources; | ||
| 202 | |||
| 203 | # alternative: sort by the config file name | ||
| 204 | # return sort {$conf->{$a}{path} cmp $conf->{$b}{path}} @authorized; | ||
| 205 | |||
| 206 | # alternative: sort by description | ||
| 207 | return sort {lc $self->description($a) cmp lc $self->description($b)} @authorized; | ||
| 208 | |||
| 209 | # alternative: sort by base name | ||
| 210 | # return sort {$a cmp $b} @authorized; | ||
| 211 | } | ||
| 212 | |||
| 213 | =head2 source() | ||
| 214 | |||
| 215 | $source = $browser->source; | ||
| 216 | $source = $browser->source($new_source); | ||
| 217 | |||
| 218 | Sets or gets the current source. The default source will the first | ||
| 219 | one found in the gbrowse.conf directory when sorted alphabetically. | ||
| 220 | |||
| 221 | If you attempt to set an invalid source, the module will issue a | ||
| 222 | warning and will return undef. | ||
| 223 | |||
| 224 | =cut | ||
| 225 | |||
| 226 | # get/set current source | ||
| 227 | sub source { | ||
| 228 | my $self = shift; | ||
| 229 | my $d = $self->{source}; | ||
| 230 | if (@_) { | ||
| 231 | my $source = shift; | ||
| 232 | unless ($self->{conf}{$source}) { | ||
| 233 | carp("invalid source: $source"); | ||
| 234 | return; | ||
| 235 | } | ||
| 236 | unless ($self->{conf}{$source}{data}->authorized('general')) { | ||
| 237 | carp ("Unauthorized source: $source"); | ||
| 238 | return; | ||
| 239 | } | ||
| 240 | $self->{source} = $source; | ||
| 241 | } | ||
| 242 | $d; | ||
| 243 | } | ||
| 244 | |||
| 245 | =head2 setting() | ||
| 246 | |||
| 247 | $value = $browser->setting(general => 'stylesheet'); | ||
| 248 | $value = $browser->setting(gene => 'fgcolor'); | ||
| 249 | $value = $browser->setting('stylesheet'); | ||
| 250 | |||
| 251 | The setting() method returns the value of one of the current source | ||
| 252 | configuration settings. setting() takes two arguments. The first | ||
| 253 | argument is the name of the stanza in which the configuration option | ||
| 254 | is located. The second argument is the name of the setting. Stanza | ||
| 255 | and option names are case sensitive, with the exception of the | ||
| 256 | "general" section, which is automatically folded to lowercase. | ||
| 257 | |||
| 258 | If only one argument is provided, then the "general" stanza is | ||
| 259 | assumed. | ||
| 260 | |||
| 261 | Option values are folded in such a way that newlines and tabs become | ||
| 262 | single spaces. For example, if the "default features" option is defined like this: | ||
| 263 | |||
| 264 | default features = Transcripts | ||
| 265 | Genes | ||
| 266 | Scaffolds | ||
| 267 | |||
| 268 | Then the value retrieved by | ||
| 269 | |||
| 270 | $browser->setting('general'=>'default features'); | ||
| 271 | |||
| 272 | will be the string "Transcripts Genes Scaffolds". Note that it is | ||
| 273 | your responsibility to split this into a list. I suggest that you use | ||
| 274 | Text::Shellwords to split the list in such a way that quotes and | ||
| 275 | escapes are preserved. | ||
| 276 | |||
| 277 | Because of the default, you could also fetch this information without | ||
| 278 | explicitly specifying the stanza. Combined with shellwords gives the | ||
| 279 | idiom: | ||
| 280 | |||
| 281 | @defaults = shellwords($browser->setting('default features')); | ||
| 282 | |||
| 283 | =cut | ||
| 284 | |||
| 285 | sub setting { | ||
| 286 | my $self = shift; | ||
| 287 | my @args = @_; | ||
| 288 | if (@args == 1) { | ||
| 289 | unshift @args,'general'; | ||
| 290 | } else { | ||
| 291 | $args[0] = 'general' | ||
| 292 | if !ref $args[0] | ||
| 293 | && | ||
| 294 | $args[0] ne 'general' | ||
| 295 | && lc($args[0]) eq 'general'; # buglet | ||
| 296 | } | ||
| 297 | my $config = $self->config or return; | ||
| 298 | $config->setting(@args); | ||
| 299 | } | ||
| 300 | |||
| 301 | # to return the list of configuration options | ||
| 302 | sub _setting { | ||
| 303 | shift->config->_setting(@_); | ||
| 304 | } | ||
| 305 | |||
| 306 | =head2 fallback_setting() | ||
| 307 | |||
| 308 | $value = $browser->setting(gene => 'fgcolor'); | ||
| 309 | |||
| 310 | Tries to find the setting for designated label (e.g. "gene") first. If | ||
| 311 | this fails, looks in [TRACK DEFAULTS]. If this fails, looks in [GENERAL]. | ||
| 312 | |||
| 313 | =cut | ||
| 314 | |||
| 315 | sub fallback_setting { | ||
| 316 | my $self = shift; | ||
| 317 | my ($label,$option) = @_; | ||
| 318 | for my $key ($label,'TRACK DEFAULTS','GENERAL') { | ||
| 319 | my $value = $self->setting($key,$option); | ||
| 320 | return $value if defined $value; | ||
| 321 | } | ||
| 322 | return; | ||
| 323 | } | ||
| 324 | |||
| 325 | =head2 plugin_setting() | ||
| 326 | |||
| 327 | $value = = $browser->plugin_setting("option_name"); | ||
| 328 | |||
| 329 | When called in the context of a plugin, returns the setting for the | ||
| 330 | requested option. The option must be placed in a [PluginName:plugin] | ||
| 331 | configuration file section: | ||
| 332 | |||
| 333 | [MyPlugin:plugin] | ||
| 334 | foo = bar | ||
| 335 | |||
| 336 | Now within the MyPlugin.pm plugin, you may call | ||
| 337 | $browser->plugin_setting('foo') to return value "bar". | ||
| 338 | |||
| 339 | =cut | ||
| 340 | |||
| 341 | sub plugin_setting { | ||
| 342 | my $self = shift; | ||
| 343 | my $caller_package = caller(); | ||
| 344 | my ($last_name) = $caller_package =~ /(\w+)$/; | ||
| 345 | my $option_name = "${last_name}:plugin"; | ||
| 346 | $self->setting($option_name => @_); | ||
| 347 | } | ||
| 348 | |||
| 349 | =head2 db_settings() | ||
| 350 | |||
| 351 | @args = $browser->db_settings; | ||
| 352 | |||
| 353 | Returns the appropriate arguments for connecting to Bio::DB::GFF. It | ||
| 354 | can be used this way: | ||
| 355 | |||
| 356 | $db = Bio::DB::GFF->new($browser->dbgff_settings); | ||
| 357 | |||
| 358 | =cut | ||
| 359 | |||
| 360 | # get database adaptor name and arguments | ||
| 361 | sub db_settings { | ||
| 362 | my $self = shift; | ||
| 363 | |||
| 364 | my $adaptor = $self->setting('db_adaptor') || DEFAULT_DB_ADAPTOR; | ||
| 365 | eval "require $adaptor; 1" or die $@; | ||
| 366 | |||
| 367 | my $args = $self->config->setting(general => 'db_args'); | ||
| 368 | my @argv = ref $args eq 'CODE' | ||
| 369 | ? $args->() | ||
| 370 | : Legacy::Graphics::Browser::Util::shellwords($args||''); | ||
| 371 | |||
| 372 | # for compatibility with older versions of the browser, we'll hard-code some arguments | ||
| 373 | if (my $adaptor = $self->setting('adaptor')) { | ||
| 374 | push @argv,(-adaptor => $adaptor); | ||
| 375 | } | ||
| 376 | |||
| 377 | if (my $dsn = $self->setting('database')) { | ||
| 378 | push @argv,(-dsn => $dsn); | ||
| 379 | } | ||
| 380 | |||
| 381 | if (my $fasta = $self->setting('fasta_files')) { | ||
| 382 | push @argv,(-fasta=>$fasta); | ||
| 383 | } | ||
| 384 | |||
| 385 | if (my $user = $self->setting('user')) { | ||
| 386 | push @argv,(-user=>$user); | ||
| 387 | } | ||
| 388 | |||
| 389 | if (my $pass = $self->setting('pass')) { | ||
| 390 | push @argv,(-pass=>$pass); | ||
| 391 | } | ||
| 392 | |||
| 393 | if (defined (my $a = $self->setting('aggregators'))) { | ||
| 394 | my @aggregators = Legacy::Graphics::Browser::Util::shellwords($a||''); | ||
| 395 | push @argv,(-aggregator => \@aggregators); | ||
| 396 | } | ||
| 397 | |||
| 398 | ($adaptor,@argv); | ||
| 399 | } | ||
| 400 | |||
| 401 | =head2 gbrowse_root() | ||
| 402 | |||
| 403 | $root = $browser->gbrowse_root() | ||
| 404 | |||
| 405 | Return the setting of "gbrowse root" | ||
| 406 | |||
| 407 | =cut | ||
| 408 | |||
| 409 | sub gbrowse_root { | ||
| 410 | my $self = shift; | ||
| 411 | my $root = $self->globals->url_base || '/gbrowse2'; | ||
| 412 | $root = "/$root" unless $root =~ /^\//; | ||
| 413 | $root; | ||
| 414 | } | ||
| 415 | |||
| 416 | =head2 relative_path() | ||
| 417 | |||
| 418 | $relative_path = $browser->relative_path('gbrowse.css'); | ||
| 419 | |||
| 420 | Add the setting of "gbrowse root" to the indicated path, if | ||
| 421 | relative. Otherwise pass through unchanged. | ||
| 422 | |||
| 423 | =cut | ||
| 424 | |||
| 425 | sub relative_path { | ||
| 426 | my $self = shift; | ||
| 427 | my $path = shift; | ||
| 428 | return $path if $path =~ /^\//; # already absolute | ||
| 429 | return $path if $path =~ /^(?:http|ftp)+:/; # a URL | ||
| 430 | my $root = $self->gbrowse_root; | ||
| 431 | return "$root/$path"; | ||
| 432 | } | ||
| 433 | |||
| 434 | =head2 relative_path_setting() | ||
| 435 | |||
| 436 | $relative_path = $browser->relative_path_setting('stylesheet'); | ||
| 437 | |||
| 438 | Like relative_path(), but works on a named setting rather than an | ||
| 439 | actual path or directory. | ||
| 440 | |||
| 441 | =cut | ||
| 442 | |||
| 443 | sub relative_path_setting { | ||
| 444 | my $self = shift; | ||
| 445 | my $setting = shift; | ||
| 446 | my $path = $self->setting('general' => $setting); | ||
| 447 | return unless $path; | ||
| 448 | return $self->relative_path($path); | ||
| 449 | } | ||
| 450 | |||
| 451 | =head2 version() | ||
| 452 | |||
| 453 | $version = $browser->version | ||
| 454 | |||
| 455 | This is a shortcut method that returns the value of the "version" | ||
| 456 | option in the general section. The value returned is the version | ||
| 457 | of the data source. | ||
| 458 | |||
| 459 | =cut | ||
| 460 | |||
| 461 | sub version { | ||
| 462 | my $self = shift; | ||
| 463 | my $source = shift; | ||
| 464 | my $c = $self->{conf}{$source}{data} or return; | ||
| 465 | return $c->setting('general','version'); | ||
| 466 | } | ||
| 467 | |||
| 468 | =head2 description() | ||
| 469 | |||
| 470 | $description = $browser->description | ||
| 471 | |||
| 472 | This is a shortcut method that returns the value of the "description" | ||
| 473 | option in the general section. The value returned is a human-readable | ||
| 474 | description of the data source. | ||
| 475 | |||
| 476 | =cut | ||
| 477 | |||
| 478 | sub description { | ||
| 479 | my $self = shift; | ||
| 480 | my $source = shift; | ||
| 481 | my $c = $self->{conf}{$source}{data} or return; | ||
| 482 | return $c->setting('general','description'); | ||
| 483 | } | ||
| 484 | |||
| 485 | =head2 $time = $browser->remember_settings_time | ||
| 486 | |||
| 487 | Return the relative time (in CGI "expires" format) to maintain | ||
| 488 | information about the current page settings, including plugin | ||
| 489 | configuration. | ||
| 490 | |||
| 491 | =cut | ||
| 492 | |||
| 493 | sub remember_settings_time { | ||
| 494 | my $self = shift; | ||
| 495 | return $self->setting('remember settings time') || REMEMBER_SETTINGS_TIME; | ||
| 496 | } | ||
| 497 | |||
| 498 | |||
| 499 | =head2 $time = $browser->remember_source_time | ||
| 500 | |||
| 501 | Return the relative time (in CGI "expires" format) to maintain information | ||
| 502 | on which source the user is viewing. | ||
| 503 | |||
| 504 | =cut | ||
| 505 | |||
| 506 | sub remember_source_time { | ||
| 507 | my $self = shift; | ||
| 508 | return $self->setting('remember cookie time') || $self->setting('remember source time') || REMEMBER_SOURCE_TIME; | ||
| 509 | } | ||
| 510 | |||
| 511 | =head2 $language = $browser->language([$new_language]) | ||
| 512 | |||
| 513 | Get/set an associated Legacy::Graphics::Browser::I18n language translation object. | ||
| 514 | |||
| 515 | =cut | ||
| 516 | |||
| 517 | sub language { | ||
| 518 | my $self = shift; | ||
| 519 | my $d = $self->{language}; | ||
| 520 | $self->{language} = shift if @_; | ||
| 521 | $d; | ||
| 522 | } | ||
| 523 | |||
| 524 | |||
| 525 | =head2 $french = $browser->tr($english) | ||
| 526 | |||
| 527 | Translate message into currently-set language, with fallback to POSIX, | ||
| 528 | via associated Legacy::Graphics::Browser::I18n language translation object. | ||
| 529 | |||
| 530 | =cut | ||
| 531 | |||
| 532 | sub tr { | ||
| 533 | my $self = shift; | ||
| 534 | my $lang = $self->language or return @_; | ||
| 535 | $lang->tr(@_); | ||
| 536 | } | ||
| 537 | |||
| 538 | =head2 $section_setting = $browser->section_setting($section_name) | ||
| 539 | |||
| 540 | Returns "open" "closed" or "off" for the named section. Named sections are: | ||
| 541 | |||
| 542 | instructions | ||
| 543 | search | ||
| 544 | overview | ||
| 545 | details | ||
| 546 | tracks | ||
| 547 | display | ||
| 548 | add tracks | ||
| 549 | |||
| 550 | =cut | ||
| 551 | |||
| 552 | sub section_setting { | ||
| 553 | my $self = shift; | ||
| 554 | my $section = shift; | ||
| 555 | my $config_setting = "\L$section\E section"; | ||
| 556 | my $s = $self->setting($config_setting); | ||
| 557 | return 'open' unless defined $s; | ||
| 558 | return $s; | ||
| 559 | } | ||
| 560 | |||
| 561 | =head2 labels() | ||
| 562 | |||
| 563 | @track_labels = $browser->labels | ||
| 564 | |||
| 565 | This method returns the names of each of the track stanzas, | ||
| 566 | hereinafter called "track labels" or simply "labels". These labels | ||
| 567 | can be used in subsequent calls as the first argument to setting() in | ||
| 568 | order to retrieve track-specific options. | ||
| 569 | |||
| 570 | =cut | ||
| 571 | |||
| 572 | sub labels { | ||
| 573 | my $self = shift; | ||
| 574 | my $order = shift; | ||
| 575 | my @labels = $self->config->labels; | ||
| 576 | if ($order) { # custom order | ||
| 577 | return @labels[@$order]; | ||
| 578 | } else { | ||
| 579 | return @labels; | ||
| 580 | } | ||
| 581 | } | ||
| 582 | |||
| 583 | =head2 default_labels() | ||
| 584 | |||
| 585 | @default_labels = $browser->default_labels | ||
| 586 | |||
| 587 | This method returns the labels for each track that is turned on by | ||
| 588 | default. | ||
| 589 | |||
| 590 | =cut | ||
| 591 | |||
| 592 | sub default_labels { | ||
| 593 | my $self = shift; | ||
| 594 | $self->config->default_labels; | ||
| 595 | } | ||
| 596 | |||
| 597 | =head2 label2type() | ||
| 598 | |||
| 599 | @feature_types = $browser->label2type($label,$lowres); | ||
| 600 | |||
| 601 | Given a track label, this method returns a list of the corresponding | ||
| 602 | sequence feature types in a form that can be passed to Bio::DB::GFF. | ||
| 603 | The optional $lowres flag can be used to tell label2type() to select a | ||
| 604 | set of features that are suitable when viewing large sections of the | ||
| 605 | sequence (it is up to the person who writes the configuration file to | ||
| 606 | specify this). | ||
| 607 | |||
| 608 | =cut | ||
| 609 | |||
| 610 | sub label2type { | ||
| 611 | my $self = shift; | ||
| 612 | $self->config->label2type(@_); | ||
| 613 | } | ||
| 614 | |||
| 615 | =head2 type2label() | ||
| 616 | |||
| 617 | $label = $browser->type2label($type); | ||
| 618 | |||
| 619 | Given a feature type, this method translates it into a track label. | ||
| 620 | |||
| 621 | =cut | ||
| 622 | |||
| 623 | sub type2label { | ||
| 624 | my $self = shift; | ||
| 625 | $self->config->type2label(@_); | ||
| 626 | } | ||
| 627 | |||
| 628 | =head2 feature2label() | ||
| 629 | |||
| 630 | $label = $browser->feature2label($feature [,$length]); | ||
| 631 | |||
| 632 | Given a Bio::DB::GFF::Feature (or anything that implements a type() | ||
| 633 | method), this method returns the corresponding label. If an optional | ||
| 634 | length is provided, the method takes semantic zooming into account. | ||
| 635 | |||
| 636 | =cut | ||
| 637 | |||
| 638 | sub feature2label { | ||
| 639 | my $self = shift; | ||
| 640 | my ($feature,$length) = @_; | ||
| 641 | return $self->config->feature2label($feature,$length); | ||
| 642 | } | ||
| 643 | |||
| 644 | =head2 citation() | ||
| 645 | |||
| 646 | $citation = $browser->citation($label) | ||
| 647 | |||
| 648 | This is a shortcut method that returns the citation for a given track | ||
| 649 | label. It simply calls $browser->setting($label=>'citation'); | ||
| 650 | |||
| 651 | =cut | ||
| 652 | |||
| 653 | sub citation { | ||
| 654 | my $self = shift; | ||
| 655 | my $label = shift; | ||
| 656 | my $language = shift; | ||
| 657 | my $config = $self->config; | ||
| 658 | my $c; | ||
| 659 | if ($language) { | ||
| 660 | for my $l ($language->language) { | ||
| 661 | $c ||= $config->setting($label=>"citation:$l"); | ||
| 662 | $c = &$c if ref $c eq 'CODE'; | ||
| 663 | } | ||
| 664 | } | ||
| 665 | $c ||= $config->setting($label=>'citation'); | ||
| 666 | $c = &$c if ref $c eq 'CODE'; | ||
| 667 | $c; | ||
| 668 | } | ||
| 669 | |||
| 670 | =head2 width() | ||
| 671 | |||
| 672 | $width = $browser->width | ||
| 673 | |||
| 674 | This is a shortcut method that returns the width of the display in | ||
| 675 | pixels. | ||
| 676 | |||
| 677 | =cut | ||
| 678 | |||
| 679 | sub width { | ||
| 680 | my $self = shift; | ||
| 681 | my $d = $self->{width}; | ||
| 682 | $self->{width} = shift if @_; | ||
| 683 | $d; | ||
| 684 | } | ||
| 685 | |||
| 686 | =head2 header() | ||
| 687 | |||
| 688 | $header = $browser->header; | ||
| 689 | |||
| 690 | This is a shortcut method that returns the header HTML for the gbrowse | ||
| 691 | page. | ||
| 692 | |||
| 693 | =cut | ||
| 694 | |||
| 695 | sub header { | ||
| 696 | my $self = shift; | ||
| 697 | my $header = $self->config->setting(general => 'header'); | ||
| 698 | if (ref $header eq 'CODE') { | ||
| 699 | my $h = eval{$header->(@_)}; | ||
| 700 | $self->_callback_complain(general=>'header') if @_; | ||
| 701 | return $h; | ||
| 702 | } | ||
| 703 | return $header; | ||
| 704 | } | ||
| 705 | |||
| 706 | =head2 footer() | ||
| 707 | |||
| 708 | $footer = $browser->footer; | ||
| 709 | |||
| 710 | This is a shortcut method that returns the footer HTML for the gbrowse | ||
| 711 | page. | ||
| 712 | |||
| 713 | =cut | ||
| 714 | |||
| 715 | sub footer { | ||
| 716 | my $self = shift; | ||
| 717 | my $footer = $self->config->setting(general => 'footer'); | ||
| 718 | if (ref $footer eq 'CODE') { | ||
| 719 | my $f = eval {$footer->(@_)}; | ||
| 720 | $self->_callback_complain(general=>'footer') if @_; | ||
| 721 | return $f; | ||
| 722 | } | ||
| 723 | return $footer; | ||
| 724 | } | ||
| 725 | |||
| 726 | =head2 config() | ||
| 727 | |||
| 728 | $config = $browser->config; | ||
| 729 | |||
| 730 | This method returns a Bio::Graphics::FeatureFile object corresponding | ||
| 731 | to the current source. | ||
| 732 | |||
| 733 | =cut | ||
| 734 | |||
| 735 | sub config { | ||
| 736 | my $self = shift; | ||
| 737 | my $source = $self->source or return; | ||
| 738 | $self->{conf}{$source}{data}; | ||
| 739 | } | ||
| 740 | |||
| 741 | =head2 mtime() | ||
| 742 | |||
| 743 | $time = $browser->mtime() | ||
| 744 | |||
| 745 | This method returns the modification time of the config file for the | ||
| 746 | current source. | ||
| 747 | |||
| 748 | =cut | ||
| 749 | |||
| 750 | sub mtime { | ||
| 751 | my $self = shift; | ||
| 752 | my $source = $self->source; | ||
| 753 | $self->{conf}{$source}{mtime}; | ||
| 754 | } | ||
| 755 | |||
| 756 | =head2 path() | ||
| 757 | |||
| 758 | $path = $browser->path() | ||
| 759 | |||
| 760 | This method returns the file path of the config file for the | ||
| 761 | current source. | ||
| 762 | |||
| 763 | =cut | ||
| 764 | |||
| 765 | sub path { | ||
| 766 | my $self = shift; | ||
| 767 | my $source = $self->source; | ||
| 768 | $self->{conf}{$source}{path}; | ||
| 769 | } | ||
| 770 | |||
| 771 | sub default_label_indexes { | ||
| 772 | my $self = shift; | ||
| 773 | $self->config->default_label_indexes; | ||
| 774 | } | ||
| 775 | |||
| 776 | =head2 make_link() | ||
| 777 | |||
| 778 | $url = $browser->make_link($feature,$panel,$label) | ||
| 779 | |||
| 780 | Given a Legacy::SeqFeatureI object, turn it into a URL suitable for use | ||
| 781 | in a hypertext link. For convenience, the Legacy::Graphics panel is also | ||
| 782 | provided. If $label is provided, then its link overrides the type of | ||
| 783 | the feature. | ||
| 784 | |||
| 785 | =cut | ||
| 786 | |||
| 787 | sub make_link { | ||
| 788 | my $self = shift; | ||
| 789 | my ($feature,$panel,$label,$src) = @_; | ||
| 790 | my @results = $self->config->make_link($feature,$panel,$label,$self->source); | ||
| 791 | return wantarray ? @results : $results[0]; | ||
| 792 | } | ||
| 793 | |||
| 794 | =head2 render_panels() | ||
| 795 | |||
| 796 | $panels = $browser->render_panels(%args); | ||
| 797 | |||
| 798 | Render an image and an image map according to the options in %args. | ||
| 799 | In a | ||
| 800 | Returns a two-element list. The first element is a URL that refers to | ||
| 801 | the image which can be used as the SRC for an <IMG> tag. The second | ||
| 802 | is a complete image map, including the <MAP> and </MAP> sections. | ||
| 803 | |||
| 804 | The arguments are a series of tag=>value pairs, where tags are: | ||
| 805 | |||
| 806 | Argument Value | ||
| 807 | |||
| 808 | segment A Bio::DB::GFF::Segment or | ||
| 809 | Legacy::Das::SegmentI object (required). | ||
| 810 | |||
| 811 | tracks An arrayref containing a series of track | ||
| 812 | labels to render (required). The order of the labels | ||
| 813 | determines the order of the tracks. | ||
| 814 | |||
| 815 | options A hashref containing options to apply to | ||
| 816 | each track (optional). Keys are the track labels | ||
| 817 | and the values are 0=auto, 1=force no bump, | ||
| 818 | 2=force bump, 3=force label, 4=expanded bump. | ||
| 819 | |||
| 820 | feature_files A hashref containing a series of | ||
| 821 | Bio::Graphics::FeatureFile objects to be | ||
| 822 | rendered onto the display (optional). The keys | ||
| 823 | are labels assigned to the 3d party | ||
| 824 | features. These labels must appear in the | ||
| 825 | tracks arrayref in order for render_panels() to | ||
| 826 | determine the order in which to render them. | ||
| 827 | |||
| 828 | do_map This argument is a flag that controls whether or not | ||
| 829 | to generate the image map. It defaults to false. | ||
| 830 | |||
| 831 | do_centering_map This argument is a flag that controls whether or not | ||
| 832 | to add elements to the image map so that the user can | ||
| 833 | center the image by clicking on the scale. It defaults | ||
| 834 | to false, and has no effect unless do_map is also true. | ||
| 835 | |||
| 836 | title Add specified title to the top of the image. | ||
| 837 | |||
| 838 | noscale Suppress the scale | ||
| 839 | |||
| 840 | flip Flip coordinates left to right | ||
| 841 | |||
| 842 | hilite_callback Callback for performing hilighting | ||
| 843 | |||
| 844 | image_and_map This argument will cause render_panels to emulate | ||
| 845 | the legacy method image_and_map() and return a | ||
| 846 | GD::Image object and a 'boxes' array reference rather | ||
| 847 | than rendered html. This argument applies only to composite | ||
| 848 | (non-draggable) panel images. | ||
| 849 | |||
| 850 | Any arguments names that begin with an initial - (hyphen) are passed | ||
| 851 | through to Bio::Graphics::Panel->new() directly | ||
| 852 | |||
| 853 | Any arguments names that begin with an initial - (hyphen) are passed | ||
| 854 | through to Bio::Graphics::Panel->new() directly | ||
| 855 | |||
| 856 | =cut | ||
| 857 | |||
| 858 | sub render_panels { | ||
| 859 | my $self = shift; | ||
| 860 | my $args = shift; | ||
| 861 | |||
| 862 | my $segment = $args->{segment}; | ||
| 863 | my $do_map = $args->{do_map}; | ||
| 864 | my $drag_n_drop = $self->drag_and_drop($args->{drag_n_drop}); | ||
| 865 | |||
| 866 | return unless $segment; | ||
| 867 | |||
| 868 | $self->_load_aggregator_types($segment) if $do_map; | ||
| 869 | my $panels = $self->generate_panels($args); | ||
| 870 | |||
| 871 | return $drag_n_drop ? $self->render_draggable_tracks($args,$panels) | ||
| 872 | : $self->render_composite_track($args,$panels->{'__all__'}); | ||
| 873 | } | ||
| 874 | |||
| 875 | =head2 drag_and_drop() | ||
| 876 | |||
| 877 | Return true if drag_and_drop tracks should be enabled on this | ||
| 878 | datasource. Looks at the "drag and drop" option and also consults a | ||
| 879 | series of user agents known to support drag_and_drop. | ||
| 880 | |||
| 881 | =cut | ||
| 882 | |||
| 883 | sub drag_and_drop { | ||
| 884 | my $self = shift; | ||
| 885 | my $override = shift; | ||
| 886 | return if defined $override && !$override; | ||
| 887 | my $dnd = $self->setting(general => 'drag and drop'); # explicit drag-and-drop setting | ||
| 888 | $dnd = 1 unless defined $dnd; | ||
| 889 | my $pg = $self->setting(general => 'postgrid'); # postgrid forces drag and drop off | ||
| 890 | return $dnd && !$pg; | ||
| 891 | } | ||
| 892 | |||
| 893 | sub cache_time { | ||
| 894 | my $self = shift; | ||
| 895 | my $override = shift; | ||
| 896 | return $override if defined $override; | ||
| 897 | my $ct = $self->setting(general => 'cache time'); | ||
| 898 | return $ct if defined $ct; # may return zero | ||
| 899 | return 1; # 1 hour default | ||
| 900 | } | ||
| 901 | |||
| 902 | sub render_draggable_tracks { | ||
| 903 | my $self = shift; | ||
| 904 | my ($args,$panels) = @_; | ||
| 905 | |||
| 906 | my $images = $self->relative_path_setting('buttons'); | ||
| 907 | my $do_map = $args->{do_map}; | ||
| 908 | my $tmpdir = $args->{tmpdir}; | ||
| 909 | my $settings = $args->{settings}; | ||
| 910 | my $do_drag = $args->{do_drag}; | ||
| 911 | my $button = $args->{image_button}; | ||
| 912 | my $section = $args->{section}; | ||
| 913 | $section =~ s/^\?//; | ||
| 914 | |||
| 915 | my $plus = "$images/plus.png"; | ||
| 916 | my $minus = "$images/minus.png"; | ||
| 917 | my $share = "$images/share.png"; | ||
| 918 | my $help = "$images/query.png"; | ||
| 919 | |||
| 920 | # get the pad image, which we use to fill up space between collapsed tracks | ||
| 921 | my $pad_url = $panels->{__pad__}{image}; | ||
| 922 | my ($pw,$ph) = @{$panels->{__pad__}}{'width','height'}; | ||
| 923 | |||
| 924 | |||
| 925 | my @result; | ||
| 926 | for my $label ('__scale__',@{$args->{labels}}) { | ||
| 927 | |||
| 928 | next unless $panels->{$label}; | ||
| 929 | my ($url,$img_map,$width,$height) = @{$panels->{$label}}{qw(image map width height)}; | ||
| 930 | |||
| 931 | # this complication is due to the fact that a plugin or uploaded file can be | ||
| 932 | # in several sections at the same time | ||
| 933 | my $element_id = $label =~ /^(file|plugin):/ ? "${section}_${label}" : $label; | ||
| 934 | |||
| 935 | my $collapsed = $settings->{track_collapsed}{$element_id}; | ||
| 936 | my $img_style = $collapsed ? "display:none" : "display:inline"; | ||
| 937 | |||
| 938 | # The javascript functions for rubber-band selection | ||
| 939 | # need this ID as a hook, please do not change it | ||
| 940 | my $id = $label eq '__scale__' | ||
| 941 | ? "${section}_image" | ||
| 942 | : "${element_id}_image"; | ||
| 943 | |||
| 944 | $img_style .= '; cursor:pointer' if $label eq '__scale__'; | ||
| 945 | |||
| 946 | my @map = $button | ||
| 947 | ? () | ||
| 948 | : (-usemap=>"#${element_id}_map"); | ||
| 949 | my $img = img({-src=>$url, | ||
| 950 | -width => $width, | ||
| 951 | -id => "$id", | ||
| 952 | -height=> $height, | ||
| 953 | -border=> 0, | ||
| 954 | -name => "${section}_${label}", | ||
| 955 | -alt => "${label} $section", | ||
| 956 | -style => $img_style, | ||
| 957 | @map, | ||
| 958 | }); | ||
| 959 | |||
| 960 | my $class = $label eq '__scale__' ? 'scale' : 'track'; | ||
| 961 | my $icon = $collapsed ? $plus : $minus; | ||
| 962 | |||
| 963 | my $config_click; | ||
| 964 | if ($label =~ /^plugin:/) { | ||
| 965 | my $help_url = "url:?plugin=".CGI::escape($label).';plugin_do=Configure'; | ||
| 966 | $config_click = "balloon.showTooltip(event,'$help_url',1)"; | ||
| 967 | } | ||
| 968 | |||
| 969 | elsif ($label =~ /^file:/) { | ||
| 970 | my $url = "?modify.${label}=".$self->tr('Edit'); | ||
| 971 | $config_click = "window.location='$url'"; | ||
| 972 | } | ||
| 973 | |||
| 974 | else { | ||
| 975 | my $help_url = "url:?configure_track=".CGI::escape($label); | ||
| 976 | $help_url .= ";rand=".rand(); # work around caching bugs... # if CGI->user_agent =~ /MSIE/; | ||
| 977 | $config_click = "balloon.showTooltip(event,'$help_url',1)"; | ||
| 978 | } | ||
| 979 | |||
| 980 | my $title; | ||
| 981 | if ($label =~ /\w+:(.+)/ && $label !~ /:overview|:region/) { | ||
| 982 | $title = $label =~ /^http|^ftp/ ? $self->url_label($label) : $1; | ||
| 983 | } | ||
| 984 | else { | ||
| 985 | $title = $self->config->setting($label=>'key') || $label; | ||
| 986 | } | ||
| 987 | |||
| 988 | if ($self->setting(general=>'show track categories')) { | ||
| 989 | my $cat = $self->config->setting($label=>'category'); | ||
| 990 | $title .= " ($cat)" if $cat; | ||
| 991 | } | ||
| 992 | my $show_or_hide = $self->tr('SHOW_OR_HIDE_TRACK'); | ||
| 993 | my $share_this_track = $self->tr('SHARE_THIS_TRACK'); | ||
| 994 | my $citation = $self->plain_citation($label,512); | ||
| 995 | #$citation =~ s/"/"/g; | ||
| 996 | #$citation =~ s/'/'/g; | ||
| 997 | |||
| 998 | my $configure_this_track = $citation || ''; | ||
| 999 | $configure_this_track .= '<br>' if $citation; | ||
| 1000 | $configure_this_track .= $self->tr('CONFIGURE_THIS_TRACK'); | ||
| 1001 | my $escaped_label = CGI::escape($label); | ||
| 1002 | |||
| 1003 | my $titlebar = $label eq '__scale__' || $label eq '__all__' | ||
| 1004 | ? '' | ||
| 1005 | : span({-class=>$collapsed ? 'titlebar_inactive' : 'titlebar',-id=>"${element_id}_title"}, | ||
| 1006 | img({-src =>$icon, | ||
| 1007 | -id => "${element_id}_icon", | ||
| 1008 | -onMouseOver => "balloon.showTooltip(event,'$show_or_hide')", | ||
| 1009 | -onClick => "collapse('$element_id')", | ||
| 1010 | -style => 'cursor:pointer', | ||
| 1011 | }), | ||
| 1012 | img({-src => $share, | ||
| 1013 | -style => 'cursor:pointer', | ||
| 1014 | -onMouseOver => "balloon.showTooltip(event,'$share_this_track')", | ||
| 1015 | -onMousedown => "balloon.showTooltip(event,'url:?share_track=$escaped_label')", | ||
| 1016 | }), | ||
| 1017 | $label !~ /^(http|ftp|das):/ | ||
| 1018 | ? img({-src => $help, | ||
| 1019 | -style => 'cursor:pointer', | ||
| 1020 | -onMouseOver => "balloon.showTooltip(event,'$configure_this_track')", | ||
| 1021 | -onmousedown => $config_click | ||
| 1022 | }) | ||
| 1023 | : (), | ||
| 1024 | span({-class=>'draghandle'},$title) | ||
| 1025 | ); | ||
| 1026 | |||
| 1027 | my $pad_img = img({-src => $pad_url, | ||
| 1028 | -width => $pw, | ||
| 1029 | -height=> $ph, | ||
| 1030 | -border=> 0, | ||
| 1031 | -id => "${element_id}_pad", | ||
| 1032 | -style => $collapsed ? "display:inline" : "display:none", | ||
| 1033 | }); | ||
| 1034 | |||
| 1035 | (my $munge_label = $label) =~ s/_/%5F/g; # freakin' scriptaculous uses _ as a delimiter!!! | ||
| 1036 | $img_map = qq(<map name="${element_id}_map" id="${element_id}_map">$img_map</map>\n) if $img_map; | ||
| 1037 | |||
| 1038 | push @result, (is_safari() | ||
| 1039 | ? | ||
| 1040 | "\n".div({-id=>"${section}_track_${munge_label}",-class=>$class}, | ||
| 1041 | $titlebar, | ||
| 1042 | div({-align=>'center', | ||
| 1043 | -style=>'margin-top: -18px; margin-bottom: 3px'}, | ||
| 1044 | $img.$pad_img), | ||
| 1045 | $img_map||'') | ||
| 1046 | : | ||
| 1047 | "\n".div({-id=>"${section}_track_${munge_label}",-class=>$class}, | ||
| 1048 | div({-align=>'center'},$titlebar.$img.$pad_img), | ||
| 1049 | $img_map||'') | ||
| 1050 | ); | ||
| 1051 | |||
| 1052 | } | ||
| 1053 | |||
| 1054 | return wantarray ? @result : join '',@result; | ||
| 1055 | } | ||
| 1056 | |||
| 1057 | sub render_composite_track { | ||
| 1058 | my $self = shift; | ||
| 1059 | my ($args,$panel) = @_; | ||
| 1060 | |||
| 1061 | my $section = $args->{section} || '?detail'; | ||
| 1062 | $section =~ s/^\?//; | ||
| 1063 | my $button = $args->{image_button}; | ||
| 1064 | |||
| 1065 | my ($width,$height,$url,$map,$gd,$boxes) = @{$panel}{qw/width height image map gd boxes/}; | ||
| 1066 | |||
| 1067 | # doesn't work | ||
| 1068 | my $css_map = $self->map_css($boxes,$section) if $section eq 'detail'; | ||
| 1069 | |||
| 1070 | if ($args->{image_and_map}) { | ||
| 1071 | return $gd, $boxes; | ||
| 1072 | } | ||
| 1073 | |||
| 1074 | $map ||= ''; | ||
| 1075 | my $map_name = param('hmap') || "${section}_map"; | ||
| 1076 | |||
| 1077 | # The javascript functions for rubber-band selection | ||
| 1078 | # need this ID as a hook, please DO NOT CHANGE THE IMAGE ID! | ||
| 1079 | my $id = $section eq 'detail' ? 'composite_track' : "${section}_image"; | ||
| 1080 | $id = 'Overview Scale_image' if $id =~ /overview/i; | ||
| 1081 | |||
| 1082 | my $img = $button | ||
| 1083 | ? image_button(-src => $url, | ||
| 1084 | -name => $section, | ||
| 1085 | -id => $id | ||
| 1086 | ) | ||
| 1087 | : img({-src=>$url, | ||
| 1088 | -usemap=>'#'.$map_name, | ||
| 1089 | -width => $width, | ||
| 1090 | -id => $id, | ||
| 1091 | -height=> $height, | ||
| 1092 | -border=> 0, | ||
| 1093 | -name => $section, | ||
| 1094 | -alt => $section, | ||
| 1095 | -style => 'position:relative'}); | ||
| 1096 | |||
| 1097 | |||
| 1098 | my $html = div({-align=>'center'}, | ||
| 1099 | $img, | ||
| 1100 | $css_map, | ||
| 1101 | qq(<map name="$map_name">$map</map>) | ||
| 1102 | ); | ||
| 1103 | |||
| 1104 | return $html; | ||
| 1105 | } | ||
| 1106 | |||
| 1107 | =head2 generate_panels() | ||
| 1108 | |||
| 1109 | Generate the GD object and the imagemap and returns a hashref in the format | ||
| 1110 | |||
| 1111 | $results->{track_label} = {image=>$uri, map=>$map_data, width=>$w, height=>$h, file=>$img_path) | ||
| 1112 | |||
| 1113 | If the "drag_n_drop" argument is false, then returns a single track named "__all__". | ||
| 1114 | |||
| 1115 | Arguments: a key=>value list | ||
| 1116 | 'section' Section type to draw; one of "overview", "region" or "detail" | ||
| 1117 | 'segment' A feature iterator that responds to next_seq() methods | ||
| 1118 | 'feature_files' A hash of Bio::Graphics::FeatureFile objects containing 3d party features | ||
| 1119 | 'options' An hashref of options, where 0=auto, 1=force no bump, 2=force bump, 3=force label | ||
| 1120 | 4=force fast bump, 5=force fast bump and label | ||
| 1121 | 'drag_n_drop' Force drag-and-drop behavior on or off | ||
| 1122 | 'limit' Place a limit on the number of features of each type to show. | ||
| 1123 | 'labels' List of named tracks, in the order in which they are to be shown | ||
| 1124 | 'tracks' List of named tracks, in the order in which they are to be shown (deprecated) | ||
| 1125 | 'label_scale' If true, prints chromosome name next to scale | ||
| 1126 | 'title' A title for the image | ||
| 1127 | 'noscale' Suppress scale entirely | ||
| 1128 | 'image_class' Optional image class for generating SVG output (by passing GD::SVG) | ||
| 1129 | 'cache_extra' Extra cache args needed to make this image unique | ||
| 1130 | 'scale_map_type' If equal to "centering_map" adds an imagemap to the ruler that recenters. | ||
| 1131 | If equal to "interval_map" creates an imagemap that jumps to a small interval in map | ||
| 1132 | 'featurefile_select' callback for selecting features to be rendered from a featurefile onto a panel | ||
| 1133 | any arguments that begin with an initial - (hyphen) are passed through to Panel->new | ||
| 1134 | directly | ||
| 1135 | |||
| 1136 | =cut | ||
| 1137 | |||
| 1138 | sub generate_panels { | ||
| 1139 | my $self = shift; | ||
| 1140 | my $args = shift; | ||
| 1141 | |||
| 1142 | my $segment = $args->{segment}; | ||
| 1143 | my ($seg_start,$seg_stop,$flip) = $self->segment_coordinates($segment, | ||
| 1144 | $args->{flip}); | ||
| 1145 | my $feature_files = $args->{feature_files} || {}; | ||
| 1146 | my $labels = $args->{labels} || $args->{tracks} || []; # legacy | ||
| 1147 | my $options = $args->{options} || {}; | ||
| 1148 | |||
| 1149 | my $limits = $args->{limit} || {}; | ||
| 1150 | my $lang = $args->{lang} || $self->language; | ||
| 1151 | my $suppress_scale= $args->{noscale}; | ||
| 1152 | my $hilite_callback = $args->{hilite_callback}; | ||
| 1153 | my $drag_n_drop = $self->drag_and_drop($args->{drag_n_drop}); | ||
| 1154 | my $do_map = $args->{do_map}; | ||
| 1155 | my $cache_extra = $args->{cache_extra} || []; | ||
| 1156 | my $cache = $args->{cache}; | ||
| 1157 | my $settings = $args->{settings}; | ||
| 1158 | my $section = $args->{section} || '?detail'; | ||
| 1159 | |||
| 1160 | # hack to turn caching off in a one-shot fashion... | ||
| 1161 | $cache = 0 if param('redisplay'); | ||
| 1162 | |||
| 1163 | my @panel_args = $self->create_panel_args($section,$args); | ||
| 1164 | |||
| 1165 | $segment->factory->debug(1) if DEBUG; | ||
| 1166 | #$self->error(''); | ||
| 1167 | |||
| 1168 | my $conf = $self->config; | ||
| 1169 | my $length = $segment->length; | ||
| 1170 | |||
| 1171 | #--------------------------------------------------------------------------------- | ||
| 1172 | # Track and panel creation | ||
| 1173 | |||
| 1174 | # we create two hashes: | ||
| 1175 | # the %panels hash maps label names to panels | ||
| 1176 | # the %tracks hash maps label names to tracks within the panels | ||
| 1177 | # in the case of no drag_n_drop, then %panels will contain a single key named "__scale__" | ||
| 1178 | my %panels; # map label names to Bio::Graphics::Panel objects | ||
| 1179 | my %tracks; # map label names to Legacy::Graphics::Track objects | ||
| 1180 | my %track_args; # map label names to track-specificic arguments (for caching) | ||
| 1181 | my %seenit; # used to avoid possible upstream error of putting track on list multiple times | ||
| 1182 | my %results; # hash of {$label}{gd} and {$label}{map} | ||
| 1183 | my %cached; # list of labels that have cached data on disk | ||
| 1184 | my %cache_key; # list that maps labels to cache keys | ||
| 1185 | |||
| 1186 | my $panel_key = $drag_n_drop ? '__scale__' : '__all__'; | ||
| 1187 | |||
| 1188 | # When running in monolithic mode, we need to be very careful about the cache key. This key | ||
| 1189 | # is the combination of the panel type, the panel args, and all the individual track args! | ||
| 1190 | my @cache_args = ($section,$panel_key,@panel_args, | ||
| 1191 | @$cache_extra,$do_map); | ||
| 1192 | if ($panel_key eq '__all__') { | ||
| 1193 | $track_args{$_} ||= [$self->create_track_args($_,$args)] foreach @$labels; | ||
| 1194 | push @cache_args,map {@$_} values %track_args; | ||
| 1195 | } | ||
| 1196 | |||
| 1197 | $cache_key{$panel_key} = $self->create_cache_key(@cache_args); | ||
| 1198 | $cached{$panel_key} = $cache && $self->panel_is_cached($cache_key{$panel_key}); | ||
| 1199 | |||
| 1200 | unless ($cached{$panel_key}) { | ||
| 1201 | $panels{$panel_key} = Bio::Graphics::Panel->new(@panel_args); | ||
| 1202 | |||
| 1203 | $panels{$panel_key}->add_track($segment => 'arrow', | ||
| 1204 | -double => 1, | ||
| 1205 | -tick => 2, | ||
| 1206 | -label => $args->{label_scale} ? $segment->seq_id : 0, | ||
| 1207 | -units => $conf->setting(general=>'units') || '', | ||
| 1208 | -unit_divider => $conf->setting(general=>'unit_divider') || 1, | ||
| 1209 | ) unless $suppress_scale; | ||
| 1210 | } | ||
| 1211 | |||
| 1212 | # create another special track for padding to be used when we "collapse" a track, but only | ||
| 1213 | # if $drag_n_drop is false. | ||
| 1214 | if ($drag_n_drop) { | ||
| 1215 | $panel_key = '__pad__'; | ||
| 1216 | my @cache_args = ($section,$panel_key,@panel_args, | ||
| 1217 | @$cache_extra,$drag_n_drop); | ||
| 1218 | $cache_key{$panel_key} = $self->create_cache_key(@cache_args); | ||
| 1219 | unless ($cached{$panel_key} = | ||
| 1220 | $cache && $self->panel_is_cached($cache_key{$panel_key}) | ||
| 1221 | ) { | ||
| 1222 | $panels{$panel_key} = Bio::Graphics::Panel->new(@panel_args); | ||
| 1223 | } | ||
| 1224 | } | ||
| 1225 | |||
| 1226 | # this will keep track of numbering of the tracks; only used when inserting | ||
| 1227 | # feature files into one big panel. | ||
| 1228 | my $trackno = $suppress_scale ? 0 : 1; | ||
| 1229 | my %feature_file_offsets; | ||
| 1230 | |||
| 1231 | for my $label (@$labels) { | ||
| 1232 | |||
| 1233 | # das tracks only go into details panel for now. | ||
| 1234 | next if $feature_files->{$label} && | ||
| 1235 | $label =~ m!/das/! && | ||
| 1236 | $section !~ /detail/; | ||
| 1237 | |||
| 1238 | next if $seenit{$label}++; # this shouldn't happen, but let's be paranoid | ||
| 1239 | |||
| 1240 | # if "hide" is set to true, then skip panel | ||
| 1241 | next if $conf->semantic_setting($label=>'hide',$length); | ||
| 1242 | |||
| 1243 | $track_args{$label} ||= [$self->create_track_args($label,$args)]; | ||
| 1244 | |||
| 1245 | # create a new panel if we are in drag_n_drop mode | ||
| 1246 | if ($drag_n_drop) { | ||
| 1247 | $panel_key = $label; | ||
| 1248 | |||
| 1249 | # get config data from the feature files | ||
| 1250 | my @extra_args = eval { | ||
| 1251 | $feature_files->{$label}->types, | ||
| 1252 | $feature_files->{$label}->mtime, | ||
| 1253 | } if $feature_files->{$label}; | ||
| 1254 | |||
| 1255 | my @args = ( | ||
| 1256 | @panel_args, | ||
| 1257 | @{$track_args{$label}}, | ||
| 1258 | @extra_args, | ||
| 1259 | @$cache_extra, | ||
| 1260 | $drag_n_drop, | ||
| 1261 | $options->{$label}, | ||
| 1262 | $label, | ||
| 1263 | ); | ||
| 1264 | |||
| 1265 | $cache_key{$label} = $self->create_cache_key(@args); | ||
| 1266 | next if $cached{$label} = $cache && $self->panel_is_cached($cache_key{$label}); | ||
| 1267 | |||
| 1268 | my @keystyle = (-key_style=>'between') | ||
| 1269 | if $label =~ /^\w+:/ && $label !~ /:(overview|region)/; # a plugin | ||
| 1270 | |||
| 1271 | $panels{$panel_key} = Bio::Graphics::Panel->new(@panel_args,@keystyle); | ||
| 1272 | } | ||
| 1273 | |||
| 1274 | # case of a third-party feature or plugin, in which case we defer creation of a track | ||
| 1275 | # but record where we would place it | ||
| 1276 | elsif ($feature_files->{$label}) { | ||
| 1277 | $feature_file_offsets{$label} = $trackno; | ||
| 1278 | next; | ||
| 1279 | } | ||
| 1280 | |||
| 1281 | $tracks{$label} = $panels{$panel_key}->add_track(@{$track_args{$label}}) | ||
| 1282 | unless $cached{$panel_key} || $feature_files->{$label}; | ||
| 1283 | } | ||
| 1284 | continue { | ||
| 1285 | $trackno++; | ||
| 1286 | } | ||
| 1287 | |||
| 1288 | #--------------------------------------------------------------------------------- | ||
| 1289 | # Add features to the database | ||
| 1290 | my @feature_types = map { $conf->label2type($_,$length) } grep {!$cached{$_}} @$labels; | ||
| 1291 | my %filters = map { my %conf = $conf->style($_); | ||
| 1292 | $conf{'-filter'} ? ($_ => $conf{'-filter'}) | ||
| 1293 | : () | ||
| 1294 | } @$labels; | ||
| 1295 | $self->add_features_to_track(-types => \@feature_types, | ||
| 1296 | -tracks => \%tracks, | ||
| 1297 | -filters => \%filters, | ||
| 1298 | -segment => $segment, | ||
| 1299 | -options => $options, | ||
| 1300 | -limits => $limits, | ||
| 1301 | ) if @feature_types; | ||
| 1302 | |||
| 1303 | # ------------------------------------------------------------------------------------------ | ||
| 1304 | # Add feature files, including remote annotations | ||
| 1305 | |||
| 1306 | # Start by removing uploaded files mentioned in the list of labels, but | ||
| 1307 | # not in the feature_files list. This is a workaround for an upstream bug. | ||
| 1308 | for my $l (grep {/^(file|http|ftp):/} @$labels) { | ||
| 1309 | next if $feature_files->{$l}; | ||
| 1310 | next unless $drag_n_drop; | ||
| 1311 | eval {$panels{$l}->finished}; | ||
| 1312 | delete $panels{$l}; | ||
| 1313 | delete $cached{$l}; | ||
| 1314 | } | ||
| 1315 | |||
| 1316 | my $featurefile_select = $args->{featurefile_select} | ||
| 1317 | || $self->feature_file_select($section); | ||
| 1318 | my $feature_file_extra_offset = 0; | ||
| 1319 | |||
| 1320 | my %trackmap; | ||
| 1321 | |||
| 1322 | for my $l (sort | ||
| 1323 | { | ||
| 1324 | ($feature_file_offsets{$a}||1) <=> ($feature_file_offsets{$b}||1) | ||
| 1325 | } keys %$feature_files) { | ||
| 1326 | |||
| 1327 | |||
| 1328 | next if $cached{$l}; | ||
| 1329 | my $file = $feature_files->{$l} or next; | ||
| 1330 | |||
| 1331 | ref $file or next; | ||
| 1332 | $panel_key = $l if $drag_n_drop; | ||
| 1333 | |||
| 1334 | next unless $panels{$panel_key}; | ||
| 1335 | |||
| 1336 | my $ff_offset = defined $feature_file_offsets{$l} ? $feature_file_offsets{$l} : 1; | ||
| 1337 | |||
| 1338 | my $override_args = $settings->{features}{$l}{override_settings} || {}; | ||
| 1339 | my @override = map {'-'.$_ => $override_args->{$_}} keys %$override_args; | ||
| 1340 | |||
| 1341 | my ($nr_tracks_added,$tracks) = | ||
| 1342 | $self->add_feature_file( | ||
| 1343 | file => $file, | ||
| 1344 | panel => $panels{$panel_key}, | ||
| 1345 | position => $ff_offset + $feature_file_extra_offset, | ||
| 1346 | options => $options, | ||
| 1347 | select => $featurefile_select, | ||
| 1348 | segment => $segment, | ||
| 1349 | override_settings => \@override, | ||
| 1350 | ); | ||
| 1351 | |||
| 1352 | do { eval {$panels{$panel_key}->finished}; | ||
| 1353 | delete $panels{$panel_key}; | ||
| 1354 | delete $cached{$panel_key}; | ||
| 1355 | } | ||
| 1356 | if $drag_n_drop && $nr_tracks_added==0; # suppress display of empty uploaded file tracks | ||
| 1357 | $trackmap{$_} = $file foreach @$tracks; | ||
| 1358 | |||
| 1359 | $feature_file_extra_offset += $nr_tracks_added-1; | ||
| 1360 | } | ||
| 1361 | |||
| 1362 | # map tracks (stringified track objects) to corresponding labels | ||
| 1363 | for my $label (keys %tracks) { $trackmap{$tracks{$label}} = $label } | ||
| 1364 | |||
| 1365 | # uncached panels need to be generated and cached | ||
| 1366 | $args->{scale_map_type} ||= 'centering_map' unless $suppress_scale; | ||
| 1367 | (my $map_name = $section) =~ s/^\?//; | ||
| 1368 | |||
| 1369 | for my $l (keys %panels) { | ||
| 1370 | my $gd = $panels{$l}->gd; | ||
| 1371 | my $boxes = $panels{$l}->boxes; | ||
| 1372 | $self->debugging_rectangles($gd,$boxes) if DEBUG; | ||
| 1373 | my $map = !$do_map ? (undef,undef) | ||
| 1374 | : $l eq '__pad__' ? (undef,undef) | ||
| 1375 | : $l eq '__scale__' ? $self->make_centering_map(shift @{$boxes}, | ||
| 1376 | $args->{flip}, | ||
| 1377 | $l, | ||
| 1378 | $args->{scale_map_type} | ||
| 1379 | ) | ||
| 1380 | : $l eq '__all__' ? $self->make_map($boxes, | ||
| 1381 | $panels{$l}, | ||
| 1382 | $map_name, | ||
| 1383 | \%trackmap, | ||
| 1384 | $args->{scale_map_type}) | ||
| 1385 | : $self->make_map($boxes, | ||
| 1386 | $panels{$l}, | ||
| 1387 | $l, | ||
| 1388 | \%trackmap, | ||
| 1389 | 0); | ||
| 1390 | my $key = $drag_n_drop ? $cache_key{$l} : $cache_key{'__all__'}; | ||
| 1391 | $self->set_cached_panel($key,$gd,$map); | ||
| 1392 | eval {$panels{$l}->finished}; | ||
| 1393 | } | ||
| 1394 | |||
| 1395 | # cached panels need to be retrieved | ||
| 1396 | for my $l (keys %cached) { | ||
| 1397 | @{$results{$l}}{qw(image map width height file gd boxes)} = $self->get_cached_panel($cache_key{$l}); | ||
| 1398 | # for apps that rely on the image_and_maps syntax, format the boxes | ||
| 1399 | $results{$l}{boxes} = $self->map_array($results{$l}{boxes}); | ||
| 1400 | } | ||
| 1401 | |||
| 1402 | return \%results; | ||
| 1403 | } | ||
| 1404 | |||
| 1405 | sub add_features_to_track { | ||
| 1406 | my $self = shift; | ||
| 1407 | my %args = @_; | ||
| 1408 | |||
| 1409 | my $segment = $args{-segment} or die "programming error"; | ||
| 1410 | my $feature_types = $args{-types} or die "programming error"; | ||
| 1411 | my $tracks = $args{-tracks} or die "programming error"; | ||
| 1412 | my $filters = $args{-filters} or die "programming error"; | ||
| 1413 | my $options = $args{-options} or die "programming error"; | ||
| 1414 | my $limits = $args{-limits} or die "programming error"; | ||
| 1415 | |||
| 1416 | my $max_labels = $self->label_density; | ||
| 1417 | my $max_bump = $self->bump_density; | ||
| 1418 | |||
| 1419 | my $length = $segment->length; | ||
| 1420 | my $conf = $self->config; | ||
| 1421 | |||
| 1422 | my (%groups,%feature_count,%group_pattern,%group_field); | ||
| 1423 | my $iterator = $segment->get_feature_stream(-type=>$feature_types); | ||
| 1424 | |||
| 1425 | while (my $feature = $iterator->next_seq) { | ||
| 1426 | |||
| 1427 | my @labels = $self->feature2label($feature,$length); | ||
| 1428 | |||
| 1429 | for my $l (@labels) { | ||
| 1430 | |||
| 1431 | my $track = $tracks->{$l} or next; | ||
| 1432 | $filters->{$l}->($feature) or next if $filters->{$l}; | ||
| 1433 | $feature_count{$l}++; | ||
| 1434 | |||
| 1435 | |||
| 1436 | # ------------------------------------------------------------------------------------------ | ||
| 1437 | # GROUP CODE | ||
| 1438 | # Handle name-based groupings. | ||
| 1439 | unless (exists $group_pattern{$l}) { | ||
| 1440 | $group_pattern{$l} = $conf->setting($l => 'group_pattern'); | ||
| 1441 | $group_pattern{$l} =~ s!^/(.+)/$!$1! | ||
| 1442 | if $group_pattern{$l}; # clean up regexp delimiters | ||
| 1443 | } | ||
| 1444 | |||
| 1445 | # Handle generic grouping (needed for GFF3 database) | ||
| 1446 | $group_field{$l} = $conf->setting($l => 'group_on') unless exists $group_field{$l}; | ||
| 1447 | |||
| 1448 | if (my $pattern = $group_pattern{$l}) { | ||
| 1449 | my $name = $feature->name or next; | ||
| 1450 | (my $base = $name) =~ s/$pattern//i; | ||
| 1451 | $groups{$l}{$base} ||= Bio::Graphics::Feature->new(-type => 'group'); | ||
| 1452 | $groups{$l}{$base}->add_segment($feature); | ||
| 1453 | $feature_count{$l}--; | ||
| 1454 | next; | ||
| 1455 | } | ||
| 1456 | |||
| 1457 | if (my $field = $group_field{$l}) { | ||
| 1458 | my $base = eval{$feature->$field}; | ||
| 1459 | if (defined $base) { | ||
| 1460 | $groups{$l}{$base} ||= $self->clone_feature($feature); | ||
| 1461 | $groups{$l}{$base}->add_SeqFeature($feature); | ||
| 1462 | $feature_count{$l}--; | ||
| 1463 | next; | ||
| 1464 | } | ||
| 1465 | } | ||
| 1466 | |||
| 1467 | $track->add_feature($feature); | ||
| 1468 | } | ||
| 1469 | } | ||
| 1470 | |||
| 1471 | # ------------------------------------------------------------------------------------------ | ||
| 1472 | # fixups | ||
| 1473 | |||
| 1474 | # fix up %group features | ||
| 1475 | # the former creates composite features based on an arbitrary method call | ||
| 1476 | # the latter is traditional name-based grouping based on a common prefix/suffix | ||
| 1477 | |||
| 1478 | for my $l (keys %groups) { | ||
| 1479 | my $track = $tracks->{$l}; | ||
| 1480 | my $g = $groups{$l} or next; | ||
| 1481 | $track->add_feature($_) foreach values %$g; | ||
| 1482 | $feature_count{$l} += keys %$g; | ||
| 1483 | } | ||
| 1484 | |||
| 1485 | # now reconfigure the tracks based on their counts | ||
| 1486 | for my $l (keys %$tracks) { | ||
| 1487 | next unless $feature_count{$l}; | ||
| 1488 | |||
| 1489 | $options->{$l} ||= 0; | ||
| 1490 | |||
| 1491 | my $count = $feature_count{$l}; | ||
| 1492 | $count = $limits->{$l} | ||
| 1493 | if $limits->{$l} && | ||
| 1494 | $limits->{$l} < $count; | ||
| 1495 | |||
| 1496 | my $do_bump = $self->do_bump($l, | ||
| 1497 | $options->{$l}, | ||
| 1498 | $count, | ||
| 1499 | $max_bump, | ||
| 1500 | $length); | ||
| 1501 | |||
| 1502 | my $do_label = $self->do_label($l, | ||
| 1503 | $options->{$l}, | ||
| 1504 | $count, | ||
| 1505 | $max_labels, | ||
| 1506 | $length); | ||
| 1507 | |||
| 1508 | my $do_description = $self->do_description($l, | ||
| 1509 | $options->{$l}, | ||
| 1510 | $count, | ||
| 1511 | $max_labels, | ||
| 1512 | $length); | ||
| 1513 | |||
| 1514 | $tracks->{$l}->configure(-bump => $do_bump, | ||
| 1515 | -label => $do_label, | ||
| 1516 | -description => $do_description, | ||
| 1517 | ); | ||
| 1518 | $tracks->{$l}->configure(-connector => 'none') if !$do_bump; | ||
| 1519 | $tracks->{$l}->configure(-bump_limit => $limits->{$l}) | ||
| 1520 | if $limits->{$l} && $limits->{$l} > 0; | ||
| 1521 | } | ||
| 1522 | } | ||
| 1523 | |||
| 1524 | =head2 add_feature_file | ||
| 1525 | |||
| 1526 | Internal use: render a feature file into a panel | ||
| 1527 | |||
| 1528 | =cut | ||
| 1529 | |||
| 1530 | sub add_feature_file { | ||
| 1531 | my $self = shift; | ||
| 1532 | my %args = @_; | ||
| 1533 | |||
| 1534 | my $file = $args{file} or return; | ||
| 1535 | my $options = $args{options} or return; | ||
| 1536 | |||
| 1537 | my $select = $args{select} or return; | ||
| 1538 | |||
| 1539 | my $name = $file->name || ''; | ||
| 1540 | $options->{$name} ||= 0; | ||
| 1541 | |||
| 1542 | my $override_settings = $args{override_settings}; | ||
| 1543 | |||
| 1544 | my ($nr_tracks_added,$panel,$tracklist) = | ||
| 1545 | eval { | ||
| 1546 | $file->render( | ||
| 1547 | $args{panel}, | ||
| 1548 | $args{position}, | ||
| 1549 | $options->{$name}, | ||
| 1550 | $self->bump_density, | ||
| 1551 | $self->label_density, | ||
| 1552 | $select, | ||
| 1553 | $args{segment}, | ||
| 1554 | $override_settings, | ||
| 1555 | ); | ||
| 1556 | }; | ||
| 1557 | |||
| 1558 | $self->error("error while rendering ",$args{file}->name,": $@") if $@; | ||
| 1559 | return ($nr_tracks_added,$tracklist); | ||
| 1560 | } | ||
| 1561 | |||
| 1562 | # this returns a coderef that will indicate whether an added (external) feature is placed | ||
| 1563 | # in the overview, region or detailed panel. If the section name begins with a "?", then | ||
| 1564 | # if not otherwise stated, the feature will be placed in this section. | ||
| 1565 | sub feature_file_select { | ||
| 1566 | my $self = shift; | ||
| 1567 | my $required_section = shift; | ||
| 1568 | |||
| 1569 | my $undef_defaults_to_true; | ||
| 1570 | if ($required_section =~ /^\?(.+)/) { | ||
| 1571 | $undef_defaults_to_true++; | ||
| 1572 | $required_section = $1; | ||
| 1573 | } | ||
| 1574 | |||
| 1575 | return sub { | ||
| 1576 | my $file = shift; | ||
| 1577 | my $type = shift; | ||
| 1578 | my $section = $file->setting($type=>'section') || $file->setting(general=>'section'); | ||
| 1579 | return $undef_defaults_to_true if !defined$section; | ||
| 1580 | return $section =~ /$required_section/; | ||
| 1581 | }; | ||
| 1582 | } | ||
| 1583 | |||
| 1584 | =head2 generate_image | ||
| 1585 | |||
| 1586 | ($url,$path) = $browser->generate_image($gd) | ||
| 1587 | |||
| 1588 | Given a GD::Image object, this method calls its png() or gif() methods | ||
| 1589 | (depending on GD version), stores the output into the temporary | ||
| 1590 | directory given by the "tmpimages" option in the configuration file, | ||
| 1591 | and returns a two element list consisting of the URL to the image and | ||
| 1592 | the physical path of the image. | ||
| 1593 | |||
| 1594 | =cut | ||
| 1595 | |||
| 1596 | sub generate_image { | ||
| 1597 | my $self = shift; | ||
| 1598 | my $image = shift; | ||
| 1599 | my ($extension,$data); | ||
| 1600 | |||
| 1601 | if (!ref $image) { # possibly raw SVG data -- this is a workaround | ||
| 1602 | $extension = 'svg'; | ||
| 1603 | $data = $image; | ||
| 1604 | } else { | ||
| 1605 | $extension = $image->can('png') ? 'png' : 'gif'; | ||
| 1606 | $data = $image->can('png') ? $image->png : $image->gif; | ||
| 1607 | } | ||
| 1608 | |||
| 1609 | my $signature = md5_hex($data); | ||
| 1610 | |||
| 1611 | warn ((CGI::param('ref')||'') . ':' . | ||
| 1612 | (CGI::param('start')||'') . '..'. | ||
| 1613 | (CGI::param('stop')||'') | ||
| 1614 | , | ||
| 1615 | " sig $signature\n") if DEBUG; | ||
| 1616 | |||
| 1617 | # untaint signature for use in open | ||
| 1618 | $signature =~ /^([0-9A-Fa-f]+)$/g or return; | ||
| 1619 | $signature = $1; | ||
| 1620 | |||
| 1621 | my ($uri,$path) = $self->tmpdir($self->source.'/img'); | ||
| 1622 | my $url = sprintf("%s/%s.%s",$uri,$signature,$extension); | ||
| 1623 | my $imagefile = sprintf("%s/%s.%s",$path,$signature,$extension); | ||
| 1624 | open (F,">$imagefile") || die("Can't open image file $imagefile for writing: $!\n"); | ||
| 1625 | binmode(F); | ||
| 1626 | print F $data; | ||
| 1627 | close F; | ||
| 1628 | return wantarray ? ($url,$imagefile) : $url; | ||
| 1629 | } | ||
| 1630 | |||
| 1631 | sub tmpdir { | ||
| 1632 | my $self = shift; | ||
| 1633 | my $path = shift || ''; | ||
| 1634 | |||
| 1635 | # Original code; retain while testing new "callback_setting" method below | ||
| 1636 | # my ($tmpuri,$tmpdir) = shellwords($self->setting('tmpimages')) | ||
| 1637 | # or die "no tmpimages option defined, can't generate a picture"; | ||
| 1638 | |||
| 1639 | my ($tmpuri,$tmpdir) = Legacy::Graphics::Browser::Util::shellwords($self->callback_setting('tmpimages')) | ||
| 1640 | or die "no tmpimages option defined, can't generate a picture"; | ||
| 1641 | |||
| 1642 | $tmpuri = $self->relative_path($tmpuri); | ||
| 1643 | $tmpuri .= "/$path" if $path; | ||
| 1644 | |||
| 1645 | if ($ENV{MOD_PERL} ) { | ||
| 1646 | my $r = modperl_request(); | ||
| 1647 | my $subr = $r->lookup_uri($tmpuri); | ||
| 1648 | $tmpdir = $subr->filename; | ||
| 1649 | my $path_info = $subr->path_info; | ||
| 1650 | $tmpdir .= $path_info if $path_info; | ||
| 1651 | } elsif ($tmpdir) { | ||
| 1652 | $tmpdir .= "/$path" if $path; | ||
| 1653 | } | ||
| 1654 | else { | ||
| 1655 | $tmpdir = "$ENV{DOCUMENT_ROOT}/$tmpuri"; | ||
| 1656 | } | ||
| 1657 | |||
| 1658 | # we need to untaint tmpdir before calling mkpath() | ||
| 1659 | return unless $tmpdir =~ /^(.+)$/; | ||
| 1660 | $path = $1; | ||
| 1661 | |||
| 1662 | mkpath($path,0,0777) unless -d $path; | ||
| 1663 | return ($tmpuri,$path); | ||
| 1664 | } | ||
| 1665 | |||
| 1666 | # Check if a configuration setting is a coderef or simple variable | ||
| 1667 | sub callback_setting { | ||
| 1668 | my $self = shift; | ||
| 1669 | my $val = $self->setting(@_); | ||
| 1670 | return ref $val eq 'CODE' ? $val->() : $val; | ||
| 1671 | } | ||
| 1672 | |||
| 1673 | sub make_map { | ||
| 1674 | my $self = shift; | ||
| 1675 | my ($boxes,$panel,$map_name,$trackmap,$first_box_is_scale) = @_; | ||
| 1676 | my @map = ($map_name); | ||
| 1677 | |||
| 1678 | my $flip = $panel->flip; | ||
| 1679 | my $tips = $self->setting('balloon tips'); | ||
| 1680 | my $use_titles_for_balloons = $self->setting('titles are balloons'); | ||
| 1681 | |||
| 1682 | my $did_map; | ||
| 1683 | |||
| 1684 | local $^W = 0; # avoid uninit variable warnings due to poor coderefs | ||
| 1685 | |||
| 1686 | if ($first_box_is_scale) { | ||
| 1687 | push @map, $self->make_centering_map(shift @$boxes,$flip,0,$first_box_is_scale); | ||
| 1688 | } | ||
| 1689 | |||
| 1690 | foreach (@$boxes){ | ||
| 1691 | next unless $_->[0]->can('primary_tag'); | ||
| 1692 | |||
| 1693 | my $label = $_->[5] ? $trackmap->{$_->[5]} : ''; | ||
| 1694 | |||
| 1695 | my $href = $self->make_href($_->[0],$panel,$label,$_->[5]); | ||
| 1696 | my $title = unescape($self->make_title($_->[0],$panel,$label,$_->[5])); | ||
| 1697 | my $target = $self->config->make_link_target($_->[0],$panel,$label,$_->[5]); | ||
| 1698 | |||
| 1699 | my ($mouseover,$mousedown,$style); | ||
| 1700 | if ($tips) { | ||
| 1701 | |||
| 1702 | #retrieve the content of the balloon from configuration files | ||
| 1703 | # if it looks like a URL, we treat it as a URL. | ||
| 1704 | my ($balloon_ht,$balloonhover) = | ||
| 1705 | $self->config->balloon_tip_setting('balloon hover',$label, | ||
| 1706 | $_->[0],$panel,$_->[5]); | ||
| 1707 | |||
| 1708 | my ($balloon_ct,$balloonclick) = | ||
| 1709 | $self->config->balloon_tip_setting('balloon click',$label, | ||
| 1710 | $_->[0],$panel,$_->[5]); | ||
| 1711 | |||
| 1712 | # balloon_ht = type of balloon to use for hovering -- usually "balloon" | ||
| 1713 | # balloon_ct = type of balloon to use for clicking -- usually "balloon" | ||
| 1714 | my $sticky = $self->setting($label,'balloon sticky'); | ||
| 1715 | my $height = $self->setting($label,'balloon height') || 300; | ||
| 1716 | my $width = $self->setting($label,'balloon width') || 0; | ||
| 1717 | my $hover_width = $self->setting($label,'balloon hover width') || $width; | ||
| 1718 | my $click_width = $self->setting($label,'balloon click width') || $width; | ||
| 1719 | |||
| 1720 | if ($use_titles_for_balloons) { | ||
| 1721 | $balloonhover ||= $title; | ||
| 1722 | $balloonhover =~ s/\'/\&\#39;/g; | ||
| 1723 | $balloonhover =~ s/\"/\&\#34;/g; | ||
| 1724 | } | ||
| 1725 | |||
| 1726 | $balloon_ht ||= 'balloon'; | ||
| 1727 | $balloon_ct ||= 'balloon'; | ||
| 1728 | |||
| 1729 | if ($balloonhover) { | ||
| 1730 | my $iframe_width = $hover_width || "'+parseInt($balloon_ct.maxWidth)+'"; | ||
| 1731 | my $stick = defined $sticky ? $sticky : 0; | ||
| 1732 | $mouseover = $balloonhover =~ /^(https?|ftp):/ | ||
| 1733 | ? "$balloon_ht.showTooltip(event,'<iframe width=$iframe_width height=$height frameborder=0 " . | ||
| 1734 | "src=$balloonhover scrolling=no></iframe>',$stick)" | ||
| 1735 | : "$balloon_ht.showTooltip(event,'$balloonhover',$stick,$hover_width)"; | ||
| 1736 | undef $title; | ||
| 1737 | } | ||
| 1738 | if ($balloonclick) { | ||
| 1739 | my $iframe_width = $click_width || "'+parseInt($balloon_ct.maxWidth)+'"; | ||
| 1740 | my $iframe_style = "style=padding-right:16px"; | ||
| 1741 | $style = "cursor:pointer"; | ||
| 1742 | $mousedown = $balloonclick =~ /^(http|ftp):/ | ||
| 1743 | ? "$balloon_ct.showTooltip(event,'<iframe width=$iframe_width height=$height " . | ||
| 1744 | "frameborder=0 src=$balloonclick $iframe_style></iframe>')" | ||
| 1745 | : "$balloon_ct.showTooltip(event,'$balloonclick',1,$click_width)"; | ||
| 1746 | undef $href; | ||
| 1747 | } | ||
| 1748 | } | ||
| 1749 | |||
| 1750 | my %attributes = ( | ||
| 1751 | title => $title, | ||
| 1752 | href => $href, | ||
| 1753 | target => $target, | ||
| 1754 | onmouseover => $mouseover, | ||
| 1755 | onmousedown => $mousedown, | ||
| 1756 | style => $style | ||
| 1757 | ); | ||
| 1758 | |||
| 1759 | my $ftype = $_->[0]->primary_tag || 'feature'; | ||
| 1760 | my $fname = $_->[0]->display_name if $_->[0]->can('display_name'); | ||
| 1761 | $fname ||= $_->[0]->name if $_->[0]->can('name'); | ||
| 1762 | $fname ||= 'unnamed'; | ||
| 1763 | $ftype = "$ftype:$fname"; | ||
| 1764 | my $line = join("\t",$ftype,@{$_}[1..4]); | ||
| 1765 | for my $att (keys %attributes) { | ||
| 1766 | next unless defined $attributes{$att} && length $attributes{$att}; | ||
| 1767 | $line .= "\t$att\t$attributes{$att}"; | ||
| 1768 | } | ||
| 1769 | push @map, $line; | ||
| 1770 | } | ||
| 1771 | |||
| 1772 | return \@map; | ||
| 1773 | |||
| 1774 | } | ||
| 1775 | |||
| 1776 | # this creates image map for rulers and scales, where clicking on the scale | ||
| 1777 | # should center the image on the scale. | ||
| 1778 | sub make_centering_map { | ||
| 1779 | my $self = shift; | ||
| 1780 | my ($ruler,$flip,$label,$scale_map_type) = @_; | ||
| 1781 | my @map = $label ? ($label) : (); | ||
| 1782 | |||
| 1783 | my $title = $self->tr('Recenter'); | ||
| 1784 | |||
| 1785 | return if $ruler->[3]-$ruler->[1] == 0; | ||
| 1786 | |||
| 1787 | my $length = $ruler->[0]->length; | ||
| 1788 | my $offset = $ruler->[0]->start; | ||
| 1789 | my $end = $ruler->[0]->end; | ||
| 1790 | my $scale = $length/($ruler->[3]-$ruler->[1]); | ||
| 1791 | my $pl = $ruler->[-1]->panel->pad_left; | ||
| 1792 | |||
| 1793 | my $ruler_intervals = RULER_INTERVALS; | ||
| 1794 | |||
| 1795 | if ($scale_map_type eq 'interval_map' && $length/RULER_INTERVALS > $self->get_max_segment) { | ||
| 1796 | my $max = $self->get_max_segment/5; # usually a safe guess | ||
| 1797 | $ruler_intervals = int($length/$max); | ||
| 1798 | } | ||
| 1799 | |||
| 1800 | # divide into RULER_INTERVAL intervals | ||
| 1801 | my $portion = ($ruler->[3]-$ruler->[1])/$ruler_intervals; | ||
| 1802 | my $ref = $ruler->[0]->seq_id; | ||
| 1803 | my $source = $self->source; | ||
| 1804 | |||
| 1805 | for my $i (0..$ruler_intervals-1) { | ||
| 1806 | my $x1 = int($portion * $i+0.5); | ||
| 1807 | my $x2 = int($portion * ($i+1)+0.5); | ||
| 1808 | |||
| 1809 | my ($start,$stop); | ||
| 1810 | if ($scale_map_type eq 'centering_map') { | ||
| 1811 | # put the middle of the sequence range into the middle of the picture | ||
| 1812 | my $middle = $flip ? $end - $scale * ($x1+$x2)/2 : $offset + $scale * ($x1+$x2)/2; | ||
| 1813 | $start = int($middle - $length/2); | ||
| 1814 | $stop = int($start + $length - 1); | ||
| 1815 | } | ||
| 1816 | elsif ($scale_map_type eq 'interval_map') { | ||
| 1817 | # center on the interval | ||
| 1818 | $start = int($flip ? $end - $scale * $x1 : $offset + $scale * $x1); | ||
| 1819 | $stop = int($start + $portion * $scale); | ||
| 1820 | } | ||
| 1821 | |||
| 1822 | $x1 += $pl; | ||
| 1823 | $x2 += $pl; | ||
| 1824 | |||
| 1825 | my $url = "?ref=$ref;start=$start;stop=$stop"; | ||
| 1826 | $url .= ";flip=1" if $flip; | ||
| 1827 | |||
| 1828 | push @map, join("\t",'ruler',$x1, $ruler->[2], $x2, $ruler->[4], | ||
| 1829 | href => $url, title => $title||'', alt => $title||''); | ||
| 1830 | } | ||
| 1831 | |||
| 1832 | return $label ? \@map : @map; | ||
| 1833 | } | ||
| 1834 | |||
| 1835 | sub make_href { | ||
| 1836 | my $self = shift; | ||
| 1837 | my ($feature,$panel,$label,$track) = @_; | ||
| 1838 | return $self->make_link($feature,$panel,$label,$self->source,$track); | ||
| 1839 | } | ||
| 1840 | |||
| 1841 | sub make_title { | ||
| 1842 | my $self = shift; | ||
| 1843 | my ($feature,$panel,$label) = @_; | ||
| 1844 | return $feature->make_title if $feature->can('make_title'); | ||
| 1845 | return $self->config->make_title($feature,$panel,$label); | ||
| 1846 | } | ||
| 1847 | |||
| 1848 | |||
| 1849 | ###### attempted substitution; unfortunately runs slower than original!! ####### | ||
| 1850 | =head2 new_hits_on_overview() | ||
| 1851 | |||
| 1852 | $hashref = $browser->hits_on_overview($db,$hits,$options,$keyname); | ||
| 1853 | |||
| 1854 | This method is used to render a series of genomic positions ("hits") | ||
| 1855 | into a graphical summary of where they hit on the genome in a | ||
| 1856 | segment-by-segment (e.g. chromosome) manner. | ||
| 1857 | |||
| 1858 | The first argument is a Bio::DB::GFF (or Bio::DasI) database. | ||
| 1859 | |||
| 1860 | The second argument is an array ref containing one of: | ||
| 1861 | |||
| 1862 | 1) a set of array refs in the form [ref,start,stop,name], where | ||
| 1863 | name is optional. | ||
| 1864 | |||
| 1865 | 2) a Bio::DB::GFF::Feature object | ||
| 1866 | |||
| 1867 | 3) a Legacy::SeqFeatureI object. | ||
| 1868 | |||
| 1869 | The third argument is the page settings hash from gbrowse. | ||
| 1870 | |||
| 1871 | The fourth option is the key to use for the "hits" track. | ||
| 1872 | |||
| 1873 | The returned HTML is stored in a hashref, where the keys are the | ||
| 1874 | reference sequence names and the values are HTML to be emitted. | ||
| 1875 | |||
| 1876 | =cut | ||
| 1877 | |||
| 1878 | sub new_hits_on_overview { | ||
| 1879 | my $self = shift; | ||
| 1880 | my ($db,$hits,$page_settings,$keyname) = @_; | ||
| 1881 | |||
| 1882 | |||
| 1883 | my %overviews; # results are a hashref sorted by chromosome | ||
| 1884 | |||
| 1885 | $keyname ||= 'Matches'; | ||
| 1886 | |||
| 1887 | my $class = eval{$hits->[0]->factory->default_class} || 'Sequence'; | ||
| 1888 | my ($padl,$padr) = $self->overview_pad([grep { $page_settings->{$_}{visible}} | ||
| 1889 | $self->config->overview_tracks], | ||
| 1890 | 'Matches'); | ||
| 1891 | |||
| 1892 | # sort hits out by reference and version | ||
| 1893 | my (%featurefiles); | ||
| 1894 | |||
| 1895 | for my $hit (@$hits) { | ||
| 1896 | if (ref($hit) eq 'ARRAY') { | ||
| 1897 | my ($ref,$start,$stop,$name) = @$hit; | ||
| 1898 | $featurefiles{$ref} ||= Bio::Graphics::FeatureFile->new(-smart_features => 1); | ||
| 1899 | $featurefiles{$ref}->add_feature(Bio::Graphics::Feature->new(-seq_id=>$ref, | ||
| 1900 | -start=>$start, | ||
| 1901 | -end=>$stop, | ||
| 1902 | -name=>$name||'', | ||
| 1903 | -type=>'hit', | ||
| 1904 | ) | ||
| 1905 | ); | ||
| 1906 | } | ||
| 1907 | elsif (UNIVERSAL::can($hit,'seq_id')) { | ||
| 1908 | my $name = $hit->can('seq_name') ? $hit->seq_name : $hit->name; | ||
| 1909 | eval {$hit->absolute(1)}; | ||
| 1910 | my $ref = my $id = $hit->seq_id; | ||
| 1911 | my $version = eval {$hit->isa('Legacy::SeqFeatureI') ? undef : $hit->version}; | ||
| 1912 | $ref .= " version $version" if defined $version; | ||
| 1913 | my($start,$end) = ($hit->start,$hit->end); | ||
| 1914 | $name =~ s/\:\d+,\d+$//; # remove coordinates if they're there | ||
| 1915 | $name = substr($name,0,7).'...' if length $name > 10; | ||
| 1916 | $featurefiles{$ref} ||= Bio::Graphics::FeatureFile->new(-smart_features => 1); | ||
| 1917 | my $f =Bio::Graphics::Feature->new(-seq_id=>$ref, | ||
| 1918 | -start=>$start, | ||
| 1919 | -end=>$end, | ||
| 1920 | -name=>$name, | ||
| 1921 | -type=>'hit', | ||
| 1922 | ); | ||
| 1923 | $featurefiles{$ref}->add_feature($f); | ||
| 1924 | } elsif (UNIVERSAL::can($hit,'location')) { | ||
| 1925 | my $location = $hit->location; | ||
| 1926 | my ($ref,$start,$stop,$name) = ($location->seq_id,$location->start, | ||
| 1927 | $location->end,$location->primary_tag); | ||
| 1928 | $featurefiles{$ref} ||= Bio::Graphics::FeatureFile->new(-smart_features => 1); | ||
| 1929 | $featurefiles{$ref}->add_feature(Bio::Graphics::Feature->new(-seq_id=>$ref, | ||
| 1930 | -start=>$start, | ||
| 1931 | -end=>$stop, | ||
| 1932 | -name=>$name||'', | ||
| 1933 | -type=>'hit') | ||
| 1934 | ); | ||
| 1935 | } | ||
| 1936 | } | ||
| 1937 | |||
| 1938 | # We now have a feature list. Create an overview for each unique ref | ||
| 1939 | my @refs = sort keys %featurefiles; | ||
| 1940 | |||
| 1941 | my @tracks_to_show = grep {$page_settings->{features}{$_}{visible} && /:overview$/ } | ||
| 1942 | @{$page_settings->{tracks}}; | ||
| 1943 | push @tracks_to_show,'my_data'; | ||
| 1944 | |||
| 1945 | for my $ref (@refs) { | ||
| 1946 | my ($name, $version) = split /\sversion\s/i, $ref; | ||
| 1947 | my $segment = ($db->segment(-class=>$class,-name=>$name, | ||
| 1948 | defined $version ? (-version => $version):()))[0] or next; | ||
| 1949 | |||
| 1950 | my @cache_extra = (time); # this will never cache | ||
| 1951 | my $count = scalar (my @h = $featurefiles{$ref}->features); | ||
| 1952 | $featurefiles{$ref}->add_type(hit => { | ||
| 1953 | glyph => 'diamond', | ||
| 1954 | bgcolor=> 'red', | ||
| 1955 | fgcolor=> 'red', | ||
| 1956 | key => $keyname, | ||
| 1957 | fallback_to_rectangle => 1, | ||
| 1958 | no_subparts => 1, | ||
| 1959 | bump => $count <= $self->bump_density, | ||
| 1960 | label => $count <= $self->bump_density, # deliberate | ||
| 1961 | link => sub {my $f = shift; | ||
| 1962 | return "?name=".$f->display_name} | ||
| 1963 | } | ||
| 1964 | ); | ||
| 1965 | my $html = $self->render_panels({ | ||
| 1966 | section => "overview_${ref}", | ||
| 1967 | segment => $segment, | ||
| 1968 | labels => \@tracks_to_show, | ||
| 1969 | feature_files => {my_data => $featurefiles{$ref}}, | ||
| 1970 | drag_n_drop => 0, | ||
| 1971 | do_map => 1, | ||
| 1972 | scale_map_type=> 'interval_map', | ||
| 1973 | keystyle => 'left', | ||
| 1974 | label_scale => 1, | ||
| 1975 | cache_extra => \@cache_extra, | ||
| 1976 | image_class => 'GD', | ||
| 1977 | featurefile_select => sub { 1 } , | ||
| 1978 | -grid => 0, | ||
| 1979 | -pad_left => $padl, | ||
| 1980 | -pad_right => $padr, | ||
| 1981 | -bgcolor => $self->setting("overview bgcolor") || DEFAULT_OVERVIEW_BGCOLOR, | ||
| 1982 | } | ||
| 1983 | ); | ||
| 1984 | $overviews{$ref} = $html; | ||
| 1985 | } | ||
| 1986 | |||
| 1987 | return \%overviews; | ||
| 1988 | } | ||
| 1989 | |||
| 1990 | sub bump_density { | ||
| 1991 | my $self = shift; | ||
| 1992 | my $conf = $self->config; | ||
| 1993 | return $conf->setting(general=>'bump density') | ||
| 1994 | || $conf->setting('TRACK DEFAULTS' =>'bump density') | ||
| 1995 | || 50; | ||
| 1996 | } | ||
| 1997 | |||
| 1998 | sub label_density { | ||
| 1999 | my $self = shift; | ||
| 2000 | my $conf = $self->config; | ||
| 2001 | return $conf->setting(general=>'label density') | ||
| 2002 | || $conf->setting('TRACK DEFAULTS' =>'label density') | ||
| 2003 | || 10; | ||
| 2004 | } | ||
| 2005 | |||
| 2006 | sub do_bump { | ||
| 2007 | my $self = shift; | ||
| 2008 | my ($track_name,$option,$count,$max,$length) = @_; | ||
| 2009 | |||
| 2010 | my $conf = $self->config; | ||
| 2011 | my $maxb = $conf->setting($track_name => 'bump density'); | ||
| 2012 | $maxb = $max unless defined $maxb; | ||
| 2013 | |||
| 2014 | my $maxed_out = $count <= $maxb; | ||
| 2015 | my $conf_bump = $conf->semantic_setting($track_name => 'bump',$length); | ||
| 2016 | $option ||= 0; | ||
| 2017 | return defined $conf_bump ? $conf_bump | ||
| 2018 | : $option == 0 ? $maxed_out | ||
| 2019 | : $option == 1 ? 0 | ||
| 2020 | : $option == 2 ? 1 | ||
| 2021 | : $option == 3 ? 1 | ||
| 2022 | : $option == 4 ? 2 | ||
| 2023 | : $option == 5 ? 2 | ||
| 2024 | : 0; | ||
| 2025 | } | ||
| 2026 | |||
| 2027 | sub do_label { | ||
| 2028 | my $self = shift; | ||
| 2029 | my ($track_name,$option,$count,$max_labels,$length) = @_; | ||
| 2030 | |||
| 2031 | my $conf = $self->config; | ||
| 2032 | |||
| 2033 | my $maxl = $conf->setting($track_name => 'label density'); | ||
| 2034 | $maxl = $max_labels unless defined $maxl; | ||
| 2035 | my $maxed_out = $count <= $maxl; | ||
| 2036 | |||
| 2037 | my $conf_label = $conf->semantic_setting($track_name => 'label',$length); | ||
| 2038 | $conf_label = 1 unless defined $conf_label; | ||
| 2039 | |||
| 2040 | $option ||= 0; | ||
| 2041 | return $option == 0 ? $maxed_out && $conf_label | ||
| 2042 | : $option == 3 ? $conf_label || 1 | ||
| 2043 | : $option == 5 ? $conf_label || 1 | ||
| 2044 | : 0; | ||
| 2045 | } | ||
| 2046 | |||
| 2047 | sub do_description { | ||
| 2048 | my $self = shift; | ||
| 2049 | my ($track_name,$option,$count,$max_labels,$length) = @_; | ||
| 2050 | |||
| 2051 | my $conf = $self->config; | ||
| 2052 | |||
| 2053 | my $maxl = $conf->setting($track_name => 'label density'); | ||
| 2054 | $maxl = $max_labels unless defined $maxl; | ||
| 2055 | my $maxed_out = $count <= $maxl; | ||
| 2056 | |||
| 2057 | my $conf_description = $conf->semantic_setting($track_name => 'description',$length); | ||
| 2058 | $conf_description = 0 unless defined $conf_description; | ||
| 2059 | $option ||= 0; | ||
| 2060 | return $option == 0 ? $maxed_out && $conf_description | ||
| 2061 | : $option == 3 ? $conf_description || 1 | ||
| 2062 | : $option == 5 ? $conf_description || 1 | ||
| 2063 | : 0; | ||
| 2064 | } | ||
| 2065 | |||
| 2066 | # given a feature, return the segment (e.g. chromosome) that it is contained in. | ||
| 2067 | sub whole_segment { | ||
| 2068 | my $self = shift; | ||
| 2069 | my $segment = shift; | ||
| 2070 | my $factory = $segment->factory; | ||
| 2071 | |||
| 2072 | # the segment class has been deprecated, but we still must support it | ||
| 2073 | my $class = eval {$segment->seq_id->class} || eval{$factory->refclass}; | ||
| 2074 | |||
| 2075 | my ($whole_segment) = $factory->segment(-class=>$class, | ||
| 2076 | -name=>$segment->seq_id); | ||
| 2077 | $whole_segment ||= $segment; # just paranoia | ||
| 2078 | $whole_segment; | ||
| 2079 | } | ||
| 2080 | |||
| 2081 | # fetch a list of Segment objects given a name or range | ||
| 2082 | # (this used to be in gbrowse executable itself) | ||
| 2083 | sub name2segments { | ||
| 2084 | my $self = shift; | ||
| 2085 | my ($literal_name,$db,$toomany,$segments_have_priority,$dont_merge) = @_; | ||
| 2086 | |||
| 2087 | $dont_merge = !$self->setting('merge searches') | ||
| 2088 | if defined $self->setting('merge searches'); | ||
| 2089 | |||
| 2090 | $toomany ||= TOO_MANY_SEGMENTS; | ||
| 2091 | |||
| 2092 | my $max_segment = $self->get_max_segment; | ||
| 2093 | |||
| 2094 | my $name = $literal_name; | ||
| 2095 | my (@segments,$class,$start,$stop); | ||
| 2096 | if ( ($name !~ /\.\./ and $name =~ /([\w._\/-]+):(-?[-e\d.]+),(-?[-e\d.]+)$/) or | ||
| 2097 | $name =~ /([\w._\/-]+):(-?[-e\d,.]+?)(?:-|\.\.)(-?[-e\d,.]+)$/) { | ||
| 2098 | $name = $1; | ||
| 2099 | $start = $2; | ||
| 2100 | $stop = $3; | ||
| 2101 | $start =~ s/,//g; # get rid of commas | ||
| 2102 | $stop =~ s/,//g; | ||
| 2103 | } | ||
| 2104 | |||
| 2105 | elsif ($name =~ /^(\w+):(.+)$/) { | ||
| 2106 | $class = $1; | ||
| 2107 | $name = $2; | ||
| 2108 | } | ||
| 2109 | |||
| 2110 | my $divisor = $self->config->setting(general=>'unit_divider') || 1; | ||
| 2111 | $start *= $divisor if defined $start; | ||
| 2112 | $stop *= $divisor if defined $stop; | ||
| 2113 | |||
| 2114 | # automatic classes to try | ||
| 2115 | my @classes = $class ? ($class) : (split /\s+/,$self->setting('automatic classes')||''); | ||
| 2116 | my $refclass = $self->setting('reference class') || 'Sequence'; | ||
| 2117 | |||
| 2118 | SEARCHING: | ||
| 2119 | for my $n ([$name,$class,$start,$stop],[$literal_name,$refclass,undef,undef]) { | ||
| 2120 | |||
| 2121 | my ($name_to_try,$class_to_try,$start_to_try,$stop_to_try) = @$n; | ||
| 2122 | |||
| 2123 | # first try the non-heuristic search | ||
| 2124 | @segments = $self->_feature_get($db,$name_to_try,$class_to_try,$start_to_try,$stop_to_try, | ||
| 2125 | $segments_have_priority,$dont_merge); | ||
| 2126 | last SEARCHING if @segments; | ||
| 2127 | |||
| 2128 | # heuristic fetch. Try various abbreviations and wildcards | ||
| 2129 | my @sloppy_names = $name_to_try; | ||
| 2130 | if ($name_to_try =~ /^([\dIVXA-F]+)$/) { | ||
| 2131 | my $id = $1; | ||
| 2132 | foreach (qw(CHROMOSOME_ Chr chr)) { | ||
| 2133 | my $n = "${_}${id}"; | ||
| 2134 | push @sloppy_names,$n; | ||
| 2135 | } | ||
| 2136 | } | ||
| 2137 | |||
| 2138 | # try to remove the chr CHROMOSOME_I | ||
| 2139 | if ($name_to_try =~ /^(chromosome_?|chr)/i) { | ||
| 2140 | (my $chr = $name_to_try) =~ s/^(chromosome_?|chr)//i; | ||
| 2141 | push @sloppy_names,$chr; | ||
| 2142 | } | ||
| 2143 | |||
| 2144 | # try the wildcard version, but only if the name is of significant length | ||
| 2145 | # IMPORTANT CHANGE: we used to put stars at the beginning and end, but this killed performance! | ||
| 2146 | push @sloppy_names,"$name_to_try*" if length $name_to_try > 3 | ||
| 2147 | and $name_to_try !~ /\*$/ | ||
| 2148 | and !$self->setting('disable wildcards'); | ||
| 2149 | |||
| 2150 | for my $n (@sloppy_names) { | ||
| 2151 | for my $c (@classes) { | ||
| 2152 | @segments = $self->_feature_get($db,$n,$c,$start_to_try,$stop_to_try,$segments_have_priority,$dont_merge); | ||
| 2153 | last SEARCHING if @segments; | ||
| 2154 | } | ||
| 2155 | } | ||
| 2156 | } | ||
| 2157 | |||
| 2158 | return @segments; | ||
| 2159 | } | ||
| 2160 | |||
| 2161 | sub _feature_get { | ||
| 2162 | my $self = shift; | ||
| 2163 | my ($db,$name,$class,$start,$stop,$segments_have_priority,$dont_merge,$f_id) = @_; | ||
| 2164 | |||
| 2165 | my $refclass = $self->setting('reference class') || 'Sequence'; | ||
| 2166 | $class ||= $refclass; | ||
| 2167 | |||
| 2168 | my @argv = (-name => $name); | ||
| 2169 | push @argv,(-class => $class) if defined $class; | ||
| 2170 | push @argv,(-start => $start) if defined $start; | ||
| 2171 | push @argv,(-end => $stop) if defined $stop; | ||
| 2172 | push @argv,(-feature_id => $f_id) if defined $f_id; | ||
| 2173 | # This step is a hack to turn off relative addressing when getting absolute coordinates on the | ||
| 2174 | # reference molecule. | ||
| 2175 | push @argv,(-absolute=>1) if $class eq $refclass; | ||
| 2176 | warn "\@argv = @argv" if DEBUG; | ||
| 2177 | |||
| 2178 | my @segments; | ||
| 2179 | @segments = $db->fetch($f_id) if defined $f_id | ||
| 2180 | && $db->can('fetch'); | ||
| 2181 | |||
| 2182 | @segments = $db->get_feature_by_primary_id($f_id) if !@segments | ||
| 2183 | && defined $f_id | ||
| 2184 | && $db->can('get_feature_by_primary_id'); | ||
| 2185 | |||
| 2186 | if (!@segments) { | ||
| 2187 | if ($segments_have_priority) { | ||
| 2188 | @segments = grep {$_->length} $db->segment(@argv); | ||
| 2189 | @segments = grep {$_->length} $db->get_feature_by_name(@argv) if !@segments; | ||
| 2190 | } else { | ||
| 2191 | @segments = grep {$_->length} $db->get_feature_by_name(@argv) if !defined($start) && !defined($stop); | ||
| 2192 | @segments = grep {$_->length} $db->get_features_by_alias(@argv) if !@segments && !defined($start) | ||
| 2193 | && !defined($stop) | ||
| 2194 | && $db->can('get_features_by_alias'); | ||
| 2195 | @segments = grep {$_->length} $db->segment(@argv) if !@segments && $name !~ /[*?]/; | ||
| 2196 | } | ||
| 2197 | } | ||
| 2198 | |||
| 2199 | # one last try for Bio::DB::GFF | ||
| 2200 | if (defined $f_id && defined $name && !@segments) { | ||
| 2201 | @segments = grep {$_->length} $db->get_feature_by_name(@argv); | ||
| 2202 | } | ||
| 2203 | |||
| 2204 | return unless @segments; | ||
| 2205 | |||
| 2206 | # Deal with multiple hits. Winnow down to just those that | ||
| 2207 | # were mentioned in the config file. | ||
| 2208 | my $types = $self->_all_types($db); | ||
| 2209 | |||
| 2210 | my @filtered = | ||
| 2211 | grep { | ||
| 2212 | my $type = $_->type; | ||
| 2213 | my $method = eval {$_->method} || ''; | ||
| 2214 | my $fclass = eval {$_->class} || ''; | ||
| 2215 | $type eq 'Segment' # ugly stuff accomodates loss of "class" concept in GFF3 | ||
| 2216 | || $type eq 'region' | ||
| 2217 | || $types->{$type} | ||
| 2218 | || $types->{$method} | ||
| 2219 | || !$fclass | ||
| 2220 | || $fclass eq $refclass | ||
| 2221 | || $fclass eq $class; | ||
| 2222 | } @segments; | ||
| 2223 | |||
| 2224 | |||
| 2225 | return @filtered if $dont_merge; | ||
| 2226 | |||
| 2227 | # consolidate features that have same name and same reference sequence | ||
| 2228 | # and take the largest one. | ||
| 2229 | local $^W=0; # uninit variable warning - can't find it | ||
| 2230 | my %longest; | ||
| 2231 | foreach (@filtered) { | ||
| 2232 | my $n = $_->display_name.$_->abs_ref.(eval{$_->version}||'').(eval{$_->class}||''); | ||
| 2233 | $longest{$n} = $_ if !defined($longest{$n}) || $_->length > $longest{$n}->length; | ||
| 2234 | } | ||
| 2235 | |||
| 2236 | values %longest; | ||
| 2237 | } | ||
| 2238 | |||
| 2239 | sub get_ranges { | ||
| 2240 | my $self = shift; | ||
| 2241 | my $divisor = $self->setting('unit_divider') || 1; | ||
| 2242 | my $rangestr = $self->setting('zoom levels'); | ||
| 2243 | if (!$rangestr) { | ||
| 2244 | return split /\s+/,DEFAULT_RANGES; | ||
| 2245 | } elsif ($divisor == 1 ) { | ||
| 2246 | return split /\s+/,$rangestr; | ||
| 2247 | } else { | ||
| 2248 | return map {$_ * $divisor} split /\s+/,$rangestr; | ||
| 2249 | } | ||
| 2250 | } | ||
| 2251 | |||
| 2252 | sub get_max_segment { | ||
| 2253 | my $self = shift; | ||
| 2254 | my $divisor = $self->setting('unit_divider') || 1; | ||
| 2255 | my $max_seg = $self->setting('max segment'); | ||
| 2256 | if (!$max_seg) { | ||
| 2257 | return MAX_SEGMENT; | ||
| 2258 | } elsif ($divisor == 1 ) { | ||
| 2259 | return $max_seg | ||
| 2260 | } else { | ||
| 2261 | return $max_seg * $divisor; | ||
| 2262 | } | ||
| 2263 | } | ||
| 2264 | |||
| 2265 | sub get_default_segment { | ||
| 2266 | my $self = shift; | ||
| 2267 | my $divisor = $self->setting('unit_divider') || 1; | ||
| 2268 | my $def_seg = $self->setting('default segment'); | ||
| 2269 | if (!$def_seg) { | ||
| 2270 | return DEFAULT_SEGMENT; | ||
| 2271 | } elsif ($divisor == 1 ) { | ||
| 2272 | return $def_seg | ||
| 2273 | } else { | ||
| 2274 | return $def_seg * $divisor; | ||
| 2275 | } | ||
| 2276 | } | ||
| 2277 | |||
| 2278 | sub _all_types { | ||
| 2279 | my $self = shift; | ||
| 2280 | my $db = shift; | ||
| 2281 | return $self->{_all_types} if exists $self->{_all_types}; # memoize | ||
| 2282 | my %types = map {$_=>1} ( | ||
| 2283 | (map {$_->get_method} eval {$db->aggregators}), | ||
| 2284 | (map {$self->label2type($_)} $self->labels) | ||
| 2285 | ); | ||
| 2286 | return $self->{_all_types} = \%types; | ||
| 2287 | } | ||
| 2288 | |||
| 2289 | # Handle types that are hidden by aggregators so that | ||
| 2290 | # features link correctly when they are subparts rather than | ||
| 2291 | # the top-level part | ||
| 2292 | sub _load_aggregator_types { | ||
| 2293 | my $self = shift; | ||
| 2294 | my $segment = shift; | ||
| 2295 | return if $self->config->{_load_aggregator_types}++; # don't do it twice | ||
| 2296 | my $db = eval {$segment->factory} or return; | ||
| 2297 | my @aggregators = eval {$db->aggregators } or return; | ||
| 2298 | for my $a (@aggregators) { | ||
| 2299 | my $method = $a->method; | ||
| 2300 | my @subparts = ($a->part_names,$a->main_name); | ||
| 2301 | for my $track ($self->type2label($method)) { | ||
| 2302 | foreach (@subparts) { | ||
| 2303 | $self->config->{_type2label}{$_}{$track}++; | ||
| 2304 | } | ||
| 2305 | } | ||
| 2306 | } | ||
| 2307 | } | ||
| 2308 | |||
| 2309 | |||
| 2310 | # utility called by hits_on_overview | ||
| 2311 | sub _hits_to_html { | ||
| 2312 | my $self = shift; | ||
| 2313 | my ($ref,$gd,$boxes) = @_; | ||
| 2314 | my ($name, $version) = split /\sversion\s/i, $ref; | ||
| 2315 | my $source = $self->source; | ||
| 2316 | my $self_url = ''; #url(-relative=>1); | ||
| 2317 | # $self_url .= "?source=$source"; | ||
| 2318 | |||
| 2319 | my $signature = md5_hex(rand().rand()); # just a big random number | ||
| 2320 | my ($width,$height) = $gd->getBounds; | ||
| 2321 | my $url = $self->generate_image($gd,$signature); | ||
| 2322 | my $img = img({-src=>$url, | ||
| 2323 | -align=>'middle', | ||
| 2324 | -usemap=>"#$ref", | ||
| 2325 | -width => $width, | ||
| 2326 | -height => $height, | ||
| 2327 | -border=>0}); | ||
| 2328 | my $html = "\n"; | ||
| 2329 | $html .= $img; | ||
| 2330 | $html .= qq(<br /><map name="$ref" alt="imagemap" />\n); | ||
| 2331 | |||
| 2332 | # use the scale as a centering mechanism | ||
| 2333 | my $ruler = shift @$boxes; | ||
| 2334 | return unless $ruler->[0]; # don't know why.... | ||
| 2335 | |||
| 2336 | |||
| 2337 | my $length = $ruler->[0]->length/RULER_INTERVALS; | ||
| 2338 | $width = ($ruler->[3]-$ruler->[1])/RULER_INTERVALS; | ||
| 2339 | for my $i (0..RULER_INTERVALS-1) { | ||
| 2340 | my $x = $ruler->[1] + $i * $width; | ||
| 2341 | my $y = $x + $width; | ||
| 2342 | my $start = int($length * $i); | ||
| 2343 | my $stop = int($start + $length); | ||
| 2344 | my $href = $self_url . "?ref=$name;start=$start;stop=$stop"; | ||
| 2345 | $href .= ";version=$version" if defined $version; | ||
| 2346 | $html .= qq(<area shape="rect" coords="$x,$ruler->[2],$y,$ruler->[4]" href="$href" alt="ruler" />\n); | ||
| 2347 | } | ||
| 2348 | |||
| 2349 | foreach (@$boxes){ | ||
| 2350 | my ($start,$stop) = ($_->[0]->start,$_->[0]->end); | ||
| 2351 | my $href = $self_url . "?ref=$name;start=$start;stop=$stop"; | ||
| 2352 | $href .= ";version=$version" if defined $version; | ||
| 2353 | $html .= qq(<area shape="rect" coords="$_->[1],$_->[2],$_->[3],$_->[4]" href="$href" alt="ruler" />\n); | ||
| 2354 | } | ||
| 2355 | $html .= "</map>\n"; | ||
| 2356 | $html; | ||
| 2357 | } | ||
| 2358 | |||
| 2359 | # I know there must be a more elegant way to insert commas into a long number... | ||
| 2360 | sub commas { | ||
| 2361 | my $i = shift; | ||
| 2362 | return $i if $i=~ /\D/; | ||
| 2363 | $i = reverse $i; | ||
| 2364 | $i =~ s/(\d{3})/$1,/g; | ||
| 2365 | chop $i if $i=~/,$/; | ||
| 2366 | $i = reverse $i; | ||
| 2367 | $i; | ||
| 2368 | } | ||
| 2369 | |||
| 2370 | sub merge { | ||
| 2371 | my $self = shift; | ||
| 2372 | my ($db,$features,$max_range) = @_; | ||
| 2373 | $max_range ||= 100_000; | ||
| 2374 | |||
| 2375 | my (%segs,@merged_segs); | ||
| 2376 | push @{$segs{$_->seq_id}},$_ foreach @$features; | ||
| 2377 | foreach (keys %segs) { | ||
| 2378 | push @merged_segs,_low_merge($db,$segs{$_},$max_range); | ||
| 2379 | } | ||
| 2380 | return @merged_segs; | ||
| 2381 | } | ||
| 2382 | |||
| 2383 | sub _low_merge { | ||
| 2384 | my ($db,$features,$max_range) = @_; | ||
| 2385 | |||
| 2386 | my ($previous_start,$previous_stop,$statistical_cutoff,@spans); | ||
| 2387 | |||
| 2388 | my @features = sort {$a->low<=>$b->low} @$features; | ||
| 2389 | |||
| 2390 | # run through the segments, and find the mean and stdev gap length | ||
| 2391 | # need at least 10 features before this becomes reliable | ||
| 2392 | if (@features >= 10) { | ||
| 2393 | my ($total,$gap_length,@gaps); | ||
| 2394 | for (my $i=0; $i<@$features-1; $i++) { | ||
| 2395 | my $gap = $features[$i+1]->low - $features[$i]->high; | ||
| 2396 | $total++; | ||
| 2397 | $gap_length += $gap; | ||
| 2398 | push @gaps,$gap; | ||
| 2399 | } | ||
| 2400 | my $mean = $gap_length/$total; | ||
| 2401 | my $variance; | ||
| 2402 | $variance += ($_-$mean)**2 foreach @gaps; | ||
| 2403 | my $stdev = sqrt($variance/$total); | ||
| 2404 | $statistical_cutoff = $stdev * 2; | ||
| 2405 | } else { | ||
| 2406 | $statistical_cutoff = $max_range; | ||
| 2407 | } | ||
| 2408 | |||
| 2409 | my $ref = $features[0]->seq_id; | ||
| 2410 | |||
| 2411 | for my $f (@features) { | ||
| 2412 | my $start = $f->low; | ||
| 2413 | my $stop = $f->high; | ||
| 2414 | |||
| 2415 | if (defined($previous_stop) && | ||
| 2416 | ( $start-$previous_stop >= $max_range || | ||
| 2417 | $previous_stop-$previous_start >= $max_range || | ||
| 2418 | $start-$previous_stop >= $statistical_cutoff)) { | ||
| 2419 | push @spans,$db->segment($ref,$previous_start,$previous_stop); | ||
| 2420 | $previous_start = $start; | ||
| 2421 | $previous_stop = $stop; | ||
| 2422 | } | ||
| 2423 | |||
| 2424 | else { | ||
| 2425 | $previous_start = $start unless defined $previous_start; | ||
| 2426 | $previous_stop = $stop; | ||
| 2427 | } | ||
| 2428 | |||
| 2429 | } | ||
| 2430 | my $class = eval { $features[0]->factory->refclass }; | ||
| 2431 | my @args = (-name=>$ref,-start=>$previous_start,-end=>$previous_stop); | ||
| 2432 | push @args,(-class=>$class) if defined $class; | ||
| 2433 | push @spans,$db ? $db->segment(@args) | ||
| 2434 | : Bio::Graphics::Feature->new(-start=>$previous_start,-end=>$previous_stop,-ref=>$ref); | ||
| 2435 | return @spans; | ||
| 2436 | } | ||
| 2437 | |||
| 2438 | sub overview_pad { | ||
| 2439 | my $self = shift; | ||
| 2440 | my $tracks = shift; | ||
| 2441 | |||
| 2442 | if ($self->drag_and_drop) { # not relevant when drag and drop is active | ||
| 2443 | my $padding = $self->image_padding; | ||
| 2444 | return ($padding,$padding); | ||
| 2445 | } | ||
| 2446 | |||
| 2447 | $tracks ||= [$self->config->overview_tracks]; | ||
| 2448 | my $max = 0; | ||
| 2449 | foreach (@$tracks) { | ||
| 2450 | my $key = $self->setting($_=>'key'); | ||
| 2451 | next unless defined $key; | ||
| 2452 | $max = length $key if length $key > $max; | ||
| 2453 | } | ||
| 2454 | foreach (@_) { #extra | ||
| 2455 | $max = length if length > $max; | ||
| 2456 | } | ||
| 2457 | |||
| 2458 | # Tremendous kludge! Not able to generate overview maps in GD yet | ||
| 2459 | # This needs to be cleaned... | ||
| 2460 | my $image_class = 'GD'; | ||
| 2461 | eval "use $image_class"; | ||
| 2462 | return (MIN_OVERVIEW_PAD,MIN_OVERVIEW_PAD) unless $max; | ||
| 2463 | return ($max * $image_class->gdMediumBoldFont->width + 3,MIN_OVERVIEW_PAD); | ||
| 2464 | } | ||
| 2465 | |||
| 2466 | sub true { 1 } | ||
| 2467 | |||
| 2468 | sub debugging_rectangles { | ||
| 2469 | my $self = shift; | ||
| 2470 | my ($image,$boxes) = @_; | ||
| 2471 | my $red = $image->colorClosest(255,0,0); | ||
| 2472 | foreach (@$boxes) { | ||
| 2473 | my @rect = @{$_}[1,2,3,4]; | ||
| 2474 | $image->rectangle(@{$_}[1,2,3,4],$red); | ||
| 2475 | } | ||
| 2476 | } | ||
| 2477 | |||
| 2478 | # Returns the language code, but only if we have a translate table for it. | ||
| 2479 | sub language_code { | ||
| 2480 | my $self = shift; | ||
| 2481 | my $lang = $self->language; | ||
| 2482 | my $table= $lang->tr_table($lang->language); | ||
| 2483 | return unless %$table; | ||
| 2484 | return $lang->language; | ||
| 2485 | } | ||
| 2486 | |||
| 2487 | =head2 error() | ||
| 2488 | |||
| 2489 | my $error = $browser->error(['new error']); | ||
| 2490 | |||
| 2491 | Retrieve or store an error message. Currently used to pass run-time | ||
| 2492 | errors involving uploaded/remote annotation files. | ||
| 2493 | |||
| 2494 | =cut | ||
| 2495 | |||
| 2496 | sub error { | ||
| 2497 | my $self = shift; # do nothing | ||
| 2498 | my $err_msg = shift; | ||
| 2499 | $err_msg = '' if ref $err_msg; | ||
| 2500 | $self->{'.err_msg'} = $err_msg; | ||
| 2501 | $self->{'.err_msg'}; | ||
| 2502 | } | ||
| 2503 | |||
| 2504 | sub fatal_error { | ||
| 2505 | my $self = shift; | ||
| 2506 | print CGI::header('text/plain'),"@_\n"; | ||
| 2507 | exit 0; | ||
| 2508 | } | ||
| 2509 | |||
| 2510 | =head2 create_panel_args() | ||
| 2511 | |||
| 2512 | @args = $self->create_panel_args($section,$args); | ||
| 2513 | |||
| 2514 | Return arguments need to create a Bio::Graphics::Panel. | ||
| 2515 | $section is one of 'detail','overview', or 'region' | ||
| 2516 | $args is a hashref that contains the keys: | ||
| 2517 | |||
| 2518 | keystyle | ||
| 2519 | title | ||
| 2520 | image_class | ||
| 2521 | postgrid | ||
| 2522 | background | ||
| 2523 | |||
| 2524 | =cut | ||
| 2525 | |||
| 2526 | sub create_panel_args { | ||
| 2527 | my $self = shift; | ||
| 2528 | my ($section,$args) = @_; | ||
| 2529 | |||
| 2530 | my $segment = $args->{segment}; | ||
| 2531 | my ($seg_start,$seg_stop,$flip) = $self->segment_coordinates($segment, | ||
| 2532 | $args->{flip}); | ||
| 2533 | |||
| 2534 | my $image_class = $args->{image_class} || 'GD'; | ||
| 2535 | eval "use $image_class" unless "${image_class}::Image"->can('new'); | ||
| 2536 | |||
| 2537 | my $keystyle = $self->drag_and_drop($args->{drag_n_drop}) | ||
| 2538 | ? 'none' | ||
| 2539 | : $args->{keystyle} || $self->setting('keystyle') || DEFAULT_KEYSTYLE; | ||
| 2540 | |||
| 2541 | my @pass_thru_args = map {/^-/ ? ($_=>$args->{$_}) : ()} keys %$args; | ||
| 2542 | |||
| 2543 | my @argv = ( | ||
| 2544 | -grid => 1, | ||
| 2545 | -seq_id => $segment->seq_id, | ||
| 2546 | -start => $seg_start, | ||
| 2547 | -end => $seg_stop, | ||
| 2548 | -stop => $seg_stop, #backward compatibility with old bioperl | ||
| 2549 | -key_color => $self->setting('key bgcolor') || 'moccasin', | ||
| 2550 | -bgcolor => $self->setting('detail bgcolor') || 'white', | ||
| 2551 | -width => $self->width, | ||
| 2552 | -key_style => $keystyle, | ||
| 2553 | -empty_tracks => $self->setting('empty_tracks') || DEFAULT_EMPTYTRACKS, | ||
| 2554 | -pad_top => $args->{title} ? $image_class->gdMediumBoldFont->height : 0, | ||
| 2555 | -image_class => $image_class, | ||
| 2556 | -postgrid => $args->{postgrid} || '', | ||
| 2557 | -background => $args->{background} || '', | ||
| 2558 | -truecolor => $self->setting('truecolor') || 0, | ||
| 2559 | @pass_thru_args, # position is important here to allow user to override settings | ||
| 2560 | ); | ||
| 2561 | |||
| 2562 | push @argv, -flip => 1 if $flip; | ||
| 2563 | my $p = $self->image_padding; | ||
| 2564 | my $pl = $self->setting('pad_left'); | ||
| 2565 | my $pr = $self->setting('pad_right'); | ||
| 2566 | $pl = $p unless defined $pl; | ||
| 2567 | $pr = $p unless defined $pr; | ||
| 2568 | |||
| 2569 | push @argv,(-pad_left =>$pl, -pad_right=>$pr) if $p; | ||
| 2570 | |||
| 2571 | |||
| 2572 | return (@argv, | ||
| 2573 | -pad_top => 18, | ||
| 2574 | -extend_grid => 1) | ||
| 2575 | if $self->drag_and_drop; | ||
| 2576 | |||
| 2577 | return @argv; | ||
| 2578 | } | ||
| 2579 | |||
| 2580 | sub image_padding { | ||
| 2581 | my $self = shift; | ||
| 2582 | return defined $self->setting('image_padding') ? $self->setting('image_padding') | ||
| 2583 | : PAD_DETAIL_SIDES; | ||
| 2584 | } | ||
| 2585 | |||
| 2586 | =head2 create_track_args() | ||
| 2587 | |||
| 2588 | @args = $self->create_track_args($label,$args); | ||
| 2589 | |||
| 2590 | Return arguments need to create a Legacy::Graphics::Track. | ||
| 2591 | $label is a config file stanza label for the track. | ||
| 2592 | |||
| 2593 | =cut | ||
| 2594 | |||
| 2595 | sub create_track_args { | ||
| 2596 | my $self = shift; | ||
| 2597 | my ($label,$args) = @_; | ||
| 2598 | |||
| 2599 | my $segment = $args->{segment}; | ||
| 2600 | my $lang = $args->{lang}; | ||
| 2601 | my $hilite_callback = $args->{hilite_callback}; | ||
| 2602 | my $override = $args->{settings}{features}{$label}{override_settings} | ||
| 2603 | || {}; # user-set override settings for tracks | ||
| 2604 | my @override = map {'-'.$_ => $override->{$_}} keys %$override; | ||
| 2605 | |||
| 2606 | my $length = $segment->length; | ||
| 2607 | my $conf = $self->config; | ||
| 2608 | |||
| 2609 | my @default_args = (-glyph => 'generic'); | ||
| 2610 | push @default_args,(-key => $label) unless $label =~ /^\w+:/; | ||
| 2611 | push @default_args,(-hilite => $hilite_callback) if $hilite_callback; | ||
| 2612 | |||
| 2613 | my @args; | ||
| 2614 | if ($conf->semantic_setting($label=>'global feature',$length)) { | ||
| 2615 | @args = ($segment, | ||
| 2616 | @default_args, | ||
| 2617 | $conf->default_style, | ||
| 2618 | $conf->i18n_style($label, | ||
| 2619 | $lang), | ||
| 2620 | @override, | ||
| 2621 | ); | ||
| 2622 | } else { | ||
| 2623 | @args = (@default_args, | ||
| 2624 | $conf->default_style, | ||
| 2625 | $conf->i18n_style($label, | ||
| 2626 | $lang, | ||
| 2627 | $length), | ||
| 2628 | @override, | ||
| 2629 | ); | ||
| 2630 | } | ||
| 2631 | return @args; | ||
| 2632 | } | ||
| 2633 | |||
| 2634 | =head2 segment_coordinates() | ||
| 2635 | |||
| 2636 | ($start,$stop,$flip) = $self->segment_coordinates($segment,$flip) | ||
| 2637 | |||
| 2638 | Method to correct for rare case in which start and stop are flipped. | ||
| 2639 | |||
| 2640 | =cut | ||
| 2641 | |||
| 2642 | sub segment_coordinates { | ||
| 2643 | my $self = shift; | ||
| 2644 | my $segment = shift; | ||
| 2645 | my $flip = shift; | ||
| 2646 | |||
| 2647 | # Create the tracks that we will need | ||
| 2648 | my ($seg_start,$seg_stop ) = ($segment->start,$segment->end); | ||
| 2649 | if ($seg_stop < $seg_start) { | ||
| 2650 | ($seg_start,$seg_stop) = ($seg_stop,$seg_start); | ||
| 2651 | $flip = 1; | ||
| 2652 | } | ||
| 2653 | return ($seg_start,$seg_stop,$flip); | ||
| 2654 | } | ||
| 2655 | |||
| 2656 | =head2 create_cache_key() | ||
| 2657 | |||
| 2658 | $cache_key = $self->create_cache_key(@args) | ||
| 2659 | |||
| 2660 | Create a unique cache key for the given args. | ||
| 2661 | |||
| 2662 | =cut | ||
| 2663 | |||
| 2664 | sub create_cache_key { | ||
| 2665 | my $self = shift; | ||
| 2666 | my @args = map {$_ || ''} grep {!ref($_)} @_; # the map gets rid of uninit variable warnings | ||
| 2667 | return md5_hex(@args); | ||
| 2668 | } | ||
| 2669 | |||
| 2670 | sub get_cache_base { | ||
| 2671 | my $self = shift; | ||
| 2672 | my ($key,$filename) = @_; | ||
| 2673 | my @comp = $key =~ /(..)/g; | ||
| 2674 | # my $rel_path = join '/',$self->source,'panel_cache',@comp[0..1],$key; | ||
| 2675 | my $rel_path = join '/',$self->source,'panel_cache',$comp[0],$key; | ||
| 2676 | my ($uri,$path) = $self->tmpdir($rel_path); | ||
| 2677 | |||
| 2678 | return wantarray ? ("$path/$filename","$uri/$filename") : "$path/$filename"; | ||
| 2679 | } | ||
| 2680 | |||
| 2681 | sub panel_is_cached { | ||
| 2682 | my $self = shift; | ||
| 2683 | my $key = shift; | ||
| 2684 | return unless (my $cache_time = $self->cache_time); | ||
| 2685 | my $size_file = $self->get_cache_base($key,'size'); | ||
| 2686 | return unless -e $size_file; | ||
| 2687 | my $mtime = (stat(_))[9]; # _ is not a bug, but an automatic filehandle | ||
| 2688 | my $hours_since_last_modified = (time()-$mtime)/(60*60); | ||
| 2689 | return unless $hours_since_last_modified < $cache_time; | ||
| 2690 | warn "cache hit for $key" if DEBUG; | ||
| 2691 | 1; | ||
| 2692 | } | ||
| 2693 | |||
| 2694 | =head2 get_cached_panel() | ||
| 2695 | |||
| 2696 | ($image_uri,$map,$width,$height) = $self->get_cached_panel($cache_key) | ||
| 2697 | |||
| 2698 | Return cached image url, imagemap data, width and height of image. | ||
| 2699 | |||
| 2700 | =cut | ||
| 2701 | |||
| 2702 | sub get_cached_panel { | ||
| 2703 | my $self = shift; | ||
| 2704 | my $key = shift; | ||
| 2705 | |||
| 2706 | my $map_file = $self->get_cache_base($key,'map') or return; | ||
| 2707 | my $size_file = $self->get_cache_base($key,'size') or return; | ||
| 2708 | my ($image_file,$image_uri) = $self->get_cache_base($key,'image') or return; | ||
| 2709 | |||
| 2710 | # get map data | ||
| 2711 | my $map_data = []; | ||
| 2712 | if (-e $map_file) { | ||
| 2713 | my $f = IO::File->new($map_file) or return; | ||
| 2714 | while (my $line = $f->getline) { | ||
| 2715 | push @$map_data, $line; | ||
| 2716 | } | ||
| 2717 | $f->close; | ||
| 2718 | } | ||
| 2719 | |||
| 2720 | # get size data | ||
| 2721 | my ($width,$height); | ||
| 2722 | if (-e $size_file) { | ||
| 2723 | my $f = IO::File->new($size_file) or return; | ||
| 2724 | chomp($width = $f->getline); | ||
| 2725 | chomp($height = $f->getline); | ||
| 2726 | $f->close; | ||
| 2727 | } | ||
| 2728 | |||
| 2729 | my $base = -e "$image_file.png" ? '.png' | ||
| 2730 | : -e "$image_file.jpg" ? '.jpg' | ||
| 2731 | : -e "$image_file.svg" ? '.svg' | ||
| 2732 | : '.gif'; | ||
| 2733 | $image_uri .= $base; | ||
| 2734 | $image_file .= $base; | ||
| 2735 | |||
| 2736 | my $gd = GD::Image->new($image_file) unless $image_file =~ /svg$/; | ||
| 2737 | my $map_html = $self->map_html(@$map_data); | ||
| 2738 | return ($image_uri,$map_html,$width,$height,$image_file,$gd,$map_data); | ||
| 2739 | } | ||
| 2740 | |||
| 2741 | # Convert the cached image map data | ||
| 2742 | # into an array structure analogous to | ||
| 2743 | # Bio::Graphics::Panel->boxes | ||
| 2744 | sub map_array { | ||
| 2745 | my $self = shift; | ||
| 2746 | my $data = shift; | ||
| 2747 | chomp @$data; | ||
| 2748 | my $name = shift @$data or return; | ||
| 2749 | my $map = [$name]; | ||
| 2750 | |||
| 2751 | for (@$data) { | ||
| 2752 | my ($type,$x1,$y1,$x2,$y2,@atts) = split "\t"; | ||
| 2753 | pop @atts if @atts % 2; | ||
| 2754 | my %atts = @atts; | ||
| 2755 | push @$map, [$type,$x1,$y1,$x2,$y2,\%atts]; | ||
| 2756 | } | ||
| 2757 | return $map; | ||
| 2758 | } | ||
| 2759 | |||
| 2760 | # Convert the cached image map data | ||
| 2761 | # into HTML. | ||
| 2762 | sub map_html { | ||
| 2763 | my $self = shift; | ||
| 2764 | my @data = @_; | ||
| 2765 | chomp @data; | ||
| 2766 | my $name = shift @data or return ''; | ||
| 2767 | |||
| 2768 | # my $html = qq(\n<map name="${name}_map" id="${name}_map">\n); | ||
| 2769 | my $html = ''; | ||
| 2770 | |||
| 2771 | for (@data) { | ||
| 2772 | my @tokens = split "\t"; | ||
| 2773 | push @tokens,undef unless @tokens%2; # ensure an odd number | ||
| 2774 | my (undef,$x1,$y1,$x2,$y2,%atts) = map {$_||''} @tokens; # get rid of uninit values | ||
| 2775 | $x1 or next; | ||
| 2776 | my $coords = join(',',$x1,$y1,$x2,$y2); | ||
| 2777 | $html .= qq(<area shape="rect" coords="$coords" ); | ||
| 2778 | for my $att (keys %atts) { | ||
| 2779 | $html .= qq($att="$atts{$att}" ); | ||
| 2780 | } | ||
| 2781 | $html .= qq(/>\n); | ||
| 2782 | } | ||
| 2783 | |||
| 2784 | # $html .= qq(</map>\n); | ||
| 2785 | return $html; | ||
| 2786 | } | ||
| 2787 | |||
| 2788 | sub map_css { | ||
| 2789 | my ($self,$data,$view) = @_; | ||
| 2790 | $data && ref $data eq 'ARRAY' or return; | ||
| 2791 | my @data = @$data; | ||
| 2792 | chomp @data; | ||
| 2793 | my $name = shift @data or return ''; | ||
| 2794 | |||
| 2795 | my $pl = $self->setting('pad_left')|| 0; | ||
| 2796 | my $pt = $self->setting('pad_top') || 0; | ||
| 2797 | |||
| 2798 | my $html; | ||
| 2799 | for (@data) { | ||
| 2800 | my @elements = @$_; | ||
| 2801 | push @elements,'' if @elements%2==0; # get rid of odd-number of elements warning | ||
| 2802 | my ($ruler,$x1,$y1,$x2,$y2,$atts) = @elements; | ||
| 2803 | my %atts = %$atts; | ||
| 2804 | $x1 or next; | ||
| 2805 | $x1 += $pl; | ||
| 2806 | $y1 += $pt; | ||
| 2807 | my $width = abs($x2 - $x1); | ||
| 2808 | my $height = abs($y2 - $y1); | ||
| 2809 | |||
| 2810 | next if $ruler eq 'ruler'; | ||
| 2811 | my %style = ( top => "${y1}px", | ||
| 2812 | left => "${x1}px", | ||
| 2813 | cursor => 'pointer', | ||
| 2814 | width => "${width}px", | ||
| 2815 | height => "${height}px", | ||
| 2816 | position => 'absolute'); | ||
| 2817 | |||
| 2818 | # my %conf = (name => "${view}_image_map"); | ||
| 2819 | my %conf = (); | ||
| 2820 | for my $att (keys %atts) { | ||
| 2821 | my $val = $atts{$att}; | ||
| 2822 | if ($att eq 'href') { | ||
| 2823 | next if $atts{onclick} || $atts{onmousedown}; | ||
| 2824 | $att = 'onmousedown'; | ||
| 2825 | $val = "window.location='$val'"; | ||
| 2826 | $style{cursor} = 'pointer'; | ||
| 2827 | } | ||
| 2828 | $conf{$att} = $val; | ||
| 2829 | } | ||
| 2830 | $conf{style} = _style(%style); | ||
| 2831 | |||
| 2832 | $html .= '<span '; | ||
| 2833 | for my $label (keys %conf) { | ||
| 2834 | $html .= qq($label="$conf{$label}" ); | ||
| 2835 | } | ||
| 2836 | $html .="></span>\n"; | ||
| 2837 | } | ||
| 2838 | |||
| 2839 | return $html; | ||
| 2840 | } | ||
| 2841 | |||
| 2842 | sub _style { | ||
| 2843 | my %h = @_; | ||
| 2844 | my $style; | ||
| 2845 | for (keys %h) { | ||
| 2846 | $style .= join(':',$_,$h{$_}). ';'; | ||
| 2847 | } | ||
| 2848 | $style; | ||
| 2849 | } | ||
| 2850 | |||
| 2851 | |||
| 2852 | sub set_cached_panel { | ||
| 2853 | my $self = shift; | ||
| 2854 | my ($key,$gd,$map_data) = @_; | ||
| 2855 | |||
| 2856 | my $map_file = $self->get_cache_base($key,'map') or return; | ||
| 2857 | my $size_file = $self->get_cache_base($key,'size') or return; | ||
| 2858 | my ($image_file,$image_uri) = $self->get_cache_base($key,'image') or return; | ||
| 2859 | |||
| 2860 | # write the map data | ||
| 2861 | if ($map_data) { | ||
| 2862 | my $f = IO::File->new(">$map_file") or die "$map_file: $!"; | ||
| 2863 | $f->print(join("\n", @$map_data),"\n"); | ||
| 2864 | $f->close; | ||
| 2865 | } | ||
| 2866 | |||
| 2867 | return unless $gd; | ||
| 2868 | |||
| 2869 | # get the width and height and write the size data | ||
| 2870 | my ($width,$height) = $gd->getBounds; | ||
| 2871 | my $f = IO::File->new(">$size_file") or die "$size_file: $!"; | ||
| 2872 | $f->print($width,"\n"); | ||
| 2873 | $f->print($height,"\n"); | ||
| 2874 | $f->close; | ||
| 2875 | |||
| 2876 | my $image_data; | ||
| 2877 | |||
| 2878 | if ($gd->can('svg')) { | ||
| 2879 | $image_file .= ".svg"; | ||
| 2880 | $image_data = $gd->svg; | ||
| 2881 | } | ||
| 2882 | elsif ($gd->can('png')) { | ||
| 2883 | $image_file .= ".png"; | ||
| 2884 | $image_data = $gd->png; | ||
| 2885 | } | ||
| 2886 | |||
| 2887 | elsif ($gd->can('gif')) { | ||
| 2888 | $image_file .= ".gif"; | ||
| 2889 | $image_data = $gd->gif; | ||
| 2890 | } | ||
| 2891 | |||
| 2892 | elsif ($gd->can('jpeg')) { | ||
| 2893 | $image_file .= ".jpg"; | ||
| 2894 | $image_data = $gd->jpeg; | ||
| 2895 | } | ||
| 2896 | |||
| 2897 | $f = IO::File->new(">$image_file") or die "$image_file: $!"; | ||
| 2898 | binmode($f); | ||
| 2899 | $f->print($image_data); | ||
| 2900 | $f->close; | ||
| 2901 | |||
| 2902 | return ($image_uri,$map_data,$width,$height,$image_file); | ||
| 2903 | } | ||
| 2904 | |||
| 2905 | # convert bp into nice Mb/Kb units | ||
| 2906 | sub unit_label { | ||
| 2907 | my $self = shift; | ||
| 2908 | my $value = shift; | ||
| 2909 | |||
| 2910 | my $unit = $self->setting('units') || 'bp'; | ||
| 2911 | my $divider = $self->setting('unit_divider') || 1; | ||
| 2912 | $value /= $divider; | ||
| 2913 | my $abs = abs($value); | ||
| 2914 | |||
| 2915 | my $label; | ||
| 2916 | $label = $abs >= 1e9 ? sprintf("%.4g G%s",$value/1e9,$unit) | ||
| 2917 | : $abs >= 1e6 ? sprintf("%.4g M%s",$value/1e6,$unit) | ||
| 2918 | : $abs >= 1e3 ? sprintf("%.4g k%s",$value/1e3,$unit) | ||
| 2919 | : $abs >= 1 ? sprintf("%.4g %s", $value, $unit) | ||
| 2920 | : $abs >= 1e-2 ? sprintf("%.4g c%s",$value*100,$unit) | ||
| 2921 | : $abs >= 1e-3 ? sprintf("%.4g m%s",$value*1e3,$unit) | ||
| 2922 | : $abs >= 1e-6 ? sprintf("%.4g u%s",$value*1e6,$unit) | ||
| 2923 | : $abs >= 1e-9 ? sprintf("%.4g n%s",$value*1e9,$unit) | ||
| 2924 | : sprintf("%.4g p%s",$value*1e12,$unit); | ||
| 2925 | if (wantarray) { | ||
| 2926 | return split ' ',$label; | ||
| 2927 | } else { | ||
| 2928 | return $label; | ||
| 2929 | } | ||
| 2930 | } | ||
| 2931 | |||
| 2932 | # convert Mb/Kb back into bp... or a ratio | ||
| 2933 | sub unit_to_value { | ||
| 2934 | my $self = shift; | ||
| 2935 | my $string = shift; | ||
| 2936 | my $sign = $string =~ /out|left/ ? '-' : '+'; | ||
| 2937 | my ($value,$units) = $string =~ /([\d.]+) ?(\S+)/; | ||
| 2938 | return unless defined $value; | ||
| 2939 | $value /= 100 if $units eq '%'; # percentage; | ||
| 2940 | $value *= 1000 if $units =~ /kb/i; | ||
| 2941 | $value *= 1e6 if $units =~ /mb/i; | ||
| 2942 | $value *= 1e9 if $units =~ /gb/i; | ||
| 2943 | return "$sign$value"; | ||
| 2944 | } | ||
| 2945 | |||
| 2946 | |||
| 2947 | =head2 | ||
| 2948 | |||
| 2949 | ($region_sizes,$region_labels,$region_default) = $config->region_sizes() | ||
| 2950 | |||
| 2951 | Return information about the region panel: | ||
| 2952 | |||
| 2953 | 1. list of valid region sizes (@$region_sizes) | ||
| 2954 | 2. mapping of size to label (%$region_labels) | ||
| 2955 | 3. default size ($region_default) | ||
| 2956 | |||
| 2957 | =cut | ||
| 2958 | |||
| 2959 | sub region_sizes { | ||
| 2960 | my $self = shift; | ||
| 2961 | my $settings = shift; | ||
| 2962 | |||
| 2963 | my @region_sizes = sort {$b<=>$a} Legacy::Graphics::Browser::Util::shellwords($self->setting('region segments')); | ||
| 2964 | unless (@region_sizes) { | ||
| 2965 | my $default = $self->setting('region segment') || $self->setting('default segment') || 50000; | ||
| 2966 | @region_sizes = ($default * 2, $default, int $default/2) unless $default eq 'AUTO';; | ||
| 2967 | } | ||
| 2968 | |||
| 2969 | my %region_labels = map {$_=>scalar $self->unit_label($_)} @region_sizes; | ||
| 2970 | my $region_default = $settings->{region_size} || $self->setting('region segment'); | ||
| 2971 | $region_default ||= $self->setting('default segment'); | ||
| 2972 | |||
| 2973 | $region_labels{AUTO} = 'AUTO'; | ||
| 2974 | unshift @region_sizes, 'AUTO'; | ||
| 2975 | |||
| 2976 | return (\@region_sizes,\%region_labels,$region_default); | ||
| 2977 | } | ||
| 2978 | |||
| 2979 | sub clone_feature { | ||
| 2980 | my $self = shift; | ||
| 2981 | my $feature = shift; | ||
| 2982 | my $clone = Bio::Graphics::Feature->new(-start => $feature->start, | ||
| 2983 | -end => $feature->end, | ||
| 2984 | -strand => $feature->strand, | ||
| 2985 | -type => $feature->primary_tag, | ||
| 2986 | -source => $feature->source, | ||
| 2987 | -name => $feature->display_name); | ||
| 2988 | # transfer attributes if we can | ||
| 2989 | eval { | ||
| 2990 | for my $tag ($feature->get_all_tags) { | ||
| 2991 | my @values = $feature->get_tag_values($tag); | ||
| 2992 | $clone->add_tag_value($tag=>@values); | ||
| 2993 | $clone->desc($values[0]) if lc $tag eq 'note'; | ||
| 2994 | } | ||
| 2995 | }; | ||
| 2996 | warn $@ if $@; | ||
| 2997 | |||
| 2998 | return $clone; | ||
| 2999 | } | ||
| 3000 | |||
| 3001 | sub coordinate_mapper { | ||
| 3002 | my $self = shift; | ||
| 3003 | my $current_segment = shift; | ||
| 3004 | my $optimize = shift; | ||
| 3005 | |||
| 3006 | my $db = $current_segment->factory; | ||
| 3007 | |||
| 3008 | my ($ref,$start,$stop) = ($current_segment->seq_id, | ||
| 3009 | $current_segment->start, | ||
| 3010 | $current_segment->end); | ||
| 3011 | |||
| 3012 | my %segments; | ||
| 3013 | |||
| 3014 | my $closure = sub { | ||
| 3015 | my ($refname,@ranges) = @_; | ||
| 3016 | |||
| 3017 | unless (exists $segments{$refname}) { | ||
| 3018 | my @segments = sort {$a->length<=>$b->length} # get the longest one | ||
| 3019 | map { | ||
| 3020 | eval{$_->absolute(0)}; $_ # so that rel2abs works properly later | ||
| 3021 | } | ||
| 3022 | $self->name2segments($refname,$db,TOO_MANY_SEGMENTS,1); | ||
| 3023 | $segments{$refname} = $segments[0]; | ||
| 3024 | return unless @segments; | ||
| 3025 | } | ||
| 3026 | |||
| 3027 | my $mapper = $segments{$refname} || return; | ||
| 3028 | my $absref = $mapper->abs_ref; | ||
| 3029 | my $cur_ref = eval {$current_segment->abs_ref} | ||
| 3030 | || eval{$current_segment->ref}; # account for api changes in Legacy::SeqI | ||
| 3031 | return unless $absref eq $cur_ref; | ||
| 3032 | |||
| 3033 | my @abs_segs; | ||
| 3034 | if ($absref eq $refname) { # doesn't need remapping | ||
| 3035 | @abs_segs = @ranges; | ||
| 3036 | } else { | ||
| 3037 | @abs_segs = map {[$mapper->rel2abs($_->[0],$_->[1])]} @ranges; | ||
| 3038 | } | ||
| 3039 | |||
| 3040 | # this inhibits mapping outside the displayed region | ||
| 3041 | if ($optimize) { | ||
| 3042 | my $in_window; | ||
| 3043 | foreach (@abs_segs) { | ||
| 3044 | next unless defined $_->[0] && defined $_->[1]; | ||
| 3045 | $in_window ||= $_->[0] <= $stop && $_->[1] >= $start; | ||
| 3046 | } | ||
| 3047 | return $in_window ? ($absref,@abs_segs) : (); | ||
| 3048 | } else { | ||
| 3049 | return ($absref,@abs_segs); | ||
| 3050 | } | ||
| 3051 | }; | ||
| 3052 | return $closure; | ||
| 3053 | } | ||
| 3054 | |||
| 3055 | sub plain_citation { | ||
| 3056 | my ($self,$label,$truncate) = @_; | ||
| 3057 | my $text = $self->citation($label,$self->language) || $self->tr('NO_CITATION'); | ||
| 3058 | $text =~ s/\<a/<span/gi; | ||
| 3059 | $text =~ s/\<\/a/\<\/span/gi; | ||
| 3060 | if ($truncate) { | ||
| 3061 | $text =~ s/^(.{$truncate}).+/$1\.\.\./; | ||
| 3062 | } | ||
| 3063 | CGI::escape($text); | ||
| 3064 | } | ||
| 3065 | |||
| 3066 | sub search_anchor { | ||
| 3067 | my $self = shift; | ||
| 3068 | my $anchor = shift; | ||
| 3069 | |||
| 3070 | return $self->{'search_anchor'} = $anchor if defined $anchor; | ||
| 3071 | return $self->{'search_anchor'}; | ||
| 3072 | } | ||
| 3073 | |||
| 3074 | |||
| 3075 | package Legacy::Graphics::BrowserConfig; | ||
| 3076 | use strict; | ||
| 3077 | use Bio::Graphics::FeatureFile; | ||
| 3078 | use Legacy::Graphics::Browser::Util 'shellwords'; | ||
| 3079 | use Carp 'croak'; | ||
| 3080 | use Socket; # for inet_aton() call | ||
| 3081 | |||
| 3082 | use vars '@ISA'; | ||
| 3083 | @ISA = 'Bio::Graphics::FeatureFile'; | ||
| 3084 | |||
| 3085 | sub labels { | ||
| 3086 | my $self = shift; | ||
| 3087 | |||
| 3088 | # Filter out all configured types that correspond to the overview, overview details | ||
| 3089 | # other non-track configuration and plugins, or other name:value types. | ||
| 3090 | # Apply restriction rules too | ||
| 3091 | my @labels = grep { | ||
| 3092 | !( $_ eq 'TRACK DEFAULTS' || # general track config | ||
| 3093 | $_ eq 'TOOLTIPS' || # ajax balloon config | ||
| 3094 | /SELECT MENU/ || # rubber-band selection menu config | ||
| 3095 | /:(\d+|plugin|DETAILS|details)$/ # plugin, etc config | ||
| 3096 | ) | ||
| 3097 | && $self->authorized($_) | ||
| 3098 | } | ||
| 3099 | $self->configured_types; | ||
| 3100 | return @labels; | ||
| 3101 | } | ||
| 3102 | |||
| 3103 | sub overview_tracks { | ||
| 3104 | my $self = shift; | ||
| 3105 | grep { ($_ eq 'overview' || /:overview$/) && $self->authorized($_) } $self->configured_types; | ||
| 3106 | } | ||
| 3107 | |||
| 3108 | sub regionview_tracks { | ||
| 3109 | my $self = shift; | ||
| 3110 | grep { ($_ eq 'region' || /:region$/) && $self->authorized($_) } $self->configured_types; | ||
| 3111 | } | ||
| 3112 | |||
| 3113 | # implement the "restrict" option | ||
| 3114 | sub authorized { | ||
| 3115 | my $self = shift; | ||
| 3116 | my $label = shift; | ||
| 3117 | my $restrict = $self->setting($label=>'restrict') | ||
| 3118 | || ($label ne 'general' && $self->setting('TRACK DEFAULTS' => 'restrict')); | ||
| 3119 | return 1 unless $restrict; | ||
| 3120 | my $host = CGI->remote_host; | ||
| 3121 | my $user = CGI->remote_user; | ||
| 3122 | my $addr = CGI->remote_addr; | ||
| 3123 | undef $host if $host eq $addr; | ||
| 3124 | return $restrict->($host,$addr,$user) if ref $restrict eq 'CODE'; | ||
| 3125 | my @tokens = split /\s*(satisfy|order|allow from|deny from|require user|require group|require valid-user)\s+/i,$restrict; | ||
| 3126 | shift @tokens unless $tokens[0] =~ /\S/; | ||
| 3127 | my $mode = 'allow,deny'; | ||
| 3128 | my $satisfy = 'all'; | ||
| 3129 | my (@allow,@deny,%users); | ||
| 3130 | while (@tokens) { | ||
| 3131 | my ($directive,$value) = splice(@tokens,0,2); | ||
| 3132 | $directive = lc $directive; | ||
| 3133 | $value ||= ''; | ||
| 3134 | if ($directive eq 'order') { | ||
| 3135 | $mode = $value; | ||
| 3136 | next; | ||
| 3137 | } | ||
| 3138 | my @values = split /[^\w.-]/,$value; | ||
| 3139 | if ($directive eq 'allow from') { | ||
| 3140 | push @allow,@values; | ||
| 3141 | next; | ||
| 3142 | } | ||
| 3143 | if ($directive eq 'deny from') { | ||
| 3144 | push @deny,@values; | ||
| 3145 | next; | ||
| 3146 | } | ||
| 3147 | if ($directive eq 'satisfy') { | ||
| 3148 | $satisfy = $value; | ||
| 3149 | next; | ||
| 3150 | } | ||
| 3151 | if ($directive eq 'require user') { | ||
| 3152 | foreach (@values) { | ||
| 3153 | if ($_ eq 'valid-user' && defined $user) { | ||
| 3154 | $users{$user}++; # ensures that this user will match | ||
| 3155 | } else { | ||
| 3156 | $users{$_}++; | ||
| 3157 | } | ||
| 3158 | } | ||
| 3159 | next; | ||
| 3160 | } | ||
| 3161 | if ($user && $directive eq 'require valid-user') { | ||
| 3162 | $users{$user}++; | ||
| 3163 | } | ||
| 3164 | if ($directive eq 'require group') { | ||
| 3165 | croak "Sorry, but gbrowse does not support the require group limit. Use a subroutine to implement role-based authentication."; | ||
| 3166 | } | ||
| 3167 | } | ||
| 3168 | |||
| 3169 | my $allow = $mode eq 'allow,deny' | ||
| 3170 | ? match_host(\@allow,$host,$addr) && !match_host(\@deny,$host,$addr) | ||
| 3171 | : 'deny,allow' | ||
| 3172 | ? !match_host(\@deny,$host,$addr) || match_host(\@allow,$host,$addr) | ||
| 3173 | : croak "$mode is not a valid authorization mode"; | ||
| 3174 | return $allow unless %users; | ||
| 3175 | $satisfy = 'any' if !@allow && !@deny; # no host restrictions | ||
| 3176 | |||
| 3177 | # prevent unint variable warnings | ||
| 3178 | $user ||= ''; | ||
| 3179 | $allow ||= ''; | ||
| 3180 | $users{$user} ||= ''; | ||
| 3181 | |||
| 3182 | return $satisfy eq 'any' ? $allow || $users{$user} | ||
| 3183 | : $allow && $users{$user}; | ||
| 3184 | } | ||
| 3185 | |||
| 3186 | sub match_host { | ||
| 3187 | my ($matches,$host,$addr) = @_; | ||
| 3188 | my $ok; | ||
| 3189 | for my $candidate (@$matches) { | ||
| 3190 | if ($candidate eq 'all') { | ||
| 3191 | $ok ||= 1; | ||
| 3192 | } elsif ($candidate =~ /^[\d.]+$/) { # ip match | ||
| 3193 | $addr .= '.' unless $addr =~ /\.$/; # these lines ensure subnets match correctly | ||
| 3194 | $candidate .= '.' unless $candidate =~ /\.$/; | ||
| 3195 | $ok ||= $addr =~ /^\Q$candidate\E/; | ||
| 3196 | } else { | ||
| 3197 | $host ||= gethostbyaddr(inet_aton($addr),AF_INET); | ||
| 3198 | next unless $host; | ||
| 3199 | $candidate = ".$candidate" unless $candidate =~ /^\./; # these lines ensure domains match correctly | ||
| 3200 | $host = ".$host" unless $host =~ /^\./; | ||
| 3201 | $ok ||= $host =~ /\Q$candidate\E$/; | ||
| 3202 | } | ||
| 3203 | return 1 if $ok; | ||
| 3204 | } | ||
| 3205 | $ok; | ||
| 3206 | } | ||
| 3207 | |||
| 3208 | sub label2type { | ||
| 3209 | my ($self,$label,$length) = @_; | ||
| 3210 | my $l = $self->semantic_label($label,$length); | ||
| 3211 | return Legacy::Graphics::Browser::Util::shellwords($self->setting($l,'feature')||$self->setting($label,'feature')||''); | ||
| 3212 | } | ||
| 3213 | |||
| 3214 | sub style { | ||
| 3215 | my ($self,$label,$length) = @_; | ||
| 3216 | my $l = $self->semantic_label($label,$length); | ||
| 3217 | return $l eq $label ? $self->SUPER::style($l) : ($self->SUPER::style($label),$self->SUPER::style($l)); | ||
| 3218 | } | ||
| 3219 | |||
| 3220 | # like setting, but obeys semantic hints | ||
| 3221 | sub semantic_setting { | ||
| 3222 | my ($self,$label,$option,$length) = @_; | ||
| 3223 | my $slabel = $self->semantic_label($label,$length); | ||
| 3224 | my $val = $self->setting($slabel => $option) if defined $slabel; | ||
| 3225 | return $val if defined $val; | ||
| 3226 | return $self->setting($label => $option); | ||
| 3227 | } | ||
| 3228 | |||
| 3229 | sub semantic_label { | ||
| 3230 | my ($self,$label,$length) = @_; | ||
| 3231 | return $label unless defined $length && $length > 0; | ||
| 3232 | # look for: | ||
| 3233 | # 1. a section like "Gene:100000" where the cutoff is less than the length of the segment | ||
| 3234 | # under display. | ||
| 3235 | # 2. a section like "Gene" which has no cutoff to use. | ||
| 3236 | if (my @lowres = map {[split ':']} | ||
| 3237 | grep {/$label:(\d+)/ && $1 <= $length} | ||
| 3238 | $self->configured_types) | ||
| 3239 | { | ||
| 3240 | ($label) = map {join ':',@$_} sort {$b->[1] <=> $a->[1]} @lowres; | ||
| 3241 | } | ||
| 3242 | $label | ||
| 3243 | } | ||
| 3244 | |||
| 3245 | # override inherited in order to be case insensitive | ||
| 3246 | # and to account for semantic zooming | ||
| 3247 | sub type2label { | ||
| 3248 | my $self = shift; | ||
| 3249 | my ($type,$length) = @_; | ||
| 3250 | $type ||= ''; | ||
| 3251 | $length ||= 0; | ||
| 3252 | |||
| 3253 | my @labels; | ||
| 3254 | |||
| 3255 | @labels = @{$self->{_type2labelmemo}{$type,$length}} | ||
| 3256 | if defined $self->{_type2labelmemo}{$type,$length}; | ||
| 3257 | |||
| 3258 | unless (@labels) { | ||
| 3259 | my @array = $self->SUPER::type2label(lc $type) or return; | ||
| 3260 | my %label_groups; | ||
| 3261 | for my $label (@array) { | ||
| 3262 | my ($label_base,$minlength) = $label =~ /([^:]+):(\d+)/; | ||
| 3263 | $label_base ||= $label; | ||
| 3264 | $minlength ||= 0; | ||
| 3265 | next if defined $length && $minlength > $length; | ||
| 3266 | $label_groups{$label_base}++; | ||
| 3267 | } | ||
| 3268 | @labels = keys %label_groups; | ||
| 3269 | $self->{_type2labelmemo}{$type,$length} = \@labels; | ||
| 3270 | } | ||
| 3271 | |||
| 3272 | return wantarray ? @labels : $labels[0]; | ||
| 3273 | } | ||
| 3274 | |||
| 3275 | # override inherited in order to allow for semantic zooming | ||
| 3276 | sub feature2label { | ||
| 3277 | my $self = shift; | ||
| 3278 | my ($feature,$length) = @_; | ||
| 3279 | my $type = eval {$feature->type} | ||
| 3280 | || eval{$feature->source_tag} || eval{$feature->primary_tag} or return; | ||
| 3281 | |||
| 3282 | (my $basetype = $type) =~ s/:.+$//; | ||
| 3283 | my @label = $self->type2label($type,$length); | ||
| 3284 | |||
| 3285 | # WARNING: if too many features start showing up in tracks, uncomment | ||
| 3286 | # the following line and comment the one after that. | ||
| 3287 | #@label = $self->type2label($basetype,$length) unless @label; | ||
| 3288 | push @label,$self->type2label($basetype,$length); | ||
| 3289 | |||
| 3290 | @label = ($type) unless @label; | ||
| 3291 | |||
| 3292 | # remove duplicate labels | ||
| 3293 | my %seen; | ||
| 3294 | @label = grep {! $seen{$_}++ } @label; | ||
| 3295 | |||
| 3296 | wantarray ? @label : $label[0]; | ||
| 3297 | } | ||
| 3298 | |||
| 3299 | sub invert_types { | ||
| 3300 | my $self = shift; | ||
| 3301 | my $config = $self->{config} or return; | ||
| 3302 | my %inverted; | ||
| 3303 | for my $label (keys %{$config}) { | ||
| 3304 | # next if $label=~/:?(overview|region)$/; # special case | ||
| 3305 | my $feature = $config->{$label}{'feature'} or next; | ||
| 3306 | foreach (Legacy::Graphics::Browser::Util::shellwords($feature||'')) { | ||
| 3307 | $inverted{lc $_}{$label}++; | ||
| 3308 | } | ||
| 3309 | } | ||
| 3310 | \%inverted; | ||
| 3311 | } | ||
| 3312 | |||
| 3313 | sub default_labels { | ||
| 3314 | my $self = shift; | ||
| 3315 | my $defaults = $self->setting('general'=>'default features'); | ||
| 3316 | return Legacy::Graphics::Browser::Util::shellwords($defaults||''); | ||
| 3317 | } | ||
| 3318 | |||
| 3319 | # return a hashref in which keys are the thresholds, and values are the list of | ||
| 3320 | # labels that should be displayed | ||
| 3321 | sub summary_mode { | ||
| 3322 | my $self = shift; | ||
| 3323 | my $summary = $self->settings(general=>'summary mode') or return {}; | ||
| 3324 | my %pairs = $summary =~ /(\d+)\s+{([^\}]+?)}/g; | ||
| 3325 | foreach (keys %pairs) { | ||
| 3326 | my @l = Legacy::Graphics::Browser::Util::shellwords($pairs{$_}||''); | ||
| 3327 | $pairs{$_} = \@l | ||
| 3328 | } | ||
| 3329 | \%pairs; | ||
| 3330 | } | ||
| 3331 | |||
| 3332 | # override make_link to allow for code references | ||
| 3333 | sub make_link { | ||
| 3334 | my $self = shift; | ||
| 3335 | my ($feature,$panel,$label,$data_source,$track) = @_; | ||
| 3336 | |||
| 3337 | $data_source ||= $self->source(); | ||
| 3338 | |||
| 3339 | if ($feature->can('url')) { | ||
| 3340 | my $link = $feature->url; | ||
| 3341 | return $link if defined $link; | ||
| 3342 | } | ||
| 3343 | |||
| 3344 | return $label->make_link($feature) | ||
| 3345 | if $label | ||
| 3346 | && $label =~ /^[a-zA-Z_]/ | ||
| 3347 | && $label->isa('Bio::Graphics::FeatureFile'); | ||
| 3348 | |||
| 3349 | $panel ||= 'Bio::Graphics::Panel'; | ||
| 3350 | $label ||= $self->feature2label($feature); | ||
| 3351 | |||
| 3352 | # most specific -- a configuration line | ||
| 3353 | my $link = $self->setting($label,'link'); | ||
| 3354 | |||
| 3355 | # less specific - a smart feature | ||
| 3356 | $link = $feature->make_link if $feature->can('make_link') && !defined $link; | ||
| 3357 | |||
| 3358 | # general defaults | ||
| 3359 | $link = $self->setting('TRACK DEFAULTS'=>'link') unless defined $link; | ||
| 3360 | $link = $self->setting(general=>'link') unless defined $link; | ||
| 3361 | |||
| 3362 | |||
| 3363 | return unless $link; | ||
| 3364 | |||
| 3365 | if (ref($link) eq 'CODE') { | ||
| 3366 | my $val = eval {$link->($feature,$panel,$track)}; | ||
| 3367 | $self->_callback_complain($label=>'link') if $@; | ||
| 3368 | return $val; | ||
| 3369 | } | ||
| 3370 | elsif (!$link || $link eq 'AUTO') { | ||
| 3371 | my $n = $feature->display_name; | ||
| 3372 | unless (defined $n) { | ||
| 3373 | my @aliases = eval {$feature->attributes('Alias')},eval{$feature->load_id},eval{$feature->primary_id}; | ||
| 3374 | $n = $aliases[0]; | ||
| 3375 | } | ||
| 3376 | my $c = $feature->seq_id; | ||
| 3377 | my $name = CGI::escape("$n"); # workaround CGI.pm bug | ||
| 3378 | my $class = eval {CGI::escape($feature->class)}||''; | ||
| 3379 | my $ref = CGI::escape("$c"); # workaround again | ||
| 3380 | my $start = CGI::escape($feature->start); | ||
| 3381 | my $end = CGI::escape($feature->end); | ||
| 3382 | my $src = CGI::escape($feature->can('source_tag') ? $feature->source_tag : ''); | ||
| 3383 | my $f_id = $feature->can('feature_id') ? CGI::escape($feature->feature_id) | ||
| 3384 | :$feature->can('primary_id') ? CGI::escape($feature->primary_id) | ||
| 3385 | :$feature->can('primary_key') ? CGI::escape($feature->primary_key) | ||
| 3386 | :undef; | ||
| 3387 | |||
| 3388 | my $url = "../../gbrowse_details/$data_source?name=$name;class=$class;ref=$ref;start=$start;end=$end"; | ||
| 3389 | if (defined $f_id) { | ||
| 3390 | return $url . ";feature_id=$f_id"; | ||
| 3391 | } | ||
| 3392 | else { | ||
| 3393 | return $url; | ||
| 3394 | } | ||
| 3395 | } | ||
| 3396 | |||
| 3397 | return $self->link_pattern($link,$feature,$panel); | ||
| 3398 | } | ||
| 3399 | |||
| 3400 | # make the title for an object on a clickable imagemap | ||
| 3401 | sub make_title { | ||
| 3402 | my $self = shift; | ||
| 3403 | my ($feature,$panel,$label,$track) = @_; | ||
| 3404 | local $^W = 0; # tired of uninitialized variable warnings | ||
| 3405 | |||
| 3406 | my ($title,$key) = ('',''); | ||
| 3407 | |||
| 3408 | TRY: { | ||
| 3409 | if ($label | ||
| 3410 | && $label =~ /^[a-zA-Z_]/ | ||
| 3411 | && $label->isa('Bio::Graphics::FeatureFile')) { | ||
| 3412 | $key = $label->name; | ||
| 3413 | $key =~ s/^(http|ftp)://; | ||
| 3414 | $title = $label->make_title($feature) or last TRY; | ||
| 3415 | return $title; | ||
| 3416 | } | ||
| 3417 | |||
| 3418 | else { | ||
| 3419 | $label ||= $self->feature2label($feature) or last TRY; | ||
| 3420 | $key ||= $self->setting($label,'key') || $label; | ||
| 3421 | $key =~ s/s$//; | ||
| 3422 | $key = "(". | ||
| 3423 | $feature->segment->dsn.")" if $feature->isa('Legacy::Das::Feature'); # for DAS sources | ||
| 3424 | $key =~ s/^(http|ftp)://; | ||
| 3425 | |||
| 3426 | my $link = $self->setting($label,'title') | ||
| 3427 | || $self->setting('TRACK DEFAULTS'=>'title') | ||
| 3428 | || $self->setting(general=>'title'); | ||
| 3429 | if (defined $link && ref($link) eq 'CODE') { | ||
| 3430 | $title = eval {$link->($feature,$panel,$track)}; | ||
| 3431 | $self->_callback_complain($label=>'title') if $@; | ||
| 3432 | return $title if defined $title; | ||
| 3433 | } return $self->link_pattern($link,$feature) if $link && $link ne 'AUTO'; | ||
| 3434 | } | ||
| 3435 | } | ||
| 3436 | |||
| 3437 | # otherwise, try it ourselves | ||
| 3438 | $title = eval { | ||
| 3439 | if ($feature->can('target') && (my $target = $feature->target)) { | ||
| 3440 | join (' ', | ||
| 3441 | "$key:", | ||
| 3442 | $feature->seq_id.':'. | ||
| 3443 | $feature->start."..".$feature->end, | ||
| 3444 | $feature->target->seq_id.':'. | ||
| 3445 | $feature->target->start."..".$feature->target->end); | ||
| 3446 | } else { | ||
| 3447 | my ($start,$end) = ($feature->start,$feature->end); | ||
| 3448 | ($start,$end) = ($end,$start) if $feature->strand < 0; | ||
| 3449 | join(' ', | ||
| 3450 | "$key:", | ||
| 3451 | $feature->can('display_name') ? $feature->display_name : $feature->info, | ||
| 3452 | ($feature->can('seq_id') ? $feature->seq_id : $feature->location->seq_id) | ||
| 3453 | .":". | ||
| 3454 | (defined $start ? $start : '?')."..".(defined $end ? $end : '?') | ||
| 3455 | ); | ||
| 3456 | } | ||
| 3457 | }; | ||
| 3458 | warn $@ if $@; | ||
| 3459 | |||
| 3460 | return $title; | ||
| 3461 | } | ||
| 3462 | |||
| 3463 | sub balloon_tip_setting { | ||
| 3464 | my $self = shift; | ||
| 3465 | my ($option,$label,$feature,$panel,$track) = @_; | ||
| 3466 | $option ||= 'balloon tip'; | ||
| 3467 | my $value; | ||
| 3468 | |||
| 3469 | |||
| 3470 | TRY: { | ||
| 3471 | if ($label | ||
| 3472 | && $label =~ /^[a-zA-Z_]/ | ||
| 3473 | && $label->isa('Bio::Graphics::FeatureFile')) { # a feature file | ||
| 3474 | $value ||= $label->setting($_=>$option) foreach $label->feature2label($feature); | ||
| 3475 | } | ||
| 3476 | last TRY if $value; | ||
| 3477 | |||
| 3478 | for $label ($label, 'TRACK DEFAULTS','general') { | ||
| 3479 | $value = $self->setting($label=>$option); | ||
| 3480 | last TRY if $value; | ||
| 3481 | } | ||
| 3482 | } | ||
| 3483 | |||
| 3484 | return unless $value; | ||
| 3485 | my $val; | ||
| 3486 | my $balloon_type = 'balloon'; | ||
| 3487 | |||
| 3488 | if (ref($value) eq 'CODE') { | ||
| 3489 | $val = eval {$value->($feature,$panel,$track)}; | ||
| 3490 | $self->_callback_complain($label=>$option) if $@; | ||
| 3491 | } | ||
| 3492 | # catch callbacks for custom balloons | ||
| 3493 | elsif (my($text,$callback) = $value =~ /^(.+?)(sub\s*(\(\$\$\))*\s*\{.+)/) { | ||
| 3494 | my $package = $self->base2package; | ||
| 3495 | my $coderef = eval "package $package; $callback"; | ||
| 3496 | $self->_callback_complain($label,$option) if $@; | ||
| 3497 | my $callback_text = $coderef->($feature,$panel,$track); | ||
| 3498 | $val = join(' ',$text,$callback_text); | ||
| 3499 | } | ||
| 3500 | else { | ||
| 3501 | $val = $self->link_pattern($value,$feature,$panel); | ||
| 3502 | } | ||
| 3503 | |||
| 3504 | if ($val=~ /^\s*\[([\w\s]+)\]\s+(.+)/s) { | ||
| 3505 | $balloon_type = $1; | ||
| 3506 | $val = $2; | ||
| 3507 | } | ||
| 3508 | # escape quotes | ||
| 3509 | $val =~ s/\'/\\'/g; | ||
| 3510 | $val =~ s/"/\"/g; | ||
| 3511 | |||
| 3512 | return ($balloon_type,$val); | ||
| 3513 | } | ||
| 3514 | |||
| 3515 | sub make_link_target { | ||
| 3516 | my $self = shift; | ||
| 3517 | my ($feature,$panel,$label,$track) = @_; | ||
| 3518 | |||
| 3519 | if ($feature->isa('Legacy::Das::Feature')) { # new window | ||
| 3520 | my $dsn = $feature->segment->dsn; | ||
| 3521 | $dsn =~ s/^.+\///; | ||
| 3522 | return $dsn; | ||
| 3523 | } | ||
| 3524 | |||
| 3525 | $label ||= $self->feature2label($feature) or return; | ||
| 3526 | my $link_target = $self->setting($label,'link_target') | ||
| 3527 | || $self->setting('TRACK DEFAULTS' => 'link_target') | ||
| 3528 | || $self->setting(general => 'link_target'); | ||
| 3529 | $link_target = eval {$link_target->($feature,$panel,$track)} | ||
| 3530 | if ref($link_target) eq 'CODE'; | ||
| 3531 | $self->_callback_complain($label=>'link_target') if $@; | ||
| 3532 | return $link_target; | ||
| 3533 | } | ||
| 3534 | |||
| 3535 | sub default_style { | ||
| 3536 | my $self = shift; | ||
| 3537 | return $self->SUPER::style('TRACK DEFAULTS'); | ||
| 3538 | } | ||
| 3539 | |||
| 3540 | # return language-specific options | ||
| 3541 | sub i18n_style { | ||
| 3542 | my $self = shift; | ||
| 3543 | my ($label,$lang,$length) = @_; | ||
| 3544 | return $self->style($label,$length) unless $lang; | ||
| 3545 | |||
| 3546 | my $charset = $lang->tr('CHARSET'); | ||
| 3547 | |||
| 3548 | # GD can't handle non-ASCII/LATIN scripts transparently | ||
| 3549 | return $self->style($label,$length) | ||
| 3550 | if $charset && $charset !~ /^(us-ascii|iso-8859)/i; | ||
| 3551 | |||
| 3552 | my @languages = $lang->language; | ||
| 3553 | |||
| 3554 | push @languages,''; | ||
| 3555 | # ('fr_CA','fr','en_BR','en','') | ||
| 3556 | |||
| 3557 | my $idx = 1; | ||
| 3558 | my %priority = map {$_=>$idx++} @languages; | ||
| 3559 | # ('fr-ca'=>1, 'fr'=>2, 'en-br'=>3, 'en'=>4, ''=>5) | ||
| 3560 | |||
| 3561 | my %options = $self->style($label,$length); | ||
| 3562 | my %lang_options = map { $_->[1] => $options{$_->[0]} } | ||
| 3563 | sort { $b->[2]<=>$a->[2] } | ||
| 3564 | map { my ($option,undef,$lang) = /^(-[^:]+)(:(\w+))?$/; [$_ => $option, $priority{$lang||''}||99] } | ||
| 3565 | keys %options; | ||
| 3566 | %lang_options; | ||
| 3567 | } | ||
| 3568 | |||
| 3569 | 1; | ||
| 3570 | |||
| 3571 | __END__ |