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, December 2009.
110 # Parses a line with a KISS entry.
112 my ( $line, # KISS line to parse
117 my ( @fields, %entry );
119 @fields = split /\t/, $line;
121 Maasha::Common::error( qq(BAD kiss entry: $line) ) if not @fields == 12;
123 $entry{ 'S_ID' } = $fields[ S_ID ];
124 $entry{ 'S_BEG' } = $fields[ S_BEG ];
125 $entry{ 'S_END' } = $fields[ S_END ];
126 $entry{ 'Q_ID' } = $fields[ Q_ID ];
127 $entry{ 'SCORE' } = $fields[ SCORE ];
128 $entry{ 'STRAND' } = $fields[ STRAND ];
129 $entry{ 'HITS' } = $fields[ HITS ];
130 $entry{ 'ALIGN' } = $fields[ ALIGN ];
131 $entry{ 'BLOCK_COUNT' } = $fields[ BLOCK_COUNT ];
132 $entry{ 'BLOCK_BEGS' } = $fields[ BLOCK_BEGS ];
133 $entry{ 'BLOCK_LENS' } = $fields[ BLOCK_LENS ];
134 $entry{ 'BLOCK_TYPE' } = $fields[ BLOCK_TYPE ];
136 return wantarray ? %entry : \%entry;
142 # Martin A. Hansen, November 2009.
144 # Outputs a KISS record to a given filehandle
145 # or STDOUT if no filehandle is given.
147 my ( $entry, # KISS entry to output
148 $fh, # file handle - OPTIONAL
155 if ( defined $entry->{ 'S_ID' } and
156 defined $entry->{ 'S_BEG' } and
157 defined $entry->{ 'S_END' }
160 Maasha::Common::error( qq(Bad S_BEG value: $entry->{ 'S_BEG' } < 0 ) ) if $entry->{ 'S_BEG' } < 0;
161 Maasha::Common::error( qq(Bad S_END value: $entry->{ 'S_END' } < $entry->{ 'S_BEG' }) ) if $entry->{ 'S_END' } < $entry->{ 'S_BEG' };
165 $fields[ S_ID ] = $entry->{ 'S_ID' };
166 $fields[ S_BEG ] = $entry->{ 'S_BEG' };
167 $fields[ S_END ] = $entry->{ 'S_END' };
168 $fields[ Q_ID ] = $entry->{ 'Q_ID' } || ".";
169 $fields[ SCORE ] = $entry->{ 'SCORE' } || ".";
170 $fields[ STRAND ] = $entry->{ 'STRAND' } || ".";
171 $fields[ HITS ] = $entry->{ 'HITS' } || ".";
172 $fields[ ALIGN ] = $entry->{ 'ALIGN' } || ".";
173 $fields[ BLOCK_COUNT ] = $entry->{ 'BLOCK_COUNT' } || ".";
174 $fields[ BLOCK_BEGS ] = $entry->{ 'BLOCK_BEGS' } || ".";
175 $fields[ BLOCK_LENS ] = $entry->{ 'BLOCK_LENS' } || ".";
176 $fields[ BLOCK_TYPE ] = $entry->{ 'BLOCK_TYPE' } || ".";
178 print $fh join( "\t", @fields ), "\n";
185 # Martin A. Hansen, November 2009.
187 # Sorts a KISS file on S_BEG and S_END
189 my ( $file, # KISS file
194 `sort -k 2,2n -k 3,3n $file > $file.sort`;
196 rename "$file.sort", $file;
202 # Martin A, Hansen, November 2009.
204 # Creates an index of a sorted KISS file.
206 my ( $file, # KISS file
211 my ( $tree, $offset, $fh, $line, $beg );
216 $fh = Maasha::Filesys::file_read_open( $file );
218 while ( $line = <$fh> )
220 ( undef, $beg ) = split "\t", $line, 3;
222 kiss_index_node_add( $tree, INDEX_LEVEL, INDEX_FACTOR, $beg, $offset );
224 $offset += length $line;
229 kiss_index_store( "$file.index", $tree );
233 sub kiss_index_node_add
235 # Martin A, Hansen, November 2009.
237 # Recursive routine to add nodes to a tree.
250 $bucket = int( $beg / $level );
252 if ( $level >= $factor )
254 $sum += $bucket * $level;
255 $beg -= $bucket * $level;
257 $node->{ 'CHILDREN' }->[ $bucket ]->{ 'COUNT' }++;
258 # $node->{ 'CHILDREN' }->[ $bucket ]->{ 'LEVEL' } = $level;
259 # $node->{ 'CHILDREN' }->[ $bucket ]->{ 'BUCKET' } = $bucket;
260 $node->{ 'CHILDREN' }->[ $bucket ]->{ 'BEG' } = $sum;
261 $node->{ 'CHILDREN' }->[ $bucket ]->{ 'END' } = $sum + $level - 1;
262 $node->{ 'CHILDREN' }->[ $bucket ]->{ 'OFFSET' } = $offset if not defined $node->{ 'CHILDREN' }->[ $bucket ]->{ 'OFFSET' };
264 kiss_index_node_add( $node->{ 'CHILDREN' }->[ $bucket ], $level / $factor, $factor, $beg, $offset, $sum );
269 sub kiss_index_offset
271 # Martin A. Hansen, November 2009.
273 # Given a KISS index and a begin position,
274 # locate the offset closest to the begin position,
277 my ( $index, # KISS index
278 $beg, # begin position
279 $level, # index level - OPTIONAL
280 $factor, # index factor - OPTIONAL
285 my ( $child, $offset );
287 $level ||= INDEX_LEVEL;
288 $factor ||= INDEX_FACTOR;
290 foreach $child ( @{ $index->{ 'CHILDREN' } } )
292 next if not defined $child;
294 if ( $child->{ 'BEG' } <= $beg and $beg <= $child->{ 'END' } )
296 if ( $level == $factor ) {
297 $offset = $child->{ 'OFFSET' };
299 $offset = kiss_index_offset( $child, $beg, $level / $factor, $factor );
304 # Maasha::Common::error( "No offset" ) if not defined $offset; # FIXME
312 # Martin A. Hansen, November 2009.
314 # Given a KISS index and a begin/end interval
315 # sum the number of counts in that interval,
318 my ( $index, # KISS index
319 $beg, # Begin position
321 $level, # index level - OPTIONAL
322 $factor, # index factor - OPTIONAL
327 my ( $count, $child );
329 $level ||= INDEX_LEVEL;
330 $factor ||= INDEX_FACTOR;
333 foreach $child ( @{ $index->{ 'CHILDREN' } } )
335 next if not defined $child;
337 if ( $level >= $factor )
339 if ( Maasha::Calc::overlap( $beg, $end, $child->{ 'BEG' }, $child->{ 'END' } ) )
341 $count += $child->{ 'COUNT' } if $level == $factor;
342 $count += kiss_index_count( $child, $beg, $end, $level / $factor, $factor );
353 # Martin A. Hansen, November 2009.
355 # Stores a KISS index to a file.
357 my ( $path, # path to KISS index
363 Maasha::Filesys::file_store( $path, $index );
367 sub kiss_index_retrieve
369 # Martin A. Hansen, November 2009.
371 # Retrieves a KISS index from a file.
373 my ( $path, # Path to KISS index
376 # Returns a data structure.
380 $index = Maasha::Filesys::file_retrieve( $path );
382 return wantarray ? @{ $index } : $index;
386 sub kiss_index_get_entries
388 # Martin A. Hansen, November 2009.
390 # Given a path to a KISS file and a KISS index
391 # along with a beg/end interval, locate all entries
392 # in that interval and return those.
394 my ( $file, # path to KISS file
396 $beg, # interval begin
402 my ( $offset, $fh, $entry, @entries );
404 $offset = kiss_index_offset( $index, $beg );
406 $fh = Maasha::Filesys::file_read_open( $file );
408 sysseek( $fh, $offset, 0 );
410 while ( $entry = kiss_entry_get( $fh ) )
412 push @entries, $entry if $entry->{ 'S_END' } > $beg;
414 last if $entry->{ 'S_BEG' } > $end;
419 return wantarray ? @entries : \@entries;
423 sub kiss_index_get_blocks
425 # Martin A. Hansen, November 2009.
427 # Given a KISS index recursively traverse
428 # this into the appropriate node size determined
429 # by the size of the given beg/end interval.
430 # Blocks consisting of hashes of BEG/END/COUNT
431 # are returned from the requested node size.
433 my ( $index, # KISS index node
434 $beg, # interval begin
436 $level, # index level - OPTIONAL
437 $factor, # index factor - OPTIONAL
438 $size, # requested node size
443 my ( $len, @blocks, $child );
445 $level ||= INDEX_LEVEL;
446 $factor ||= INDEX_FACTOR;
448 $size ||= 100; # TODO: lazy list loading?
450 # if ( not defined $size )
452 # $len = $end - $beg + 1;
454 # if ( $len > 100_000_000 ) {
456 # } elsif ( $len > 1_000_000 ) {
463 if ( $level >= $size )
465 foreach $child ( @{ $index->{ 'CHILDREN' } } )
467 next if not defined $child;
469 if ( Maasha::Calc::overlap( $beg, $end, $child->{ 'BEG' }, $child->{ 'END' } ) )
471 if ( $level == $size )
474 BEG => $child->{ 'BEG' },
475 END => $child->{ 'END' },
476 COUNT => $child->{ 'COUNT' },
480 push @blocks, kiss_index_get_blocks( $child, $beg, $end, $level / $factor, $factor, $size );
485 return wantarray ? @blocks : \@blocks;
491 # Martin A. Hansen, November 2009.
493 # Encodes alignment descriptors for two
496 my ( $s_seq, # Subject sequence reference
497 $q_seq, # Query sequence reference
498 $offset, # alternate offset - OPTIONAL
503 my ( $i, $s, $q, @align );
505 Maasha::Common::error( "Sequence lengths don't match" ) if length ${ $s_seq } != length ${ $q_seq };
509 for ( $i = 0; $i < length ${ $s_seq }; $i++ )
511 $s = uc substr ${ $s_seq }, $i, 1;
512 $q = uc substr ${ $q_seq }, $i, 1;
514 if ( $s eq '-' and $q eq '-' )
524 push @align, "$offset:$s>$q";
526 $offset++ if not $s eq '-';
530 return wantarray ? @align : \@align;
536 # Martin A. Hansen, November 2009.
538 # Test routine of correct resolve of ALIGN descriptors.
540 # S.aur_COL 41 75 5_vnlh2ywXsN1/1 1 - . 17:A>T,31:A>N . . . .
542 my ( $s_seqref, # subject sequence reference
548 my ( $align, $pos, $s_symbol, $q_symbol, $s_seq, $q_seq, $insertions );
550 $s_seq = substr ${ $s_seqref }, $entry->{ 'S_BEG' }, $entry->{ 'S_END' } - $entry->{ 'S_BEG' } + 1;
555 foreach $align ( split /,/, $entry->{ 'ALIGN' } )
557 if ( $align =~ /(\d+):(.)>(.)/ )
563 if ( $s_symbol eq '-' ) # insertion
565 substr $s_seq, $pos + $insertions, 0, $s_symbol;
566 substr $q_seq, $pos + $insertions, 0, $q_symbol;
570 elsif ( $q_symbol eq '-' ) # deletion
572 substr $q_seq, $pos + $insertions, 1, $q_symbol;
576 substr $q_seq, $pos + $insertions, 1, $q_symbol;
581 Maasha::Common::error( qq(Bad alignment descriptor: "$align") );
585 Maasha::Align::align_print_pairwise( [ $entry->{ 'S_ID' }, $s_seq ], [ $entry->{ 'Q_ID' }, $q_seq ] );
591 # Martin A. Hansen, November 2009.
593 # Converts a KISS entry to a Biopiece record.
594 # TODO: Consistency checking
596 my ( $entry, # KISS entry
601 return wantarray ? %{ $entry } : $entry;
607 # Martin A. Hansen, November 2009.
609 # Converts a Biopiece record to a KISS entry.
611 my ( $record, # Biopiece record
616 $record->{ 'SCORE' } ||= $record->{ 'E_VAL' } || ".";
617 $record->{ 'HITS' } ||= ".";
618 $record->{ 'BLOCK_COUNT' } ||= ".";
619 $record->{ 'BLOCK_BEGS' } ||= ".";
620 $record->{ 'BLOCK_LENS' } ||= ".";
621 $record->{ 'BLOCK_TYPE' } ||= ".";
622 $record->{ 'ALIGN' } ||= $record->{ 'DESCRIPTOR' } || ".";
624 return wantarray ? %{ $record } : $record;
628 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<