]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_perl/Maasha/KISS.pm
updateing to idba hybrid
[biopieces.git] / code_perl / Maasha / KISS.pm
index 7cfdbcd86ba62a8cc5648f90b53e387595d8943c..35d21108d9b853f59dd74738278a29a9054d4fd5 100644 (file)
@@ -33,9 +33,9 @@ package Maasha::KISS;
 use warnings;
 use strict;
 use Data::Dumper;
+use JSON::XS;
 use Maasha::Common;
 use Maasha::Filesys;
-use Maasha::NClist;
 use Maasha::Align;
 
 use vars qw( @ISA @EXPORT );
@@ -100,12 +100,43 @@ sub kiss_entry_get
 }
 
 
+sub kiss_retrieve
+{
+    # Martin A. Hansen, February 2010.
+
+    # Retrieves KISS entries from a given sorted KISS file
+    # within an optional interval.
+
+    my ( $file,   # path to KISS file
+         $beg,    # interval begin  -  OPTIONAL
+         $end,    # interval end    -  OPTIONAL
+       ) = @_;
+
+    # Returns a list.
+
+    my ( $fh, $entry, @entries );
+
+    $beg ||= 0;
+    $end ||= 999999999;
+
+    $fh = Maasha::Filesys::file_read_open( $file );
+
+    while ( $entry = kiss_entry_get( $fh ) )
+    {
+        last if $entry->[ S_BEG ] > $end;
+        push @entries, $entry if $entry->[ S_END ] > $beg;
+    }
+
+    close $fh;
+
+    return wantarray ? @entries : \@entries;
+}
+
+
 sub kiss_entry_parse
 {
     # Martin A. Hansen, December 2009.
 
-    # TODO find out what uses this and kill it!
-
     # Parses a line with a KISS entry.
 
     my ( $line,   #  KISS line to parse
@@ -119,20 +150,7 @@ sub kiss_entry_parse
 
     Maasha::Common::error( qq(BAD kiss entry: $line) ) if not @fields == 12;
     
-    $entry{ 'S_ID' }        = $fields[ S_ID ];
-    $entry{ 'S_BEG' }       = $fields[ S_BEG ];
-    $entry{ 'S_END' }       = $fields[ S_END ];
-    $entry{ 'Q_ID' }        = $fields[ Q_ID ];
-    $entry{ 'SCORE' }       = $fields[ SCORE ];
-    $entry{ 'STRAND' }      = $fields[ STRAND ];
-    $entry{ 'HITS' }        = $fields[ HITS ];
-    $entry{ 'ALIGN' }       = $fields[ ALIGN ];
-    $entry{ 'BLOCK_COUNT' } = $fields[ BLOCK_COUNT ];
-    $entry{ 'BLOCK_BEGS' }  = $fields[ BLOCK_BEGS ];
-    $entry{ 'BLOCK_LENS' }  = $fields[ BLOCK_LENS ];
-    $entry{ 'BLOCK_TYPE' }  = $fields[ BLOCK_TYPE ];
-
-    return wantarray ? %entry : \%entry;
+    return wantarray ? @fields : \@fields;
 }
 
 
@@ -193,13 +211,13 @@ sub kiss_sort
 }
 
 
