Bio-Graphics

 view release on metacpan or  search on metacpan

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

package Bio::Graphics::Glyph::xyplot;

use strict;
#use GD 'gdTinyFont';

use base qw(Bio::Graphics::Glyph::segments Bio::Graphics::Glyph::minmax);
use constant DEFAULT_POINT_RADIUS=>4;
use Bio::Root::Version;
our $VERSION = ${Bio::Root::Version::VERSION};

use constant DEBUG=>0;
use constant EXTRA_LABEL_PAD=>8;

sub my_description { 
    return <<'END';
This glyph is used for drawing features that have a position on the
genome and a numeric value.  It can be used to represent gene
prediction scores, motif-calling scores, percent similarity,
microarray intensities, or other features that require a line plot.

The plot is designed to work on a single feature group that contains
subfeatures. It is the subfeatures that carry the score
information. For a more efficient implementation that is suitable for
dense genome-wide data, use Bio::Graphics::Wiggle and the
wiggle_xyplot glyph.
END

}
sub my_options {
    {
	point_radius => [
	      'integer',
	      1,
	      'When drawing data points, this specifies the radius of each point.',
	    ],
	 clip => [
	     'boolean',
	     0,
	     'If min_score and/or max_score are manually specified,',
	     'then setting this to true will cause values outside the',
	     'range to be clipped.'
	    ],
	 graph_type => [
	     ['histogram','line','points','linepoints'],
	     'histogram',
	     'Type of graph to generate. Options are "boxes",',
	     '"line","points", or "linepoints".',
	     'The deprecated "boxes" subtype is equivalent to "histogram".'
	     ],
	 point_symbol => [
	     'string',
	     'none',
	     'Symbol to use for each data point when drawing line graphs.',
	     'Options are "triangle", "square", "disc", "filled_triangle",',
	     '"filled_square", "filled_disc", "point" and "none"',
	 ],
	 scale => [
	     'string',
	     'three',
	     'Position where the Y axis scale is drawn, if any.',
	     'Options are one of "left", "right", "both", "three" or "none".',
	     '"three" will cause the scale to be drawn in the left, right and center.',
	 ],

	 scale_color => [
	     'color',
	     'fgcolor',
	     'Color of the X and Y scales. Defaults to the same as fgcolor.',
	 ],
    };
}

my %SYMBOLS = (
	       triangle => \&draw_triangle,
	       square   => \&draw_square,
	       disc     => \&draw_disc,
	       point    => \&draw_point,
	      );

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

	  my $y = $self->{top} + $top;

	  $self->render_label($gd,
			      $font,
			      $x,
			      $y,
			      $label);

    } else {
	$self->SUPER::draw_label(@_);
    }

}

sub contrasting_label_color {
    my $self = shift;
    my ($gd,$bgcolor) = @_;
    my ($r,$g,$b)   = $gd->rgb($bgcolor);
    my $avg         = ($r+$g+$b)/3;
    return $self->translate_color($avg > 128 ? 'black' : 'white');
}

sub draw_legend {
  my $self = shift;
  my ($gd,$left,$top,$partno,$total_parts) = @_;
  return  if $self->bump eq 'overlap';

  my $color = $self->option('fgcolor'); 
  my $name = $self->feature->{name};

  my $label = "<a id=\"legend_$name\" target=\"_blank\" href=\"#\"> <font color=\'$color\';\">" . $name . "</font></a>" or return;

  my $font = $self->labelfont;
  my $x = $self->left + $left - $self->string_width($label,$font) - $self->extra_label_pad;
  my $y = $self->{top} + $top;
  my $is_legend = 1;
  $self->render_label($gd,
		      $font,
		      $x,
		      $y,
		      $label,
		      $is_legend);
}

1;

__END__

=head1 NAME

Bio::Graphics::Glyph::xyplot - The xyplot glyph

=head1 SYNOPSIS

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

=head1 DESCRIPTION

This glyph is used for drawing features that have a position on the
genome and a numeric value.  It can be used to represent gene
prediction scores, motif-calling scores, percent similarity,
microarray intensities, or other features that require a line plot.

The X axis represents the position on the genome, as per all other
glyphs.  The Y axis represents the score.  Options allow you to set
the height of the glyph, the maximum and minimum scores, the color of
the line and axis, and the symbol to draw.

The plot is designed to work on a single feature group that contains
subfeatures.  It is the subfeatures that carry the score
information. The best way to arrange for this is to create an
aggregator for the feature.  We'll take as an example a histogram of
repeat density in which interval are spaced every megabase and the
score indicates the number of repeats in the interval; we'll assume
that the database has been loaded in in such a way that each interval
is a distinct feature with the method name "density" and the source
name "repeat".  Furthermore, all the repeat features are grouped
together into a single group (the name of the group is irrelevant).
If you are using Bio::DB::GFF and Bio::Graphics directly, the sequence
of events would look like this:

  my $agg = Bio::DB::GFF::Aggregator->new(-method    => 'repeat_density',
                                          -sub_parts => 'density:repeat');
  my $db  = Bio::DB::GFF->new(-dsn=>'my_database',
                              -aggregators => $agg);
  my $segment  = $db->segment('Chr1');
  my @features = $segment->features('repeat_density');

  my $panel = Bio::Graphics::Panel->new(-pad_left=>40,-pad_right=>40);
  $panel->add_track(\@features,
                    -glyph => 'xyplot',
  		    -graph_type=>'points',
		    -point_symbol=>'disc',
		    -point_radius=>4,
		    -scale=>'both',
		    -height=>200,
  );

If you are using Generic Genome Browser, you will add this to the
configuration file:

  aggregators = repeat_density{density:repeat}
                clone alignment etc

Note that it is a good idea to add some padding to the left and right
of the panel; otherwise the scale will be partially cut off by the
edge of the image.

The "boxes" variant allows you to specify a pivot point such that
scores above the pivot point are drawn in one color, and scores below
are drawn in a different color. These "bicolor" plots are controlled
by the options -bicolor_pivot, -pos_color and -neg_color, as described
below.

=head2 OPTIONS

The following options are standard among all Glyphs.  See
L<Bio::Graphics::Glyph> for a full explanation.

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



( run in 2.250 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )