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