| 1 | package Bio::Graphics::Browser2::Plugin::FastaDumper; | ||
| 2 | # $Id: FastaDumper.pm,v 1.11 2005-12-09 22:19:09 mwz444 Exp $ | ||
| 3 | # test plugin | ||
| 4 | use strict; | ||
| 5 | use Bio::Graphics::Browser2::Plugin; | ||
| 6 | use Bio::Graphics::Browser2::Markup; | ||
| 7 | |||
| 8 | use CGI qw(:standard ); | ||
| 9 | |||
| 10 | use constant DEBUG => 0; | ||
| 11 | |||
| 12 | use vars qw($VERSION @ISA @MARKUPS %LABELS | ||
| 13 | $BACKGROUNDUPPER %COLORNAMES $PANEL); | ||
| 14 | |||
| 15 | my @COLORS = qw(red brown magenta maroon pink orange | ||
| 16 | yellow tan teal cyan lime green blue | ||
| 17 | lightgrey grey darkgrey | ||
| 18 | ); | ||
| 19 | |||
| 20 | BEGIN { | ||
| 21 | $BACKGROUNDUPPER = 'YELLOW'; | ||
| 22 | @MARKUPS = ( undef, # none | ||
| 23 | "UPPERCASE", # for uppercase | ||
| 24 | 'Font-weight: bold', | ||
| 25 | 'Text-decoration: underline', | ||
| 26 | 'Font-style: italic', | ||
| 27 | 'FGCOLOR %s', | ||
| 28 | 'BGCOLOR %s', | ||
| 29 | ); | ||
| 30 | |||
| 31 | %LABELS = ( 0 => 'None', | ||
| 32 | 1 => 'CAPS', | ||
| 33 | 2 => '<b>Bold</b>', | ||
| 34 | 3 => '<u>Underline</u>', | ||
| 35 | 4 => '<i>Italics</i>', | ||
| 36 | 5 => '<span style="Color: red">Font</span>', | ||
| 37 | 6 => '<span style="Background-color: red">Bkg</span>', | ||
| 38 | ); | ||
| 39 | } | ||
| 40 | |||
| 41 | $VERSION = '0.30'; | ||
| 42 | |||
| 43 | @ISA = qw(Bio::Graphics::Browser2::Plugin); | ||
| 44 | |||
| 45 | sub name { "Decorated FASTA File" } | ||
| 46 | sub description { | ||
| 47 | p("The marked-up FASTA dumper plugin dumps out the currently displayed genomic segment", | ||
| 48 | "in FASTA format."). | ||
| 49 | p("This plugin was written by Lincoln Stein and Jason Stajich."); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub dump { | ||
| 53 | my $self = shift; | ||
| 54 | my $segment = shift; | ||
| 55 | |||
| 56 | unless ($segment) { | ||
| 57 | my $mime_type = $self->mime_type; | ||
| 58 | print start_html($self->name) if $mime_type =~ /html/; | ||
| 59 | print "No sequence specified.\n"; | ||
| 60 | print end_html if $mime_type =~ /html/; | ||
| 61 | exit 0; | ||
| 62 | } | ||
| 63 | |||
| 64 | my $config = $self->configuration; | ||
| 65 | my $dna = lc $segment->dna; | ||
| 66 | my $browser = $self->browser_config(); | ||
| 67 | warn("====== beginning dump =====\n") if DEBUG; | ||
| 68 | warn "length of dna = ",length($dna) if DEBUG; | ||
| 69 | |||
| 70 | my %types; | ||
| 71 | |||
| 72 | my $flip = defined $config->{flip} ? $config->{flip} | ||
| 73 | : $self->page_settings->{flip}; | ||
| 74 | if ($flip) { | ||
| 75 | $dna = reverse $dna; | ||
| 76 | $dna =~ tr/gatcGATC/ctagCTAG/; | ||
| 77 | } | ||
| 78 | my $markup = Bio::Graphics::Browser2::Markup->new; | ||
| 79 | |||
| 80 | while( my ($type,$val) = each %{$config} ) { | ||
| 81 | |||
| 82 | |||
| 83 | next unless $val; | ||
| 84 | next if $type =~ /\.(f|b)gcolor$/i; | ||
| 85 | next if $type =~ /format$/; | ||
| 86 | next if $type =~ /orientation$/; | ||
| 87 | |||
| 88 | warn "configuring $type => $val\n" if DEBUG; | ||
| 89 | |||
| 90 | my $style = $MARKUPS[$val] || ''; | ||
| 91 | if ($style =~ /^(F|B)GCOLOR/) { | ||
| 92 | $style = sprintf($style,$config->{"$type.\L$1\Egcolor"}); | ||
| 93 | } | ||
| 94 | next if $config->{format} eq 'text' && $style ne 'UPPERCASE'; | ||
| 95 | |||
| 96 | (my $feature_type = $type) =~ s/^[^.]+\.//; | ||
| 97 | # there may be several feature types defined for each track | ||
| 98 | my @types = $browser->label2type($feature_type) or next; | ||
| 99 | for my $t (@types) { | ||
| 100 | $markup->add_style($t => $style); | ||
| 101 | warn "adding style $t => $style\n" if DEBUG | ||
| 102 | } | ||
| 103 | |||
| 104 | foreach (@types) { $types{$_}++ }; | ||
| 105 | } | ||
| 106 | |||
| 107 | my @regions_to_markup = $self->make_markup($segment,[keys %types],$markup,$flip) if %types; | ||
| 108 | |||
| 109 | # add a newline every 60 positions | ||
| 110 | $markup->add_style('newline',"\n"); | ||
| 111 | push @regions_to_markup,map {['newline',60*$_]} (1..length($dna)/60); | ||
| 112 | $markup->markup(\$dna,\@regions_to_markup); | ||
| 113 | |||
| 114 | my $label = "$segment"; | ||
| 115 | $label .= " (reverse complemented)" if $flip; | ||
| 116 | |||
| 117 | # HTML formatting | ||
| 118 | if ($config->{format} eq 'html') { | ||
| 119 | |||
| 120 | print start_html($segment); #,h1($label); | ||
| 121 | print pre(">$label\n$dna"); | ||
| 122 | print end_html; | ||
| 123 | } | ||
| 124 | |||
| 125 | # text/plain formatting | ||
| 126 | else { | ||
| 127 | print ">$label\n"; | ||
| 128 | print $dna; | ||
| 129 | } | ||
| 130 | warn("====== end of dump =====\n") if DEBUG; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub mime_type { | ||
| 134 | my $self = shift; | ||
| 135 | my $config = $self->configuration; | ||
| 136 | return $config->{format} eq 'html' ? 'text/html' : 'text/plain'; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub config_defaults { | ||
| 140 | my $self = shift; | ||
| 141 | return { format => 'html' }; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub reconfigure { | ||
| 145 | my $self = shift; | ||
| 146 | my $current_config = $self->configuration; | ||
| 147 | %$current_config = (); | ||
| 148 | |||
| 149 | foreach my $param ( $self->config_param() ) { | ||
| 150 | warn "param = $param\n" if DEBUG; | ||
| 151 | next if $param =~/\.(f|b)gcolor$/; | ||
| 152 | my $value = $self->config_param($param) or next; | ||
| 153 | $current_config->{$param} = $value; | ||
| 154 | warn "current_config($param) = $current_config->{$param}\n" if DEBUG; | ||
| 155 | } | ||
| 156 | # handle colors specially | ||
| 157 | for my $type (keys %$current_config) { | ||
| 158 | next unless $current_config->{$type} =~ /^\d+$/; | ||
| 159 | next unless $MARKUPS[$current_config->{$type}] =~ /^(F|B)GCOLOR/; | ||
| 160 | my $color_key = lc("$1gcolor"); | ||
| 161 | $current_config->{"$type.$color_key"} = $self->config_param("$type.$color_key"); | ||
| 162 | warn "current_config($type.$color_key) = ",$current_config->{"$type.$color_key"},"\n" if DEBUG; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | sub configure_form { | ||
| 167 | my $self = shift; | ||
| 168 | my $current_config = $self->configuration; | ||
| 169 | my @choices = TR({-class => 'searchtitle'}, | ||
| 170 | th({-align=>'RIGHT',-width=>'25%'},"Output", | ||
| 171 | td(radio_group(-name => $self->config_name('format'), | ||
| 172 | -values => [qw(text html)], | ||
| 173 | -default => $current_config->{'format'}, | ||
| 174 | -override => 1)) | ||
| 175 | ) | ||
| 176 | ); | ||
| 177 | push @choices,TR({-class=>'searchtitle'}, | ||
| 178 | th({-align=>'RIGHT',-width=>'25%'},"Orientation", | ||
| 179 | td(checkbox(-name => $self->config_name('flip'), | ||
| 180 | -label => 'Flip', | ||
| 181 | -checked => $self->page_settings->{flip}, | ||
| 182 | -override => 1)))); | ||
| 183 | |||
| 184 | my $browser = $self->browser_config(); | ||
| 185 | # this to be fixed as more general | ||
| 186 | my @labels; | ||
| 187 | foreach ( $browser->labels() ) { | ||
| 188 | push @labels, $_ unless ! defined $browser->setting($_,'feature'); | ||
| 189 | } | ||
| 190 | |||
| 191 | autoEscape(0); | ||
| 192 | my %selected = map {$_=>1} ($self->selected_tracks); | ||
| 193 | |||
| 194 | foreach my $featuretype ( @labels ) { | ||
| 195 | next if ! $selected{$featuretype}; | ||
| 196 | my $realtext = $browser->setting($featuretype,'key') || $featuretype; | ||
| 197 | push @choices, TR({-class => 'searchtitle'}, | ||
| 198 | th({-align=>'RIGHT',-width=>'25%'}, $realtext, | ||
| 199 | td(join (' ', | ||
| 200 | radio_group(-name => $self->config_name($featuretype), | ||
| 201 | -values => [ (sort keys %LABELS)[0..4] ], | ||
| 202 | -labels => \%LABELS, | ||
| 203 | -default => $current_config->{$featuretype} || 0), | ||
| 204 | radio_group(-name => $self->config_name($featuretype), | ||
| 205 | -values => 5, | ||
| 206 | -labels => \%LABELS, | ||
| 207 | -default => $current_config->{$featuretype} || 0), | ||
| 208 | popup_menu(-name => $self->config_name("$featuretype.fgcolor"), | ||
| 209 | -values => \@COLORS, | ||
| 210 | -default => $current_config->{"$featuretype.fgcolor"}), | ||
| 211 | radio_group(-name => $self->config_name($featuretype), | ||
| 212 | -values => 6, | ||
| 213 | -labels => \%LABELS, | ||
| 214 | -default => $current_config->{$featuretype} || 0), | ||
| 215 | popup_menu(-name => $self->config_name("$featuretype.bgcolor"), | ||
| 216 | -values => \@COLORS, | ||
| 217 | -default => $current_config->{"$featuretype.bgcolor"} | ||
| 218 | ), | ||
| 219 | )))); | ||
| 220 | } | ||
| 221 | autoEscape(1); | ||
| 222 | my $html= table({-width=>'100%'},@choices); | ||
| 223 | $html; | ||
| 224 | } | ||
| 225 | |||
| 226 | |||
| 227 | sub make_markup { | ||
| 228 | my $self = shift; | ||
| 229 | my ($segment,$types,$markup,$flip) = @_; | ||
| 230 | |||
| 231 | my @regions_to_markup; | ||
| 232 | |||
| 233 | warn("segment length is ".$segment->length()."\n") if DEBUG; | ||
| 234 | my $iterator = $segment->get_seq_stream(-types=>$types, | ||
| 235 | -automerge=>1) or return; | ||
| 236 | my $segment_start = $segment->start; | ||
| 237 | my $segment_end = $segment->end; | ||
| 238 | my $segment_length = $segment->length; | ||
| 239 | |||
| 240 | while (my $markupregion = $iterator->next_seq) { | ||
| 241 | |||
| 242 | warn "got feature $markupregion\n" if DEBUG; | ||
| 243 | |||
| 244 | # handle both sub seqfeatures and split locations... | ||
| 245 | # somebody rescue me from this insanity! | ||
| 246 | my @parts = eval { $markupregion->sub_SeqFeature } ; | ||
| 247 | @parts = eval { my $id = $markupregion->location->seq_id; | ||
| 248 | my @subs = $markupregion->location->sub_Location; | ||
| 249 | grep {$id eq $_->seq_id} @subs } unless @parts; | ||
| 250 | @parts = ($markupregion) unless @parts; | ||
| 251 | |||
| 252 | for my $p (@parts) { | ||
| 253 | my $start = $p->start - $segment_start; | ||
| 254 | my $end = $start + $p->length; | ||
| 255 | |||
| 256 | $start++ if $p->strand < 0; | ||
| 257 | ($start,$end) = map {$segment_length-$_} ($end,$start) if $flip; | ||
| 258 | |||
| 259 | warn("$p ". $p->location->to_FTstring() . " type is ".$p->primary_tag) if DEBUG; | ||
| 260 | $start = 0 if $start < 0; # this can happen | ||
| 261 | $end = $segment->length if $end > $segment->length; | ||
| 262 | warn "annotating $p $start..$end" if DEBUG; | ||
| 263 | |||
| 264 | my $style_symbol; | ||
| 265 | foreach ($p->type,$p->method,$markupregion->type,$markupregion->method) { | ||
| 266 | $style_symbol ||= $markup->valid_symbol($_) ? $_ : undef; | ||
| 267 | } | ||
| 268 | warn "style symbol for $p is $style_symbol, and style is ",$markup->style($style_symbol),"\n" if DEBUG; | ||
| 269 | next unless $style_symbol; | ||
| 270 | |||
| 271 | warn "[$style_symbol,$start,$end]\n" if DEBUG; | ||
| 272 | push @regions_to_markup,[$style_symbol,$start,$end]; | ||
| 273 | } | ||
| 274 | } | ||
| 275 | @regions_to_markup; | ||
| 276 | } | ||
| 277 | |||
| 278 | 1; |