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