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 parsing and emitting KISS records.
27 # http://code.google.com/p/biopieces/wiki/KissFormat
30 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
39 use vars qw( @ISA @EXPORT );
41 @ISA = qw( Exporter );
56 INDEX_BLOCK_SIZE => 100,
57 INDEX_LEVEL => 100_000_000,
62 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
67 my ( $fh, # file handle
72 my ( $line, @fields, %entry );
74 while ( $line = <$fh> )
78 next if $line =~ /^$|^#/;
80 @fields = split /\t/, $line;
82 Maasha::Common::error( qq(BAD kiss entry: $line) ) if not @fields == 12;
84 $entry{ 'S_ID' } = $fields[ S_ID ];
85 $entry{ 'S_BEG' } = $fields[ S_BEG ];
86 $entry{ 'S_END' } = $fields[ S_END ];
87 $entry{ 'Q_ID' } = $fields[ Q_ID ];
88 $entry{ 'SCORE' } = $fields[ SCORE ];
89 $entry{ 'STRAND' } = $fields[ STRAND ];
90 $entry{ 'HITS' } = $fields[ HITS ];
91 $entry{ 'ALIGN' } = $fields[ ALIGN ];
92 $entry{ 'BLOCK_COUNT' } = $fields[ BLOCK_COUNT ];
93 $entry{ 'BLOCK_BEGS' } = $fields[ BLOCK_BEGS ];
94 $entry{ 'BLOCK_LENS' } = $fields[ BLOCK_LENS ];
95 $entry{ 'BLOCK_TYPE' } = $fields[ BLOCK_TYPE ];
97 return wantarray ? %entry : \%entry;
104 my ( $entry, # KISS entry to output
105 $fh, # file handle - OPTIONAL
112 if ( defined $entry->{ 'S_ID' } and
113 defined $entry->{ 'S_BEG' } and
114 defined $entry->{ 'S_END' }
119 $fields[ S_ID ] = $entry->{ 'S_ID' };
120 $fields[ S_BEG ] = $entry->{ 'S_BEG' };
121 $fields[ S_END ] = $entry->{ 'S_END' };
122 $fields[ Q_ID ] = $entry->{ 'Q_ID' };
123 $fields[ SCORE ] = $entry->{ 'SCORE' };
124 $fields[ STRAND ] = $entry->{ 'STRAND' };
125 $fields[ HITS ] = $entry->{ 'HITS' };
126 $fields[ ALIGN ] = $entry->{ 'ALIGN' };
127 $fields[ BLOCK_COUNT ] = $entry->{ 'BLOCK_COUNT' };
128 $fields[ BLOCK_BEGS ] = $entry->{ 'BLOCK_BEGS' };
129 $fields[ BLOCK_LENS ] = $entry->{ 'BLOCK_LENS' };
130 $fields[ BLOCK_TYPE ] = $entry->{ 'BLOCK_TYPE' };
132 print $fh join( "\t", @fields ), "\n";
139 # Martin A. Hansen, November 2009.
141 # Sorts a KISS file on S_BEG and S_END
143 my ( $file, # KISS file
148 `sort -k 2,2n -k 3,3n $file > $file.sort`;
150 rename "$file.sort", $file;
156 # Martin A, Hansen, November 2009.
158 # Creates an index of a sorted KISS file.
160 my ( $file, # KISS file
165 my ( $tree, $offset, $fh, $line, $beg );
170 $fh = Maasha::Filesys::file_read_open( $file );
172 while ( $line = <$fh> )
174 ( undef, $beg ) = split "\t", $line, 3;
176 kiss_index_node_add( $tree, INDEX_LEVEL, INDEX_FACTOR, $beg, $offset );
178 $offset += length $line;
183 kiss_index_store( "$file.index", $tree );
187 sub kiss_index_node_add
189 # Martin A, Hansen, November 2009.
191 # Recursive routine to add nodes to a tree.
204 $bucket = int( $beg / $level );
206 if ( $level >= $factor )
208 $sum += $bucket * $level;
209 $beg -= $bucket * $level;
211 $node->{ 'CHILDREN' }->[ $bucket ]->{ 'COUNT' }++;
212 # $node->{ 'CHILDREN' }->[ $bucket ]->{ 'LEVEL' } = $level;
213 # $node->{ 'CHILDREN' }->[ $bucket ]->{ 'BUCKET' } = $bucket;
214 $node->{ 'CHILDREN' }->[ $bucket ]->{ 'BEG' } = $sum;
215 $node->{ 'CHILDREN' }->[ $bucket ]->{ 'END' } = $sum + $level - 1;
216 $node->{ 'CHILDREN' }->[ $bucket ]->{ 'OFFSET' } = $offset if not defined $node->{ 'CHILDREN' }->[ $bucket ]->{ 'OFFSET' };
218 kiss_index_node_add( $node->{ 'CHILDREN' }->[ $bucket ], $level / $factor, $factor, $beg, $offset, $sum );
223 sub kiss_index_offset
225 # Martin A. Hansen, November 2009.
227 # Given a KISS index and a begin position,
228 # locate the offset closest to the begin position,
231 my ( $index, # KISS index
232 $beg, # begin position
233 $level, # index level - OPTIONAL
234 $factor, # index factor - OPTIONAL
239 my ( $child, $offset );
241 $level ||= INDEX_LEVEL;
242 $factor ||= INDEX_FACTOR;
244 foreach $child ( @{ $index->{ 'CHILDREN' } } )
246 next if not defined $child;
248 if ( $child->{ 'BEG' } <= $beg and $beg <= $child->{ 'END' } )
250 if ( $level == $factor ) {
251 $offset = $child->{ 'OFFSET' };
253 $offset = kiss_index_offset( $child, $beg, $level / $factor, $factor );
264 # Martin A. Hansen, November 2009.
266 # Given a KISS index and a begin/end interval
267 # sum the number of counts in that interval,
270 my ( $index, # KISS index
271 $beg, # Begin position
273 $level, # index level - OPTIONAL
274 $factor, # index factor - OPTIONAL
279 my ( $count, $child );
281 $level ||= INDEX_LEVEL;
282 $factor ||= INDEX_FACTOR;
285 foreach $child ( @{ $index->{ 'CHILDREN' } } )
287 next if not defined $child;
289 if ( $level >= $factor )
291 if ( Maasha::Calc::overlap( $beg, $end, $child->{ 'BEG' }, $child->{ 'END' } ) )
293 $count += $child->{ 'COUNT' } if $level == $factor;
294 $count += kiss_index_count( $child, $beg, $end, $level / $factor, $factor );
309 Maasha::Filesys::file_store( $path, $index );
313 sub kiss_index_retrieve
320 $index = Maasha::Filesys::file_retrieve( $path );
322 return wantarray ? @{ $index } : $index;
326 sub kiss_index_get_entries
334 my ( $offset, $fh, $entry, @entries );
336 $offset = kiss_index_offset( $index, $beg );
338 $fh = Maasha::Filesys::file_read_open( $file );
340 sysseek( $fh, $offset, 0 );
342 while ( $entry = kiss_entry_get( $fh ) )
344 push @entries, $entry if $entry->{ 'S_END' } > $beg;
346 last if $entry->{ 'S_BEG' } > $end;
351 return wantarray ? @entries : \@entries;
355 sub kiss_index_get_blocks
360 $level, # index level - OPTIONAL
361 $factor, # index factor - OPTIONAL
367 my ( $len, @blocks, $child );
369 $level ||= INDEX_LEVEL;
370 $factor ||= INDEX_FACTOR;
372 $size ||= 100; # TODO: lazy list loading?
374 # if ( not defined $size )
376 # $len = $end - $beg + 1;
378 # if ( $len > 100_000_000 ) {
380 # } elsif ( $len > 1_000_000 ) {
387 if ( $level >= $size )
389 foreach $child ( @{ $index->{ 'CHILDREN' } } )
391 next if not defined $child;
393 if ( Maasha::Calc::overlap( $beg, $end, $child->{ 'BEG' }, $child->{ 'END' } ) )
395 if ( $level == $size )
398 BEG => $child->{ 'BEG' },
399 END => $child->{ 'END' },
400 COUNT => $child->{ 'COUNT' },
404 push @blocks, kiss_index_get_blocks( $child, $beg, $end, $level / $factor, $factor, $size );
409 return wantarray ? @blocks : \@blocks;
415 # S.aur_COL 41 75 5_vnlh2ywXsN1/1 1 - . 17:A>T,31:A>N . . . .
417 my ( $s_seqref, # subject sequence reference
423 my ( $align, $pos, $s_symbol, $q_symbol, $s_seq, $q_seq, $insertions );
425 $s_seq = substr ${ $s_seqref }, $entry->{ 'S_BEG' }, $entry->{ 'S_END' } - $entry->{ 'S_BEG' } + 1;
430 foreach $align ( split /,/, $entry->{ 'ALIGN' } )
432 if ( $align =~ /(\d+):(.)>(.)/ )
438 if ( $s_symbol eq '-' ) # insertion
440 substr $s_seq, $pos + $insertions, 0, $s_symbol;
441 substr $q_seq, $pos + $insertions, 0, $q_symbol;
445 elsif ( $q_symbol eq '-' ) # deletion
447 substr $q_seq, $pos + $insertions, 1, $q_symbol;
451 substr $q_seq, $pos + $insertions, 1, $q_symbol;
456 Maasha::Common::error( qq(Bad alignment descriptor: "$align") );
460 Maasha::Align::align_print_pairwise( [ $entry->{ 'S_ID' }, $s_seq ], [ $entry->{ 'Q_ID' }, $q_seq ] );
466 my ( $entry, # KISS entry
469 return wantarray ? %{ $entry } : $entry;
475 my ( $record, # Biopiece record
478 $record->{ 'SCORE' } ||= $record->{ 'E_VAL' } || ".";
479 $record->{ 'HITS' } ||= ".";
480 $record->{ 'BLOCK_COUNT' } ||= ".";
481 $record->{ 'BLOCK_BEGS' } ||= ".";
482 $record->{ 'BLOCK_LENS' } ||= ".";
483 $record->{ 'BLOCK_TYPE' } ||= ".";
484 $record->{ 'ALIGN' } ||= $record->{ 'DESCRIPTOR' } || ".";
486 return wantarray ? %{ $record } : $record;
490 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
498 # Martin A, Hansen, October 2009.
500 # Creates an index of a sorted KISS file that
501 # allowing the location of the byte position
502 # from where records can be read given a
503 # specific S_BEG position. The index consists of
504 # triples: [ beg, end, bytepos ], where beg and
505 # end denotes the interval where the next KISS
506 # record begins at bytepos.
508 my ( $fh, # filehandle to KISS file
513 my ( $line, @fields, $beg, $end, $pos, @index );
518 while ( $line = <$fh> )
522 @fields = split /\t/, $line, 3;
524 $end = $fields[ S_BEG ];
528 push @index, [ $beg, $end, $pos ];
531 elsif ( $end > $beg )
533 push @index, [ $beg, $end - 1, $pos ];
536 elsif ( $end < $beg )
538 Maasha::Common::error( qq(KISS file not sorted: beg > end -> $beg > $end) );
541 $pos += 1 + length $line;
544 return wantarray ? @index : \@index;
548 sub kiss_index_search
556 my ( $high, $low, $try );
559 $high = scalar @{ $index };
561 while ( $low <= $high )
563 $try = int( ( $high + $low ) / 2 );
565 if ( $num < $index->[ $try ]->[ 0 ] ) {
567 } elsif ( $num > $index->[ $try ]->[ 1 ] ) {
570 return $index->[ $try ]->[ 2 ];
574 Maasha::Common::error( "Could not find number->$num in index" );