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