1 package Maasha::KISS::IO;
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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
40 use vars qw( @ISA @EXPORT );
42 @ISA = qw( Exporter );
57 INDEX_BLOCK_SIZE => 100,
58 INDEX_LEVEL => 100_000_000,
63 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
68 my ( $fh, # file handle
73 my ( $line, @fields, %entry );
75 while ( $line = <$fh> )
79 next if $line =~ /^$|^#/;
81 @fields = split /\t/, $line;
83 Maasha::Common::error( qq(BAD kiss entry: $line) ) if not @fields == 12;
85 $entry{ 'S_ID' } = $fields[ S_ID ];
86 $entry{ 'S_BEG' } = $fields[ S_BEG ];
87 $entry{ 'S_END' } = $fields[ S_END ];
88 $entry{ 'Q_ID' } = $fields[ Q_ID ];
89 $entry{ 'SCORE' } = $fields[ SCORE ];
90 $entry{ 'STRAND' } = $fields[ STRAND ];
91 $entry{ 'HITS' } = $fields[ HITS ];
92 $entry{ 'ALIGN' } = $fields[ ALIGN ];
93 $entry{ 'BLOCK_COUNT' } = $fields[ BLOCK_COUNT ];
94 $entry{ 'BLOCK_BEGS' } = $fields[ BLOCK_BEGS ];
95 $entry{ 'BLOCK_LENS' } = $fields[ BLOCK_LENS ];
96 $entry{ 'BLOCK_TYPE' } = $fields[ BLOCK_TYPE ];
98 return wantarray ? %entry : \%entry;
105 my ( $entry, # KISS entry to output
106 $fh, # file handle - OPTIONAL
113 if ( defined $entry->{ 'S_ID' } and
114 defined $entry->{ 'S_BEG' } and
115 defined $entry->{ 'S_END' }
120 $fields[ S_ID ] = $entry->{ 'S_ID' };
121 $fields[ S_BEG ] = $entry->{ 'S_BEG' };
122 $fields[ S_END ] = $entry->{ 'S_END' };
123 $fields[ Q_ID ] = $entry->{ 'Q_ID' };
124 $fields[ SCORE ] = $entry->{ 'SCORE' };
125 $fields[ STRAND ] = $entry->{ 'STRAND' };
126 $fields[ HITS ] = $entry->{ 'HITS' };
127 $fields[ ALIGN ] = $entry->{ 'ALIGN' };
128 $fields[ BLOCK_COUNT ] = $entry->{ 'BLOCK_COUNT' };
129 $fields[ BLOCK_BEGS ] = $entry->{ 'BLOCK_BEGS' };
130 $fields[ BLOCK_LENS ] = $entry->{ 'BLOCK_LENS' };
131 $fields[ BLOCK_TYPE ] = $entry->{ 'BLOCK_TYPE' };
133 print $fh join( "\t", @fields ), "\n";
140 my ( $dbh, # Database handle
142 $s_beg, # Subject begin
143 $s_end, # Subject end
146 my ( $sql, $entries );
148 # $sql = "SELECT * FROM $table WHERE S_BEG >= $s_beg AND S_END <= $s_end ORDER BY S_BEG,S_END";
149 $sql = "SELECT S_BEG,S_END,Q_ID,ALIGN FROM $table WHERE S_BEG >= $s_beg AND S_END <= $s_end";
151 $entries = Maasha::SQL::query_hashref_list( $dbh, $sql );
153 return wantarray ? @{ $entries } : $entries;
159 # Martin A, Hansen, November 2009.
161 # Creates an index of a sorted KISS file.
163 my ( $file, # KISS file to index
168 my ( $tree, $offset, $fh, $line, $beg );
173 $fh = Maasha::Filesys::file_read_open( $file );
175 while ( $line = <$fh> )
177 ( undef, $beg ) = split "\t", $line, 3;
179 kiss_index_node_add( $tree, INDEX_LEVEL, INDEX_FACTOR, $beg, $offset );
181 $offset += length $line;
186 kiss_index_store( "$file.index", $tree );
190 sub kiss_index_node_add
192 # Martin A, Hansen, November 2009.
194 # Recursive routine to add nodes to a tree.
207 $bucket = int( $beg / $level );
209 if ( $level >= $factor )
211 $sum += $bucket * $level;
212 $beg -= $bucket * $level;
214 $node->{ 'CHILDREN' }->[ $bucket ]->{ 'COUNT' }++;
215 # $node->{ 'CHILDREN' }->[ $bucket ]->{ 'LEVEL' } = $level;
216 # $node->{ 'CHILDREN' }->[ $bucket ]->{ 'BUCKET' } = $bucket;
217 $node->{ 'CHILDREN' }->[ $bucket ]->{ 'BEG' } = $sum;
218 $node->{ 'CHILDREN' }->[ $bucket ]->{ 'END' } = $sum + $level - 1;
219 $node->{ 'CHILDREN' }->[ $bucket ]->{ 'OFFSET' } = $offset if not defined $node->{ 'CHILDREN' }->[ $bucket ]->{ 'OFFSET' };
221 kiss_index_node_add( $node->{ 'CHILDREN' }->[ $bucket ], $level / $factor, $factor, $beg, $offset, $sum );
226 sub kiss_index_offset
228 # Martin A. Hansen, November 2009.
230 # Given a KISS index and a begin position,
231 # locate the offset closest to the begin position,
234 my ( $index, # KISS index
235 $beg, # begin position
236 $level, # index level - OPTIONAL
237 $factor, # index factor - OPTIONAL
242 my ( $child, $offset );
244 $level ||= INDEX_LEVEL;
245 $factor ||= INDEX_FACTOR;
247 foreach $child ( @{ $index->{ 'CHILDREN' } } )
249 next if not defined $child;
251 if ( $child->{ 'BEG' } <= $beg and $beg <= $child->{ 'END' } )
253 if ( $level == $factor ) {
254 $offset = $child->{ 'OFFSET' };
256 $offset = kiss_index_offset( $child, $beg, $level / $factor, $factor );
267 # Martin A. Hansen, November 2009.
269 # Given a KISS index and a begin/end interval
270 # sum the number of counts in that interval,
273 my ( $index, # KISS index
274 $beg, # Begin position
276 $level, # index level - OPTIONAL
277 $factor, # index factor - OPTIONAL
282 my ( $count, $child );
284 $level ||= INDEX_LEVEL;
285 $factor ||= INDEX_FACTOR;
288 foreach $child ( @{ $index->{ 'CHILDREN' } } )
290 next if not defined $child;
292 if ( $level >= $factor )
294 if ( Maasha::Calc::overlap( $beg, $end, $child->{ 'BEG' }, $child->{ 'END' } ) )
296 $count += $child->{ 'COUNT' } if $level == $factor;
297 $count += kiss_index_count( $child, $beg, $end, $level / $factor, $factor );
312 Maasha::Filesys::file_store( $path, $index );
316 sub kiss_index_retrieve
323 $index = Maasha::Filesys::file_retrieve( $path );
325 return wantarray ? @{ $index } : $index;
329 sub kiss_index_get_entries
337 my ( $offset, $fh, $entry, @entries );
339 $offset = kiss_index_offset( $index, $beg );
341 $fh = Maasha::Filesys::file_read_open( $file );
343 sysseek( $fh, $offset, 0 );
345 while ( $entry = kiss_entry_get( $fh ) )
347 push @entries, $entry if $entry->{ 'S_END' } > $beg;
349 last if $entry->{ 'S_BEG' } > $end;
354 return wantarray ? @entries : \@entries;
358 sub kiss_index_get_blocks
363 $level, # index level - OPTIONAL
364 $factor, # index factor - OPTIONAL
370 my ( $len, @blocks, $child );
372 $level ||= INDEX_LEVEL;
373 $factor ||= INDEX_FACTOR;
375 $size ||= 100; # TODO: lazy list loading?
377 # if ( not defined $size )
379 # $len = $end - $beg + 1;
381 # if ( $len > 100_000_000 ) {
383 # } elsif ( $len > 1_000_000 ) {
390 if ( $level >= $size )
392 foreach $child ( @{ $index->{ 'CHILDREN' } } )
394 next if not defined $child;
396 if ( Maasha::Calc::overlap( $beg, $end, $child->{ 'BEG' }, $child->{ 'END' } ) )
398 if ( $level == $size )
401 BEG => $child->{ 'BEG' },
402 END => $child->{ 'END' },
403 COUNT => $child->{ 'COUNT' },
407 push @blocks, kiss_index_get_blocks( $child, $beg, $end, $level / $factor, $factor, $size );
412 return wantarray ? @blocks : \@blocks;
418 # S.aur_COL 41 75 5_vnlh2ywXsN1/1 1 - . 17:A>T,31:A>N . . . .
420 my ( $s_seqref, # subject sequence reference
426 my ( $align, $pos, $s_symbol, $q_symbol, $s_seq, $q_seq, $insertions );
428 $s_seq = substr ${ $s_seqref }, $entry->{ 'S_BEG' }, $entry->{ 'S_END' } - $entry->{ 'S_BEG' } + 1;
433 foreach $align ( split /,/, $entry->{ 'ALIGN' } )
435 if ( $align =~ /(\d+):(.)>(.)/ )
441 if ( $s_symbol eq '-' ) # insertion
443 substr $s_seq, $pos + $insertions, 0, $s_symbol;
444 substr $q_seq, $pos + $insertions, 0, $q_symbol;
448 elsif ( $q_symbol eq '-' ) # deletion
450 substr $q_seq, $pos + $insertions, 1, $q_symbol;
454 substr $q_seq, $pos + $insertions, 1, $q_symbol;
459 Maasha::Common::error( qq(Bad alignment descriptor: "$align") );
463 Maasha::Align::align_print_pairwise( [ $entry->{ 'S_ID' }, $s_seq ], [ $entry->{ 'Q_ID' }, $q_seq ] );
469 my ( $entry, # KISS entry
472 return wantarray ? %{ $entry } : $entry;
478 my ( $record, # Biopiece record
481 $record->{ 'SCORE' } ||= $record->{ 'E_VAL' } || ".";
482 $record->{ 'HITS' } ||= ".";
483 $record->{ 'BLOCK_COUNT' } ||= ".";
484 $record->{ 'BLOCK_BEGS' } ||= ".";
485 $record->{ 'BLOCK_LENS' } ||= ".";
486 $record->{ 'BLOCK_TYPE' } ||= ".";
487 $record->{ 'ALIGN' } ||= $record->{ 'DESCRIPTOR' } || ".";
489 return wantarray ? %{ $record } : $record;
493 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
501 # Martin A, Hansen, October 2009.
503 # Creates an index of a sorted KISS file that
504 # allowing the location of the byte position
505 # from where records can be read given a
506 # specific S_BEG position. The index consists of
507 # triples: [ beg, end, bytepos ], where beg and
508 # end denotes the interval where the next KISS
509 # record begins at bytepos.
511 my ( $fh, # filehandle to KISS file
516 my ( $line, @fields, $beg, $end, $pos, @index );
521 while ( $line = <$fh> )
525 @fields = split /\t/, $line, 3;
527 $end = $fields[ S_BEG ];
531 push @index, [ $beg, $end, $pos ];
534 elsif ( $end > $beg )
536 push @index, [ $beg, $end - 1, $pos ];
539 elsif ( $end < $beg )
541 Maasha::Common::error( qq(KISS file not sorted: beg > end -> $beg > $end) );
544 $pos += 1 + length $line;
547 return wantarray ? @index : \@index;
552 sub kiss_index_search
560 my ( $high, $low, $try );
563 $high = scalar @{ $index };
565 while ( $low <= $high )
567 $try = int( ( $high + $low ) / 2 );
569 if ( $num < $index->[ $try ]->[ 0 ] ) {
571 } elsif ( $num > $index->[ $try ]->[ 1 ] ) {
574 return $index->[ $try ]->[ 2 ];
578 Maasha::Common::error( "Could not find number->$num in index" );