| 1 | package Bio::Graphics::Browser2::Plugin::SinWave; | ||
| 2 | # $Id: SinWave.pm,v 1.2 2008-12-02 23:56:53 lstein Exp $ | ||
| 3 | # test plugin | ||
| 4 | use strict; | ||
| 5 | use Bio::Graphics::Browser2::Plugin; | ||
| 6 | use Bio::Graphics::Feature; | ||
| 7 | use CGI qw(:standard *table); | ||
| 8 | |||
| 9 | use vars '$VERSION','@ISA'; | ||
| 10 | $VERSION = '0.1'; | ||
| 11 | use constant RADIANS_PER_CYCLE => 2*3.14159265; # pi | ||
| 12 | use constant BINS_PER_SEGMENT => 1000; # number of data points | ||
| 13 | |||
| 14 | @ISA = qw(Bio::Graphics::Browser2::Plugin); | ||
| 15 | |||
| 16 | sub name { "Sine Wave" } | ||
| 17 | |||
| 18 | sub description { | ||
| 19 | p("The sine wave plugin generates a sine wave ", | ||
| 20 | "on the current view."). | ||
| 21 | p("It was written to illustrate how to create quantitative features for the xyplot."); | ||
| 22 | } | ||
| 23 | |||
| 24 | sub type { 'annotator' } | ||
| 25 | |||
| 26 | sub init { } | ||
| 27 | |||
| 28 | sub config_defaults { | ||
| 29 | my $self = shift; | ||
| 30 | return { cycles => 10, | ||
| 31 | }; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub reconfigure { | ||
| 35 | my $self = shift; | ||
| 36 | my $current_config = $self->configuration; | ||
| 37 | my $defaults = $self->config_defaults; | ||
| 38 | |||
| 39 | my $new_cycles = $self->config_param('cycles'); | ||
| 40 | if ($new_cycles >= 1 and $new_cycles < 100) { # sanity check | ||
| 41 | $current_config->{cycles} = $new_cycles; | ||
| 42 | } else { # doesn't pass check, so go to defaults | ||
| 43 | $current_config->{cycles} = $defaults->{cycles}; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | sub configure_form { | ||
| 50 | my $self = shift; | ||
| 51 | my $current_config = $self->configuration; | ||
| 52 | return | ||
| 53 | "Cycles per window: ".textfield(-name=>$self->config_name('cycles'), | ||
| 54 | -default=>$current_config->{cycles} | ||
| 55 | ); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub annotate { | ||
| 59 | my $self = shift; | ||
| 60 | my $segment = shift; | ||
| 61 | |||
| 62 | my $chr = $segment->seq_id; | ||
| 63 | my $segstart = $segment->start; | ||
| 64 | my $length = $segment->length; | ||
| 65 | |||
| 66 | my $cycles = $self->configuration->{cycles}; | ||
| 67 | my $radians_per_bin = RADIANS_PER_CYCLE * $cycles/BINS_PER_SEGMENT; | ||
| 68 | my $bases_per_bin = $length/BINS_PER_SEGMENT; | ||
| 69 | |||
| 70 | my $feature_list = Bio::Graphics::FeatureFile->new; | ||
| 71 | $feature_list->add_type('wave' => {glyph => 'xyplot', | ||
| 72 | key => "$cycles cycles", | ||
| 73 | bgcolor => 'blue', | ||
| 74 | height => 50, | ||
| 75 | min_score => -1.0, | ||
| 76 | max_score => +1.0, | ||
| 77 | }); | ||
| 78 | |||
| 79 | my $curve = Bio::Graphics::Feature->new(); | ||
| 80 | |||
| 81 | for (0..BINS_PER_SEGMENT-1) { | ||
| 82 | my $start = $segstart + int($_ * $bases_per_bin); | ||
| 83 | my $end = int($start + $bases_per_bin); | ||
| 84 | my $score = sin($_ * $radians_per_bin); | ||
| 85 | |||
| 86 | $curve->add_SeqFeature(Bio::Graphics::Feature->new(-seq_id=>$chr, | ||
| 87 | -start => $start, | ||
| 88 | -end => $end, | ||
| 89 | -score => $score)); | ||
| 90 | } | ||
| 91 | $feature_list->add_feature($curve,'wave'); | ||
| 92 | return $feature_list; | ||
| 93 | } | ||
| 94 | |||
| 95 | 1; | ||
| 96 |