Bio-Graphics-Glyph-decorated_gene

 view release on metacpan or  search on metacpan

lib/Bio/Graphics/Glyph/decorated_transcript.pm  view on Meta::CPAN

By default, the glyph automatically assigns different colors to different types of protein decorations, whereas 
decorations of the same type are always assigned the same color. 

Protein decorations are provided either with mRNA features inside GFF files (see example below) or 
dynamically via callback function using the B<additional_decorations> option (see glyph options).
The following line is an example of an mRNA feature in a GFF file that contains two protein decorations, 
one signal peptide predicted by SignalP and one transmembrane domain predicted by TMHMM:

chr1   my_source   mRNA  74796  75599   .  +  .  ID=rna_gene-1;protein_decorations=SignalP40:SP:1:23:0:my_comment,TMHMM:TM:187:209:0

Each protein decoration consists of six fields separated by a colon:

1) Type. For example used to specify decoration source (e.g. 'SignalP40')
2) Name. Decoration name. Used as decoration label by default (e.g. 'SP' for signal peptide)
3) Start. Start coordinate at the protein-level (1-based coordinate)
4) End. End coordinate at the protein-level
5) Score. Optional. Score associated with a decoration (e.g. Pfam E-value). This score can be used 
   to dynamically filter or color decorations via callbacks (see glyph options).
6) Description. Optional. User-defined description of decoration. The glyph ignores this description, 
   but it will be made available to callback functions for inspection. Special characters 
   like ':' or ',' that might interfere with the GFF tag parser should be avoided. 

If callback functions are used as glyph parameters (see below), the callback is called for each
decoration separately. That is, the callback can get called multiple times for the same CDS feature,
but each time with a different active decoration. The currently drawn (active) decoration is made available 
to the callback via the glyph method 'active_decoration'. The active decoration is returned in form
of a Bio::Graphics::Feature object, with decoration data fields mapped to corresponding feature
attributes in the following way:

  type --> \$glyph->active_decoration->type
  name --> \$glyph->active_decoration->name
  nucleotide start coordinate --> \$glyph->active_decoration->start
  nucleotide end coordinate --> \$glyph->active_decoration->end
  protein start coordinate --> \$glyph->active_decoration->get_tag_values('p_start')
  protein end coordinate --> \$glyph->active_decoration->get_tag_values('p_end')
  score --> \$glyph->active_decoration->score
  description --> \$glyph->active_decoration->desc

In addition, the glyph passed to the callback allows access to the parent glyph and
parent feature if required (use \$glyph->parent or \$glyph->parent->feature). 

NOTE: This glyph works only with GFF3 compliant features. In particular, make sure that
every feature has a valid unique ID and that all child features have a valid parent id.

END
}

