1 package Maasha::BBrowser::Track;
3 # Copyright (C) 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
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
25 # Routines for creating Biopieces Browser tracks.
28 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
39 use vars qw( @ISA @EXPORT );
41 @ISA = qw( Exporter );
44 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
49 my ( $cookie, # browser cookie
52 my ( $beg, $end, $factor, $step, $i, $txt, $x, @ruler );
54 $beg = $cookie->{ 'NAV_START' };
55 $end = $cookie->{ 'NAV_END' };
56 $factor = $cookie->{ 'IMG_WIDTH' } / ( $end - $beg );
60 while ( ( $end - $beg ) / $step > 20 )
65 for ( $i = $beg; $i < $end; $i++ )
67 if ( ( $i % $step ) == 0 )
69 $txt = Maasha::Calc::commify( $i ) . "|";
70 $x = sprintf( "%.0f", ( $i - $beg - length $txt ) * $factor );
72 if ( $x > 0 and $x + ( $cookie->{ 'RULER_FONT_SIZE' } * length $txt ) < $cookie->{ 'IMG_WIDTH' } )
77 font_size => $cookie->{ 'RULER_FONT_SIZE' },
78 color => $cookie->{ 'RULER_COLOR' },
80 y1 => $cookie->{ 'TRACK_OFFSET' },
86 $cookie->{ 'TRACK_OFFSET' } += $cookie->{ 'TRACK_SPACE' };
88 return wantarray ? @ruler : \@ruler;
94 my ( $cookie, # browser cookie
97 my ( $file, $fh, $seq, @chars, $factor, $i, @seq_list );
99 if ( $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' } + 1 <= 220 )
101 $file = path_seq( $cookie );
102 $fh = Maasha::Filesys::file_read_open( $file );
103 $seq = Maasha::Filesys::file_read( $fh, $cookie->{ 'NAV_START' } - 1, $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' } + 1 );
106 @chars = split //, $seq;
108 $factor = $cookie->{ 'IMG_WIDTH' } / @chars;
110 for ( $i = 0; $i < @chars; $i++ ) {
114 font_size => $cookie->{ 'SEQ_FONT_SIZE' },
115 color => $cookie->{ 'SEQ_COLOR' },
116 x1 => sprintf( "%.0f", $i * $factor ),
117 y1 => $cookie->{ 'TRACK_OFFSET' },
121 $cookie->{ 'TRACK_OFFSET' } += $cookie->{ 'TRACK_SPACE' };
123 return wantarray ? @seq_list : \@seq_list;
140 my ( $index, $count, $track_name, $start, $end, $entries, $features );
142 $start = $cookie->{ 'NAV_START' };
143 $end = $cookie->{ 'NAV_END' };
145 $index = Maasha::KISS::kiss_index_retrieve( "$track/track_data.kiss.index" );
146 $count = Maasha::KISS::kiss_index_count( $index, $start, $end );
148 $track_name = ( split "/", $track )[ -1 ];
149 $track_name =~ s/^\d+_//;
150 $track_name =~ s/_/ /g;
155 font_size => $cookie->{ 'SEQ_FONT_SIZE' },
156 color => $cookie->{ 'SEQ_COLOR' },
158 y1 => $cookie->{ 'TRACK_OFFSET' },
161 $cookie->{ 'TRACK_OFFSET' } += 10;
165 $entries = Maasha::KISS::kiss_index_get_blocks( $index, $start, $end );
166 push @{ $features }, track_feature_histogram( $cookie, $start, $end, $entries );
170 $entries = Maasha::KISS::kiss_index_get_entries( "$track/track_data.kiss", $index, $start, $end );
171 push @{ $features }, track_feature_linear( $cookie, $start, $end, $entries );
174 return wantarray ? @{ $features } : $features;
178 sub track_feature_linear
180 my ( $cookie, # hashref with image draw metrics
181 $beg, # base window beg
182 $end, # base window end
183 $entries, # list of unsorted KISS entries
188 my ( $factor, $entry, $y_step, @ladder, $y_max, $w, $x1, $y1, $x2, $y2, @features );
190 @{ $entries } = sort { $a->{ 'S_BEG' } <=> $b->{ 'S_BEG' } or $a->{ 'S_END' } <=> $b->{ 'S_END' } } @{ $entries };
192 $factor = $cookie->{ 'IMG_WIDTH' } / ( $end - $beg );
196 foreach $entry ( @{ $entries } )
198 $w = sprintf( "%.0f", ( $entry->{ 'S_END' } - $entry->{ 'S_BEG' } + 1 ) * $factor );
202 $x1 = sprintf( "%.0f", ( $entry->{ 'S_BEG' } - $beg ) * $factor );
204 for ( $y_step = 0; $y_step < @ladder; $y_step++ ) {
205 last if $x1 >= $ladder[ $y_step ] + 1;
208 $y1 = $cookie->{ 'TRACK_OFFSET' } + ( $cookie->{ 'FEAT_WIDTH' } * $y_step );
212 line_width => $cookie->{ 'FEAT_WIDTH' },
213 color => $cookie->{ 'FEAT_COLOR' },
214 title => "Q_ID: $entry->{ 'Q_ID' } S_BEG: $entry->{ 'S_BEG' } S_END: $entry->{ 'S_END' } STRAND: $entry->{ 'STRAND' }",
215 id => $entry->{ 'Q_ID' },
219 y2 => $y1 + $cookie->{ 'FEAT_WIDTH' },
222 $y_max = Maasha::Calc::max( $y_max, $y_step * $cookie->{ 'FEAT_WIDTH' } );
224 push @features, feature_align( $entry, $beg, $y1, $factor, $cookie->{ 'FEAT_WIDTH' } ) if $entry->{ 'ALIGN' } ne '.';
226 $ladder[ $y_step ] = $x1 + $w;
230 $cookie->{ 'TRACK_OFFSET' } += $y_max + $cookie->{ 'TRACK_SPACE' };
232 return wantarray ? @features : \@features;
240 my ( $entry, # Partial KISS entry
241 $beg, # base window beg
242 $y_offset, # y axis draw offset
243 $factor, # scale factor
244 $feat_height, # hight of feature in pixels
249 my ( $w, $align, $pos, $nt_before, $nt_after, $x1, @features );
251 $w = sprintf( "%.0f", 1 * $factor );
255 foreach $align ( split /,/, $entry->{ 'ALIGN' } )
257 if ( $align =~ /(\d+):([ATCGN-])>([ATCGN-])/ )
265 Maasha::Common::error( qq(BAD align descriptor: "$align") );
268 $x1 = sprintf( "%.0f", ( $entry->{ 'S_BEG' } + $pos - $beg ) * $factor );
272 line_width => $feat_height,
273 color => [ 1, 0, 0 ],
278 y2 => $y_offset + $feat_height,
281 if ( $w > $feat_height )
285 font_size => $feat_height + 2,
286 color => [ 0, 0, 0 ],
288 x1 => $x1 + sprintf( "%.0f", ( $w / 2 ) ) - $feat_height / 2,
289 y1 => $y_offset + $feat_height,
295 return wantarray ? @features : \@features;
299 sub track_feature_histogram
301 my ( $cookie, # hashref with image draw metrics
302 $min, # minimum base position
303 $max, # maximum base position
304 $blocks, # list of blocks
309 my ( $hist_height, $bucket_width, $bucket_count, $min_bucket, $factor, $factor_heigth, $max_height, $block, $bucket_beg, $bucket_end, $i, @buckets, $h, $x, @hist );
311 return if $max <= $min;
313 $hist_height = 100; # pixels
315 $bucket_count = $cookie->{ 'IMG_WIDTH' } / $bucket_width;
316 $factor = ( $cookie->{ 'IMG_WIDTH' } / $bucket_width ) / ( $max - $min + 1 );
318 $min_bucket = 999999999;
321 foreach $block ( @{ $blocks } )
323 $bucket_beg = int( $block->{ 'BEG' } * $factor );
324 $bucket_end = int( $block->{ 'END' } * $factor );
326 $min_bucket = Maasha::Calc::min( $min_bucket, $bucket_beg );
328 for ( $i = $bucket_beg; $i <= $bucket_end; $i++ )
330 $buckets[ $i ] += $block->{ 'COUNT' };
332 $max_height = Maasha::Calc::max( $max_height, $buckets[ $i ] );
336 if ( $max_height > 0 )
338 $factor_heigth = $hist_height / $max_height;
342 for ( $i = $min_bucket; $i < @buckets; $i++ )
344 if ( defined $buckets[ $i ] )
346 $h = sprintf( "%.0f", $buckets[ $i ] * $factor_heigth );
352 line_width => $bucket_width,
353 color => $cookie->{ 'FEAT_COLOR' },
354 title => "Features: $buckets[ $i ]",
356 y1 => $cookie->{ 'TRACK_OFFSET' } + $hist_height,
358 y2 => $cookie->{ 'TRACK_OFFSET' } + $hist_height - $h,
367 $cookie->{ 'TRACK_OFFSET' } += $hist_height + $cookie->{ 'TRACK_SPACE' };
369 return wantarray ? @hist : \@hist;
382 die qq(ERROR: no USER in cookie.\n) if not $cookie->{ 'USER' };
383 die qq(ERROR: no CLADE in cookie.\n) if not $cookie->{ 'CLADE' };
384 die qq(ERROR: no GENOME in cookie.\n) if not $cookie->{ 'GENOME' };
385 die qq(ERROR: no ASSEMBLY in cookie.\n) if not $cookie->{ 'ASSEMBLY' };
386 die qq(ERROR: no CONTIG in cookie.\n) if not $cookie->{ 'CONTIG' };
389 $cookie->{ 'DATA_DIR' },
392 $cookie->{ 'CLADE' },
393 $cookie->{ 'GENOME' },
394 $cookie->{ 'ASSEMBLY' },
395 $cookie->{ 'CONTIG' },
400 die qq(ERROR: no such file: "$path".\n) if not -e $path;
413 my ( $path, @tracks );
415 die qq(ERROR: no USER in cookie.\n) if not $cookie->{ 'USER' };
416 die qq(ERROR: no CLADE in cookie.\n) if not $cookie->{ 'CLADE' };
417 die qq(ERROR: no GENOME in cookie.\n) if not $cookie->{ 'GENOME' };
418 die qq(ERROR: no ASSEMBLY in cookie.\n) if not $cookie->{ 'ASSEMBLY' };
419 die qq(ERROR: no CONTIG in cookie.\n) if not $cookie->{ 'CONTIG' };
422 $cookie->{ 'DATA_DIR' },
425 $cookie->{ 'CLADE' },
426 $cookie->{ 'GENOME' },
427 $cookie->{ 'ASSEMBLY' },
428 $cookie->{ 'CONTIG' },
434 @tracks = Maasha::Filesys::ls_dirs( $path );
436 @tracks = grep { $_ !~ /\/\.\.?$/ } @tracks;
438 return wantarray ? @tracks : \@tracks;
442 return wantarray ? () : [];
446 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<