]> git.donarmstrong.com Git - biopieces.git/blob - www/index.cgi
BGB cleanup of hrefs
[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_page_next
451 {
452     # Martin A. Hansen, November 2009.
453     
454     # Returns the next page in the taxonomy path.
455
456     my ( $cookie,   # cookie hash
457        ) = @_;
458
459     # Returns a string.
460
461     my ( $i );
462
463     for ( $i = 0; $i < @{ $cookie->{ 'LIST_PAGES' } }; $i++ ) {
464         last if $cookie->{ 'PAGE' } eq $cookie->{ 'LIST_PAGES' }->[ $i ];
465     }
466
467     return $cookie->{ 'LIST_PAGES' }->[ $i + 1 ];
468 }
469
470
471 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PAGES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
472
473
474 sub page
475 {
476     # Martin A. Hansen, November 2009.
477
478     # Determines what page to render based on
479     # the cookie's PAGE setting.
480
481     my ( $cookie,   # cookie hash
482        ) = @_;
483
484     # Returns a list.
485
486     my ( @html );
487
488     if ( $cookie->{ 'PAGE' } eq 'login' ) {
489         push @html, page_login( $cookie );
490     } elsif ( $cookie->{ 'PAGE' } eq 'search' ) {
491         push @html, page_search( $cookie );
492     } elsif ( $cookie->{ 'PAGE' } eq 'browse' ) {
493         push @html, page_browse( $cookie );
494     } elsif ( $cookie->{ 'PAGE' } eq 'dna' ) {
495         push @html, page_dna( $cookie );
496     } elsif ( $cookie->{ 'PAGE' } eq 'export' ) {
497         push @html, page_export( $cookie );
498     } else {
499         push @html, page_taxonomy( $cookie );
500     }
501
502     if ( $cookie->{ 'SESSION_ID' } ) {
503         push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "session_id", value => $cookie->{ 'SESSION_ID' } ) );
504     }
505
506     return wantarray ? @html : \@html;
507 }
508
509
510 sub page_login
511 {
512     # Martin A. Hansen, December 2009.
513
514     # Renders the login page.
515
516     my ( $cookie,
517        ) = @_;
518
519     # Returns a list.
520
521     my ( @html );
522
523     push @html, section_login( $cookie );
524
525     return wantarray ? @html : \@html;
526 }
527
528
529 sub page_search
530 {
531     # Martin A. Hansen, December 2009.
532
533     # Renders the search page.
534
535     my ( $cookie,
536        ) = @_;
537
538     # Returns a list.
539
540     my ( @html );
541
542     push @html, section_taxonomy_table( $cookie );
543     push @html, section_search( $cookie );
544
545     return wantarray ? @html : \@html;
546 }
547
548
549 sub page_browse
550 {
551     # Martin A. Hansen, November 2009.
552     
553     # Renders the browse page.
554
555     my ( $cookie,
556        ) = @_;
557
558     # Returns a list.
559
560     my ( @html );
561
562     push @html, section_taxonomy_table( $cookie );
563     push @html, section_navigate( $cookie );
564     push @html, section_browse( $cookie );
565
566     return wantarray ? @html : \@html;
567 }
568
569
570 sub page_dna
571 {
572     # Martin A. Hansen, November 2009.
573     
574     # Renders the DNA page.
575
576     my ( $cookie,
577        ) = @_;
578
579     # Returns a list.
580
581     my ( @html );
582
583     push @html, section_taxonomy_table( $cookie );
584     push @html, section_dna( $cookie );
585
586     return wantarray ? @html : \@html;
587 }
588
589
590 sub page_export
591 {
592     # Martin A. Hansen, November 2009.
593
594     # Renders the export page.
595
596     my ( $cookie,
597        ) = @_;
598
599     # Returns a list.
600
601     my ( @html );
602
603     push @html, section_taxonomy_table( $cookie );
604     push @html, section_export( $cookie );
605
606     return wantarray ? @html : \@html;
607 }
608
609
610 sub page_taxonomy
611 {
612     # Martin A. Hansen, November 2009.
613     
614     # Renders the browse page.
615
616     my ( $cookie,   # cookie hash
617        ) = @_;
618
619     # Returns a list.
620
621     my ( @html );
622
623     push @html, section_taxonomy_table( $cookie );
624     push @html, section_taxonomy_select( $cookie );
625
626     return wantarray ? @html : \@html;
627 }
628
629
630 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SECTIONS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
631
632
633 sub section_login
634 {
635     # Martin A. Hansen, December 2009.
636    
637     # Returns a HTML section with a login menu.
638
639     my ( $cookie,   # cookie hash
640        ) = @_;
641
642     # Returns a list.
643
644     my ( $user, $password, $login, @html );
645
646     $user     = Maasha::XHTML::text( name => "user", value => "", size => 20 );
647     $password = Maasha::XHTML::password( name => "password", value => "", size => 20 );
648     $login    = Maasha::XHTML::submit( name => "login_submit", value => "Login" );
649
650     push @html, Maasha::XHTML::h2( txt => "Login", class => 'center' );
651
652     push @html, Maasha::XHTML::table_beg( summary => "Login table", align => 'center' );
653     push @html, Maasha::XHTML::table_row_simple( tr => [ "User:", $user ] );
654     push @html, Maasha::XHTML::table_row_simple( tr => [ "Password:", $password ] );
655     push @html, Maasha::XHTML::table_row_simple( tr => [ "", $login ] );
656     push @html, Maasha::XHTML::table_end;
657
658     if ( $cookie->{ 'LOGIN' } and $cookie->{ 'LOGIN' } eq 'ERROR' ) {
659         push @html, Maasha::XHTML::h3( txt => "Bad user or password - please retry", class => 'error' );
660     }
661
662     return wantarray ? @html : \@html;
663 }
664
665
666 sub section_taxonomy_table
667 {
668     # Martin A. Hansen, November 2009.
669
670     # Returns a HTML section with a taxonomy table
671     # showing the location in the taxonomy and with
672     # links to browse the taxonomy.
673
674     my ( $cookie,   # cookie hash
675        ) = @_;
676
677     # Returns a list.
678
679     my ( $page, @row, @html, $href, $txt );
680     
681     foreach $page ( @{ $cookie->{ 'LIST_PAGES' } } )
682     {
683         last if $page eq $cookie->{ 'PAGE' } or $page eq 'browse';
684
685         $href  = "$cookie->{ 'SCRIPT' }?page=$page";
686         $href .= "&session_id=$cookie->{ 'SESSION_ID' }";
687         $href .= "&user=$cookie->{ 'USER' }";
688         $href .= "&clade=$cookie->{ 'CLADE' }"       if $page !~ /clade/;
689         $href .= "&genome=$cookie->{ 'GENOME' }"     if $page !~ /clade|genome/;
690         $href .= "&assembly=$cookie->{ 'ASSEMBLY' }" if $page !~ /clade|genome|assembly/;
691         $href .= "&contig=$cookie->{ 'CONTIG' }"     if $page !~ /clade|genome|assembly|contig/;
692
693         $txt = ": $cookie->{ uc $page }";
694
695         push @row, Maasha::XHTML::ln( txt => $page, href => $href, class => 'inline' );
696         push @row, Maasha::XHTML::p(  txt => $txt, class => 'inline' );
697     }
698
699     push @html, Maasha::XHTML::table_beg( summary => "Taxonomy table", align => 'center', cellpadding => '5px' );
700     push @html, Maasha::XHTML::table_row_simple( tr => [ join( "\n", @row ) ], align => 'center' );
701     push @html, Maasha::XHTML::table_end;
702
703     return wantarray ? @html : \@html;
704 }
705
706
707 sub section_taxonomy_select
708 {
709     # Martin A. Hansen, November 2009.
710     
711     # Returns a HTML section with selection choices
712     # for navigating the taxonomy tree.
713
714     my ( $cookie,   # cookie hash
715        ) = @_;
716
717     # Returns a list.
718
719     my ( $list, @html, $item, $href );
720
721     $list = "LIST_" . uc $cookie->{ 'PAGE' };
722
723     push @html, Maasha::XHTML::h2( txt => "Select $cookie->{ 'PAGE' }", class => 'center' );
724
725     push @html, Maasha::XHTML::table_beg( summary => "Taxonomy select table", align => 'center', cellpadding => '5px' );
726
727     foreach $item ( @{ $cookie->{ $list } } )
728     {
729         $cookie->{ uc $cookie->{ 'PAGE' } } = $item;
730
731         $href  = "$cookie->{ 'SCRIPT' }?page=" . cookie_page_next( $cookie );
732         $href .= "&session_id=$cookie->{ 'SESSION_ID' }";
733         $href .= "&user=$cookie->{ 'USER' }";
734         $href .= "&clade=$cookie->{ 'CLADE' }";
735         $href .= "&genome=$cookie->{ 'GENOME' }"     if $cookie->{ 'GENOME' };
736         $href .= "&assembly=$cookie->{ 'ASSEMBLY' }" if $cookie->{ 'ASSEMBLY' };
737         $href .= "&contig=$cookie->{ 'CONTIG' }"     if $cookie->{ 'CONTIG' };
738
739         push @html, Maasha::XHTML::table_row_simple( tr => [ Maasha::XHTML::ln( txt => $item, href => $href ) ] );
740     }
741
742     push @html, Maasha::XHTML::table_end;
743
744     return wantarray ? @html : \@html;
745 }
746
747
748 sub section_navigate
749 {
750     # Martin A. Hansen, November 2009.
751
752     # Returns a HTML section for navigating in the browser window.
753
754     my ( $cookie,   # cookie hash
755        ) = @_;
756
757     # Returns a list.
758
759     my ( @html, $nav_val );
760
761     $nav_val = Maasha::Calc::commify( $cookie->{ 'NAV_START' } ) . "-" . Maasha::Calc::commify( $cookie->{ 'NAV_END' } );
762
763     push @html, Maasha::XHTML::table_beg( summary => "Navigation table", align => 'center' );
764     push @html, Maasha::XHTML::table_row_simple( tr => [
765         "Position or search term:",
766         Maasha::XHTML::text( name => "nav_search", value => $nav_val, size => 30 ),
767         Maasha::XHTML::submit( name => "nav_submit", value => "Submit" ),
768     ] );
769     push @html, Maasha::XHTML::table_end;
770
771     push @html, Maasha::XHTML::table_beg( summary => "Zoom table", align => 'center' );
772     push @html, Maasha::XHTML::table_row_simple( tr => [
773         Maasha::XHTML::p( txt => 'Move:' ),
774         Maasha::XHTML::submit( name => "move_left3",  value => "<<<", title => "move 95% to the left" ),
775         Maasha::XHTML::submit( name => "move_left2",  value => "<<",  title => "move 47.5% to the left" ),
776         Maasha::XHTML::submit( name => "move_left1",  value => "<",   title => "move 10% to the left" ),
777         Maasha::XHTML::submit( name => "move_right1", value => ">",   title => "move 10% to the rigth" ),
778         Maasha::XHTML::submit( name => "move_right2", value => ">>",  title => "move 47.5% to the rigth" ),
779         Maasha::XHTML::submit( name => "move_right3", value => ">>>", title => "move 95% to the right" ),
780         Maasha::XHTML::p( txt => 'Zoom in:' ),
781         Maasha::XHTML::submit( name => "zoom_in1", value => "1.5x" ),
782         Maasha::XHTML::submit( name => "zoom_in2", value => "3x" ),
783         Maasha::XHTML::submit( name => "zoom_in3", value => "10x" ),
784         Maasha::XHTML::p( txt => 'Zoom out:' ),
785         Maasha::XHTML::submit( name => "zoom_out1", value => "1.5x" ),
786         Maasha::XHTML::submit( name => "zoom_out2", value => "3x" ),
787         Maasha::XHTML::submit( name => "zoom_out3", value => "10x" ),
788     ] );
789     push @html, Maasha::XHTML::table_end;
790
791     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "page",     value => "browse" ) );
792     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "user",     value => $cookie->{ 'USER' } ) );
793     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "clade",    value => $cookie->{ 'CLADE' } ) );
794     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "genome",   value => $cookie->{ 'GENOME' } ) );
795     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "assembly", value => $cookie->{ 'ASSEMBLY' } ) );
796     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "contig",   value => $cookie->{ 'CONTIG' } ) );
797
798     return wantarray ? @html : \@html;
799 }
800
801
802 sub section_browse
803 {
804     my ( $cookie,
805        ) = @_;
806
807     # Returns a list.
808
809     my ( @tracks, $i, @features, $feat, $elem, $surface, $cr, $png_data, @html, @img, $x1, $y1, $x2, $y2, $factor, $center );
810
811     push @features, [ Maasha::BGB::Track::track_ruler( $cookie ) ];
812     push @features, [ Maasha::BGB::Track::track_seq( $cookie ) ];
813
814     @tracks = Maasha::BGB::Track::path_tracks( $cookie );
815
816     for ( $i = 0; $i < @tracks; $i++ )
817     {
818         $cookie->{ 'FEAT_COLOR' } = Maasha::BGB::Draw::palette( $i );
819
820         push @features, [ Maasha::BGB::Track::track_feature( $tracks[ $i ], $cookie ) ];
821     }
822
823     $surface = Cairo::ImageSurface->create( 'argb32', $cookie->{ 'IMG_WIDTH' }, $cookie->{ 'TRACK_OFFSET' } );
824     $cr      = Cairo::Context->create( $surface );
825
826     $cr->rectangle ( 0, 0, $cookie->{ 'IMG_WIDTH' },  $cookie->{ 'TRACK_OFFSET' } );
827     $cr->set_source_rgb ( 1, 1, 1 );
828     $cr->fill;
829
830     foreach $feat ( @features ) {
831         Maasha::BGB::Draw::draw_feature( $cr, $feat ) if $feat;
832     }
833
834     $png_data = Maasha::BGB::Draw::base64_png( $surface );
835
836     push @img, Maasha::XHTML::img(
837         src    => "data:image/png;base64,$png_data",
838         alt    => "Browser Tracks",
839         height => $cookie->{ 'TRACK_OFFSET' },
840         width  => $cookie->{ 'IMG_WIDTH' },
841         id     => "browser_map",
842         usemap => "#browser_map"
843     );
844
845     push @img, Maasha::XHTML::map_beg( name => "browser_map", id => "browser_map" );
846
847     $factor = ( $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' } + 1 ) / $cookie->{ 'IMG_WIDTH' };
848
849     for ( $i = 0; $i < $cookie->{ 'IMG_WIDTH' }; $i += 5 )
850     {
851         $x1 = $i;
852         $y1 = 10;
853         $x2 = $i + 5;
854         $y2 = 20;
855
856         $center = int( $cookie->{ 'NAV_START' } + $factor * $i );
857
858         push @img, Maasha::XHTML::area(
859             href     => join( "&", "$cookie->{ 'SCRIPT' }?page=browse",
860                                    "session_id=$cookie->{ 'SESSION_ID' }",
861                                    "user=$cookie->{ 'USER' }",
862                                    "clade=$cookie->{ 'CLADE' }",
863                                    "genome=$cookie->{ 'GENOME' }",
864                                    "contig=$cookie->{ 'CONTIG' }",
865                                    "assembly=$cookie->{ 'ASSEMBLY' }",
866                                    "nav_start=$cookie->{ 'NAV_START' }",
867                                    "nav_end=$cookie->{ 'NAV_END' }",
868                                    "nav_center=$center",
869                       ),
870             shape  => 'rect',
871             coords => "$x1, $y1, $x2, $y2",
872             title  => "Center on " . Maasha::Calc::commify( $center ),
873         );
874     }
875
876     foreach $feat ( @features )
877     {
878         foreach $elem ( @{ $feat } )
879         {
880             next if $elem->{ 'type' } eq 'text';
881             next if $elem->{ 'type' } eq 'wiggle';
882
883             #$elem->{ 'strand' } = '&#43' if $elem->{ 'strand' } eq '+';
884
885             $cookie->{ 'Q_ID' }   = $elem->{ 'q_id' };
886             $cookie->{ 'S_BEG' }  = $elem->{ 's_beg' };
887             $cookie->{ 'S_END' }  = $elem->{ 's_end' };
888             $cookie->{ 'STRAND' } = $elem->{ 'strand' };
889
890             push @img, Maasha::XHTML::area(
891                 href     => join( "&", "$cookie->{ 'SCRIPT' }?page=export",
892                                        "session_id=$cookie->{ 'SESSION_ID' }",
893                                        "user=$cookie->{ 'USER' }",
894                                        "clade=$cookie->{ 'CLADE' }",
895                                        "genome=$cookie->{ 'GENOME' }",
896                                        "contig=$cookie->{ 'CONTIG' }",
897                                        "assembly=$cookie->{ 'ASSEMBLY' }",
898                                        "s_beg=$cookie->{ 'S_BEG' }",
899                                        "s_end=$cookie->{ 'S_END' }",
900                                        "strand=$cookie->{ 'STRAND' }",
901                           ),
902                 shape  => 'rect',
903                 coords => "$elem->{ x1 }, $elem->{ y1 }, $elem->{ x2 }, $elem->{ y2 }",
904                 title  => "$elem->{ 'title' }",
905             );
906         }
907     }
908
909     push @img, Maasha::XHTML::map_end();
910
911     push @html, Maasha::XHTML::p( txt => join( "\n", @img ) );
912
913     @html = Maasha::XHTML::div( txt => join( "\n", @html ), class => 'browse' );
914
915     return wantarray ? @html : \@html;
916 }
917
918
919 sub section_export
920 {
921     # Martin A. Hansen, November 2009.
922
923     # Returns a HTML section with export table.
924
925     my ( $cookie,   # cookie hash
926        ) = @_;
927
928     # Returns a list.
929
930     my ( @row_dna_contig, @html );
931
932     push @row_dna_contig, Maasha::XHTML::p( txt => qq(Export Feature: "$cookie->{ 'Q_ID' }" DNA from Contig: ), class => 'inline' );
933     push @row_dna_contig, Maasha::XHTML::p( txt  => "$cookie->{ 'CONTIG' } Start: ", class => 'inline' );
934     push @row_dna_contig, Maasha::XHTML::text( name => "s_beg", value => Maasha::Calc::commify( $cookie->{ 'S_BEG' } ), size => 15 );
935     push @row_dna_contig, Maasha::XHTML::p( txt => "End: ", class => 'inline' );
936     push @row_dna_contig, Maasha::XHTML::text( name => "s_end", value => Maasha::Calc::commify( $cookie->{ 'S_END' } ), size => 15 );
937     push @row_dna_contig, Maasha::XHTML::p( txt => "Strand: ", class => 'inline' );
938     push @row_dna_contig, Maasha::XHTML::menu( name => "strand", options => [ qw( + - ) ], selected => $cookie->{ 'STRAND' } );
939     push @row_dna_contig, Maasha::XHTML::submit( name => "export", value => "Export", title => "Fetch DNA from this region" );
940
941     push @html, Maasha::XHTML::h2( txt => "Export", class => 'center' );
942     push @html, Maasha::XHTML::table_beg( summary => "Taxonomy select table", align => 'center' );
943     push @html, Maasha::XHTML::table_row_simple( tr => \@row_dna_contig );
944     push @html, Maasha::XHTML::table_end;
945
946     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "page",     value => "dna" ) );
947     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "user",     value => $cookie->{ 'USER' } ) );
948     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "clade",    value => $cookie->{ 'CLADE' } ) );
949     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "genome",   value => $cookie->{ 'GENOME' } ) );
950     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "assembly", value => $cookie->{ 'ASSEMBLY' } ) );
951     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "contig",   value => $cookie->{ 'CONTIG' } ) );
952
953     return wantarray ? @html : \@html;
954 }
955
956
957 sub section_search
958 {
959     # Martin A. Hansen, November 2009.
960
961     # Returns a HTML section with export table.
962
963     my ( $cookie,   # cookie hash
964        ) = @_;
965
966     # Returns a list.
967
968     my ( $results, $result, $count, @html, $export, $browse );
969
970     $results = Maasha::BGB::Track::search_tracks( $cookie );
971
972     $count = scalar @{ $results };
973
974     push @html, Maasha::XHTML::h2( txt => "Search", class => 'center' );
975     push @html, Maasha::XHTML::p( txt => qq(Results for "$cookie->{ 'SEARCH' }": $count), class => 'center' );
976
977     if ( $count > 0 )
978     {
979         push @html, Maasha::XHTML::table_beg( summary => "Search table", align => 'center', cellpadding => '5px' );
980         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) ] );
981
982         foreach $result ( @{ $results } )
983         {
984             $result = Maasha::KISS::kiss2biopiece( $result );
985
986             $cookie->{ 'CONTIG' }    = $result->{ 'S_ID' };
987             $cookie->{ 'NAV_START' } = $result->{ 'S_BEG' };
988             $cookie->{ 'NAV_END' }   = $result->{ 'S_END' };
989             $cookie->{ 'S_BEG' }     = $result->{ 'S_BEG' };
990             $cookie->{ 'S_END' }     = $result->{ 'S_END' };
991             $cookie->{ 'STRAND' }    = $result->{ 'STRAND' };
992             $cookie->{ 'Q_ID' }      = $result->{ 'Q_ID' };
993
994             $browse = Maasha::XHTML::ln(
995                 txt  => "browse",
996                 href => join( "&", "$cookie->{ 'SCRIPT' }?page=browse",
997                                    "session_id=$cookie->{ 'SESSION_ID' }",
998                                    "user=$cookie->{ 'USER' }",
999                                    "clade=$cookie->{ 'CLADE' }",
1000                                    "genome=$cookie->{ 'GENOME' }",
1001                                    "contig=$cookie->{ 'CONTIG' }",
1002                                    "assembly=$cookie->{ 'ASSEMBLY' }",
1003                                    "nav_start=$cookie->{ 'NAV_START' }",
1004                                    "nav_end=$cookie->{ 'NAV_END' }",
1005                 ),
1006             );
1007
1008
1009             $export = Maasha::XHTML::ln(
1010                 txt  => "export",
1011                 href => join( "&", "$cookie->{ 'SCRIPT' }?page=export",
1012                                    "session_id=$cookie->{ 'SESSION_ID' }",
1013                                    "user=$cookie->{ 'USER' }",
1014                                    "clade=$cookie->{ 'CLADE' }",
1015                                    "genome=$cookie->{ 'GENOME' }",
1016                                    "contig=$cookie->{ 'CONTIG' }",
1017                                    "assembly=$cookie->{ 'ASSEMBLY' }",
1018                                    "s_beg=$cookie->{ 'S_BEG' }",
1019                                    "s_end=$cookie->{ 'S_END' }",
1020                                    "strand=$cookie->{ 'STRAND' }",
1021                 ),
1022             );
1023
1024             push @html, Maasha::XHTML::table_row_advanced(
1025                 tr => [ { td => $result->{ 'S_ID' } },
1026                         { td => $result->{ 'S_BEG' }, align => 'right' },
1027                         { td => $result->{ 'S_END' }, align => 'right' },
1028                         { td => $result->{ 'Q_ID' } },
1029                         { td => $result->{ 'SCORE' }, align => 'right' },
1030                         { td => $result->{ 'STRAND' } },
1031                         { td => $result->{ 'HITS' }, align => 'right' },
1032                         { td => $result->{ 'ALIGN' } },
1033                         { td => $result->{ 'BLOCK_COUNT' }, align => 'right' },
1034                         { td => $result->{ 'BLOCK_BEGS' } },
1035                         { td => $result->{ 'BLOCK_LENS' } },
1036                         { td => $result->{ 'BLOCK_TYPE' } },
1037                         { td => $browse },
1038                         { td => $export },
1039                 ], class => "monospace"
1040             );
1041         }
1042
1043         push @html, Maasha::XHTML::table_end;
1044     }
1045
1046     return wantarray ? @html : \@html;
1047 }
1048
1049
1050 sub section_dna
1051 {
1052     # Martin A. Hansen, November 2009.
1053
1054     # Returns a HTML section with extracted DNA.
1055
1056     my ( $cookie,   # cookie hash
1057        ) = @_;
1058
1059     # Returns a list.
1060
1061     my ( @html, $beg, $end, $seq );
1062     
1063     $beg = $cookie->{ 'S_BEG' };
1064     $end = $cookie->{ 'S_END' };
1065     $beg =~ tr/,//d;
1066     $end =~ tr/,//d;
1067
1068     $seq = join ";", ">$cookie->{ 'GENOME' }", $cookie->{ 'ASSEMBLY' }, $cookie->{ 'CONTIG' }, $beg, $end, "$cookie->{ 'STRAND' }\n";
1069     $seq .= Maasha::BGB::Track::dna_get( $cookie );
1070
1071     push @html, Maasha::XHTML::h2( txt => "DNA", class => 'center' );
1072     push @html, Maasha::XHTML::pre( txt => $seq );
1073
1074     return wantarray ? @html : \@html;
1075 }
1076
1077
1078 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1079
1080
1081 __END__