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/";
36 use Maasha::KISS::Track;
37 use Maasha::KISS::Draw;
39 my ( $cgi, $cookie, @html );
42 $cookie = cookie_default( $cgi );;
44 push @html, Maasha::XHTML::html_header(
46 title => "KISS Genome Browser",
47 css_file => "kiss.css",
48 author => "Martin A. Hansen, mail\@maasha.dk",
49 description => "Biopieces bacterial genome browser - KISS",
50 keywords => [ qw( KISS Biopieces biopiece genome browser viewer bacterium bacteria prokaryote prokaryotes ) ],
54 push @html, Maasha::XHTML::h1( txt => "KISS Genome Browser", class => 'center' );
55 push @html, Maasha::XHTML::form_beg( action => $cookie->{ 'SCRIPT' }, method => "get", enctype => "multipart/form-data" );
57 push @html, page( $cookie );
59 push @html, Maasha::XHTML::form_end;
60 push @html, Maasha::XHTML::body_end;
61 push @html, Maasha::XHTML::html_end;
63 # push @html, Maasha::KISS::Draw::hdump( $cgi->param );
64 # push @html, Maasha::KISS::Draw::hdump( $cookie );
66 print "$_\n" foreach @html;
69 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
74 my ( $cgi, # CGI object
79 my ( $cookie, $path );
83 $cookie->{ 'SCRIPT' } = Maasha::Common::get_scriptname();
84 $cookie->{ 'DATA_DIR' } = "Data";
85 $cookie->{ 'LIST_PAGES' } = [ qw( user clade genome assembly contig browse ) ];
86 $cookie->{ 'PAGE' } = $cgi->param( 'page' ) || 'user';
87 $cookie->{ 'USER' } = $cgi->param( 'user' ) || '';
88 $cookie->{ 'CLADE' } = $cgi->param( 'clade' ) || '';
89 $cookie->{ 'GENOME' } = $cgi->param( 'genome' ) || '';
90 $cookie->{ 'ASSEMBLY' } = $cgi->param( 'assembly' ) || '';
91 $cookie->{ 'CONTIG' } = $cgi->param( 'contig' ) || '';
92 $cookie->{ 'NAV_START' } = $cgi->param( 'nav_start' );
93 $cookie->{ 'NAV_END' } = $cgi->param( 'nav_end' );
94 $cookie->{ 'ZOOM_IN1' } = $cgi->param( 'zoom_in1' );
95 $cookie->{ 'ZOOM_IN2' } = $cgi->param( 'zoom_in2' );
96 $cookie->{ 'ZOOM_IN3' } = $cgi->param( 'zoom_in3' );
97 $cookie->{ 'ZOOM_OUT1' } = $cgi->param( 'zoom_out1' );
98 $cookie->{ 'ZOOM_OUT2' } = $cgi->param( 'zoom_out2' );
99 $cookie->{ 'ZOOM_OUT3' } = $cgi->param( 'zoom_out3' );
100 $cookie->{ 'MOVE_LEFT1' } = $cgi->param( 'move_left1' );
101 $cookie->{ 'MOVE_LEFT2' } = $cgi->param( 'move_left2' );
102 $cookie->{ 'MOVE_LEFT3' } = $cgi->param( 'move_left3' );
103 $cookie->{ 'MOVE_RIGHT1' } = $cgi->param( 'move_right1' );
104 $cookie->{ 'MOVE_RIGHT2' } = $cgi->param( 'move_right2' );
105 $cookie->{ 'MOVE_RIGHT3' } = $cgi->param( 'move_right3' );
107 $path = "$cookie->{ 'DATA_DIR' }/Users";
109 $cookie->{ 'LIST_USER' } = ls_dir_base( $path ) if -d $path;
111 $path .= "/$cookie->{ 'USER' }";
113 $cookie->{ 'LIST_CLADE' } = ls_dir_base( $path ) if -d $path;
115 $path .= "/$cookie->{ 'CLADE' }";
117 $cookie->{ 'LIST_GENOME' } = ls_dir_base( $path ) if -d $path;
119 $path .= "/$cookie->{ 'GENOME' }";
121 $cookie->{ 'LIST_ASSEMBLY' } = ls_dir_base( $path ) if -d $path;
123 $path .= "/$cookie->{ 'ASSEMBLY' }";
125 $cookie->{ 'LIST_CONTIG' } = ls_dir_base( $path ) if -d $path;
127 if ( $cookie->{ 'CONTIG' } )
129 cookie_start( $cookie );
130 cookie_end( $cookie );
131 cookie_zoom( $cookie );
132 cookie_move( $cookie );
135 return wantarray ? %{ $cookie } : $cookie;
146 my ( @dirs, $dir, @list );
148 @dirs = Maasha::Filesys::ls_dirs( $path );
150 foreach $dir ( @dirs )
152 next if $dir =~ /\/\.\.?$/;
154 push @list, ( split "/", $dir )[ -1 ];
157 return wantarray ? @list : \@list;
168 my ( $user, @dirs, $dir );
170 $user = $cookie->{ 'USER' };
172 @dirs = Maasha::Filesys::ls_dirs( "$cookie->{ 'DATA_DIR' }/Users/$user" );
174 foreach $dir ( @dirs )
176 next if $dir =~ /\/\.\.?$/;
178 push @{ $cookie->{ 'LIST_CLADE' } }, ( split "/", $dir )[ -1 ];
190 my ( $user, $clade, @dirs, $dir );
192 $user = $cookie->{ 'USER' };
193 $clade = $cookie->{ 'CLADE' };
195 @dirs = Maasha::Filesys::ls_dirs( "$cookie->{ 'DATA_DIR' }/Users/$user/$clade" );
197 foreach $dir ( @dirs )
199 next if $dir =~ /\/\.\.?$/;
201 push @{ $cookie->{ 'LIST_GENOME' } }, ( split "/", $dir )[ -1 ];
213 my ( $user, $clade, $genome, @dirs, $dir );
215 $user = $cookie->{ 'USER' };
216 $clade = $cookie->{ 'CLADE' };
217 $genome = $cookie->{ 'GENOME' };
219 @dirs = Maasha::Filesys::ls_dirs( "$cookie->{ 'DATA_DIR' }/Users/$user/$clade/$genome" );
221 foreach $dir ( @dirs )
223 next if $dir =~ /\/\.\.?$/;
225 push @{ $cookie->{ 'LIST_ASSEMBLY' } }, ( split "/", $dir )[ -1 ];
237 my ( $user, $clade, $genome, $assembly, @dirs, $dir );
239 $user = $cookie->{ 'USER' };
240 $clade = $cookie->{ 'CLADE' };
241 $genome = $cookie->{ 'GENOME' };
242 $assembly = $cookie->{ 'ASSEMBLY' };
244 @dirs = Maasha::Filesys::ls_dirs( "$cookie->{ 'DATA_DIR' }/Users/$user/$clade/$genome/$assembly" );
246 foreach $dir ( @dirs )
248 next if $dir =~ /\/\.\.?$/;
250 push @{ $cookie->{ 'LIST_CONTIG' } }, ( split "/", $dir )[ -1 ];
262 if ( defined $cookie->{ 'NAV_START' } )
264 $cookie->{ 'NAV_START' } =~ tr/,//d;
265 $cookie->{ 'NAV_START' } = 1 if $cookie->{ 'NAV_START' } <= 0;
269 $cookie->{ 'NAV_START' } = 1;
283 $max = Maasha::Filesys::file_size( Maasha::KISS::Track::path_seq( $cookie ) );
285 if ( defined $cookie->{ 'NAV_END' } )
287 $cookie->{ 'NAV_END' } =~ tr/,//d;
288 $cookie->{ 'NAV_END' } = $max if $cookie->{ 'NAV_END' } > $max;
292 $cookie->{ 'NAV_END' } = $max;
304 my ( $max, $dist, $new_dist, $dist_diff );
306 $max = Maasha::Filesys::file_size( Maasha::KISS::Track::path_seq( $cookie ) );
308 $dist = $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' };
310 if ( defined $cookie->{ 'ZOOM_IN1' } ) {
311 $new_dist = $dist / 1.5;
312 } elsif ( defined $cookie->{ 'ZOOM_IN2' } ) {
313 $new_dist = $dist / 3;
314 } elsif ( defined $cookie->{ 'ZOOM_IN3' } ) {
315 $new_dist = $dist / 10;
316 } elsif ( defined $cookie->{ 'ZOOM_OUT1' } ) {
317 $new_dist = $dist * 1.5;
318 } elsif ( defined $cookie->{ 'ZOOM_OUT2' } ) {
319 $new_dist = $dist * 3;
320 } elsif ( defined $cookie->{ 'ZOOM_OUT3' } ) {
321 $new_dist = $dist * 10;
326 $dist_diff = $dist - $new_dist;
328 $cookie->{ 'NAV_START' } = int( $cookie->{ 'NAV_START' } + ( $dist_diff / 2 ) );
329 $cookie->{ 'NAV_END' } = int( $cookie->{ 'NAV_END' } - ( $dist_diff / 2 ) );
331 $cookie->{ 'NAV_START' } = 1 if $cookie->{ 'NAV_START' } <= 0;
332 $cookie->{ 'NAV_END' } = $max if $cookie->{ 'NAV_END' } > $max;
342 my ( $max, $dist, $shift, $new_start, $new_end );
344 $max = Maasha::Filesys::file_size( Maasha::KISS::Track::path_seq( $cookie ) );
346 $dist = $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' };
348 if ( defined $cookie->{ 'MOVE_LEFT1' } ) {
349 $shift = -1 * $dist * 0.10;
350 } elsif ( defined $cookie->{ 'MOVE_LEFT2' } ) {
351 $shift = -1 * $dist * 0.475;
352 } elsif ( defined $cookie->{ 'MOVE_LEFT3' } ) {
353 $shift = -1 * $dist * 0.95;
354 } elsif ( defined $cookie->{ 'MOVE_RIGHT1' } ) {
355 $shift = $dist * 0.10;
356 } elsif ( defined $cookie->{ 'MOVE_RIGHT2' } ) {
357 $shift = $dist * 0.475;
358 } elsif ( defined $cookie->{ 'MOVE_RIGHT3' } ) {
359 $shift = $dist * 0.95;
364 $new_start = int( $cookie->{ 'NAV_START' } + $shift );
365 $new_end = int( $cookie->{ 'NAV_END' } + $shift );
367 if ( $new_start > 0 and $new_end < $max )
369 $cookie->{ 'NAV_START' } = $new_start;
370 $cookie->{ 'NAV_END' } = $new_end;
383 my ( @html, $list, $item, $href );
385 push @html, breadcrumb( $cookie );
387 $list = "LIST_" . uc $cookie->{ 'PAGE' };
389 if ( $cookie->{ 'PAGE' } eq 'export' )
391 push @html, Maasha::XHTML::h2( txt => "Export", class => 'center' );
393 elsif ( $cookie->{ 'PAGE' } ne 'browse' )
395 push @html, Maasha::XHTML::h2( txt => "Select $cookie->{ 'PAGE' }", class => 'center' );
397 push @html, Maasha::XHTML::table_beg( summary => "Select table", align => 'center', cellpadding => '5px' );
399 foreach $item ( @{ $cookie->{ $list } } )
401 $cookie->{ uc $cookie->{ 'PAGE' } } = $item;
403 $href = page_href( $cookie, page_next( $cookie ) );
405 push @html, Maasha::XHTML::table_row_simple( tr => [ Maasha::XHTML::ln( txt => $item, href => $href ) ] );
408 push @html, Maasha::XHTML::table_end;
412 push @html, sec_navigate( $cookie );
413 push @html, sec_browse( $cookie );
416 return wantarray ? @html : \@html;
427 for ( $i = 0; $i < @{ $cookie->{ 'LIST_PAGES' } }; $i++ ) {
428 last if $cookie->{ 'PAGE' } eq $cookie->{ 'LIST_PAGES' }->[ $i ];
431 return $cookie->{ 'LIST_PAGES' }->[ $i + 1 ];
442 my ( @pages, $page, @row1, @row2, @html, $href, $txt );
444 @pages = @{ $cookie->{ 'LIST_PAGES' } };
446 pop @pages; # remove 'browse'
448 foreach $page ( @pages )
450 $href = page_href( $cookie, $page );
452 $txt = $cookie->{ uc $page } || "";
454 push @row1, Maasha::XHTML::ln( txt => $page, href => $href, class => 'inline' );
455 push @row2, Maasha::XHTML::p( txt => $txt, class => 'inline' );
457 last if $page eq $cookie->{ 'PAGE' };
460 push @html, Maasha::XHTML::table_beg( summary => "Taxonomy table", align => 'center', cellpadding => '5px' );
461 push @html, Maasha::XHTML::table_row_simple( tr => \@row1, align => 'center' );
462 push @html, Maasha::XHTML::table_row_simple( tr => \@row2, align => 'center' );
463 push @html, Maasha::XHTML::table_end;
465 return wantarray ? @html : \@html;
481 push @href, "$cookie->{ 'SCRIPT' }?page=$page";
482 push @href, "user=$cookie->{ 'USER' }" if $cookie->{ 'USER' };
483 last if $page eq 'user';
484 push @href, "clade=$cookie->{ 'CLADE' }" if $cookie->{ 'CLADE' };
485 last if $page eq 'clade';
486 push @href, "genome=$cookie->{ 'GENOME' }" if $cookie->{ 'GENOME' };
487 last if $page eq 'genome';
488 push @href, "assembly=$cookie->{ 'ASSEMBLY' }" if $cookie->{ 'ASSEMBLY' };
489 last if $page eq 'assembly';
490 push @href, "contig=$cookie->{ 'CONTIG' }" if $cookie->{ 'CONTIG' };
491 last if $page eq 'contig';
495 return join "&", @href;
508 push @html, Maasha::XHTML::table_beg( summary => "Navigation table", align => 'center' );
509 push @html, Maasha::XHTML::table_row_simple( tr => [ qw( Start End ) ], align => 'center' );
510 push @html, Maasha::XHTML::table_row_simple( tr => [
511 Maasha::XHTML::text( name => "nav_start", value => Maasha::Calc::commify( $cookie->{ 'NAV_START' } ), size => 20 ),
512 Maasha::XHTML::text( name => "nav_end", value => Maasha::Calc::commify( $cookie->{ 'NAV_END' } ), size => 20 ),
513 Maasha::XHTML::submit( name => "nav_submit", value => "Submit" ),
515 push @html, Maasha::XHTML::table_end;
517 push @html, Maasha::XHTML::table_beg( summary => "Zoom table", align => 'center' );
518 push @html, Maasha::XHTML::table_row_simple( tr => [
519 Maasha::XHTML::p( txt => 'Move:' ),
520 Maasha::XHTML::submit( name => "move_left3", value => "<<<", title => "move 95% to the left" ),
521 Maasha::XHTML::submit( name => "move_left2", value => "<<", title => "move 47.5% to the left" ),
522 Maasha::XHTML::submit( name => "move_left1", value => "<", title => "move 10% to the left" ),
523 Maasha::XHTML::submit( name => "move_right1", value => ">", title => "move 10% to the rigth" ),
524 Maasha::XHTML::submit( name => "move_right2", value => ">>", title => "move 47.5% to the rigth" ),
525 Maasha::XHTML::submit( name => "move_right3", value => ">>>", title => "move 95% to the right" ),
526 Maasha::XHTML::p( txt => 'Zoom in:' ),
527 Maasha::XHTML::submit( name => "zoom_in1", value => "1.5x" ),
528 Maasha::XHTML::submit( name => "zoom_in2", value => "3x" ),
529 Maasha::XHTML::submit( name => "zoom_in3", value => "10x" ),
530 Maasha::XHTML::p( txt => 'Zoom out:' ),
531 Maasha::XHTML::submit( name => "zoom_out1", value => "1.5x" ),
532 Maasha::XHTML::submit( name => "zoom_out2", value => "3x" ),
533 Maasha::XHTML::submit( name => "zoom_out3", value => "10x" ),
535 push @html, Maasha::XHTML::table_end;
537 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "page", value => "browse" ) );
538 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "user", value => "$cookie->{ 'USER' }" ) );
539 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "clade", value => "$cookie->{ 'CLADE' }" ) );
540 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "genome", value => "$cookie->{ 'GENOME' }" ) );
541 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "assembly", value => "$cookie->{ 'ASSEMBLY' }" ) );
542 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "contig", value => "$cookie->{ 'CONTIG' }" ) );
544 return wantarray ? @html : \@html;
555 my ( $draw_metrics, @tracks, $i, @features, $feat, $elem, $file, $surface, $cr, @html, @img );
562 RULER_FONT_SIZE => 10,
563 RULER_COLOR => [ 0, 0, 0 ],
565 SEQ_COLOR => [ 0, 0, 0, ],
567 FEAT_COLOR => [ 0, 1, 0 ],
570 push @features, [ Maasha::KISS::Track::track_ruler( $draw_metrics, $cookie ) ];
571 push @features, [ Maasha::KISS::Track::track_seq( $draw_metrics, $cookie ) ];
573 @tracks = Maasha::KISS::Track::path_tracks( $cookie );
575 for ( $i = 0; $i < @tracks; $i++ )
577 $draw_metrics->{ 'FEAT_COLOR' } = palette( $i );
579 push @features, [ Maasha::KISS::Track::track_feature( $tracks[ $i ], $draw_metrics, $cookie ) ];
584 $surface = Cairo::ImageSurface->create( 'argb32', $draw_metrics->{ 'IMG_WIDTH' }, $draw_metrics->{ 'TRACK_OFFSET' } );
585 $cr = Cairo::Context->create( $surface );
587 foreach $feat ( @features ) {
588 Maasha::KISS::Draw::draw_feature( $cr, $feat ) if $feat;
591 Maasha::KISS::Draw::file_png( $surface, $file );
593 push @img, Maasha::XHTML::img(
595 alt => "Browser Tracks",
596 height => $draw_metrics->{ 'TRACK_OFFSET' },
597 width => $draw_metrics->{ 'IMG_WIDTH' },
599 usemap => "#browser_map"
602 push @img, Maasha::XHTML::map_beg( name => "browser_map", id => "browser_map" );
604 foreach $feat ( @features )
606 foreach $elem ( @{ $feat } )
608 next if $elem->{ 'type' } eq 'text';
610 push @img, Maasha::XHTML::area(
611 href => page_href( $cookie, "export" ) . "&Q_ID=$elem->{ 'id' }",
613 coords => "$elem->{ x1 }, $elem->{ y1 }, $elem->{ x2 }, $elem->{ y2 }", title => "$elem->{ 'title' }",
618 push @img, Maasha::XHTML::map_end();
620 push @html, Maasha::XHTML::p( txt => join( "\n", @img ) );
622 @html = Maasha::XHTML::div( txt => join( "\n", @html ), class => 'browse' );
624 return wantarray ? @html : \@html;
633 my ( $palette, $color );
645 $color = $palette->[ $i ];
647 map { $_ /= 255 } @{ $color };
652 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
657 # push @html, Maasha::KISS::Draw::hdump( $features );
659 $t0 = Time::HiRes::gettimeofday();
660 $t1 = Time::HiRes::gettimeofday();