]> git.donarmstrong.com Git - biopieces.git/blob - www/cgi-bin/index.cgi
01a001138e1f08e4162a35d47e93c0f043171bf3
[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, $database, $user, $password, $dbh, $script, @html );
43
44 $cgi = new CGI;
45
46 $database = 'S_aur_COL';
47 $user     = Maasha::Biopieces::biopiecesrc( "MYSQL_USER" );
48 $password = Maasha::Biopieces::biopiecesrc( "MYSQL_PASSWORD" );
49
50 $dbh = Maasha::SQL::connect( $database, $user, $password );
51
52 $script = Maasha::Common::get_scriptname();
53
54 push @html, Maasha::XHTML::html_header(
55     cgi_header  => 1,
56     title       => "KISS Genome Browser",
57     css_file    => "kiss.css",
58     author      => "Martin A. Hansen, mail\@maasha.dk",
59     description => "Biopieces bacterial genome browser - KISS",
60     keywords    => [ qw( KISS Biopieces biopiece genome browser viewer bacterium bacteria prokaryote prokaryotes ) ],
61     no_cache    => 1,
62 );
63
64 push @html, Maasha::XHTML::h1( txt => "KISS Genome Browser", class => 'center' );
65 push @html, Maasha::XHTML::form_beg( action => $script, method => "get", enctype => "multipart/form-data" );
66
67 push @html, sec_navigate( $cgi );
68 push @html, sec_browse( $dbh, $cgi );
69
70 push @html, Maasha::XHTML::form_end;
71 push @html, Maasha::XHTML::body_end;
72 push @html, Maasha::XHTML::html_end;
73
74 print "$_\n" foreach @html;
75
76
77 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
78
79
80 sub sec_navigate
81 {
82     my ( $cgi,   # CGI object
83        ) = @_;
84
85     # Returns a list.
86
87     my ( $list_clade, $list_genome, $list_assembly, $list_contig, $def_clade, $def_genome, $def_assembly, $def_contig, $def_start, $def_end, @html );
88
89     $list_clade    = nav_list_clade();
90     $list_genome   = nav_list_genome();
91     $list_assembly = nav_list_assembly();
92     $list_contig   = nav_list_contig();
93
94     nav_zoom( $cgi );
95     nav_move( $cgi, 2_800_000 ); # FIXME
96
97     $def_clade     = nav_def_clade( $cgi );
98     $def_genome    = nav_def_genome( $cgi );
99     $def_assembly  = nav_def_assembly( $cgi );
100     $def_contig    = nav_def_contig( $cgi );
101     $def_start     = nav_def_start( $cgi );
102     $def_end       = nav_def_end( $cgi );
103
104     push @html, Maasha::XHTML::table_beg( summary => "Navigation table", align => 'center' );
105     push @html, Maasha::XHTML::table_row_simple( tr => [ qw( Clade Genome Assembly Contig Start End ) ], align => 'center' );
106     push @html, Maasha::XHTML::table_row_simple( tr => [
107         Maasha::XHTML::menu( name => "nav_clade",    options => $list_clade,    selected => $def_clade ),
108         Maasha::XHTML::menu( name => "nav_genome",   options => $list_genome,   selected => $def_genome ),
109         Maasha::XHTML::menu( name => "nav_assembly", options => $list_assembly, selected => $def_assembly ),
110         Maasha::XHTML::menu( name => "nav_contig",   options => $list_contig,   selected => $def_contig ),
111         Maasha::XHTML::text( name => "nav_start", value => Maasha::Calc::commify( $def_start ), size => 20 ),
112         Maasha::XHTML::text( name => "nav_end",   value => Maasha::Calc::commify( $def_end ),   size => 20 ), 
113         Maasha::XHTML::submit( name => "nav_submit", value => "Submit" ),
114     ] );
115     push @html, Maasha::XHTML::table_end;
116
117     push @html, Maasha::XHTML::table_beg( summary => "Zoom table", align => 'center' );
118     push @html, Maasha::XHTML::table_row_simple( tr => [
119         Maasha::XHTML::p( txt => 'Move:' ),
120         Maasha::XHTML::submit( name => "move_left3",  value => "<<<", title => "move 95% to the left" ),
121         Maasha::XHTML::submit( name => "move_left2",  value => "<<",  title => "move 47.5% to the left" ),
122         Maasha::XHTML::submit( name => "move_left1",  value => "<",   title => "move 10% to the left" ),
123         Maasha::XHTML::submit( name => "move_right1", value => ">",   title => "move 10% to the rigth" ),
124         Maasha::XHTML::submit( name => "move_right2", value => ">>",  title => "move 47.5% to the rigth" ),
125         Maasha::XHTML::submit( name => "move_right3", value => ">>>", title => "move 95% to the right" ),
126         Maasha::XHTML::p( txt => 'Zoom in:' ),
127         Maasha::XHTML::submit( name => "zoom_in1", value => "1.5x" ),
128         Maasha::XHTML::submit( name => "zoom_in2", value => "3x" ),
129         Maasha::XHTML::submit( name => "zoom_in3", value => "10x" ),
130         Maasha::XHTML::p( txt => 'Zoom out:' ),
131         Maasha::XHTML::submit( name => "zoom_out1", value => "1.5x" ),
132         Maasha::XHTML::submit( name => "zoom_out2", value => "3x" ),
133         Maasha::XHTML::submit( name => "zoom_out3", value => "10x" ),
134     ] );
135     push @html, Maasha::XHTML::table_end;
136
137     @html = Maasha::XHTML::div( txt => join( "\n", @html ), class => 'navigate' );
138
139     return wantarray ? @html : \@html;
140 }
141
142
143 sub sec_browse
144 {
145     my ( $dbh,   # Database handle
146          $cgi,   # CGI object
147        ) = @_;
148
149     # Returns a list.
150
151     my ( $t0, $t1, @stats, $start, $end, $ruler, $index, $index_beg, $index_len, $fh, $seq, $dna, $table, $entries, $features, $surface, $cr, $file, @html, @img );
152
153     $start = $cgi->param( 'nav_start' );
154     $end   = $cgi->param( 'nav_end' );
155
156     $ruler = Maasha::KISS::Track::track_ruler( 1200, 25, $start, $end, 10, 'black' );
157
158     $index = Maasha::Fasta::index_retrieve( "$ENV{ 'BP_DATA' }/genomes/S_aur_COL/fasta/S_aur_COL.index" );
159
160     ( $index_beg, $index_len ) = @{ $index->{ 'S_aur_COL' } };
161
162     $fh = Maasha::Filesys::file_read_open( "$ENV{ 'BP_DATA' }/genomes/S_aur_COL/fasta/S_aur_COL.fna" );
163
164     $seq = Maasha::Filesys::file_read( $fh, $index_beg + $start, $end - $start + 1 );
165
166     close $fh;
167
168     $dna = Maasha::KISS::Track::track_seq( 1200, 50, $seq, 10, 'black' ) if length $seq <= 220;
169
170     $table = 'Solexa';
171
172     $t0 = Time::HiRes::gettimeofday();
173     $entries = Maasha::KISS::IO::kiss_sql_get( $dbh, $table, $start, $end );
174     $t1 = Time::HiRes::gettimeofday();
175
176     push @stats, "Feature count: " . Maasha::Calc::commify( scalar @$entries );
177     push @stats, "Time SQL: " . sprintf( "%.4f", $t1 - $t0 );
178
179     $t0 = Time::HiRes::gettimeofday();
180
181     my $MAX = 4000;  # FIXME should depend on height of track as well
182
183     if ( @$entries > $MAX ) {
184         $features = Maasha::KISS::Track::track_histogram( 1200, 75, $start, $end, $entries );
185     } else {
186         $features = Maasha::KISS::Track::track_feature( 1200, 75, $start, $end, $entries );
187     }
188
189     $t1 = Time::HiRes::gettimeofday();
190
191     # push @html, Maasha::KISS::Draw::hdump( $entries );
192     # push @html, Maasha::KISS::Draw::hdump( $features );
193
194     push @stats, "Time Track: " . sprintf( "%.4f", $t1 - $t0 );
195
196     $file = "fisk.png";
197
198     $surface = Cairo::ImageSurface->create( 'argb32', 1200, 800 );
199     $cr      = Cairo::Context->create( $surface );
200
201     $t0 = Time::HiRes::gettimeofday();
202
203     Maasha::KISS::Draw::draw_feature( $cr, $ruler )    if $ruler;
204     Maasha::KISS::Draw::draw_feature( $cr, $dna )      if $dna;
205     Maasha::KISS::Draw::draw_feature( $cr, $features ) if $features;
206
207     Maasha::KISS::Draw::file_png( $surface, $file );
208
209     $t1 = Time::HiRes::gettimeofday();
210
211     push @stats, "Time Draw: " . sprintf( "%.4f", $t1 - $t0 );
212
213     push @html, Maasha::XHTML::p( txt => join( " ", @stats ) );
214
215     push @img, Maasha::XHTML::img( src => $file, alt => "Browser Tracks", height => 800, width => 1200, id => "browser_map", usemap => "#browser_map" );
216
217     push @img, Maasha::XHTML::map_beg( name => "browser_map", id => "browser_map" );
218
219     map { push @img, Maasha::XHTML::area( href => "www.dmi.dk", shape => "rect", coords => "$_->{ x1 }, $_->{ y1 }, $_->{ x2 }, $_->{ y2 }", title => "$_->{ title }" ) } @{ $features };
220
221     push @img, Maasha::XHTML::map_end();
222
223     push @html, Maasha::XHTML::p( txt => join( "\n", @img ) );
224
225     @html = Maasha::XHTML::div( txt => join( "\n", @html ), class => 'browse' );
226
227     return wantarray ? @html : \@html;
228 }
229
230
231 sub nav_list_clade
232 {
233     my ( @dirs, $dir, @list_clade );
234
235     @dirs = Maasha::Filesys::ls_dirs( "Data" );
236
237     foreach $dir ( @dirs )
238     {
239         next if $dir eq "Data/." or $dir eq "Data/..";
240
241         push @list_clade, ( split "/", $dir )[ -1 ];
242     }
243
244     return wantarray ? @list_clade : \@list_clade;
245 }
246
247
248 sub nav_list_genome
249 {
250     my ( $list_genome );
251
252     $list_genome = [ qw( S.aur_COL E.col B.sub ) ];
253
254     return wantarray ? @{ $list_genome } : $list_genome;
255 }
256
257
258 sub nav_list_assembly
259 {
260     my ( $list_assembly );
261
262     $list_assembly = [ qw( 2008-02-21 2009-01-23 ) ];
263
264     return wantarray ? @{ $list_assembly } : $list_assembly;
265 }
266
267
268 sub nav_list_contig
269 {
270     my ( $list_contig );
271
272     $list_contig = [ qw( chr1 chr2 ) ];
273
274     return wantarray ? @{ $list_contig } : $list_contig;
275 }
276
277
278 sub nav_zoom
279 {
280     my ( $cgi,   # CGI object
281        ) = @_;
282
283     my ( $start, $end, $dist, $new_dist, $dist_diff, $new_start, $new_end );
284
285     if ( defined $cgi->param( 'nav_start' ) and $cgi->param( 'nav_end' ) )
286     {
287         $start = $cgi->param( 'nav_start' );
288         $end   = $cgi->param( 'nav_end' );
289
290         $start =~ tr/,//d;
291         $end   =~ tr/,//d;
292
293         $dist = $end - $start;
294
295         if ( defined $cgi->param( 'zoom_in1' ) ) {
296             $new_dist = $dist / 1.5;
297         } elsif ( defined $cgi->param( 'zoom_in2' ) ) {
298             $new_dist = $dist / 3;
299         } elsif ( defined $cgi->param( 'zoom_in3' ) ) {
300             $new_dist = $dist / 10;
301         } elsif ( defined $cgi->param( 'zoom_out1' ) ) {
302             $new_dist = $dist * 1.5;
303         } elsif ( defined $cgi->param( 'zoom_out2' ) ) {
304             $new_dist = $dist * 3;
305         } elsif ( defined $cgi->param( 'zoom_out3' ) ) {
306             $new_dist = $dist * 10;
307         }
308
309         if ( $new_dist )
310         {
311             $dist_diff = $dist - $new_dist;
312             $new_start = int( $start + ( $dist_diff / 2 ) );
313             $new_end   = int( $end   - ( $dist_diff / 2 ) );
314
315             $cgi->param( 'nav_start', $new_start );
316             $cgi->param( 'nav_end',  $new_end );
317         }
318     }
319 }
320
321
322 sub nav_move
323 {
324     my ( $cgi,   # CGI object
325          $max,   # Max end position
326        ) = @_;
327
328     my ( $start, $end, $dist, $shift, $new_start, $new_end );
329
330     if ( defined $cgi->param( 'nav_start' ) and $cgi->param( 'nav_end' ) )
331     {
332         $start = $cgi->param( 'nav_start' );
333         $end   = $cgi->param( 'nav_end' );
334
335         $start =~ tr/,//d;
336         $end   =~ tr/,//d;
337
338         $dist = $end - $start;
339
340         if ( defined $cgi->param( 'move_left1' ) ) {
341             $shift = -1 * $dist * 0.10;
342         } elsif ( defined $cgi->param( 'move_left2' ) ) {
343             $shift = -1 * $dist * 0.475;
344         } elsif ( defined $cgi->param( 'move_left3' ) ) {
345             $shift = -1 * $dist * 0.95;
346         } elsif ( defined $cgi->param( 'move_right1' ) ) {
347             $shift = $dist * 0.10;
348         } elsif ( defined $cgi->param( 'move_right2' ) ) {
349             $shift = $dist * 0.475;
350         } elsif ( defined $cgi->param( 'move_right3' ) ) {
351             $shift = $dist * 0.95;
352         }
353
354         if ( $shift )
355         {
356             $new_start = int( $start + $shift );
357             $new_end   = int( $end   + $shift );
358
359             print "HERRRR: shift: $shift    start: $new_start    end: $new_end\n";
360
361             if ( $new_start > 0 and $new_end < $max )
362             {
363                 $cgi->param( 'nav_start', $new_start );
364                 $cgi->param( 'nav_end',  $new_end );
365             }
366         }
367     }
368 }
369
370
371 sub nav_def_clade
372 {
373     my ( $cgi,   # CGI object
374        ) = @_;
375
376     my ( $def_clade );
377
378     if ( defined $cgi->param( 'nav_clade' ) )
379     {
380         $def_clade = $cgi->param( 'nav_clade' );
381     }
382     else
383     {
384         $def_clade = "Bacteria";
385     }
386
387     return $def_clade;
388 }
389
390
391 sub nav_def_genome
392 {
393     my ( $cgi,   # CGI object
394        ) = @_;
395
396     my ( $def_genome );
397
398     if ( defined $cgi->param( 'nav_genome' ) )
399     {
400         $def_genome = $cgi->param( 'nav_genome' );
401     }
402     else
403     {
404         $def_genome = "S.aur_COL";
405     }
406
407     return $def_genome;
408 }
409
410
411 sub nav_def_assembly
412 {
413     my ( $cgi,   # CGI object
414        ) = @_;
415
416     my ( $def_assembly );
417
418     if ( defined $cgi->param( 'nav_assembly' ) )
419     {
420         $def_assembly = $cgi->param( 'nav_assembly' );
421     }
422     else
423     {
424         $def_assembly = "2009-01-23";
425     }
426
427     return $def_assembly;
428 }
429
430
431 sub nav_def_contig
432 {
433     my ( $cgi,   # CGI object
434        ) = @_;
435
436     my ( $def_contig );
437
438     if ( defined $cgi->param( 'nav_contig' ) )
439     {
440         $def_contig = $cgi->param( 'nav_contig' );
441     }
442     else
443     {
444         $def_contig = "chr1";
445     }
446
447     return $def_contig;
448 }
449
450
451 sub nav_def_start
452 {
453     my ( $cgi,   # CGI object
454        ) = @_;
455
456     my ( $def_start );
457
458     if ( defined $cgi->param( 'nav_start' ) ) {
459         $def_start = $cgi->param( 'nav_start' );
460     } else {
461         $def_start = 1;
462     }
463
464     $def_start =~ tr/,//d;
465
466     if ( $def_start <= 0 ) {
467         $def_start = 1;
468     }
469
470     $cgi->param( 'nav_start', $def_start );
471
472     return $def_start;
473 }
474
475
476 sub nav_def_end
477 {
478     my ( $cgi,   # CGI object
479        ) = @_;
480
481     my ( $def_end );
482     
483     if ( defined $cgi->param( 'nav_end' ) ) {
484         $def_end = $cgi->param( 'nav_end' );
485     } else {
486         $def_end = 2809422;
487         $def_end = 2000;
488     }
489
490     $def_end =~ tr/,//d;
491
492     if ( $def_end > 2809422 ) {
493         $def_end = 2809422;
494     }
495
496     $cgi->param( 'nav_end', $def_end );
497
498     return $def_end;
499 }
500
501
502 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
503
504
505 END
506 {
507     Maasha::SQL::disconnect( $dbh ) if $dbh;
508 }
509
510
511 __END__