Merge pull request #42 from solgenomics/topic/duplicate_image_warning
[cxgn-corelibs.git] / lib / CXGN / Phylo / Image.pm
blob06d8687ce1bcf9d87c144ee9ffd008dcd142c2ea
1 #!/usr/bin/perl
3 =head1 NAME
5 Image.pm - a front-end class to the Phylo library to easily generate tree images.
7 =head1 DESCRIPTION
9 This class provides some simple functions to generate a tree images in PNG format directly from a tree
10 definition file provided in the constructor and can embed the file on any html page using generate_html().
11 The image width and the image height can be set using set_image_width() and set_image_height().
13 A standard set of links is created in the file. There is currently not much control over how the program
14 generates these links. They essentially point to the SGN tree browser, so that the tree can be explored
15 interactively.
17 =head1 AUTHOR
19 Lukas Mueller (lam87@cornell.edu)
21 =cut
23 use strict;
25 use CXGN::Page;
26 use CXGN::Phylo;
27 use CXGN::Phylo::Tree_browser;
29 package CXGN::Phylo::Image;
31 sub new {
32 my $class = shift;
33 my $hash_ref = shift;
34 my $args={};
35 my $self = bless $args, $class;
37 $self->set_file($hash_ref->{file});
38 $self->set_file_type($hash_ref->{type});
39 $self->set_image_width(150);
40 $self->set_image_height(150);
41 return $self;
44 =head2 function get_file
46 Synopsis:
47 Arguments:
48 Returns:
49 Side effects:
50 Description:
52 =cut
54 sub get_file {
55 my $self=shift;
56 return $self->{file};
59 =head2 function set_file
61 Synopsis:
62 Arguments:
63 Returns:
64 Side effects:
65 Description:
67 =cut
69 sub set_file {
70 my $self=shift;
71 $self->{file}=shift;
74 =head2 function get_file_type
76 Synopsis:
77 Arguments:
78 Returns:
79 Side effects:
80 Description:
82 =cut
84 sub get_file_type {
85 my $self=shift;
86 return $self->{file_type};
89 =head2 function set_file_type
91 Synopsis:
92 Arguments:
93 Returns:
94 Side effects:
95 Description:
97 =cut
99 sub set_file_type {
100 my $self=shift;
101 $self->{file_type}=shift;
106 sub set_view {
107 my $self = shift;
108 $self->{view}=shift;
111 sub get_view {
112 my $self = shift;
113 return $self->{view};
116 =head2 function get_image_width
118 Synopsis:
119 Arguments:
120 Returns:
121 Side effects:
122 Description:
124 =cut
126 sub get_image_width {
127 my $self=shift;
128 return $self->{image_width};
131 =head2 function set_image_width
133 Synopsis:
134 Arguments:
135 Returns:
136 Side effects:
137 Description:
139 =cut
141 sub set_image_width {
142 my $self=shift;
143 $self->{image_width}=shift;
146 =head2 function get_image_height
148 Synopsis:
149 Arguments:
150 Returns:
151 Side effects:
152 Description:
154 =cut
156 sub get_image_height {
157 my $self=shift;
158 return $self->{image_height};
161 =head2 function set_image_height
163 Synopsis:
164 Arguments:
165 Returns:
166 Side effects:
167 Description:
169 =cut
171 sub set_image_height {
172 my $self=shift;
173 $self->{image_height}=shift;
176 =head2 function read_file()
178 Synopsis: $image->read_file($filename);
179 Arguments:
180 Returns: a newick formatted string
181 Side effects: sets the tree_string property
182 Description: it reads either a tre file or a file with no particular
183 format that contains a newick string. It the file type that
184 is available through get_file_type() to determine what file
185 type the file is. It also looks at the extension: files with a
186 suffix of .tre are assumed to be of tre format.
188 =cut
190 sub read_file {
191 my $self = shift;
192 my $file = shift;
193 my $type = shift;
194 my $tree_file_obj = CXGN::Phylo::Tree->new(file => $file, type=>$type);
195 return $tree_file_obj->get_tree_string();
198 =head2 function get_tree_string
200 Synopsis:
201 Arguments:
202 Returns:
203 Side effects:
204 Description:
206 =cut
208 sub get_tree_string {
209 my $self=shift;
210 return $self->{tree_string};
213 =head2 function set_tree_string
215 Synopsis:
216 Arguments:
217 Returns:
218 Side effects:
219 Description:
221 =cut
223 sub set_tree_string {
224 my $self=shift;
225 $self->{tree_string}=shift;
229 sub generate_html {
230 my $self = shift;
231 # read asterids definition file
233 open (F, "<".$self->get_file()) || die "Can't open file\n";
234 my $newick="";
235 while (<F>) {
236 chomp;
237 $newick .=$_;
239 close(F);
241 print STDERR "Newick String: $newick\n";
242 my $parser = CXGN::Phylo::Parse_newick->new($newick);
243 my $tree = $parser->parse();
247 if (!$tree) { exit(-1); }
248 if (!$self->get_view()) {
249 $self->set_view("asterid");
252 # retrieve some key nodes
254 my ($sol) = $tree->search_node_name("Solanaceae");
255 my ($con) = $tree->search_node_name("Convolvulaceae");
256 my ($ros) = $tree->search_node_name("Asterids");
257 my ($mon) = $tree->search_node_name("Monocots");
259 if ($self->get_view() eq "asterid") {
261 $sol->set_hidden(1);
263 $con->set_hidden(1);
265 $ros->set_hidden(1);
267 $mon->set_hidden(1);
270 if ($self->get_view() eq "solanaceae") {
271 $tree->set_root($sol);
272 $tree->get_layout()->set_image_height($self->get_image_height());
273 $tree->get_layout()->set_image_width($self->get_image_width());
276 # render image
278 my $browser=CXGN::Phylo::Tree_browser->new();
279 my ($tempfile, $temp_url) = $browser->create_temp_file();
280 my $renderer = CXGN::Phylo::PNG_tree_renderer->new($tree);
281 $renderer->get_layout()->set_top_margin(20);
282 $renderer->get_layout()->set_bottom_margin(20);
284 $renderer->render_png($tempfile.".png");
285 my $html_image_map = $renderer->get_html_image_map("map");
288 # output link
289 print "<img src=\"$temp_url.png\" usemap=\"#map\" /> " ;
290 print "$html_image_map";