blib/lib/Bio/Graphics/Browser2/Markup.pm untested

filter: Statementstmt 0.0% 0 / 205 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 30 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 332 CC 32 SCAR 34.9
CC
32
Cov
0%
_unify
90.0
add_style
42.0
markup
42.0
CRAP 32.9
1 package Bio::Graphics::Browser2::Markup;
2  
3 use strict;
4 use Carp 'croak';
5 use Bio::Graphics::Panel;
6  
7 =head1 NAME
8  
9 Bio::Graphics::Browser2::Markup - Markup routines for sequences in text form
10  
11 =head1 VERSION (CVS-info)
12  
13 $RCSfile: Markup.pm,v $
14 $Revision$
15 $Author: lstein $
16 $Date: 2008-09-09 20:53:36 $
17  
18 =head1 SYNOPSIS
19  
20 use Bio::Graphics::Browser2::Markup;
21  
22 my $string = join '','a'..'z','a'..'z','a'..'z';
23 my $markup = Bio::Graphics::Browser2::Markup->new;
24  
25 $markup->add_style(cds=>'UPPERCASE');
26 $markup->add_style(exon =>'Text-decoration: underline');
27 $markup->add_style(variation=>'Font-weight: bold');
28 $markup->add_style(italic=>'Font-style: oblique');
29 $markup->add_style(yellow=>'BGCOLOR blue');
30 $markup->add_style(green=>'BGCOLOR red');
31 $markup->add_style(orange=>'BGCOLOR orange');
32 $markup->add_style(mango=>'FGCOLOR red');
33 $markup->add_style(br=>'<br>');
34 $markup->markup(\$string,[
35 ['cds',1=>10],
36 ['cds',12=>15],
37 ['variation',20=>41],
38 ['exon',0=>29],
39 ['exon',32=>40],
40 ['italic',18=>29],
41 ['yellow',5=>40],
42 ['green',20=>50],
43 ['orange',30=>60],
44 ['mango',0=>36],
45 ['br',10=>10],
46 ['br',20=>20],
47 ['br',30=>30],
48 ['br',40=>40],
49 ['br',50=>50],
50 ['br',60=>60],
51 ]);
52 print $string,"\n";
53  
54 =head1 DESCRIPTION
55  
56 This module marks up a string with HTML cascading stylesheet styles in
57 such a way that intersecting regions contain the union of the two
58 intersecting styles. It also handles colors in such a way that
59 intersecting colors are added up.
60  
61 =head1 METHODS
62  
63  
64 =head2 $annotator = Bio::Graphics::Browser2::Markup->new
65  
66 Create a new Markup object.
67  
68 =cut
69  
70 my $SYNTHETIC_SYMBOL = "CLR00000000000";
71  
72 sub new {
73 my $class = shift;
74 my $self = {
75 colors => {},
76 seq => 0,
77 symbols => {},
78 };
79 return bless $self,ref($class) || $class;
80 }
81  
82  
83 =head2 $old_style = $annotator->add_style($symbolic_name=>$style)
84  
85 Add an annotation style.
86  
87 $symbolic name is a unique identifier to be used for ornamenting the
88 string.
89  
90 $style is one of:
91  
92 - a CSS/2 stylesheet entry of form "style: value"
93 - the word "UPPERCASE" (make uppercase)
94 - the word "LOWERCASE" (make lowercase)
95 - the phrase "FGCOLOR #RRGGBB" (foreground color)
96 - the phrase "FGCOLOR color-name"
97 - the phrase "BGCOLOR #RRGGBB" (background color)
98 - the phrase "BGCOLOR color-name"
99 - an HTML tag, indicated by a leading "E<lt>" character
100 - anything else, will be inserted blindly
101  
102 When calculating intersected regions, styles will be aggregated, upper
103 and lowercasing will be performed directly on the data, colors will be
104 additive, and HTML will be inserted blindly.
105  
106 An invalid color name will cause this module to die. Valid color
107 names can be obtained this way:
108  
109 perl -MBio::Graphics::Panel \
110 -e 'print join "\n",sort Bio::Graphics::Panel->color_names'
111  
112 =cut
113  
114 sub add_style {
115 my $self = shift;
116 my ($symbolic_name,$style) = @_;
117  
118 my $entry;
119  
120 if ($style =~ /^(UPPER|LOWER)CASE$/) { # upper/lower case
121 $entry = [uclc => $1];
122 }
123  
124 elsif ($style =~ /^(FG|BG)COLOR\s+\#([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])/){ # a hex color
125 $entry = [lc($1).'color' => [hex $2, hex $3, hex $4]];
126 }
127  
128 elsif ($style =~ /^(FG|BG)COLOR\s+(\w+)/) { # a symbolic color
129 my $rgb = Bio::Graphics::Panel->color_name_to_rgb($2) or croak "invalid color name";
130 $entry = [lc($1).'color' => $rgb];
131 }
132  
133 elsif ($style =~ /^[\w-]+:.+/) { # css entry
134 $entry = [style => $style ];
135 }
136  
137 elsif ($style =~ /^<.+>$/) { # html
138 $entry = [html => $style];
139 }
140  
141 else { # something else, just insert it blindly
142 $entry = [literal => $style];
143 }
144  
145 $self->{symbols}{$symbolic_name} = $entry;
146 }
147  
148  
149 =head2 $style = $annotator->style($symbolic_name)
150  
151 Get the style corresponding to symbolic name, or undef if the name is
152 unrecognized.
153  
154 =cut
155  
156 sub style {
157 my $self = shift;
158 my $symbolic = shift;
159 return $self->{symbols}{$symbolic}[1];
160 }
161  
162 =head2 $style = $annotator->get_style($symbolic_name)
163  
164 Get a list of CSS/2 styles corresponding to symbolic name. Will die
165 if the name is not recognized or does not correspond to an entry of
166 type "style".
167  
168 =cut
169  
170 sub get_style {
171 my $self = shift;
172 my $symbolic = shift;
173 croak "invalid style" unless $self->{symbols}{$symbolic}[0] eq 'style';
174 return split /[:;]\s*/,$self->{symbols}{$symbolic}[1];
175 }
176  
177  
178  
179 =head2 $flag = $annotator->valid_symbol($symbolic_name)
180  
181 Return true if the symbolic name is valid.
182  
183 =cut
184  
185 sub valid_symbol {
186 my $self = shift;
187 my $symbolic = shift;
188 return exists $self->{symbols}{$symbolic};
189 }
190  
191  
192 =head2 $result = $annotator->markup(\$sequence,\@annotated_regions)
193  
194 Mark up the string referenced by $sequence, according to the regions
195 contained in $annotated_regions.
196  
197 $sequence is a scalar ref, which will be modified in place (make a
198 copy of it if you need to). $annotated_regions is a arrayref to the
199 following list:
200  
201 ([symbolic_name,start,end], [symbolic_name,start,end], ....)
202  
203 The result indicates whether the markup was successful.
204  
205 IMPORTANT: Regions are numbered using space-oriented coordinates,
206 which means that start=0 means to insert in front of the first base
207 pair, and an end equal to the sequence length will insert after the
208 last base pair:
209  
210 0 1 2 3 4 5 6 7 perl string coordinates
211 1 2 3 4 5 6 7 8 sequence coordinates
212 g a t c g a t c sequence
213 0 1 2 3 4 5 6 7 8 space coordinates
214  
215 To select first base: start=0, end=1
216 To insert markup between bases 1 and 2: start=1, end=1
217 To select last base: start=7, end=8
218 To select entire sequence: start=0, end=8
219  
220 This means that some munging of the sequence annotation must be
221 performed, but it keeps the notation unambiguous.
222  
223 =cut
224  
225 sub markup {
226 my $self = shift;
227 my ($sequence,$regions) = @_;
228  
229 $self->{seq} = 0; # package global
230  
231 # classify regions by type
232 my %regions;
233 foreach (@$regions) {
234 my $type = $self->{symbols}{$_->[0]}[0] or croak "Unknown region type: $_->[0]";
235 push @{$regions{$type}},$_;
236 }
237  
238 # pull out the regions that ask for upper/lower case and
239 # give them the special treatment
240 foreach (@{$regions{uclc}}) {
241 my ($style,$start,$end) = @$_;
242 $self->{symbols}{$style}[1] eq 'UPPER' ? substr($$sequence,$start,$end-$start) =~ tr/a-z/A-Z/
243 : substr($$sequence,$start,$end-$start) =~ tr/A-Z/a-z/;
244 }
245  
246 my @style_regions;
247  
248 # meld the "color" regions into a set of additive regions
249 push @style_regions,$self->_add_colors('Color',$regions{fgcolor}) if $regions{fgcolor};
250 push @style_regions,$self->_add_colors('Background-color',$regions{bgcolor}) if $regions{bgcolor};
251  
252 # add the "style" regions
253 push @style_regions,@{$regions{style}} if $regions{style};
254  
255 my @tags = $self->_unify(\@style_regions);
256  
257 # add HTML markup
258 push @tags,$self->_linearize_html($regions{html});
259  
260 # add literals
261 push @tags,map { [$self->{symbols}{$_->[0]}[1],$_->[1],$self->{seq}++] } @{$regions{literal}}
262 if $regions{literal};
263  
264 # insert the tags in their proper place
265 $self->_add_markup($sequence,\@tags);
266  
267 1;
268 }
269  
270 =head2 Internal Methods (not for external use; documentation incomplete)
271  
272 =over 4
273  
274 =item @events = $annotator->_add_colors($style_tag,$regions)
275  
276 =back
277  
278 =cut
279  
280 # calculate the intersection of the regions with additive colors
281 sub _add_colors {
282 my $self = shift;
283 my ($style_tag,$regions) = @_;
284  
285 # convert regions into events
286 my $events = $self->_regions_to_events($regions);
287  
288 # insert new color events whenever we see two or more start events in a row
289 my @events;
290 my $current_color = [0,0,0];
291 my $current_position;
292  
293 for my $e (@$events) {
294 my ($event,$symbol,$position) = @$e;
295 my $rgb = $self->{symbols}{$symbol}[1] or croak "unknown color style";
296 my $cc = join '',@$current_color;
297  
298 if ($event eq 'start') {
299 push @events,[$self->_color_symbol($style_tag,$current_color),$current_position,$position]
300 if defined $current_position && $cc != 0;
301 $current_color = $self->_add_color($current_color,$rgb);
302 }
303  
304 else { # event eq 'end'
305 push @events,[$self->_color_symbol($style_tag,$current_color),$current_position,$position]
306 if defined $current_position && $cc != 0;
307 $current_color = $self->_subtract_color($current_color,$rgb);
308 }
309 $current_position = $position;
310 }
311 return @events;
312 }
313  
314 =over 4
315  
316 =item $style_symbol = $annotator->_add_colors($style_tag,$regions)
317  
318 =back
319  
320 =cut
321  
322 sub _color_symbol {
323 my $self = shift;
324 my ($style_tag,$color) = @_;
325 my $html_color = $self->_to_html_color($color);
326 if (!$self->{colors}{$style_tag,$html_color}) {
327 my $synthetic = $self->{colors}{$style_tag,$html_color} = $SYNTHETIC_SYMBOL++;
328 $self->add_style($synthetic,"$style_tag: $html_color");
329 }
330 $self->{colors}{$style_tag,$html_color}
331 }
332  
333 =over 4
334  
335 =item $color = $annotator->_add_color($color1,$color2)
336  
337 =back
338  
339 =cut
340  
341 sub _add_color {
342 my $self = shift;
343 my ($a_color,$b_color) = @_;
344 my @result;
345 for (0..2) {
346 my $result = $a_color->[$_] + $b_color->[$_];
347 push @result,$result;
348 }
349 \@result;
350 }
351  
352  
353 =over 4
354  
355 =item $color = $annotator->_subtract_color($color1,$color2)
356  
357 =back
358  
359 =cut
360  
361 sub _subtract_color {
362 my $self = shift;
363 my ($a_color,$b_color) = @_;
364 my @result;
365 for (0..2) {
366 my $result = $a_color->[$_] - $b_color->[$_];
367 push @result,$result;
368 }
369 \@result;
370 }
371  
372 =over 4
373  
374 =item $html_color = $annotator->_to_html_color($color)
375  
376 =back
377  
378 =cut
379  
380 sub _to_html_color {
381 my $self = shift;
382 my $rgb = shift;
383 my @hex = map {sprintf("%02X",$_ % 256)} @$rgb;
384 return '#'.join '',@hex;
385 }
386  
387 =over 4
388  
389 =item @tag_positions = $annotator->_unify($region_definitions)
390  
391 =back
392  
393 =cut
394  
395 sub _unify {
396 my $self = shift;
397 my $regions = shift;
398  
399 # convert regions into events
400 my $events = $self->_regions_to_events($regions);
401  
402 my @result;
403 my %current_symbols;
404 my $open = 0;
405  
406 for my $e (@$events) {
407 my ($event,$symbol,$position,$seq) = @$e;
408  
409 if ($event eq 'start') {
410 $current_symbols{$symbol}++;
411  
412 if ($open++) {
413 if ($result[-1][1] < $position) { # this test inhibits empty <span></span> sections
414 push @result,[qq(</span>),$position,$self->{seq}++];
415 } else {
416 pop @result; # suppress empty tag sections
417 }
418 }
419  
420 my $style = $self->_to_style(\%current_symbols);
421 push @result,[qq(<span style="$style">),$position,$self->{seq}++];
422 }
423  
424 elsif ($event eq 'end') {
425 $current_symbols{$symbol}--;
426  
427 if ($open--) { # this test inhibits empty <span></span> sections
428 if ($result[-1][1] < $position) {
429 push @result,[qq(</span>),$position,$self->{seq}++];
430 } else {
431 pop @result; # suppress empty tag sections
432 }
433 }
434 next unless $open;
435  
436 my $style = $self->_to_style(\%current_symbols);
437 push @result,[qq(<span style="$style">),$position,$self->{seq}++];
438 }
439 }
440 die "programmer error: open tags != close tags" if $open != 0;
441 return @result;
442 }
443  
444 =over 4
445  
446 =item $style_fragment = $annotator->_to_style($symbols)
447  
448 =back
449  
450 =cut
451  
452 sub _to_style {
453 my $self = shift;
454 my $symbols = shift;
455 my @active = sort grep {$symbols->{$_}>0} keys %$symbols;
456 my $symbol_key = join '-',@active;
457 return $self->{_style_cache}{$symbol_key}
458 if exists $self->{_style_cache}{$symbol_key};
459  
460 my %attributes;
461 foreach (@active) {
462 next unless $symbols->{$_}>0;
463 my %a = $self->get_style($_);
464 %attributes = (%attributes,%a);
465 }
466 my @style;
467 foreach (keys %attributes) {
468 push @style,"$_: $attributes{$_}";
469 }
470 my $style = join '; ',@style;
471  
472 return $self->{_style_cache}{$symbol_key} = $style;
473 }
474  
475 =over 4
476  
477 =item @tag_positions = $annotator->_linearize_html($region_definitions)
478  
479 =back
480  
481 =cut
482  
483 sub _linearize_html {
484 my $self = shift;
485 my $regions = shift;
486 my $events = $self->_regions_to_events($regions);
487 my @result;
488 my ($last_symbol,$last_position);
489 foreach (@$events) {
490 my ($event,$symbol,$pos,$seq) = @$_;
491 if ($event eq 'end'
492 && defined($last_symbol)
493 && $symbol eq $last_symbol
494 && $pos == $last_position) {
495 my $tag = $self->{symbols}{$symbol}[1];
496 $tag =~ s!>$! />!;
497 push @result,[$tag,$pos,$self->{seq}++];
498 undef $last_symbol;
499 next;
500 }
501  
502 if (defined $last_symbol) {
503 my $tag = $self->{symbols}{$last_symbol}[1];
504 push @result,[$tag,$pos,$self->{seq}++];
505 }
506  
507 if ($event eq 'start') {
508 $last_symbol = $symbol;
509 $last_position = $pos;
510 }
511 else {
512 my $tag = $self->{symbols}{$last_symbol}[1];
513 $tag =~ s!^<(\w+).*>$!</$1>!;
514 push @result,[$tag,$pos,-$self->{seq}++];
515 undef $last_symbol;
516 }
517 }
518 if (defined $last_symbol) {
519 my $tag = $self->{symbols}{$last_symbol}[1];
520 push @result,[$tag,$last_position,$self->{seq}++];
521 }
522  
523 @result;
524 }
525  
526 =over 4
527  
528 =item \@events = $annotator->_regions_to_events($regions)
529  
530 turn a series of regions into a series of start and end events
531 because of the problem of events that start/stop in the same place
532 each event also gets a sequence that can be used to keep events
533 matched in a nested way
534  
535 =back
536  
537 =cut
538  
539 sub _regions_to_events {
540 my $self = shift;
541 my $regions = shift;
542 my @events;
543 foreach (@$regions) {
544 $self->{seq}++;
545 my ($symbol,$start,$end) = @$_;
546 push @events,[start => $symbol,$start,$self->{seq}];
547 push @events,[end => $symbol,$end, $start == $end ? ++$self->{seq} : -$self->{seq}];
548 }
549 # now sort events according to their position, using the
550 # sequence to resolve ties. Notice the use of -$seq so
551 # that the sorting order of end ties is reversed relative
552 # to start ties. This causes regions to nest properly.
553 my @sorted_events = sort {$a->[2]<=>$b->[2] || $a->[3]<=>$b->[3]} @events;
554 \@sorted_events;
555 }
556  
557 =over 4
558  
559 =item $annotator->_add_markup($string_to_modify,$tag_positions)
560  
561 =back
562  
563 =cut
564  
565 sub _add_markup {
566 my $self = shift;
567 my $string = shift;
568 my $markups = shift;
569 for my $m (sort {$b->[1]<=>$a->[1] || $b->[2]<=>$a->[2]} @$markups) {
570 my ($thing,$position) = @$m;
571 next if $position > length($$string);
572 substr($$string,$position,0) = $thing;
573  
574 }
575 }
576  
577  
578 =head1 SEE ALSO
579  
580 L<Bio::Graphics::Panel>,
581 L<Bio::Graphics::Glyph>,
582 L<Bio::Graphics::Feature>,
583 L<Bio::Graphics::FeatureFile>,
584 L<Bio::Graphics::Browser>,
585 L<Bio::Graphics::Browser2::Plugin>
586  
587 =head1 AUTHOR
588  
589 Lincoln Stein E<lt>lstein@cshl.orgE<gt>.
590  
591 Copyright (c) 2002 Cold Spring Harbor Laboratory
592  
593 This library is free software; you can redistribute it and/or modify
594 it under the same terms as Perl itself. See DISCLAIMER.txt for
595 disclaimers of warranty.
596  
597 =cut
598  
599 1;
600  
601 __END__