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