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 "/Users/maasha/biopieces/code_perl/"; # FIXME should be shell and httpd env variable
35 use Maasha::KISS::Track;
36 use Maasha::KISS::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::KISS::Draw::hdump( $cgi->param ); # DEBUG
63 # push @html, Maasha::KISS::Draw::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 ];
124 $path = "$cookie->{ 'DATA_DIR' }/Users";
126 $cookie->{ 'LIST_USER' } = Maasha::Filesys::ls_dirs_base( $path ) if -d $path;
128 $path .= "/$cookie->{ 'USER' }";
130 $cookie->{ 'LIST_CLADE' } = Maasha::Filesys::ls_dirs_base( $path ) if -d $path;
132 $path .= "/$cookie->{ 'CLADE' }";
134 $cookie->{ 'LIST_GENOME' } = Maasha::Filesys::ls_dirs_base( $path ) if -d $path;
136 $path .= "/$cookie->{ 'GENOME' }";
138 $cookie->{ 'LIST_ASSEMBLY' } = Maasha::Filesys::ls_dirs_base( $path ) if -d $path;
140 $path .= "/$cookie->{ 'ASSEMBLY' }";
142 $cookie->{ 'LIST_CONTIG' } = Maasha::Filesys::ls_dirs_base( $path ) if -d $path;
144 if ( $cookie->{ 'CONTIG' } )
146 cookie_start( $cookie );
147 cookie_end( $cookie );
148 cookie_zoom( $cookie );
149 cookie_move( $cookie );
152 return wantarray ? %{ $cookie } : $cookie;
158 # Martin A. Hansen, November 2009.
160 # Decommify the cookie value for NAV_START and adjust it to
161 # to prevent negative values.
163 my ( $cookie, # cookie hash
168 if ( defined $cookie->{ 'NAV_START' } )
170 $cookie->{ 'NAV_START' } =~ tr/,//d;
171 $cookie->{ 'NAV_START' } = 1 if $cookie->{ 'NAV_START' } <= 0;
175 $cookie->{ 'NAV_START' } = 1;
182 # Martin A. Hansen, November 2009.
184 # Decommify the cookie value for NAV_END and adjust it to prevent
185 # overshooting the max value for the contig size as determined
188 my ( $cookie, # cookie hash
195 $max = Maasha::Filesys::file_size( Maasha::KISS::Track::path_seq( $cookie ) );
197 if ( defined $cookie->{ 'NAV_END' } )
199 $cookie->{ 'NAV_END' } =~ tr/,//d;
200 $cookie->{ 'NAV_END' } = $max if $cookie->{ 'NAV_END' } > $max;
204 $cookie->{ 'NAV_END' } = $max;
211 # Martin A. Hansen, November 2009.
213 # Adjust the cookie values for NAV_START and NAV_END based
214 # on cookie ZOOM values.
216 my ( $cookie, # cookie hash
221 my ( $max, $dist, $new_dist, $dist_diff );
223 $max = Maasha::Filesys::file_size( Maasha::KISS::Track::path_seq( $cookie ) );
225 $dist = $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' };
227 if ( defined $cookie->{ 'ZOOM_IN1' } ) {
228 $new_dist = $dist / 1.5;
229 } elsif ( defined $cookie->{ 'ZOOM_IN2' } ) {
230 $new_dist = $dist / 3;
231 } elsif ( defined $cookie->{ 'ZOOM_IN3' } ) {
232 $new_dist = $dist / 10;
233 } elsif ( defined $cookie->{ 'ZOOM_OUT1' } ) {
234 $new_dist = $dist * 1.5;
235 } elsif ( defined $cookie->{ 'ZOOM_OUT2' } ) {
236 $new_dist = $dist * 3;
237 } elsif ( defined $cookie->{ 'ZOOM_OUT3' } ) {
238 $new_dist = $dist * 10;
243 $dist_diff = $dist - $new_dist;
245 $cookie->{ 'NAV_START' } = int( $cookie->{ 'NAV_START' } + ( $dist_diff / 2 ) );
246 $cookie->{ 'NAV_END' } = int( $cookie->{ 'NAV_END' } - ( $dist_diff / 2 ) );
248 $cookie->{ 'NAV_START' } = 1 if $cookie->{ 'NAV_START' } <= 0;
249 $cookie->{ 'NAV_END' } = $max if $cookie->{ 'NAV_END' } > $max;
256 # Martin A. Hansen, November 2009.
258 # Adjust the cookie values for NAV_START and NAV_END based
259 # on cookie MOVE values.
261 my ( $cookie, # cookie hash
264 my ( $max, $dist, $shift, $new_start, $new_end );
266 $max = Maasha::Filesys::file_size( Maasha::KISS::Track::path_seq( $cookie ) );
268 $dist = $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' };
270 if ( defined $cookie->{ 'MOVE_LEFT1' } ) {
271 $shift = -1 * $dist * 0.10;
272 } elsif ( defined $cookie->{ 'MOVE_LEFT2' } ) {
273 $shift = -1 * $dist * 0.475;
274 } elsif ( defined $cookie->{ 'MOVE_LEFT3' } ) {
275 $shift = -1 * $dist * 0.95;
276 } elsif ( defined $cookie->{ 'MOVE_RIGHT1' } ) {
277 $shift = $dist * 0.10;
278 } elsif ( defined $cookie->{ 'MOVE_RIGHT2' } ) {
279 $shift = $dist * 0.475;
280 } elsif ( defined $cookie->{ 'MOVE_RIGHT3' } ) {
281 $shift = $dist * 0.95;
286 $new_start = int( $cookie->{ 'NAV_START' } + $shift );
287 $new_end = int( $cookie->{ 'NAV_END' } + $shift );
289 if ( $new_start > 0 and $new_end < $max )
291 $cookie->{ 'NAV_START' } = $new_start;
292 $cookie->{ 'NAV_END' } = $new_end;
300 # Martin A. Hansen, November 2009.
302 # Returns the href for a taxonomy path terminated at a given
303 # page using information stored in a cookie.
305 my ( $cookie, # cookie hash
315 push @href, "$cookie->{ 'SCRIPT' }?page=$page";
316 push @href, "user=$cookie->{ 'USER' }" if $cookie->{ 'USER' };
317 last if $page eq 'user';
318 push @href, "clade=$cookie->{ 'CLADE' }" if $cookie->{ 'CLADE' };
319 last if $page eq 'clade';
320 push @href, "genome=$cookie->{ 'GENOME' }" if $cookie->{ 'GENOME' };
321 last if $page eq 'genome';
322 push @href, "assembly=$cookie->{ 'ASSEMBLY' }" if $cookie->{ 'ASSEMBLY' };
323 last if $page eq 'assembly';
324 push @href, "contig=$cookie->{ 'CONTIG' }" if $cookie->{ 'CONTIG' };
325 last if $page eq 'contig';
329 return join "&", @href;
335 # Martin A. Hansen, November 2009.
337 # Returns the next page in the taxonomy path.
339 my ( $cookie, # cookie hash
346 for ( $i = 0; $i < @{ $cookie->{ 'LIST_PAGES' } }; $i++ ) {
347 last if $cookie->{ 'PAGE' } eq $cookie->{ 'LIST_PAGES' }->[ $i ];
350 return $cookie->{ 'LIST_PAGES' }->[ $i + 1 ];
354 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PAGES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
359 # Martin A. Hansen, November 2009.
361 # Determines what page to render based on
362 # the cookie's PAGE setting.
364 my ( $cookie, # cookie hash
371 if ( $cookie->{ 'PAGE' } eq 'export' ) {
372 push @html, page_export( $cookie );
373 } elsif ( $cookie->{ 'PAGE' } eq 'browse' ) {
374 push @html, page_browse( $cookie );
376 push @html, page_taxonomy( $cookie );
379 return wantarray ? @html : \@html;
385 # Martin A. Hansen, November 2009.
387 # Renders the export page.
394 push @html, section_taxonomy_table( $cookie );
395 push @html, section_export( $cookie );
397 return wantarray ? @html : \@html;
403 # Martin A. Hansen, November 2009.
405 # Renders the browse page.
414 push @html, section_taxonomy_table( $cookie );
415 push @html, section_navigate( $cookie );
416 push @html, section_browse( $cookie );
418 return wantarray ? @html : \@html;
424 # Martin A. Hansen, November 2009.
426 # Renders the browse page.
428 my ( $cookie, # cookie hash
435 push @html, section_taxonomy_table( $cookie );
436 push @html, section_taxonomy_select( $cookie );
438 return wantarray ? @html : \@html;
442 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SECTIONS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
445 sub section_taxonomy_table
447 # Martin A. Hansen, November 2009.
449 # Returns a HTML section with a taxonomy table
450 # showing the location in the taxonomy and with
451 # links to browse the taxonomy.
453 my ( $cookie, # cookie hash
458 my ( $page, @row, @html, $href, $txt );
460 foreach $page ( @{ $cookie->{ 'LIST_PAGES' } } )
462 last if $page eq $cookie->{ 'PAGE' };
464 $href = cookie_href( $cookie, $page );
466 $txt = ": $cookie->{ uc $page }";
468 push @row, Maasha::XHTML::ln( txt => $page, href => $href, class => 'inline' );
469 push @row, Maasha::XHTML::p( txt => $txt, class => 'inline' );
472 push @html, Maasha::XHTML::table_beg( summary => "Taxonomy table", align => 'center', cellpadding => '5px' );
473 push @html, Maasha::XHTML::table_row_simple( tr => [ join( " ", @row ) ], align => 'center' );
474 push @html, Maasha::XHTML::table_end;
476 return wantarray ? @html : \@html;
480 sub section_taxonomy_select
482 # Martin A. Hansen, November 2009.
484 # Returns a HTML section with selection choices
485 # for navigating the taxonomy tree.
487 my ( $cookie, # cookie hash
492 my ( $list, @html, $item, $href );
494 $list = "LIST_" . uc $cookie->{ 'PAGE' };
496 push @html, Maasha::XHTML::h2( txt => "Select $cookie->{ 'PAGE' }", class => 'center' );
498 push @html, Maasha::XHTML::table_beg( summary => "Select table", align => 'center', cellpadding => '5px' );
500 foreach $item ( @{ $cookie->{ $list } } )
502 $cookie->{ uc $cookie->{ 'PAGE' } } = $item;
504 $href = cookie_href( $cookie, cookie_page_next( $cookie ) );
506 push @html, Maasha::XHTML::table_row_simple( tr => [ Maasha::XHTML::ln( txt => $item, href => $href ) ] );
509 push @html, Maasha::XHTML::table_end;
511 return wantarray ? @html : \@html;
517 # Martin A. Hansen, November 2009.
519 # Returns a HTML section for navigating in the browser window.
521 my ( $cookie, # cookie hash
528 push @html, Maasha::XHTML::table_beg( summary => "Navigation table", align => 'center' );
529 push @html, Maasha::XHTML::table_row_simple( tr => [
531 Maasha::XHTML::text( name => "nav_start", value => Maasha::Calc::commify( $cookie->{ 'NAV_START' } ), size => 20 ),
533 Maasha::XHTML::text( name => "nav_end", value => Maasha::Calc::commify( $cookie->{ 'NAV_END' } ), size => 20 ),
534 Maasha::XHTML::submit( name => "nav_submit", value => "Submit" ),
536 push @html, Maasha::XHTML::table_end;
538 push @html, Maasha::XHTML::table_beg( summary => "Zoom table", align => 'center' );
539 push @html, Maasha::XHTML::table_row_simple( tr => [
540 Maasha::XHTML::p( txt => 'Move:' ),
541 Maasha::XHTML::submit( name => "move_left3", value => "<<<", title => "move 95% to the left" ),
542 Maasha::XHTML::submit( name => "move_left2", value => "<<", title => "move 47.5% to the left" ),
543 Maasha::XHTML::submit( name => "move_left1", value => "<", title => "move 10% to the left" ),
544 Maasha::XHTML::submit( name => "move_right1", value => ">", title => "move 10% to the rigth" ),
545 Maasha::XHTML::submit( name => "move_right2", value => ">>", title => "move 47.5% to the rigth" ),
546 Maasha::XHTML::submit( name => "move_right3", value => ">>>", title => "move 95% to the right" ),
547 Maasha::XHTML::p( txt => 'Zoom in:' ),
548 Maasha::XHTML::submit( name => "zoom_in1", value => "1.5x" ),
549 Maasha::XHTML::submit( name => "zoom_in2", value => "3x" ),
550 Maasha::XHTML::submit( name => "zoom_in3", value => "10x" ),
551 Maasha::XHTML::p( txt => 'Zoom out:' ),
552 Maasha::XHTML::submit( name => "zoom_out1", value => "1.5x" ),
553 Maasha::XHTML::submit( name => "zoom_out2", value => "3x" ),
554 Maasha::XHTML::submit( name => "zoom_out3", value => "10x" ),
556 push @html, Maasha::XHTML::table_end;
558 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "page", value => "browse" ) );
559 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "user", value => "$cookie->{ 'USER' }" ) );
560 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "clade", value => "$cookie->{ 'CLADE' }" ) );
561 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "genome", value => "$cookie->{ 'GENOME' }" ) );
562 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "assembly", value => "$cookie->{ 'ASSEMBLY' }" ) );
563 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "contig", value => "$cookie->{ 'CONTIG' }" ) );
565 return wantarray ? @html : \@html;
576 my ( @tracks, $i, @features, $feat, $elem, $file, $surface, $cr, @html, @img );
578 push @features, [ Maasha::KISS::Track::track_ruler( $cookie ) ];
579 push @features, [ Maasha::KISS::Track::track_seq( $cookie ) ];
581 @tracks = Maasha::KISS::Track::path_tracks( $cookie );
583 for ( $i = 0; $i < @tracks; $i++ )
585 $cookie->{ 'FEAT_COLOR' } = Maasha::KISS::Draw::palette( $i );
587 push @features, [ Maasha::KISS::Track::track_feature( $tracks[ $i ], $cookie, $cookie ) ];
590 $file = "fisk.png"; # FIXME
592 $surface = Cairo::ImageSurface->create( 'argb32', $cookie->{ 'IMG_WIDTH' }, $cookie->{ 'TRACK_OFFSET' } );
593 $cr = Cairo::Context->create( $surface );
595 foreach $feat ( @features ) {
596 Maasha::KISS::Draw::draw_feature( $cr, $feat ) if $feat;
599 Maasha::KISS::Draw::file_png( $surface, $file );
601 push @img, Maasha::XHTML::img(
603 alt => "Browser Tracks",
604 height => $cookie->{ 'TRACK_OFFSET' },
605 width => $cookie->{ 'IMG_WIDTH' },
607 usemap => "#browser_map"
610 push @img, Maasha::XHTML::map_beg( name => "browser_map", id => "browser_map" );
612 foreach $feat ( @features )
614 foreach $elem ( @{ $feat } )
616 next if $elem->{ 'type' } eq 'text';
618 push @img, Maasha::XHTML::area(
619 href => cookie_href( $cookie, "export" ) . "&Q_ID=$elem->{ 'id' }",
621 coords => "$elem->{ x1 }, $elem->{ y1 }, $elem->{ x2 }, $elem->{ y2 }", title => "$elem->{ 'title' }",
626 push @img, Maasha::XHTML::map_end();
628 push @html, Maasha::XHTML::p( txt => join( "\n", @img ) );
630 @html = Maasha::XHTML::div( txt => join( "\n", @html ), class => 'browse' );
632 return wantarray ? @html : \@html;
638 # Martin A. Hansen, November 2009.
640 # Returns a HTML section with export table.
642 my ( $cookie, # cookie hash
649 push @html, Maasha::XHTML::h2( txt => "Export", class => 'center' ); # TODO section_export!
651 return wantarray ? @html : \@html;
655 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<