File: Factory.pm

package info (click to toggle)
libbio-graphics-perl 2.40-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,276 kB
  • sloc: perl: 21,801; makefile: 14
file content (481 lines) | stat: -rwxr-xr-x 11,768 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
=head1 NAME

Bio::Graphics::Glyph::Factory - Factory for Bio::Graphics::Glyph objects

=head1 SYNOPSIS

See L<Bio::Graphics::Panel>.

=head1 DESCRIPTION

This class is used internally by Bio::Graphics to generate new Glyph
objects by combining a list of features with the user's desired
configuration.  It is intended to be used internally by Bio::Graphics.

=head1 FEEDBACK

=head2 Mailing Lists

User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to one
of the Bioperl mailing lists.  Your participation is much appreciated.

  bioperl-l@bioperl.org                  - General discussion
  http://bioperl.org/wiki/Mailing_lists  - About the mailing lists

=head2 Reporting Bugs

Report bugs to the Bioperl bug tracking system to help us keep track
the bugs and their resolution.  Bug reports can be submitted via the
web:

  http://bugzilla.open-bio.org/

=head1 AUTHOR - Lincoln Stein

Email - lstein@cshl.org

=head1 SEE ALSO

L<Bio::Graphics::Panel>

=head1 APPENDIX

The rest of the documentation details each of the object
methods. Internal methods are usually preceded with an "_"
(underscore).

=cut

package Bio::Graphics::Glyph::Factory;

use strict;
use Carp qw(:DEFAULT cluck);
use Bio::Root::Version;
use base qw(Bio::Root::Root);
#use Memoize 'memoize';
#memoize('option');

my %LOADED_GLYPHS = ();
my %GENERIC_OPTIONS = (
		       bgcolor    => 'turquoise',
		       fgcolor    => 'black',
		       fontcolor  => 'black',
		       font2color => 'blue',
		       height     => 8,
		       font       => 'gdSmallFont', # This must be a string not method call
		       bump       => +1,       # bump by default (perhaps a mistake?)
		       );

=head2 new

  Title   : new
  Usage   : $f = Bio::Graphics::Glyph::Factory->new(
                     -stylesheet => $stylesheet,
		     -glyph_map  => $glyph_map,
		     -options    => $options);
  Function : create a new Bio::Graphics::Glyph::Factory object
  Returns  : the new object
  Args     : $stylesheet is a Bio::Das::Stylesheet object that can
                 convert Bio::Das feature objects into glyph names and
                 associated options.
             $glyph_map is a hash that maps primary tags to glyph names.
             $options is a hash that maps option names to their values.
  Status   : Internal to Bio::Graphics

=cut

sub new {
  my $class = shift;
  my $panel = shift;
  my %args = @_;
  my $stylesheet = $args{-stylesheet};   # optional, for Bio::Das compatibility
  my $map        = $args{-map};          # map type name to glyph name
  my $options    = $args{-options};      # map type name to glyph options
  return bless {
		stylesheet => $stylesheet,
		glyph_map  => $map,
		options    => $options,
		panel      => $panel,
		},$class;
}

=head2 clone

  Title    : clone
  Usage    : $f2 = $f->clone
  Function : Deep copy of a factory object
  Returns  : a deep copy of the factory object
  Args     : None
  Status   : Internal to Bio::Graphics

=cut

sub clone {
  my $self = shift;
  my %new = %$self;
  my $new = bless \%new,ref($self);
  $new;
}

=head2 stylesheet

  Title    : stylesheet
  Usage    : $stylesheet = $f->stylesheet
  Function : accessor for stylesheet
  Returns  : a Bio::Das::Stylesheet object
  Args     : None
  Status   : Internal to Bio::Graphics

=cut

sub stylesheet { 
    my $self = shift;
    my $d    = $self->{stylesheet};
    $self->{stylesheet} = shift if @_;
    $d;
}

=head2 glyph_map

  Title    : glyph_map
  Usage    : $map = $f->glyph_map
  Function : accessor for the glyph map
  Returns  : a hash mapping primary tags to glyphs
  Args     : None
  Status   : Internal to Bio::Graphics

=cut

sub glyph_map  { shift->{glyph_map}   }

