1 package Maasha::BGB::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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
38 use Maasha::Biopieces;
41 use vars qw( @ISA @EXPORT );
43 @ISA = qw( Exporter );
46 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
51 # Martin A. Hansen, November 2009.
53 # Create a track with a ruler of tics and positions for
56 my ( $cookie, # browser cookie
61 my ( $beg, $end, $factor, $step, $i, $txt, $x, @ruler );
63 $beg = $cookie->{ 'NAV_START' };
64 $end = $cookie->{ 'NAV_END' };
65 $factor = $cookie->{ 'IMG_WIDTH' } / ( $end - $beg + 1 );
69 while ( ( $end - $beg ) / $step > 20 ) {
73 for ( $i = $beg; $i < $end; $i++ )
75 if ( ( $i % $step ) == 0 )
77 $txt = "|" . Maasha::Calc::commify( $i );
78 $x = sprintf( "%.0f", ( ( $i - $beg ) * $factor ) + 2 );
80 if ( $x > 0 and $x + ( $cookie->{ 'RULER_FONT_SIZE' } * length $txt ) < $cookie->{ 'IMG_WIDTH' } )
85 font_size => $cookie->{ 'RULER_FONT_SIZE' },
86 color => $cookie->{ 'RULER_COLOR' },
88 y1 => $cookie->{ 'TRACK_OFFSET' },
94 $cookie->{ 'TRACK_OFFSET' } += $cookie->{ 'TRACK_SPACE' };
96 return wantarray ? @ruler : \@ruler;
102 # Martin A. Hansen, November 2009.
104 # Create a sequence track by extracting the appropriate
105 # stretch of sequence from the sequence file.
107 my ( $cookie, # browser cookie
112 my ( $file, $fh, $seq, @chars, $factor, $i, @seq_list );
114 if ( $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' } + 1 <= 220 )
116 $file = path_seq( $cookie );
117 $fh = Maasha::Filesys::file_read_open( $file );
118 $seq = Maasha::Filesys::file_read( $fh, $cookie->{ 'NAV_START' } - 1, $cookie->{ 'NAV_END' } - $cookie->{ 'NAV_START' } + 1 );
121 @chars = split //, $seq;
123 $factor = $cookie->{ 'IMG_WIDTH' } / @chars;
125 for ( $i = 0; $i < @chars; $i++ ) {
129 font_size => $cookie->{ 'SEQ_FONT_SIZE' },
130 color => $cookie->{ 'SEQ_COLOR' },
131 x1 => sprintf( "%.0f", $i * $factor ),
132 y1 => $cookie->{ 'TRACK_OFFSET' },
136 $cookie->{ 'TRACK_OFFSET' } += $cookie->{ 'TRACK_SPACE' };
138 return wantarray ? @seq_list : \@seq_list;
149 # Martin A. Hansen, November 2009.
151 # Create a track with features. If there are more than $cookie->FEAT_MAX
152 # features the track created will be a histogram, else linear.
154 my ( $track, # path to kiss file with track data
155 $cookie, # cookie hash
160 my ( $index, $count, $track_name, $start, $end, $entries, $features );
162 $start = $cookie->{ 'NAV_START' };
163 $end = $cookie->{ 'NAV_END' };
165 $index = Maasha::KISS::kiss_index_retrieve( "$track/track_data.kiss.index" );
166 $count = Maasha::KISS::kiss_index_count( $index, $start, $end );
168 $track_name = ( split "/", $track )[ -1 ];
169 $track_name =~ s/^\d+_//;
170 $track_name =~ s/_/ /g;
175 font_size => $cookie->{ 'SEQ_FONT_SIZE' },
176 color => $cookie->{ 'SEQ_COLOR' },
178 y1 => $cookie->{ 'TRACK_OFFSET' },
181 $cookie->{ 'TRACK_OFFSET' } += 10;
183 if ( $count > $cookie->{ 'FEAT_MAX' } )
185 $entries = Maasha::KISS::kiss_index_get_blocks( $index, $start, $end );
186 push @{ $features }, track_feature_histogram( $cookie, $start, $end, $entries );
190 $entries = Maasha::KISS::kiss_index_get_entries( "$track/track_data.kiss", $index, $start, $end );
191 push @{ $features }, track_feature_linear( $cookie, $start, $end, $entries );
194 return wantarray ? @{ $features } : $features;
198 sub track_feature_linear
200 # Martin A. Hansen, November 2009.
202 # Create a linear feature track where the granularity depends
203 # on the lenght of the features and the browser window width.
205 my ( $cookie, # hashref with image draw metrics
206 $beg, # base window beg
207 $end, # base window end
208 $entries, # list of unsorted KISS entries
213 my ( $factor, $entry, $y_step, @ladder, $y_max, $w, $x1, $y1, $x2, $y2, @features );
215 @{ $entries } = sort { $a->{ 'S_BEG' } <=> $b->{ 'S_BEG' } or $a->{ 'S_END' } <=> $b->{ 'S_END' } } @{ $entries };
217 $factor = $cookie->{ 'IMG_WIDTH' } / ( $end - $beg + 1 );
221 foreach $entry ( @{ $entries } )
223 $w = sprintf( "%.0f", ( $entry->{ 'S_END' } - $entry->{ 'S_BEG' } + 1 ) * $factor );
227 $x1 = sprintf( "%.0f", ( $entry->{ 'S_BEG' } - $beg ) * $factor );
229 for ( $y_step = 0; $y_step < @ladder; $y_step++ ) {
230 last if $x1 >= $ladder[ $y_step ] + 1;
233 $y1 = $cookie->{ 'TRACK_OFFSET' } + ( ( 0.1 + $cookie->{ 'FEAT_WIDTH' } ) * $y_step );
237 line_width => $cookie->{ 'FEAT_WIDTH' },
238 color => $cookie->{ 'FEAT_COLOR' },
239 title => "Q_ID: $entry->{ 'Q_ID' } S_BEG: $entry->{ 'S_BEG' } S_END: $entry->{ 'S_END' } STRAND: $entry->{ 'STRAND' }",
240 q_id => $entry->{ 'Q_ID' },
241 s_beg => $entry->{ 'S_BEG' },
242 s_end => $entry->{ 'S_END' },
243 strand => $entry->{ 'STRAND' },
247 y2 => $y1 + $cookie->{ 'FEAT_WIDTH' },
250 $y_max = Maasha::Calc::max( $y_max, $y_step * ( 0.1 + $cookie->{ 'FEAT_WIDTH' } ) );
252 push @features, feature_align( $entry, $beg, $y1, $factor, $cookie->{ 'FEAT_WIDTH' } ) if $entry->{ 'ALIGN' } ne '.';
254 $ladder[ $y_step ] = $x1 + $w;
258 $cookie->{ 'TRACK_OFFSET' } += $y_max + $cookie->{ 'TRACK_SPACE' };
260 return wantarray ? @features : \@features;
266 # Martin A. Hansen, November 2009.
268 # Add to feature track alignment info if the granularity is
270 # TODO: The printing of chars is imprecise.
272 my ( $entry, # Partial KISS entry
273 $beg, # base window beg
274 $y_offset, # y axis draw offset
275 $factor, # scale factor
276 $feat_height, # hight of feature in pixels
281 my ( $w, $align, $pos, $nt_before, $nt_after, $x1, @features );
283 $w = sprintf( "%.0f", 1 * $factor );
287 foreach $align ( split /,/, $entry->{ 'ALIGN' } )
289 if ( $align =~ /(\d+):([ATCGN-])>([ATCGN-])/ )
297 Maasha::Common::error( qq(BAD align descriptor: "$align") );
300 $x1 = sprintf( "%.0f", ( $entry->{ 'S_BEG' } + $pos - $beg ) * $factor );
304 line_width => $feat_height,
305 color => [ 1, 0, 0 ],
310 y2 => $y_offset + $feat_height,
313 if ( $w > $feat_height )
317 font_size => $feat_height + 2,
318 color => [ 0, 0, 0 ],
320 x1 => $x1 + sprintf( "%.0f", ( $w / 2 ) ) - $feat_height / 2,
321 y1 => $y_offset + $feat_height,
327 return wantarray ? @features : \@features;
331 sub track_feature_histogram
333 # Martin A. Hansen, November 2009.
335 # Create a feature track as a histogram using information
336 # from the index only thus avoiding to load features from the
339 my ( $cookie, # hashref with image draw metrics
340 $min, # minimum base position
341 $max, # maximum base position
342 $blocks, # list of blocks
347 my ( $hist_height, $bucket_width, $bucket_count, $min_bucket, $factor, $factor_heigth, $max_height, $block, $bucket_beg, $bucket_end, $i, @buckets, $h, $x, @hist );
349 return if $max <= $min;
351 $hist_height = 100; # pixels
353 $bucket_count = $cookie->{ 'IMG_WIDTH' } / $bucket_width;
354 $factor = ( $cookie->{ 'IMG_WIDTH' } / $bucket_width ) / ( $max - $min + 1 );
356 $min_bucket = 999999999;
359 foreach $block ( @{ $blocks } )
361 $bucket_beg = int( $block->{ 'BEG' } * $factor );
362 $bucket_end = int( $block->{ 'END' } * $factor );
364 $min_bucket = Maasha::Calc::min( $min_bucket, $bucket_beg );
366 for ( $i = $bucket_beg; $i <= $bucket_end; $i++ )
368 $buckets[ $i ] += $block->{ 'COUNT' };
370 $max_height = Maasha::Calc::max( $max_height, $buckets[ $i ] );
374 if ( $max_height > 0 )
376 $factor_heigth = $hist_height / $max_height;
380 for ( $i = $min_bucket; $i < @buckets; $i++ )
382 if ( defined $buckets[ $i ] )
384 $h = sprintf( "%.0f", $buckets[ $i ] * $factor_heigth );
390 line_width => $bucket_width,
391 color => $cookie->{ 'FEAT_COLOR' },
392 title => "Features: $buckets[ $i ]",
394 y1 => $cookie->{ 'TRACK_OFFSET' } + $hist_height,
396 y2 => $cookie->{ 'TRACK_OFFSET' } + $hist_height - $h,
405 $cookie->{ 'TRACK_OFFSET' } += $hist_height + $cookie->{ 'TRACK_SPACE' };
407 return wantarray ? @hist : \@hist;
413 # Martin A. Hansen, November 2009.
415 # Returns the sequence from the contig in the beg/end interval
416 # contained in the cookie.
418 my ( $cookie, # cookie hash
423 my ( $path, $fh, $beg, $end, $len, $dna );
425 $path = path_seq( $cookie );
427 $beg = $cookie->{ 'S_BEG' };
428 $end = $cookie->{ 'S_END' };
431 $len = $end - $beg + 1;
434 $fh = Maasha::Filesys::file_read_open( $path );
436 $dna = Maasha::Filesys::file_read( $fh, $beg, $len );
438 $dna = Maasha::Seq::dna_revcomp( $dna ) if $cookie->{ 'STRAND' } eq '-';
440 Maasha::Seq::wrap( \$dna, 100 );
450 # Martin A. Hansen, November 2009.
452 # Returns the path to the sequence file for a specified
453 # contig as written in the cookie.
455 my ( $cookie, # cookie hash
462 die qq(ERROR: no USER in cookie.\n) if not $cookie->{ 'USER' };
463 die qq(ERROR: no CLADE in cookie.\n) if not $cookie->{ 'CLADE' };
464 die qq(ERROR: no GENOME in cookie.\n) if not $cookie->{ 'GENOME' };
465 die qq(ERROR: no ASSEMBLY in cookie.\n) if not $cookie->{ 'ASSEMBLY' };
466 die qq(ERROR: no CONTIG in cookie.\n) if not $cookie->{ 'CONTIG' };
469 $cookie->{ 'DATA_DIR' },
472 $cookie->{ 'CLADE' },
473 $cookie->{ 'GENOME' },
474 $cookie->{ 'ASSEMBLY' },
475 $cookie->{ 'CONTIG' },
480 die qq(ERROR: no such file: "$path".\n) if not -e $path;
488 # Martin A. Hansen, November 2009.
490 # Returns a list of paths to all tracks for a specified
491 # contig as written in the cookie.
493 my ( $cookie, # cookie path
498 my ( $path, @tracks );
500 die qq(ERROR: no USER in cookie.\n) if not $cookie->{ 'USER' };
501 die qq(ERROR: no CLADE in cookie.\n) if not $cookie->{ 'CLADE' };
502 die qq(ERROR: no GENOME in cookie.\n) if not $cookie->{ 'GENOME' };
503 die qq(ERROR: no ASSEMBLY in cookie.\n) if not $cookie->{ 'ASSEMBLY' };
504 die qq(ERROR: no CONTIG in cookie.\n) if not $cookie->{ 'CONTIG' };
507 $cookie->{ 'DATA_DIR' },
510 $cookie->{ 'CLADE' },
511 $cookie->{ 'GENOME' },
512 $cookie->{ 'ASSEMBLY' },
513 $cookie->{ 'CONTIG' },
519 @tracks = Maasha::Filesys::ls_dirs( $path );
521 @tracks = grep { $_ !~ /\/\.\.?$/ } @tracks;
523 return wantarray ? @tracks : \@tracks;
527 return wantarray ? () : [];
534 # Martin A. Hansen, December 2009.
536 # Uses grep to search all tracks in all contigs
537 # for a given pattern and return a list of KISS entries.
539 my ( $cookie, # cookie hash
544 my ( $contig, @tracks, $track, $file, $line, $out_file, $fh, $entry, @entries );
546 foreach $contig ( @{ $cookie->{ 'LIST_CONTIG' } } )
548 $cookie->{ 'CONTIG' } = $contig;
550 push @tracks, path_tracks( $cookie );
553 foreach $track ( @tracks )
555 $file = "$track/track_data.kiss";
559 $fh = Maasha::Filesys::file_read_open( $file );
561 while ( $line = <$fh> )
565 if ( $line =~ /$cookie->{ 'SEARCH' }/i )
567 $entry = Maasha::KISS::kiss_entry_parse( $line );
569 push @entries, $entry;
577 return wantarray ? @entries : \@entries;
581 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<