sub my_options {
    return {
	decoration_visible => [
	    'boolean',
	    'false',
	    'Specifies whether decorations should be visible or not. For selective display of individual', 
        'decorations, specify a callback function and return 1 or 0 after inspecting the active',
        'decoration of the glyph. '],
	decoration_color => [
	    'color',
	    undef,
	    'Decoration background color. If no color is specified, colors are assigned automatically',
	    'by decoration type and name, whereas decorations of identical type and name are assigned',
	    'the same color. A special color \'transparent\' can be used here in combination with',
	    'the option \'decoration_border\' to draw decorations as outlines.'],
	decoration_border => [
	    ['none', 'solid', 'dashed'],
	    'none',
	    'Decoration border style. By default, decorations are drawn without border (\'none\' or',
	    '0). Other valid options here include \'solid\' or \'dashed\'.'],
	decoration_border_color => [
	    'color',
	    'black',
	    'Color of decoration border.'],
	decoration_label => [
	    'string',
	    undef,
	    'Decoration label. If not specified, the second data field of the decoration is used',
	    'as label. Set this option to 0 to get unlabeled decorations. If the label text',
	    'extends beyond the size of the decorated segment, the label will be clipped. Clipping',
	    'does not occur for SVG output.'],
	decoration_label_position => [
	    ['inside', 'above', 'below'],
	    'inside',
	    'Position of decoration label. Labels can be drawn \'inside\' decorations (default)',
	    'or \'above\' and \'below\' decorations.'],
	decoration_label_color => [
	    'color',
	    'undef',
	    'Decoration label color. If not specified, this color is complementary to',
	    'decoration_color (e.g., yellow text on blue background, white on black, etc.). If the', 
        'decoration background color is transparent and no decoration label color is specified,',
        'the foreground color of the underlying transcript glyph is used as default.'],
	additional_decorations => [
	    'string',
	    undef,
	    'Additional decorations to those specified in the GFF file. Expected is a',
	    'comma-separated string in the same format as described above for GFF files.',
	    'Example string: "SignalP40:SP:1:23:0:my_comment,TMHMM:TM:187:209:0"',
	    'This parameter is intended',
	    'to be used as callback function, which inspects the currently processed transcript',
	    'feature (first parameter to callback) and returns additional protein decorations',
	    'that should be drawn. Alternatively, decorations not specified in the GFF file can', 
	    'also be added dynamically to feature objects before rendering using the',
	    'add_tag_value() method of the feature object.'],
	decoration_height => [
	    'integer',
	    undef,
	    'Decoration height. Unless specified otherwise, the height of the decoration is the',
	    'height of the underlying transcript glyph minus 2, such that the decoration is drawn',
	    'within transcript boundaries.'],
	decoration_position => [
	    ['inside', 'stacked_bottom', '<integer value>'],
	    'inside',
	    'Vertical position of the decoration. If \'inside\' is specified',
        '(default), decorations are drawn inside CDS segments.', 
        'Alternatively, a positive or negative integer value can be',
        'specified, which will vertically offset the decoration by',
        'the specified amount (in pixels) relative to the CDS segment.',
        'Specifying \'stacked_bottom\' will stack decorations below',
        'CDS segments in a non-overlapping manner (experimental).'],
	decoration_position => [
	    ['CDS', 'mRNA'],
	    'CDS',
        'Feature level at which decoration is drawn (\'CDS\' or \'mRNA\').',
        'By default, decorations are drawn at the \'CDS\'',
        'level, which means the decoration is only visible where it',
        'overlaps with a coding sequence, skipping introns.',
        'Under some circumstances decorations should span introns,',
        'which can be achieved by specifying \'mRNA\' here.'],
	box_subparts => [
	    'integer',
	    '0',
	    'Same functionality as for basic glyph. Enables mouse-over effects',
	    '(tooltips and hyperlinks) for decorations via generation of image maps.', 
	    'Image maps for decorations will be generated if the level specified here exceeds',
	    'the level of the underlying glyph part. For example, if you specify level 3,', 
	    'image maps will be generated for decorations of part level 1 (mRNA)', 
	    'and part level 2 (CDS). Note that this option must be used in combination with', 
	    '-link and -title.']
    }
}

sub new {
	my ( $class, @args ) = @_;
	my %param = @args;

	warn "new(): " . join( ",", @args ) . "\n" if (DEBUG == 2);

	my $feature = $param{'-feature'};
	my $factory = $param{'-factory'};
	

lib/Bio/Graphics/Glyph/decorated_transcript.pm  view on Meta::CPAN


	$self->{'active_decoration'} = $decoration; # set active decoration for callback
	my $decoration_height = $self->option('decoration_height');

	$decoration_height = $self->height-2
	  if ( !$decoration_height );

	return $decoration_height;
}

sub decoration_position {
	my $self = shift;
	my $decoration = shift;

	if (!$decoration)
	{
		$self->throw("decoration not specified")	if (DEBUG);
		return "inside";
	}

	$self->{'active_decoration'} = $decoration; # set active decoration for callback
	my $decoration_position = $self->option('decoration_position');
	
	$decoration_position = 'inside'
	  if ( !$decoration_position );

	return $decoration_position;
}

sub _hash {
	my $hash = 0;
	foreach ( split //, shift ) {
		$hash = $hash * 33 + ord($_);
	}
	$hash = $hash + ($hash >> 5);
	return $hash;
}

