1 package Maasha::KISS::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 KISS tracks.
28 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
35 use vars qw( @ISA @EXPORT );
37 @ISA = qw( Exporter );
40 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
45 my ( $width, # draw window width
46 $y_offset, # y axis draw offset
47 $beg, # base window beg
48 $end, # base window end
51 my ( $factor, $step, $i, $txt, $x, @ruler );
53 $factor = $width / ( $end - $beg );
57 while ( ( $end - $beg ) / $step > 20 )
62 for ( $i = $beg; $i < $end; $i++ )
64 if ( ( $i % $step ) == 0 )
67 $x = sprintf( "%.0f", ( $i - $beg - length $txt ) * $factor );
69 push @ruler, { txt => $txt, x => $x, y => $y_offset };
73 return wantarray ? @ruler : \@ruler;
79 my ( $width, # draw window width
80 $y_offset, # y axis draw offset
81 $seq, # sequence to draw
84 my ( @chars, $factor, $i, @seq_list );
86 @chars = split //, $seq;
88 $factor = $width / @chars;
90 for ( $i = 0; $i < @chars; $i++ ) {
91 push @seq_list, { txt => $chars[ $i ], x => sprintf( "%.0f", $i * $factor ), y => $y_offset };
94 return wantarray ? @seq_list : \@seq_list;
100 my ( $width, # draw window width
101 $y_offset, # y axis draw offset
102 $beg, # base window beg
103 $end, # base window end
104 $entries, # list of unsorted KISS entries
109 my ( $feat_height, $factor, $entry, $y_step, @ladder, $w, $x1, $y1, $x2, $y2, @features );
111 @{ $entries } = sort { $a->{ 'S_BEG' } <=> $b->{ 'S_BEG' } or $a->{ 'S_END' } <=> $b->{ 'S_END' } } @{ $entries };
114 $factor = $width / ( $end - $beg );
117 foreach $entry ( @{ $entries } )
119 $w = sprintf( "%.0f", ( $entry->{ 'S_END' } - $entry->{ 'S_BEG' } + 1 ) * $factor );
123 $x1 = sprintf( "%.0f", ( $entry->{ 'S_BEG' } - $beg ) * $factor );
125 for ( $y_step = 0; $y_step < @ladder; $y_step++ )
127 last if $x1 >= $ladder[ $y_step ] + 1;
130 $y1 = $y_offset + ( $feat_height * $y_step );
134 line_width => $feat_height,
142 push @features, feature_align( $entry, $beg, $y1, $factor, $feat_height ) if defined $entry->{ 'ALIGN' };
144 $ladder[ $y_step ] = $x1 + $w;
148 return wantarray ? @features : \@features;
156 my ( $entry, # Partial KISS entry
157 $beg, # base window beg
158 $y_offset, # y axis draw offset
159 $factor, # scale factor
160 $feat_height, # hight of feature in pixels
165 my ( $w, $align, $pos, $nt_before, $nt_after, $x1, @features );
167 $w = sprintf( "%.0f", 1 * $factor );
171 foreach $align ( split /,/, $entry->{ 'ALIGN' } )
173 if ( $align =~ /(\d+):(\w)>(\w)/ )
184 $x1 = sprintf( "%.0f", ( $entry->{ 'S_BEG' } + $pos - $beg ) * $factor );
188 line_width => $feat_height,
200 font_size => $feat_height,
203 x1 => $x1 + sprintf( "%.0f", ( $w / 2 ) ) - $feat_height / 2,
210 return wantarray ? @features : \@features;
216 my ( $width, # draw window width
217 $y_offset, # y axis draw offset
218 $min, # minimum base position
219 $max, # maximum base position
220 $entries, # list of unsorted KISS entries
225 my ( $hist_height, $bucket_width, $factor_heigth, $factor_width, $entry, $min_bucket, $max_height, $bucket_beg, $bucket_end, $i, @buckets, $bucket, @hist, $x, $h );
227 return if $max == $min;
229 $hist_height = 100; # pixels
230 $bucket_width = 5; # pixels
232 $factor_width = ( $width / $bucket_width ) / ( $max - $min );
234 $min_bucket = 999999999;
237 foreach $entry ( @{ $entries } )
239 $bucket_beg = int( $entry->{ 'S_BEG' } * $factor_width );
240 $bucket_end = int( $entry->{ 'S_END' } * $factor_width );
242 $min_bucket = Maasha::Calc::min( $min_bucket, $bucket_beg );
244 for ( $i = $bucket_beg; $i <= $bucket_end; $i++ )
248 $max_height = Maasha::Calc::max( $max_height, $buckets[ $i ] );
252 if ( $max_height > 0 )
254 $factor_heigth = $hist_height / $max_height;
258 for ( $i = $min_bucket; $i < @buckets; $i++ )
260 if ( defined $buckets[ $i ] )
262 $h = sprintf( "%.0f", $buckets[ $i ] * $factor_heigth );
268 line_width => $bucket_width,
271 y1 => $y_offset + $hist_height,
273 y2 => $y_offset + $hist_height - $h,
282 return wantarray ? @hist : \@hist;
294 $div = $num / $bucket_size;
297 if ( $div - $int >= 0.5 ) {
298 return $bucket_size * ( $int + 1 );
300 return $bucket_size * $int;
305 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<