| 1 | package Bio::Graphics::Browser2::DataSource; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use base 'Bio::Graphics::Browser2::AuthorizedFeatureFile'; | ||
| 6 | |||
| 7 | use Bio::Graphics::Browser2::Shellwords; | ||
| 8 | use Bio::Graphics::Browser2::Util 'modperl_request'; | ||
| 9 | use Bio::Graphics::Browser2::DataBase; | ||
| 10 | use Bio::DB::SeqFeature::Store::Alias; | ||
| 11 | use File::Basename 'dirname'; | ||
| 12 | use File::Path 'mkpath'; | ||
| 13 | use File::Spec; | ||
| 14 | use Data::Dumper 'Dumper'; | ||
| 15 | use Digest::MD5 'md5_hex'; | ||
| 16 | use Carp 'croak'; | ||
| 17 | use CGI 'pre'; | ||
| 18 | use Storable 'lock_store','lock_retrieve'; | ||
| 19 | |||
| 20 | my %CONFIG_CACHE; # cache parsed config files | ||
| 21 | my %DB_SETTINGS; # cache database settings | ||
| 22 | my %LOADED_GLYPHS; | ||
| 23 | my $SQLITE_FIXED; | ||
| 24 | |||
| 25 | BEGIN { | ||
| 26 | if( $ENV{MOD_PERL} && | ||
| 27 | exists $ENV{MOD_PERL_API_VERSION} && | ||
| 28 | $ENV{MOD_PERL_API_VERSION} >= 2) { | ||
| 29 | require Apache2::SubRequest; | ||
| 30 | require Apache2::RequestUtil; | ||
| 31 | require Apache2::ServerUtil; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | =head1 DESCRIPTION | ||
| 38 | |||
| 39 | =head2 METHODS | ||
| 40 | |||
| 41 | =over 4 | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | sub new { | ||
| 46 | my $class = shift; | ||
| 47 | my $config_file_path = shift; | ||
| 48 | my ($name,$description,$globals) = @_; | ||
| 49 | |||
| 50 | # we expire what's in the config file path if the global | ||
| 51 | # modification time of the path has changed | ||
| 52 | |||
| 53 | my $cache_age = time() - ($CONFIG_CACHE{$config_file_path}{ctime}||0); | ||
| 54 | |||
| 55 | # this code caches the config info so that we don't need to | ||
| 56 | # reparse in persistent (e.g. modperl) environment | ||
| 57 | my $mtime = $class->file_mtime($config_file_path); | ||
| 58 | if (exists $CONFIG_CACHE{$config_file_path}{mtime} | ||
| 59 | && $CONFIG_CACHE{$config_file_path}{mtime} >= $mtime) { | ||
| 60 | my $object = $CONFIG_CACHE{$config_file_path}{object}; | ||
| 61 | $object->clear_cached_dbids; | ||
| 62 | $object->clear_usertracks; | ||
| 63 | return $object; | ||
| 64 | } | ||
| 65 | |||
| 66 | my @args = (-file=>$config_file_path,-safe=>1); | ||
| 67 | my $self = $class->can('new_from_cache') && !$ENV{GBROWSE_NOCACHE} && ($config_file_path !~ /\|$/) | ||
| 68 | ? $class->new_from_cache(@args) | ||
| 69 | : $class->_new(@args); | ||
| 70 | |||
| 71 | $self->name($name); | ||
| 72 | $self->description($description); | ||
| 73 | $self->globals($globals); | ||
| 74 | $self->dir(dirname($config_file_path)); | ||
| 75 | $self->config_file($config_file_path); | ||
| 76 | $self->add_scale_tracks(); | ||
| 77 | $self->precompile_glyphs; | ||
| 78 | |||
| 79 | # this generates the invert_types data structure which will then get cached to disk | ||
| 80 | $self->type2label('foo',0,'bar'); | ||
| 81 | |||
| 82 | $CONFIG_CACHE{$config_file_path}{object} = $self; | ||
| 83 | $CONFIG_CACHE{$config_file_path}{mtime} = $mtime; | ||
| 84 | $CONFIG_CACHE{$config_file_path}{ctime} = time(); | ||
| 85 | return $self; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub name { | ||
| 89 | my $self = shift; | ||
| 90 | my $d = $self->{name}; | ||
| 91 | $self->{name} = shift if @_; | ||
| 92 | $d; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub description { | ||
| 96 | my $self = shift; | ||
| 97 | my $d = $self->{description}; | ||
| 98 | $self->{description} = shift if @_; | ||
| 99 | $d; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub dir { | ||
| 103 | my $self = shift; | ||
| 104 | my $d = $self->{dir}; | ||
| 105 | $self->{dir} = shift if @_; | ||
| 106 | $d; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub config_file { | ||
| 110 | my $self = shift; | ||
| 111 | my $d = $self->{config_file}; | ||
| 112 | $self->{config_file} = shift if @_; | ||
| 113 | $d; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub add_conf_files { | ||
| 117 | my $self = shift; | ||
| 118 | my $expression = shift; | ||
| 119 | while (my $conf = glob($expression)) { | ||
| 120 | $self->parse_file($conf); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | sub globals { | ||
| 125 | my $self = shift; | ||
| 126 | my $d = $self->{globals}; | ||
| 127 | $self->{globals} = shift if @_; | ||
| 128 | $d; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub clear_cached_dbids { | ||
| 132 | my $self = shift; | ||
| 133 | delete $self->{feature2dbid}; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub precompile_glyphs { | ||
| 137 | my $self = shift; | ||
| 138 | for my $l ($self->labels) { | ||
| 139 | my $glyph = $self->setting($l=>'glyph'); | ||
| 140 | next unless $glyph; | ||
| 141 | next if ref($glyph) eq 'CODE'; | ||
| 142 | next if $LOADED_GLYPHS{$glyph}++; | ||
| 143 | eval "use Bio::Graphics::Glyph::$glyph" unless | ||
| 144 | "Bio::Graphics::Glyph\:\:$glyph"->can('new'); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | sub clear_cached_config { | ||
| 149 | my $self = shift; | ||
| 150 | delete $CONFIG_CACHE{$self->config_file}; | ||
| 151 | } | ||
| 152 | |||
| 153 | =head2 userdata() | ||
| 154 | |||
| 155 | $path = $source->userdata(@path_components) | ||
| 156 | |||
| 157 | Returns a path to somewhere in the tmp file system for the | ||
| 158 | indicated userdata. | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | sub userdata { | ||
| 163 | my $self = shift; | ||
| 164 | my @path = @_; | ||
| 165 | my $globals = $self->globals; | ||
| 166 | return $globals->user_dir($self->name,@path); | ||
| 167 | } | ||
| 168 | |||
| 169 | sub admindata { | ||
| 170 | my $self = shift; | ||
| 171 | my @path = @_; | ||
| 172 | my $globals = $self->globals; | ||
| 173 | return $self->userdata(@path) unless $globals->admin_dbs; | ||
| 174 | return $globals->admin_dir($self->name,@path); | ||
| 175 | } | ||
| 176 | |||
| 177 | sub taxon_id { | ||
| 178 | my $self = shift; | ||
| 179 | my $meta = $self->metadata or return; | ||
| 180 | return $meta->{taxid}; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub build_id { | ||
| 184 | my $self = shift; | ||
| 185 | my $meta = $self->metadata or return; | ||
| 186 | my $authority = $meta->{authority} or return; | ||
| 187 | my $version = $meta->{coordinates_version} or return; | ||
| 188 | return "$authority$version"; | ||
| 189 | } | ||
| 190 | |||
| 191 | sub species { | ||
| 192 | my $self = shift; | ||
| 193 | my $meta = $self->metadata or return; | ||
| 194 | return $meta->{species}; | ||
| 195 | } | ||
| 196 | |||
| 197 | =head2 global_setting() | ||
| 198 | |||
| 199 | $setting = $source->global_setting('option') | ||
| 200 | |||
| 201 | Like code_setting() except that it is only for 'general' options. If the | ||
| 202 | option is not found in the datasource config file, then looks in the | ||
| 203 | global file. | ||
| 204 | |||
| 205 | =cut | ||
| 206 | |||
| 207 | sub global_setting { | ||
| 208 | my $self = shift; | ||
| 209 | my $option = shift; | ||
| 210 | my $value = $self->code_setting(general=>$option); | ||
| 211 | return $value if defined $value; | ||
| 212 | my $globals = $self->globals or return; | ||
| 213 | return $globals->code_setting(general=>$option); | ||
| 214 | } | ||
| 215 | |||
| 216 | # format for time can be in any of the forms... | ||
| 217 | # "now" -- 0 seconds | ||
| 218 | # "+180s" -- in 180 seconds | ||
| 219 | # "+2m" -- in 2 minutes | ||
| 220 | # "+12h" -- in 12 hours | ||
| 221 | # "+1d" -- in 1 day | ||
| 222 | # "+3M" -- in 3 months | ||
| 223 | # "+2y" -- in 2 years | ||
| 224 | # "-3m" -- 3 minutes ago(!) | ||
| 225 | # If you don't supply one of these forms, we assume you are | ||
| 226 | # specifying the date yourself | ||
| 227 | |||
| 228 | sub global_time { | ||
| 229 | my $self = shift; | ||
| 230 | my $option = shift; | ||
| 231 | |||
| 232 | my $time = $self->global_setting($option); | ||
| 233 | return unless defined($time); | ||
| 234 | return $self->globals->time2sec($time); | ||
| 235 | } | ||
| 236 | |||
| 237 | sub cache_time { | ||
| 238 | my $self = shift; | ||
| 239 | if (@_) { | ||
| 240 | $self->{cache_time} = shift; | ||
| 241 | } | ||
| 242 | return $self->{cache_time} if exists $self->{cache_time}; | ||
| 243 | my $globals = $self->globals; | ||
| 244 | my $ct = $globals->time2sec($globals->cache_time); | ||
| 245 | return $self->{cache_time} = $ct; | ||
| 246 | } | ||
| 247 | |||
| 248 | # this method is for compatibility with some plugins | ||
| 249 | sub config { | ||
| 250 | my $self = shift; | ||
| 251 | return $self; | ||
| 252 | } | ||
| 253 | |||
| 254 | sub must_authenticate { | ||
| 255 | my $self = shift; | ||
| 256 | my $restrict = $self->global_setting('restrict') || | ||
| 257 | $self->globals->data_source_restrict($self->name) | ||
| 258 | or return; | ||
| 259 | return $restrict =~ /require\s+(valid-user|user|group)/; | ||
| 260 | } | ||
| 261 | |||
| 262 | sub auth_plugin { shift->globals->auth_plugin } | ||
| 263 | |||
| 264 | sub details_multiplier { | ||
| 265 | my $self = shift; | ||
| 266 | my $state = shift; | ||
| 267 | |||
| 268 | my $value = $self->global_setting('details multiplier') || 1; | ||
| 269 | $value = 1 if ($value < 1); #lower limit | ||
| 270 | $value = 25 if ($value > 25); #set upper limit for performance reasons (prevent massive image files) | ||
| 271 | |||
| 272 | return $value unless $state; | ||
| 273 | foreach (qw(seg_min seg_max view_start view_stop)) { | ||
| 274 | return $value unless defined $state->{$_}; | ||
| 275 | } | ||
| 276 | |||
| 277 | my $max_length = $state->{seg_max} - $state->{seg_min}; | ||
| 278 | my $request_length = $state->{view_stop} - $state->{view_start}; | ||
| 279 | if (($max_length > 0) && ($request_length > 0) && (($request_length * $value) > $max_length)) { | ||
| 280 | return $max_length / $request_length; #limit details multiplier so that region does not go out of bounds | ||
| 281 | } | ||
| 282 | |||
| 283 | return $value; | ||
| 284 | } | ||
| 285 | |||
| 286 | sub unit_label { | ||
| 287 | my $self = shift; | ||
| 288 | my $value = shift || 0; | ||
| 289 | |||
| 290 | my $unit = $self->setting('units') || 'bp'; | ||
| 291 | my $divider = $self->setting('unit_divider') || 1; | ||
| 292 | $value /= $divider; | ||
| 293 | my $abs = abs($value); | ||
| 294 | |||
| 295 | my $label; | ||
| 296 | $label = $unit =~ /^[kcM]/ ? sprintf("%.4g %s",$value,$unit) | ||
| 297 | : $abs >= 1e9 ? sprintf("%.4g G%s",$value/1e9,$unit) | ||
| 298 | : $abs >= 1e6 ? sprintf("%.4g M%s",$value/1e6,$unit) | ||
| 299 | : $abs >= 1e3 ? sprintf("%.4g k%s",$value/1e3,$unit) | ||
| 300 | : $abs >= 1 ? sprintf("%.4g %s", $value, $unit) | ||
| 301 | : $abs == 0 ? sprintf("%.4g %s", $value, $unit) | ||
| 302 | : $abs >= 1e-2 ? sprintf("%.4g c%s",$value*100,$unit) | ||
| 303 | : $abs >= 1e-3 ? sprintf("%.4g m%s",$value*1e3,$unit) | ||
| 304 | : $abs >= 1e-6 ? sprintf("%.4g u%s",$value*1e6,$unit) | ||
| 305 | : $abs >= 1e-9 ? sprintf("%.4g n%s",$value*1e9,$unit) | ||
| 306 | : sprintf("%.4g p%s",$value*1e12,$unit); | ||
| 307 | if (wantarray) { | ||
| 308 | return split ' ',$label; | ||
| 309 | } else { | ||
| 310 | return $label; | ||
| 311 | } | ||
| 312 | } | ||
| 313 | |||
| 314 | sub commas { | ||
| 315 | my $self = shift; | ||
| 316 | my $i = shift; | ||
| 317 | return $i if $i=~ /\D/; | ||
| 318 | $i = reverse $i; | ||
| 319 | $i =~ s/(\d{3})/$1,/g; | ||
| 320 | chop $i if $i=~/,$/; | ||
| 321 | $i = reverse $i; | ||
| 322 | $i; | ||
| 323 | } | ||
| 324 | |||
| 325 | #copy over from Render.pm to provide wider availability? | ||
| 326 | #sub overview_ratio { | ||
| 327 | # my $self = shift; | ||
| 328 | # return 1.0; # for now | ||
| 329 | #} | ||
| 330 | |||
| 331 | sub overview_bgcolor { shift->global_setting('overview bgcolor') } | ||
| 332 | sub detailed_bgcolor { shift->global_setting('detailed bgcolor') } | ||
| 333 | sub key_bgcolor { shift->global_setting('key bgcolor') } | ||
| 334 | sub image_widths { shellwords(shift->global_setting('image widths')) } | ||
| 335 | sub default_width { shift->global_setting('default width') } | ||
| 336 | |||
| 337 | sub head_html { shift->global_setting('head') } | ||
| 338 | sub header_html { shift->global_setting('header') } | ||
| 339 | sub footer_html { shift->global_setting('footer') } | ||
| 340 | sub html1 { shift->global_setting('html1') } | ||
| 341 | sub html2 { shift->global_setting('html2') } | ||
| 342 | sub html3 { shift->global_setting('html3') } | ||
| 343 | sub html4 { shift->global_setting('html4') } | ||
| 344 | sub html5 { shift->global_setting('html5') } | ||
| 345 | sub html6 { shift->global_setting('html6') } | ||
| 346 | |||
| 347 | sub max_segment { shift->global_setting('max segment') } | ||
| 348 | sub default_segment { shift->global_setting('max segment') } | ||
| 349 | sub min_overview_pad { shift->global_setting('min overview pad') || 10 } | ||
| 350 | |||
| 351 | sub too_many_landmarks { shift->global_setting('too many landmarks') || 100 } | ||
| 352 | |||
| 353 | sub plugins { shellwords(shift->global_setting('plugins')) } | ||
| 354 | |||
| 355 | sub remote_renderer { | ||
| 356 | my $self = shift; | ||
| 357 | my $label = shift; | ||
| 358 | my $url = $self->fallback_setting($label => 'remote renderer'); | ||
| 359 | return $url if defined $url; | ||
| 360 | return $self->global_setting('remote renderer'); | ||
| 361 | } | ||
| 362 | |||
| 363 | sub labels { | ||
| 364 | my $self = shift; | ||
| 365 | |||
| 366 | # filter out all configured types that correspond to the overview, overview details | ||
| 367 | # plugins, or other name:value types | ||
| 368 | my @labels = grep { | ||
| 369 | !( $_ eq 'TRACK DEFAULTS' # general track config | ||
| 370 | || $_ eq 'TOOLTIPS' # ajax balloon config | ||
| 371 | || /SELECT MENU/ # rubberband selection menu config | ||
| 372 | || /:(\d+|plugin|DETAILS|details|database)$/) # plugin, etc config | ||
| 373 | } $self->configured_types; | ||
| 374 | |||
| 375 | # apply restriction rules | ||
| 376 | return grep { $self->authorized($_)} @labels; | ||
| 377 | } | ||
| 378 | |||
| 379 | sub detail_tracks { | ||
| 380 | my $self = shift; | ||
| 381 | return grep { !/:.+$/ } $self->labels; | ||
| 382 | } | ||
| 383 | |||
| 384 | sub overview_tracks { | ||
| 385 | my $self = shift; | ||
| 386 | grep { ($_ eq 'overview' || /:overview$/ && !/^_/) && $self->authorized($_) } $self->configured_types; | ||
| 387 | } | ||
| 388 | |||
| 389 | sub regionview_tracks { | ||
| 390 | my $self = shift; | ||
| 391 | grep { ($_ eq 'region' || /:region$/) && !/^_/ && $self->authorized($_) } $self->configured_types; | ||
| 392 | } | ||
| 393 | |||
| 394 | sub karyotype_tracks { | ||
| 395 | my $self = shift; | ||
| 396 | grep { ($_ eq 'karyotype' || /:karyotype$/) && $self->authorized($_) } $self->configured_types; | ||
| 397 | } | ||
| 398 | |||
| 399 | sub plugin_tracks { | ||
| 400 | my $self = shift; | ||
| 401 | grep { ($_ eq 'plugin' || m!plugin:!) && $self->authorized($_) } $self->configured_types; | ||
| 402 | } | ||
| 403 | |||
| 404 | sub label2type { | ||
| 405 | my ($self,$label,$length) = @_; | ||
| 406 | my $l = $self->semantic_label($label,$length); | ||
| 407 | return shellwords($self->setting($l,'feature')||$self->setting($label,'feature')||''); | ||
| 408 | } | ||
| 409 | |||
| 410 | sub track_listing_class { | ||
| 411 | my $self = shift; | ||
| 412 | my $style = lc($self->global_setting('track listing style') || 'categories'); | ||
| 413 | my $subclass = $style eq 'categories' ? 'Categories' | ||
| 414 | : $style eq 'facets' ? 'Facets' | ||
| 415 | : die "invalid track listing style '$style'"; | ||
| 416 | return 'Bio::Graphics::Browser2::Render::HTML::TrackListing::'.$subclass; | ||
| 417 | } | ||
| 418 | |||
| 419 | sub seqid_prefix { shift->fallback_setting(general=>'seqid_prefix') } | ||
| 420 | |||
| 421 | sub default_style { | ||
| 422 | my $self = shift; | ||
| 423 | return $self->SUPER::style('TRACK DEFAULTS'); | ||
| 424 | } | ||
| 425 | |||
| 426 | sub summary_style { | ||
| 427 | my $self = shift; | ||
| 428 | my $type = shift; | ||
| 429 | $type .= ":summary"; | ||
| 430 | |||
| 431 | local $self->{config} = $self->{_user_tracks}{config} | ||
| 432 | if exists $self->{_user_tracks}{config}{$type}; | ||
| 433 | |||
| 434 | my $config = $self->{config} or return; | ||
| 435 | my $hashref = $config->{$type}; | ||
| 436 | unless ($hashref) { | ||
| 437 | $hashref = $config->{$type} or return; | ||
| 438 | } | ||
| 439 | return map {("-$_" => $hashref->{$_})} keys %$hashref; | ||
| 440 | } | ||
| 441 | |||
| 442 | sub style { | ||
| 443 | my ($self,$label,$length) = @_; | ||
| 444 | return map {$self->user_style($_)} $self->semantic_labels($label,$length); | ||
| 445 | } | ||
| 446 | |||
| 447 | # return language-specific options | ||
| 448 | sub i18n_style { | ||
| 449 | my $self = shift; | ||
| 450 | my ($label,$lang,$length) = @_; | ||
| 451 | |||
| 452 | return $self->style($label,$length) unless $lang; | ||
| 453 | |||
| 454 | my $charset = $lang->tr('CHARSET'); | ||
| 455 | |||
| 456 | # GD can't handle non-ASCII/LATIN scripts transparently | ||
| 457 | return $self->style($label,$length) | ||
| 458 | if $charset && $charset !~ /^(us-ascii|iso-8859)/i; | ||
| 459 | |||
| 460 | my @languages = $lang->language; | ||
| 461 | |||
| 462 | push @languages,''; | ||
| 463 | # ('fr_CA','fr','en_BR','en','') | ||
| 464 | |||
| 465 | my $idx = 1; | ||
| 466 | my %priority = map {$_=>$idx++} @languages; | ||
| 467 | # ('fr-ca'=>1, 'fr'=>2, 'en-br'=>3, 'en'=>4, ''=>5) | ||
| 468 | |||
| 469 | my %options = $self->style($label,$length); | ||
| 470 | my %lang_options = map { $_->[1] => $options{$_->[0]} } | ||
| 471 | sort { $b->[2]<=>$a->[2] } | ||
| 472 | map { my ($option,undef,$lang) = | ||
| 473 | /^(-[^:]+)(:(\w+))?$/; [$_ => $option, $priority{$lang||''}||99] } | ||
| 474 | keys %options; | ||
| 475 | %lang_options; | ||
| 476 | } | ||
| 477 | |||
| 478 | # return true if we should show a coverage summary for this | ||
| 479 | # track at the current length | ||
| 480 | sub show_summary { | ||
| 481 | my $self = shift; | ||
| 482 | my ($label,$length,$settings) = @_; | ||
| 483 | |||
| 484 | $settings ||= {}; | ||
| 485 | return 0 if defined $settings->{features}{$label}{summary_mode_len} && | ||
| 486 | !$settings->{features}{$label}{summary_mode_len}; | ||
| 487 | my $c = $settings->{features}{$label}{summary_mode_len} | ||
| 488 | || $self->semantic_fallback_setting($label=>'show summary',$length); | ||
| 489 | my $g = $self->semantic_fallback_setting($label=>'glyph',$length); | ||
| 490 | my $class = 'Bio::Graphics::Glyph::'.$g; | ||
| 491 | eval "require $class" unless $class->can('new'); | ||
| 492 | return 0 if $class->isa('Bio::Graphics::Glyph::xyplot') | ||
| 493 | or $class->isa('Bio::Graphics::Glyph::minmax'); | ||
| 494 | return 0 if $self->semantic_fallback_setting($label=>'global feature',$length); | ||
| 495 | return 0 unless $c; | ||
| 496 | |||
| 497 | my $section = $self->get_section_from_label($label) || 'detail'; | ||
| 498 | |||
| 499 | $c =~ s/_//g; | ||
| 500 | return 0 unless $c <= $length; | ||
| 501 | my $db = $self->open_database($label,$length) or return; | ||
| 502 | return 0 unless $db->can('feature_summary'); | ||
| 503 | return 1; | ||
| 504 | } | ||
| 505 | |||
| 506 | sub can_summarize { | ||
| 507 | my $self = shift; | ||
| 508 | my $label = shift; | ||
| 509 | my $g = $self->fallback_setting($label=>'glyph'); | ||
| 510 | return if $g =~ /wiggle|xyplot|density/; # don't summarize wiggles or xyplots | ||
| 511 | my $db = $self->open_database($label) or return; | ||
| 512 | return unless $db->can('feature_summary'); | ||
| 513 | } | ||
| 514 | |||
| 515 | sub clear_usertracks { | ||
| 516 | my $self = shift; | ||
| 517 | delete $self->{_user_tracks}; | ||
| 518 | } | ||
| 519 | |||
| 520 | sub usertrack_labels { | ||
| 521 | my $self = shift; | ||
| 522 | return keys %{$self->{_user_tracks}{config}}; | ||
| 523 | } | ||
| 524 | |||
| 525 | sub is_usertrack { | ||
| 526 | my $self = shift; | ||
| 527 | my $label = shift; | ||
| 528 | return exists $self->{_user_tracks}{config}{$label}; | ||
| 529 | } | ||
| 530 | |||
| 531 | sub is_remotetrack { | ||
| 532 | my $self = shift; | ||
| 533 | my $label = shift; | ||
| 534 | (my $base = $label) =~ s/:\w+$//; | ||
| 535 | return exists $self->{config}{$base}{'remote feature'} | ||
| 536 | || exists $self->{config}{$label}{'remote feature'}; | ||
| 537 | } | ||
| 538 | |||
| 539 | sub category_open { | ||
| 540 | my $self = shift; | ||
| 541 | my $categories = $self->setting('category state') or return {}; | ||
| 542 | my %settings = shellwords($categories); | ||
| 543 | my %c; | ||
| 544 | while (my ($key,$value) = each %settings) { | ||
| 545 | $key .= "_section" unless $key =~ /_section$/; | ||
| 546 | $value = 1 if $value eq 'open'; | ||
| 547 | $value = 0 if $value eq 'closed'; | ||
| 548 | $c{$key} = $value; | ||
| 549 | } | ||
| 550 | return \%c; | ||
| 551 | } | ||
| 552 | |||
| 553 | sub category_default { | ||
| 554 | my $self = shift; | ||
| 555 | my $open = $self->setting('category default state') || 'open'; | ||
| 556 | return $open eq 'open' ? 1 : 0; | ||
| 557 | } | ||
| 558 | |||
| 559 | sub user_style { | ||
| 560 | my $self = shift; | ||
| 561 | my $type = shift; | ||
| 562 | local $self->{config} = $self->{_user_tracks}{config} | ||
| 563 | if exists $self->{_user_tracks}{config}{$type}; | ||
| 564 | return $self->SUPER::style($type); | ||
| 565 | } | ||
| 566 | |||
| 567 | # like setting, but falls back to 'track defaults' and then to 'general' | ||
| 568 | sub fallback_setting { | ||
| 569 | my $self = shift; | ||
| 570 | my ($label,$option,@rest) = @_; | ||
| 571 | my $setting = $self->SUPER::setting($label,$option,@rest); | ||
| 572 | return $setting if defined $setting; | ||
| 573 | $setting = $self->SUPER::setting('TRACK DEFAULTS',$option,@rest); | ||
| 574 | return $setting if defined $setting; | ||
| 575 | $setting = $self->SUPER::setting('general',$option,@rest); | ||
| 576 | return $setting; | ||
| 577 | } | ||
| 578 | |||
| 579 | sub plugin_setting { | ||
| 580 | my $self = shift; | ||
| 581 | my $caller_package = caller(); | ||
| 582 | my ($last_name) = $caller_package =~ /(\w+)$/; | ||
| 583 | my $option_name = "${last_name}:plugin"; | ||
| 584 | return $self->label_options($option_name) unless @_; | ||
| 585 | return $self->setting($option_name => @_); | ||
| 586 | } | ||
| 587 | |||
| 588 | sub karyotype_setting { | ||
| 589 | my $self = shift; | ||
| 590 | my $caller_package = caller(); | ||
| 591 | $self->setting('builtin:karyotype' => @_); | ||
| 592 | } | ||
| 593 | |||
| 594 | # like code_setting, but obeys semantic hints | ||
| 595 | sub semantic_setting { | ||
| 596 | my ($self,$label,$option,$length) = @_; | ||
| 597 | my @labels = reverse $self->semantic_labels($label,$length); | ||
| 598 | foreach (@labels) { | ||
| 599 | my $val = $self->code_setting($_ => $option); | ||
| 600 | return $val if defined $val; | ||
| 601 | } | ||
| 602 | return $self->code_setting($label=>$option); | ||
| 603 | } | ||
| 604 | |||
| 605 | sub semantic_thresholds { | ||
| 606 | my ($self,$label) = @_; | ||
| 607 | my @l = 0; | ||
| 608 | foreach ($self->configured_types) { | ||
| 609 | next unless /^$label:(\d+)/; | ||
| 610 | push @l,$1; | ||
| 611 | } | ||
| 612 | |||
| 613 | @l; | ||
| 614 | } | ||
| 615 | |||
| 616 | sub semantic_labels { | ||
| 617 | my ($self,$label,$length) = @_; | ||
| 618 | return $label unless defined $length && $length > 0; | ||
| 619 | if (my @lowres = map {[split ':']} | ||
| 620 | grep {/^$label:(\d+)/ && $1 <= $length} | ||
| 621 | $self->configured_types) { | ||
| 622 | return $label,map {join ':',@$_} sort {$a->[-1] <=> $b->[-1]} @lowres; | ||
| 623 | } | ||
| 624 | return $label | ||
| 625 | } | ||
| 626 | |||
| 627 | sub semantic_label { | ||
| 628 | my ($self,$label,$length) = @_; | ||
| 629 | return $label unless defined $length && $length > 0; | ||
| 630 | |||
| 631 | # look for: | ||
| 632 | # 1. a section like "Gene:100000" where the cutoff is <= the length of the segment | ||
| 633 | # under display. | ||
| 634 | # 2. a section like "Gene" which has no cutoff to use. | ||
| 635 | if (my @lowres = map {[split ':']} | ||
| 636 | grep {/^$label:(\d+)/ && $1 <= $length} | ||
| 637 | $self->configured_types) | ||
| 638 | { | ||
| 639 | ($label) = map {join ':',@$_} sort {$b->[-1] <=> $a->[-1]} @lowres; | ||
| 640 | } | ||
| 641 | $label | ||
| 642 | } | ||
| 643 | |||
| 644 | sub semantic_fallback_setting { | ||
| 645 | my $self = shift; | ||
| 646 | my ($label,$option,$length) = @_; | ||
| 647 | my $setting = $self->semantic_setting($label,$option,$length); | ||
| 648 | return $setting if defined $setting; | ||
| 649 | return $self->fallback_setting($label,$option); | ||
| 650 | } | ||
| 651 | |||
| 652 | sub button_url { | ||
| 653 | my $self = shift; | ||
| 654 | my $globals = $self->globals; | ||
| 655 | my $path = $self->global_setting('buttons'); | ||
| 656 | return $globals->resolve_path($path,'url'); | ||
| 657 | } | ||
| 658 | |||
| 659 | =head2 $section_setting = $data_source->section_setting($section_name) | ||
| 660 | |||
| 661 | Returns "open" "closed" or "off" for the named section. Named sections are: | ||
| 662 | |||
| 663 | instructions | ||
| 664 | search | ||
| 665 | overview | ||
| 666 | details | ||
| 667 | tracks | ||
| 668 | display | ||
| 669 | upload_tracks | ||
| 670 | |||
| 671 | =cut | ||
| 672 | |||
| 673 | sub section_setting { | ||
| 674 | my $self = shift; | ||
| 675 | my $section = shift; | ||
| 676 | my $config_setting = "\L$section\E section"; | ||
| 677 | my $s = $self->global_setting($config_setting); | ||
| 678 | return 'open' unless defined $s; | ||
| 679 | return $s; | ||
| 680 | } | ||
| 681 | |||
| 682 | sub show_section { # one of instructions, upload_tracks, search, overview, region, detail, tracks, or display_settings | ||
| 683 | my $self = shift; | ||
| 684 | my $setting = $self->section_setting(@_); | ||
| 685 | return $setting eq 'hide' || $setting eq 'off' ? 0 : 1; | ||
| 686 | } | ||
| 687 | |||
| 688 | sub unit_divider { | ||
| 689 | my $self = shift; | ||
| 690 | return $self->global_setting('unit_divider') || 1; | ||
| 691 | } | ||
| 692 | |||
| 693 | sub get_ranges { | ||
| 694 | my $self = shift; | ||
| 695 | my $divisor = $self->unit_divider; | ||
| 696 | my $rangestr = $self->global_setting('zoom levels') || '100 1000 10000 100000 1000000 10000000'; | ||
| 697 | if ($divisor == 1 ) { | ||
| 698 | return split /\s+/,$rangestr; | ||
| 699 | } else { | ||
| 700 | return map {$_ * $divisor} split /\s+/,$rangestr; | ||
| 701 | } | ||
| 702 | } | ||
| 703 | |||
| 704 | # override inherited in order to be case insensitive | ||
| 705 | # and to account for semantic zooming | ||
| 706 | sub type2label { | ||
| 707 | my $self = shift; | ||
| 708 | my ($type,$length,$dbid) = @_; | ||
| 709 | $dbid ||= ''; | ||
| 710 | $type ||= ''; | ||
| 711 | $length ||= 0; | ||
| 712 | $type = lc $type; | ||
| 713 | $dbid =~ s/:database//; | ||
| 714 | |||
| 715 | if (exists $self->{_type2label}{$type,$length,$dbid}) { | ||
| 716 | my @labels = @{$self->{_type2label}{$type,$length,$dbid}}; | ||
| 717 | return wantarray ? @labels : $labels[0]; | ||
| 718 | } | ||
| 719 | |||
| 720 | my %labels; | ||
| 721 | my $main_inverted = $self->invert_types($self->{config}); | ||
| 722 | my $user_inverted = $self->invert_types($self->{_user_tracks}{config}); | ||
| 723 | |||
| 724 | for my $i ($main_inverted,$user_inverted) { | ||
| 725 | my @lengths = grep {$length >= $_} keys %{$i->{$type}{$dbid}}; | ||
| 726 | $labels{$_}++ foreach map {keys %{$i->{$type}{$dbid}{$_}}} @lengths; | ||
| 727 | } | ||
| 728 | my @labels = keys %labels; | ||
| 729 | $self->{_type2label}{$type,$length,$dbid} = \@labels; #cache in memory | ||
| 730 | return wantarray ? @labels : $labels[0]; | ||
| 731 | } | ||
| 732 | |||
| 733 | # override inherited in order to allow for semantic zooming | ||
| 734 | sub feature2label { | ||
| 735 | my $self = shift; | ||
| 736 | my ($feature,$length) = @_; | ||
| 737 | my $type = eval {$feature->type} | ||
| 738 | || eval{$feature->source_tag} || eval{$feature->primary_tag} or return; | ||
| 739 | my $dbid = eval{$feature->gbrowse_dbid}; | ||
| 740 | |||
| 741 | (my $basetype = $type) =~ s/:.+$//; | ||
| 742 | my %label; | ||
| 743 | |||
| 744 | $label{$_}++ foreach $self->type2label($type,$length,$dbid); | ||
| 745 | $label{$_}++ foreach $self->type2label($basetype,$length,$dbid); | ||
| 746 | |||
| 747 | my @label = keys %label; | ||
| 748 | wantarray ? @label : $label[0]; | ||
| 749 | } | ||
| 750 | |||
| 751 | sub invert_types { | ||
| 752 | my $self = shift; | ||
| 753 | my $config = shift; | ||
| 754 | return unless $config; | ||
| 755 | |||
| 756 | my $keys = md5_hex(keys %$config); | ||
| 757 | |||
| 758 | # check in-memory cache | ||
| 759 | if (exists $self->{_inverted}{$keys}) { | ||
| 760 | return $self->{_inverted}{$keys}; | ||
| 761 | } | ||
| 762 | |||
| 763 | # check for a valid cache file | ||
| 764 | (my $cache_name = $self->config_file) =~ s!/!_!g; | ||
| 765 | my $master_cache = $self->cachefile($cache_name); # inherited | ||
| 766 | $master_cache or die "no bio::graphics cache file found at $master_cache? Shouldn't happen.."; | ||
| 767 | |||
| 768 | my $inv_path = $master_cache . ".${keys}.inverted"; | ||
| 769 | if (-e $inv_path && -M $master_cache >= -M $inv_path) { | ||
| 770 | return $self->{_inverted}{$keys} = lock_retrieve($inv_path); | ||
| 771 | } | ||
| 772 | |||
| 773 | my $multiplier = $self->global_setting('details multiplier') || 1; | ||
| 774 | |||
| 775 | my %inverted; | ||
| 776 | for my $label (sort keys %{$config}) { | ||
| 777 | my $length = 0; | ||
| 778 | if ($label =~ /^(.+):(\d+)$/) { | ||
| 779 | $label = $1; | ||
| 780 | $length = $2; | ||
| 781 | } | ||
| 782 | |||
| 783 | my $section = $self->get_section_from_label($label) || 'detail'; | ||
| 784 | $length *= $multiplier if $section eq 'detail'; | ||
| 785 | |||
| 786 | my $feature = $self->semantic_setting($label => 'feature',$length) or next; | ||
| 787 | my ($dbid) = $self->db_settings($label => $length) or next; | ||
| 788 | $dbid =~ s/:database$//; | ||
| 789 | foreach (shellwords($feature||'')) { | ||
| 790 | $inverted{lc $_}{$dbid}{$length}{$label}++; | ||
| 791 | } | ||
| 792 | } | ||
| 793 | lock_store(\%inverted,$inv_path); | ||
| 794 | return $self->{_inverted}{$keys} = \%inverted; | ||
| 795 | } | ||
| 796 | |||
| 797 | sub default_labels { | ||
| 798 | my $self = shift; | ||
| 799 | my $defaults = $self->setting('general'=>'default tracks'); | ||
| 800 | $defaults ||= $self->setting('general'=>'default features'); # backward compatibility | ||
| 801 | return $self->scale_tracks,shellwords($defaults||''); | ||
| 802 | } | ||
| 803 | |||
| 804 | sub metadata { | ||
| 805 | my $self = shift; | ||
| 806 | return $self->{metadata} if exists $self->{metadata}; | ||
| 807 | |||
| 808 | my $metadata = $self->fallback_setting(general => 'metadata'); | ||
| 809 | return unless $metadata; | ||
| 810 | |||
| 811 | my %a = $metadata =~ m/-(\w+)\s+([^-].*?(?= -[a-z]|$))/g; | ||
| 812 | |||
| 813 | my %metadata; | ||
| 814 | for (keys %a) { | ||
| 815 | $a{$_} =~ s/\s+$// ; | ||
| 816 | $metadata{lc $_} = $a{$_}; | ||
| 817 | }; # trim | ||
| 818 | |||
| 819 | return $self->{metadata} = \%metadata; | ||
| 820 | } | ||
| 821 | |||
| 822 | =head2 add_scale_tracks() | ||
| 823 | |||
| 824 | This is called at initialization time to add track configs | ||
| 825 | for the automatic "scale" (arrow) tracks for details, overview and regionview | ||
| 826 | |||
| 827 | =cut | ||
| 828 | |||
| 829 | sub add_scale_tracks { | ||
| 830 | my $self = shift; | ||
| 831 | my @scale_tracks = $self->scale_tracks; | ||
| 832 | |||
| 833 | for my $label (@scale_tracks) { | ||
| 834 | $self->add_type($label,{ | ||
| 835 | 'global feature' => 1, | ||
| 836 | glyph => 'arrow', | ||
| 837 | fgcolor => 'black', | ||
| 838 | double => 1, | ||
| 839 | tick => 2, | ||
| 840 | label => 1, | ||
| 841 | key => '', | ||
| 842 | }); | ||
| 843 | } | ||
| 844 | # Sort of a bug here. We want the scale tracks to start out on the | ||
| 845 | # top of the others. But add_type puts the labels on the bottom. So | ||
| 846 | # we reorder so these guys come first. This breaks encapsulation. | ||
| 847 | my @types = @{$self->{types}}; | ||
| 848 | my $items_to_reorder = @scale_tracks; | ||
| 849 | my @items = splice(@types,-$items_to_reorder); | ||
| 850 | |||
| 851 | splice(@types,0,0,@items); | ||
| 852 | $self->{types} = \@types; | ||
| 853 | } | ||
| 854 | |||
| 855 | sub scale_tracks { return qw(_scale _scale:overview _scale:region); } | ||
| 856 | |||
| 857 | # return a hashref in which keys are the thresholds, and values are the list of | ||
| 858 | # labels that should be displayed | ||
| 859 | sub summary_mode { | ||
| 860 | my $self = shift; | ||
| 861 | my $summary = $self->settings(general=>'summary mode') or return {}; | ||
| 862 | my %pairs = $summary =~ /(\d+)\s+{([^\}]+?)}/g; | ||
| 863 | foreach (keys %pairs) { | ||
| 864 | my @l = shellwords($pairs{$_}||''); | ||
| 865 | $pairs{$_} = \@l | ||
| 866 | } | ||
| 867 | \%pairs; | ||
| 868 | } | ||
| 869 | |||
| 870 | sub make_link { | ||
| 871 | croak "Do not call make_link() on the DataSource. Call it on the Render object"; | ||
| 872 | } | ||
| 873 | |||
| 874 | =item $db = $dsn->databases | ||
| 875 | |||
| 876 | Return all named databases from [name:database] tracks. | ||
| 877 | |||
| 878 | =cut | ||
| 879 | |||
| 880 | sub databases { | ||
| 881 | my $self = shift; | ||
| 882 | my @dbs = map {s/:database//; $_ } grep {/:database$/} $self->configured_types; | ||
| 883 | return @dbs; | ||
| 884 | } | ||
| 885 | |||
| 886 | =item ($adaptor,@argv) = $dsn->db2args('databasename') | ||
| 887 | |||
| 888 | Given a database named by ['databasename':database], return its | ||
| 889 | adaptor and arguments. | ||
| 890 | |||
| 891 | =cut | ||
| 892 | |||
| 893 | sub db2args { | ||
| 894 | my $self = shift; | ||
| 895 | my $dbname = shift; | ||
| 896 | return $self->db_settings("$dbname:database"); | ||
| 897 | } | ||
| 898 | |||
| 899 | =item ($dbid,$adaptor,@argv) = $dsn->db_settings('track_label') | ||
| 900 | |||
| 901 | Return the adaptor and arguments suitable for the database identified | ||
| 902 | by the given track label. If no track label is given then the | ||
| 903 | "general" default database is used. | ||
| 904 | |||
| 905 | =cut | ||
| 906 | |||
| 907 | # get the db settings for a track or from [general] | ||
| 908 | sub db_settings { | ||
| 909 | my $self = shift; | ||
| 910 | my $track = shift; | ||
| 911 | my $length= shift; | ||
| 912 | |||
| 913 | $track ||= 'general'; | ||
| 914 | |||
| 915 | if ($track =~ /(.+):(\d+)$/) { # in the case that we get called with foo:499 | ||
| 916 | $track = $1; | ||
| 917 | $length = $2; | ||
| 918 | } | ||
| 919 | |||
| 920 | # caching to avoid calling setting() too many times | ||
| 921 | my $semantic_label = $self->semantic_label($track,$length); | ||
| 922 | |||
| 923 | return @{$DB_SETTINGS{$self,$semantic_label}} if defined $DB_SETTINGS{$self,$semantic_label}; | ||
| 924 | |||
| 925 | my $symbolic_db_name; | ||
| 926 | |||
| 927 | if ($track =~ /:database$/) { | ||
| 928 | $symbolic_db_name = $track | ||
| 929 | } elsif (my $d = $self->semantic_fallback_setting($track => 'database', $length)) { | ||
| 930 | $symbolic_db_name = "$d:database"; | ||
| 931 | } elsif ($self->semantic_setting($track=>'db_adaptor',$length)) { | ||
| 932 | $symbolic_db_name = $track; | ||
| 933 | } elsif ($self->semantic_fallback_setting($track => 'db_adaptor', $length)) { | ||
| 934 | $symbolic_db_name = 'general'; | ||
| 935 | } | ||
| 936 | |||
| 937 | if (defined $DB_SETTINGS{$self,$symbolic_db_name}) { | ||
| 938 | return @{$DB_SETTINGS{$self,$semantic_label} ||= $DB_SETTINGS{$self,$symbolic_db_name}}; | ||
| 939 | } | ||
| 940 | |||
| 941 | my $adaptor = $self->semantic_setting($symbolic_db_name => 'db_adaptor', $length); | ||
| 942 | unless ($adaptor) { | ||
| 943 | warn "Unknown database defined for $track"; | ||
| 944 | return; | ||
| 945 | } | ||
| 946 | eval "require $adaptor; 1" or die $@; | ||
| 947 | |||
| 948 | my $args = $self->semantic_fallback_setting($symbolic_db_name => 'db_args', $length); | ||
| 949 | my @argv = ref $args eq 'CODE' | ||
| 950 | ? $args->() | ||
| 951 | : shellwords($args||''); | ||
| 952 | |||
| 953 | # Do environment substitutions in the args. Assume that the environment is safe. | ||
| 954 | foreach (@argv) { | ||
| 955 | s/\$ENV{(\w+)}/$ENV{$1}||''/ge; | ||
| 956 | s/\$HTDOCS/Bio::Graphics::Browser2->htdocs_base/ge; | ||
| 957 | s/\$DB/Bio::Graphics::Browser2->db_base/ge; | ||
| 958 | s/\$CONF/Bio::Graphics::Browser2->config_base/ge; | ||
| 959 | s/\$ROOT/Bio::Graphics::Browser2->url_base/ge; | ||
| 960 | } | ||
| 961 | |||
| 962 | if (defined (my $a = | ||
| 963 | $self->semantic_setting($symbolic_db_name => 'aggregators',$length))) { | ||
| 964 | my @aggregators = shellwords($a||''); | ||
| 965 | push @argv,(-aggregator => \@aggregators); | ||
| 966 | } | ||
| 967 | |||
| 968 | # uniquify dbids | ||
| 969 | my $key = Dumper($adaptor,@argv); | ||
| 970 | $self->{arg2dbid}{$key} ||= $symbolic_db_name; | ||
| 971 | |||
| 972 | my @result = ($self->{arg2dbid}{$key},$adaptor,@argv); | ||
| 973 | |||
| 974 | # cache settings | ||
| 975 | $DB_SETTINGS{$self,$symbolic_db_name} ||= \@result; | ||
| 976 | $DB_SETTINGS{$self,$semantic_label } ||= \@result; | ||
| 977 | |||
| 978 | return @result; | ||
| 979 | } | ||
| 980 | |||
| 981 | =item $db = $dsn->open_database('track') | ||
| 982 | |||
| 983 | Return the database handle specified by the given track label or | ||
| 984 | 'general' if not given. The databases are cached and so it is ok to | ||
| 985 | call repeatedly. | ||
| 986 | |||
| 987 | =cut | ||
| 988 | |||
| 989 | sub open_database { | ||
| 990 | my $self = shift; | ||
| 991 | my $track = shift; | ||
| 992 | my $length = shift; | ||
| 993 | |||
| 994 | $track ||= 'general'; | ||
| 995 | |||
| 996 | my ($dbid,$adaptor,@argv) = $self->db_settings($track,$length); | ||
| 997 | my $db = Bio::Graphics::Browser2::DataBase->open_database($adaptor,@argv) or return; | ||
| 998 | |||
| 999 | $self->fix_sqlite if $adaptor eq 'Bio::DB::SeqFeature::Store' && "@argv" =~ /SQLite/; # work around broken bioperl | ||
| 1000 | |||
| 1001 | # do a little extra stuff the first time we see a new database | ||
| 1002 | unless ($self->{databases_seen}{$db}++) { | ||
| 1003 | my $refclass = $self->setting('reference class'); | ||
| 1004 | $db->default_class($refclass) if $refclass && $db->can('default_class'); | ||
| 1005 | $db->strict_bounds_checking(1) if $db->can('strict_bounds_checking'); | ||
| 1006 | $db->absolute(1) if $db->can('absolute'); | ||
| 1007 | |||
| 1008 | unless ($track eq 'general') { | ||
| 1009 | my $default = $self->open_database(); # I hope we don't get into a loop here | ||
| 1010 | $db->dna_accessor($default) if $default ne $db && $db->can('dna_accessor'); | ||
| 1011 | } | ||
| 1012 | |||
| 1013 | } | ||
| 1014 | |||
| 1015 | # add extra semantic data if requested | ||
| 1016 | if ($track ne 'general' && (my $metadata = $self->semantic_setting($track,'metadata',$length||0))) { | ||
| 1017 | eval "require Bio::DB::SeqFeature::Store::Alias; 1;" | ||
| 1018 | unless Bio::DB::SeqFeature::Store::Alias->can('new'); | ||
| 1019 | my @feature_types = shellwords($self->semantic_setting($track,'feature',$length||0)); | ||
| 1020 | $db = Bio::DB::SeqFeature::Store::Alias->new(-store => $db, | ||
| 1021 | -metadata => $metadata, | ||
| 1022 | -type => $feature_types[0]); | ||
| 1023 | } | ||
| 1024 | |||
| 1025 | # remember mapping of this database to this track | ||
| 1026 | $self->{db2track}{$db}{$dbid}++; | ||
| 1027 | |||
| 1028 | return $db; | ||
| 1029 | } | ||
| 1030 | |||
| 1031 | sub default_dbid { | ||
| 1032 | my $self = shift; | ||
| 1033 | return $self->db2id($self->open_database); | ||
| 1034 | } | ||
| 1035 | |||
| 1036 | sub search_options { | ||
| 1037 | my $self = shift; | ||
| 1038 | my $dbid = shift; | ||
| 1039 | return $self->setting($dbid => 'search options') | ||
| 1040 | || $self->setting($dbid => 'search_options') | ||
| 1041 | || 'default'; | ||
| 1042 | } | ||
| 1043 | |||
| 1044 | sub exclude_types { | ||
| 1045 | my $self = shift; | ||
| 1046 | my $dbid = shift; | ||
| 1047 | return shellwords($self->setting($dbid => 'exclude types')||''); | ||
| 1048 | } | ||
| 1049 | |||
| 1050 | =item @ids = $dsn->db2id($db) | ||
| 1051 | |||
| 1052 | =item $dbid = $dsn->db2id($db) | ||
| 1053 | |||
| 1054 | Given a database handle, return all dbids that correspond to that | ||
| 1055 | database. In a scalar context, returns just the first dbid that uses | ||
| 1056 | it. It is less confusing to call in a scalar context. | ||
| 1057 | |||
| 1058 | =cut | ||
| 1059 | |||
| 1060 | sub db2id { | ||
| 1061 | my $self = shift; | ||
| 1062 | my $db = shift; | ||
| 1063 | my @tracks = keys %{$self->{db2track}{$db}} or return; | ||
| 1064 | my (@symbolic,@general,@rest); | ||
| 1065 | for (@tracks) { | ||
| 1066 | if (/:database$/i) { | ||
| 1067 | push @symbolic,$_; | ||
| 1068 | } elsif (lc $_ eq 'general') { | ||
| 1069 | push @general,$_; | ||
| 1070 | } else { | ||
| 1071 | push @rest,$_; | ||
| 1072 | } | ||
| 1073 | } | ||
| 1074 | @tracks = (@symbolic,@general,@rest); # triage | ||
| 1075 | return wantarray ? @tracks : $tracks[0]; | ||
| 1076 | } | ||
| 1077 | |||
| 1078 | =item @ids = $dsn->dbs | ||
| 1079 | |||
| 1080 | Return the names of all the sections that define dbs in the datasource: both track sections | ||
| 1081 | that define dbargs and named database sections. Note that we do not | ||
| 1082 | uniquify the case in which the same adaptor and dbargs are defined twice in two different | ||
| 1083 | sections. | ||
| 1084 | |||
| 1085 | =cut | ||
| 1086 | |||
| 1087 | sub dbs { | ||
| 1088 | my $self = shift; | ||
| 1089 | my @labels = ('general',$self->configured_types); | ||
| 1090 | my %named = map {$_=>1} grep {/:database/} @labels; | ||
| 1091 | my @anonymous = grep {!$named{$_} && $self->setting($_=>'db_adaptor')} @labels; | ||
| 1092 | return (keys %named,@anonymous); | ||
| 1093 | } | ||
| 1094 | |||
| 1095 | # this is an aggregator-aware way of retrieving all the named types | ||
| 1096 | sub _all_types { | ||
| 1097 | my $self = shift; | ||
| 1098 | my $db = shift; | ||
| 1099 | return $self->{_all_types} if exists $self->{_all_types}; # memoize | ||
| 1100 | my %types = map {$_=>1} ( | ||
| 1101 | (map {$_->get_method} eval {$db->aggregators}), | ||
| 1102 | (map {$self->label2type($_)} $self->labels) | ||
| 1103 | ); | ||
| 1104 | return $self->{_all_types} = \%types; | ||
| 1105 | } | ||
| 1106 | |||
| 1107 | =item $dsn->clear_cache | ||
| 1108 | |||
| 1109 | Empty out our cache of database settings and fetch anew from config file | ||
| 1110 | |||
| 1111 | =cut | ||
| 1112 | |||
| 1113 | sub clear_cache { | ||
| 1114 | my $self = shift; | ||
| 1115 | %DB_SETTINGS = (); | ||
| 1116 | } | ||
| 1117 | |||
| 1118 | =head2 generate_image | ||
| 1119 | |||
| 1120 | ($url,$path) = generate_image($gd); | ||
| 1121 | |||
| 1122 | Given a GD::Image object, this method calls its png() or gif() methods | ||
| 1123 | (depending on GD version), stores the output into the temporary | ||
| 1124 | "images" subdirectory of the directory given by the "tmp_base" option | ||
| 1125 | in the configuration file. It returns a two element list consisting of | ||
| 1126 | the URL to the image and the physical path of the image. | ||
| 1127 | |||
| 1128 | =cut | ||
| 1129 | |||
| 1130 | sub generate_image { | ||
| 1131 | my $self = shift; | ||
| 1132 | my $image = shift; | ||
| 1133 | |||
| 1134 | if ($self->global_setting('truecolor') | ||
| 1135 | && $image->can('saveAlpha')) { | ||
| 1136 | $image->trueColor(1); | ||
| 1137 | $image->saveAlpha(1); | ||
| 1138 | } | ||
| 1139 | |||
| 1140 | my $extension = $image->can('png') ? 'png' : 'gif'; | ||
| 1141 | my $data = $image->can('png') ? $image->png : $image->gif; | ||
| 1142 | my $signature = md5_hex($data); | ||
| 1143 | |||
| 1144 | # untaint signature for use in open | ||
| 1145 | $signature =~ /^([0-9A-Fa-f]+)$/g or return; | ||
| 1146 | $signature = $1; | ||
| 1147 | |||
| 1148 | my $self_name = $self->name; | ||
| 1149 | $self_name =~ s/\\/_/g; # Because Safari and Chrome translate backslash to forward slash | ||
| 1150 | my $path = $self->globals->tmpimage_dir($self_name); | ||
| 1151 | |||
| 1152 | my $image_url = $self->globals->image_url; | ||
| 1153 | my $url = sprintf("%s/%s/%s.%s",$image_url,$self_name,$signature,$extension); | ||
| 1154 | my $imagefile = sprintf("%s/%s.%s",$path,$signature,$extension); | ||
| 1155 | open (my $f,'>',$imagefile) | ||
| 1156 | || die("Can't open image file $imagefile for writing: $!\n"); | ||
| 1157 | binmode($f); | ||
| 1158 | print $f $data; | ||
| 1159 | close $f; | ||
| 1160 | return $url; | ||
| 1161 | } | ||
| 1162 | |||
| 1163 | =head2 $source->add_dbid_to_feature($feature,$dbid_hashref) | ||
| 1164 | |||
| 1165 | This adds a new method called gbrowse_dbid() to a feature. Do not call | ||
| 1166 | it if the method is already in the feature's class. The hashref should | ||
| 1167 | be populated by feature memory locations (overload::StrVal($feature)) | ||
| 1168 | as keys and database symbolic IDs as values. | ||
| 1169 | |||
| 1170 | =cut | ||
| 1171 | |||
| 1172 | sub add_dbid_to_feature { | ||
| 1173 | my $self = shift; | ||
| 1174 | my ($feature,$dbid) = @_; | ||
| 1175 | return unless $feature; | ||
| 1176 | |||
| 1177 | no strict 'refs'; | ||
| 1178 | |||
| 1179 | if ($feature->isa('HASH')) { | ||
| 1180 | $feature->{__gbrowse_dbid} = $dbid; | ||
| 1181 | my $class = ref $feature; | ||
| 1182 | return if $class->can('gbrowse_dbid'); | ||
| 1183 | my $method = sub { | ||
| 1184 | my $f = shift; | ||
| 1185 | return $f->{__gbrowse_dbid}; | ||
| 1186 | }; | ||
| 1187 | *{"${class}::gbrowse_dbid"} = $method; | ||
| 1188 | } | ||
| 1189 | |||
| 1190 | else { | ||
| 1191 | $self->{feature2dbid}{overload::StrVal($feature)} = $dbid; | ||
| 1192 | my $class = ref $feature; | ||
| 1193 | return if $class->can('gbrowse_dbid'); | ||
| 1194 | my $method = sub { my $f = shift; | ||
| 1195 | return $self->{feature2dbid}{overload::StrVal($f)} | ||
| 1196 | }; | ||
| 1197 | *{"${class}::gbrowse_dbid"} = $method; | ||
| 1198 | } | ||
| 1199 | } | ||
| 1200 | |||
| 1201 | |||
| 1202 | =head2 @labels = $source->data_source_to_label(@data_sources) | ||
| 1203 | |||
| 1204 | Search through all stanzas for those with a matching "data source" | ||
| 1205 | option. Data sources look like this: | ||
| 1206 | |||
| 1207 | [stanzaLabel1] | ||
| 1208 | data source = FlyBase | ||
| 1209 | |||
| 1210 | [stanzaLabel2] | ||
| 1211 | data source = FlyBase | ||
| 1212 | |||
| 1213 | Now searching for $source->data_source_to_label('FlyBase') will return | ||
| 1214 | "stanzaLabel1" and "stanzaLabel2" along with others that match. A | ||
| 1215 | track may have several data sources, separated by spaces. | ||
| 1216 | |||
| 1217 | =cut | ||
| 1218 | |||
| 1219 | |||
| 1220 | sub data_source_to_label { | ||
| 1221 | my $self = shift; | ||
| 1222 | return $self->_secondary_key_to_label('data source',@_); | ||
| 1223 | } | ||
| 1224 | |||
| 1225 | =head2 @labels = $source->track_source_to_label(@track_sources) | ||
| 1226 | |||
| 1227 | Search through all stanzas for those with a matching "track source" | ||
| 1228 | option. Track sources look like this: | ||
| 1229 | |||
| 1230 | [stanzaLabel] | ||
| 1231 | track source = UCSC EBI NCBI | ||
| 1232 | |||
| 1233 | Now searching for $source->track_source_to_label('UCSC','EBI') will | ||
| 1234 | return "stanzaLabel" along with others that match. A track may have | ||
| 1235 | several space-delimited track sources. | ||
| 1236 | |||
| 1237 | =cut | ||
| 1238 | sub track_source_to_label { | ||
| 1239 | my $self = shift; | ||
| 1240 | return $self->_secondary_key_to_label('track source',@_); | ||
| 1241 | } | ||
| 1242 | |||
| 1243 | sub _secondary_key_to_label { | ||
| 1244 | my $self = shift; | ||
| 1245 | my $field = shift; | ||
| 1246 | my $index = $self->{'.secondary_key'}; | ||
| 1247 | if (!exists $index->{$field}) { | ||
| 1248 | for my $label ($self->labels) { | ||
| 1249 | my @sources = shellwords $self->setting($label=>$field) or next; | ||
| 1250 | push @{$index->{$field}{lc $_}},$label foreach @sources; | ||
| 1251 | } | ||
| 1252 | } | ||
| 1253 | |||
| 1254 | my %seenit; | ||
| 1255 | return grep {!$seenit{$_}++} | ||
| 1256 | map {exists $index->{$field}{lc $_} ? @{$index->{$field}{lc $_}} : () } @_; | ||
| 1257 | } | ||
| 1258 | |||
| 1259 | |||
| 1260 | ######### experimental code to manage user-specific tracks ########## | ||
| 1261 | sub add_user_type { | ||
| 1262 | my $self = shift; | ||
| 1263 | my ($type,$type_configuration) = @_; | ||
| 1264 | |||
| 1265 | my $cc = ($type =~ /^(general|default)$/i) ? 'general' : $type; # normalize | ||
| 1266 | |||
| 1267 | my $base = $self->{_user_tracks} ||= {}; | ||
| 1268 | |||
| 1269 | push @{$base->{types}},$cc | ||
| 1270 | unless $cc eq 'general' or $base->{config}{$cc}; | ||
| 1271 | |||
| 1272 | if (defined $type_configuration) { | ||
| 1273 | for my $tag (keys %$type_configuration) { | ||
| 1274 | $base->{config}{$cc}{lc $tag} = $type_configuration->{$tag}; | ||
| 1275 | } | ||
| 1276 | } | ||
| 1277 | } | ||
| 1278 | |||
| 1279 | sub configured_types { | ||
| 1280 | my $self = shift; | ||
| 1281 | my @types = $self->SUPER::configured_types; | ||
| 1282 | my @user_types = @{$self->{_user_tracks}{types}} | ||
| 1283 | if exists $self->{_user_tracks}{types}; | ||
| 1284 | return (@types,@user_types); | ||
| 1285 | } | ||
| 1286 | |||
| 1287 | sub usertype2label { | ||
| 1288 | my $self = shift; | ||
| 1289 | my ($type,$length) = @_; | ||
| 1290 | return unless $self->{_user_tracks}{types}; | ||
| 1291 | my @userlabels = @{$self->{_user_tracks}{types}} or return; | ||
| 1292 | |||
| 1293 | my %labels; | ||
| 1294 | for my $label (@userlabels) { | ||
| 1295 | my ($label_base,$minlength) = $label =~ /(.+)(?::(\d+))?/; | ||
| 1296 | $minlength ||= 0; | ||
| 1297 | next if defined $length && $minlength > $length; | ||
| 1298 | my @types = shellwords($self->{_user_tracks}{config}{$label}{feature}); | ||
| 1299 | @types = $label unless @types; | ||
| 1300 | next unless grep {/$type/i} @types; | ||
| 1301 | $labels{$label}++; | ||
| 1302 | } | ||
| 1303 | return keys %labels; | ||
| 1304 | } | ||
| 1305 | |||
| 1306 | sub _setting { | ||
| 1307 | my $self = shift; | ||
| 1308 | my $base = $self->{_user_tracks}; | ||
| 1309 | if ($base && defined $_[0] && exists $base->{config}{$_[0]}) { | ||
| 1310 | return $base->{config}{$_[0]}{$_[1]} if @_ == 2; | ||
| 1311 | return keys %{$base->{config}{$_[0]}} if @_ == 1; | ||
| 1312 | } | ||
| 1313 | return $self->SUPER::_setting(@_); | ||
| 1314 | } | ||
| 1315 | |||
| 1316 | sub code_setting { | ||
| 1317 | my $self = shift; | ||
| 1318 | my $base = $self->{_user_tracks}; | ||
| 1319 | if ($base && exists $base->{config}{$_[0]||''}) { | ||
| 1320 | return $self->_setting(@_); # don't allow code_setting on user tracks | ||
| 1321 | } else { | ||
| 1322 | return $self->SUPER::code_setting(@_); | ||
| 1323 | } | ||
| 1324 | } | ||
| 1325 | |||
| 1326 | sub parse_user_file { | ||
| 1327 | my $self = shift; | ||
| 1328 | |||
| 1329 | $self->{_user_tracks}{types} ||= []; | ||
| 1330 | $self->{_user_tracks}{config} ||= {}; | ||
| 1331 | |||
| 1332 | local $self->{types} = $self->{_user_tracks}{types}; | ||
| 1333 | local $self->{config} = $self->{_user_tracks}{config}; | ||
| 1334 | $self->SUPER::parse_file(@_); | ||
| 1335 | } | ||
| 1336 | |||
| 1337 | sub parse_user_fh { | ||
| 1338 | my $self = shift; | ||
| 1339 | local $self->{types} = $self->{_user_tracks}{types}; | ||
| 1340 | local $self->{config} = $self->{_user_tracks}{config}; | ||
| 1341 | $self->SUPER::parse_fh(@_); | ||
| 1342 | } | ||
| 1343 | |||
| 1344 | |||
| 1345 | sub subtrack_scan_list { | ||
| 1346 | my $self = shift; | ||
| 1347 | my $label = shift; | ||
| 1348 | my $stt = Bio::Graphics::Browser2::Render->create_subtrack_manager($label,$self,{}) or return; | ||
| 1349 | my @ids = keys %{$stt->elements}; | ||
| 1350 | return \@ids; | ||
| 1351 | } | ||
| 1352 | |||
| 1353 | sub fix_sqlite { | ||
| 1354 | return if $SQLITE_FIXED++; | ||
| 1355 | no warnings; | ||
| 1356 | *Bio::DB::SeqFeature::Store::DBI::SQLite::_has_spatial_index = sub { return }; | ||
| 1357 | } | ||
| 1358 | |||
| 1359 | sub get_section_from_label { | ||
| 1360 | my $self = shift; | ||
| 1361 | my $label = shift; | ||
| 1362 | |||
| 1363 | return 'detail' if ref $label; # work around a DAS bug | ||
| 1364 | |||
| 1365 | if ($label eq 'overview' || $label =~ /:overview$/){ | ||
| 1366 | return 'overview'; | ||
| 1367 | } | ||
| 1368 | elsif ($label eq 'region' || $label =~ /:region$/){ | ||
| 1369 | return 'region'; | ||
| 1370 | } | ||
| 1371 | return 'detail' | ||
| 1372 | } | ||
| 1373 | |||
| 1374 | =head2 $boolean = $source->use_inline_imagemap($label=>$length) | ||
| 1375 | |||
| 1376 | Given a track label and length (for semantic zooming) return true | ||
| 1377 | if we should generate an inline clickable imagemap vs one in which | ||
| 1378 | the actions are generated by Ajax callbacks | ||
| 1379 | |||
| 1380 | =cut | ||
| 1381 | |||
| 1382 | sub use_inline_imagemap { | ||
| 1383 | my $self = shift; | ||
| 1384 | my ($label,$length) = @_; | ||
| 1385 | my $inline = $self->semantic_fallback_setting($label=>'inline imagemaps',$length) | ||
| 1386 | ||$self->semantic_fallback_setting($label=>'inline imagemap', $length); | ||
| 1387 | return $inline if defined $inline; | ||
| 1388 | return 1 if not (Bio::Graphics::Browser2::Render->fcgi_running() | ||
| 1389 | || | ||
| 1390 | $ENV{MOD_PERL_API_VERSION} | ||
| 1391 | ); | ||
| 1392 | my $db = $self->open_database($label,$length) or return 1; | ||
| 1393 | return !$db->can('get_feature_by_id'); | ||
| 1394 | } | ||
| 1395 | |||
| 1396 | 1; | ||
| 1397 |