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