oops, this directory was missed...
[cview.git] / lib / CXGN / ViewMaps.pm
blob059268b4d042446e5af539e7a82365af54683f7c
2 package CXGN::Cview::ViewMaps;
4 use CXGN::Page::FormattingHelpers qw | page_title_html |;
5 use CXGN::Cview::MapFactory;
6 use CXGN::Cview::Chromosome_viewer;
7 use CXGN::Cview::ChrLink;
8 use CXGN::Cview::Utils qw | set_marker_color |;
9 use CXGN::Cview::MapImage;
10 use CXGN::Tools::WebImageCache;
11 use CXGN::Map;
12 use CXGN::VHost;
14 use base qw | CXGN::DB::Object |;
18 =head2 function new()
20 Synopsis:
21 Arguments: a database handle [ DBI object or CXGN::DB::Connection object]
22 the base dir [ file path ]
23 the temp_dir [ relative path ]
24 Returns: a handle to a view_maps object
25 Side effects:
26 Description: constructor
28 =cut
30 sub new {
31 my $class = shift;
32 my $dbh = shift;
33 my $basepath = shift;
34 my $temp_dir = shift;
36 my $self = bless {}, $class;
37 $self->set_dbh($dbh);
38 # set some defaults...
40 $self->{unzoomedheight} = 20; # how many cM are seen at zoom level 1
42 # create an set the cache file in the constructor,
43 # such that we can define the temp dirs before
44 # we generate the image using generate_image()
46 my $cache = CXGN::Tools::WebImageCache->new();
47 $self->set_cache($cache);
48 $cache->set_basedir($basepath);
49 $cache->set_temp_dir($temp_dir);
51 return $self;
54 sub adjust_parameters {
55 my $self = shift;
57 # adjust input arguments
63 =head2 accessors set_maps(), get_maps()
65 Property:
66 Setter Args:
67 Getter Args:
68 Getter Ret:
69 Side Effects:
70 Description:
72 =cut
74 sub get_maps {
75 my $self=shift;
76 return @{$self->{maps}};
79 sub set_maps {
80 my $self=shift;
81 @{$self->{maps}}=@_;
84 =head2 accessors set_cache(), get_cache()
86 Property: the CXGN::Tools::WebImageCache object
87 Args/Ret: the same
88 Side Effects: this is the object used to generate the
89 cache image.
90 Description:
92 =cut
94 sub get_cache {
95 my $self=shift;
96 return $self->{cache};
99 sub set_cache {
100 my $self=shift;
101 $self->{cache}=shift;
107 =head2 function generate_page()
109 Arguments: none
110 Returns: nothing
111 Side effects: generates the CXGN::Cview::MapImage and stores it
112 to the cache if necessary, or reads just reads
113 the image cache if it is still valid.
115 =cut
117 sub generate_image {
118 my $self = shift;
120 # define a key for the cache. lets just use the name of the
121 # script and the map_version_ids of the maps being displayed
123 $self->get_cache()->set_key("view_maps".(join "-", map { $_->get_id() } ($self->get_maps())));
125 $self->get_cache()->set_expiration_time(86400);
128 if (! $self->get_cache()->is_valid()) {
129 my $map_width = $self->{map_width} = 720;
130 my $x_distance = $map_width/4; # the number of pixels the different elements are spaced
131 my $row_count = 0;
132 my $row_height = 120; # the height of a row (vertical space for each chromosome image)
133 my $y_distance = $row_height * (1/3);
134 my $chr_height = $row_height * (2/3);
135 my $map_height;
137 # determine the maximum chromosome count among all maps
138 # so that we can accommodate it
140 my $max_chr = 0;
141 foreach my $m ($self->get_maps()) {
142 my $chr_count = 0;
143 if ($m) {
144 $chr_count = $m->get_chromosome_count();
146 if ($chr_count > $max_chr) { $max_chr=$chr_count; }
149 $map_height = $row_height * $max_chr+2*$y_distance;
150 # show the ruler if requested
152 # if ($self->{show_ruler}) {
153 # my $r = ruler->new($x_distance-20, $row_height * $row_count + $y_distance, $chr_height, 0, $self->{c}{$track}[$i]->get_chromosome_length());
154 # $self->{map}->add_ruler($r);
156 $row_count++;
158 $self->{map} = CXGN::Cview::MapImage -> new("", $map_width, $map_height);
160 # get all the chromosomes and add them to the map
162 my $track = 0;
163 foreach my $map ($self->get_maps()) {
164 my @chr_names = $map->get_chromosome_names();
165 for (my $i=0; $i<$map->get_chromosome_count(); $i++) {
167 $self->{c}{$track}[$i] = ($self->get_maps())[$track]->get_chromosome($i+1);
168 $self->{c}{$track}[$i] -> set_vertical_offset($row_height*$i+$y_distance);
169 $self->{c}{$track}[$i] -> set_horizontal_offset($x_distance + $x_distance * ($track));
170 $self->{c}{$track}[$i] -> set_height($chr_height);
171 $self->{c}{$track}[$i] -> set_caption( $chr_names[$i] );
172 $self->{c}{$track}[$i] -> set_width(16);
173 $self->{c}{$track}[$i] -> set_url("/cview/view_chromosome.pl?map_version_id=".($self->get_maps())[$track]->get_id()."&amp;chr_nr=$i");
175 $self->{c}{$track}[$i] -> set_labels_none();
176 $self->{map}->add_chromosome($self->{c}{$track}[$i]);
178 $track++;
182 # get the connections between the chromosomes
184 my %find = ();
186 for (my $track=0; $track<($self->get_maps()); $track++) {
187 for (my $i =0; $i< ($self->get_maps())[$track]->get_chromosome_count(); $i++) {
188 foreach my $m ($self->{c}{$track}[$i]->get_markers()) {
189 $m->hide_label();
190 # make entry into the find hash and store corrsponding chromosomes and offset
191 # (for drawing connections)
192 # if the map is the reference map ($compare_to_map is false).
194 $find{$m->get_id()}->{$track}->{chr}=$i;
195 $find{$m->get_id()}->{$track}->{offset}=$m->get_offset();
197 # set the marker colors
199 set_marker_color($m, "marker_types");
204 foreach my $f (keys(%find)) {
205 foreach my $t (keys %{$find{$f}}) {
206 my $chr = $find{$f}->{$t}->{chr};
207 my $offset = $find{$f}->{$t}->{offset};
209 if (exists($find{$f}->{$t-1}) || defined($find{$f}->{$t-1})) {
210 my $comp_chr = $find{$f}->{$t-1}->{chr};
211 my $comp_offset = $find{$f}->{$t-1}->{offset};
212 #print STDERR "Found on track $t: Chr=$chr offset=$offset, links to track ".($t-1)." Chr=$comp_chr offset $comp_offset\n";
213 if ($comp_chr) {
214 my $link1 = CXGN::Cview::ChrLink->new($self->{c}{$t}[$chr], $offset, $self->{c}{$t-1}[$comp_chr], $comp_offset);
215 $self->{map}->add_chr_link($link1);
218 if (exists($find{$f}->{$t+1})) {
219 my $comp_chr = $find{$f}->{$t+1}->{chr};
220 my $comp_offset = $find{$f}->{$t+1}->{offset};
221 my $link2 = CXGN::Cview::ChrLink->new($self->{c}{$t}[$chr], $offset, $self->{c}{$t+1}[$comp_chr], $comp_offset);
222 $self->{map}->add_chr_link($link2);
229 $self->get_cache()->set_map_name("viewmap");
230 $self->get_cache()->set_image_data($self->{map}->render_png_string());
231 $self->get_cache()->set_image_map_data($self->{map}->get_image_map("viewmap"));
233 # # show the ruler if requested
235 # if ($self->{show_ruler}) {
236 # my $r = ruler->new($x_distance-20, $row_height * $row_count + $y_distance, $chr_height, 0, $self->{c}{$track}[$i]->get_chromosome_length());
237 # $self->{map}->add_ruler($r);
242 # my $filename = "cview".(rand())."_".$$.".png";
244 # $self->{image_path} = $vhost_conf->get_conf('basepath').$vhost_conf->get_conf('tempfiles_subdir')."/cview/$filename";
245 # $self->{image_url} = $vhost_conf->get_conf('tempfiles_subdir')."/cview/$filename";
247 # $self->{map} -> render_png_file($self->{image_path});
249 # $self->{imagemap} = $self->{map}->get_image_map("imagemap");
255 sub get_select_toolbar {
256 my $self = shift;
258 # $left_map = $self->get_left_map() || 0;
259 # $center_map = $self->get_center_map() || 0;
260 # $right_map = $self->get_right_map || 0;
261 my @names = ("left_map_version_id", "center_map_version_id", "right_map_version_id");
262 my @selects = ();
263 for (my $i=0; $i< 3; $i++) {
264 if ( defined(($self->get_maps())[$i])) {
265 push @selects, CXGN::Cview::Utils::get_maps_select($self->get_dbh(), ($self->get_maps())[$i]->get_id(), $names[$i], 1);
267 else {
268 push @selects, CXGN::Cview::Utils::get_maps_select($self->get_dbh(), undef, $names[$i], 1);
271 # my $center_select = CXGN::Cview::Utils::get_maps_select($self, !$center_map || $center_map->get_id(), "center_map_version_id", 1);
272 # my $right_select = CXGN::Cview::Utils::get_maps_select($self, !$right_map || $right_map->get_id(), "right_map_version_id", 1);
275 return qq {
276 <form action="#">
277 <center>
278 <table summary=""><tr><td>$selects[0]</td><td>$selects[1]</td><td>$selects[2]</td></tr></table>
279 <input type="submit" value="set" />
280 </center>
281 </form>
285 # =head2 function display()
287 # Synopsis:
288 # Arguments:
289 # Returns:
290 # Side effects:
291 # Description: composes the page and displays it.
293 # =cut
295 # sub display {
296 # my $self = shift;
299 # $self->{page}->header("SGN comparative mapviewer");
300 # my $width = int($self->{map_width}/3);
302 # my $select = $self->get_select_toolbar();
304 # print "$select";
306 # if (!$self->get_maps()) {
307 # print "<br /><br /><center>Note: No maps are selected. Please select maps from the pull down menus above.</center>\n";
310 # print $self->get_cache()->get_image_html();
312 # # print "<img src=\"$self->{image_url}\" usemap=\"#chr_comp_map\" border=\"0\" alt=\"\" />\n";
314 # # print $self->{map}->get_image_map("chr_comp_map");
316 # $self->{page}->footer();
322 =head2 function error_message_page()
324 Synopsis:
325 Arguments:
326 Returns:
327 Side effects:
328 Description:
330 =cut
332 sub error_message_page {
333 my $self = shift;
335 my $title = page_title_html("Error: No center map defined");
337 print <<HTML;
339 $title
341 A center map needs to be defined for this page to work. Please supply
342 a center_map_version_id as a parameter. If this was the result of a link,
343 please inform SGN about the error.
344 </p>
346 Contact SGN at <a href="mailto:sgn-feedback\@sgn.cornell.edu">sgn-feedback\@sgn.cornell.edu</a>
348 HTML
350 exit();
354 sub clean_up {
355 my $self = shift;
359 return 1;