« blib/lib/Bio/Graphics/Browser2/OptionPick.pm
↑ Summary
blib/lib/Bio/Graphics/Browser2/RemoteSet.pm »
| 1 | package Bio::Graphics::Browser2::Plugin::AttributeHiliter; | ||
| 2 | # $Id: AttributeHiliter.pm,v 1.3 2009-05-22 14:33:38 lstein Exp $ | ||
| 3 | use strict; | ||
| 4 | use Bio::Graphics::Browser2::Plugin; | ||
| 5 | use Bio::Graphics::Browser2::Util 'shellwords'; | ||
| 6 | use CGI qw(:standard); | ||
| 7 | |||
| 8 | use constant DEBUG => 0; | ||
| 9 | |||
| 10 | use vars qw($VERSION @ISA); | ||
| 11 | |||
| 12 | my @COLORS = ('',qw( | ||
| 13 | red brown magenta maroon pink orange | ||
| 14 | yellow tan teal cyan lime green blue | ||
| 15 | lightgrey grey darkgrey | ||
| 16 | )); | ||
| 17 | |||
| 18 | $VERSION = '0.01'; | ||
| 19 | |||
| 20 | @ISA = qw(Bio::Graphics::Browser2::Plugin); | ||
| 21 | |||
| 22 | sub name { "Selected Properties" } | ||
| 23 | sub description { | ||
| 24 | p("This plugin highlights features whose properties match certain criteria.", | ||
| 25 | "It was written by Lincoln Stein."); | ||
| 26 | } | ||
| 27 | |||
| 28 | sub type { 'highlighter' } | ||
| 29 | |||
| 30 | # This routine is a bit more complicated than it needs to be because of | ||
| 31 | # an optimization. What it does is to compile the highlighting pattern specified | ||
| 32 | # by the current configuration into a subroutine called "memoized_sub" and then | ||
| 33 | # invoke it. On subsequent invocations if the config hasn't changed, the | ||
| 34 | # compiled subroutine is reinvoked. Otherwise a new sub is compiled. The compiled | ||
| 35 | # sub can be seen by setting the DEBUG constant at the top of this file to true. | ||
| 36 | |||
| 37 | sub highlight { | ||
| 38 | my $self = shift; | ||
| 39 | my $feature = shift; | ||
| 40 | |||
| 41 | my $config = $self->configuration; | ||
| 42 | return unless %$config; | ||
| 43 | |||
| 44 | return $self->{memoized_sub}->($feature) | ||
| 45 | if $self->{memoized_sub} && $self->{memoized_config} eq join ' ',%$config; | ||
| 46 | |||
| 47 | my $sub = "sub { \n"; | ||
| 48 | $sub .= " my \$feature = shift;\n"; | ||
| 49 | |||
| 50 | for my $attribute (keys %$config) { | ||
| 51 | my ($color,$text) = split(/\s+/,$config->{$attribute},2); | ||
| 52 | next unless defined $color && defined $text; | ||
| 53 | |||
| 54 | warn "trying to colorize $attribute with text=$text, color = $color\n" if DEBUG; | ||
| 55 | |||
| 56 | my $regexp = quotemeta($text); | ||
| 57 | if ($attribute eq 'Feature Name') { | ||
| 58 | $sub .= " return '$color' if \$feature->display_name =~ /$regexp/i;\n"; | ||
| 59 | } elsif ($attribute eq 'Feature Type') { | ||
| 60 | $sub .= " return '$color' if \$feature->type =~ /$regexp/i;\n"; | ||
| 61 | } elsif (defined $attribute) { | ||
| 62 | $sub .= " return unless \$feature->can('attributes');\n"; | ||
| 63 | $sub .= " foreach (\$feature->attributes('$attribute')) { return '$color' if /$regexp/i }\n"; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | $sub .= " return\n}"; | ||
| 67 | warn $sub if DEBUG; | ||
| 68 | $self->{memoized_sub} = eval $sub or warn $@; | ||
| 69 | $self->{memoized_config} = join ' ',%$config; | ||
| 70 | return $self->{memoized_sub}->($feature) if $self->{memoized_sub}; | ||
| 71 | return; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub config_defaults { | ||
| 75 | my $self = shift; | ||
| 76 | return { }; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub reconfigure { | ||
| 80 | my $self = shift; | ||
| 81 | my $current_config = $self->configuration; | ||
| 82 | my %c; | ||
| 83 | foreach my $param ($self->config_param) { | ||
| 84 | warn "param = $param" if DEBUG; | ||
| 85 | my ($operation,$attribute) = $param =~ /(match|color)\.(.+)/ or next; | ||
| 86 | $c{$attribute}{$operation} = $self->config_param($param); | ||
| 87 | } | ||
| 88 | foreach my $attribute (keys %c) { | ||
| 89 | if ( (my $match_text = $c{$attribute}{match}) && (my $match_color = $c{$attribute}{color})) { | ||
| 90 | $current_config->{$attribute} = "$match_color $match_text"; | ||
| 91 | } else { | ||
| 92 | delete $current_config->{$attribute}; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | delete $self->{memoized_sub}; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub configure_form { | ||
| 99 | my $self = shift; | ||
| 100 | my $current_config = $self->configuration; | ||
| 101 | my @attributes = shellwords $self->browser_config->plugin_setting('attributes'); | ||
| 102 | unshift @attributes,'Feature Name','Feature Type'; | ||
| 103 | |||
| 104 | my @rows; | ||
| 105 | push @rows,TR({-class=>'searchtitle'},th(['Property','Text to Match','Highlight Color'])); | ||
| 106 | |||
| 107 | for my $attribute (@attributes) { | ||
| 108 | next unless $attribute; | ||
| 109 | my ($color,$text) = split(/\s+/,$current_config->{$attribute}||'',2); | ||
| 110 | push @rows,TR( | ||
| 111 | th({-class=>'searchtitle',-align=>'RIGHT'},$attribute), | ||
| 112 | td({-align=>'CENTER'},textfield(-name => $self->config_name("match.$attribute"), | ||
| 113 | -default => $text, | ||
| 114 | -size => 60)), | ||
| 115 | td(popup_menu(-name => $self->config_name("color.$attribute"), | ||
| 116 | -values=> \@COLORS, | ||
| 117 | -default => $color, | ||
| 118 | ))) | ||
| 119 | } | ||
| 120 | |||
| 121 | return table({-width=>'10%',-border=>0},@rows); | ||
| 122 | } | ||
| 123 | |||
| 124 | |||
| 125 | 1; | ||
| 126 | |||
| 127 | |||
| 128 | __END__ |