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 # Martin A. Hansen, November 2009.
51 # Create a track with a ruler of tics and positions for
54 my ( $cookie, # browser cookie
59 my ( $beg, $end, $factor, $step, $i, $txt, $x, @ruler );
61 $beg = $cookie->{ 'NAV_START' };
62 $end = $cookie->{ 'NAV_END' };
63 $factor = $cookie->{ 'IMG_WIDTH' } / ( $end - $beg );
67 while ( ( $end - $beg ) / $step > 20 ) {
71 for ( $i = $beg; $i < $end; $i++ )
73 if ( ( $i % $step ) == 0 )
75 $txt = Maasha::Calc::commify( $i ) . "|"; # TODO reverse this so |123 instead of 123|
76 $x = sprintf( "%.0f", ( $i - $beg - length $txt ) * $factor );
78 if ( $x > 0 and $x + ( $cookie->{ 'RULER_FONT_SIZE' } * length $txt ) < $cookie->{ 'IMG_WIDTH' } )
83 font_size => $cookie->{ 'RULER_FONT_SIZE' },
84 color => $cookie->{ 'RULER_COLOR' },
86 y1 => $cookie->{ 'TRACK_OFFSET' },
92 $cookie->{ 'TRACK_OFFSET' } += $cookie->{ 'TRACK_SPACE' };
94 return wantarray ? @ruler : \@ruler;
100 # Martin A. Hansen, November 2009.
102 # Create a sequence track by extracting the appropriate
103 # stretch of sequence from the sequence file.
105 my ( $cookie, # browser cookie
110 my ( $file, $fh, $seq, @chars, $factor, $i, @seq_list );
112 if ( $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' } + 1 <= 220 )
114 $file = path_seq( $cookie );
115 $fh = Maasha::Filesys::file_read_open( $file );
116 $seq = Maasha::Filesys::file_read( $fh, $cookie->{ 'NAV_START' } - 1, $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' } + 1 );
119 @chars = split //, $seq;
121 $factor = $cookie->{ 'IMG_WIDTH' } / @chars;
123 for ( $i = 0; $i < @chars; $i++ ) {
127 font_size => $cookie->{ 'SEQ_FONT_SIZE' },
128 color => $cookie->{ 'SEQ_COLOR' },
129 x1 => sprintf( "%.0f", $i * $factor ),
130 y1 => $cookie->{ 'TRACK_OFFSET' },
134 $cookie->{ 'TRACK_OFFSET' } += $cookie->{ 'TRACK_SPACE' };
136 return wantarray ? @seq_list : \@seq_list;
147 # Martin A. Hansen, November 2009.
149 # Create a track with features. If there are more than $cookie->FEAT_MAX
150 # features the track created will be a histogram, else linear.
152 my ( $track, # path to kiss file with track data
153 $cookie, # cookie hash
158 my ( $index, $count, $track_name, $start, $end, $entries, $features );
160 $start = $cookie->{ 'NAV_START' };
161 $end = $cookie->{ 'NAV_END' };
163 $index = Maasha::KISS::kiss_index_retrieve( "$track/track_data.kiss.index" );
164 $count = Maasha::KISS::kiss_index_count( $index, $start, $end );
166 $track_name = ( split "/", $track )[ -1 ];
167 $track_name =~ s/^\d+_//;
168 $track_name =~ s/_/ /g;
173 font_size => $cookie->{ 'SEQ_FONT_SIZE' },
174 color => $cookie->{ 'SEQ_COLOR' },
176 y1 => $cookie->{ 'TRACK_OFFSET' },
179 $cookie->{ 'TRACK_OFFSET' } += 10;
181 if ( $count > $cookie->{ 'FEAT_MAX' } )
183 $entries = Maasha::KISS::kiss_index_get_blocks( $index, $start, $end );
184 push @{ $features }, track_feature_histogram( $cookie, $start, $end, $entries );
188 $entries = Maasha::KISS::kiss_index_get_entries( "$track/track_data.kiss", $index, $start, $end );
189 push @{ $features }, track_feature_linear( $cookie, $start, $end, $entries );
192 return wantarray ? @{ $features } : $features;
196 sub track_feature_linear
198 # Martin A. Hansen, November 2009.
200 # Create a linear feature track where the granularity depends
201 # on the lenght of the features and the browser window width.
203 my ( $cookie, # hashref with image draw metrics
204 $beg, # base window beg
205 $end, # base window end
206 $entries, # list of unsorted KISS entries
211 my ( $factor, $entry, $y_step, @ladder, $y_max, $w, $x1, $y1, $x2, $y2, @features );
213 @{ $entries } = sort { $a->{ 'S_BEG' } <=> $b->{ 'S_BEG' } or $a->{ 'S_END' } <=> $b->{ 'S_END' } } @{ $entries };
215 $factor = $cookie->{ 'IMG_WIDTH' } / ( $end - $beg );
219 foreach $entry ( @{ $entries } )
221 $w = sprintf( "%.0f", ( $entry->{ 'S_END' } - $entry->{ 'S_BEG' } + 1 ) * $factor );
225 $x1 = sprintf( "%.0f", ( $entry->{ 'S_BEG' } - $beg ) * $factor );
227 for ( $y_step = 0; $y_step < @ladder; $y_step++ ) {
228 last if $x1 >= $ladder[ $y_step ] + 1;
231 $y1 = $cookie->{ 'TRACK_OFFSET' } + ( $cookie->{ 'FEAT_WIDTH' } * $y_step );
235 line_width => $cookie->{ 'FEAT_WIDTH' },
236 color => $cookie->{ 'FEAT_COLOR' },
237 title => "Q_ID: $entry->{ 'Q_ID' } S_BEG: $entry->{ 'S_BEG' } S_END: $entry->{ 'S_END' } STRAND: $entry->{ 'STRAND' }",
238 id => $entry->{ 'Q_ID' },
242 y2 => $y1 + $cookie->{ 'FEAT_WIDTH' },
245 $y_max = Maasha::Calc::max( $y_max, $y_step * $cookie->{ 'FEAT_WIDTH' } );
247 push @features, feature_align( $entry, $beg, $y1, $factor, $cookie->{ 'FEAT_WIDTH' } ) if $entry->{ 'ALIGN' } ne '.';
249 $ladder[ $y_step ] = $x1 + $w;
253 $cookie->{ 'TRACK_OFFSET' } += $y_max + $cookie->{ 'TRACK_SPACE' };
255 return wantarray ? @features : \@features;
261 # Martin A. Hansen, November 2009.
263 # Add to feature track alignment info if the granularity is
265 # TODO: The printing of chars is imprecise.
267 my ( $entry, # Partial KISS entry
268 $beg, # base window beg
269 $y_offset, # y axis draw offset
270 $factor, # scale factor
271 $feat_height, # hight of feature in pixels
276 my ( $w, $align, $pos, $nt_before, $nt_after, $x1, @features );
278 $w = sprintf( "%.0f", 1 * $factor );
282 foreach $align ( split /,/, $entry->{ 'ALIGN' } )
284 if ( $align =~ /(\d+):([ATCGN-])>([ATCGN-])/ )
292 Maasha::Common::error( qq(BAD align descriptor: "$align") );
295 $x1 = sprintf( "%.0f", ( $entry->{ 'S_BEG' } + $pos - $beg ) * $factor );
299 line_width => $feat_height,
300 color => [ 1, 0, 0 ],
305 y2 => $y_offset + $feat_height,
308 if ( $w > $feat_height )
312 font_size => $feat_height + 2,
313 color => [ 0, 0, 0 ],
315 x1 => $x1 + sprintf( "%.0f", ( $w / 2 ) ) - $feat_height / 2,
316 y1 => $y_offset + $feat_height,
322 return wantarray ? @features : \@features;
326 sub track_feature_histogram
328 # Martin A. Hansen, November 2009.
330 # Create a feature track as a histogram using information
331 # from the index only thus avoiding to load features from the
334 my ( $cookie, # hashref with image draw metrics
335 $min, # minimum base position
336 $max, # maximum base position
337 $blocks, # list of blocks
342 my ( $hist_height, $bucket_width, $bucket_count, $min_bucket, $factor, $factor_heigth, $max_height, $block, $bucket_beg, $bucket_end, $i, @buckets, $h, $x, @hist );
344 return if $max <= $min;
346 $hist_height = 100; # pixels
348 $bucket_count = $cookie->{ 'IMG_WIDTH' } / $bucket_width;
349 $factor = ( $cookie->{ 'IMG_WIDTH' } / $bucket_width ) / ( $max - $min + 1 );
351 $min_bucket = 999999999;
354 foreach $block ( @{ $blocks } )
356 $bucket_beg = int( $block->{ 'BEG' } * $factor );
357 $bucket_end = int( $block->{ 'END' } * $factor );
359 $min_bucket = Maasha::Calc::min( $min_bucket, $bucket_beg );
361 for ( $i = $bucket_beg; $i <= $bucket_end; $i++ )
363 $buckets[ $i ] += $block->{ 'COUNT' };
365 $max_height = Maasha::Calc::max( $max_height, $buckets[ $i ] );
369 if ( $max_height > 0 )
371 $factor_heigth = $hist_height / $max_height;
375 for ( $i = $min_bucket; $i < @buckets; $i++ )
377 if ( defined $buckets[ $i ] )
379 $h = sprintf( "%.0f", $buckets[ $i ] * $factor_heigth );
385 line_width => $bucket_width,
386 color => $cookie->{ 'FEAT_COLOR' },
387 title => "Features: $buckets[ $i ]",
389 y1 => $cookie->{ 'TRACK_OFFSET' } + $hist_height,
391 y2 => $cookie->{ 'TRACK_OFFSET' } + $hist_height - $h,
400 $cookie->{ 'TRACK_OFFSET' } += $hist_height + $cookie->{ 'TRACK_SPACE' };
402 return wantarray ? @hist : \@hist;
408 # Martin A. Hansen, November 2009.
410 # Returns the path to the sequence file for a specified
411 # contig as written in the cookie.
413 my ( $cookie, # cookie hash
420 die qq(ERROR: no USER in cookie.\n) if not $cookie->{ 'USER' };
421 die qq(ERROR: no CLADE in cookie.\n) if not $cookie->{ 'CLADE' };
422 die qq(ERROR: no GENOME in cookie.\n) if not $cookie->{ 'GENOME' };
423 die qq(ERROR: no ASSEMBLY in cookie.\n) if not $cookie->{ 'ASSEMBLY' };
424 die qq(ERROR: no CONTIG in cookie.\n) if not $cookie->{ 'CONTIG' };
427 $cookie->{ 'DATA_DIR' },
430 $cookie->{ 'CLADE' },
431 $cookie->{ 'GENOME' },
432 $cookie->{ 'ASSEMBLY' },
433 $cookie->{ 'CONTIG' },
438 die qq(ERROR: no such file: "$path".\n) if not -e $path;
446 # Martin A. Hansen, November 2009.
448 # Returns a list of paths to all tracks for a specified
449 # contig as written in the cookie.
451 my ( $cookie, # cookie path
456 my ( $path, @tracks );
458 die qq(ERROR: no USER in cookie.\n) if not $cookie->{ 'USER' };
459 die qq(ERROR: no CLADE in cookie.\n) if not $cookie->{ 'CLADE' };
460 die qq(ERROR: no GENOME in cookie.\n) if not $cookie->{ 'GENOME' };
461 die qq(ERROR: no ASSEMBLY in cookie.\n) if not $cookie->{ 'ASSEMBLY' };
462 die qq(ERROR: no CONTIG in cookie.\n) if not $cookie->{ 'CONTIG' };
465 $cookie->{ 'DATA_DIR' },
468 $cookie->{ 'CLADE' },
469 $cookie->{ 'GENOME' },
470 $cookie->{ 'ASSEMBLY' },
471 $cookie->{ 'CONTIG' },
477 @tracks = Maasha::Filesys::ls_dirs( $path );
479 @tracks = grep { $_ !~ /\/\.\.?$/ } @tracks;
481 return wantarray ? @tracks : \@tracks;
485 return wantarray ? () : [];
489 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<