Merge pull request #5 from solgenomics/topic/fix_il_maps
[cview.git] / lib / CXGN / Cview / MapImage.pm
blobcb21329ea143a8bf50093d27a3e1bc808e0b58a5
1 package CXGN::Cview::MapImage;
3 =head1 NAME
5 CXGN::Cview::MapImage - an interface for Cview map images.
7 =head1 DESCRIPTION
9 Inherits from L<CXGN::Cview::ImageI>.
11 =head1 AUTHOR(S)
13 Lukas Mueller (lam87@cornell.edu)
15 =head1 FUNCTIONS
18 =cut
20 use strict;
21 use warnings;
24 use GD;
25 use CXGN::Cview::ImageI;
27 use base qw / CXGN::Cview::ImageI /;
31 =head2 function MapImage::new()
33 MapImage -> new(map name, map_width [pixels], map_height [pixels])
35 Creates a new map object.
37 =cut
39 sub new {
40 my $class = shift;
41 my $map_name = shift;
42 my $width = shift; # the image width in pixels
43 my $height = shift; # the image height in pixels
45 my $self = $class->SUPER::new();
46 GD::Image->trueColor(1);
47 my $image = GD::Image->new($width, $height);
48 # $image || die "Can't generate image...";
49 $self->set_image($image); # make it truecolor (the last argument =1)
50 $self->{chromosomes} = ();
51 $self->{chr_links} = ();
52 $self->set_width($width);
53 $self->set_height($height);
54 $self->set_name($map_name);
56 return $self;
62 =head2 function render()
64 $map -> render() # takes no parameters
66 renders the map on the internal image.
68 =cut
70 sub render {
71 my $self = shift;
72 # the first color allocated is the background color.
73 $self->{white} = $self->get_image()->colorResolve(255,255,255);
74 $self->get_image()->filledRectangle(0,0 ,$self->{width}, $self->{height}, $self->{white});
77 foreach my $c (@{$self->{chromosomes}}) {
78 $c -> layout();
79 $c -> draw_chromosome($self->get_image());
81 foreach my $l (@{$self->{chr_links}}) {
82 $l -> render($self->get_image());
84 foreach my $r (@{$self->{rulers}}) {
85 $r -> render($self->get_image());
87 foreach my $p (@{$self->{physical}}) {
88 $p -> render($self->get_image());
90 foreach my $o (@{$self->{image_objects}}) {
91 $o -> render ($self->get_image());
94 foreach my $c (@{$self->{chromosomes}}) {
96 $c -> render_markers($self->get_image());
102 =head2 function render_png()
104 $map->render_png(); # no parameters
106 renders the image as a png to STDOUT.
108 =cut
110 sub render_png {
111 my $self= shift;
112 $self->render();
113 print $self->get_image()->png();
116 =head2 function render_png_string()
118 renders the png and returns it as a string.
120 =cut
124 sub render_png_string {
125 my $self =shift;
126 $self->render();
127 return $self->get_image()->png();
130 =head2 function render_png_file()
132 $map->render_png_file ($filepath)
134 render the image as a png saving the image at $filepath.
136 =cut
138 sub render_png_file {
139 my ($self, $filename) = @_;
140 $self->_render_to_file($filename,'png');
143 =head2 function render_jpg()
145 $map->render_jpg()
147 renders the image as a jpg to STDOUT.
149 =cut
152 sub render_jpg {
153 my $self = shift;
154 $self->render();
155 print $self->get_image()->jpeg();
158 =head2 function render_jpg_file()
160 $map->render_jpg_file(filepath)
162 renders the image as a jpg file at filepath
164 =cut
166 sub render_jpg_file {
167 my ($self, $filename) = @_;
168 $self->_render_to_file($filename,'jpeg');
172 sub render_gif_file {
173 my ($self, $filename) = @_;
174 $self->_render_to_file($filename,'gif');
177 sub _render_to_file {
178 my ($self, $filename, $format) = @_;
179 $self->render();
180 open (my $fh, ">", $filename) || die "Can't open $filename for writing!!! Check write permission in dest directory. : $!";
181 print $fh $self->get_image()->$format();
182 close($fh);
185 =head2 function get_image_map()
187 $string = $map->get_image_map()
189 Get the image map as a string. Calls get_image_map for all the objects contained
190 in the MapImage.
192 =cut
194 sub get_image_map {
195 my $self = shift;
196 my $map_name = shift;
197 #print STDERR "get_image_map map\n";
198 #as of 1/6/07, must use both NAME and ID to have both Mozilla and IE conformance, although standard xhtml uses ID only -- Evan
199 my $imagemap = "<map name=\"$map_name\" id=\"$map_name\">";
200 foreach my $c (@{$self->{chromosomes}}) {
201 #print STDERR "getting the chromosome image maps...\n";
202 $imagemap .= $c -> get_image_map();
204 foreach my $p (@{$self->{physical}}) {
205 $imagemap .= $p -> get_image_map();
208 #in xhtml 1.0+, a <map> must have child nodes, so if it doesn't, don't print it -- Evan, 1/6/07
209 if(scalar(@{$self->{chromosomes}}) > 0 or scalar(@{$self->{physical}}) > 0)
211 return $imagemap."</map>";
213 else
215 return "";
219 =head2 function add_chromosome()
221 $map->add_chromosome($chromosome_object)
223 adds the chromosome object to the map. Obviously works also for subclasses of
224 chromosomes such as physical and IL.
226 =cut
228 sub add_image_object {
229 my $self = shift;
230 my $object = shift;
231 push @{$self->{image_objects}}, $object;
234 sub add_chromosome {
235 my $self = shift;
236 my $chromosome = shift;
238 push @{$self->{chromosomes}}, $chromosome;
241 sub get_chromosomes {
242 my $self = shift;
243 return @{$self->{chromosomes}};
246 =head2 function add_chr_link()
248 $map->add_chr_link($chr_link)
250 adds the chromosome linking object $chr_link to the map.
252 =cut
254 sub add_chr_link {
255 my $self = shift;
256 my $chr_link = shift;
257 push @{$self->{chr_links}}, $chr_link;
260 =head2 function add_ruler()
262 $map->add_ruler($ruler)
264 adds the ruler $ruler to the map.
266 =cut
268 sub add_ruler {
269 my $self = shift;
270 my $ruler = shift;
271 push @{$self->{rulers}}, $ruler;
274 =head2 function add_physical()
276 $map->add_physical($physical)
278 adds the physical map $physical to the map.
280 Note: The physical object has to be populated both in terms of marker
281 positions and physical map.
283 =cut
285 sub add_physical {
286 my $self = shift;
287 my $physical = shift;
288 push @{$self->{physical}}, $physical;