3 # Copyright (C) 2006-2009 Martin A. Hansen.
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.
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.
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.
19 # http://www.gnu.org/copyleft/gpl.html
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26 use lib $ENV{ 'BP_PERL' };
35 use Maasha::BBrowser::Track;
36 use Maasha::BBrowser::Draw;
38 my ( $cgi, $cookie, @html );
41 $cookie = cookie_default( $cgi );;
43 push @html, Maasha::XHTML::html_header(
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 ) ],
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" );
56 push @html, page( $cookie );
58 push @html, Maasha::XHTML::form_end;
59 push @html, Maasha::XHTML::body_end;
60 push @html, Maasha::XHTML::html_end;
62 # push @html, Maasha::XHTML::hdump( $cgi->param ); # DEBUG
63 # push @html, Maasha::XHTML::hdump( $cookie ); # DEBUG
65 print "$_\n" foreach @html;
68 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
71 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> COOKIE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
76 # Martin A. Hansen, November 2009.
78 # Set a cookie with values from the CGI object or defaults.
80 my ( $cgi, # CGI object
85 my ( $cookie, $path );
89 $cookie->{ 'SCRIPT' } = Maasha::Common::get_scriptname();
90 $cookie->{ 'DATA_DIR' } = "Data";
91 $cookie->{ 'LIST_PAGES' } = [ qw( user clade genome assembly contig browse ) ];
92 $cookie->{ 'PAGE' } = $cgi->param( 'page' ) || 'user';
93 $cookie->{ 'USER' } = $cgi->param( 'user' ) || '';
94 $cookie->{ 'CLADE' } = $cgi->param( 'clade' ) || '';
95 $cookie->{ 'GENOME' } = $cgi->param( 'genome' ) || '';
96 $cookie->{ 'ASSEMBLY' } = $cgi->param( 'assembly' ) || '';
97 $cookie->{ 'CONTIG' } = $cgi->param( 'contig' ) || '';
98 $cookie->{ 'NAV_START' } = $cgi->param( 'nav_start' );
99 $cookie->{ 'NAV_END' } = $cgi->param( 'nav_end' );
100 $cookie->{ 'ZOOM_IN1' } = $cgi->param( 'zoom_in1' );
101 $cookie->{ 'ZOOM_IN2' } = $cgi->param( 'zoom_in2' );
102 $cookie->{ 'ZOOM_IN3' } = $cgi->param( 'zoom_in3' );
103 $cookie->{ 'ZOOM_OUT1' } = $cgi->param( 'zoom_out1' );
104 $cookie->{ 'ZOOM_OUT2' } = $cgi->param( 'zoom_out2' );
105 $cookie->{ 'ZOOM_OUT3' } = $cgi->param( 'zoom_out3' );
106 $cookie->{ 'MOVE_LEFT1' } = $cgi->param( 'move_left1' );
107 $cookie->{ 'MOVE_LEFT2' } = $cgi->param( 'move_left2' );
108 $cookie->{ 'MOVE_LEFT3' } = $cgi->param( 'move_left3' );
109 $cookie->{ 'MOVE_RIGHT1' } = $cgi->param( 'move_right1' );
110 $cookie->{ 'MOVE_RIGHT2' } = $cgi->param( 'move_right2' );
111 $cookie->{ 'MOVE_RIGHT3' } = $cgi->param( 'move_right3' );
113 $cookie->{ 'IMG_WIDTH' } = 1200;
114 $cookie->{ 'IMG_HEIGHT' } = 800;
115 $cookie->{ 'TRACK_OFFSET' } = 20;
116 $cookie->{ 'TRACK_SPACE' } = 20;
117 $cookie->{ 'RULER_FONT_SIZE' } = 10;
118 $cookie->{ 'RULER_COLOR' } = [ 0, 0, 0 ];
119 $cookie->{ 'SEQ_FONT_SIZE' } = 10;
120 $cookie->{ 'SEQ_COLOR' } = [ 0, 0, 0, ];
121 $cookie->{ 'FEAT_WIDTH' } = 5;
122 $cookie->{ 'FEAT_COLOR' } = [ 0, 0, 0 ];
123 $cookie->{ 'FEAT_MAX' } = 5000;
125 $path = "$cookie->{ 'DATA_DIR' }/Users";
127 $cookie->{ 'LIST_USER' } = Maasha::Filesys::ls_dirs_base( $path ) if -d $path;
129 $path .= "/$cookie->{ 'USER' }";
131 $cookie->{ 'LIST_CLADE' } = Maasha::Filesys::ls_dirs_base( $path ) if -d $path;
133 $path .= "/$cookie->{ 'CLADE' }";
135 $cookie->{ 'LIST_GENOME' } = Maasha::Filesys::ls_dirs_base( $path ) if -d $path;
137 $path .= "/$cookie->{ 'GENOME' }";
139 $cookie->{ 'LIST_ASSEMBLY' } = Maasha::Filesys::ls_dirs_base( $path ) if -d $path;
141 $path .= "/$cookie->{ 'ASSEMBLY' }";
143 $cookie->{ 'LIST_CONTIG' } = Maasha::Filesys::ls_dirs_base( $path ) if -d $path;
145 if ( $cookie->{ 'CONTIG' } )
147 cookie_start( $cookie );
148 cookie_end( $cookie );
149 cookie_zoom( $cookie );
150 cookie_move( $cookie );
153 return wantarray ? %{ $cookie } : $cookie;
159 # Martin A. Hansen, November 2009.
161 # Decommify the cookie value for NAV_START and adjust it to
162 # to prevent negative values.
164 my ( $cookie, # cookie hash
169 if ( defined $cookie->{ 'NAV_START' } )
171 $cookie->{ 'NAV_START' } =~ tr/,//d;
172 $cookie->{ 'NAV_START' } = 1 if $cookie->{ 'NAV_START' } <= 0;
176 $cookie->{ 'NAV_START' } = 1;
183 # Martin A. Hansen, November 2009.
185 # Decommify the cookie value for NAV_END and adjust it to prevent
186 # overshooting the max value for the contig size as determined
189 my ( $cookie, # cookie hash
196 $max = Maasha::Filesys::file_size( Maasha::BBrowser::Track::path_seq( $cookie ) );
198 if ( defined $cookie->{ 'NAV_END' } )
200 $cookie->{ 'NAV_END' } =~ tr/,//d;
201 $cookie->{ 'NAV_END' } = $max if $cookie->{ 'NAV_END' } > $max;
205 $cookie->{ 'NAV_END' } = $max;
212 # Martin A. Hansen, November 2009.
214 # Adjust the cookie values for NAV_START and NAV_END based
215 # on cookie ZOOM values.
217 my ( $cookie, # cookie hash
222 my ( $max, $dist, $new_dist, $dist_diff );
224 $max = Maasha::Filesys::file_size( Maasha::BBrowser::Track::path_seq( $cookie ) );
226 $dist = $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' };
228 if ( defined $cookie->{ 'ZOOM_IN1' } ) {
229 $new_dist = $dist / 1.5;
230 } elsif ( defined $cookie->{ 'ZOOM_IN2' } ) {
231 $new_dist = $dist / 3;
232 } elsif ( defined $cookie->{ 'ZOOM_IN3' } ) {
233 $new_dist = $dist / 10;
234 } elsif ( defined $cookie->{ 'ZOOM_OUT1' } ) {
235 $new_dist = $dist * 1.5;
236 } elsif ( defined $cookie->{ 'ZOOM_OUT2' } ) {
237 $new_dist = $dist * 3;
238 } elsif ( defined $cookie->{ 'ZOOM_OUT3' } ) {
239 $new_dist = $dist * 10;
244 $dist_diff = $dist - $new_dist;
246 $cookie->{ 'NAV_START' } = int( $cookie->{ 'NAV_START' } + ( $dist_diff / 2 ) );
247 $cookie->{ 'NAV_END' } = int( $cookie->{ 'NAV_END' } - ( $dist_diff / 2 ) );
249 $cookie->{ 'NAV_START' } = 1 if $cookie->{ 'NAV_START' } <= 0;
250 $cookie->{ 'NAV_END' } = $max if $cookie->{ 'NAV_END' } > $max;
257 # Martin A. Hansen, November 2009.
259 # Adjust the cookie values for NAV_START and NAV_END based
260 # on cookie MOVE values.
262 my ( $cookie, # cookie hash
265 my ( $max, $dist, $shift, $new_start, $new_end );
267 $max = Maasha::Filesys::file_size( Maasha::BBrowser::Track::path_seq( $cookie ) );
269 $dist = $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' };
271 if ( defined $cookie->{ 'MOVE_LEFT1' } ) {
272 $shift = -1 * $dist * 0.10;
273 } elsif ( defined $cookie->{ 'MOVE_LEFT2' } ) {
274 $shift = -1 * $dist * 0.475;
275 } elsif ( defined $cookie->{ 'MOVE_LEFT3' } ) {
276 $shift = -1 * $dist * 0.95;
277 } elsif ( defined $cookie->{ 'MOVE_RIGHT1' } ) {
278 $shift = $dist * 0.10;
279 } elsif ( defined $cookie->{ 'MOVE_RIGHT2' } ) {
280 $shift = $dist * 0.475;
281 } elsif ( defined $cookie->{ 'MOVE_RIGHT3' } ) {
282 $shift = $dist * 0.95;
287 $new_start = int( $cookie->{ 'NAV_START' } + $shift );
288 $new_end = int( $cookie->{ 'NAV_END' } + $shift );
290 if ( $new_start > 0 and $new_end < $max )
292 $cookie->{ 'NAV_START' } = $new_start;
293 $cookie->{ 'NAV_END' } = $new_end;
301 # Martin A. Hansen, November 2009.
303 # Returns the href for a taxonomy path terminated at a given
304 # page using information stored in a cookie.
306 my ( $cookie, # cookie hash
316 push @href, "$cookie->{ 'SCRIPT' }?page=$page";
317 push @href, "user=$cookie->{ 'USER' }" if $cookie->{ 'USER' };
318 last if $page eq 'user';
319 push @href, "clade=$cookie->{ 'CLADE' }" if $cookie->{ 'CLADE' };
320 last if $page eq 'clade';
321 push @href, "genome=$cookie->{ 'GENOME' }" if $cookie->{ 'GENOME' };
322 last if $page eq 'genome';
323 push @href, "assembly=$cookie->{ 'ASSEMBLY' }" if $cookie->{ 'ASSEMBLY' };
324 last if $page eq 'assembly';
325 push @href, "contig=$cookie->{ 'CONTIG' }" if $cookie->{ 'CONTIG' };
326 last if $page eq 'contig';
330 return join "&", @href;
336 # Martin A. Hansen, November 2009.
338 # Returns the next page in the taxonomy path.
340 my ( $cookie, # cookie hash
347 for ( $i = 0; $i < @{ $cookie->{ 'LIST_PAGES' } }; $i++ ) {
348 last if $cookie->{ 'PAGE' } eq $cookie->{ 'LIST_PAGES' }->[ $i ];
351 return $cookie->{ 'LIST_PAGES' }->[ $i + 1 ];
355 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PAGES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
360 # Martin A. Hansen, November 2009.
362 # Determines what page to render based on
363 # the cookie's PAGE setting.
365 my ( $cookie, # cookie hash
372 if ( $cookie->{ 'PAGE' } eq 'export' ) {
373 push @html, page_export( $cookie );
374 } elsif ( $cookie->{ 'PAGE' } eq 'browse' ) {
375 push @html, page_browse( $cookie );
377 push @html, page_taxonomy( $cookie );
380 return wantarray ? @html : \@html;
386 # Martin A. Hansen, November 2009.
388 # Renders the export page.
395 push @html, section_taxonomy_table( $cookie );
396 push @html, section_export( $cookie );
398 return wantarray ? @html : \@html;
404 # Martin A. Hansen, November 2009.
406 # Renders the browse page.
415 push @html, section_taxonomy_table( $cookie );
416 push @html, section_navigate( $cookie );
417 push @html, section_browse( $cookie );
419 return wantarray ? @html : \@html;
425 # Martin A. Hansen, November 2009.
427 # Renders the browse page.
429 my ( $cookie, # cookie hash
436 push @html, section_taxonomy_table( $cookie );
437 push @html, section_taxonomy_select( $cookie );
439 return wantarray ? @html : \@html;
443 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SECTIONS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
446 sub section_taxonomy_table
448 # Martin A. Hansen, November 2009.
450 # Returns a HTML section with a taxonomy table
451 # showing the location in the taxonomy and with
452 # links to browse the taxonomy.
454 my ( $cookie, # cookie hash
459 my ( $page, @row, @html, $href, $txt );
461 foreach $page ( @{ $cookie->{ 'LIST_PAGES' } } )
463 last if $page eq $cookie->{ 'PAGE' };
465 $href = cookie_href( $cookie, $page );
467 $txt = ": $cookie->{ uc $page }";
469 push @row, Maasha::XHTML::ln( txt => $page, href => $href, class => 'inline' );
470 push @row, Maasha::XHTML::p( txt => $txt, class => 'inline' );
473 push @html, Maasha::XHTML::table_beg( summary => "Taxonomy table", align => 'center', cellpadding => '5px' );
474 push @html, Maasha::XHTML::table_row_simple( tr => [ join( " ", @row ) ], align => 'center' );
475 push @html, Maasha::XHTML::table_end;
477 return wantarray ? @html : \@html;
481 sub section_taxonomy_select
483 # Martin A. Hansen, November 2009.
485 # Returns a HTML section with selection choices
486 # for navigating the taxonomy tree.
488 my ( $cookie, # cookie hash
493 my ( $list, @html, $item, $href );
495 $list = "LIST_" . uc $cookie->{ 'PAGE' };
497 push @html, Maasha::XHTML::h2( txt => "Select $cookie->{ 'PAGE' }", class => 'center' );
499 push @html, Maasha::XHTML::table_beg( summary => "Select table", align => 'center', cellpadding => '5px' );
501 foreach $item ( @{ $cookie->{ $list } } )
503 $cookie->{ uc $cookie->{ 'PAGE' } } = $item;
505 $href = cookie_href( $cookie, cookie_page_next( $cookie ) );
507 push @html, Maasha::XHTML::table_row_simple( tr => [ Maasha::XHTML::ln( txt => $item, href => $href ) ] );
510 push @html, Maasha::XHTML::table_end;
512 return wantarray ? @html : \@html;
518 # Martin A. Hansen, November 2009.
520 # Returns a HTML section for navigating in the browser window.
522 my ( $cookie, # cookie hash
529 push @html, Maasha::XHTML::table_beg( summary => "Navigation table", align => 'center' );
530 push @html, Maasha::XHTML::table_row_simple( tr => [
532 Maasha::XHTML::text( name => "nav_start", value => Maasha::Calc::commify( $cookie->{ 'NAV_START' } ), size => 20 ),
534 Maasha::XHTML::text( name => "nav_end", value => Maasha::Calc::commify( $cookie->{ 'NAV_END' } ), size => 20 ),
535 Maasha::XHTML::submit( name => "nav_submit", value => "Submit" ),
537 push @html, Maasha::XHTML::table_end;
539 push @html, Maasha::XHTML::table_beg( summary => "Zoom table", align => 'center' );
540 push @html, Maasha::XHTML::table_row_simple( tr => [
541 Maasha::XHTML::p( txt => 'Move:' ),
542 Maasha::XHTML::submit( name => "move_left3", value => "<<<", title => "move 95% to the left" ),
543 Maasha::XHTML::submit( name => "move_left2", value => "<<", title => "move 47.5% to the left" ),
544 Maasha::XHTML::submit( name => "move_left1", value => "<", title => "move 10% to the left" ),
545 Maasha::XHTML::submit( name => "move_right1", value => ">", title => "move 10% to the rigth" ),
546 Maasha::XHTML::submit( name => "move_right2", value => ">>", title => "move 47.5% to the rigth" ),
547 Maasha::XHTML::submit( name => "move_right3", value => ">>>", title => "move 95% to the right" ),
548 Maasha::XHTML::p( txt => 'Zoom in:' ),
549 Maasha::XHTML::submit( name => "zoom_in1", value => "1.5x" ),
550 Maasha::XHTML::submit( name => "zoom_in2", value => "3x" ),
551 Maasha::XHTML::submit( name => "zoom_in3", value => "10x" ),
552 Maasha::XHTML::p( txt => 'Zoom out:' ),
553 Maasha::XHTML::submit( name => "zoom_out1", value => "1.5x" ),
554 Maasha::XHTML::submit( name => "zoom_out2", value => "3x" ),
555 Maasha::XHTML::submit( name => "zoom_out3", value => "10x" ),
557 push @html, Maasha::XHTML::table_end;
559 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "page", value => "browse" ) );
560 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "user", value => "$cookie->{ 'USER' }" ) );
561 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "clade", value => "$cookie->{ 'CLADE' }" ) );
562 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "genome", value => "$cookie->{ 'GENOME' }" ) );
563 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "assembly", value => "$cookie->{ 'ASSEMBLY' }" ) );
564 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "contig", value => "$cookie->{ 'CONTIG' }" ) );
566 return wantarray ? @html : \@html;
577 my ( @tracks, $i, @features, $feat, $elem, $file, $surface, $cr, @html, @img );
579 push @features, [ Maasha::BBrowser::Track::track_ruler( $cookie ) ];
580 push @features, [ Maasha::BBrowser::Track::track_seq( $cookie ) ];
582 @tracks = Maasha::BBrowser::Track::path_tracks( $cookie );
584 for ( $i = 0; $i < @tracks; $i++ )
586 $cookie->{ 'FEAT_COLOR' } = Maasha::BBrowser::Draw::palette( $i );
588 push @features, [ Maasha::BBrowser::Track::track_feature( $tracks[ $i ], $cookie ) ];
591 $file = "fisk.png"; # FIXME
593 $surface = Cairo::ImageSurface->create( 'argb32', $cookie->{ 'IMG_WIDTH' }, $cookie->{ 'TRACK_OFFSET' } );
594 $cr = Cairo::Context->create( $surface );
596 foreach $feat ( @features ) {
597 Maasha::BBrowser::Draw::draw_feature( $cr, $feat ) if $feat;
600 Maasha::BBrowser::Draw::file_png( $surface, $file );
602 push @img, Maasha::XHTML::img(
604 alt => "Browser Tracks",
605 height => $cookie->{ 'TRACK_OFFSET' },
606 width => $cookie->{ 'IMG_WIDTH' },
608 usemap => "#browser_map"
611 push @img, Maasha::XHTML::map_beg( name => "browser_map", id => "browser_map" );
613 foreach $feat ( @features )
615 foreach $elem ( @{ $feat } )
617 next if $elem->{ 'type' } eq 'text';
619 push @img, Maasha::XHTML::area(
620 href => cookie_href( $cookie, "export" ) . "&Q_ID=$elem->{ 'id' }",
622 coords => "$elem->{ x1 }, $elem->{ y1 }, $elem->{ x2 }, $elem->{ y2 }", title => "$elem->{ 'title' }",
627 push @img, Maasha::XHTML::map_end();
629 push @html, Maasha::XHTML::p( txt => join( "\n", @img ) );
631 @html = Maasha::XHTML::div( txt => join( "\n", @html ), class => 'browse' );
633 return wantarray ? @html : \@html;
639 # Martin A. Hansen, November 2009.
641 # Returns a HTML section with export table.
643 my ( $cookie, # cookie hash
650 push @html, Maasha::XHTML::h2( txt => "Export", class => 'center' ); # TODO section_export!
652 return wantarray ? @html : \@html;
656 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<