=head2 option_map

  Title    : option_map
  Usage    : $map = $f->option_map
  Function : accessor for the option map
  Returns  : a hash mapping option names to values
  Args     : None
  Status   : Internal to Bio::Graphics

=cut

sub option_map { shift->{options}     }

=head2 global_opts

  Title    : global_opts
  Usage    : $map = $f->global_opts
  Function : accessor for global options
  Returns  : a hash mapping option names to values
  Args     : None
  Status   : Internal to Bio::Graphics

This returns a set of defaults for option values.

=cut

sub global_opts{ shift->{global_opts} }

=head2 panel

  Title    : panel
  Usage    : $panel = $f->panel
  Function : accessor for Bio::Graphics::Panel
  Returns  : a Bio::Graphics::Panel
  Args     : None
  Status   : Internal to Bio::Graphics

This returns the panel with which the factory is associated.

=cut

sub panel      { shift->{panel}       }

=head2 scale

  Title    : scale
  Usage    : $scale = $f->scale
  Function : accessor for the scale
  Returns  : a floating point number
  Args     : None
  Status   : Internal to Bio::Graphics

This returns the scale, in pixels/bp for glyphs constructed by this
factory.

=cut

sub scale      { shift->{panel}->scale }

=head2 font

  Title    : font
  Usage    : $font = $f->font
  Function : accessor for the font
  Returns  : a font name
  Args     : None
  Status   : Internal to Bio::Graphics

This returns a GD font name.

=cut

sub font       {
  my $self = shift;
  my $glyph = shift;
  $self->option($glyph,'font') || $self->{font};
}

=head2 map_pt

  Title    : map_pt
  Usage    : @pixel_positions = $f->map_pt(@bp_positions)
  Function : map bp positions to pixel positions
  Returns  : a list of pixel positions
  Args     : a list of bp positions
  Status   : Internal to Bio::Graphics

The real work is done by the panel, but factory subclasses can
override if desired.

=cut

sub map_pt {
  my $self = shift;
  my @result = $self->panel->map_pt(@_);
  return wantarray ? @result : $result[0];
}

=head2 map_no_trunc

  Title    : map_no_trunc
  Usage    : @pixel_positions = $f->map_no_trunc(@bp_positions)
  Function : map bp positions to pixel positions
  Returns  : a list of pixel positions
  Args     : a list of bp positions
  Status   : Internal to Bio::Graphics

Same as map_pt(), but it will NOT clip pixel positions to be within
the drawing frame.

=cut

sub map_no_trunc {
  my $self = shift;
  my @result = $self->panel->map_no_trunc(@_);
  return wantarray ? @result : $result[0];
}

=head2 translate_color

  Title    : translate_color
  Usage    : $index = $f->translate_color($color_name)
  Function : translate symbolic color names into GD indexes
  Returns  : an integer
  Args     : a color name in format "green" or "#00FF00"
  Status   : Internal to Bio::Graphics

The real work is done by the panel, but factory subclasses can
override if desired.

=cut

sub translate_color {
  my $self = shift;
  my $color_name = shift;
  $self->panel->translate_color($color_name);
}

=head2 transparent_color

  Title    : transparent_color
  Usage    : $index = $f->transparent_color($opacity,$color_name)
  Function : translate symbolic color names into GD indexes, with
                an opacity value taken into account
  Returns  : an integer
  Args     : an opacity value from 0-1.0, plus a color name in format "green" or "#00FF00"
  Status   : Internal to Bio::Graphics

The real work is done by the panel, but factory subclasses can
override if desired.

=cut

sub transparent_color {
  my $self = shift;
  $self->panel->transparent_color(@_);
}

=head2 make_glyph

  Title    : make_glyph
  Usage    : @glyphs = $f->glyph($level,[$type,]$feature1,$feature2...)
  Function : transform features into glyphs.
  Returns  : a list of Bio::Graphics::Glyph objects
  Args     : a feature "level", followed by a list of FeatureI objects.
  Status   : Internal to Bio::Graphics

The level is used to track the level of nesting of features that have
subfeatures. The option $type argument can be used to force the glyph type

=cut

