« blib/conf/plugins/CreateBlastDB.pm
↑ Summary
blib/lib/Bio/Graphics/Browser2/Render/SnapshotManager.pm »
| 1 | package Bio::Graphics::Browser2::Plugin; | ||
| 2 | # $Id$ | ||
| 3 | # base class for plugins for the Generic Genome Browser | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Bio::Graphics::Browser2::Plugin -- Base class for gbrowse plugins. | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | package Bio::Graphics::Browser2::Plugin::MyPlugin; | ||
| 12 | use Bio::Graphics::Browser2::Plugin; | ||
| 13 | use CGI ':standard'; | ||
| 14 | @ISA = 'Bio::Graphics::Browser2::Plugin'; | ||
| 15 | |||
| 16 | # called by gbrowse to return name of plugin for popup menu | ||
| 17 | sub name { 'Example Plugin' } | ||
| 18 | |||
| 19 | # called by gbrowse to return the descriptive verb for popup menu | ||
| 20 | sub verb { 'Demonstrate' } | ||
| 21 | |||
| 22 | # called by gbrowse to return description of plugin | ||
| 23 | sub description { 'This is an example plugin' } | ||
| 24 | |||
| 25 | # called by gbrowse to return type of plugin | ||
| 26 | sub type { 'annotator' } | ||
| 27 | |||
| 28 | # called by gbrowse to configure default settings for plugin | ||
| 29 | sub config_defaults { | ||
| 30 | my $self = shift; | ||
| 31 | return {foo => $value1, | ||
| 32 | bar => $value2} | ||
| 33 | } | ||
| 34 | |||
| 35 | # called by gbrowse to reconfigure plugin settings based on CGI parameters | ||
| 36 | sub reconfigure { | ||
| 37 | my $self = shift; | ||
| 38 | my $current = $self->configuration; | ||
| 39 | $current->{foo} = $self->config_param('foo'); | ||
| 40 | $current->{bar} = $self->config_param('bar'); | ||
| 41 | } | ||
| 42 | |||
| 43 | # called by gbrowse to create a <form> fragment for changing settings | ||
| 44 | sub configure_form { | ||
| 45 | my $self = shift; | ||
| 46 | my $current = $self->configuration; | ||
| 47 | my $form = textfield(-name => $self->config_name('foo'), | ||
| 48 | -value => $current->{foo}) | ||
| 49 | . | ||
| 50 | textfield(-name => $self->config_name('bar'), | ||
| 51 | -value => $current->{bar}); | ||
| 52 | return $form; | ||
| 53 | } | ||
| 54 | |||
| 55 | # called by gbrowse to annotate the DNA, returning features | ||
| 56 | sub annotate { | ||
| 57 | my $self = shift; | ||
| 58 | my ($segment,$coordinate_mapper) = @_; | ||
| 59 | my $config = $self->configuration; | ||
| 60 | my $feature_list = $self->new_feature_list; | ||
| 61 | $feature_list->add_type('my_type' => {glyph => 'generic', | ||
| 62 | key => 'my type', | ||
| 63 | bgcolor => 'green', | ||
| 64 | link => 'http://www.google.com/search?q=$name' | ||
| 65 | } | ||
| 66 | ); | ||
| 67 | # do something with the sequence segment | ||
| 68 | my @features = do_something(); | ||
| 69 | $feature_list->add_feature($_ => 'my_type') foreach @features; | ||
| 70 | return $feature_list; | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | =head1 DESCRIPTION | ||
| 75 | |||
| 76 | This is the base class for Generic Genome Browser plugins. Plugins | ||
| 77 | are perl .pm files that are stored in the gbrowse.conf/plugins | ||
| 78 | directory. Plugins are activated in the gbrowse.conf/ configuration | ||
| 79 | file by including them on the list indicated by the "plugins" setting: | ||
| 80 | |||
| 81 | plugins = BatchDumper FastaDumper GFFDumper | ||
| 82 | OligoFinder RestrictionAnnotator | ||
| 83 | |||
| 84 | Site-specific plugins may be placed in one or more site-specific | ||
| 85 | directories and added to the plugin search path using the plugin_path | ||
| 86 | setting: | ||
| 87 | |||
| 88 | plugin_path = /usr/local/gbrowse_plugins | ||
| 89 | |||
| 90 | GBrowse currently recognizes five distinct types of plugins: | ||
| 91 | |||
| 92 | =over 4 | ||
| 93 | |||
| 94 | =item 1) dumpers | ||
| 95 | |||
| 96 | These plugins receive the genomic segment object and generate a dump | ||
| 97 | -- the output can be text, html or some other specialized | ||
| 98 | format. Example: GAME dumper. | ||
| 99 | |||
| 100 | =item 2) finders | ||
| 101 | |||
| 102 | These plugins accept input from the user and return a | ||
| 103 | list of genomic regions. The main browser displays the found regions | ||
| 104 | and allows the user to select among them. Example: BLAST search. | ||
| 105 | |||
| 106 | =item 3) annotators | ||
| 107 | |||
| 108 | These plugins receive the genomic segment object and either 1) return | ||
| 109 | a list of features which are overlayed on top of the detailed view | ||
| 110 | (Example: restriction site annotator) or 2) update the database with | ||
| 111 | new or modified features and return nothing (Example: basic editor) | ||
| 112 | |||
| 113 | =item 4) trackfilters | ||
| 114 | |||
| 115 | These plugins can be used to reduce the complexity of sites that have | ||
| 116 | many tracks, by providing search and filtering functions for the track | ||
| 117 | table. When a trackfilter is active, its form-based user interface is | ||
| 118 | positioned directly above the tracks table, and changes to the for | ||
| 119 | cause the list of tracks to be updated dynamically. | ||
| 120 | |||
| 121 | =item 5) highlighters | ||
| 122 | |||
| 123 | These plugins will color-highlight features based on user-defined | ||
| 124 | attributes. For example, you could highlight all features that are in | ||
| 125 | the positive strand. | ||
| 126 | |||
| 127 | =item 6) filters | ||
| 128 | |||
| 129 | These plugins will filter features based on user-defined | ||
| 130 | attributes. Only features that match the attributes will be | ||
| 131 | displayed. For example, you could filter out RNA transcript features | ||
| 132 | based on their size, so that only features that are less than 50 bp in | ||
| 133 | length (e.g. short RNAs) are shown. | ||
| 134 | |||
| 135 | =back | ||
| 136 | |||
| 137 | All plug-ins inherit from Bio::Graphics::Browser2::Plugin, which | ||
| 138 | defines reasonable (but uninteresting) defaults for each of the | ||
| 139 | methods. Specific behavior is then implemented by selectively | ||
| 140 | overriding certain methods. | ||
| 141 | |||
| 142 | The best way to understand how this works is to look at the source | ||
| 143 | code for some working plugins. Examples provided with the gbrowse | ||
| 144 | distribution include: | ||
| 145 | |||
| 146 | =over 4 | ||
| 147 | |||
| 148 | =item GFFDumper.pm | ||
| 149 | |||
| 150 | A simple dumper which produces GFF format output representing the | ||
| 151 | features of the currently-selected segment. | ||
| 152 | |||
| 153 | =item FastaDumper.pm | ||
| 154 | |||
| 155 | A more complex dumper that illustrates how to create and manage | ||
| 156 | persistent user-modifiable settings. | ||
| 157 | |||
| 158 | =item SequenceDumper.pm | ||
| 159 | |||
| 160 | Another dumper that shows how plugins interact with the Bio::SeqIO | ||
| 161 | system. | ||
| 162 | |||
| 163 | =item OligoFinder.pm | ||
| 164 | |||
| 165 | A finder that searches for short oligos in the entire database. (Only | ||
| 166 | works with Bio::DB::GFF databases.) | ||
| 167 | |||
| 168 | =item RestrictionAnnotator.pm | ||
| 169 | |||
| 170 | An annotator that finds restriction sites in the currently selected | ||
| 171 | region of the genome. It creates a new track for each type of | ||
| 172 | restriction site selected. | ||
| 173 | |||
| 174 | =item RandomGene.pm | ||
| 175 | |||
| 176 | An example annotator that generates random gene-like structures in the | ||
| 177 | currently displayed region of the genome. It's intended as a template | ||
| 178 | for front-ends to gene prediction programs. | ||
| 179 | |||
| 180 | =item AttributeHiliter.pm | ||
| 181 | |||
| 182 | An example feature hiliter that works with Bio::DB::GFF and | ||
| 183 | Bio::SeqFeature::Store databases. | ||
| 184 | |||
| 185 | =item FilterTest.pm | ||
| 186 | |||
| 187 | An example feature filter that filters based on strand of the feature. | ||
| 188 | |||
| 189 | =item SimpleTrackFinder.pm | ||
| 190 | |||
| 191 | An example track filter that filters tracks based on their name. | ||
| 192 | |||
| 193 | =item SourceTrackFinder.pm | ||
| 194 | |||
| 195 | Another example track filter that filters tracks based on the contents | ||
| 196 | of their "track source" and "data source" options. | ||
| 197 | |||
| 198 | =back | ||
| 199 | |||
| 200 | =head1 METHODS | ||
| 201 | |||
| 202 | The remainder of this document describes the methods available to the | ||
| 203 | programmer. | ||
| 204 | |||
| 205 | =head2 INITIALIZATION | ||
| 206 | |||
| 207 | The initialization methods establish the human-readable name, | ||
| 208 | description, and basic operating parameters of the plugin. They | ||
| 209 | should be overridden in each plugin you write. | ||
| 210 | |||
| 211 | =over 4 | ||
| 212 | |||
| 213 | =item $name = $self->name() | ||
| 214 | |||
| 215 | Return a short human-readable name for the plugin. This will be | ||
| 216 | displayed to the user in a menu using one of the following forms: | ||
| 217 | |||
| 218 | Dump <name> | ||
| 219 | Find <name> | ||
| 220 | Annotate <name> | ||
| 221 | plugin_defined_verb <name> | ||
| 222 | |||
| 223 | =item $description = $self->description() | ||
| 224 | |||
| 225 | This method returns a longer description for the plugin. The text may | ||
| 226 | contain HTML tags, and should describe what the plugin does and who | ||
| 227 | wrote it. This text is displayed when the user presses the "About..." | ||
| 228 | button. | ||
| 229 | |||
| 230 | =item $verb = $self->verb() | ||
| 231 | |||
| 232 | This method returns a verb to be used in the plugin popup menu | ||
| 233 | in cases where the main three don't fit. This method should | ||
| 234 | be set return whitespace or an empty string (not undefined) | ||
| 235 | if you do not want a descriptive verb for the menu | ||
| 236 | |||
| 237 | =item $suppress_title = $self->suppress_title() | ||
| 238 | |||
| 239 | The purpose of this methods is to suppress the 'Configure...' | ||
| 240 | or 'Find...' title that is printed at the top of the page when the | ||
| 241 | plugin is loaded. It will return false unless overriden by a plugin where | ||
| 242 | this behaviour is desired. | ||
| 243 | |||
| 244 | =item $type = $self->type() | ||
| 245 | |||
| 246 | This tells gbrowse what the plugin's type is. It must return one of | ||
| 247 | the scripts "dumper," "finder,", "annotator" as described in the | ||
| 248 | introduction to this documentation. If the method is not overridden, | ||
| 249 | type() will return "dumper." | ||
| 250 | |||
| 251 | =item $self->init() | ||
| 252 | |||
| 253 | This method is called before any methods are invoked and allows the | ||
| 254 | plugin to do any run-time initialization it needs. The default is to | ||
| 255 | do nothing. Ordinarily this method does not need to be implemented. | ||
| 256 | |||
| 257 | =back | ||
| 258 | |||
| 259 | =head2 ACCESS TO THE ENVIRONMENT | ||
| 260 | |||
| 261 | The following methods give the plugin access to the environment, | ||
| 262 | including the gbrowse page settings, the sequence features database, | ||
| 263 | and the plugin's own configuration settings. | ||
| 264 | |||
| 265 | These methods do not generally need to be overridden. | ||
| 266 | |||
| 267 | =over 4 | ||
| 268 | |||
| 269 | =item $config = $self->configuration() | ||
| 270 | |||
| 271 | Call this method to retrieve the persistent configuration for this | ||
| 272 | plugin. The configuration is a hashref containing the default | ||
| 273 | configuration settings established by config_defaults(), possibly | ||
| 274 | modified by the user. Due to cookie limitations, the values of the | ||
| 275 | hashref must be scalars or array references. | ||
| 276 | |||
| 277 | See CONFIGURATION METHODS for instructions on how to create and | ||
| 278 | maintain the plugin's persistent configuration information. | ||
| 279 | |||
| 280 | =item $renderer = $self->renderer | ||
| 281 | |||
| 282 | This method returns a copy of the Render object, which provides access | ||
| 283 | to the internal workings of the page layout engine. You will need to | ||
| 284 | troll the Bio::Graphics::Browser2::Render source code to understand | ||
| 285 | how to use this object. | ||
| 286 | |||
| 287 | =item $database = $self->database | ||
| 288 | |||
| 289 | This method returns a copy of the default database. Depending on the | ||
| 290 | data source chosen by the gbrowse administrator, this may be a | ||
| 291 | Bio::DB::GFF database, a Bio::DB::Das::Chado database, a Bio::Das | ||
| 292 | database, a Bio::DB::Das::BioSQL database, or any of the other | ||
| 293 | Das-like databases that gbrowse supports. | ||
| 294 | |||
| 295 | =item @dbs = $self->all_databases | ||
| 296 | |||
| 297 | This method returns copies of all the databases defined for this data | ||
| 298 | source. Most useful for Finder plugins. | ||
| 299 | |||
| 300 | =item @track_names = $self->selected_tracks | ||
| 301 | |||
| 302 | This method returns the list of track names that the user currently | ||
| 303 | has turned on. Track names are the internal names identified in | ||
| 304 | gbrowse configuration file stanzas, for example "ORFs" in the | ||
| 305 | 01.yeast.conf example file. | ||
| 306 | |||
| 307 | =item @feature_types = $self->selected_features | ||
| 308 | |||
| 309 | This method returns the list of feature types that the user currently | ||
| 310 | has turned on. Feature types are the feature identifiers indicated by | ||
| 311 | the "feature" setting in each track in the gbrowse configuration file, | ||
| 312 | for example "ORF:sgd" in the 01.yeast.conf [ORFs] track. | ||
| 313 | |||
| 314 | =item $gbrowse_settings = $self->page_settings | ||
| 315 | |||
| 316 | This method returns a big hash containing the current gbrowse | ||
| 317 | persistent user settings. These settings are documented in the | ||
| 318 | gbrowse executable source code. You will not ordinarily need to | ||
| 319 | access the contents of this hash, and you should *not* change its | ||
| 320 | values. | ||
| 321 | |||
| 322 | =item $browser_config = $self->browser_config | ||
| 323 | |||
| 324 | This method returns a copy of the Bio::Graphics::Browser2::DataSource | ||
| 325 | object that drives gbrowse. This object allows you to interrogate | ||
| 326 | (and change!) the values set in the current gbrowse configuration | ||
| 327 | file. | ||
| 328 | |||
| 329 | =item $value = $self->setting('setting name') | ||
| 330 | |||
| 331 | The recommended use for this object is to recover plugin-specific | ||
| 332 | settings from the gbrowse configuration file. These can be defined by | ||
| 333 | the gbrowse administrator by placing the following type of stanza into | ||
| 334 | the gbrowse config file: | ||
| 335 | |||
| 336 | [GOSearch:plugin] | ||
| 337 | traverse_isa = 1 | ||
| 338 | use_server = http://amigo.geneontology.org | ||
| 339 | |||
| 340 | "GOSearch" is the package name of the plugin, and the ":plugin" part | ||
| 341 | of the stanza name tells gbrowse that this is a plugin-private | ||
| 342 | configuration section. | ||
| 343 | |||
| 344 | You can now access these settings from within the plugin by using the | ||
| 345 | following idiom: | ||
| 346 | |||
| 347 | my $traverse_isa = $self->setting('traverse_isa'); | ||
| 348 | my $server = $self->setting('use_server'); | ||
| 349 | |||
| 350 | This facility is intended to be used for any settings that should not | ||
| 351 | be changed by the end user. Persistent user preferences should be | ||
| 352 | stored in the hash returned by configuration(). | ||
| 353 | |||
| 354 | If your plugin inherits from another one, then the inheritance path will | ||
| 355 | be searched for settings. For example, if the GOSearch plugin inherits | ||
| 356 | from the OntologySearch plugin, then setting() will search first for a stanza named | ||
| 357 | "GOSearch:plugin" and then for "OntologySearch:plugin". | ||
| 358 | |||
| 359 | =item $search = $self->db_search | ||
| 360 | |||
| 361 | This method returns a Bio::Graphics::Browser2::RegionSearch object, | ||
| 362 | which you can use to search all local and remote databases. The | ||
| 363 | interface is this: | ||
| 364 | |||
| 365 | $features = $search->search_features(\%args); | ||
| 366 | |||
| 367 | where \%args are the various arguments (e.g. -type, -seq_id, -name) | ||
| 368 | passed to the dbadaptors' search_features() method. Alternatively: | ||
| 369 | |||
| 370 | $features = $search->search_features('keyword string') | ||
| 371 | |||
| 372 | which implements GBrowse's heuristic search. | ||
| 373 | |||
| 374 | The result is an array reference of features found. | ||
| 375 | |||
| 376 | The search object also has a get_seq_stream() method that accepts the | ||
| 377 | same arguments as search_features() but returns an iterator. The | ||
| 378 | iterator implements a next_seq() method. | ||
| 379 | |||
| 380 | =item $language = $self->language | ||
| 381 | |||
| 382 | This method returns the current I18n language file. You can use this | ||
| 383 | to make translations with the tr() method: | ||
| 384 | |||
| 385 | print $self->language->tr('WELCOME'); | ||
| 386 | |||
| 387 | =item $segments = $self->segments | ||
| 388 | |||
| 389 | This method returns the current segments in use by gbrowse. The active | ||
| 390 | segments are set from within gbrowse | ||
| 391 | |||
| 392 | $plugin->segments(\@segments); | ||
| 393 | |||
| 394 | The active segments can then be retrieved from within the plugin. This is | ||
| 395 | useful in cases where segment-specific information is required by plugin methods | ||
| 396 | that are not passed a segment object. | ||
| 397 | |||
| 398 | |||
| 399 | =item $config_path = $self->config_path | ||
| 400 | |||
| 401 | This method returns the path to the directory in which gbrowse stores | ||
| 402 | its configuration files. This is very useful for storing | ||
| 403 | plugin-specific configuration files. See the sourcecode of | ||
| 404 | RestrictionAnnotator for an exmaple of this. | ||
| 405 | |||
| 406 | =item $feature_file = $self->new_feature_file | ||
| 407 | |||
| 408 | This method creates a new Bio::Graphics::FeatureFile for use by | ||
| 409 | annotators. The annotate() method must invoke this method, configure | ||
| 410 | the resulting feature file, and then add one or more | ||
| 411 | Bio::Graphics::Feature objects to it. | ||
| 412 | |||
| 413 | This method is equivalent to calling | ||
| 414 | Bio::Graphics::FeatureFile->new(-smart_features=>1), where the | ||
| 415 | -smart_features argument allows features to be turned into imagemap | ||
| 416 | links. | ||
| 417 | |||
| 418 | =back | ||
| 419 | |||
| 420 | =head2 METHODS TO BE IMPLEMENTED IN DUMPERS | ||
| 421 | |||
| 422 | All plugins that act as feature dumpers should override one or more of | ||
| 423 | the methods described in this section. | ||
| 424 | |||
| 425 | =over 4 | ||
| 426 | |||
| 427 | =item $self->dump($segment) | ||
| 428 | |||
| 429 | Given a Bio::Das::SegmentI object, produce some output from its | ||
| 430 | sequence and/or features. This can be used to dump something as | ||
| 431 | simple as a FASTA file, or as complex as a motif analysis performed on | ||
| 432 | the sequence. | ||
| 433 | |||
| 434 | As described in L<Bio::Das::SegmentI>, the segment object represents | ||
| 435 | the region of the genome currently on display in the gbrowse "detail" | ||
| 436 | panel. You may call its seq() method to return the sequence as a | ||
| 437 | string, or its features() method to return a list of all features that | ||
| 438 | have been annotated onto this segment of the genome. | ||
| 439 | |||
| 440 | At the time that dump() is called, gbrowse will already have set up | ||
| 441 | the HTTP header and performed other initialization. The dump() method | ||
| 442 | merely needs to begin printing output using the appropriate MIME | ||
| 443 | type. By default, the MIME type is text/plain, but this can be | ||
| 444 | changed with the mime_type() method described next. | ||
| 445 | |||
| 446 | The following trivial example shows a dump() method that prints the | ||
| 447 | name and length of the segment: | ||
| 448 | |||
| 449 | sub dump { | ||
| 450 | my $self = shift; | ||
| 451 | my $segment = shift; | ||
| 452 | print "name = ",$segment->seq_id,"\n"; | ||
| 453 | print "length = ",$segment->length,"\n"; | ||
| 454 | } | ||
| 455 | |||
| 456 | =item $type = $self->mime_type | ||
| 457 | |||
| 458 | Return the MIME type of the information produced by the plugin. By | ||
| 459 | default, this method returns "text/plain". Override it to return | ||
| 460 | another MIME type, such as "text/xml". | ||
| 461 | |||
| 462 | =back | ||
| 463 | |||
| 464 | =head2 METHODS TO BE IMPLEMENTED IN FINDERS | ||
| 465 | |||
| 466 | All finder plugins will need to override one or more of the methods | ||
| 467 | described in this section. | ||
| 468 | |||
| 469 | =over 4 | ||
| 470 | |||
| 471 | =item $features = $self->find($segment); | ||
| 472 | |||
| 473 | The find() method will be passed a Bio::Das::SegmentI segment object, | ||
| 474 | as described earlier for the dump() method. Your code should search | ||
| 475 | the segment for features of interest, and return a two element | ||
| 476 | list. The first element should be an arrayref of Bio::SeqFeatureI | ||
| 477 | objects (see L<Bio::SeqFeatureI>), or an empty list if nothing was | ||
| 478 | found. These synthetic feature objects should indicate the position, | ||
| 479 | name and type of the features found. The second element of the | ||
| 480 | returned list should be a (possibly shortened) version of the search | ||
| 481 | string for display in informational messages. | ||
| 482 | |||
| 483 | Depending on the type of find you are performing, you might search the | ||
| 484 | preexisting features on the segment for matches, or create your own | ||
| 485 | features from scratch in the way that the annotator plugins do. You | ||
| 486 | may choose to ignore the passed segment and perform the search on the | ||
| 487 | entire database, which you can obtain using the database() method | ||
| 488 | call. | ||
| 489 | |||
| 490 | To create features from scratch I suggest you use either | ||
| 491 | Bio::Graphics::Feature, or Bio::SeqFeature::Generic to generate the | ||
| 492 | features. See their respective manual pages for details, and the | ||
| 493 | OligoFinder.pm plugin for an example of how to do this. | ||
| 494 | |||
| 495 | If the plugin requires user input before it can perform its task, | ||
| 496 | find() should return undef. Gbrowse will invoke configure_form() | ||
| 497 | followed by reconfigure() in order to prompt the user for input. If | ||
| 498 | nothing is found, the plugin should return an empty list. The | ||
| 499 | following is an example of how to prompt the user for input -- in this | ||
| 500 | case, a gene ontology term: | ||
| 501 | |||
| 502 | sub find { | ||
| 503 | my $self = shift; | ||
| 504 | my $segment = shift; # we ignore this! | ||
| 505 | my $config = $self->configuration; | ||
| 506 | my $query = $config->{query} or return undef; # PROMPT FOR INPUT | ||
| 507 | my $search = $self->db_search; | ||
| 508 | my @features = $search->features(-attributes=>{GO_Term => $query}); | ||
| 509 | return (\@features,$query); | ||
| 510 | } | ||
| 511 | |||
| 512 | sub configure_form { | ||
| 513 | my $self = shift; | ||
| 514 | return "Enter a GO Term: " | ||
| 515 | . textfield(-name=>$self->config_name('query')); | ||
| 516 | } | ||
| 517 | |||
| 518 | sub reconfigure { | ||
| 519 | my $self = shift; | ||
| 520 | my $config = $self->configuration; | ||
| 521 | $config->{query} = $self->config_param('query'); | ||
| 522 | } | ||
| 523 | |||
| 524 | See the sections below for more description of the configure_form() | ||
| 525 | and reconfigure() methods. | ||
| 526 | |||
| 527 | NOTE: If you need to use auxiliary files like BLAST files, you can | ||
| 528 | store the location of those files in the gbrowse .conf file under the | ||
| 529 | stanza [YourPlugin:plugin]: | ||
| 530 | |||
| 531 | [YourPlugin:plugin] | ||
| 532 | blast_path = /usr/local/blast/databases | ||
| 533 | |||
| 534 | sub find { | ||
| 535 | my $self = shift; | ||
| 536 | my $segment = shift; # ignored | ||
| 537 | my $blast_path = $self->browser_config->plugin_setting('blast_path'); | ||
| 538 | # etc etc etc | ||
| 539 | } | ||
| 540 | |||
| 541 | =item $features = $self->auto_find($search_string) | ||
| 542 | |||
| 543 | If the plugin has an "auto_find" method, then the method will be | ||
| 544 | invoked whenever the user types a string into GBrowse's search | ||
| 545 | box. The plugin may search any of the current data source's databases | ||
| 546 | (which you can get using $self->all_databases), or its own databases. | ||
| 547 | |||
| 548 | Return an arrayref containing the features found. Return an empty | ||
| 549 | arrayref to indicate that no features were found. Return undef to | ||
| 550 | indicate that the plugin declines to perform the search, in which case | ||
| 551 | GBrowse will default to its own search algorithm. | ||
| 552 | |||
| 553 | You may also choose to merge your search results with GBrowse's. To do | ||
| 554 | this, you can initiate the default search by calling: | ||
| 555 | |||
| 556 | $default_features | ||
| 557 | = $self->db_search->search_features({-search_term => 'searchterm'}) | ||
| 558 | |||
| 559 | Then do what you need to do to merge your customized search with the | ||
| 560 | default terms. | ||
| 561 | |||
| 562 | =back | ||
| 563 | |||
| 564 | =head2 METHODS TO BE IMPLEMENTED IN ANNOTATORS | ||
| 565 | |||
| 566 | All annotator plugins will need to override the method described in | ||
| 567 | this section. | ||
| 568 | |||
| 569 | =over 4 | ||
| 570 | |||
| 571 | =item $feature_file = $plugin->annotate($segment[,$coordinate_mapper]) | ||
| 572 | |||
| 573 | The annotate() method will be invoked with a Bio::Das::SegmentI | ||
| 574 | segment representing the region of the genome currently on view in the | ||
| 575 | gbrowse detail panel. The method should first call its own | ||
| 576 | new_feature_list() to create a Bio::Graphics::FeatureFile feature set | ||
| 577 | object, and define one or more feature types to added to the feature | ||
| 578 | set. The method should then create one or more Bio::Graphics::Feature | ||
| 579 | objects and add them to the feature set using add_feature. | ||
| 580 | |||
| 581 | The reason that annotate() returns a Bio::Graphics::FeatureFile rather | ||
| 582 | than an array of features the way that find() does is because | ||
| 583 | Bio::Graphics::FeatureFile also allows you to set up how the features | ||
| 584 | will be rendered; you can define tracks, assign different feature | ||
| 585 | types to different tracks, and assign each feature type a glyph, | ||
| 586 | color, and other options. | ||
| 587 | |||
| 588 | The annotate() function will also be passed a coordinate_mapper | ||
| 589 | variable. This is a code ref to a function that will transform | ||
| 590 | coordinates from relative to absolute coordinates. The function takes | ||
| 591 | a reference sequence name and a list of [$start,$end] coordinate | ||
| 592 | pairs, and returns a similar function result, except that the sequence | ||
| 593 | name and coordinates are all in absolute coordinate space. Currently | ||
| 594 | there are no plugins that make use of this facility. | ||
| 595 | |||
| 596 | See L<Bio::Graphics::FeatureFile> for details, and the | ||
| 597 | RestrictionAnnotator.pm plugin for an example. | ||
| 598 | |||
| 599 | =back | ||
| 600 | |||
| 601 | =head2 METHODS TO BE IMPLEMENTED IN TRACKFILTERS | ||
| 602 | |||
| 603 | =over | ||
| 604 | |||
| 605 | =item @track_names = $plugin->filter_tracks($tracks,$source) | ||
| 606 | |||
| 607 | Given a list of track names and a Bio::Graphics::Browser2::DataSource | ||
| 608 | object, identify the track names to display and return them as a | ||
| 609 | list. The tracks are passed as a reference to a list of all possible | ||
| 610 | track names. | ||
| 611 | |||
| 612 | To make the form interactive, you may wish to pepper the plugin's | ||
| 613 | configuration form methods with calls to the javascript routine | ||
| 614 | doPluginUpdate(). This causes GBrowse to update the plugin's | ||
| 615 | configuration and refresh the tracks table as a side effect. | ||
| 616 | |||
| 617 | =item @terms = $plugin->hilite_terms | ||
| 618 | |||
| 619 | Returns a list of terms to hilight in the tracks table, or empty if none. | ||
| 620 | |||
| 621 | =back | ||
| 622 | |||
| 623 | =head2 METHODS TO BE IMPLEMENTED IN FEATURE HILITERS | ||
| 624 | |||
| 625 | =over 4 | ||
| 626 | |||
| 627 | =item $color = $self->highlight($feature) | ||
| 628 | |||
| 629 | This method is passed a feature. It returns a color name (or any | ||
| 630 | Bio::Graphics color string) to highlight the feature with that color, | ||
| 631 | or undef if the feature should not be highlighted at all. | ||
| 632 | |||
| 633 | =back | ||
| 634 | |||
| 635 | =head2 METHODS TO BE IMPLEMENTED IN FEATURE FILTERS | ||
| 636 | |||
| 637 | =over 4 | ||
| 638 | |||
| 639 | =item ($filter,$newkey) = $self->filter($track_label,$key) | ||
| 640 | |||
| 641 | This method is passed a track label and the original key of the | ||
| 642 | label. It is expected to return a two-element list consisting of a | ||
| 643 | coderef and a new key for the track. The coderef should be a | ||
| 644 | subroutine that takes a feature as its single argument and returns | ||
| 645 | true to include the feature in the track and false to exclude the | ||
| 646 | feature from the track. | ||
| 647 | |||
| 648 | sub { | ||
| 649 | my $feature = shift; | ||
| 650 | return do_something() ? 1 : 0; | ||
| 651 | } | ||
| 652 | |||
| 653 | The filter() method should return an updated string for the track key | ||
| 654 | to indicate that the track is being filtered. This is to inform the | ||
| 655 | user that the track is not showing all possible features. | ||
| 656 | |||
| 657 | The filter() method should return an empty list if it does not wish to | ||
| 658 | install or filter, or to remove a filter that was previously | ||
| 659 | installed. | ||
| 660 | |||
| 661 | |||
| 662 | =back | ||
| 663 | |||
| 664 | =head2 PERSISTENT CONFIGURATION METHODS | ||
| 665 | |||
| 666 | The following methods can be called to retrieve data about the | ||
| 667 | environment in which the plugin is running. These methods are also | ||
| 668 | used by gbrowse to change the plugin state. | ||
| 669 | |||
| 670 | =over 4 | ||
| 671 | |||
| 672 | =item $config = $self->config_defaults() | ||
| 673 | |||
| 674 | This method will be called once at plugin startup time to give the | ||
| 675 | plugin a chance to set up its default configuration state. If you | ||
| 676 | implement this method you should return the configuration as a hash | ||
| 677 | reference in which the values of the hash are either scalar values or | ||
| 678 | array references. The contents of this hash will be placed in a | ||
| 679 | CGI::Session. | ||
| 680 | |||
| 681 | You will wish to implement this method if the plugin has | ||
| 682 | user-modifiable settings. | ||
| 683 | |||
| 684 | NOTE ON FILEHANDLES: You are not allowed to permanently store a | ||
| 685 | filehandle in the persistent configuration data structure because the | ||
| 686 | session-handling code will try to serialize and store the filehandle, | ||
| 687 | which is not allowed by the default serializer. If you must store a | ||
| 688 | filehandle in the configuration data structure, be sure to delete it | ||
| 689 | within the annotate(), find() or dump() methods once you are finished | ||
| 690 | using it. | ||
| 691 | |||
| 692 | =item $self->configure_form() | ||
| 693 | |||
| 694 | This method will be called when the user presses the "Configure | ||
| 695 | plugin" button. You should return the HTML for a fill-out form that | ||
| 696 | allows the user to change the current settings. The HTML should | ||
| 697 | contain the contents of an HTML <form> section, but B<not> the actual | ||
| 698 | <form> and </form> tags. These tags, along with the Submit and Cancel | ||
| 699 | buttons, will be added automatically. Typically you will build up the | ||
| 700 | HTML to return using a series of .= append operations. | ||
| 701 | |||
| 702 | It is highly recommended that you use the CGI module to generate the | ||
| 703 | fill-out form. In order to avoid clashing with other parts of | ||
| 704 | gbrowse, plugin fill-out forms must respect a namespacing convention | ||
| 705 | in which the name of each form field is preceded by the plugin package | ||
| 706 | name and a dot. The package name is the last component of the | ||
| 707 | plugin's package; for example "GoSearch" is the package name for | ||
| 708 | Bio::Graphics::Browser2::Plugin::GoSearch. To represent the "query" | ||
| 709 | field of the plugin named "GOSearch", the text field must be named | ||
| 710 | "GOSearch.query". | ||
| 711 | |||
| 712 | To make this easier to do right, the Plugin module provides a method | ||
| 713 | named config_name() which will add the prefix for you. Here is how | ||
| 714 | to use it with the "query" example: | ||
| 715 | |||
| 716 | $html .= textfield(-name => $self->config_name('query')); | ||
| 717 | |||
| 718 | =item $self->reconfigure() | ||
| 719 | |||
| 720 | If you implement a configure_form() method, you must also implement a | ||
| 721 | reconfigure() method. This method is called after the user submits | ||
| 722 | the form and should be used to integrate the form values with the | ||
| 723 | current configuration. | ||
| 724 | |||
| 725 | Remember that the form fields are namespaced. You may recover them | ||
| 726 | using the CGI param() method by preceding them with the proper prefix. | ||
| 727 | To make this easier to manage, this module provides a config_param() | ||
| 728 | method that manages the namespaces transparently. | ||
| 729 | |||
| 730 | Here is a working example: | ||
| 731 | |||
| 732 | sub reconfigure { | ||
| 733 | my $self = shift; | ||
| 734 | my $current_configuration = $self->configuration; | ||
| 735 | $current_configuration->{query} = $self->config_param('query'); | ||
| 736 | } | ||
| 737 | |||
| 738 | All this does is to retrieve the current configuration by calling the | ||
| 739 | configuration() method. The value of the "query" key is then replaced | ||
| 740 | by a fill-out form parameter named "query", using config_param() | ||
| 741 | instead of the more familiar CGI module's param() function. | ||
| 742 | |||
| 743 | =back | ||
| 744 | |||
| 745 | =cut | ||
| 746 | |||
| 747 | |||
| 748 | use strict; | ||
| 749 | use Bio::Graphics::Browser2; | ||
| 750 | use Data::Dumper; | ||
| 751 | use Digest::MD5 'md5_hex'; | ||
| 752 | use CGI qw(url header p); | ||
| 753 | |||
| 754 | $Data::Dumper::Sortkeys = 1; | ||
| 755 | |||
| 756 | use vars '$VERSION','@ISA','@EXPORT'; | ||
| 757 | $VERSION = '0.30'; | ||
| 758 | |||
| 759 | # currently doesn't inherit | ||
| 760 | @ISA = (); | ||
| 761 | |||
| 762 | # currently doesn't export | ||
| 763 | @EXPORT = (); | ||
| 764 | |||
| 765 | sub new { | ||
| 766 | my $class = shift; | ||
| 767 | return bless {},$class; | ||
| 768 | } | ||
| 769 | |||
| 770 | # initialize other globals | ||
| 771 | sub init { | ||
| 772 | my $self = shift; | ||
| 773 | # do nothing | ||
| 774 | } | ||
| 775 | |||
| 776 | sub name { | ||
| 777 | my $self = shift; | ||
| 778 | return "generic"; | ||
| 779 | } | ||
| 780 | |||
| 781 | sub id { | ||
| 782 | my $self = shift; | ||
| 783 | my $class = ref($self) || $self; | ||
| 784 | my ($id) = $class =~ /(\w+)$/; | ||
| 785 | return $id; | ||
| 786 | } | ||
| 787 | |||
| 788 | # return nothing unless the plugin overides this method | ||
| 789 | sub verb { | ||
| 790 | my $self = shift; | ||
| 791 | return ''; | ||
| 792 | } | ||
| 793 | |||
| 794 | # return nothing unless the plugin overrides this method | ||
| 795 | sub suppress_title { | ||
| 796 | my $self = shift; | ||
| 797 | return ''; | ||
| 798 | } | ||
| 799 | |||
| 800 | sub description { | ||
| 801 | my $self = shift; | ||
| 802 | my $language = $self->language; | ||
| 803 | return p($self->language->tr('PLUGIN_BASE_CLASS_DESC')); | ||
| 804 | } | ||
| 805 | |||
| 806 | sub type { | ||
| 807 | my $self = shift; | ||
| 808 | return 'dumper'; | ||
| 809 | } | ||
| 810 | |||
| 811 | sub mime_type { | ||
| 812 | return 'text/plain'; | ||
| 813 | } | ||
| 814 | |||
| 815 | sub config_defaults { | ||
| 816 | my $self = shift; | ||
| 817 | return; # no configuration | ||
| 818 | } | ||
| 819 | |||
| 820 | sub configuration { | ||
| 821 | my $self = shift; | ||
| 822 | my $d = $self->{g_config}; | ||
| 823 | if (@_) { | ||
| 824 | $self->{g_config} = shift; | ||
| 825 | } | ||
| 826 | $d; | ||
| 827 | } | ||
| 828 | |||
| 829 | sub renderer { | ||
| 830 | my $self = shift; | ||
| 831 | my $d = $self->{render}; | ||
| 832 | $self->{render} = shift if @_; | ||
| 833 | $d; | ||
| 834 | } | ||
| 835 | |||
| 836 | sub configure_form { | ||
| 837 | return; | ||
| 838 | } | ||
| 839 | |||
| 840 | sub reconfigure { | ||
| 841 | my $self = shift; | ||
| 842 | # do nothing | ||
| 843 | } | ||
| 844 | |||
| 845 | sub authenticate { | ||
| 846 | my $self = shift; | ||
| 847 | # do nothing | ||
| 848 | } | ||
| 849 | |||
| 850 | sub authentication_hint { | ||
| 851 | my $self = shift; | ||
| 852 | # return nothing | ||
| 853 | } | ||
| 854 | |||
| 855 | # get/store database | ||
| 856 | sub database { | ||
| 857 | my $self = shift; | ||
| 858 | my $d = $self->{g_database}; | ||
| 859 | $self->{g_database} = shift if @_; | ||
| 860 | $d; | ||
| 861 | } | ||
| 862 | |||
| 863 | sub all_databases { | ||
| 864 | my $self = shift; | ||
| 865 | my $source = $self->browser_config; | ||
| 866 | my @db_labels = $source->databases; | ||
| 867 | return map {$source->open_database($_)} @db_labels; | ||
| 868 | } | ||
| 869 | |||
| 870 | # get/store language | ||
| 871 | sub language { | ||
| 872 | my $self = shift; | ||
| 873 | my $d = $self->{g_language}; | ||
| 874 | $self->{g_language} = shift if @_; | ||
| 875 | $d; | ||
| 876 | } | ||
| 877 | |||
| 878 | # get/store configuration file | ||
| 879 | # it's a Bio::Graphics::Browser file | ||
| 880 | sub browser_config { | ||
| 881 | my $self = shift; | ||
| 882 | my $d = $self->{g_config_file}; | ||
| 883 | $self->{g_config_file} = shift if @_; | ||
| 884 | $d; | ||
| 885 | } | ||
| 886 | |||
| 887 | # get/store page settings | ||
| 888 | # it's a big hash as described in the notes of the gbrowse executable | ||
| 889 | sub page_settings { | ||
| 890 | my $self = shift; | ||
| 891 | my $d = $self->{g_page_settings}; | ||
| 892 | $self->{g_page_settings} = shift if @_; | ||
| 893 | $d; | ||
| 894 | } | ||
| 895 | |||
| 896 | # get/store configuration directory path | ||
| 897 | sub config_path { | ||
| 898 | my $self = shift; | ||
| 899 | my $d = $self->{g_config_path}; | ||
| 900 | $self->{g_config_path} = shift if @_; | ||
| 901 | $d; | ||
| 902 | } | ||
| 903 | |||
| 904 | # get/store the current segments | ||
| 905 | sub segments { | ||
| 906 | my $self = shift; | ||
| 907 | my $d = $self->{segments}; | ||
| 908 | $self->{segments} = shift if @_; | ||
| 909 | $d; | ||
| 910 | } | ||
| 911 | |||
| 912 | # get/store a RegionSearch object (see Bio::Graphics::Browser2::RegionSearch) | ||
| 913 | sub db_search { | ||
| 914 | my $self = shift; | ||
| 915 | my $d = $self->{db_search}; | ||
| 916 | $self->{db_search} = shift if @_; | ||
| 917 | $d; | ||
| 918 | } | ||
| 919 | |||
| 920 | # just dump out the name of the thing | ||
| 921 | sub dump { | ||
| 922 | my $self = shift; | ||
| 923 | my $segment = shift; | ||
| 924 | my $language = $self->language; | ||
| 925 | print header('text/plain'); | ||
| 926 | print $self->language->tr('PLUGIN_BASE_CLASS_DUMP'); | ||
| 927 | } | ||
| 928 | |||
| 929 | sub find { | ||
| 930 | my $self = shift; | ||
| 931 | my $segment = shift; | ||
| 932 | return (); | ||
| 933 | } | ||
| 934 | |||
| 935 | sub annotate { | ||
| 936 | my $self = shift; | ||
| 937 | my $segment = shift; | ||
| 938 | my $coordinate_mapper = shift; | ||
| 939 | # do nothing | ||
| 940 | return; | ||
| 941 | } | ||
| 942 | |||
| 943 | sub filter_tracks { | ||
| 944 | my $self = shift; | ||
| 945 | my ($tracks,$source) = @_; | ||
| 946 | return @$tracks; # pass 'em all through | ||
| 947 | } | ||
| 948 | |||
| 949 | sub hilite_terms { | ||
| 950 | my $self = shift; | ||
| 951 | return; | ||
| 952 | } | ||
| 953 | |||
| 954 | sub pkg { | ||
| 955 | my $self = shift; | ||
| 956 | my $class = ref $self or return; | ||
| 957 | $class =~ /(\w+)$/ or return; | ||
| 958 | return $1; | ||
| 959 | } | ||
| 960 | |||
| 961 | sub config_param { | ||
| 962 | my $self = shift; | ||
| 963 | my $pkg = $self->pkg; | ||
| 964 | unless (@_) { | ||
| 965 | my @result; | ||
| 966 | foreach (CGI::param()) { | ||
| 967 | next unless /^$pkg\.(.+)/; | ||
| 968 | push @result,$1; | ||
| 969 | } | ||
| 970 | return @result; | ||
| 971 | } | ||
| 972 | CGI::param($self->config_name(shift())); | ||
| 973 | } | ||
| 974 | |||
| 975 | sub config_name { | ||
| 976 | my $self = shift; | ||
| 977 | my $name = shift; | ||
| 978 | my $pkg = $self->pkg; | ||
| 979 | return "$pkg.$name"; | ||
| 980 | } | ||
| 981 | |||
| 982 | sub selected_tracks { | ||
| 983 | my $self = shift; | ||
| 984 | my $page_settings = $self->page_settings; | ||
| 985 | return grep {!/^_/ && $page_settings->{features}{$_}{visible}} @{$page_settings->{tracks}}; | ||
| 986 | } | ||
| 987 | |||
| 988 | sub selected_features { | ||
| 989 | my $self = shift; | ||
| 990 | my $conf = $self->browser_config; | ||
| 991 | my @tracks = $self->selected_tracks; | ||
| 992 | return map {$conf->config->label2type($_)} @tracks; | ||
| 993 | } | ||
| 994 | |||
| 995 | # called by annotators when they need to create a new list of features | ||
| 996 | sub new_feature_list { | ||
| 997 | my $self = shift; | ||
| 998 | return Bio::Graphics::FeatureFile->new(-smart_features=>1, | ||
| 999 | -safe => 1); | ||
| 1000 | } | ||
| 1001 | |||
| 1002 | # install the plugin but do not show it in the "Reports & Analysis" menu | ||
| 1003 | # off by default | ||
| 1004 | sub hide {} | ||
| 1005 | |||
| 1006 | # front end to $self->browser_config->plugin_setting | ||
| 1007 | sub setting { | ||
| 1008 | my $self = shift; | ||
| 1009 | my $setting_name = shift; | ||
| 1010 | |||
| 1011 | eval "use Class::ISA"; | ||
| 1012 | |||
| 1013 | my $config = $self->browser_config; | ||
| 1014 | my $globals = $config->globals; | ||
| 1015 | |||
| 1016 | my @classes = Class::ISA->can('self_and_super_path') | ||
| 1017 | ? Class::ISA::self_and_super_path(ref $self) | ||
| 1018 | : ref $self; | ||
| 1019 | for (@classes) { | ||
| 1020 | my ($last_name) = /(\w+)$/; | ||
| 1021 | my $option_name = "${last_name}:plugin"; | ||
| 1022 | if ($setting_name) { | ||
| 1023 | my $result = $config->setting($option_name => $setting_name) | ||
| 1024 | || $globals->setting($option_name=>$setting_name); | ||
| 1025 | return $result if defined $result; | ||
| 1026 | } else { | ||
| 1027 | my @options = $config->label_options($option_name); | ||
| 1028 | @options = $globals->label_options($option_name) unless @options; | ||
| 1029 | return @options if @options; | ||
| 1030 | } | ||
| 1031 | } | ||
| 1032 | return; | ||
| 1033 | } | ||
| 1034 | |||
| 1035 | sub config_hash { | ||
| 1036 | return md5_hex( Dumper( shift->configuration ) ); | ||
| 1037 | } | ||
| 1038 | |||
| 1039 | # A list of all extra scripts required by a plugin, defaults to nothing. | ||
| 1040 | sub scripts { | ||
| 1041 | my $self = shift; | ||
| 1042 | return; | ||
| 1043 | } | ||
| 1044 | |||
| 1045 | # A list of all additional stylesheets required by a plugin, defaults to nothing. | ||
| 1046 | sub stylesheets { | ||
| 1047 | my $self = shift; | ||
| 1048 | return; | ||
| 1049 | } | ||
| 1050 | |||
| 1051 | |||
| 1052 | # A list of all Javascript commands to be run once the page or tab is loaded (in either the body's onLoad section, or the callback event from the tab's rendering animation). | ||
| 1053 | # Should return a hash, key is the section which contains the elements plus "_select", value is the function (e.g. "track_page_select", "some.function();"). | ||
| 1054 | # Will accet "main_page_select", "track_page_select", "custom_track_page_select", "settings_page_select" and "body" as keys. | ||
| 1055 | sub onLoads { | ||
| 1056 | my $self = shift; | ||
| 1057 | return; | ||
| 1058 | } | ||
| 1059 | |||
| 1060 | sub DESTROY { | ||
| 1061 | my $self = shift; | ||
| 1062 | # warn "$self is destroyed"; | ||
| 1063 | } | ||
| 1064 | |||
| 1065 | |||
| 1066 | 1; | ||
| 1067 | |||
| 1068 | __END__ |