]> git.donarmstrong.com Git - biopieces.git/blob - www/index.cgi
1dfeb539862cb92daf81e3f598a109b5366e6aab
[biopieces.git] / www / index.cgi
1 #!/usr/bin/env perl
2
3 # Copyright (C) 2006-2009 Martin A. Hansen.
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
22
23 use strict;
24 use warnings;
25
26 use lib $ENV{ 'BP_PERL' };
27
28 use CGI;
29 use URI::Escape;
30 use Data::Dumper;
31 use Digest::MD5;
32 use Maasha::Common;
33 use Maasha::Filesys;
34 use Maasha::Calc;
35 use Maasha::XHTML;
36 use Maasha::BGB::Session;
37 use Maasha::BGB::Track;
38 use Maasha::BGB::Draw;
39
40 my ( $cgi, $cookie, @html );
41
42 $cgi    = new CGI;
43 $cookie = cookie_default( $cgi );;
44
45 push @html, Maasha::XHTML::html_header(
46     cgi_header  => 1,
47     title       => 'Biopieces Genome Browser',
48     css_file    => 'bgb.css',
49     author      => 'Martin A. Hansen, mail@maasha.dk',
50     description => 'Biopieces Genome Browser',
51     keywords    => [ qw( Biopieces biopiece genome browser viewer bacterium bacteria prokaryote prokaryotes ) ],
52     no_cache    => 1,
53 );
54
55 push @html, Maasha::XHTML::h1( txt => "Biopieces Genome Browser", class => 'center' );
56 push @html, Maasha::XHTML::form_beg( action => $cookie->{ 'SCRIPT' }, method => "post", enctype => "multipart/form-data" );
57
58 push @html, page( $cookie );
59
60 push @html, Maasha::XHTML::form_end;
61 push @html, Maasha::XHTML::body_end;
62 push @html, Maasha::XHTML::html_end;
63
64 # push @html, Maasha::XHTML::hdump( { $cgi->Vars } );  # DEBUG
65 # push @html, Maasha::XHTML::hdump( $cookie );         # DEBUG
66 # push @html, Maasha::XHTML::hdump( \%ENV );           # DEBUG
67
68 print "$_\n" foreach @html;
69
70 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
71
72
73 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> COOKIE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
74
75
76 sub cookie_default
77 {
78     # Martin A. Hansen, November 2009.
79
80     # Set a cookie with values from the CGI object or defaults.
81
82     my ( $cgi,   # CGI object
83        ) = @_;
84
85     # Returns a hash.
86
87     my ( $cookie, $session );
88
89     $cookie = {};
90
91     if ( $cgi->param( 'nav_search' ) )
92     {
93         if ( $cgi->param( 'nav_search' ) =~ /([0-9,]+)[ -]([0-9,]+)/ )
94         {
95             $cookie->{ 'NAV_START' } = $1;
96             $cookie->{ 'NAV_END' }   = $2;
97         }
98         else
99         {
100             $cookie->{ 'SEARCH' } = $cgi->param( 'nav_search' );
101
102             $cgi->param( 'page', 'search' );
103         }
104     }
105
106     $cookie->{ 'SCRIPT' }      = Maasha::Common::get_scriptname();
107     $cookie->{ 'DATA_DIR' }    = "Data";
108     $cookie->{ 'SESSION_DIR' } = "Sessions";
109     $cookie->{ 'LIST_PAGES' }  = [ qw( clade genome assembly contig browse ) ];
110     $cookie->{ 'USER' }        = $cgi->param( 'user' );
111     $cookie->{ 'PASSWORD' }    = $cgi->param( 'password' );
112     $cookie->{ 'SESSION_ID' }  = $cgi->param( 'session_id' );
113
114     $session = session_restore( $cookie );
115
116     cookie_login( $cookie );
117
118     # session_id_check( $cookie, $session );
119
120     if ( $cookie->{ 'LOGIN_ERROR' } or $cookie->{ 'SESSION_ERROR' } )
121     {
122         $cookie->{ 'PAGE' } = 'login';
123     }
124     elsif ( $cookie->{ 'LOGIN' } )
125     {
126         if ( $cgi->param( 'genome' ) ) {
127             $cookie->{ 'PAGE' } = 'browse';
128         } else {
129             $cookie->{ 'PAGE' } = $session->{ 'PAGE' } || 'clade';
130         }
131     }
132     elsif ( not defined $cookie->{ 'USER' } )
133     {
134         $cookie->{ 'PAGE' } = 'login';
135     }
136     else
137     {
138          $cookie->{ 'PAGE' } = $cgi->param( 'page' ) || 'login';
139     }
140
141     $cookie->{ 'TRACK_STATUS' }    = $session->{ 'TRACK_STATUS' };
142     $cookie->{ 'CLADE' }           = $cgi->param( 'clade' )       || $session->{ 'CLADE' };
143     $cookie->{ 'GENOME' }          = $cgi->param( 'genome' )      || $session->{ 'GENOME' };
144     $cookie->{ 'ASSEMBLY' }        = $cgi->param( 'assembly' )    || $session->{ 'ASSEMBLY' };
145     $cookie->{ 'CONTIG' }          = $cgi->param( 'contig' )      || $session->{ 'CONTIG' };
146     $cookie->{ 'Q_ID' }            = $cgi->param( 'q_id' )        || $session->{ 'Q_ID' };
147     $cookie->{ 'NAV_START' }     ||= defined $cgi->param( 'nav_start' ) ? $cgi->param( 'nav_start' ) : $session->{ 'NAV_START' };
148     $cookie->{ 'NAV_END' }       ||= defined $cgi->param( 'nav_end' )   ? $cgi->param( 'nav_end' )   : $session->{ 'NAV_END' };
149     $cookie->{ 'NAV_CENTER' }      = $cgi->param( 'nav_center' );
150     $cookie->{ 'S_BEG' }           = $cgi->param( 's_beg' )       || $session->{ 'S_BEG' };
151     $cookie->{ 'S_END' }           = $cgi->param( 's_end' )       || $session->{ 'S_END' };
152     $cookie->{ 'STRAND' }          = $cgi->param( 'strand' )      || $session->{ 'STRAND' };
153     $cookie->{ 'TRACK' }           = $cgi->param( 'track' );
154     $cookie->{ 'ZOOM_IN1' }        = $cgi->param( 'zoom_in1' );
155     $cookie->{ 'ZOOM_IN2' }        = $cgi->param( 'zoom_in2' );
156     $cookie->{ 'ZOOM_IN3' }        = $cgi->param( 'zoom_in3' );
157     $cookie->{ 'ZOOM_OUT1' }       = $cgi->param( 'zoom_out1' );
158     $cookie->{ 'ZOOM_OUT2' }       = $cgi->param( 'zoom_out2' );
159     $cookie->{ 'ZOOM_OUT3' }       = $cgi->param( 'zoom_out3' );
160     $cookie->{ 'MOVE_LEFT1' }      = $cgi->param( 'move_left1' );
161     $cookie->{ 'MOVE_LEFT2' }      = $cgi->param( 'move_left2' );
162     $cookie->{ 'MOVE_LEFT3' }      = $cgi->param( 'move_left3' );
163     $cookie->{ 'MOVE_RIGHT1' }     = $cgi->param( 'move_right1' );
164     $cookie->{ 'MOVE_RIGHT2' }     = $cgi->param( 'move_right2' );
165     $cookie->{ 'MOVE_RIGHT3' }     = $cgi->param( 'move_right3' );
166
167     $cookie->{ 'IMG_WIDTH' }       = 1200;   # Width of browser image in pixels
168     $cookie->{ 'IMG_HEIGHT' }      = 800;    # Height of browser image in pixels  # TODO: Redundant?
169     $cookie->{ 'WIGGLE_HEIGHT' }   = 75;     # Height of Wiggle tracks in pixels
170     $cookie->{ 'TRACK_OFFSET' }    = 20;
171     $cookie->{ 'TRACK_SPACE' }     = 20;
172     $cookie->{ 'RULER_FONT_SIZE' } = 10;     # Size of ruler font in pixels
173     $cookie->{ 'RULER_COLOR' }     = [ 0, 0, 0 ];
174     $cookie->{ 'SEQ_FONT_SIZE' }   = 10;
175     $cookie->{ 'SEQ_COLOR' }       = [ 0, 0, 0, ];
176     $cookie->{ 'FEAT_WIDTH' }      = 5;
177     $cookie->{ 'FEAT_COLOR' }      = [ 0, 0, 0 ];
178 #    $cookie->{ 'FEAT_MAX' }        = 5000;   # TODO: Reduntant?
179
180 #    $cookie->{ 'LIST_USER' }       = Maasha::BGB::Track::list_users();  # TODO: Redundant?
181
182     if ( $cookie->{ 'USER' } and not $cookie->{ 'LOGIN_ERROR' } )
183     {
184         $cookie->{ 'LIST_CLADE' }      = Maasha::BGB::Track::list_clades(     $cookie->{ 'USER' } );
185         $cookie->{ 'LIST_GENOME' }     = Maasha::BGB::Track::list_genomes(    $cookie->{ 'USER' }, $cookie->{ 'CLADE' } );
186         $cookie->{ 'LIST_ASSEMBLY' }   = Maasha::BGB::Track::list_assemblies( $cookie->{ 'USER' }, $cookie->{ 'CLADE' }, $cookie->{ 'GENOME' } );
187         $cookie->{ 'LIST_CONTIG' }     = Maasha::BGB::Track::list_contigs(    $cookie->{ 'USER' }, $cookie->{ 'CLADE' }, $cookie->{ 'GENOME' }, $cookie->{ 'ASSEMBLY' } );
188
189         if ( $cookie->{ 'CONTIG' } )
190         {
191             cookie_start( $cookie );
192             cookie_end( $cookie );
193             cookie_zoom( $cookie );
194             cookie_move( $cookie );
195             cookie_center( $cookie );
196             cookie_track_status( $cookie );
197         }
198     }
199
200     $cookie->{ 'STRAND' } = '+' if defined $cookie->{ 'STRAND' } and $cookie->{ 'STRAND' } eq ' ';   # FIXME ugly HTML fix
201
202     session_store( $cookie, $session );
203
204     return wantarray ? %{ $cookie } : $cookie;
205 }
206
207
208 sub session_restore
209 {
210     # Martin A. Hansen, March 2010.
211
212     # Restores session and returns this.
213
214     my ( $cookie,   # cookie hash
215        ) = @_;
216
217     # Returns hashref.
218
219     my ( $session );
220
221     if ( defined $cookie->{ 'USER' } and -d "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }" ) {
222         $session = Maasha::BGB::Session::session_restore( "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }/session.json" );
223     }
224
225     return wantarray ? %{ $session } : $session;
226 }
227
228
229 sub session_store
230 {
231     # Martin A. Hansen, March 2010.
232
233     # Store a session updated with cookie information to session file.
234
235     my ( $cookie,    # cookie hash
236          $session,   # session hash
237        ) = @_;
238
239     # Returns nothing.
240
241     my ( $new_session );
242
243     if ( defined $cookie->{ 'USER' } and $cookie->{ 'USER' } ne '' and -d "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }" )
244     {
245         $new_session->{ 'PASSWORD' }   = $session->{ 'PASSWORD' };
246         $new_session->{ 'SESSION_ID' } = $session->{ 'SESSION_ID' };
247         $new_session->{ 'TIME' }       = Maasha::Common::time_stamp();
248         $new_session->{ 'PAGE' }       = $cookie->{ 'PAGE' };
249
250         if ( $cookie->{ 'PAGE' } =~ /browse|export_pdf|export_svg/ )
251         {
252             $new_session->{ 'CLADE' }     = $cookie->{ 'CLADE' };
253             $new_session->{ 'GENOME' }    = $cookie->{ 'GENOME' };
254             $new_session->{ 'ASSEMBLY' }  = $cookie->{ 'ASSEMBLY' };
255             $new_session->{ 'CONTIG' }    = $cookie->{ 'CONTIG' };
256             $new_session->{ 'NAV_START' } = $cookie->{ 'NAV_START' };
257             $new_session->{ 'NAV_END' }   = $cookie->{ 'NAV_END' };
258         }
259         elsif ( $cookie->{ 'PAGE' } eq 'contig' )
260         {
261             $new_session->{ 'CLADE' }     = $cookie->{ 'CLADE' };
262             $new_session->{ 'GENOME' }    = $cookie->{ 'GENOME' };
263             $new_session->{ 'ASSEMBLY' }  = $cookie->{ 'ASSEMBLY' };
264         }
265         elsif ( $cookie->{ 'PAGE' } eq 'assembly' )
266         {
267             $new_session->{ 'CLADE' }     = $cookie->{ 'CLADE' };
268             $new_session->{ 'GENOME' }    = $cookie->{ 'GENOME' };
269         }
270         elsif ( $cookie->{ 'PAGE' } eq 'genome' )
271         {
272             $new_session->{ 'CLADE' }     = $cookie->{ 'CLADE' };
273         }
274         elsif ( $cookie->{ 'PAGE' } eq 'export' )
275         {
276             $new_session->{ 'CLADE' }     = $cookie->{ 'CLADE' };
277             $new_session->{ 'GENOME' }    = $cookie->{ 'GENOME' };
278             $new_session->{ 'ASSEMBLY' }  = $cookie->{ 'ASSEMBLY' };
279             $new_session->{ 'CONTIG' }    = $cookie->{ 'CONTIG' };
280             $new_session->{ 'Q_ID' }      = $cookie->{ 'Q_ID' };
281             $new_session->{ 'S_BEG' }     = $cookie->{ 'S_BEG' };
282             $new_session->{ 'S_END' }     = $cookie->{ 'S_END' };
283             $new_session->{ 'STRAND' }    = $cookie->{ 'STRAND' };
284         }
285
286         $new_session->{ 'TRACK_STATUS' } = $cookie->{ 'TRACK_STATUS' };
287
288         Maasha::BGB::Session::session_store( "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }/session.json", $new_session );
289     }
290 }
291
292
293 sub session_id_check
294 {
295     # Martin A. Hansen, March 2010.
296
297     # Check that the session id in the cookie and session match.
298     # Sets SESSION_ERROR flag in cookie if not matching.
299
300     my ( $cookie,    # cookie hash
301          $session,   # session hash
302        ) = @_;
303
304     # Returns nothing.
305
306     if ( defined $cookie->{ 'SESSION_ID' } and defined $session->{ 'SESSION_ID' } )
307     {
308         if ( $cookie->{ 'SESSION_ID' } ne $session->{ 'SESSION_ID' } ) {
309             $cookie->{ 'SESSION_ERROR' } = 1;
310         } else {
311             $cookie->{ 'SESSION_ERROR' } = 0;
312         }
313     }
314 }
315
316
317 sub cookie_login
318 {
319     # Martin A. Hansen, December 2009.
320
321     # Check a user and password from CGI against the password file and
322     # set the session ID, if found, in the cookie.
323
324     my ( $cookie,   # cookie hash
325        ) = @_;
326
327     # Returns nothing.
328
329     my ( $session );
330
331     if ( defined $cookie->{ 'PASSWORD' } )
332     {
333         if ( defined $cookie->{ 'USER' } and -d "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }" )
334         {
335             $session = Maasha::BGB::Session::session_restore( "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }/session.json" );
336
337             if ( $session->{ 'PASSWORD' } eq Digest::MD5::md5_hex( $cookie->{ 'PASSWORD' } ) )
338             {
339                 $cookie->{ 'TIME' }          = Maasha::Common::time_stamp();
340                 $cookie->{ 'SESSION_ID' }  ||= Maasha::BGB::Session::session_new();
341                 $cookie->{ 'LOGIN' }         = 1;
342                 $cookie->{ 'LOGIN_ERROR' }   = 0;
343             }
344             else
345             {
346                 $cookie->{ 'LOGIN_ERROR' } = 1;
347             }
348         }
349         else
350         {
351             $cookie->{ 'LOGIN_ERROR' } = 1;
352         }
353     }
354 }
355
356
357 sub cookie_start
358 {
359     # Martin A. Hansen, November 2009.
360
361     # Decommify the cookie value for NAV_START and adjust it to
362     # to prevent negative values.
363
364     my ( $cookie,   # cookie hash
365        ) = @_;
366
367     # Returns nothing.
368
369     if ( defined $cookie->{ 'NAV_START' } )
370     {
371         $cookie->{ 'NAV_START' } =~ tr/,//d;
372         $cookie->{ 'NAV_START' } = 0 if $cookie->{ 'NAV_START' } < 0;
373     }
374     else
375     {
376         $cookie->{ 'NAV_START' } = 0;
377     }
378 }
379
380
381 sub cookie_end
382 {
383     # Martin A. Hansen, November 2009.
384
385     # Decommify the cookie value for NAV_END and adjust it to prevent
386     # overshooting the max value for the contig size as determined
387     # from the cookie.
388
389     my ( $cookie,   # cookie hash
390        ) = @_;
391
392     # Returns nothing.
393
394     my ( $max );
395     
396     $max = Maasha::Filesys::file_size( Maasha::BGB::Track::path_seq( $cookie ) );
397
398     if ( defined $cookie->{ 'NAV_END' } )
399     {
400         $cookie->{ 'NAV_END' } =~ tr/,//d;
401         $cookie->{ 'NAV_END' } = $max if $cookie->{ 'NAV_END' } > $max;
402     }
403     else
404     {
405         $cookie->{ 'NAV_END' } = $max;
406     }
407 }
408
409
410 sub cookie_zoom
411 {
412     # Martin A. Hansen, November 2009.
413
414     # Adjust the cookie values for NAV_START and NAV_END based
415     # on cookie ZOOM values.
416
417     my ( $cookie,   # cookie hash
418        ) = @_;
419
420     # Returns nothing.
421
422     my ( $max, $dist, $new_dist, $dist_diff );
423
424     $max = Maasha::Filesys::file_size( Maasha::BGB::Track::path_seq( $cookie ) );
425
426     $dist = $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' };
427
428     if ( defined $cookie->{ 'ZOOM_IN1' } ) {
429         $new_dist = $dist / 1.5;
430     } elsif ( defined $cookie->{ 'ZOOM_IN2' } ) {
431         $new_dist = $dist / 3;
432     } elsif ( defined $cookie->{ 'ZOOM_IN3' } ) {
433         $new_dist = $dist / 10;
434     } elsif ( defined $cookie->{ 'ZOOM_OUT1' } ) {
435         $new_dist = $dist * 1.5;
436     } elsif ( defined $cookie->{ 'ZOOM_OUT2' } ) {
437         $new_dist = $dist * 3;
438     } elsif ( defined $cookie->{ 'ZOOM_OUT3' } ) {
439         $new_dist = $dist * 10;
440     }
441
442     if ( $new_dist )
443     {
444         $dist_diff = $dist - $new_dist;
445
446         $cookie->{ 'NAV_START' } = int( $cookie->{ 'NAV_START' } + ( $dist_diff / 2 ) );
447         $cookie->{ 'NAV_END' }   = int( $cookie->{ 'NAV_END' }   - ( $dist_diff / 2 ) );
448
449         $cookie->{ 'NAV_START' } = 0    if $cookie->{ 'NAV_START' } < 0;
450         $cookie->{ 'NAV_END' }   = $max if $cookie->{ 'NAV_END' } > $max;
451     }
452 }
453
454
455 sub cookie_move
456 {
457     # Martin A. Hansen, November 2009.
458
459     # Adjust the cookie values for NAV_START and NAV_END based
460     # on cookie MOVE values.
461
462     my ( $cookie,   # cookie hash
463        ) = @_;
464
465     # Returns nothing.
466     
467     my ( $max, $dist, $shift, $new_start, $new_end );
468
469     $max = Maasha::Filesys::file_size( Maasha::BGB::Track::path_seq( $cookie ) );
470
471     $dist = $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' };
472
473     if ( defined $cookie->{ 'MOVE_LEFT1' } ) {
474         $shift = -1 * $dist * 0.10;
475     } elsif ( defined $cookie->{ 'MOVE_LEFT2' } ) {
476         $shift = -1 * $dist * 0.475;
477     } elsif ( defined $cookie->{ 'MOVE_LEFT3' } ) {
478         $shift = -1 * $dist * 0.95;
479     } elsif ( defined $cookie->{ 'MOVE_RIGHT1' } ) {
480         $shift = $dist * 0.10;
481     } elsif ( defined $cookie->{ 'MOVE_RIGHT2' } ) {
482         $shift = $dist * 0.475;
483     } elsif ( defined $cookie->{ 'MOVE_RIGHT3' } ) {
484         $shift = $dist * 0.95;
485     }
486
487     if ( $shift )
488     {
489         $new_start = int( $cookie->{ 'NAV_START' } + $shift );
490         $new_end   = int( $cookie->{ 'NAV_END' }   + $shift );
491
492         if ( $new_start > 0 and $new_end < $max )
493         {
494             $cookie->{ 'NAV_START' } = $new_start;
495             $cookie->{ 'NAV_END' }   = $new_end;
496         }
497     }
498 }
499
500
501 sub cookie_center
502 {
503     # Martin A. Hansen, March 2010.
504    
505     # Adjust the cookie values for NAV_START and NAV_END based
506     # on cookie CENTER value if defined.
507
508     my ( $cookie,   # cookie hash
509        ) = @_;
510
511     # Returns nothing.
512
513     my ( $l_dist, $r_dist );
514
515     if ( defined $cookie->{ 'NAV_CENTER' } )
516     {
517         $l_dist = $cookie->{ 'NAV_CENTER' } - $cookie->{ 'NAV_START' };
518         $r_dist = $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_CENTER' };
519
520         if ( $l_dist > $r_dist ) {
521             $cookie->{ 'NAV_START' } = $cookie->{ 'NAV_END' } - 2 * $r_dist;
522         } else {
523             $cookie->{ 'NAV_END' } = $cookie->{ 'NAV_START' } + 2 * $l_dist;
524         }
525     }
526 }
527
528
529 sub cookie_track_status
530 {
531     # Martin A. Hansen, March 2010.
532
533     #
534     
535     my ( $cookie,   # cookie hash
536        ) = @_;
537
538     # Returns nothing.
539
540     my ( $clade, $genome, $assembly, $track );
541
542     $clade    = $cookie->{ 'CLADE' };
543     $genome   = $cookie->{ 'GENOME' };
544     $assembly = $cookie->{ 'ASSEMBLY' };
545     $track    = $cookie->{ 'TRACK' };
546
547     if ( $track )
548     {
549         if ( $cookie->{ 'TRACK_STATUS' }->{ $clade }->{ $genome }->{ $assembly }->{ $track } ) {
550             $cookie->{ 'TRACK_STATUS' }->{ $clade }->{ $genome }->{ $assembly }->{ $track } = 0;
551         } else {
552             $cookie->{ 'TRACK_STATUS' }->{ $clade }->{ $genome }->{ $assembly }->{ $track } = 1;
553         }
554     }
555 }
556
557
558 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PAGES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
559
560
561 sub page
562 {
563     # Martin A. Hansen, November 2009.
564
565     # Determines what page to render based on
566     # the cookie's PAGE setting.
567
568     my ( $cookie,   # cookie hash
569        ) = @_;
570
571     # Returns a list.
572
573     my ( @html );
574
575     if ( $cookie->{ 'SESSION_ID' } ) {
576         push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "session_id", value => $cookie->{ 'SESSION_ID' } ) );
577     }
578
579     if ( $cookie->{ 'PAGE' } eq 'login' ) {
580         push @html, page_login( $cookie );
581     } elsif ( $cookie->{ 'PAGE' } eq 'search' ) {
582         push @html, page_search( $cookie );
583     } elsif ( $cookie->{ 'PAGE' } eq 'browse' ) {
584         push @html, page_browse( $cookie );
585     } elsif ( $cookie->{ 'PAGE' } eq 'dna' ) {
586         push @html, page_dna( $cookie );
587     } elsif ( $cookie->{ 'PAGE' } eq 'export' ) {
588         push @html, page_export( $cookie );
589     } elsif ( $cookie->{ 'PAGE' } eq 'export_pdf' ) {
590         push @html, page_export_pdf( $cookie );
591     } elsif ( $cookie->{ 'PAGE' } eq 'export_svg' ) {
592         push @html, page_export_svg( $cookie );
593     } elsif ( $cookie->{ 'PAGE' } eq 'clade' ) {
594         push @html, page_clade( $cookie );
595     } elsif ( $cookie->{ 'PAGE' } eq 'genome' ) {
596         push @html, page_genome( $cookie );
597     } elsif ( $cookie->{ 'PAGE' } eq 'assembly' ) {
598         push @html, page_assembly( $cookie );
599     } elsif ( $cookie->{ 'PAGE' } eq 'contig' ) {
600         push @html, page_contig( $cookie );
601     }
602
603     return wantarray ? @html : \@html;
604 }
605
606
607 sub page_login
608 {
609     # Martin A. Hansen, December 2009.
610
611     # Renders the login page.
612
613     my ( $cookie,
614        ) = @_;
615
616     # Returns a list.
617
618     my ( @html );
619
620     push @html, section_login( $cookie );
621
622     return wantarray ? @html : \@html;
623 }
624
625
626 sub page_search
627 {
628     # Martin A. Hansen, December 2009.
629
630     # Renders the search page.
631
632     my ( $cookie,
633        ) = @_;
634
635     # Returns a list.
636
637     my ( @html );
638
639     push @html, section_taxonomy_table( $cookie );
640     push @html, section_search( $cookie );
641
642     return wantarray ? @html : \@html;
643 }
644
645
646 sub page_browse
647 {
648     # Martin A. Hansen, November 2009.
649     
650     # Renders the browse page.
651
652     my ( $cookie,
653        ) = @_;
654
655     # Returns a list.
656
657     my ( @html );
658
659     push @html, section_taxonomy_table( $cookie );
660     push @html, section_navigate( $cookie );
661     push @html, section_browse( $cookie );
662     push @html, section_linkout( $cookie );
663
664     return wantarray ? @html : \@html;
665 }
666
667
668 sub page_dna
669 {
670     # Martin A. Hansen, November 2009.
671     
672     # Renders the DNA page.
673
674     my ( $cookie,
675        ) = @_;
676
677     # Returns a list.
678
679     my ( @html );
680
681     push @html, section_taxonomy_table( $cookie );
682     push @html, section_dna( $cookie );
683
684     return wantarray ? @html : \@html;
685 }
686
687
688 sub page_export
689 {
690     # Martin A. Hansen, November 2009.
691
692     # Renders the export page.
693
694     my ( $cookie,
695        ) = @_;
696
697     # Returns a list.
698
699     my ( @html );
700
701     push @html, section_taxonomy_table( $cookie );
702     push @html, section_export( $cookie );
703
704     return wantarray ? @html : \@html;
705 }
706
707
708 sub page_export_pdf
709 {
710     # Martin A. Hansen, March 2010.
711
712     # Renders the export PDF page.
713
714     my ( $cookie,
715        ) = @_;
716
717     # Returns a list.
718
719     my ( @html );
720
721     push @html, section_taxonomy_table( $cookie );
722     push @html, section_export_pdf( $cookie );
723
724     return wantarray ? @html : \@html;
725 }
726
727
728 sub page_export_svg
729 {
730     # Martin A. Hansen, March 2010.
731
732     # Renders the export SVG page.
733
734     my ( $cookie,
735        ) = @_;
736
737     # Returns a list.
738
739     my ( @html );
740
741     push @html, section_taxonomy_table( $cookie );
742     push @html, section_export_svg( $cookie );
743
744     return wantarray ? @html : \@html;
745 }
746
747
748 sub page_clade
749 {
750     # Martin A. Hansen, March 2010.
751     
752     # Renders the clade selection page.
753
754     my ( $cookie,   # cookie hash
755        ) = @_;
756
757     # Returns a list.
758
759     my ( @html );
760
761     push @html, section_taxonomy_table( $cookie );
762     push @html, section_clade( $cookie );
763
764     return wantarray ? @html : \@html;
765 }
766
767
768 sub page_genome
769 {
770     # Martin A. Hansen, March 2010.
771     
772     # Renders the genome selection page.
773
774     my ( $cookie,   # cookie hash
775        ) = @_;
776
777     # Returns a list.
778
779     my ( @html );
780
781     push @html, section_taxonomy_table( $cookie );
782     push @html, section_genome( $cookie );
783
784     return wantarray ? @html : \@html;
785 }
786
787
788 sub page_assembly
789 {
790     # Martin A. Hansen, March 2010.
791     
792     # Renders the clade selection page.
793
794     my ( $cookie,   # cookie hash
795        ) = @_;
796
797     # Returns a list.
798
799     my ( @html );
800
801     push @html, section_taxonomy_table( $cookie );
802     push @html, section_assembly( $cookie );
803
804     return wantarray ? @html : \@html;
805 }
806
807
808 sub page_contig
809 {
810     # Martin A. Hansen, November 2009.
811     
812     # Renders the browse page.
813
814     my ( $cookie,   # cookie hash
815        ) = @_;
816
817     # Returns a list.
818
819     my ( @html );
820
821     push @html, section_taxonomy_table( $cookie );
822     push @html, section_contig( $cookie );
823
824     return wantarray ? @html : \@html;
825 }
826
827
828 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SECTIONS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
829
830
831 sub section_login
832 {
833     # Martin A. Hansen, December 2009.
834    
835     # Returns a HTML section with a login menu.
836
837     my ( $cookie,   # cookie hash
838        ) = @_;
839
840     # Returns a list.
841
842     my ( $user, $password, $login, @html );
843
844     $user     = Maasha::XHTML::text( name => "user", value => "", size => 20 );
845     $password = Maasha::XHTML::password( name => "password", value => "", size => 20 );
846     $login    = Maasha::XHTML::submit( name => "login_submit", value => "Login" );
847
848     push @html, Maasha::XHTML::h2( txt => "Login", class => 'center' );
849
850     push @html, Maasha::XHTML::table_beg( summary => "Login table", align => 'center' );
851     push @html, Maasha::XHTML::table_row_simple( tr => [ "User:", $user ] );
852     push @html, Maasha::XHTML::table_row_simple( tr => [ "Password:", $password ] );
853     push @html, Maasha::XHTML::table_row_simple( tr => [ "", $login ] );
854     push @html, Maasha::XHTML::table_end;
855
856     if ( defined $cookie->{ 'CLADE' }     and
857          defined $cookie->{ 'GENOME' }    and
858          defined $cookie->{ 'ASSEMBLY' }  and
859          defined $cookie->{ 'CONTIG' }    and
860          defined $cookie->{ 'NAV_START' } and
861          defined $cookie->{ 'NAV_END' }
862     )
863     {
864         push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "clade",     value => $cookie->{ 'CLADE' } ) );
865         push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "genome",    value => $cookie->{ 'GENOME' } ) );
866         push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "assembly",  value => $cookie->{ 'ASSEMBLY' } ) );
867         push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "contig",    value => $cookie->{ 'CONTIG' } ) );
868         push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "nav_start", value => $cookie->{ 'NAV_START' } ) );
869         push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "nav_end",   value => $cookie->{ 'NAV_END' } ) );
870     }
871
872     if ( $cookie->{ 'LOGIN_ERROR' } ) {
873         push @html, Maasha::XHTML::h3( txt => "Bad user or password - please retry", class => 'error' );
874     }
875
876     return wantarray ? @html : \@html;
877 }
878
879
880 sub section_taxonomy_table
881 {
882     # Martin A. Hansen, November 2009.
883
884     # Returns a HTML section with a taxonomy table
885     # showing the location in the taxonomy and with
886     # links to browse the taxonomy.
887
888     my ( $cookie,   # cookie hash
889        ) = @_;
890
891     # Returns a list.
892
893     my ( $page, @row, @html, $href, $txt );
894     
895     foreach $page ( @{ $cookie->{ 'LIST_PAGES' } } )
896     {
897         last if $page eq $cookie->{ 'PAGE' } or $page eq 'browse';
898
899         $href  = "$cookie->{ 'SCRIPT' }?page=$page";
900         $href .= "&session_id=$cookie->{ 'SESSION_ID' }";
901         $href .= "&user=$cookie->{ 'USER' }";
902         $href .= "&clade=$cookie->{ 'CLADE' }"       if $page !~ /clade/;
903         $href .= "&genome=$cookie->{ 'GENOME' }"     if $page !~ /clade|genome/;
904         $href .= "&assembly=$cookie->{ 'ASSEMBLY' }" if $page !~ /clade|genome|assembly/;
905         $href .= "&contig=$cookie->{ 'CONTIG' }"     if $page !~ /clade|genome|assembly|contig/;
906
907         $txt = ": $cookie->{ uc $page }";
908
909         push @row, Maasha::XHTML::ln( txt => $page, href => $href, class => 'inline' );
910         push @row, Maasha::XHTML::p(  txt => $txt, class => 'inline' );
911     }
912
913     push @html, Maasha::XHTML::table_beg( summary => "Taxonomy table", align => 'center', cellpadding => '5px' );
914     push @html, Maasha::XHTML::table_row_simple( tr => [ join( "\n", @row ) ], align => 'center' );
915     push @html, Maasha::XHTML::table_end;
916
917     return wantarray ? @html : \@html;
918 }
919
920
921 sub section_clade
922 {
923     # Martin A. Hansen, March 2010.
924     
925     # Returns a HTML section with clade selection choices
926     # for navigating the taxonomy tree.
927
928     my ( $cookie,   # cookie hash
929        ) = @_;
930
931     # Returns a list.
932
933     my ( @html, $clade, $href );
934
935     push @html, Maasha::XHTML::h2( txt => "Select clade", class => 'center' );
936
937     push @html, Maasha::XHTML::table_beg( summary => "Taxonomy select table", align => 'center', cellpadding => '5px' );
938
939     foreach $clade ( @{ $cookie->{ 'LIST_CLADE' } } )
940     {
941         $href  = "$cookie->{ 'SCRIPT' }?page=genome";
942         $href .= "&session_id=$cookie->{ 'SESSION_ID' }";
943         $href .= "&user=$cookie->{ 'USER' }";
944         $href .= "&clade=$clade";
945
946         push @html, Maasha::XHTML::table_row_simple( tr => [ Maasha::XHTML::ln( txt => $clade, href => $href ) ] );
947     }
948
949     push @html, Maasha::XHTML::table_end;
950
951     return wantarray ? @html : \@html;
952 }
953
954
955 sub section_genome
956 {
957     # Martin A. Hansen, March 2010.
958     
959     # Returns a HTML section with genome selection choices
960     # for navigating the taxonomy tree.
961
962     my ( $cookie,   # cookie hash
963        ) = @_;
964
965     # Returns a list.
966
967     my ( @html, $genome, $href );
968
969     push @html, Maasha::XHTML::h2( txt => "Select genome", class => 'center' );
970
971     push @html, Maasha::XHTML::table_beg( summary => "Taxonomy select table", align => 'center', cellpadding => '5px' );
972
973     foreach $genome ( @{ $cookie->{ 'LIST_GENOME' } } )
974     {
975         $href  = "$cookie->{ 'SCRIPT' }?page=assembly";
976         $href .= "&session_id=$cookie->{ 'SESSION_ID' }";
977         $href .= "&user=$cookie->{ 'USER' }";
978         $href .= "&clade=$cookie->{ 'CLADE' }";
979         $href .= "&genome=$genome";
980
981         push @html, Maasha::XHTML::table_row_simple( tr => [ Maasha::XHTML::ln( txt => $genome, href => $href ) ] );
982     }
983
984     push @html, Maasha::XHTML::table_end;
985
986     return wantarray ? @html : \@html;
987 }
988
989
990 sub section_assembly
991 {
992     # Martin A. Hansen, March 2010.
993     
994     # Returns a HTML section with assembly selection choices
995     # for navigating the taxonomy tree.
996
997     my ( $cookie,   # cookie hash
998        ) = @_;
999
1000     # Returns a list.
1001
1002     my ( @html, $assembly, $href );
1003
1004     push @html, Maasha::XHTML::h2( txt => "Select assembly", class => 'center' );
1005
1006     push @html, Maasha::XHTML::table_beg( summary => "Taxonomy select table", align => 'center', cellpadding => '5px' );
1007
1008     foreach $assembly ( @{ $cookie->{ 'LIST_ASSEMBLY' } } )
1009     {
1010         $href  = "$cookie->{ 'SCRIPT' }?page=contig";
1011         $href .= "&session_id=$cookie->{ 'SESSION_ID' }";
1012         $href .= "&user=$cookie->{ 'USER' }";
1013         $href .= "&clade=$cookie->{ 'CLADE' }";
1014         $href .= "&genome=$cookie->{ 'GENOME' }";
1015         $href .= "&assembly=$assembly";
1016
1017         push @html, Maasha::XHTML::table_row_simple( tr => [ Maasha::XHTML::ln( txt => $assembly, href => $href ) ] );
1018     }
1019
1020     push @html, Maasha::XHTML::table_end;
1021
1022     return wantarray ? @html : \@html;
1023 }
1024
1025
1026 sub section_contig
1027 {
1028     # Martin A. Hansen, March 2010.
1029     
1030     # Returns a HTML section with contig selection choices
1031     # for navigating the taxonomy tree.
1032
1033     my ( $cookie,   # cookie hash
1034        ) = @_;
1035
1036     # Returns a list.
1037
1038     my ( @html, $contig, $href );
1039
1040     push @html, Maasha::XHTML::h2( txt => "Select contig", class => 'center' );
1041
1042     push @html, Maasha::XHTML::table_beg( summary => "Taxonomy select table", align => 'center', cellpadding => '5px' );
1043
1044     foreach $contig ( @{ $cookie->{ 'LIST_CONTIG' } } )
1045     {
1046         $href  = "$cookie->{ 'SCRIPT' }?page=browse";
1047         $href .= "&session_id=$cookie->{ 'SESSION_ID' }";
1048         $href .= "&user=$cookie->{ 'USER' }";
1049         $href .= "&clade=$cookie->{ 'CLADE' }";
1050         $href .= "&genome=$cookie->{ 'GENOME' }";
1051         $href .= "&assembly=$cookie->{ 'ASSEMBLY' }";
1052         $href .= "&contig=$contig";
1053
1054         push @html, Maasha::XHTML::table_row_simple( tr => [ Maasha::XHTML::ln( txt => $contig, href => $href ) ] );
1055     }
1056
1057     push @html, Maasha::XHTML::table_end;
1058
1059     return wantarray ? @html : \@html;
1060 }
1061
1062
1063 sub section_navigate
1064 {
1065     # Martin A. Hansen, November 2009.
1066
1067     # Returns a HTML section for navigating in the browser window.
1068
1069     my ( $cookie,   # cookie hash
1070        ) = @_;
1071
1072     # Returns a list.
1073
1074     my ( @html, $nav_val );
1075
1076     $nav_val = Maasha::Calc::commify( $cookie->{ 'NAV_START' } ) . "-" . Maasha::Calc::commify( $cookie->{ 'NAV_END' } );
1077
1078     push @html, Maasha::XHTML::table_beg( summary => "Navigation table", align => 'center' );
1079     push @html, Maasha::XHTML::table_row_simple( tr => [
1080         "Position or search term:",
1081         Maasha::XHTML::text( name => "nav_search", value => $nav_val, size => 30 ),
1082         Maasha::XHTML::submit( name => "nav_submit", value => "Submit" ),
1083     ] );
1084     push @html, Maasha::XHTML::table_end;
1085
1086     push @html, Maasha::XHTML::table_beg( summary => "Zoom table", align => 'center' );
1087     push @html, Maasha::XHTML::table_row_simple( tr => [
1088         Maasha::XHTML::p( txt => 'Move:' ),
1089         Maasha::XHTML::submit( name => "move_left3",  value => "<<<", title => "move 95% to the left" ),
1090         Maasha::XHTML::submit( name => "move_left2",  value => "<<",  title => "move 47.5% to the left" ),
1091         Maasha::XHTML::submit( name => "move_left1",  value => "<",   title => "move 10% to the left" ),
1092         Maasha::XHTML::submit( name => "move_right1", value => ">",   title => "move 10% to the rigth" ),
1093         Maasha::XHTML::submit( name => "move_right2", value => ">>",  title => "move 47.5% to the rigth" ),
1094         Maasha::XHTML::submit( name => "move_right3", value => ">>>", title => "move 95% to the right" ),
1095         Maasha::XHTML::p( txt => 'Zoom in:' ),
1096         Maasha::XHTML::submit( name => "zoom_in1", value => "1.5x" ),
1097         Maasha::XHTML::submit( name => "zoom_in2", value => "3x" ),
1098         Maasha::XHTML::submit( name => "zoom_in3", value => "10x" ),
1099         Maasha::XHTML::p( txt => 'Zoom out:' ),
1100         Maasha::XHTML::submit( name => "zoom_out1", value => "1.5x" ),
1101         Maasha::XHTML::submit( name => "zoom_out2", value => "3x" ),
1102         Maasha::XHTML::submit( name => "zoom_out3", value => "10x" ),
1103     ] );
1104     push @html, Maasha::XHTML::table_end;
1105
1106     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "page",     value => "browse" ) );
1107     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "user",     value => $cookie->{ 'USER' } ) );
1108     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "clade",    value => $cookie->{ 'CLADE' } ) );
1109     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "genome",   value => $cookie->{ 'GENOME' } ) );
1110     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "assembly", value => $cookie->{ 'ASSEMBLY' } ) );
1111     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "contig",   value => $cookie->{ 'CONTIG' } ) );
1112
1113     return wantarray ? @html : \@html;
1114 }
1115
1116
1117 sub section_linkout
1118 {
1119     # Martin A. Hansen, March 2010.
1120
1121     # Returns a HTML section for a static link based on information in the cookie.
1122
1123     my ( $cookie,   # cookie hash
1124        ) = @_;
1125
1126     # Returns a list.
1127
1128     my ( $link_out, $link_pdf, $link_svg, @html );
1129
1130     $link_out = Maasha::XHTML::ln(
1131         txt  => 'link',
1132         href => join( "&", "$cookie->{ 'SCRIPT' }?page=browse",
1133                            "clade=$cookie->{ 'CLADE' }",
1134                            "genome=$cookie->{ 'GENOME' }",
1135                            "assembly=$cookie->{ 'ASSEMBLY' }",
1136                            "contig=$cookie->{ 'CONTIG' }",
1137                            "nav_start=$cookie->{ 'NAV_START' }",
1138                            "nav_end=$cookie->{ 'NAV_END' }",
1139               ),
1140         title  => "Static link to this view",
1141     );
1142
1143     $link_pdf = Maasha::XHTML::ln(
1144         txt  => 'PDF',
1145         href => join( "&", "$cookie->{ 'SCRIPT' }?page=export_pdf",
1146                            "session_id=$cookie->{ 'SESSION_ID' }",
1147                            "user=$cookie->{ 'USER' }",
1148                            "clade=$cookie->{ 'CLADE' }",
1149                            "genome=$cookie->{ 'GENOME' }",
1150                            "assembly=$cookie->{ 'ASSEMBLY' }",
1151                            "contig=$cookie->{ 'CONTIG' }",
1152                            "nav_start=$cookie->{ 'NAV_START' }",
1153                            "nav_end=$cookie->{ 'NAV_END' }",
1154               ),
1155         title  => "Export view in PDF",
1156     );
1157     
1158     $link_svg = Maasha::XHTML::ln(
1159         txt  => 'SVG',
1160         href => join( "&", "$cookie->{ 'SCRIPT' }?page=export_svg",
1161                            "session_id=$cookie->{ 'SESSION_ID' }",
1162                            "user=$cookie->{ 'USER' }",
1163                            "clade=$cookie->{ 'CLADE' }",
1164                            "genome=$cookie->{ 'GENOME' }",
1165                            "assembly=$cookie->{ 'ASSEMBLY' }",
1166                            "contig=$cookie->{ 'CONTIG' }",
1167                            "nav_start=$cookie->{ 'NAV_START' }",
1168                            "nav_end=$cookie->{ 'NAV_END' }",
1169               ),
1170         title  => "Export view in SVG",
1171     );
1172
1173     push @html, Maasha::XHTML::p( txt => "$link_out $link_pdf $link_svg", class => 'center' );
1174
1175     return wantarray ? @html : \@html;
1176 }
1177
1178
1179 sub section_browse
1180 {
1181     my ( $cookie,
1182        ) = @_;
1183
1184     # Returns a list.
1185
1186     my ( @track_list, $i, @tracks, $track, $elem, $png_data, @html, @img, $x1, $y1, $x2, $y2, $factor, $center );
1187
1188     push @tracks, [ Maasha::BGB::Track::track_ruler( $cookie ) ];
1189     push @tracks, [ Maasha::BGB::Track::track_seq( $cookie ) ];
1190
1191     @track_list = Maasha::BGB::Track::list_track_dir( $cookie->{ 'USER' }, $cookie->{ 'CLADE' }, $cookie->{ 'GENOME' }, $cookie->{ 'ASSEMBLY' }, $cookie->{ 'CONTIG' } );
1192
1193     for ( $i = 0; $i < @track_list; $i++ )
1194     {
1195         $cookie->{ 'FEAT_COLOR' } = Maasha::BGB::Draw::palette( $i );
1196
1197         push @tracks, [ Maasha::BGB::Track::track_feature( $track_list[ $i ], $cookie ) ];
1198     }
1199
1200     unshift @tracks, [ Maasha::BGB::Track::track_grid( $cookie ) ];
1201
1202     $png_data = Maasha::BGB::Draw::render_png( $cookie->{ 'IMG_WIDTH' }, $cookie->{ 'TRACK_OFFSET' }, \@tracks );
1203
1204     push @img, Maasha::XHTML::img(
1205         src    => "data:image/png;base64,$png_data",
1206         alt    => "Browser Tracks",
1207         height => $cookie->{ 'TRACK_OFFSET' },
1208         width  => $cookie->{ 'IMG_WIDTH' },
1209         id     => "browser_map",
1210         usemap => "#browser_map"
1211     );
1212
1213     push @img, Maasha::XHTML::map_beg( name => "browser_map", id => "browser_map" );
1214
1215     $factor = ( $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' } + 1 ) / $cookie->{ 'IMG_WIDTH' };
1216
1217     for ( $i = 0; $i < $cookie->{ 'IMG_WIDTH' }; $i += 5 )
1218     {
1219         $x1 = $i;
1220         $y1 = 10;
1221         $x2 = $i + 5;
1222         $y2 = 20;
1223
1224         $center = int( $cookie->{ 'NAV_START' } + $factor * $i );
1225
1226         push @img, Maasha::XHTML::area(
1227             href   => join( "&", "$cookie->{ 'SCRIPT' }?page=browse",
1228                                  "session_id=$cookie->{ 'SESSION_ID' }",
1229                                  "user=$cookie->{ 'USER' }",
1230                                  "clade=$cookie->{ 'CLADE' }",
1231                                  "genome=$cookie->{ 'GENOME' }",
1232                                  "assembly=$cookie->{ 'ASSEMBLY' }",
1233                                  "contig=$cookie->{ 'CONTIG' }",
1234                                  "nav_start=$cookie->{ 'NAV_START' }",
1235                                  "nav_end=$cookie->{ 'NAV_END' }",
1236                                  "nav_center=$center",
1237                       ),
1238             shape  => 'rect',
1239             coords => "$x1, $y1, $x2, $y2",
1240             title  => "Center on " . Maasha::Calc::commify( $center ),
1241         );
1242     }
1243
1244     foreach $track ( @tracks )
1245     {
1246         foreach $elem ( @{ $track } )
1247         {
1248             next if $elem->{ 'type' } =~ /grid|text|wiggle/;
1249
1250             if ( $elem->{ 'type' } eq 'track_name' )
1251             {
1252                 $x1 = $elem->{ 'x1' };
1253                 $y1 = $elem->{ 'y1' } - 10;
1254                 $x2 = $elem->{ 'x1' } + 6 * length $elem->{ 'txt' };
1255                 $y2 = $elem->{ 'y1' };
1256
1257                 if ( Maasha::BGB::Track::track_hide( $cookie, $elem->{ 'track' } ) )
1258                 {
1259                     push @img, Maasha::XHTML::area(
1260                         href   => join( "&", "$cookie->{ 'SCRIPT' }?page=browse",
1261                                              "session_id=$cookie->{ 'SESSION_ID' }",
1262                                              "user=$cookie->{ 'USER' }",
1263                                              "clade=$cookie->{ 'CLADE' }",
1264                                              "genome=$cookie->{ 'GENOME' }",
1265                                              "assembly=$cookie->{ 'ASSEMBLY' }",
1266                                              "contig=$cookie->{ 'CONTIG' }",
1267                                              "track=$elem->{ 'track' }",
1268                                   ),
1269                         shape  => 'rect',
1270                         coords => "$x1, $y1, $x2, $y2",
1271                         title  => qq(Show track: $elem->{ 'txt' }),
1272                     );
1273                 }
1274                 else
1275                 {
1276                     push @img, Maasha::XHTML::area(
1277                         href   => join( "&", "$cookie->{ 'SCRIPT' }?page=browse",
1278                                              "session_id=$cookie->{ 'SESSION_ID' }",
1279                                              "user=$cookie->{ 'USER' }",
1280                                              "clade=$cookie->{ 'CLADE' }",
1281                                              "genome=$cookie->{ 'GENOME' }",
1282                                              "assembly=$cookie->{ 'ASSEMBLY' }",
1283                                              "contig=$cookie->{ 'CONTIG' }",
1284                                              "track=$elem->{ 'track' }",
1285                                   ),
1286                         shape  => 'rect',
1287                         coords => "$x1, $y1, $x2, $y2",
1288                         title  => qq(Hide track: $elem->{ 'txt' }),
1289                     );
1290                 }
1291             }
1292             else
1293             {
1294                 push @img, Maasha::XHTML::area(
1295                     href     => join( "&", "$cookie->{ 'SCRIPT' }?page=export",
1296                                            "session_id=$cookie->{ 'SESSION_ID' }",
1297                                            "user=$cookie->{ 'USER' }",
1298                                            "clade=$cookie->{ 'CLADE' }",
1299                                            "genome=$cookie->{ 'GENOME' }",
1300                                            "assembly=$cookie->{ 'ASSEMBLY' }",
1301                                            "contig=$cookie->{ 'CONTIG' }",
1302                                            "s_beg=$elem->{ 's_beg' }",
1303                                            "s_end=$elem->{ 's_end' }",
1304                                            "strand=$elem->{ 'strand' }",
1305                                            "q_id=$elem->{ 'q_id' }",
1306                               ),
1307                     shape  => 'rect',
1308                     coords => "$elem->{ x1 }, $elem->{ y1 }, $elem->{ x2 }, $elem->{ y2 }",
1309                     title  => "$elem->{ 'title' }",
1310                 );
1311             }
1312         }
1313     }
1314
1315     push @img, Maasha::XHTML::map_end();
1316
1317     push @html, Maasha::XHTML::p( txt => join( "\n", @img ) );
1318
1319     @html = Maasha::XHTML::div( txt => join( "\n", @html ), class => 'browse' );
1320
1321     return wantarray ? @html : \@html;
1322 }
1323
1324
1325 sub section_export
1326 {
1327     # Martin A. Hansen, November 2009.
1328
1329     # Returns a HTML section with export table.
1330
1331     my ( $cookie,   # cookie hash
1332        ) = @_;
1333
1334     # Returns a list.
1335
1336     my ( $feat, @row_dna_contig, @html );
1337
1338     $feat = $cookie->{ 'Q_ID' } || "[no name]";
1339
1340     push @row_dna_contig, Maasha::XHTML::p( txt => qq(Export Feature: "$feat" DNA from Contig: ), class => 'inline' );
1341     push @row_dna_contig, Maasha::XHTML::p( txt  => "$cookie->{ 'CONTIG' } Start: ", class => 'inline' );
1342     push @row_dna_contig, Maasha::XHTML::text( name => "s_beg", value => Maasha::Calc::commify( $cookie->{ 'S_BEG' } ), size => 15 );
1343     push @row_dna_contig, Maasha::XHTML::p( txt => "End: ", class => 'inline' );
1344     push @row_dna_contig, Maasha::XHTML::text( name => "s_end", value => Maasha::Calc::commify( $cookie->{ 'S_END' } ), size => 15 );
1345     push @row_dna_contig, Maasha::XHTML::p( txt => "Strand: ", class => 'inline' );
1346     push @row_dna_contig, Maasha::XHTML::menu( name => "strand", options => [ qw( + - ) ], selected => $cookie->{ 'STRAND' } );
1347     push @row_dna_contig, Maasha::XHTML::submit( name => "export", value => "Export", title => "Fetch DNA from this region" );
1348
1349     push @html, Maasha::XHTML::h2( txt => "Export", class => 'center' );
1350     push @html, Maasha::XHTML::table_beg( summary => "Taxonomy select table", align => 'center' );
1351     push @html, Maasha::XHTML::table_row_simple( tr => \@row_dna_contig );
1352     push @html, Maasha::XHTML::table_end;
1353
1354     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "page",     value => "dna" ) );
1355     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "user",     value => $cookie->{ 'USER' } ) );
1356     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "clade",    value => $cookie->{ 'CLADE' } ) );
1357     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "genome",   value => $cookie->{ 'GENOME' } ) );
1358     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "assembly", value => $cookie->{ 'ASSEMBLY' } ) );
1359     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "contig",   value => $cookie->{ 'CONTIG' } ) );
1360
1361     return wantarray ? @html : \@html;
1362 }
1363
1364
1365 sub section_export_pdf
1366 {
1367     # Martin A. Hansen, March 2010.
1368
1369     # Returns a HTML section with export table.
1370
1371     my ( $cookie,   # cookie hash
1372        ) = @_;
1373
1374     # Returns a list.
1375
1376     my ( @tracks, @track_list, $i, $file, @html );
1377
1378     push @tracks, [ Maasha::BGB::Track::track_ruler( $cookie ) ];
1379     push @tracks, [ Maasha::BGB::Track::track_seq( $cookie ) ];
1380
1381     @track_list = Maasha::BGB::Track::list_track_dir( $cookie->{ 'USER' }, $cookie->{ 'CLADE' }, $cookie->{ 'GENOME' }, $cookie->{ 'ASSEMBLY' }, $cookie->{ 'CONTIG' } );
1382
1383     for ( $i = 0; $i < @track_list; $i++ )
1384     {
1385         $cookie->{ 'FEAT_COLOR' } = Maasha::BGB::Draw::palette( $i );
1386
1387         push @tracks, [ Maasha::BGB::Track::track_feature( $track_list[ $i ], $cookie ) ];
1388     }
1389
1390     unshift @tracks, [ Maasha::BGB::Track::track_grid( $cookie ) ];
1391
1392     $file = "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }/BGB_export.pdf";
1393
1394     Maasha::BGB::Draw::render_pdf_file( $file, $cookie->{ 'IMG_WIDTH' }, $cookie->{ 'TRACK_OFFSET' }, \@tracks );
1395
1396     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::ln( txt => "BGB_export.pdf", href => $file ), class => 'center' );
1397
1398     return wantarray ? @html : \@html;
1399 }
1400
1401
1402 sub section_export_svg
1403 {
1404     # Martin A. Hansen, March 2010.
1405
1406     # Export view in SVG format.
1407
1408     my ( $cookie,   # cookie hash
1409        ) = @_;
1410
1411     # Returns a list.
1412
1413     my ( @tracks, @track_list, $i, $file, @html );
1414
1415     push @tracks, [ Maasha::BGB::Track::track_ruler( $cookie ) ];
1416     push @tracks, [ Maasha::BGB::Track::track_seq( $cookie ) ];
1417
1418     @track_list = Maasha::BGB::Track::list_track_dir( $cookie->{ 'USER' }, $cookie->{ 'CLADE' }, $cookie->{ 'GENOME' }, $cookie->{ 'ASSEMBLY' }, $cookie->{ 'CONTIG' } );
1419
1420     for ( $i = 0; $i < @track_list; $i++ )
1421     {
1422         $cookie->{ 'FEAT_COLOR' } = Maasha::BGB::Draw::palette( $i );
1423
1424         push @tracks, [ Maasha::BGB::Track::track_feature( $track_list[ $i ], $cookie ) ];
1425     }
1426
1427     unshift @tracks, [ Maasha::BGB::Track::track_grid( $cookie ) ];
1428
1429     $file = "$cookie->{ 'SESSION_DIR' }/$cookie->{ 'USER' }/BGB_export.svg";
1430
1431     Maasha::BGB::Draw::render_svg_file( $file, $cookie->{ 'IMG_WIDTH' }, $cookie->{ 'TRACK_OFFSET' }, \@tracks );
1432
1433     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::ln( txt => "BGB_export.svg", href => $file ), class => 'center' );
1434
1435     return wantarray ? @html : \@html;
1436 }
1437
1438
1439 sub section_search
1440 {
1441     # Martin A. Hansen, November 2009.
1442
1443     # Returns a HTML section with export table.
1444
1445     my ( $cookie,   # cookie hash
1446        ) = @_;
1447
1448     # Returns a list.
1449
1450     my ( $results, $result, $count, @html, $export, $browse );
1451
1452     $results = Maasha::BGB::Track::search_tracks( $cookie );
1453
1454     $count = scalar @{ $results };
1455
1456     push @html, Maasha::XHTML::h2( txt => "Search", class => 'center' );
1457     push @html, Maasha::XHTML::p( txt => qq(Results for "$cookie->{ 'SEARCH' }": $count), class => 'center' );
1458
1459     if ( $count > 0 )
1460     {
1461         push @html, Maasha::XHTML::table_beg( summary => "Search table", align => 'center', cellpadding => '5px' );
1462         push @html, Maasha::XHTML::table_row_simple( tr => [ qw( S_ID S_BEG S_END Q_ID SCORE STRAND HITS ALIGN BLOCK_COUNT BLOCK_BEGS BLOCK_LENS BLOCK_TYPE) ] );
1463
1464         foreach $result ( @{ $results } )
1465         {
1466             $cookie->{ 'CONTIG' }    = $result->{ 'S_ID' };
1467             $cookie->{ 'NAV_START' } = $result->{ 'S_BEG' };
1468             $cookie->{ 'NAV_END' }   = $result->{ 'S_END' };
1469             $cookie->{ 'S_BEG' }     = $result->{ 'S_BEG' };
1470             $cookie->{ 'S_END' }     = $result->{ 'S_END' };
1471             $cookie->{ 'STRAND' }    = $result->{ 'STRAND' };
1472             $cookie->{ 'Q_ID' }      = $result->{ 'Q_ID' };
1473
1474             $browse = Maasha::XHTML::ln(
1475                 txt  => "browse",
1476                 href => join( "&", "$cookie->{ 'SCRIPT' }?page=browse",
1477                                    "session_id=$cookie->{ 'SESSION_ID' }",
1478                                    "user=$cookie->{ 'USER' }",
1479                                    "clade=$cookie->{ 'CLADE' }",
1480                                    "genome=$cookie->{ 'GENOME' }",
1481                                    "assembly=$cookie->{ 'ASSEMBLY' }",
1482                                    "contig=$cookie->{ 'CONTIG' }",
1483                                    "nav_start=$cookie->{ 'NAV_START' }",
1484                                    "nav_end=$cookie->{ 'NAV_END' }",
1485                 ),
1486             );
1487
1488
1489             $export = Maasha::XHTML::ln(
1490                 txt  => "export",
1491                 href => join( "&", "$cookie->{ 'SCRIPT' }?page=export",
1492                                    "session_id=$cookie->{ 'SESSION_ID' }",
1493                                    "user=$cookie->{ 'USER' }",
1494                                    "clade=$cookie->{ 'CLADE' }",
1495                                    "genome=$cookie->{ 'GENOME' }",
1496                                    "assembly=$cookie->{ 'ASSEMBLY' }",
1497                                    "contig=$cookie->{ 'CONTIG' }",
1498                                    "s_beg=$cookie->{ 'S_BEG' }",
1499                                    "s_end=$cookie->{ 'S_END' }",
1500                                    "strand=$cookie->{ 'STRAND' }",
1501                 ),
1502             );
1503
1504             push @html, Maasha::XHTML::table_row_advanced(
1505                 tr => [ { td => $result->{ 'S_ID' } },
1506                         { td => $result->{ 'S_BEG' }, align => 'right' },
1507                         { td => $result->{ 'S_END' }, align => 'right' },
1508                         { td => $result->{ 'Q_ID' } },
1509                         { td => $result->{ 'SCORE' }, align => 'right' },
1510                         { td => $result->{ 'STRAND' } },
1511                         { td => $result->{ 'HITS' }, align => 'right' },
1512                         { td => $result->{ 'ALIGN' } },
1513                         { td => $result->{ 'BLOCK_COUNT' }, align => 'right' },
1514                         { td => $result->{ 'BLOCK_BEGS' } },
1515                         { td => $result->{ 'BLOCK_LENS' } },
1516                         { td => $result->{ 'BLOCK_TYPE' } },
1517                         { td => $browse },
1518                         { td => $export },
1519                 ], class => "monospace"
1520             );
1521         }
1522
1523         push @html, Maasha::XHTML::table_end;
1524     }
1525
1526     return wantarray ? @html : \@html;
1527 }
1528
1529
1530 sub section_dna
1531 {
1532     # Martin A. Hansen, November 2009.
1533
1534     # Returns a HTML section with extracted DNA.
1535
1536     my ( $cookie,   # cookie hash
1537        ) = @_;
1538
1539     # Returns a list.
1540
1541     my ( @html, $beg, $end, $seq );
1542     
1543     $beg = $cookie->{ 'S_BEG' };
1544     $end = $cookie->{ 'S_END' };
1545     $beg =~ tr/,//d;
1546     $end =~ tr/,//d;
1547
1548     $seq = join ";", ">$cookie->{ 'GENOME' }", $cookie->{ 'ASSEMBLY' }, $cookie->{ 'CONTIG' }, $beg, $end, "$cookie->{ 'STRAND' }\n";
1549     $seq .= Maasha::BGB::Track::dna_get( $cookie );
1550
1551     push @html, Maasha::XHTML::h2( txt => "DNA", class => 'center' );
1552     push @html, Maasha::XHTML::pre( txt => $seq );
1553
1554     return wantarray ? @html : \@html;
1555 }
1556
1557
1558 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1559
1560
1561 __END__