# create a glyph
sub make_glyph {
  my $self  = shift;
  my $level = shift;
  my $forced_type  = shift unless ref($_[0]);

  my @result;
  my $panel = $self->panel;
  my $flip  = $panel->flip;

  for my $f (@_) {
    my $type = $forced_type || $self->feature_to_glyph($f);

    my $glyphclass = 'Bio::Graphics::Glyph';
    $type ||= 'generic';
    $glyphclass .= "\:\:\L$type";

    unless ($LOADED_GLYPHS{$glyphclass}++) {
      $self->throw("The requested glyph class, ``$type'' is not available: $@")
        unless (eval "require $glyphclass");
    }

    my $glyph = $glyphclass->new(-feature  => $f,
				 -factory  => $self,
				 -flip     => $flip,
				 -level    => $level);

    push @result,$glyph;

  }
  return wantarray ? @result : $result[0];
}


=head2 feature_to_glyph

  Title    : feature_to_glyph
  Usage    : $glyph_name = $f->feature_to_glyph($feature)
  Function : choose the glyph name given a feature
  Returns  : a glyph name
  Args     : a Bio::Seq::FeatureI object
  Status   : Internal to Bio::Graphics

=cut

sub feature_to_glyph {
  my $self    = shift;
  my $feature = shift;

  my $val;

  if ($self->{stylesheet} && $feature->type !~ /track|group/) {
      $val = scalar $self->{stylesheet}->glyph($feature);
      return $val || 'generic';
  }

  my $map = $self->glyph_map;
  if ($map) {
      if (ref($map) eq 'CODE') {
	  $val = eval {$map->($feature)};
	  warn $@ if $@;
      }
      else {
	  $val = $map->{$feature->primary_tag};
      }
  }

  return $val || 'generic';
}


=head2 set_option

  Title    : set_option
  Usage    : $f->set_option($option_name=>$option_value)
  Function : set or change an option
  Returns  : nothing
  Args     : a name/value pair
  Status   : Internal to Bio::Graphics

=cut

sub set_option {
  my $self = shift;
  my ($option_name,$option_value) = @_;
  $self->{overriding_options}{lc $option_name} = $option_value;
}

# options:
#    the overriding_options hash has precedence
#    ...followed by the option_map
#    ...followed by the stylesheet
#    ...followed by generic options
sub option {
  my $self = shift;
  my ($glyph,$option_name,$partno,$total_parts) = @_;
  return unless defined $option_name;
  $option_name = lc $option_name;   # canonicalize

  return $self->{overriding_options}{$option_name} 
    if exists $self->{overriding_options} && exists $self->{overriding_options}{$option_name};

  if (exists $self->{stylesheet} && (my $ss = $self->{stylesheet})) {
    my(undef,%options) = $ss->glyph($glyph->feature);
    my $value = $options{$option_name};
    if (defined $value) {  # some cleanup on DAS glyphs
	$value =~ s/yes/1/i;
	$value =~ s/no/0/i;
    }
    return $value if defined $value;
  }

  if (exists $self->{options} && (my $map    = $self->{options})) {
    if (exists $map->{$option_name} && defined(my $value  = $map->{$option_name})) {
      my $feature = $glyph->feature;

      return $value unless ref $value eq 'CODE';
      my $val = eval { $value->($feature,$option_name,$partno,$total_parts,$glyph)};
      warn "Error returned while evaluating value of '$option_name' option for glyph $glyph, feature $feature: ",$@,"\n"
	if $@;
      return defined $val && $val eq '*default*' ? $GENERIC_OPTIONS{$option_name} : $val;
    }
  }

  return $GENERIC_OPTIONS{$option_name};
}

sub get_option {
  my $self = shift;
  my $option_name = shift;
  my $map = $self->{options} or return;
  $map->{$option_name};
}


=head2 options

  Title    : options
  Usage    : @option_names = $f->options
  Function : return all configured option names
  Returns  : a list of option names
  Args     : none
  Status   : Internal to Bio::Graphics

=cut

# return names of all the options in the option hashes
sub options {
  my $self = shift;
  my %options;
  if (my $map    = $self->option_map) {
    $options{lc($_)}++ foreach keys %$map;
  }
  $options{lc($_)}++ foreach keys %GENERIC_OPTIONS;
  return keys %options;
}

1;