1 package Maasha::BGB::Wiggle;
3 # Copyright (C) 2010 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 wiggle tracks.
28 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
40 use vars qw( @ISA @EXPORT );
42 @ISA = qw( Exporter );
59 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
64 # Martin A. Hansen, February 2010.
66 # Read a KISS file and encode a Wiggle list.
68 my ( $file, # KISS file
73 my ( $fh, $entry, $vals, $i );
75 $fh = Maasha::Filesys::file_read_open( $file );
77 while ( $entry = Maasha::KISS::kiss_entry_get( $fh ) ) {
78 map { $vals->[ $_ ]++ } ( $entry->[ S_BEG ] .. $entry->[ S_END ] );
83 for ( $i = 0; $i < scalar @{ $vals }; $i++ ) {
84 $vals->[ $i ] = 0 if not defined $vals->[ $i ];
87 return wantarray ? @{ $vals } : $vals;
93 my ( $vals, # Wiggle values
94 $size, # New list size
99 if ( scalar @{ $vals } < $size ) {
100 Maasha::Matrix::list_inflate( $vals, $size );
101 } elsif ( scalar @{ $vals } > $size ) {
102 Maasha::Matrix::list_deflate( $vals, $size );
105 return wantarray ? @{ $vals } : $vals;
111 # Martin A. Hansen, February 2010.
113 # Store a list of wiggle values as a byte array to a
116 my ( $file, # path to file
117 $vals, # Wiggle values
124 $bin = pack( "S*", @{ $vals } );
126 $fh = Maasha::Filesys::file_write_open( $file );
136 # Martin A. Hansen, February 2010.
138 # Restore an interval of Wiggle values from
139 # a specified Wiggle file and return these
142 my ( $file, # path to wiggle file
143 $beg, # begin position
149 my ( $fh, $bin, @vals );
151 Maasha::Common::error( qq(begin < 0: $beg) ) if $beg < 0;
152 Maasha::Common::error( qq(begin > end: $beg > $end) ) if $beg > $end;
154 $fh = Maasha::Filesys::file_read_open( $file );
156 sysseek( $fh, $beg * 2, 0 );
157 sysread( $fh, $bin, ( $end - $beg + 1 ) * 2 );
161 @vals = unpack( "S*", $bin );
163 map { push @vals, 0 } ( scalar @vals .. $end - $beg ); # Padding
165 return wantarray ? @vals : \@vals;
169 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<