1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
package Bio::Graphics::Browser2::PluginSet;
# API for using plugins
# $Id$
use strict;
use Bio::Graphics::Browser2;
use Bio::Graphics::Browser2::Util 'shellwords';
use CGI 'param';
use constant DEBUG=>0;
sub new {
my $package = shift;
my $config = shift;
my @search_path = @_;
my %plugin_list = ();
warn "initializing plugins with $config..." if DEBUG;
my @plugins = shellwords($config->plugins);
# only one authorization plugin allowed, from globals
if (my $auth = $config->auth_plugin) {
unshift @plugins,$auth; # first one
}
warn "PLUGINS = @plugins" if DEBUG;
PLUGIN:
for my $plugin (@plugins) {
my $class = "Bio\:\:Graphics\:\:Browser2\:\:Plugin\:\:$plugin";
for my $search_path (@search_path) {
my $plugin_with_path = "$search_path/$plugin.pm";
if (eval {require $plugin_with_path}) {
warn "plugin $plugin loaded successfully" if DEBUG;
my $obj = eval{$class->new};
unless ($obj) {
warn "$plugin: $@";
next PLUGIN;
}
warn "plugin name = ",$obj->name," base = $plugin" if DEBUG;
$plugin_list{$plugin} = $obj;
next PLUGIN;
} else {
warn $@ if $@ and $@ !~ /^Can\'t locate/;
}
}
warn $@ if !$plugin_list{$plugin} && $@ =~ /^Can\'t locate/;
}
my $self = bless {
config => $config,
plugins => \%plugin_list
},ref $package || $package;
return $self;
}
sub config { shift->{config} }
sub plugins {
my $self = shift;
return wantarray ? values %{$self->{plugins}} : $self->{plugins};
}
sub plugin {
my $self = shift;
my $plugin_base = shift;
$self->plugins->{$plugin_base};
}
sub language {
my $self = shift;
my $d = $self->{language};
$self->{language} = shift if @_;
$d;
}
sub auth_plugin {
my $self = shift;
my @a = grep {$_->type eq 'authenticator'} values %{$self->{plugins}};
return unless @a;
return $a[0];
}
sub configure {
my $self = shift;
my $render = shift;
my $database = $render->db;
my $page_settings = $render->state;
my $language = $render->language;
my $session = $render->session;
my $search = $render->get_search_object;
my $conf = $self->config;
my $plugins = $self->plugins;
my $conf_dir = $conf->globals->config_base;
$self->language($language);
for my $name (keys %$plugins) {
eval {
my $p = $plugins->{$name};
$p->renderer($render);
$p->database($database);
$p->browser_config($conf);
$p->config_path($conf_dir);
$p->language($language);
$p->page_settings($page_settings);
$p->db_search($search);
$p->init(); # other initialization
# retrieve persistent configuration
my $config = $session->plugin_settings($p->name);
unless (%$config) {
my $defaults = $p->config_defaults;
%$config = %{$defaults} if $defaults;
}
# and tell the plugin about it
$p->configuration($config);
# if there are any CGI parameters from the
# plugin's configuration screen, set it here
if (my @params = grep {/^$name\./} param()) {
$p->reconfigure unless param('plugin_action') eq $language->tr('Cancel');
# turn the plugin on
my $setting_name = 'plugin:'.$p->name;
$p->page_settings->{features}{$setting_name}{visible} = 1;
}
if ($p->type eq 'authenticator') {
my $source = $self->config;
$source->set_authenticator($p);
$source->set_username($render->session->username);
}
};
warn "$name: $@" if $@;
}
$self->set_filters(); # allow filter plugins to adjust the data source
}
sub destroy {
my $self = shift;
my $plugins = $self->plugins;
for my $name (keys %$plugins) {
eval {
my $p = $plugins->{$name};
$p->renderer(undef);
};
}
}
sub set_filters {
my $self = shift;
my $source = $self->config;
my @labels = grep {!/^_/} $source->labels;
for my $p ($self->plugins) {
next unless $p->type eq 'filter';
for my $l (@labels) {
$self->{'.ok'}{$l} ||= $source->setting($l,'key'); # remember this!
$self->{'.of'}{$l} ||= $source->setting($l,'filter'); # remember this!
if (my ($filter,$new_key) = $p->filter($l,$self->{'.ok'}{$l})) {
$source->set($l, filter => $filter);
$source->set($l, key => $new_key);
}
else {
$source->set($l, key => $self->{'.ok'}{$l}) if exists $self->{'.ok'}{$l};
$source->set($l, filter => $self->{'.of'}{$l}) if exists $self->{'.of'}{$l};
}
}
}
}
sub annotate {
my $self = shift;
my $segment = shift;
my $feature_files = shift || {};
my $fast_mapper = shift; # fast mapper filters out features that are outside cur segment
my $slow_mapper = shift; # slow mapper doesn't
my $max_segment = shift; # ignored
my $whole_segment = shift;
my $region_segment = shift;
my @plugins = $self->plugins;
for my $p (@plugins) {
next unless $p->type eq 'annotator';
my $name = "plugin:".$p->id;
next unless $p->page_settings && $p->page_settings->{features}{$name}{visible};
warn "Plugin $name is visible, so running it on segment $segment" if DEBUG;
if ($segment->length > $max_segment) {
$feature_files->{$name} = Bio::Graphics::FeatureFile->new(); # empty
} else {
my $features = $p->annotate($segment,$fast_mapper) or next;
$features->name($name);
$feature_files->{$name} = $features;
}
}
}
sub set_segments {
my $self = shift;
my $segments = shift;
my $plugins = $self->plugins;
for my $k ( values %$plugins ) {
$k->segments($segments);
}
}
sub _retrieve_plugin_config {
my $plugin = shift;
my $name = $plugin->name;
my %settings = cookie("${name}_config");
return $plugin->config_defaults unless %settings;
foreach (keys %settings) {
# need better serialization than this...
if ($settings{$_} =~ /$;/) {
my @settings = split $;,$settings{$_};
pop @settings unless defined $settings[-1];
$settings{$_} = \@settings;
}
}
\%settings;
}
sub menu_labels {
my $self = shift;
my $plugins = $self->plugins;
my $config = $self->config;
my $lang = $self->language;
my %verbs = (dumper => $lang->tr('Dump'),
finder => $lang->tr('Find'),
highlighter => $lang->tr('Highlight'),
annotator => $lang->tr('Annotate'),
filter => $lang->tr('Filter'),
);
my %labels = ();
# Adjust plugin menu labels
for ( keys %{$plugins} ) {
# plugin-defined verb
if ( $plugins->{$_}->verb ) {
$labels{$_} = $lang->tr($plugins->{$_}->verb) ||
ucfirst $plugins->{$_}->verb;
}
# default verb
else {
$labels{$_} = $verbs{$plugins->{$_}->type} ||
ucfirst $plugins->{$_}->type;
}
my $name = $plugins->{$_}->name;
$labels{$_} .= " $name";
$labels{$_} =~ s/^\s+//;
}
return \%labels;
}
1;
__END__
=head1 NAME
Bio::Graphics::Browser2::PluginSet -- A set of plugins
=head1 SYNOPSIS
None. Used internally by gbrowse & gbrowse_img.
=head1 METHODS
=over 4
=item $plugin_set = Bio::Graphics::Browser2::PluginSet->new($config,$page_settings,@search_path)
Initialize plugins according to the configuration, page settings and
the plugin search path. Returns an object.
=item $plugin_set->configure($database)
Configure the plugins given the database.
=item $plugin_set->annotate($segment,$feature_files,$rel2abs)
Run plugin annotations on the $segment, adding the resulting feature
files to the hash ref in $feature_files ({track_name=>$feature_list}).
The $rel2abs argument holds a coordinate mapper callback, but is
currently unused.
=back
=head1 SEE ALSO
L<Bio::Graphics::Browser2>
=head1 AUTHOR
Lincoln Stein E<lt>lstein@cshl.orgE<gt>.
Copyright (c) 2005 Cold Spring Harbor Laboratory
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself. See DISCLAIMER.txt for
disclaimers of warranty.
=cut
|