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