]> git.donarmstrong.com Git - biopieces.git/blob - www/cgi-bin/index.cgi
more work on KISS
[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    => "test.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->param( 'nav_start' ), $cgi->param( 'nav_end' ) );
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" );
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" );
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     return wantarray ? @html : \@html;
138 }
139
140
141 sub sec_browse
142 {
143     my ( $dbh,     # Database handle
144          $start,   # Browse start position
145          $end,     # Browse end position
146        ) = @_;
147
148     # Returns a list.
149
150     my ( $t0, $t1, $ruler, $index, $index_beg, $index_len, $fh, $seq, $dna, $table, $entries, $features, $surface, $cr, $file, @html );
151
152     $ruler = Maasha::KISS::Track::track_ruler( 1200, 25, $start, $end );
153
154     $index = Maasha::Fasta::index_retrieve( "$ENV{ 'BP_DATA' }/genomes/S_aur_COL/fasta/S_aur_COL.index" );
155
156     ( $index_beg, $index_len ) = @{ $index->{ 'S_aur_COL' } };
157
158     $fh = Maasha::Filesys::file_read_open( "$ENV{ 'BP_DATA' }/genomes/S_aur_COL/fasta/S_aur_COL.fna" );
159
160     $seq = Maasha::Filesys::file_read( $fh, $index_beg + $start, $end - $start + 1 );
161
162     close $fh;
163
164     $dna = Maasha::KISS::Track::track_seq( 1200, 50, $seq ) if length $seq <= 220;
165
166     $table = 'Solexa';
167
168     $t0 = Time::HiRes::gettimeofday();
169     $entries = Maasha::KISS::IO::kiss_sql_get( $dbh, $table, $start, $end );
170     $t1 = Time::HiRes::gettimeofday();
171
172     push @html, Maasha::XHTML::p( txt => "Feature count: " . Maasha::Calc::commify( scalar @$entries ) );
173
174     push @html, Maasha::XHTML::p( txt => "Time SQL: " . ( $t1 - $t0 ) );
175
176     $t0 = Time::HiRes::gettimeofday();
177
178     my $MAX = 4000;  # FIXME should depend on height of track as well
179
180     if ( @$entries > $MAX ) {
181         $features = Maasha::KISS::Track::track_histogram( 1200, 75, $start, $end, $entries );
182     } else {
183         $features = Maasha::KISS::Track::track_feature( 1200, 75, $start, $end, $entries );
184     }
185
186     $t1 = Time::HiRes::gettimeofday();
187
188     # push @html, Maasha::KISS::Draw::hdump( $entries );
189     # push @html, Maasha::KISS::Draw::hdump( $features );
190
191     push @html, Maasha::XHTML::p( txt => "Time Track: " . ( $t1 - $t0 ) );
192
193     $file = "fisk.png";
194
195     $surface = Cairo::ImageSurface->create( 'argb32', 1200, 800 );
196     $cr      = Cairo::Context->create( $surface );
197
198     $t0 = Time::HiRes::gettimeofday();
199
200     Maasha::KISS::Draw::track_text( $cr, $ruler, "red" ) if $ruler;
201     Maasha::KISS::Draw::track_text( $cr, $dna, "red" )   if $dna;
202
203     Maasha::KISS::Draw::track_feature( $cr, $features, 'green' ) if $features;
204
205     Maasha::KISS::Draw::file_png( $surface, $file );
206
207     $t1 = Time::HiRes::gettimeofday();
208
209     push @html, Maasha::XHTML::p( txt => "Time Draw: " . ( $t1 - $t0 ) );
210
211     push @html, Maasha::XHTML::p( txt => Maasha::XHTML::img( src => $file, alt => "Browser Tracks", height => 800, width => 1200, class => "foo", id => "pix_id", usemap => "map"  ) );
212
213     return wantarray ? @html : \@html;
214 }
215
216
217 sub nav_list_clade
218 {
219     my ( $list_clade );
220
221     $list_clade = [ qw( Eukaryote Bacillus Fish ) ];
222
223     return wantarray ? @{ $list_clade } : $list_clade;
224 }
225
226
227 sub nav_list_genome
228 {
229     my ( $list_genome );
230
231     $list_genome = [ qw( S.aur_COL E.col B.sub ) ];
232
233     return wantarray ? @{ $list_genome } : $list_genome;
234 }
235
236
237 sub nav_list_assembly
238 {
239     my ( $list_assembly );
240
241     $list_assembly = [ qw( 2008-02-21 2009-01-23 ) ];
242
243     return wantarray ? @{ $list_assembly } : $list_assembly;
244 }
245
246
247 sub nav_list_contig
248 {
249     my ( $list_contig );
250
251     $list_contig = [ qw( chr1 chr2 ) ];
252
253     return wantarray ? @{ $list_contig } : $list_contig;
254 }
255
256
257 sub nav_zoom
258 {
259     my ( $cgi,   # CGI object
260        ) = @_;
261
262     my ( $start, $end, $dist, $new_dist, $dist_diff, $new_start, $new_end );
263
264     if ( defined $cgi->param( 'nav_start' ) and $cgi->param( 'nav_end' ) )
265     {
266         $start = $cgi->param( 'nav_start' );
267         $end   = $cgi->param( 'nav_end' );
268
269         $start =~ tr/,//d;
270         $end   =~ tr/,//d;
271
272         $dist = $end - $start;
273
274         if ( defined $cgi->param( 'zoom_in1' ) ) {
275             $new_dist = $dist / 1.5;
276         } elsif ( defined $cgi->param( 'zoom_in2' ) ) {
277             $new_dist = $dist / 3;
278         } elsif ( defined $cgi->param( 'zoom_in3' ) ) {
279             $new_dist = $dist / 10;
280         } elsif ( defined $cgi->param( 'zoom_out1' ) ) {
281             $new_dist = $dist * 1.5;
282         } elsif ( defined $cgi->param( 'zoom_out2' ) ) {
283             $new_dist = $dist * 3;
284         } elsif ( defined $cgi->param( 'zoom_out3' ) ) {
285             $new_dist = $dist * 10;
286         }
287
288         if ( $new_dist )
289         {
290             $dist_diff = $dist - $new_dist;
291             $new_start = int( $start + ( $dist_diff / 2 ) );
292             $new_end   = int( $end   - ( $dist_diff / 2 ) );
293
294             $cgi->param( 'nav_start', $new_start );
295             $cgi->param( 'nav_end',  $new_end );
296         }
297     }
298 }
299
300
301 sub nav_move
302 {
303     my ( $cgi,   # CGI object
304          $max,   # Max end position
305        ) = @_;
306
307     my ( $start, $end, $dist, $shift, $new_start, $new_end );
308
309     if ( defined $cgi->param( 'nav_start' ) and $cgi->param( 'nav_end' ) )
310     {
311         $start = $cgi->param( 'nav_start' );
312         $end   = $cgi->param( 'nav_end' );
313
314         $start =~ tr/,//d;
315         $end   =~ tr/,//d;
316
317         $dist = $end - $start;
318
319         if ( defined $cgi->param( 'move_left1' ) ) {
320             $shift = -1 * $dist * 0.10;
321         } elsif ( defined $cgi->param( 'move_left2' ) ) {
322             $shift = -1 * $dist * 0.475;
323         } elsif ( defined $cgi->param( 'move_left3' ) ) {
324             $shift = -1 * $dist * 0.95;
325         } elsif ( defined $cgi->param( 'move_right1' ) ) {
326             $shift = $dist * 0.10;
327         } elsif ( defined $cgi->param( 'move_right2' ) ) {
328             $shift = $dist * 0.475;
329         } elsif ( defined $cgi->param( 'move_right3' ) ) {
330             $shift = $dist * 0.95;
331         }
332
333         if ( $shift )
334         {
335             $new_start = int( $start + $shift );
336             $new_end   = int( $end   + $shift );
337
338             print "HERRRR: shift: $shift    start: $new_start    end: $new_end\n";
339
340             if ( $new_start > 0 and $new_end < $max )
341             {
342                 $cgi->param( 'nav_start', $new_start );
343                 $cgi->param( 'nav_end',  $new_end );
344             }
345         }
346     }
347 }
348
349
350 sub nav_def_clade
351 {
352     my ( $cgi,   # CGI object
353        ) = @_;
354
355     my ( $def_clade );
356
357     if ( defined $cgi->param( 'nav_clade' ) )
358     {
359         $def_clade = $cgi->param( 'nav_clade' );
360     }
361     else
362     {
363         $def_clade = "Bacteria";
364     }
365
366     return $def_clade;
367 }
368
369
370 sub nav_def_genome
371 {
372     my ( $cgi,   # CGI object
373        ) = @_;
374
375     my ( $def_genome );
376
377     if ( defined $cgi->param( 'nav_genome' ) )
378     {
379         $def_genome = $cgi->param( 'nav_genome' );
380     }
381     else
382     {
383         $def_genome = "S.aur_COL";
384     }
385
386     return $def_genome;
387 }
388
389
390 sub nav_def_assembly
391 {
392     my ( $cgi,   # CGI object
393        ) = @_;
394
395     my ( $def_assembly );
396
397     if ( defined $cgi->param( 'nav_assembly' ) )
398     {
399         $def_assembly = $cgi->param( 'nav_assembly' );
400     }
401     else
402     {
403         $def_assembly = "2009-01-23";
404     }
405
406     return $def_assembly;
407 }
408
409
410 sub nav_def_contig
411 {
412     my ( $cgi,   # CGI object
413        ) = @_;
414
415     my ( $def_contig );
416
417     if ( defined $cgi->param( 'nav_contig' ) )
418     {
419         $def_contig = $cgi->param( 'nav_contig' );
420     }
421     else
422     {
423         $def_contig = "chr1";
424     }
425
426     return $def_contig;
427 }
428
429
430 sub nav_def_start
431 {
432     my ( $cgi,   # CGI object
433        ) = @_;
434
435     my ( $def_start );
436
437     if ( defined $cgi->param( 'nav_start' ) ) {
438         $def_start = $cgi->param( 'nav_start' );
439     } else {
440         $def_start = 1;
441     }
442
443     $def_start =~ tr/,//d;
444
445     if ( $def_start <= 0 ) {
446         $def_start = 1;
447     }
448
449     $cgi->param( 'nav_start', $def_start );
450
451     return $def_start;
452 }
453
454
455 sub nav_def_end
456 {
457     my ( $cgi,   # CGI object
458        ) = @_;
459
460     my ( $def_end );
461     
462     if ( defined $cgi->param( 'nav_end' ) ) {
463         $def_end = $cgi->param( 'nav_end' );
464     } else {
465         $def_end = 2809422;
466         $def_end = 2000;
467     }
468
469     $def_end =~ tr/,//d;
470
471     if ( $def_end > 2809422 ) {
472         $def_end = 2809422;
473     }
474
475     $cgi->param( 'nav_end', $def_end );
476
477     return $def_end;
478 }
479
480
481 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
482
483
484 END
485 {
486     Maasha::SQL::disconnect( $dbh ) if $dbh;
487 }
488
489
490 __END__