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