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' } ne 'browse' )
391 push @html, Maasha::XHTML::h2( txt => "Select $cookie->{ 'PAGE' }", class => 'center' );
393 push @html, Maasha::XHTML::table_beg( summary => "Select table", align => 'center', cellpadding => '5px' );
395 foreach $item ( @{ $cookie->{ $list } } )
397 $cookie->{ uc $cookie->{ 'PAGE' } } = $item;
399 $href = page_href( $cookie, page_next( $cookie ) );
401 push @html, Maasha::XHTML::table_row_simple( tr => [ Maasha::XHTML::ln( txt => $item, href => $href ) ] );
404 push @html, Maasha::XHTML::table_end;
408 push @html, sec_navigate( $cookie );
409 push @html, sec_browse( $cookie );
412 return wantarray ? @html : \@html;
423 for ( $i = 0; $i < @{ $cookie->{ 'LIST_PAGES' } }; $i++ ) {
424 last if $cookie->{ 'PAGE' } eq $cookie->{ 'LIST_PAGES' }->[ $i ];
427 return $cookie->{ 'LIST_PAGES' }->[ $i + 1 ];
438 my ( @pages, $page, @row1, @row2, @html, $href, $txt );
440 @pages = @{ $cookie->{ 'LIST_PAGES' } };
442 pop @pages; # remove 'browse'
444 foreach $page ( @pages )
446 $href = page_href( $cookie, $page );
448 $txt = $cookie->{ uc $page } || "";
450 push @row1, Maasha::XHTML::ln( txt => $page, href => $href, class => 'inline' );
451 push @row2, Maasha::XHTML::p( txt => $txt, class => 'inline' );
453 last if $page eq $cookie->{ 'PAGE' };
456 push @html, Maasha::XHTML::table_beg( summary => "Taxonomy table", align => 'center', cellpadding => '5px' );
457 push @html, Maasha::XHTML::table_row_simple( tr => \@row1, align => 'center' );
458 push @html, Maasha::XHTML::table_row_simple( tr => \@row2, align => 'center' );
459 push @html, Maasha::XHTML::table_end;
461 return wantarray ? @html : \@html;
477 push @href, "$cookie->{ 'SCRIPT' }?page=$page";
478 push @href, "user=$cookie->{ 'USER' }" if $cookie->{ 'USER' };
479 last if $page eq 'user';
480 push @href, "clade=$cookie->{ 'CLADE' }" if $cookie->{ 'CLADE' };
481 last if $page eq 'clade';
482 push @href, "genome=$cookie->{ 'GENOME' }" if $cookie->{ 'GENOME' };
483 last if $page eq 'genome';
484 push @href, "assembly=$cookie->{ 'ASSEMBLY' }" if $cookie->{ 'ASSEMBLY' };
485 last if $page eq 'assembly';
486 push @href, "contig=$cookie->{ 'CONTIG' }" if $cookie->{ 'CONTIG' };
487 last if $page eq 'contig';
491 return join "&", @href;
504 push @html, Maasha::XHTML::table_beg( summary => "Navigation table", align => 'center' );
505 push @html, Maasha::XHTML::table_row_simple( tr => [ qw( Start End ) ], align => 'center' );
506 push @html, Maasha::XHTML::table_row_simple( tr => [
507 Maasha::XHTML::text( name => "nav_start", value => Maasha::Calc::commify( $cookie->{ 'NAV_START' } ), size => 20 ),
508 Maasha::XHTML::text( name => "nav_end", value => Maasha::Calc::commify( $cookie->{ 'NAV_END' } ), size => 20 ),
509 Maasha::XHTML::submit( name => "nav_submit", value => "Submit" ),
511 push @html, Maasha::XHTML::table_end;
513 push @html, Maasha::XHTML::table_beg( summary => "Zoom table", align => 'center' );
514 push @html, Maasha::XHTML::table_row_simple( tr => [
515 Maasha::XHTML::p( txt => 'Move:' ),
516 Maasha::XHTML::submit( name => "move_left3", value => "<<<", title => "move 95% to the left" ),
517 Maasha::XHTML::submit( name => "move_left2", value => "<<", title => "move 47.5% to the left" ),
518 Maasha::XHTML::submit( name => "move_left1", value => "<", title => "move 10% to the left" ),
519 Maasha::XHTML::submit( name => "move_right1", value => ">", title => "move 10% to the rigth" ),
520 Maasha::XHTML::submit( name => "move_right2", value => ">>", title => "move 47.5% to the rigth" ),
521 Maasha::XHTML::submit( name => "move_right3", value => ">>>", title => "move 95% to the right" ),
522 Maasha::XHTML::p( txt => 'Zoom in:' ),
523 Maasha::XHTML::submit( name => "zoom_in1", value => "1.5x" ),
524 Maasha::XHTML::submit( name => "zoom_in2", value => "3x" ),
525 Maasha::XHTML::submit( name => "zoom_in3", value => "10x" ),
526 Maasha::XHTML::p( txt => 'Zoom out:' ),
527 Maasha::XHTML::submit( name => "zoom_out1", value => "1.5x" ),
528 Maasha::XHTML::submit( name => "zoom_out2", value => "3x" ),
529 Maasha::XHTML::submit( name => "zoom_out3", value => "10x" ),
531 push @html, Maasha::XHTML::table_end;
533 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "page", value => "browse" ) );
534 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "user", value => "$cookie->{ 'USER' }" ) );
535 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "clade", value => "$cookie->{ 'CLADE' }" ) );
536 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "genome", value => "$cookie->{ 'GENOME' }" ) );
537 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "assembly", value => "$cookie->{ 'ASSEMBLY' }" ) );
538 push @html, Maasha::XHTML::p( txt => Maasha::XHTML::hidden( name => "contig", value => "$cookie->{ 'CONTIG' }" ) );
540 return wantarray ? @html : \@html;
551 my ( $draw_metrics, @tracks, $i, @features, $feat, $elem, $file, $surface, $cr, @html, @img );
558 RULER_FONT_SIZE => 10,
559 RULER_COLOR => [ 0, 0, 0 ],
561 SEQ_COLOR => [ 0, 0, 0, ],
563 FEAT_COLOR => [ 0, 1, 0 ],
566 push @features, [ Maasha::KISS::Track::track_ruler( $draw_metrics, $cookie ) ];
567 push @features, [ Maasha::KISS::Track::track_seq( $draw_metrics, $cookie ) ];
569 @tracks = Maasha::KISS::Track::path_tracks( $cookie );
571 for ( $i = 0; $i < @tracks; $i++ )
573 push @features, [ Maasha::KISS::Track::track_feature( $tracks[ $i ], $draw_metrics, $cookie ) ];
575 $draw_metrics->{ 'FEAT_COLOR' } = [ 0, 0, 1 ];
580 $surface = Cairo::ImageSurface->create( 'argb32', $draw_metrics->{ 'IMG_WIDTH' }, $draw_metrics->{ 'TRACK_OFFSET' } );
581 $cr = Cairo::Context->create( $surface );
583 foreach $feat ( @features ) {
584 Maasha::KISS::Draw::draw_feature( $cr, $feat ) if $feat;
587 Maasha::KISS::Draw::file_png( $surface, $file );
589 push @img, Maasha::XHTML::img(
591 alt => "Browser Tracks",
592 height => $draw_metrics->{ 'TRACK_OFFSET' },
593 width => $draw_metrics->{ 'IMG_WIDTH' },
595 usemap => "#browser_map"
598 push @img, Maasha::XHTML::map_beg( name => "browser_map", id => "browser_map" );
600 foreach $feat ( @features )
602 foreach $elem ( @{ $feat } )
604 next if $elem->{ 'type' } eq 'text';
606 push @img, Maasha::XHTML::area(
607 href => "www.dmi.dk",
609 coords => "$elem->{ x1 }, $elem->{ y1 }, $elem->{ x2 }, $elem->{ y2 }", title => "$elem->{ 'title' }",
614 push @img, Maasha::XHTML::map_end();
616 push @html, Maasha::XHTML::p( txt => join( "\n", @img ) );
618 @html = Maasha::XHTML::div( txt => join( "\n", @html ), class => 'browse' );
620 return wantarray ? @html : \@html;
624 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
629 # push @html, Maasha::KISS::Draw::hdump( $features );
631 $t0 = Time::HiRes::gettimeofday();
632 $t1 = Time::HiRes::gettimeofday();