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