]> git.donarmstrong.com Git - biopieces.git/blob - www/index.cgi
dedd080b544892833b035fe03a48f25e220e8401
[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
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 );
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     cookie_session( $cookie );
116     cookie_login( $cookie );
117
118     $cookie->{ 'PAGE' }          ||= $cgi->param( 'page' )     || 'login';
119
120     if ( $cookie->{ 'LOGIN' } )
121     {
122         if ( $cgi->param( 'genome' ) ) {
123             $cookie->{ 'PAGE' } = 'browse';
124         } else {
125             $cookie->{ 'PAGE' } = 'clade';
126         }
127     }
128
129     $cookie->{ 'CLADE' }           = $cgi->param( 'clade' )    || '';
130     $cookie->{ 'GENOME' }          = $cgi->param( 'genome' )   || '';
131     $cookie->{ 'ASSEMBLY' }        = $cgi->param( 'assembly' ) || '';
132     $cookie->{ 'CONTIG' }          = $cgi->param( 'contig' )   || '';
133     $cookie->{ 'Q_ID' }            = $cgi->param( 'q_id' );
134     $cookie->{ 'NAV_START' }     ||= $cgi->param( 'nav_start' );
135     $cookie->{ 'NAV_END' }       ||= $cgi->param( 'nav_end' );
136     $cookie->{ 'NAV_CENTER' }      = $cgi->param( 'nav_center' );
137     $cookie->{ 'S_BEG' }           = $cgi->param( 's_beg' );
138     $cookie->{ 'S_END' }           = $cgi->param( 's_end' );
139     $cookie->{ 'STRAND' }          = $cgi->param( 'strand' );
140     $cookie->{ 'ZOOM_IN1' }        = $cgi->param( 'zoom_in1' );
141     $cookie->{ 'ZOOM_IN2' }        = $cgi->param( 'zoom_in2' );
142     $cookie->{ 'ZOOM_IN3' }        = $cgi->param( 'zoom_in3' );
143     $cookie->{ 'ZOOM_OUT1' }       = $cgi->param( 'zoom_out1' );
144     $cookie->{ 'ZOOM_OUT2' }       = $cgi->param( 'zoom_out2' );
145     $cookie->{ 'ZOOM_OUT3' }       = $cgi->param( 'zoom_out3' );
146     $cookie->{ 'MOVE_LEFT1' }      = $cgi->param( 'move_left1' );
147     $cookie->{ 'MOVE_LEFT2' }      = $cgi->param( 'move_left2' );
148     $cookie->{ 'MOVE_LEFT3' }      = $cgi->param( 'move_left3' );
149     $cookie->{ 'MOVE_RIGHT1' }     = $cgi->param( 'move_right1' );
150     $cookie->{ 'MOVE_RIGHT2' }     = $cgi->param( 'move_right2' );
151     $cookie->{ 'MOVE_RIGHT3' }     = $cgi->param( 'move_right3' );
152
153     $cookie->{ 'IMG_WIDTH' }       = 1200;   # Width of browser image in pixels
154     $cookie->{ 'IMG_HEIGHT' }      = 800;    # Height of browser image in pixels
155     $cookie->{ 'WIGGLE_HEIGHT' }   = 75;     # Height of Wiggle tracks in pixels
156     $cookie->{ 'TRACK_OFFSET' }    = 20;
157     $cookie->{ 'TRACK_SPACE' }     = 20;
158     $cookie->{ 'RULER_FONT_SIZE' } = 10;     # Size of ruler font in pixels
159     $cookie->{ 'RULER_COLOR' }     = [ 0, 0, 0 ];
160     $cookie->{ 'SEQ_FONT_SIZE' }   = 10;
161     $cookie->{ 'SEQ_COLOR' }       = [ 0, 0, 0, ];
162     $cookie->{ 'FEAT_WIDTH' }      = 5;
163     $cookie->{ 'FEAT_COLOR' }      = [ 0, 0, 0 ];
164     $cookie->{ 'FEAT_MAX' }        = 5000;   # TODO: Reduntant?
165
166     $cookie->{ 'LIST_USER' }       = Maasha::BGB::Track::list_users();
167
168     if ( $cookie->{ 'USER' } and not $cookie->{ 'LOGIN_ERROR' } )
169     {
170         $cookie->{ 'LIST_CLADE' }      = Maasha::BGB::Track::list_clades(     $cookie->{ 'USER' } );
171         $cookie->{ 'LIST_GENOME' }     = Maasha::BGB::Track::list_genomes(    $cookie->{ 'USER' }, $cookie->{ 'CLADE' } );
172         $cookie->{ 'LIST_ASSEMBLY' }   = Maasha::BGB::Track::list_assemblies( $cookie->{ 'USER' }, $cookie->{ 'CLADE' }, $cookie->{ 'GENOME' } );
173         $cookie->{ 'LIST_CONTIG' }     = Maasha::BGB::Track::list_contigs(    $cookie->{ 'USER' }, $cookie->{ 'CLADE' }, $cookie->{ 'GENOME' }, $cookie->{ 'ASSEMBLY' } );
174
175         if ( $cookie->{ 'CONTIG' } )
176         {
177             cookie_start( $cookie );
178             cookie_end( $cookie );
179             cookie_zoom( $cookie );
180             cookie_move( $cookie );
181             cookie_center( $cookie );
182         }
183     }
184
185     $cookie->{ 'STRAND' } = '+' if defined $cookie->{ 'STRAND' } and $cookie->{ 'STRAND' } eq ' ';   # FIXME ugly HTML fix
186
187     return wantarray ? %{ $cookie } : $cookie;
188 }
189
190
191 sub cookie_session
192 {
193     # Martin A. Hansen, December 2009.
194
195     # Check cookie information against session information.
196
197     my ( $cookie,   # cookie hash
198        ) = @_;
199
200     # Returns nothing.
201
202     my ( $session );
203
204     $session = Maasha::BGB::Session::session_restore( "$cookie->{ 'SESSION_DIR' }/sessions.json" );
205
206     # if ( $cookie->{ 'SESSION_ID' } )
207     # {
208     #     if ( $cookie->{ 'SESSION_ID' } ne $ENV{ 'SSL_SESSION_ID' } ) {
209     #         $cookie->{ 'PAGE' } = 'login';
210     #         print STDERR "HER1\n"; # DEBUG
211     #     }
212     # }
213     # else
214     # {
215     #     $cookie->{ 'PAGE' } = 'login';
216     # 
217     #     print STDERR "HER2\n"; # DEBUG
218     # }
219
220     if ( exists $session->{ $cookie->{ 'USER' } } and $cookie->{ 'SESSION_ID' } )
221     {
222         if ( $session->{ $cookie->{ 'USER' } }->{ 'SESSION_ID' } ne $cookie->{ 'SESSION_ID' } ) {
223             $cookie->{ 'PAGE' } = 'login';
224             print STDERR "HER3\n"; # DEBUG
225         }
226     }
227     else
228     {
229         $cookie->{ 'PAGE' } = 'login';
230         print STDERR "HER4\n";                                                                 # DEBUG
231         print STDERR "NO COOKIE SESSION ID\n" if not $cookie->{ 'SESSION_ID' };                # DEBUG
232         print STDERR "NO COOKIE USER\n"       if not $cookie->{ 'USER' };                      # DEBUG
233         print STDERR "NO SESSION USER\n"      if not exists $session->{ $cookie->{ 'USER' } }; # DEBUG
234     }
235 }
236
237
238 sub cookie_login
239 {
240     # Martin A. Hansen, December 2009.
241
242     # Check a user and password from CGI against the password file and
243     # set the session ID, if found, in the cookie.
244
245     my ( $cookie,   # cookie hash
246        ) = @_;
247
248     # Returns nothing.
249
250     my ( $session );
251
252     if ( defined $cookie->{ 'PASSWORD' } )
253     {
254         $session = Maasha::BGB::Session::session_restore( "$cookie->{ 'SESSION_DIR' }/sessions.json" );
255     
256         if ( exists $session->{ $cookie->{ 'USER' } } and
257              $session->{ $cookie->{ 'USER' } }->{ 'PASSWORD' } eq Digest::MD5::md5_hex( $cookie->{ 'PASSWORD' } ) )
258         {
259             $session->{ $cookie->{ 'USER' } }->{ 'SESSION_ID' } ||= Maasha::BGB::Session::session_new();
260             # $session->{ $cookie->{ 'USER' } }->{ 'SESSION_ID' }   = $ENV{ 'SSL_SESSION_ID' };
261             $session->{ $cookie->{ 'USER' } }->{ 'TIME' }         = Maasha::Common::time_stamp();
262
263             $cookie->{ 'SESSION_ID' } = $session->{ $cookie->{ 'USER' } }->{ 'SESSION_ID' };
264             $cookie->{ 'LOGIN' }      = 1;
265
266             Maasha::BGB::Session::session_store( "$cookie->{ 'SESSION_DIR' }/sessions.json", $session );
267         }
268         else
269         {
270             $cookie->{ 'LOGIN_ERROR' } = 1;
271         }
272     }
273 }
274
275
276 sub cookie_start
277 {
278     # Martin A. Hansen, November 2009.
279
280     # Decommify the cookie value for NAV_START and adjust it to
281     # to prevent negative values.
282
283     my ( $cookie,   # cookie hash
284        ) = @_;
285
286     # Returns nothing.
287
288     if ( defined $cookie->{ 'NAV_START' } )
289     {
290         $cookie->{ 'NAV_START' } =~ tr/,//d;
291         $cookie->{ 'NAV_START' } = 0 if $cookie->{ 'NAV_START' } < 0;
292     }
293     else
294     {
295         $cookie->{ 'NAV_START' } = 0;
296     }
297 }
298
299
300 sub cookie_end
301 {
302     # Martin A. Hansen, November 2009.
303
304     # Decommify the cookie value for NAV_END and adjust it to prevent
305     # overshooting the max value for the contig size as determined
306     # from the cookie.
307
308     my ( $cookie,   # cookie hash
309        ) = @_;
310
311     # Returns nothing.
312
313     my ( $max );
314     
315     $max = Maasha::Filesys::file_size( Maasha::BGB::Track::path_seq( $cookie ) );
316
317     if ( defined $cookie->{ 'NAV_END' } )
318     {
319         $cookie->{ 'NAV_END' } =~ tr/,//d;
320         $cookie->{ 'NAV_END' } = $max if $cookie->{ 'NAV_END' } > $max;
321     }
322     else
323     {
324         $cookie->{ 'NAV_END' } = $max;
325     }
326 }
327
328
329 sub cookie_zoom
330 {
331     # Martin A. Hansen, November 2009.
332
333     # Adjust the cookie values for NAV_START and NAV_END based
334     # on cookie ZOOM values.
335
336     my ( $cookie,   # cookie hash
337        ) = @_;
338
339     # Returns nothing.
340
341     my ( $max, $dist, $new_dist, $dist_diff );
342
343     $max = Maasha::Filesys::file_size( Maasha::BGB::Track::path_seq( $cookie ) );
344
345     $dist = $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' };
346
347     if ( defined $cookie->{ 'ZOOM_IN1' } ) {
348         $new_dist = $dist / 1.5;
349     } elsif ( defined $cookie->{ 'ZOOM_IN2' } ) {
350         $new_dist = $dist / 3;
351     } elsif ( defined $cookie->{ 'ZOOM_IN3' } ) {
352         $new_dist = $dist / 10;
353     } elsif ( defined $cookie->{ 'ZOOM_OUT1' } ) {
354         $new_dist = $dist * 1.5;
355     } elsif ( defined $cookie->{ 'ZOOM_OUT2' } ) {
356         $new_dist = $dist * 3;
357     } elsif ( defined $cookie->{ 'ZOOM_OUT3' } ) {
358         $new_dist = $dist * 10;
359     }
360
361     if ( $new_dist )
362     {
363         $dist_diff = $dist - $new_dist;
364
365         $cookie->{ 'NAV_START' } = int( $cookie->{ 'NAV_START' } + ( $dist_diff / 2 ) );
366         $cookie->{ 'NAV_END' }   = int( $cookie->{ 'NAV_END' }   - ( $dist_diff / 2 ) );
367
368         $cookie->{ 'NAV_START' } = 0    if $cookie->{ 'NAV_START' } < 0;
369         $cookie->{ 'NAV_END' }   = $max if $cookie->{ 'NAV_END' } > $max;
370     }
371 }
372
373
374 sub cookie_move
375 {
376     # Martin A. Hansen, November 2009.
377
378     # Adjust the cookie values for NAV_START and NAV_END based
379     # on cookie MOVE values.
380
381     my ( $cookie,   # cookie hash
382        ) = @_;
383
384     # Returns nothing.
385     
386     my ( $max, $dist, $shift, $new_start, $new_end );
387
388     $max = Maasha::Filesys::file_size( Maasha::BGB::Track::path_seq( $cookie ) );
389
390     $dist = $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' };
391
392     if ( defined $cookie->{ 'MOVE_LEFT1' } ) {
393         $shift = -1 * $dist * 0.10;
394     } elsif ( defined $cookie->{ 'MOVE_LEFT2' } ) {
395         $shift = -1 * $dist * 0.475;
396     } elsif ( defined $cookie->{ 'MOVE_LEFT3' } ) {
397         $shift = -1 * $dist * 0.95;
398     } elsif ( defined $cookie->{ 'MOVE_RIGHT1' } ) {
399         $shift = $dist * 0.10;
400     } elsif ( defined $cookie->{ 'MOVE_RIGHT2' } ) {
401         $shift = $dist * 0.475;
402     } elsif ( defined $cookie->{ 'MOVE_RIGHT3' } ) {
403         $shift = $dist * 0.95;
404     }
405
406     if ( $shift )
407     {
408         $new_start = int( $cookie->{ 'NAV_START' } + $shift );
409         $new_end   = int( $cookie->{ 'NAV_END' }   + $shift );
410
411         if ( $new_start > 0 and $new_end < $max )
412         {
413             $cookie->{ 'NAV_START' } = $new_start;
414             $cookie->{ 'NAV_END' }   = $new_end;
415         }
416     }
417 }
418
419
420 sub cookie_center
421 {
422     # Martin A. Hansen, March 2010
423    
424     # Adjust the cookie values for NAV_START and NAV_END based
425     # on cookie CENTER value if defined.
426
427     my ( $cookie,   # cookie hash
428        ) = @_;
429
430     # Returns nothing.
431
432     my ( $l_dist, $r_dist );
433
434     if ( defined $cookie->{ 'NAV_CENTER' } )
435     {
436         $l_dist = $cookie->{ 'NAV_CENTER' } - $cookie->{ 'NAV_START' };
437         $r_dist = $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_CENTER' };
438
439         if ( $l_dist > $r_dist ) {
440             $cookie->{ 'NAV_START' } = $cookie->{ 'NAV_END' } - 2 * $r_dist;
441         } else {
442             $cookie->{ 'NAV_END' } = $cookie->{ 'NAV_START' } + 2 * $l_dist;
443         }
444     }
445 }
446
447
448 sub cookie_page_next
449 {
450     # Martin A. Hansen, November 2009.
451     
452     # Returns the next page in the taxonomy path.
453
454     my ( $cookie,   # cookie hash
455        ) = @_;
456
457     # Returns a string.
458
459     my ( $i );
460
461     for ( $i = 0; $i < @{ $cookie->{ 'LIST_PAGES' } }; $i++ ) {
462         last if $cookie->{ 'PAGE' } eq $cookie->{ 'LIST_PAGES' }->[ $i ];
463     }
464
465     return $cookie->{ 'LIST_PAGES' }->[ $i + 1 ];
466 }
467
468
469 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PAGES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
470
471
472 sub page
473 {
474     # Martin A. Hansen, November 2009.
475
476     # Determines what page to render based on
477     # the cookie's PAGE setting.
478
479     my ( $cookie,   # cookie hash
480        ) = @_;
481
482     # Returns a list.
483
484     my ( @html );
485
486     if ( $cookie->{ 'SESSION_ID' } ) {
487         push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "session_id", value => $cookie->{ 'SESSION_ID' } ) );
488     }
489
490     if ( $cookie->{ 'PAGE' } eq 'login' ) {
491         push @html, page_login( $cookie );
492     } elsif ( $cookie->{ 'PAGE' } eq 'search' ) {
493         push @html, page_search( $cookie );
494     } elsif ( $cookie->{ 'PAGE' } eq 'browse' ) {
495         push @html, page_browse( $cookie );
496     } elsif ( $cookie->{ 'PAGE' } eq 'dna' ) {
497         push @html, page_dna( $cookie );
498     } elsif ( $cookie->{ 'PAGE' } eq 'export' ) {
499         push @html, page_export( $cookie );
500     } else {
501         push @html, page_taxonomy( $cookie );
502     }
503
504     return wantarray ? @html : \@html;
505 }
506
507
508 sub page_login
509 {
510     # Martin A. Hansen, December 2009.
511
512     # Renders the login page.
513
514     my ( $cookie,
515        ) = @_;
516
517     # Returns a list.
518
519     my ( @html );
520
521     push @html, section_login( $cookie );
522
523     return wantarray ? @html : \@html;
524 }
525
526
527 sub page_search
528 {
529     # Martin A. Hansen, December 2009.
530
531     # Renders the search page.
532
533     my ( $cookie,
534        ) = @_;
535
536     # Returns a list.
537
538     my ( @html );
539
540     push @html, section_taxonomy_table( $cookie );
541     push @html, section_search( $cookie );
542
543     return wantarray ? @html : \@html;
544 }
545
546
547 sub page_browse
548 {
549     # Martin A. Hansen, November 2009.
550     
551     # Renders the browse page.
552
553     my ( $cookie,
554        ) = @_;
555
556     # Returns a list.
557
558     my ( @html );
559
560     push @html, section_taxonomy_table( $cookie );
561     push @html, section_navigate( $cookie );
562     push @html, section_browse( $cookie );
563     push @html, section_linkout( $cookie );
564
565     return wantarray ? @html : \@html;
566 }
567
568
569 sub page_dna
570 {
571     # Martin A. Hansen, November 2009.
572     
573     # Renders the DNA page.
574
575     my ( $cookie,
576        ) = @_;
577
578     # Returns a list.
579
580     my ( @html );
581
582     push @html, section_taxonomy_table( $cookie );
583     push @html, section_dna( $cookie );
584
585     return wantarray ? @html : \@html;
586 }
587
588
589 sub page_export
590 {
591     # Martin A. Hansen, November 2009.
592
593     # Renders the export page.
594
595     my ( $cookie,
596        ) = @_;
597
598     # Returns a list.
599
600     my ( @html );
601
602     push @html, section_taxonomy_table( $cookie );
603     push @html, section_export( $cookie );
604
605     return wantarray ? @html : \@html;
606 }
607
608
609 sub page_taxonomy
610 {
611     # Martin A. Hansen, November 2009.
612     
613     # Renders the browse page.
614
615     my ( $cookie,   # cookie hash
616        ) = @_;
617
618     # Returns a list.
619
620     my ( @html );
621
622     push @html, section_taxonomy_table( $cookie );
623     push @html, section_taxonomy_select( $cookie );
624
625     return wantarray ? @html : \@html;
626 }
627
628
629 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SECTIONS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
630
631
632 sub section_login
633 {
634     # Martin A. Hansen, December 2009.
635    
636     # Returns a HTML section with a login menu.
637
638     my ( $cookie,   # cookie hash
639        ) = @_;
640
641     # Returns a list.
642
643     my ( $user, $password, $login, @html );
644
645     $user     = Maasha::XHTML::text( name => "user", value => "", size => 20 );
646     $password = Maasha::XHTML::password( name => "password", value => "", size => 20 );
647     $login    = Maasha::XHTML::submit( name => "login_submit", value => "Login" );
648
649     push @html, Maasha::XHTML::h2( txt => "Login", class => 'center' );
650
651     push @html, Maasha::XHTML::table_beg( summary => "Login table", align => 'center' );
652     push @html, Maasha::XHTML::table_row_simple( tr => [ "User:", $user ] );
653     push @html, Maasha::XHTML::table_row_simple( tr => [ "Password:", $password ] );
654     push @html, Maasha::XHTML::table_row_simple( tr => [ "", $login ] );
655     push @html, Maasha::XHTML::table_end;
656
657     if ( defined $cookie->{ 'CLADE' }     and
658          defined $cookie->{ 'GENOME' }    and
659          defined $cookie->{ 'ASSEMBLY' }  and
660          defined $cookie->{ 'CONTIG' }    and
661          defined $cookie->{ 'NAV_START' } and
662          defined $cookie->{ 'NAV_END' }
663     )
664     {
665         push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "clade",     value => $cookie->{ 'CLADE' } ) );
666         push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "genome",    value => $cookie->{ 'GENOME' } ) );
667         push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "assembly",  value => $cookie->{ 'ASSEMBLY' } ) );
668         push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "contig",    value => $cookie->{ 'CONTIG' } ) );
669         push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "nav_start", value => $cookie->{ 'NAV_START' } ) );
670         push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "nav_end",   value => $cookie->{ 'NAV_END' } ) );
671     }
672
673     if ( $cookie->{ 'LOGIN_ERROR' } ) {
674         push @html, Maasha::XHTML::h3( txt => "Bad user or password - please retry", class => 'error' );
675     }
676
677     return wantarray ? @html : \@html;
678 }
679
680
681 sub section_taxonomy_table
682 {
683     # Martin A. Hansen, November 2009.
684
685     # Returns a HTML section with a taxonomy table
686     # showing the location in the taxonomy and with
687     # links to browse the taxonomy.
688
689     my ( $cookie,   # cookie hash
690        ) = @_;
691
692     # Returns a list.
693
694     my ( $page, @row, @html, $href, $txt );
695     
696     foreach $page ( @{ $cookie->{ 'LIST_PAGES' } } )
697     {
698         last if $page eq $cookie->{ 'PAGE' } or $page eq 'browse';
699
700         $href  = "$cookie->{ 'SCRIPT' }?page=$page";
701         $href .= "&session_id=$cookie->{ 'SESSION_ID' }";
702         $href .= "&user=$cookie->{ 'USER' }";
703         $href .= "&clade=$cookie->{ 'CLADE' }"       if $page !~ /clade/;
704         $href .= "&genome=$cookie->{ 'GENOME' }"     if $page !~ /clade|genome/;
705         $href .= "&assembly=$cookie->{ 'ASSEMBLY' }" if $page !~ /clade|genome|assembly/;
706         $href .= "&contig=$cookie->{ 'CONTIG' }"     if $page !~ /clade|genome|assembly|contig/;
707
708         $txt = ": $cookie->{ uc $page }";
709
710         push @row, Maasha::XHTML::ln( txt => $page, href => $href, class => 'inline' );
711         push @row, Maasha::XHTML::p(  txt => $txt, class => 'inline' );
712     }
713
714     push @html, Maasha::XHTML::table_beg( summary => "Taxonomy table", align => 'center', cellpadding => '5px' );
715     push @html, Maasha::XHTML::table_row_simple( tr => [ join( "\n", @row ) ], align => 'center' );
716     push @html, Maasha::XHTML::table_end;
717
718     return wantarray ? @html : \@html;
719 }
720
721
722 sub section_taxonomy_select
723 {
724     # Martin A. Hansen, November 2009.
725     
726     # Returns a HTML section with selection choices
727     # for navigating the taxonomy tree.
728
729     my ( $cookie,   # cookie hash
730        ) = @_;
731
732     # Returns a list.
733
734     my ( $list, @html, $item, $href );
735
736     $list = "LIST_" . uc $cookie->{ 'PAGE' };
737
738     push @html, Maasha::XHTML::h2( txt => "Select $cookie->{ 'PAGE' }", class => 'center' );
739
740     push @html, Maasha::XHTML::table_beg( summary => "Taxonomy select table", align => 'center', cellpadding => '5px' );
741
742     foreach $item ( @{ $cookie->{ $list } } )
743     {
744         $cookie->{ uc $cookie->{ 'PAGE' } } = $item;
745
746         $href  = "$cookie->{ 'SCRIPT' }?page=" . cookie_page_next( $cookie );
747         $href .= "&session_id=$cookie->{ 'SESSION_ID' }";
748         $href .= "&user=$cookie->{ 'USER' }";
749         $href .= "&clade=$cookie->{ 'CLADE' }";
750         $href .= "&genome=$cookie->{ 'GENOME' }"     if $cookie->{ 'GENOME' };
751         $href .= "&assembly=$cookie->{ 'ASSEMBLY' }" if $cookie->{ 'ASSEMBLY' };
752         $href .= "&contig=$cookie->{ 'CONTIG' }"     if $cookie->{ 'CONTIG' };
753
754         push @html, Maasha::XHTML::table_row_simple( tr => [ Maasha::XHTML::ln( txt => $item, href => $href ) ] );
755     }
756
757     push @html, Maasha::XHTML::table_end;
758
759     return wantarray ? @html : \@html;
760 }
761
762
763 sub section_navigate
764 {
765     # Martin A. Hansen, November 2009.
766
767     # Returns a HTML section for navigating in the browser window.
768
769     my ( $cookie,   # cookie hash
770        ) = @_;
771
772     # Returns a list.
773
774     my ( @html, $nav_val );
775
776     $nav_val = Maasha::Calc::commify( $cookie->{ 'NAV_START' } ) . "-" . Maasha::Calc::commify( $cookie->{ 'NAV_END' } );
777
778     push @html, Maasha::XHTML::table_beg( summary => "Navigation table", align => 'center' );
779     push @html, Maasha::XHTML::table_row_simple( tr => [
780         "Position or search term:",
781         Maasha::XHTML::text( name => "nav_search", value => $nav_val, size => 30 ),
782         Maasha::XHTML::submit( name => "nav_submit", value => "Submit" ),
783     ] );
784     push @html, Maasha::XHTML::table_end;
785
786     push @html, Maasha::XHTML::table_beg( summary => "Zoom table", align => 'center' );
787     push @html, Maasha::XHTML::table_row_simple( tr => [
788         Maasha::XHTML::p( txt => 'Move:' ),
789         Maasha::XHTML::submit( name => "move_left3",  value => "<<<", title => "move 95% to the left" ),
790         Maasha::XHTML::submit( name => "move_left2",  value => "<<",  title => "move 47.5% to the left" ),
791         Maasha::XHTML::submit( name => "move_left1",  value => "<",   title => "move 10% to the left" ),
792         Maasha::XHTML::submit( name => "move_right1", value => ">",   title => "move 10% to the rigth" ),
793         Maasha::XHTML::submit( name => "move_right2", value => ">>",  title => "move 47.5% to the rigth" ),
794         Maasha::XHTML::submit( name => "move_right3", value => ">>>", title => "move 95% to the right" ),
795         Maasha::XHTML::p( txt => 'Zoom in:' ),
796         Maasha::XHTML::submit( name => "zoom_in1", value => "1.5x" ),
797         Maasha::XHTML::submit( name => "zoom_in2", value => "3x" ),
798         Maasha::XHTML::submit( name => "zoom_in3", value => "10x" ),
799         Maasha::XHTML::p( txt => 'Zoom out:' ),
800         Maasha::XHTML::submit( name => "zoom_out1", value => "1.5x" ),
801         Maasha::XHTML::submit( name => "zoom_out2", value => "3x" ),
802         Maasha::XHTML::submit( name => "zoom_out3", value => "10x" ),
803     ] );
804     push @html, Maasha::XHTML::table_end;
805
806     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "page",     value => "browse" ) );
807     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "user",     value => $cookie->{ 'USER' } ) );
808     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "clade",    value => $cookie->{ 'CLADE' } ) );
809     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "genome",   value => $cookie->{ 'GENOME' } ) );
810     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "assembly", value => $cookie->{ 'ASSEMBLY' } ) );
811     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "contig",   value => $cookie->{ 'CONTIG' } ) );
812
813     return wantarray ? @html : \@html;
814 }
815
816
817 sub section_linkout
818 {
819     # Martin A. Hansen, March 2010.
820
821     # Returns a HTML section for a static link based on information in the cookie.
822
823     my ( $cookie,   # cookie hash
824        ) = @_;
825
826     # Returns a list.
827
828     my ( $link, @html );
829
830     $link = Maasha::XHTML::ln(
831         txt  => 'link',
832         href => join( "&", "$cookie->{ 'SCRIPT' }?page=browse",
833                            "clade=$cookie->{ 'CLADE' }",
834                            "genome=$cookie->{ 'GENOME' }",
835                            "assembly=$cookie->{ 'ASSEMBLY' }",
836                            "contig=$cookie->{ 'CONTIG' }",
837                            "nav_start=$cookie->{ 'NAV_START' }",
838                            "nav_end=$cookie->{ 'NAV_END' }",
839               ),
840         title  => "Static link to this view",
841     );
842
843     push @html, Maasha::XHTML::p( txt => $link, class => 'center' );
844
845     return wantarray ? @html : \@html;
846 }
847
848
849 sub section_browse
850 {
851     my ( $cookie,
852        ) = @_;
853
854     # Returns a list.
855
856     my ( @tracks, $i, @features, $feat, $elem, $surface, $cr, $png_data, @html, @img, $x1, $y1, $x2, $y2, $factor, $center );
857
858     push @features, [ Maasha::BGB::Track::track_ruler( $cookie ) ];
859     push @features, [ Maasha::BGB::Track::track_seq( $cookie ) ];
860
861     @tracks = Maasha::BGB::Track::path_tracks( $cookie );
862
863     for ( $i = 0; $i < @tracks; $i++ )
864     {
865         $cookie->{ 'FEAT_COLOR' } = Maasha::BGB::Draw::palette( $i );
866
867         push @features, [ Maasha::BGB::Track::track_feature( $tracks[ $i ], $cookie ) ];
868     }
869
870     $surface = Cairo::ImageSurface->create( 'argb32', $cookie->{ 'IMG_WIDTH' }, $cookie->{ 'TRACK_OFFSET' } );
871     $cr      = Cairo::Context->create( $surface );
872
873     $cr->rectangle ( 0, 0, $cookie->{ 'IMG_WIDTH' },  $cookie->{ 'TRACK_OFFSET' } );
874     $cr->set_source_rgb ( 1, 1, 1 );
875     $cr->fill;
876
877     foreach $feat ( @features ) {
878         Maasha::BGB::Draw::draw_feature( $cr, $feat ) if $feat;
879     }
880
881     $png_data = Maasha::BGB::Draw::base64_png( $surface );
882
883     push @img, Maasha::XHTML::img(
884         src    => "data:image/png;base64,$png_data",
885         alt    => "Browser Tracks",
886         height => $cookie->{ 'TRACK_OFFSET' },
887         width  => $cookie->{ 'IMG_WIDTH' },
888         id     => "browser_map",
889         usemap => "#browser_map"
890     );
891
892     push @img, Maasha::XHTML::map_beg( name => "browser_map", id => "browser_map" );
893
894     $factor = ( $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' } + 1 ) / $cookie->{ 'IMG_WIDTH' };
895
896     for ( $i = 0; $i < $cookie->{ 'IMG_WIDTH' }; $i += 5 )
897     {
898         $x1 = $i;
899         $y1 = 10;
900         $x2 = $i + 5;
901         $y2 = 20;
902
903         $center = int( $cookie->{ 'NAV_START' } + $factor * $i );
904
905         push @img, Maasha::XHTML::area(
906             href     => join( "&", "$cookie->{ 'SCRIPT' }?page=browse",
907                                    "session_id=$cookie->{ 'SESSION_ID' }",
908                                    "user=$cookie->{ 'USER' }",
909                                    "clade=$cookie->{ 'CLADE' }",
910                                    "genome=$cookie->{ 'GENOME' }",
911                                    "assembly=$cookie->{ 'ASSEMBLY' }",
912                                    "contig=$cookie->{ 'CONTIG' }",
913                                    "nav_start=$cookie->{ 'NAV_START' }",
914                                    "nav_end=$cookie->{ 'NAV_END' }",
915                                    "nav_center=$center",
916                       ),
917             shape  => 'rect',
918             coords => "$x1, $y1, $x2, $y2",
919             title  => "Center on " . Maasha::Calc::commify( $center ),
920         );
921     }
922
923     foreach $feat ( @features )
924     {
925         foreach $elem ( @{ $feat } )
926         {
927             next if $elem->{ 'type' } eq 'text';
928             next if $elem->{ 'type' } eq 'wiggle';
929
930             #$elem->{ 'strand' } = '&#43' if $elem->{ 'strand' } eq '+';
931
932             $cookie->{ 'Q_ID' }   = $elem->{ 'q_id' };
933             $cookie->{ 'S_BEG' }  = $elem->{ 's_beg' };
934             $cookie->{ 'S_END' }  = $elem->{ 's_end' };
935             $cookie->{ 'STRAND' } = $elem->{ 'strand' };
936
937             push @img, Maasha::XHTML::area(
938                 href     => join( "&", "$cookie->{ 'SCRIPT' }?page=export",
939                                        "session_id=$cookie->{ 'SESSION_ID' }",
940                                        "user=$cookie->{ 'USER' }",
941                                        "clade=$cookie->{ 'CLADE' }",
942                                        "genome=$cookie->{ 'GENOME' }",
943                                        "assembly=$cookie->{ 'ASSEMBLY' }",
944                                        "contig=$cookie->{ 'CONTIG' }",
945                                        "s_beg=$cookie->{ 'S_BEG' }",
946                                        "s_end=$cookie->{ 'S_END' }",
947                                        "strand=$cookie->{ 'STRAND' }",
948                           ),
949                 shape  => 'rect',
950                 coords => "$elem->{ x1 }, $elem->{ y1 }, $elem->{ x2 }, $elem->{ y2 }",
951                 title  => "$elem->{ 'title' }",
952             );
953         }
954     }
955
956     push @img, Maasha::XHTML::map_end();
957
958     push @html, Maasha::XHTML::p( txt => join( "\n", @img ) );
959
960     @html = Maasha::XHTML::div( txt => join( "\n", @html ), class => 'browse' );
961
962     return wantarray ? @html : \@html;
963 }
964
965
966 sub section_export
967 {
968     # Martin A. Hansen, November 2009.
969
970     # Returns a HTML section with export table.
971
972     my ( $cookie,   # cookie hash
973        ) = @_;
974
975     # Returns a list.
976
977     my ( $feat, @row_dna_contig, @html );
978
979     $feat = $cookie->{ 'Q_ID' } || "[no name]";
980
981     push @row_dna_contig, Maasha::XHTML::p( txt => qq(Export Feature: "$feat" DNA from Contig: ), class => 'inline' );
982     push @row_dna_contig, Maasha::XHTML::p( txt  => "$cookie->{ 'CONTIG' } Start: ", class => 'inline' );
983     push @row_dna_contig, Maasha::XHTML::text( name => "s_beg", value => Maasha::Calc::commify( $cookie->{ 'S_BEG' } ), size => 15 );
984     push @row_dna_contig, Maasha::XHTML::p( txt => "End: ", class => 'inline' );
985     push @row_dna_contig, Maasha::XHTML::text( name => "s_end", value => Maasha::Calc::commify( $cookie->{ 'S_END' } ), size => 15 );
986     push @row_dna_contig, Maasha::XHTML::p( txt => "Strand: ", class => 'inline' );
987     push @row_dna_contig, Maasha::XHTML::menu( name => "strand", options => [ qw( + - ) ], selected => $cookie->{ 'STRAND' } );
988     push @row_dna_contig, Maasha::XHTML::submit( name => "export", value => "Export", title => "Fetch DNA from this region" );
989
990     push @html, Maasha::XHTML::h2( txt => "Export", class => 'center' );
991     push @html, Maasha::XHTML::table_beg( summary => "Taxonomy select table", align => 'center' );
992     push @html, Maasha::XHTML::table_row_simple( tr => \@row_dna_contig );
993     push @html, Maasha::XHTML::table_end;
994
995     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "page",     value => "dna" ) );
996     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "user",     value => $cookie->{ 'USER' } ) );
997     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "clade",    value => $cookie->{ 'CLADE' } ) );
998     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "genome",   value => $cookie->{ 'GENOME' } ) );
999     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "assembly", value => $cookie->{ 'ASSEMBLY' } ) );
1000     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "contig",   value => $cookie->{ 'CONTIG' } ) );
1001
1002     return wantarray ? @html : \@html;
1003 }
1004
1005
1006 sub section_search
1007 {
1008     # Martin A. Hansen, November 2009.
1009
1010     # Returns a HTML section with export table.
1011
1012     my ( $cookie,   # cookie hash
1013        ) = @_;
1014
1015     # Returns a list.
1016
1017     my ( $results, $result, $count, @html, $export, $browse );
1018
1019     $results = Maasha::BGB::Track::search_tracks( $cookie );
1020
1021     $count = scalar @{ $results };
1022
1023     push @html, Maasha::XHTML::h2( txt => "Search", class => 'center' );
1024     push @html, Maasha::XHTML::p( txt => qq(Results for "$cookie->{ 'SEARCH' }": $count), class => 'center' );
1025
1026     if ( $count > 0 )
1027     {
1028         push @html, Maasha::XHTML::table_beg( summary => "Search table", align => 'center', cellpadding => '5px' );
1029         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) ] );
1030
1031         foreach $result ( @{ $results } )
1032         {
1033             $cookie->{ 'CONTIG' }    = $result->{ 'S_ID' };
1034             $cookie->{ 'NAV_START' } = $result->{ 'S_BEG' };
1035             $cookie->{ 'NAV_END' }   = $result->{ 'S_END' };
1036             $cookie->{ 'S_BEG' }     = $result->{ 'S_BEG' };
1037             $cookie->{ 'S_END' }     = $result->{ 'S_END' };
1038             $cookie->{ 'STRAND' }    = $result->{ 'STRAND' };
1039             $cookie->{ 'Q_ID' }      = $result->{ 'Q_ID' };
1040
1041             $browse = Maasha::XHTML::ln(
1042                 txt  => "browse",
1043                 href => join( "&", "$cookie->{ 'SCRIPT' }?page=browse",
1044                                    "session_id=$cookie->{ 'SESSION_ID' }",
1045                                    "user=$cookie->{ 'USER' }",
1046                                    "clade=$cookie->{ 'CLADE' }",
1047                                    "genome=$cookie->{ 'GENOME' }",
1048                                    "assembly=$cookie->{ 'ASSEMBLY' }",
1049                                    "contig=$cookie->{ 'CONTIG' }",
1050                                    "nav_start=$cookie->{ 'NAV_START' }",
1051                                    "nav_end=$cookie->{ 'NAV_END' }",
1052                 ),
1053             );
1054
1055
1056             $export = Maasha::XHTML::ln(
1057                 txt  => "export",
1058                 href => join( "&", "$cookie->{ 'SCRIPT' }?page=export",
1059                                    "session_id=$cookie->{ 'SESSION_ID' }",
1060                                    "user=$cookie->{ 'USER' }",
1061                                    "clade=$cookie->{ 'CLADE' }",
1062                                    "genome=$cookie->{ 'GENOME' }",
1063                                    "assembly=$cookie->{ 'ASSEMBLY' }",
1064                                    "contig=$cookie->{ 'CONTIG' }",
1065                                    "s_beg=$cookie->{ 'S_BEG' }",
1066                                    "s_end=$cookie->{ 'S_END' }",
1067                                    "strand=$cookie->{ 'STRAND' }",
1068                 ),
1069             );
1070
1071             push @html, Maasha::XHTML::table_row_advanced(
1072                 tr => [ { td => $result->{ 'S_ID' } },
1073                         { td => $result->{ 'S_BEG' }, align => 'right' },
1074                         { td => $result->{ 'S_END' }, align => 'right' },
1075                         { td => $result->{ 'Q_ID' } },
1076                         { td => $result->{ 'SCORE' }, align => 'right' },
1077                         { td => $result->{ 'STRAND' } },
1078                         { td => $result->{ 'HITS' }, align => 'right' },
1079                         { td => $result->{ 'ALIGN' } },
1080                         { td => $result->{ 'BLOCK_COUNT' }, align => 'right' },
1081                         { td => $result->{ 'BLOCK_BEGS' } },
1082                         { td => $result->{ 'BLOCK_LENS' } },
1083                         { td => $result->{ 'BLOCK_TYPE' } },
1084                         { td => $browse },
1085                         { td => $export },
1086                 ], class => "monospace"
1087             );
1088         }
1089
1090         push @html, Maasha::XHTML::table_end;
1091     }
1092
1093     return wantarray ? @html : \@html;
1094 }
1095
1096
1097 sub section_dna
1098 {
1099     # Martin A. Hansen, November 2009.
1100
1101     # Returns a HTML section with extracted DNA.
1102
1103     my ( $cookie,   # cookie hash
1104        ) = @_;
1105
1106     # Returns a list.
1107
1108     my ( @html, $beg, $end, $seq );
1109     
1110     $beg = $cookie->{ 'S_BEG' };
1111     $end = $cookie->{ 'S_END' };
1112     $beg =~ tr/,//d;
1113     $end =~ tr/,//d;
1114
1115     $seq = join ";", ">$cookie->{ 'GENOME' }", $cookie->{ 'ASSEMBLY' }, $cookie->{ 'CONTIG' }, $beg, $end, "$cookie->{ 'STRAND' }\n";
1116     $seq .= Maasha::BGB::Track::dna_get( $cookie );
1117
1118     push @html, Maasha::XHTML::h2( txt => "DNA", class => 'center' );
1119     push @html, Maasha::XHTML::pre( txt => $seq );
1120
1121     return wantarray ? @html : \@html;
1122 }
1123
1124
1125 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1126
1127
1128 __END__