sub decoration_label_color {
	my $self = shift;
	my $decoration = shift;
	
	if (!$decoration)
	{
		$self->throw("decoration not specified")	if (DEBUG);
		return "black";
	}

	$self->{'active_decoration'} = $decoration; # set active decoration for callback
	my $decoration_label_color = $self->option('decoration_label_color');

	return $decoration_label_color
	  if (  defined $decoration_label_color
		and $decoration_label_color ne 'auto'
		and $decoration_label_color ne '' );
		
	my $decoration_color = $self->decoration_color($decoration);

	return $self->fgcolor
		if ((!$decoration_label_color or $decoration_label_color eq 'auto') 
		      and $decoration_color eq "transparent");

	# assign color complementary to decoration color
	my ( $red, $green, $blue ) =
	  Bio::Graphics::Panel->color_name_to_rgb($decoration_color);

	$decoration_label_color =
	  sprintf( "#%02X%02X%02X", 255 - $red, 255 - $green, 255 - $blue );    # background complement

	return $decoration_label_color;
}

sub decoration_label {
	my $self = shift;
	my $decoration = shift;
	
	if (!$decoration)
	{
		$self->throw("decoration not specified")	if (DEBUG);
		return "";
	}

	$self->{'active_decoration'} = $decoration; # set active decoration for callback
	my $decoration_label = $self->option('decoration_label');
	
	return undef 
		if ( defined $decoration_label and $decoration_label eq "0" );

	return $decoration_label
		if ( $decoration_label and $decoration_label ne "1");
		
	# assign decoration name as default label
	return $decoration->name;
}

sub decoration_label_position {
	my $self = shift;
	my $decoration = shift;
	
	if (!$decoration)
	{
		$self->throw("decoration not specified")	if (DEBUG);
		return "";
	}

	$self->{'active_decoration'} = $decoration; # set active decoration for callback
	my $decoration_label_position = $self->option('decoration_label_position');
	
	return "inside"
		if (!$decoration_label_position);
		
	return $decoration_label_position;
}

