3
Bio::Graphics::Glyph::Factory - Factory for Bio::Graphics::Glyph objects
7
See L<Bio::Graphics::Panel>.
11
This class is used internally by Bio::Graphics to generate new Glyph
12
objects by combining a list of features with the user's desired
13
configuration. It is intended to be used internally by Bio::Graphics.
19
User feedback is an integral part of the evolution of this and other
20
Bioperl modules. Send your comments and suggestions preferably to one
21
of the Bioperl mailing lists. Your participation is much appreciated.
23
bioperl-l@bioperl.org - General discussion
24
http://bioperl.org/wiki/Mailing_lists - About the mailing lists
28
Report bugs to the Bioperl bug tracking system to help us keep track
29
the bugs and their resolution. Bug reports can be submitted via the
32
http://bugzilla.open-bio.org/
34
=head1 AUTHOR - Lincoln Stein
36
Email - lstein@cshl.org
40
L<Bio::Graphics::Panel>
44
The rest of the documentation details each of the object
45
methods. Internal methods are usually preceded with an "_"
50
package Bio::Graphics::Glyph::Factory;
53
use Carp qw(:DEFAULT cluck);
54
use Bio::Root::Version;
55
use base qw(Bio::Root::Root);
57
my %LOADED_GLYPHS = ();
58
my %GENERIC_OPTIONS = (
59
bgcolor => 'turquoise',
62
font2color => 'turquoise',
64
font => 'gdSmallFont', # This must be a string not method call
65
bump => +1, # bump by default (perhaps a mistake?)
71
Usage : $f = Bio::Graphics::Glyph::Factory->new(
72
-stylesheet => $stylesheet,
73
-glyph_map => $glyph_map,
74
-options => $options);
75
Function : create a new Bio::Graphics::Glyph::Factory object
76
Returns : the new object
77
Args : $stylesheet is a Bio::Das::Stylesheet object that can
78
convert Bio::Das feature objects into glyph names and
80
$glyph_map is a hash that maps primary tags to glyph names.
81
$options is a hash that maps option names to their values.
82
Status : Internal to Bio::Graphics
90
my $stylesheet = $args{-stylesheet}; # optional, for Bio::Das compatibility
91
my $map = $args{-map}; # map type name to glyph name
92
my $options = $args{-options}; # map type name to glyph options
94
stylesheet => $stylesheet,
104
Usage : $f2 = $f->clone
105
Function : Deep copy of a factory object
106
Returns : a deep copy of the factory object
108
Status : Internal to Bio::Graphics
115
my $new = bless \%new,ref($self);
122
Usage : $stylesheet = $f->stylesheet
123
Function : accessor for stylesheet
124
Returns : a Bio::Das::Stylesheet object
126
Status : Internal to Bio::Graphics
130
sub stylesheet { shift->{stylesheet} }
135
Usage : $map = $f->glyph_map
136
Function : accessor for the glyph map
137
Returns : a hash mapping primary tags to glyphs
139
Status : Internal to Bio::Graphics
143
sub glyph_map { shift->{glyph_map} }
148
Usage : $map = $f->option_map
149
Function : accessor for the option map
150
Returns : a hash mapping option names to values
152
Status : Internal to Bio::Graphics
156
sub option_map { shift->{options} }
161
Usage : $map = $f->global_opts
162
Function : accessor for global options
163
Returns : a hash mapping option names to values
165
Status : Internal to Bio::Graphics
167
This returns a set of defaults for option values.
171
sub global_opts{ shift->{global_opts} }
176
Usage : $panel = $f->panel
177
Function : accessor for Bio::Graphics::Panel
178
Returns : a Bio::Graphics::Panel
180
Status : Internal to Bio::Graphics
182
This returns the panel with which the factory is associated.
186
sub panel { shift->{panel} }
191
Usage : $scale = $f->scale
192
Function : accessor for the scale
193
Returns : a floating point number
195
Status : Internal to Bio::Graphics
197
This returns the scale, in pixels/bp for glyphs constructed by this
202
sub scale { shift->{panel}->scale }
207
Usage : $font = $f->font
208
Function : accessor for the font
209
Returns : a font name
211
Status : Internal to Bio::Graphics
213
This returns a GD font name.
220
$self->option($glyph,'font') || $self->{font};
226
Usage : @pixel_positions = $f->map_pt(@bp_positions)
227
Function : map bp positions to pixel positions
228
Returns : a list of pixel positions
229
Args : a list of bp positions
230
Status : Internal to Bio::Graphics
232
The real work is done by the panel, but factory subclasses can
239
my @result = $self->panel->map_pt(@_);
240
return wantarray ? @result : $result[0];
246
Usage : @pixel_positions = $f->map_no_trunc(@bp_positions)
247
Function : map bp positions to pixel positions
248
Returns : a list of pixel positions
249
Args : a list of bp positions
250
Status : Internal to Bio::Graphics
252
Same as map_pt(), but it will NOT clip pixel positions to be within
259
my @result = $self->panel->map_no_trunc(@_);
260
return wantarray ? @result : $result[0];
263
=head2 translate_color
265
Title : translate_color
266
Usage : $index = $f->translate_color($color_name)
267
Function : translate symbolic color names into GD indexes
269
Args : a color name in format "green" or "#00FF00"
270
Status : Internal to Bio::Graphics
272
The real work is done by the panel, but factory subclasses can
277
sub translate_color {
279
my $color_name = shift;
280
$self->panel->translate_color($color_name);
286
Usage : @glyphs = $f->glyph($level,$feature1,$feature2...)
287
Function : transform features into glyphs.
288
Returns : a list of Bio::Graphics::Glyph objects
289
Args : a feature "level", followed by a list of FeatureI objects.
290
Status : Internal to Bio::Graphics
292
The level is used to track the level of nesting of features that have
302
my $panel = $self->panel;
303
my $flip = $panel->flip;
306
my $type = $self->feature_to_glyph($f);
307
my $glyphclass = 'Bio::Graphics::Glyph';
309
$glyphclass .= "\:\:\L$type";
311
unless ($LOADED_GLYPHS{$glyphclass}++) {
312
$self->throw("The requested glyph class, ``$type'' is not available: $@")
313
unless (eval "require $glyphclass");
316
my $glyph = $glyphclass->new(-feature => $f,
324
return wantarray ? @result : $result[0];
327
=head2 feature_to_glyph
329
Title : feature_to_glyph
330
Usage : $glyph_name = $f->feature_to_glyph($feature)
331
Function : choose the glyph name given a feature
332
Returns : a glyph name
333
Args : a Bio::Seq::FeatureI object
334
Status : Internal to Bio::Graphics
338
sub feature_to_glyph {
342
return scalar $self->{stylesheet}->glyph($feature) if $self->{stylesheet};
343
my $map = $self->glyph_map or return 'generic';
344
if (ref($map) eq 'CODE') {
345
my $val = eval {$map->($feature)};
347
return $val || 'generic';
349
return $map->{$feature->primary_tag} || 'generic';
356
Usage : $f->set_option($option_name=>$option_value)
357
Function : set or change an option
359
Args : a name/value pair
360
Status : Internal to Bio::Graphics
366
my ($option_name,$option_value) = @_;
367
$self->{overriding_options}{lc $option_name} = $option_value;
371
# the overriding_options hash has precedence
372
# ...followed by the option_map
373
# ...followed by the stylesheet
374
# ...followed by generic options
377
my ($glyph,$option_name,$partno,$total_parts) = @_;
378
return unless defined $option_name;
379
$option_name = lc $option_name; # canonicalize
381
return $self->{overriding_options}{$option_name}
382
if exists $self->{overriding_options} && exists $self->{overriding_options}{$option_name};
384
if (exists $self->{options} && (my $map = $self->{options})) {
385
if (exists $map->{$option_name} && defined(my $value = $map->{$option_name})) {
386
my $feature = $glyph->feature;
387
return $value unless ref $value eq 'CODE';
388
my $val = eval { $value->($feature,$option_name,$partno,$total_parts,$glyph)};
389
warn "Error returned while evaluating value of '$option_name' option for glyph $glyph, feature $feature: ",$@,"\n"
391
return defined $val && $val eq '*default*' ? $GENERIC_OPTIONS{$option_name} : $val;
395
if (exists $self->{stylesheet} && (my $ss = $self->{stylesheet})) {
396
my($glyph,%options) = $ss->glyph($glyph->feature);
397
my $value = $options{$option_name};
398
return $value if defined $value;
401
return $GENERIC_OPTIONS{$option_name};
406
my $option_name = shift;
407
my $map = $self->{options} or return;
408
$map->{$option_name};
415
Usage : @option_names = $f->options
416
Function : return all configured option names
417
Returns : a list of option names
419
Status : Internal to Bio::Graphics
423
# return names of all the options in the option hashes
427
if (my $map = $self->option_map) {
428
$options{lc($_)}++ foreach keys %$map;
430
$options{lc($_)}++ foreach keys %GENERIC_OPTIONS;
431
return keys %options;