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