]> git.donarmstrong.com Git - biopieces.git/blob - www/cgi-bin/index.cgi
major work on KISS browser completed
[biopieces.git] / www / cgi-bin / 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 "/Users/maasha/biopieces/code_perl/";
27
28 use CGI;
29 use Cairo;
30 use Pango;
31 use Data::Dumper;
32 use Time::HiRes;
33 use Maasha::Common;
34 use Maasha::Filesys;
35 use Maasha::Calc;
36 use Maasha::XHTML;
37 use Maasha::Biopieces;
38 use Maasha::KISS::IO;
39 use Maasha::KISS::Track;
40 use Maasha::KISS::Draw;
41
42 my ( $cgi, $cookie, $script, @html );
43
44 $cgi      = new CGI;
45 $script   = Maasha::Common::get_scriptname();
46 $cookie   = cookie_default( $cgi );;
47
48 push @html, Maasha::XHTML::html_header(
49     cgi_header  => 1,
50     title       => "KISS Genome Browser",
51     css_file    => "kiss.css",
52     author      => "Martin A. Hansen, mail\@maasha.dk",
53     description => "Biopieces bacterial genome browser - KISS",
54     keywords    => [ qw( KISS Biopieces biopiece genome browser viewer bacterium bacteria prokaryote prokaryotes ) ],
55     no_cache    => 1,
56 );
57
58 push @html, Maasha::XHTML::h1( txt => "KISS Genome Browser", class => 'center' );
59 push @html, Maasha::XHTML::form_beg( action => $script, method => "get", enctype => "multipart/form-data" );
60
61 push @html, sec_navigate( $cookie );
62 push @html, sec_browse( $cookie );
63
64 push @html, Maasha::XHTML::form_end;
65 push @html, Maasha::XHTML::body_end;
66 push @html, Maasha::XHTML::html_end;
67
68 print "$_\n" foreach @html;
69
70
71 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
72
73
74 sub cookie_default
75 {
76     my ( $cgi,   # CGI object
77        ) = @_;
78
79     # Returns a hash
80
81     my ( $cookie );
82
83     $cookie = {};
84
85     $cookie->{ 'DATA_DIR' } = "Data";
86
87     cookie_cgi( $cookie, $cgi );
88     cookie_user( $cookie );
89     cookie_clade( $cookie );
90     cookie_genome( $cookie );
91     cookie_assembly( $cookie );
92     cookie_contig( $cookie );
93     cookie_start( $cookie );
94     cookie_end( $cookie );
95     cookie_zoom( $cookie );
96     cookie_move( $cookie );
97
98     # print STDERR Dumper( $cookie );
99
100     return wantarray ? %{ $cookie } : $cookie;
101 }
102
103
104 sub cookie_cgi
105 {
106     my ( $cookie,
107          $cgi,
108        ) = @_;
109
110     # Returns nothing.
111
112     $cookie->{ 'DEF_CLADE' }    = $cgi->param( 'nav_clade' );
113     $cookie->{ 'DEF_GENOME' }   = $cgi->param( 'nav_genome' );
114     $cookie->{ 'DEF_ASSEMBLY' } = $cgi->param( 'nav_assembly' );
115     $cookie->{ 'DEF_CONTIG' }   = $cgi->param( 'nav_contig' );
116     $cookie->{ 'NAV_START' }    = $cgi->param( 'nav_start' );
117     $cookie->{ 'NAV_END' }      = $cgi->param( 'nav_end' );
118     $cookie->{ 'ZOOM_IN1' }     = $cgi->param( 'zoom_in1' );
119     $cookie->{ 'ZOOM_IN2' }     = $cgi->param( 'zoom_in2' );
120     $cookie->{ 'ZOOM_IN3' }     = $cgi->param( 'zoom_in3' );
121     $cookie->{ 'ZOOM_OUT1' }    = $cgi->param( 'zoom_out1' );
122     $cookie->{ 'ZOOM_OUT2' }    = $cgi->param( 'zoom_out2' );
123     $cookie->{ 'ZOOM_OUT3' }    = $cgi->param( 'zoom_out3' );
124     $cookie->{ 'MOVE_LEFT1' }   = $cgi->param( 'move_left1' );
125     $cookie->{ 'MOVE_LEFT2' }   = $cgi->param( 'move_left2' );
126     $cookie->{ 'MOVE_LEFT3' }   = $cgi->param( 'move_left3' );
127     $cookie->{ 'MOVE_RIGHT1' }  = $cgi->param( 'move_right1' );
128     $cookie->{ 'MOVE_RIGHT2' }  = $cgi->param( 'move_right2' );
129     $cookie->{ 'MOVE_RIGHT3' }  = $cgi->param( 'move_right3' );
130 }
131
132
133 sub cookie_user
134 {
135     my ( $cookie,
136        ) = @_;
137
138     # Returns nothing.
139     
140     my ( @dirs, $dir );
141
142     @dirs = Maasha::Filesys::ls_dirs( "$cookie->{ 'DATA_DIR' }/Users" );
143
144     foreach $dir ( @dirs )
145     {
146         next if $dir =~ /\/\.\.?$/;
147
148         push @{ $cookie->{ 'LIST_USER' } }, ( split "/", $dir )[ -1 ];
149     }
150
151     $cookie->{ 'DEF_USER' } = $cookie->{ 'LIST_USER' }->[ 0 ];
152 }
153
154
155 sub cookie_clade
156 {
157     my ( $cookie,
158        ) = @_;
159
160     # Returns nothing.
161     
162     my ( $user, @dirs, $dir );
163
164     $user = $cookie->{ 'DEF_USER' };
165
166     @dirs = Maasha::Filesys::ls_dirs( "$cookie->{ 'DATA_DIR' }/Users/$user" );
167
168     foreach $dir ( @dirs )
169     {
170         next if $dir =~ /\/\.\.?$/;
171
172         push @{ $cookie->{ 'LIST_CLADE' } }, ( split "/", $dir )[ -1 ];
173     }
174
175     if ( not defined $cookie->{ 'DEF_CLADE' } ) {
176         $cookie->{ 'DEF_CLADE' } = $cookie->{ 'LIST_CLADE' }->[ 1 ];
177     }
178 }
179
180
181 sub cookie_genome
182 {
183     my ( $cookie,
184        ) = @_;
185
186     # Returns nothing.
187     
188     my ( $user, $clade, @dirs, $dir );
189
190     $user  = $cookie->{ 'DEF_USER' };
191     $clade = $cookie->{ 'DEF_CLADE' };
192
193     @dirs = Maasha::Filesys::ls_dirs( "$cookie->{ 'DATA_DIR' }/Users/$user/$clade" );
194
195     foreach $dir ( @dirs )
196     {
197         next if $dir =~ /\/\.\.?$/;
198
199         push @{ $cookie->{ 'LIST_GENOME' } }, ( split "/", $dir )[ -1 ];
200     }
201
202     if ( not defined $cookie->{ 'DEF_GENOME' } ) {
203         $cookie->{ 'DEF_GENOME' } = $cookie->{ 'LIST_GENOME' }->[ 0 ];
204     }
205 }
206
207
208 sub cookie_assembly
209 {
210     my ( $cookie,
211        ) = @_;
212
213     # Returns nothing.
214     
215     my ( $user, $clade, $genome, @dirs, $dir );
216
217     $user   = $cookie->{ 'DEF_USER' };
218     $clade  = $cookie->{ 'DEF_CLADE' };
219     $genome = $cookie->{ 'DEF_GENOME' };
220
221     @dirs = Maasha::Filesys::ls_dirs( "$cookie->{ 'DATA_DIR' }/Users/$user/$clade/$genome" );
222
223     foreach $dir ( @dirs )
224     {
225         next if $dir =~ /\/\.\.?$/;
226
227         push @{ $cookie->{ 'LIST_ASSEMBLY' } }, ( split "/", $dir )[ -1 ];
228     }
229
230     if ( not defined $cookie->{ 'DEF_ASSEMBLY' } ) {
231         $cookie->{ 'DEF_ASSEMBLY' } = $cookie->{ 'LIST_ASSEMBLY' }->[ 0 ];
232     }
233 }
234
235
236 sub cookie_contig
237 {
238     my ( $cookie,
239        ) = @_;
240
241     # Returns nothing.
242     
243     my ( $user, $clade, $genome, $assembly, @dirs, $dir );
244
245     $user     = $cookie->{ 'DEF_USER' };
246     $clade    = $cookie->{ 'DEF_CLADE' };
247     $genome   = $cookie->{ 'DEF_GENOME' };
248     $assembly = $cookie->{ 'DEF_ASSEMBLY' };
249
250     @dirs = Maasha::Filesys::ls_dirs( "$cookie->{ 'DATA_DIR' }/Users/$user/$clade/$genome/$assembly" );
251
252     foreach $dir ( @dirs )
253     {
254         next if $dir =~ /\/\.\.?$/;
255
256         push @{ $cookie->{ 'LIST_CONTIG' } }, ( split "/", $dir )[ -1 ];
257     }
258
259     if ( not defined $cookie->{ 'DEF_CONTIG' } ) {
260         $cookie->{ 'DEF_CONTIG' } = $cookie->{ 'LIST_CONTIG' }->[ 0 ];
261     }
262 }
263
264
265 sub cookie_start
266 {
267     my ( $cookie,
268        ) = @_;
269
270     # Returns nothing.
271
272     if ( defined $cookie->{ 'NAV_START' } )
273     {
274         $cookie->{ 'NAV_START' } =~ tr/,//d;
275         $cookie->{ 'NAV_START' } = 1 if $cookie->{ 'NAV_START' } <= 0;
276     }
277     else
278     {
279         $cookie->{ 'NAV_START' } = 1;
280     }
281 }
282
283
284 sub cookie_end
285 {
286     my ( $cookie,
287        ) = @_;
288
289     # Returns nothing.
290
291     my ( $max );
292     
293     $max = Maasha::Filesys::file_size( Maasha::KISS::Track::path_seq( $cookie ) );
294
295     if ( defined $cookie->{ 'NAV_END' } )
296     {
297         $cookie->{ 'NAV_END' } =~ tr/,//d;
298         $cookie->{ 'NAV_END' } = $max if $cookie->{ 'NAV_END' } > $max;
299     }
300     else
301     {
302         $cookie->{ 'NAV_END' } = $max;
303     }
304 }
305
306
307 sub cookie_zoom
308 {
309     my ( $cookie,
310        ) = @_;
311
312     # Returns nothing.
313
314     my ( $max, $dist, $new_dist, $dist_diff );
315
316     $max = Maasha::Filesys::file_size( Maasha::KISS::Track::path_seq( $cookie ) );
317
318     $dist = $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' };
319
320     if ( defined $cookie->{ 'ZOOM_IN1' } ) {
321         $new_dist = $dist / 1.5;
322     } elsif ( defined $cookie->{ 'ZOOM_IN2' } ) {
323         $new_dist = $dist / 3;
324     } elsif ( defined $cookie->{ 'ZOOM_IN3' } ) {
325         $new_dist = $dist / 10;
326     } elsif ( defined $cookie->{ 'ZOOM_OUT1' } ) {
327         $new_dist = $dist * 1.5;
328     } elsif ( defined $cookie->{ 'ZOOM_OUT2' } ) {
329         $new_dist = $dist * 3;
330     } elsif ( defined $cookie->{ 'ZOOM_OUT3' } ) {
331         $new_dist = $dist * 10;
332     }
333
334     if ( $new_dist )
335     {
336         $dist_diff = $dist - $new_dist;
337
338         $cookie->{ 'NAV_START' } = int( $cookie->{ 'NAV_START' } + ( $dist_diff / 2 ) );
339         $cookie->{ 'NAV_END' }   = int( $cookie->{ 'NAV_END' }   - ( $dist_diff / 2 ) );
340
341         $cookie->{ 'NAV_START' } = 1     if $cookie->{ 'NAV_START' } <= 0;
342         $cookie->{ 'NAV_END' }   = $max if $cookie->{ 'NAV_END' } > $max;
343     }
344 }
345
346
347 sub cookie_move
348 {
349     my ( $cookie,
350        ) = @_;
351
352     my ( $max, $dist, $shift, $new_start, $new_end );
353
354     $max = Maasha::Filesys::file_size( Maasha::KISS::Track::path_seq( $cookie ) );
355
356     $dist = $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' };
357
358     if ( defined $cookie->{ 'MOVE_LEFT1' } ) {
359         $shift = -1 * $dist * 0.10;
360     } elsif ( defined $cookie->{ 'MOVE_LEFT2' } ) {
361         $shift = -1 * $dist * 0.475;
362     } elsif ( defined $cookie->{ 'MOVE_LEFT3' } ) {
363         $shift = -1 * $dist * 0.95;
364     } elsif ( defined $cookie->{ 'MOVE_RIGHT1' } ) {
365         $shift = $dist * 0.10;
366     } elsif ( defined $cookie->{ 'MOVE_RIGHT2' } ) {
367         $shift = $dist * 0.475;
368     } elsif ( defined $cookie->{ 'MOVE_RIGHT3' } ) {
369         $shift = $dist * 0.95;
370     }
371
372     if ( $shift )
373     {
374         $new_start = int( $cookie->{ 'NAV_START' } + $shift );
375         $new_end   = int( $cookie->{ 'NAV_END' }   + $shift );
376
377         if ( $new_start > 0 and $new_end < $max )
378         {
379             $cookie->{ 'NAV_START' } = $new_start;
380             $cookie->{ 'NAV_END' }   = $new_end;
381         }
382     }
383 }
384
385
386 sub sec_navigate
387 {
388     my ( $cookie,
389        ) = @_;
390
391     # Returns a list.
392
393     my ( @html );
394
395     push @html, Maasha::XHTML::table_beg( summary => "Navigation table", align => 'center' );
396     push @html, Maasha::XHTML::table_row_simple( tr => [ qw( Clade Genome Assembly Contig Start End ) ], align => 'center' );
397     push @html, Maasha::XHTML::table_row_simple( tr => [
398         Maasha::XHTML::menu( name => "nav_clade",    options => $cookie->{ 'LIST_CLADE' },    selected => $cookie->{ 'DEF_CLADE' } ),
399         Maasha::XHTML::menu( name => "nav_genome",   options => $cookie->{ 'LIST_GENOME' },   selected => $cookie->{ 'DEF_GENOME' } ),
400         Maasha::XHTML::menu( name => "nav_assembly", options => $cookie->{ 'LIST_ASSEMBLY' }, selected => $cookie->{ 'DEF_ASSEMBLY' } ),
401         Maasha::XHTML::menu( name => "nav_contig",   options => $cookie->{ 'LIST_CONTIG' },   selected => $cookie->{ 'DEF_CONTIG' } ),
402         Maasha::XHTML::text( name => "nav_start", value => Maasha::Calc::commify( $cookie->{ 'NAV_START' } ), size => 20 ),
403         Maasha::XHTML::text( name => "nav_end",   value => Maasha::Calc::commify( $cookie->{ 'NAV_END' } ),   size => 20 ), 
404         Maasha::XHTML::submit( name => "nav_submit", value => "Submit" ),
405     ] );
406     push @html, Maasha::XHTML::table_end;
407
408     push @html, Maasha::XHTML::table_beg( summary => "Zoom table", align => 'center' );
409     push @html, Maasha::XHTML::table_row_simple( tr => [
410         Maasha::XHTML::p( txt => 'Move:' ),
411         Maasha::XHTML::submit( name => "move_left3",  value => "<<<", title => "move 95% to the left" ),
412         Maasha::XHTML::submit( name => "move_left2",  value => "<<",  title => "move 47.5% to the left" ),
413         Maasha::XHTML::submit( name => "move_left1",  value => "<",   title => "move 10% to the left" ),
414         Maasha::XHTML::submit( name => "move_right1", value => ">",   title => "move 10% to the rigth" ),
415         Maasha::XHTML::submit( name => "move_right2", value => ">>",  title => "move 47.5% to the rigth" ),
416         Maasha::XHTML::submit( name => "move_right3", value => ">>>", title => "move 95% to the right" ),
417         Maasha::XHTML::p( txt => 'Zoom in:' ),
418         Maasha::XHTML::submit( name => "zoom_in1", value => "1.5x" ),
419         Maasha::XHTML::submit( name => "zoom_in2", value => "3x" ),
420         Maasha::XHTML::submit( name => "zoom_in3", value => "10x" ),
421         Maasha::XHTML::p( txt => 'Zoom out:' ),
422         Maasha::XHTML::submit( name => "zoom_out1", value => "1.5x" ),
423         Maasha::XHTML::submit( name => "zoom_out2", value => "3x" ),
424         Maasha::XHTML::submit( name => "zoom_out3", value => "10x" ),
425     ] );
426     push @html, Maasha::XHTML::table_end;
427
428     @html = Maasha::XHTML::div( txt => join( "\n", @html ), class => 'navigate' );
429
430     return wantarray ? @html : \@html;
431 }
432
433
434 sub sec_browse
435 {
436     my ( $cookie,
437        ) = @_;
438
439     # Returns a list.
440
441     my ( $draw_metrics, @tracks, @features, $feat, $elem, $file, $surface, $cr, @html, @img );
442
443     $draw_metrics = {
444         IMG_WIDTH       => 1200,
445         IMG_HEIGHT      => 800,
446         TRACK_OFFSET    => 20,
447         TRACK_SPACE     => 20,
448         RULER_FONT_SIZE => 10,
449         RULER_COLOR     => 'black',
450         SEQ_FONT_SIZE   => 10,
451         SEQ_COLOR       => 'black',
452         FEAT_WIDTH      => 5,
453     };
454     
455     push @features, [ Maasha::KISS::Track::track_ruler( $draw_metrics, $cookie ) ];
456     push @features, [ Maasha::KISS::Track::track_seq( $draw_metrics, $cookie ) ];
457
458     @tracks = Maasha::KISS::Track::path_tracks( $cookie );
459
460     map { push @features, [ Maasha::KISS::Track::track_feature( $_, $draw_metrics, $cookie ) ] } @tracks;
461
462     $file = "fisk.png";
463
464     $surface = Cairo::ImageSurface->create( 'argb32', $draw_metrics->{ 'IMG_WIDTH' }, $draw_metrics->{ 'TRACK_OFFSET' } );
465     $cr      = Cairo::Context->create( $surface );
466
467     foreach $feat ( @features ) {
468         Maasha::KISS::Draw::draw_feature( $cr, $feat ) if $feat;
469     }
470
471     Maasha::KISS::Draw::file_png( $surface, $file );
472
473     push @img, Maasha::XHTML::img(
474         src    => $file,
475         alt    => "Browser Tracks",
476         height => $draw_metrics->{ 'TRACK_OFFSET' },
477         width  => $draw_metrics->{ 'IMG_WIDTH' },
478         id     => "browser_map",
479         usemap => "#browser_map"
480     );
481
482     push @img, Maasha::XHTML::map_beg( name => "browser_map", id => "browser_map" );
483
484     foreach $feat ( @features )
485     {
486         foreach $elem ( @{ $feat } )
487         {
488             next if not $elem->{ 'type' } eq 'line';
489
490             push @img, Maasha::XHTML::area(
491                 href   => "www.dmi.dk",
492                 shape  => "rect",
493                 coords => "$elem->{ x1 }, $elem->{ y1 }, $elem->{ x2 }, $elem->{ y2 }", title => "$elem->{ 'title' }",
494             );
495         }
496     }
497
498     push @img, Maasha::XHTML::map_end();
499
500     push @html, Maasha::XHTML::p( txt => join( "\n", @img ) );
501
502     @html = Maasha::XHTML::div( txt => join( "\n", @html ), class => 'browse' );
503
504     return wantarray ? @html : \@html;
505 }
506
507
508 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
509
510
511 __END__
512
513     # push @html, Maasha::KISS::Draw::hdump( $entries );
514     # push @html, Maasha::KISS::Draw::hdump( $features );
515
516     $t0 = Time::HiRes::gettimeofday();
517     $t1 = Time::HiRes::gettimeofday();