sub decoration_border {
	my $self = shift;
	my $decoration = shift;
	
	if (!$decoration)
	{
		$self->throw("decoration not specified")	if (DEBUG);

lib/Bio/Graphics/Glyph/decorated_transcript.pm  view on Meta::CPAN

	foreach my $mh (@{$self->sorted_decorations}) {
		  
		next if ( !$self->decoration_visible($mh) ); 		# skip invisible decorations
		next if ( $self->decoration_level($mh) ne 'mRNA' ); # only decorations at mRNA level 

		$self->draw_decoration($gd, $dx, $dy, $mh, $mh->start, $mh->end, $self->decoration_label($mh));
	}
}

sub draw_decorations_CDS {
	my $self = shift;
	my ( $gd, $dx, $dy ) = @_;

	warn "draw_decorations_CDS(): " . $self->feature . "\n" if (DEBUG == 2);

	# determine overlaps of visible decorations with CDS
	my @overlaps;
	foreach my $mh (@{$self->sorted_decorations}) {
			  
		next if ( !$self->decoration_visible($mh) );  # skip invisible decorations
		next if ( $self->decoration_level($mh) ne 'CDS' ); # only decorations at CDS level 
	
		# determine overlapping segments between protein decorations and feature components
		my $overlap_start_nt = max( $self->feature->start, $mh->start );
		my $overlap_end_nt = min( $self->feature->end, $mh->end );
		
		push (@overlaps, [$mh, $overlap_start_nt, $overlap_end_nt]) if ( $overlap_start_nt <= $overlap_end_nt ); 
	}
	
	# draw decorations
	foreach my $o (@overlaps) {
		
		my ($mh, $overlap_start_nt, $overlap_end_nt) = @$o;

		# draw label only on first overlapping component
		my $h_label = $self->decoration_label($mh);
		$h_label = undef
			if ( (!$self->flip and $overlap_start_nt > $mh->start) or ($self->flip and $overlap_end_nt < $mh->end) );

		$self->draw_decoration($gd, $dx, $dy, $mh, $overlap_start_nt, $overlap_end_nt, $h_label);
	}	
}

sub draw_decoration {
	my $self = shift;
	my ( $gd, $dx, $dy, $mh, $nt_start, $nt_end, $label ) = @_;

	my ( $left, $top, $right, $bottom ) = $self->bounds( $dx, $dy );

	warn "  bounds: left:$left,top:$top,right:$right,bottom:$bottom\n"
	  if (DEBUG == 2);

	my ($h_left, $h_top, $h_right, $h_bottom) = $self->_map_decoration($gd, $dx, $dy, $mh, $nt_start, $nt_end);

	my $color = $self->decoration_color($mh);

	 # don't draw over borders; not supported by SVG
	$gd->clip( $left + 1, $h_top, $right - 1, $h_bottom )
	  if ( !$gd->isa("GD::SVG::Image") );

	if ($color ne 'transparent')
	{
		warn "filledRectangle: left=$h_left,top=$h_top,right=$h_right,bottom=$h_bottom\n"
		  if (DEBUG == 2);
		$gd->filledRectangle( $h_left, $h_top, $h_right, $h_bottom,
			$self->factory->translate_color($color) );				
	}

	my $border_style = $self->decoration_border($mh);
	if ($border_style)
	{
		my ($b_left, $b_top, $b_right, $b_bottom) = ($h_left, $h_top, $h_right, $h_bottom);
		my $border_color = $self->factory->translate_color($self->decoration_border_color($mh));

		warn "border rectangle ($border_style): left=$b_left,top=$b_top,right=$b_right,bottom=$b_bottom\n"
			if (DEBUG == 2);

		if ($border_style eq "dashed")
		{
			my $image_class   = $self->panel->image_class;
			my $gdTransparent = $image_class->gdTransparent;
			my $gdStyled      = $image_class->gdStyled;
		    $gd->setStyle($border_color,$border_color,$border_color,$gdTransparent,$gdTransparent);				
			$gd->rectangle( $b_left, $b_top, $b_right, $b_bottom, $gdStyled );				
		}
		else
		{
			$gd->rectangle( $b_left, $b_top, $b_right, $b_bottom, $border_color );				
		}  				
		
	}
	
	$gd->clip( 0, 0, $gd->width, $gd->height )
	  if ( !$gd->isa("GD::SVG::Image") );

	# draw label
	if ( $label ) {
		$self->draw_decoration_label( $gd, $dx, $dy, $mh, $h_top,
			$h_left, $h_bottom, $h_right, $label );
	}
}

sub _map_decoration {
	my $self = shift;
	my ( $gd, $dx, $dy, $mh, $nt_start, $nt_end ) = @_;
	
	my ( $left, $top, $right, $bottom ) = $self->bounds( $dx, $dy );

	my ( $h_left, $h_right ) =
	  $self->map_no_trunc( $nt_start, $nt_end + 1 );
	( $h_left, $h_right ) = ( $h_right, $h_left )
	  if ( $h_left > $h_right );
	$h_left += 1 if ($gd->isa("GD::SVG::Image"));
	$h_right = max($h_right-1, $h_left) if ($gd->isa("GD::SVG::Image"));
	my $h_top = $dy + $self->decoration_top($mh);
	my $h_bottom = $dy + $self->decoration_bottom($mh);

	return ($h_left, $h_top, $h_right, $h_bottom);
}

sub draw_decoration_label {

lib/Bio/Graphics/Glyph/decorated_transcript.pm  view on Meta::CPAN

but it will be made available to callback functions for inspection. Special characters 
like ':' or ',' that might interfere with the GFF tag parser should be avoided. 

=back 

If callback functions are used as glyph parameters (see below), the callback is called for each
decoration separately. That is, the callback can get called multiple times for the same CDS feature,
but each time with a different active decoration. The currently drawn (active) decoration is made available 
to the callback via the glyph method 'active_decoration'. The active decoration is returned in form
of a Bio::Graphics::Feature object, with decoration data fields mapped to corresponding feature
attributes in the following way:

=over

=item * type --> $glyph->active_decoration->type

=item * name --> $glyph->active_decoration->name

=item * nucleotide start coordinate --> $glyph->active_decoration->start

=item * nucleotide end coordinate --> $glyph->active_decoration->end

=item * protein start coordinate --> $glyph->active_decoration->get_tag_values('p_start')

=item * protein end coordinate --> $glyph->active_decoration->get_tag_values('p_end')

=item * score --> $glyph->active_decoration->score

=item * description --> $glyph->active_decoration->desc

=back 

In addition, the glyph passed to the callback allows access to the parent glyph and
parent feature if required (use $glyph->parent or $glyph->parent->feature). 

NOTE: This glyph works only with GFF3 compliant features. In particular, make sure that
every feature has a valid unique ID and that all child features have a valid parent id.

=head2 OPTIONS

This glyph inherits all options from the L<Bio::Graphics::Glyph::processed_transcript> glyph. 
In addition, it recognizes the following glyph-specific options:

  Option          Description                                              Default
  ------          -----------                                              -------

  -decoration_visible                                                      false
  
                  Specifies whether decorations should be visible
                  or not. For selective display of individual 
                  decorations, specify a callback function and 
                  return 1 or 0 after inspecting the active decoration
                  of the glyph. 

  -decoration_color                                                        <auto>
  
                  Decoration background color. If no color is
                  specified, colors are assigned automatically by
                  decoration type and name, whereas decorations of 
                  identical type and name are assigned the same color.
                  A special color 'transparent' can be used here in 
                  combination with the option 'decoration_border' to 
                  draw decorations as outlines.
                            
  -decoration_border                                                       0 (none)
  
                  Decoration border style. By default, decorations are
                  drawn without border ('none' or 0). Other valid 
                  options here include 'solid' or 'dashed'.
                            
  -decoration_border_color                                                 black
  
                  Color of decoration border. 
                            
  -decoration_label                                                        true
                                                                           (decoration name)
                  Decoration label. If not specified, the second data
                  field of the decoration is used as label. Set this
                  option to 0 to get unlabeled decorations. If the label 
                  text extends beyond the size of the decorated segment, 
                  the label will be clipped. Clipping does not occur 
                  for SVG output.

  -decoration_label_position                                               inside
  
                  Position of decoration label. Labels can be drawn 
                  'inside' decorations (default) or 'above' and 'below'
                  decorations.
                  
  -decoration_label_color                                                  <auto>
  
                  Decoration label color. If not specified, this color 
                  is complementary to decoration_color (e.g., yellow text 
                  on blue background, white on black, etc.). If the 
                  decoration background color is transparent and no
                  decoration label color is specified, the foreground color 
                  of the underlying transcript glyph is used as default.

  -additional_decorations                                                  undefined
  
                  Additional decorations to those specified in the GFF file. 
                  Expected is a comma-separated string in the same format as 
                  described above for GFF files, for example
             
                    "SignalP40:SP:1:23:0:my_comment,TMHMM:TM:187:209:0"
              
                  This parameter is intended to be used as callback function,  
                  which inspects the currently processed transcript feature
                  (first parameter to callback) and returns additional protein
                  decorations that should be drawn. Alternatively, decorations 
                  not specified in the GFF file can also be added dynamically to 
                  feature objects before rendering using the add_tag_value()
                  method of the feature object. 

  -decoration_height                                                       CDS height-2
                  
                  Decoration height. Unless specified otherwise,
                  the height of the decoration is the height of the 
                  underlying transcript glyph minus 2, such that 
                  the decoration is drawn within transcript boundaries.

  -decoration_position                                                     inside
  
                  Vertical position of the decoration. If 'inside' is specified
                  (default), decorations are drawn inside CDS segments. 
                  Alternatively, a positive or negative integer value can be
                  specified, which will vertically offset the decoration by
                  the specified amount (in pixels) relative to the CDS segment.
                  Specifying 'stacked_bottom' will stack decorations below 
                  CDS segments in a non-overlapping manner (experimental).
                            
  -decoration_level                                                        CDS
  
                  Feature level at which decoration is drawn ('CDS' or 'mRNA'). 
                  By default, decorations are drawn at the 'CDS'
                  level, which means the decoration is only visible where it
                  overlaps with a coding sequence, skipping introns. 
                  Under some circumstances decorations should span introns, 
                  which can be achieved by specifying 'mRNA' here.
                  
  -box_subparts                                                            0 
                  
                  Same functionality as for basic glyph. Enables mouse-over
                  effects (tooltips and hyperlinks) for decorations via 
                  generation of image maps. Image maps for decorations will 
                  be generated if the level specified here exceeds the level
                  of the underlying glyph part. For example, if you specify
                  level 3, image maps will be generated for decorations of 
                  part level 1 (mRNA) and part level 2 (CDS). Note that this
                  option must be used in combination with -link and -title.

=head1 BUGS

Strandedness arrows are decorated incorrectly. Currently, the glyph plots a rectangular box 
over the arrow instead of properly coloring the arrow.



( run in 0.493 second using v1.01-cache-2.11-cpan-7fcb06a456a )