Bio-Graphics

 view release on metacpan or  search on metacpan

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

inside or around CDS segments. Protein decorations are specified at the 'mRNA' transcript level 
in protein coordinates. Protein coordinates are automatically mapped to nucleotide coordinates by the glyph. 
Decorations are allowed to span exon-exon junctions, in which case decorations are split between exons. 
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 be called multiple times for the same CDS feature,
but each time with a different 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->description

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). 

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 boder.'],
	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'],
	    undef,
	    '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 string',
	    'in the same format as described above for GFF files. 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.'],
	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'],
	    'inside',
	    'Currently, decorations can only be drawn inside CDS segments.'],
	flip_minus => [
	    'boolean',
	    0,
	    'If set to 1, features on the negative strand will be drawn flipped.',
	    'This is not particularly useful in GBrowse, but becomes handy if multiple features',
	    'should be drawn within the same panel, left-aligned, and on top of each other,',
	    'for example to allow easy gene structure comparisons.'],
    }
}

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

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

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

	my $flip_minus = $factory->get_option('flip_minus');
	if ( $flip_minus and $feature->strand < 1 ) {
		for ( my $i = 0 ; $i < @args ; $i++ ) {
			$args[ $i + 1 ] = 1 if ( $args[$i] eq '-flip' );
		}
	}

	my $self = $class->Bio::Graphics::Glyph::processed_transcript::new(@args);

	$self->{'parent'} = undef;
	$self->{'additional_decorations'} = undef;
	$self->{'active_decoration'} = undef;
	$self->{'add_pad_bottom'} = undef;

	# give sub-glyphs access to parent glyph's decorations
	if ($self->decorations_visible)
	{
		foreach my $sub_glyph ( $self->parts ) {
			$sub_glyph->{'parent'} = $self;
		}		
	}

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

	  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 );

	if ($decoration_position ne 'inside' and $decoration_position ne 'stacked_bottom')
	{
		$self->throw('invalid decoration_position: '.$decoration_position) if (DEBUG);
		$decoration_position = 'inside';
	}
	
	return $decoration_position;
}

sub _hash {
	my $hash = 0;
	foreach ( split //, shift ) {
		$hash = $hash * 33 + ord($_);
	}
	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


sub draw_component {
	my $self = shift;

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

	# draw regular glyph first
	if ( $self->feature->source eq 'legend' ) {
 		#  hack, but processed_transcript cannot be drawn without arrow...
 		$self->Bio::Graphics::Glyph::segments::draw_component(@_);
	}
	else {
		$self->Bio::Graphics::Glyph::processed_transcript::draw_component(@_);
	}

	# draw decorations if parent information available
	if ( $self->{'parent'} and $self->feature->primary_tag eq "CDS") {
		return $self->draw_decorations(@_);
	}
}

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

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

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

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

	foreach my $mh (@{$self->sorted_decorations}) {
		  
		# skip invisible decorations
		next if ( !$self->decoration_visible($mh) );

		# 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 );
		if ( $overlap_start_nt <= $overlap_end_nt ) {

			# manual override; forces flip to be drawn flipped
			$self->factory->panel->flip( $self->flip )
			  if ( $self->option('flip_minus') ); 
			
			my ( $h_left, $h_right ) =
			  $self->map_no_trunc( $overlap_start_nt, $overlap_end_nt + 1 );
			( $h_left, $h_right ) = ( $h_right, $h_left )
			  if ( $h_left > $h_right );
#			my ($h_top, $h_bottom) = ($dy + $self->top + $self->pad_top, $dy + $self->bottom - $self->pad_bottom);
			my $h_top = $dy + $self->decoration_top($mh);
			my $h_bottom = $dy + $self->decoration_bottom($mh);

			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) );				
			}

			if ($self->decoration_border($mh))
			{
				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: left=$b_left,top=$b_top,right=$b_right,bottom=$b_bottom\n"
					if (DEBUG == 2);

				if ($self->decoration_border($mh) 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 on first overlapping component
			my $h_label = $self->decoration_label($mh);
			if ( $h_label
				  and (
				  	( $self->feature->strand > 0
				       and $mh->start >= $self->feature->start )
				    or ( $self->feature->strand <= 0
					   and $mh->end <= $self->feature->end )
				  )
			   )
			{
				$self->draw_decoration_label( $gd, $dx, $dy, $mh, $h_top,
					$h_left, $h_bottom, $h_right, $h_label );
			}
		}
	}
}

sub draw_decoration_label {
	my $self = shift;
	my ( $gd, $dx, $dy, $mh, $h_top, $h_left, $h_bottom, $h_right, $label ) = @_;

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

	my $font      = $self->labelfont;
	my $label_top = $h_top + ($self->decoration_height($mh)-$font->height)/2;
	my $label_pos = $self->decoration_label_position($mh);

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

=item 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. 

=back 

If callback functions are used as glyph parameters (see below), the callback is called for each
decoration separately. That is, the callback can be called multiple times for a given CDS feature,
but each time with a different decoration that overlaps with this CDS. 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->description

=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). 

=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       
  
                  Specifies whether decorations should be visible          false
                  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
  
                  Decoration background color. If no color is              <auto>
                  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
  
                  Decoration border style. By default, decorations are     0 (none)
                  drawn without border ('none' or 0). Other valid 
                  options here include 'solid' or 'dashed'.
                            
  -decoration_border_color
  
                  Color of decoration border.                              black 
                            
  -decoration_label

                  Decoration label. If not specified, the second data      true
                  field of the decoration is used as label. Set this       (decoration name)
                  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
  
                  Position of decoration label. Labels can be drawn        inside 
                  'inside' decorations (default) or 'above' and 'below'
                  decorations.
                  
  -decoration_label_color
  
                  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
   
                  Additional decorations to those specified in the GFF     undefined 
                  file. Expected is a string in the same format as 
                  described above for GFF files. 
                  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.

  -decoration_height
                  
                  Decoration height. Unless specified otherwise,           CDS height-2
                  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       
  
                  Currently decorations can only be drawn inside           inside 
                  CDS segments.
                            
  -flip_minus
  
                  If set to 1, features on the negative strand will be     false 
                  drawn flipped. This is not particularly useful in 
                  GBrowse, but becomes handy if multiple features should 
                  be drawn within the same panel, left-aligned, and on 
                  top of each other, for example to allow for easy gene 
                  structure comparisons.

=head1 BUGS

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

Overlapping decorations are drawn on top of each other without particular order. The only 
solution to this problem at this point is to reduce decorations to a non-overlapping
set. 

For SVG output or if drawn not inside decorations, decoration labels are not clipped.
Similar as for overlapping decorations, this can result in labels being drawn on top 
of each other. 

Please report all errors.

=head1 SEE ALSO


L<Bio::Graphics::Panel>,
L<Bio::Graphics::Glyph>,
L<Bio::Graphics::Glyph::decorated_gene>,
L<Bio::Graphics::Glyph::processed_transcript>

=head1 AUTHOR



( run in 0.626 second using v1.01-cache-2.11-cpan-39bf76dae61 )