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