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