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