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 ( $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 };
113 $factor = $width / ( $end - $beg );
116 foreach $entry ( @{ $entries } )
118 $w = sprintf( "%.0f", ( $entry->{ 'S_END' } - $entry->{ 'S_BEG' } + 1 ) * $factor );
122 $x1 = sprintf( "%.0f", ( $entry->{ 'S_BEG' } - $beg ) * $factor );
124 for ( $y_step = 0; $y_step < @ladder; $y_step++ )
126 last if $x1 >= $ladder[ $y_step ] + 1;
129 $y1 = $y_offset + ( 5 * $y_step );
138 $ladder[ $y_step ] = $x1 + $w;
142 return wantarray ? @features : \@features;
148 my ( $width, # draw window width
149 $y_offset, # y axis draw offset
150 $min, # minimum base position
151 $max, # maximum base position
152 $entries, # list of unsorted KISS entries
157 my ( $hist_height, $bucket_width, $factor_heigth, $factor_width, $entry, $min_bucket, $max_height, $bucket_beg, $bucket_end, $i, @buckets, $bucket, @hist, $x, $h );
159 return if $max == $min;
161 $hist_height = 100; # pixels
162 $bucket_width = 5; # pixels
164 $factor_width = ( $width / $bucket_width ) / ( $max - $min );
166 $min_bucket = 999999999;
169 foreach $entry ( @{ $entries } )
171 $bucket_beg = int( $entry->{ 'S_BEG' } * $factor_width );
172 $bucket_end = int( $entry->{ 'S_END' } * $factor_width );
174 $min_bucket = Maasha::Calc::min( $min_bucket, $bucket_beg );
176 for ( $i = $bucket_beg; $i <= $bucket_end; $i++ )
180 $max_height = Maasha::Calc::max( $max_height, $buckets[ $i ] );
184 if ( $max_height > 0 )
186 $factor_heigth = $hist_height / $max_height;
190 for ( $i = $min_bucket; $i < @buckets; $i++ )
192 if ( defined $buckets[ $i ] )
194 $h = sprintf( "%.0f", $buckets[ $i ] * $factor_heigth );
200 y1 => $y_offset + $hist_height,
202 y2 => $y_offset + $hist_height - $h,
211 return wantarray ? @hist : \@hist;
223 $div = $num / $bucket_size;
226 if ( $div - $int >= 0.5 ) {
227 return $bucket_size * ( $int + 1 );
229 return $bucket_size * $int;
234 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<