-sub kiss_index_old
+sub kiss_index
 {
     # Martin A. Hansen, December 2009.
 
     # Creates a lookup index of a sorted KISS file.
 
-    my ( $file,         # path to KISS file
+    my ( $file,   # path to KISS file
        ) = @_;
 
     # Returns nothing.
@@ -228,72 +246,72 @@ sub kiss_index_old
 }
 
 
-sub kiss_index
+sub kiss_index_offset
 {
-    # Martin A. Hansen, February 2010.
+    # Martin A. Hansen, December 2009.
 
-    # Creates a NC list index of a sorted KISS file.
+    # Given a KISS index and a begin position,
+    # locate the offset closest to the begin position,
+    # and return this.
 
-    my ( $file,         # path to KISS file
+    my ( $index,    # KISS index
+         $beg,      # begin position
        ) = @_;
 
-    # Returns nothing.
-
-    my ( $fh, $line, @fields, $nc_list );
+    # Returns a number.
 
-    $fh = Maasha::Filesys::file_read_open( $file );
+    my ( $bucket );
 
-    while ( $line = <$fh> )
-    {
-        chomp $line;
+    Maasha::Common::error( qq(Negative begin position: "$beg") ) if $beg < 0;
 
-        @fields = split "\t", $line;
+    $bucket = int( $beg / BUCKET_SIZE ); 
 
-        if ( not defined $nc_list ) {
-            $nc_list = [ [ @fields ] ];
-        } else {
-            Maasha::NClist::nc_list_add( $nc_list, [ @fields ], INDEX_END, INDEX );
-        }
-    }
+    $bucket = scalar @{ $index } if $bucket > scalar @{ $index };
 
-    close $fh;
+    while ( $bucket >= 0 )
+    {
+        return $index->[ $bucket ]->[ OFFSET ] if defined $index->[ $bucket ];
 
-    Maasha::NClist::nc_list_store( $nc_list, "$file.json" );
+        $bucket--;
+    }
 }
 
 
-sub kiss_index_offset
+sub kiss_index_count
 {
     # Martin A. Hansen, December 2009.
 
-    # Given a KISS index and a begin position,
-    # locate the offset closest to the begin position,
+    # Given a KISS index and a begin/end interval
+    # sum the number of counts in that interval,
     # and return this.
 
-    my ( $index,    # KISS index
-         $beg,      # begin position
+    my ( $index,   # KISS index
+         $beg,     # Begin position
+         $end,     # End position
        ) = @_;
 
     # Returns a number.
 
-    my ( $bucket );
+    my ( $bucket_beg, $bucket_end, $count, $i );
 
     Maasha::Common::error( qq(Negative begin position: "$beg") ) if $beg < 0;
 
-    $bucket = int( $beg / BUCKET_SIZE ); 
+    $bucket_beg = int( $beg / BUCKET_SIZE );
+    $bucket_end = int( $end / BUCKET_SIZE );
 
-    $bucket = scalar @{ $index } if $bucket > scalar @{ $index };
+    $bucket_end = scalar @{ $index } if $bucket_end > scalar @{ $index };
 
-    while ( $bucket >= 0 )
-    {
-        return $index->[ $bucket ]->[ OFFSET ] if defined $index->[ $bucket ];
+    $count = 0;
 
-        $bucket--;
+    for ( $i = $bucket_beg; $i <= $bucket_end; $i++ ) {
+        $count += $index->[ $i ]->[ COUNT ] if defined $index->[ $i ];
     }
+
+    return $count;
 }
 
 
-sub kiss_index_count
+sub kiss_index_count_nc
 {
     # Martin A. Hansen, December 2009.
 
@@ -326,18 +344,69 @@ sub kiss_index_get_entries
     # along with a beg/end interval, locate all entries
     # in that interval and return those.
 
-    my ( $index,   # KISS index
+    my ( $file,    # path to KISS file
+         $index,   # KISS index
          $beg,     # interval begin
          $end,     # interval end
        ) = @_;
 
     # Returns a list.
 
-    my ( $features );
+    my ( $offset, $fh, $entry, @entries );
 
-    $features = Maasha::NClist::nc_list_get_interval( $index, $beg, $end, INDEX_BEG, INDEX_END, INDEX );
+    # $offset = kiss_index_offset( $index, $beg );
 
-    return wantarray ? @{ $features } : $features;
+    $fh = Maasha::Filesys::file_read_open( $file );
+
+    # sysseek( $fh, $offset, 0 );
+
+    while ( $entry = Maasha::KISS::kiss_entry_get( $fh ) )
+    {
+        push @entries, $entry if $entry->[ S_END ] > $beg;
+        
+        last if $entry->[ S_BEG ] > $end;
+    }
+
+    close $fh;
+
+    return wantarray ? @entries : \@entries;
+}
+
+
+sub kiss_index_get_entries_OLD
+{
+    # Martin A. Hansen, November 2009.
+
+    # Given a path to a KISS file and a KISS index
+    # along with a beg/end interval, locate all entries
+    # in that interval and return those.
+
+    my ( $file,    # path to KISS file
+         $index,   # KISS index
+         $beg,     # interval begin
+         $end,     # interval end
+       ) = @_;
+
+    # Returns a list.
+
+    my ( $offset, $fh, $entry, @entries );
+
+    $offset = kiss_index_offset( $index, $beg );
+
+    $fh = Maasha::Filesys::file_read_open( $file );
+
+    sysseek( $fh, $offset, 0 );
+
+    while ( $entry = Maasha::KISS::kiss_entry_get( $fh ) )
+    {
+        push @entries, $entry if $entry->[ S_END ] > $beg;
+        
+        last if $entry->[ S_BEG ] > $end;
+    }
+
+    close $fh;
+
+    return wantarray ? @entries : \@entries;
 }
 
 
@@ -441,7 +510,15 @@ sub kiss_index_store
 
     # Returns nothing.
 
-    Maasha::Filesys::file_store( $path, $index );
+    my ( $fh, $json );
+
+    $json = JSON::XS::encode_json( $index );
+
+    $fh = Maasha::Filesys::file_write_open( $path );
+
+    print $fh $json;
+
+    close $fh;
 }
 
 
@@ -456,9 +533,17 @@ sub kiss_index_retrieve
 
     # Returns a data structure.
 
-    my ( $index );
+    my ( $fh, $json, $index );
 
-    $index = Maasha::NClist::nc_list_retrieve( $path );
+    local $/ = undef;
+
+    $fh = Maasha::Filesys::file_read_open( $path );
+
+    $json = <$fh>;
+
+    close $fh;
+
+    $index = JSON::XS::decode_json( $json );
 
     return wantarray ? @{ $index } : $index;
 }
@@ -624,12 +709,12 @@ sub biopiece2kiss
     $entry->[ S_BEG ]       = $record->{ 'S_BEG' };
     $entry->[ S_END ]       = $record->{ 'S_END' };
     $entry->[ Q_ID ]        = $record->{ 'Q_ID' }        || ".";
-    $entry->[ SCORE ]       = $record->{ 'SCORE' }       || $record->{ 'E_VAL' } || ".";
+    $entry->[ SCORE ]       = $record->{ 'SCORE' }       || $record->{ 'BIT_SCORE' } || $record->{ 'ID' } || ".";
     $entry->[ STRAND ]      = $record->{ 'STRAND' }      || ".";
     $entry->[ HITS ]        = $record->{ 'HITS' }        || ".";
     $entry->[ ALIGN ]       = $record->{ 'ALIGN' }       || $record->{ 'DESCRIPTOR' } || ".";
     $entry->[ BLOCK_COUNT ] = $record->{ 'BLOCK_COUNT' } || ".";
-    $entry->[ BLOCK_BEGS ]  = $record->{ 'BLOCK_BEGS' }  || ".";
+    $entry->[ BLOCK_BEGS ]  = $record->{ 'BLOCK_BEGS' }  || $record->{ 'Q_BEGS' } || ".";
     $entry->[ BLOCK_LENS ]  = $record->{ 'BLOCK_LENS' }  || ".";
     $entry->[ BLOCK_TYPE ]  = $record->{ 'BLOCK_TYPE' }  || ".";
 
@@ -641,3 +726,79 @@ sub biopiece2kiss
 
 1;
 
+__END__
+
+sub kiss_index_nc
+{
+    # Martin A. Hansen, February 2010.
+
+    # Creates a NC list index of a sorted KISS file.
+
+    my ( $file,         # path to KISS file
+       ) = @_;
+
+    # Returns nothing.
+
+    my ( $fh, $line, @fields, $nc_list );
+
+    $fh = Maasha::Filesys::file_read_open( $file );
+
+    while ( $line = <$fh> )
+    {
+        chomp $line;
+
+        @fields = split "\t", $line;
+
+        if ( not defined $nc_list ) {
+            $nc_list = [ [ @fields ] ];
+        } else {
+            Maasha::NClist::nc_list_add( $nc_list, [ @fields ], INDEX_END, INDEX );
+        }
+    }
+
+    close $fh;
+
+    Maasha::NClist::nc_list_store( $nc_list, "$file.json" );
+}
+
+
+sub kiss_index_get_entries_nc
+{
+    # Martin A. Hansen, November 2009.
+
+    # Given a path to a KISS file and a KISS index
+    # along with a beg/end interval, locate all entries
+    # in that interval and return those.
+
+    my ( $index,   # KISS index
+         $beg,     # interval begin
+         $end,     # interval end
+       ) = @_;
+
+    # Returns a list.
+
+    my ( $features );
+
+    $features = Maasha::NClist::nc_list_get_interval( $index, $beg, $end, INDEX_BEG, INDEX_END, INDEX );
+
+    return wantarray ? @{ $features } : $features;
+}
+
+
+sub kiss_index_retrieve_nc
+{
+    # Martin A. Hansen, November 2009.
+
+    # Retrieves a KISS index from a file.
+
+    my ( $path,   # Path to KISS index
+       ) = @_;
+
+    # Returns a data structure.
+
+    my ( $index );
+
+    $index = Maasha::NClist::nc_list_retrieve( $path );
+
+    return wantarray ? @{ $index } : $index;
+}