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 # Martin A. Hansen, November 2009.
69 # Gets the next KISS entry from a file handle.
71 my ( $fh, # file handle
76 my ( $line, @fields, %entry );
78 while ( $line = <$fh> )
82 next if $line =~ /^$|^#/;
84 @fields = split /\t/, $line;
86 Maasha::Common::error( qq(BAD kiss entry: $line) ) if not @fields == 12;
88 $entry{ 'S_ID' } = $fields[ S_ID ];
89 $entry{ 'S_BEG' } = $fields[ S_BEG ];
90 $entry{ 'S_END' } = $fields[ S_END ];
91 $entry{ 'Q_ID' } = $fields[ Q_ID ];
92 $entry{ 'SCORE' } = $fields[ SCORE ];
93 $entry{ 'STRAND' } = $fields[ STRAND ];
94 $entry{ 'HITS' } = $fields[ HITS ];
95 $entry{ 'ALIGN' } = $fields[ ALIGN ];
96 $entry{ 'BLOCK_COUNT' } = $fields[ BLOCK_COUNT ];
97 $entry{ 'BLOCK_BEGS' } = $fields[ BLOCK_BEGS ];
98 $entry{ 'BLOCK_LENS' } = $fields[ BLOCK_LENS ];
99 $entry{ 'BLOCK_TYPE' } = $fields[ BLOCK_TYPE ];
101 return wantarray ? %entry : \%entry;
108 # Martin A. Hansen, November 2009.
110 # Outputs a KISS record to a given filehandle
111 # or STDOUT if no filehandle is given.
113 my ( $entry, # KISS entry to output
114 $fh, # file handle - OPTIONAL
121 if ( defined $entry->{ 'S_ID' } and
122 defined $entry->{ 'S_BEG' } and
123 defined $entry->{ 'S_END' }
126 Maasha::Common::error( qq(Bad S_BEG value: $entry->{ 'S_BEG' } < 0 ) ) if $entry->{ 'S_BEG' } < 0;
127 Maasha::Common::error( qq(Bad S_END value: $entry->{ 'S_END' } < $entry->{ 'S_BEG' }) ) if $entry->{ 'S_END' } < $entry->{ 'S_BEG' };
131 $fields[ S_ID ] = $entry->{ 'S_ID' };
132 $fields[ S_BEG ] = $entry->{ 'S_BEG' };
133 $fields[ S_END ] = $entry->{ 'S_END' };
134 $fields[ Q_ID ] = $entry->{ 'Q_ID' } || ".";
135 $fields[ SCORE ] = $entry->{ 'SCORE' } || ".";
136 $fields[ STRAND ] = $entry->{ 'STRAND' } || ".";
137 $fields[ HITS ] = $entry->{ 'HITS' } || ".";
138 $fields[ ALIGN ] = $entry->{ 'ALIGN' } || ".";
139 $fields[ BLOCK_COUNT ] = $entry->{ 'BLOCK_COUNT' } || ".";
140 $fields[ BLOCK_BEGS ] = $entry->{ 'BLOCK_BEGS' } || ".";
141 $fields[ BLOCK_LENS ] = $entry->{ 'BLOCK_LENS' } || ".";
142 $fields[ BLOCK_TYPE ] = $entry->{ 'BLOCK_TYPE' } || ".";
144 print $fh join( "\t", @fields ), "\n";
151 # Martin A. Hansen, November 2009.
153 # Sorts a KISS file on S_BEG and S_END
155 my ( $file, # KISS file
160 `sort -k 2,2n -k 3,3n $file > $file.sort`;
162 rename "$file.sort", $file;
168 # Martin A, Hansen, November 2009.
170 # Creates an index of a sorted KISS file.
172 my ( $file, # KISS file
177 my ( $tree, $offset, $fh, $line, $beg );
182 $fh = Maasha::Filesys::file_read_open( $file );
184 while ( $line = <$fh> )
186 ( undef, $beg ) = split "\t", $line, 3;
188 kiss_index_node_add( $tree, INDEX_LEVEL, INDEX_FACTOR, $beg, $offset );
190 $offset += length $line;
195 kiss_index_store( "$file.index", $tree );
199 sub kiss_index_node_add
201 # Martin A, Hansen, November 2009.
203 # Recursive routine to add nodes to a tree.
216 $bucket = int( $beg / $level );
218 if ( $level >= $factor )
220 $sum += $bucket * $level;
221 $beg -= $bucket * $level;
223 $node->{ 'CHILDREN' }->[ $bucket ]->{ 'COUNT' }++;
224 # $node->{ 'CHILDREN' }->[ $bucket ]->{ 'LEVEL' } = $level;
225 # $node->{ 'CHILDREN' }->[ $bucket ]->{ 'BUCKET' } = $bucket;
226 $node->{ 'CHILDREN' }->[ $bucket ]->{ 'BEG' } = $sum;
227 $node->{ 'CHILDREN' }->[ $bucket ]->{ 'END' } = $sum + $level - 1;
228 $node->{ 'CHILDREN' }->[ $bucket ]->{ 'OFFSET' } = $offset if not defined $node->{ 'CHILDREN' }->[ $bucket ]->{ 'OFFSET' };
230 kiss_index_node_add( $node->{ 'CHILDREN' }->[ $bucket ], $level / $factor, $factor, $beg, $offset, $sum );
235 sub kiss_index_offset
237 # Martin A. Hansen, November 2009.
239 # Given a KISS index and a begin position,
240 # locate the offset closest to the begin position,
243 my ( $index, # KISS index
244 $beg, # begin position
245 $level, # index level - OPTIONAL
246 $factor, # index factor - OPTIONAL
251 my ( $child, $offset );
253 $level ||= INDEX_LEVEL;
254 $factor ||= INDEX_FACTOR;
256 foreach $child ( @{ $index->{ 'CHILDREN' } } )
258 next if not defined $child;
260 if ( $child->{ 'BEG' } <= $beg and $beg <= $child->{ 'END' } )
262 if ( $level == $factor ) {
263 $offset = $child->{ 'OFFSET' };
265 $offset = kiss_index_offset( $child, $beg, $level / $factor, $factor );
276 # Martin A. Hansen, November 2009.
278 # Given a KISS index and a begin/end interval
279 # sum the number of counts in that interval,
282 my ( $index, # KISS index
283 $beg, # Begin position
285 $level, # index level - OPTIONAL
286 $factor, # index factor - OPTIONAL
291 my ( $count, $child );
293 $level ||= INDEX_LEVEL;
294 $factor ||= INDEX_FACTOR;
297 foreach $child ( @{ $index->{ 'CHILDREN' } } )
299 next if not defined $child;
301 if ( $level >= $factor )
303 if ( Maasha::Calc::overlap( $beg, $end, $child->{ 'BEG' }, $child->{ 'END' } ) )
305 $count += $child->{ 'COUNT' } if $level == $factor;
306 $count += kiss_index_count( $child, $beg, $end, $level / $factor, $factor );
317 # Martin A. Hansen, November 2009.
319 # Stores a KISS index to a file.
321 my ( $path, # path to KISS index
327 Maasha::Filesys::file_store( $path, $index );
331 sub kiss_index_retrieve
333 # Martin A. Hansen, November 2009.
335 # Retrieves a KISS index from a file.
337 my ( $path, # Path to KISS index
340 # Returns a data structure.
344 $index = Maasha::Filesys::file_retrieve( $path );
346 return wantarray ? @{ $index } : $index;
350 sub kiss_index_get_entries
352 # Martin A. Hansen, November 2009.
354 # Given a path to a KISS file and a KISS index
355 # along with a beg/end interval, locate all entries
356 # in that interval and return those.
358 my ( $file, # path to KISS file
360 $beg, # interval begin
366 my ( $offset, $fh, $entry, @entries );
368 $offset = kiss_index_offset( $index, $beg );
370 $fh = Maasha::Filesys::file_read_open( $file );
372 sysseek( $fh, $offset, 0 );
374 while ( $entry = kiss_entry_get( $fh ) )
376 push @entries, $entry if $entry->{ 'S_END' } > $beg;
378 last if $entry->{ 'S_BEG' } > $end;
383 return wantarray ? @entries : \@entries;
387 sub kiss_index_get_blocks
389 # Martin A. Hansen, November 2009.
391 # Given a KISS index recursively traverse
392 # this into the appropriate node size determined
393 # by the size of the given beg/end interval.
394 # Blocks consisting of hashes of BEG/END/COUNT
395 # are returned from the requested node size.
397 my ( $index, # KISS index node
398 $beg, # interval begin
400 $level, # index level - OPTIONAL
401 $factor, # index factor - OPTIONAL
402 $size, # requested node size
407 my ( $len, @blocks, $child );
409 $level ||= INDEX_LEVEL;
410 $factor ||= INDEX_FACTOR;
412 $size ||= 100; # TODO: lazy list loading?
414 # if ( not defined $size )
416 # $len = $end - $beg + 1;
418 # if ( $len > 100_000_000 ) {
420 # } elsif ( $len > 1_000_000 ) {
427 if ( $level >= $size )
429 foreach $child ( @{ $index->{ 'CHILDREN' } } )
431 next if not defined $child;
433 if ( Maasha::Calc::overlap( $beg, $end, $child->{ 'BEG' }, $child->{ 'END' } ) )
435 if ( $level == $size )
438 BEG => $child->{ 'BEG' },
439 END => $child->{ 'END' },
440 COUNT => $child->{ 'COUNT' },
444 push @blocks, kiss_index_get_blocks( $child, $beg, $end, $level / $factor, $factor, $size );
449 return wantarray ? @blocks : \@blocks;
455 # Martin A. Hansen, November 2009.
457 # Encodes alignment descriptors for two
460 my ( $s_seq, # Subject sequence reference
461 $q_seq, # Query sequence reference
462 $offset, # alternate offset - OPTIONAL
467 my ( $i, $s, $q, @align );
469 Maasha::Common::error( "Sequence lengths don't match" ) if length ${ $s_seq } != length ${ $q_seq };
473 for ( $i = 0; $i < length ${ $s_seq }; $i++ )
475 $s = uc substr ${ $s_seq }, $i, 1;
476 $q = uc substr ${ $q_seq }, $i, 1;
478 if ( $s eq '-' and $q eq '-' )
488 push @align, "$offset:$s>$q";
490 $offset++ if not $s eq '-';
494 return wantarray ? @align : \@align;
500 # Martin A. Hansen, November 2009.
502 # Test routine of correct resolve of ALIGN descriptors.
504 # S.aur_COL 41 75 5_vnlh2ywXsN1/1 1 - . 17:A>T,31:A>N . . . .
506 my ( $s_seqref, # subject sequence reference
512 my ( $align, $pos, $s_symbol, $q_symbol, $s_seq, $q_seq, $insertions );
514 $s_seq = substr ${ $s_seqref }, $entry->{ 'S_BEG' }, $entry->{ 'S_END' } - $entry->{ 'S_BEG' } + 1;
519 foreach $align ( split /,/, $entry->{ 'ALIGN' } )
521 if ( $align =~ /(\d+):(.)>(.)/ )
527 if ( $s_symbol eq '-' ) # insertion
529 substr $s_seq, $pos + $insertions, 0, $s_symbol;
530 substr $q_seq, $pos + $insertions, 0, $q_symbol;
534 elsif ( $q_symbol eq '-' ) # deletion
536 substr $q_seq, $pos + $insertions, 1, $q_symbol;
540 substr $q_seq, $pos + $insertions, 1, $q_symbol;
545 Maasha::Common::error( qq(Bad alignment descriptor: "$align") );
549 Maasha::Align::align_print_pairwise( [ $entry->{ 'S_ID' }, $s_seq ], [ $entry->{ 'Q_ID' }, $q_seq ] );
555 # Martin A. Hansen, November 2009.
557 # Converts a KISS entry to a Biopiece record.
558 # TODO: Consistency checking
560 my ( $entry, # KISS entry
565 return wantarray ? %{ $entry } : $entry;
571 # Martin A. Hansen, November 2009.
573 # Converts a Biopiece record to a KISS entry.
575 my ( $record, # Biopiece record
580 $record->{ 'SCORE' } ||= $record->{ 'E_VAL' } || ".";
581 $record->{ 'HITS' } ||= ".";
582 $record->{ 'BLOCK_COUNT' } ||= ".";
583 $record->{ 'BLOCK_BEGS' } ||= ".";
584 $record->{ 'BLOCK_LENS' } ||= ".";
585 $record->{ 'BLOCK_TYPE' } ||= ".";
586 $record->{ 'ALIGN' } ||= $record->{ 'DESCRIPTOR' } || ".";
588 return wantarray ? %{ $record } : $record;
592 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<