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