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