moving more stuff
[cview.git] / lib / CXGN / Cview / Chromosome / Glyph.pm
blob978cc67e6a2a131300cf81a5832c1812889e76a6
2 =head1 NAME
4 CXGN::Cview::Chromosome::Glyph - a class for drawing chromosome glyphs.
6 =head1 DESCRIPTION
8 The chromosomes are represented as small glyphs that can be partially filled with a color to represent the state of a chromosome sequencing project, for example. This class inherits from L<CXGN::Cview::Chromosome>.
10 =head1 SEE ALSO
12 See also the documentation in L<CXGN::Cview>.
14 =head1 AUTHOR(S)
16 Lukas Mueller (lam87@cornell.edu)
18 =head1 FUNCTIONS
21 =cut
25 use strict;
26 use CXGN::Cview::Chromosome;
29 package CXGN::Cview::Chromosome::Glyph;
31 use base qw | CXGN::Cview::Chromosome |;
33 =head2 function new()
35 Synopsis: constructor
36 Arguments: see L<CXGN::Cview::Chromosome>
38 =cut
40 sub new {
41 my $class = shift;
42 my $self = $class->SUPER::new(@_);
43 $self->set_outline_color(0,0,0);
44 $self->set_color(255, 255, 255);
45 $self->set_hilite_color(50, 255, 50);
46 $self->set_width(10);
47 $self->{curved_height} = 8;
48 $self->set_track_colors( [ 0, 255, 0],
49 [200, 255, 200],
51 [200, 200, 200],
52 [100, 150, 100], );
53 return $self;
57 =head2 function set_fill_level()
59 Synopsis:
60 Arguments: the track number and the fill level of the chromosome in %
61 (eg, 50, not 0.5, to represent 50%).
62 tracks: 0 for available and htgs3
63 1 for available and < htgs3
64 2 for complete, not submitted
65 3 for in progress
66 Returns: nothing
67 Side effects: the chromosome will be displayed with % filled in.
68 For fill levels specified above 100, the fill level value
69 will be capped at 100. (Yay! nice problem to have!).
70 Description:
72 =cut
74 sub set_fill_level {
76 # set the percentage level to be displayed as finished in the chromosome. 50% would be 50, not 0.5
78 my $self=shift;
79 my $track = shift;
80 my $level = shift;
82 if ($level>100) { $level = 100; }
83 if (!defined($track)) { die "set_fill_level: Need a track"; }
84 $self->{fill_level}->[$track] = $level;
87 sub get_fill_level {
88 my $self = shift;
89 my $track = shift;
90 return $self->{fill_level}->[$track];
93 =head2 function set_bac_count()
95 Synopsis:
96 Arguments: an integer reflecting the BACs sequenced.
97 is displayed below the glyph.
98 Returns:
99 Side effects:
100 Description:
102 =cut
105 sub set_bac_count {
107 # set the number of bacs sequenced for that chromosome
109 my $self=shift;
110 $self->{bac_count} = shift;
113 =head2 accessors get_track_colors(), set_track_colors()
115 Usage: $glyph->set_track_colors([255,255,0], [0,0,255]);
116 Desc:
117 Ret:
118 Args:
119 Side Effects:
120 Example:
122 =cut
124 sub get_track_colors {
125 my $self=shift;
126 return @{$self->{track_colors}};
130 sub set_track_colors {
131 my $self=shift;
132 @{$self->{track_colors}}=@_;
135 =head2 get_track_color
137 Usage: my ($r, $g, $b) = $glyph->get_track_color($track1)
138 Desc:
139 Ret:
140 Args:
141 Side Effects:
142 Example:
144 =cut
146 sub get_track_color {
147 my $self = shift;
148 my $track = shift;
149 return $self->{track_colors}->[$track];
152 =head2 function render()
154 Synopsis:
155 Arguments: a GD image object.
156 Returns:
157 Side effects: generates the image representing the glyph
158 Description:
160 =cut
162 sub render {
164 # draw the chromosome
166 my $self= shift;
168 $self->_calculate_scaling_factor();
170 my $image = shift;
172 # allocate colors
174 my $outline_color = $image -> colorResolve($self->{outline_color}->[0], $self->{outline_color}->[1], $self->{outline_color}->[2]);
175 my $hilite_color = $image -> colorResolve($self->{hilite_color}->[0], $self->{hilite_color}->[1], $self->{hilite_color}->[2]);
176 my $color = $image -> colorResolve($self->{color}->[0], $self->{color}->[1], $self->{color}->[2]);
178 my $halfwidth = $self ->{width}/2;
180 $image -> line($self->get_horizontal_offset() - $halfwidth, $self->get_vertical_offset() , $self->get_horizontal_offset()-$halfwidth, $self->get_vertical_offset()+$self->{height}, $outline_color);
181 $image -> line($self->get_horizontal_offset() + $halfwidth, $self->get_vertical_offset() , $self->get_horizontal_offset()+$halfwidth, $self->get_vertical_offset()+$self->{height}, $outline_color);
182 $image -> arc ($self->get_horizontal_offset(), $self->get_vertical_offset(), $self->{width}, $self->{curved_height}, 180, 0, $outline_color);
183 $image -> arc ($self->get_horizontal_offset(), $self->get_vertical_offset()+$self->{height}, $self->{width}, $self->{curved_height}, 0, 180, $outline_color);
184 $image -> fill ($self->get_horizontal_offset(), $self->get_vertical_offset(), $color);
186 my @track_color = ();
187 foreach my $track (0..(scalar($self->get_track_colors())-1)) {
189 #if ($track ==4) { die "TRACK=$track!!!!!"; }
190 my $colorRef = $self->get_track_color($track);
191 #print STDERR "TRACK COLOR: $colorRef->[0], $colorRef->[1], $colorRef->[2]\n";
192 $track_color[$track] = $image->colorResolve($colorRef->[0], $colorRef->[1], $colorRef->[2]);
195 my $start_level = $self->get_vertical_offset() + $self->mapunits2pixels(0);
196 foreach my $track (0..(scalar($self->get_track_colors())-1)) {
197 my $level = $self->get_vertical_offset() + $self->mapunits2pixels($self->get_fill_level($track));
198 #print STDERR "LEVEL: $level\n";
200 $image ->filledRectangle($self->get_horizontal_offset()-$halfwidth+1, $level,
201 $self->get_horizontal_offset()+$halfwidth-1, $start_level,
202 $track_color[$track]);
204 if ($self->get_fill_level($track) >= 99) {
205 $image->fill($self->get_horizontal_offset(), $self->get_vertical_offset()-2, $track_color[$track]);
207 $start_level = $level;
210 $image->fill($self->get_horizontal_offset(), $self->get_vertical_offset()+$self->mapunits2pixels(0)+1, $track_color[0]);
212 if ($self->{caption}) {
213 my $bigfont = GD::Font->Large();
214 $image -> string($bigfont, $self->get_horizontal_offset()- $bigfont->width() * length($self->{caption})/2, $self->get_vertical_offset()-$bigfont->height()-$self->{curved_height}/2, "$self->{caption}", $outline_color);
216 my $percent_finished = $self->get_fill_level(2);
217 # my $percent_finished_caption = (sprintf "%3d", $self->get_fill_level(0)."\%"); #+$self->get_fill_level(1))."\%";
218 my $percent_finished_caption = (sprintf "%3d", $percent_finished)."\%";
219 $image -> string($self->{font}, $self->get_horizontal_offset()- $self->{font}->width() * length($percent_finished_caption)/2, $self->get_vertical_offset() + $self->get_height()+$self->{curved_height}, "$percent_finished_caption", $outline_color);
224 sub draw_chromosome {
225 my $self = shift;
226 my $image = shift;
227 $self->render($image);
230 sub mapunits2pixels {
231 my $self = shift;
232 my $mapunits = shift;
234 my $pixels = $self->get_height() * (100-$mapunits)/100;
235 #print STDERR "Mapunits2pixels: $mapunits are $pixels pixels\n";
236 return $pixels;