]> git.donarmstrong.com Git - biopieces.git/blobdiff - bp_bin/grab
adding bzip2 support in ruby
[biopieces.git] / bp_bin / grab
index 806ae1c10d2ad70c5a2708efce01f783e5dc14eb..b034f2de42ca047a93550cecf9bcaab9f2d457c2 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env perl -w
+#!/usr/bin/env perl
 
 # Copyright (C) 2007-2009 Martin A. Hansen.
 
@@ -26,7 +26,9 @@
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
+use warnings;
 use strict;
+use Data::Dumper;
 use Maasha::Biopieces;
 use Maasha::Common;
 use Maasha::Patscan;
@@ -36,7 +38,7 @@ use Maasha::Patscan;
 
 
 my ( $options, $in, $out, $record, $keys, $vals_only, $keys_only, $invert,
-     $patterns, $regex, %lookup_hash, $key, $op, $val, $found );
+     $patterns, $regex, %lookup_hash, $key, $op, $val, $found, $total, $grabbed );
 
 $options = Maasha::Biopieces::parse_options(
     [
@@ -99,6 +101,9 @@ elsif ( $options->{ 'eval' } )
     }
 } 
 
+$total   = 0;
+$grabbed = 0;
+
 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
 {
     $found = 0;
@@ -113,16 +118,31 @@ while ( $record = Maasha::Biopieces::get_record( $in ) )
         $found = grab_eval( $key, $op, $val, $record );
     }
 
-    if ( $found and not $invert ) {
+    if ( $found and not $invert )
+    {
         Maasha::Biopieces::put_record( $record, $out );
-    } elsif ( not $found and $invert ) {
+        $grabbed += 1;
+    }
+    elsif ( not $found and $invert )
+    {
         Maasha::Biopieces::put_record( $record, $out );
+        $grabbed += 1;
     }
+
+    $total += 1;
 }
 
 Maasha::Biopieces::close_stream( $in );
 Maasha::Biopieces::close_stream( $out );
 
+if ( $options->{ 'verbose' } )
+{
+    print STDERR "Records grabbed: $grabbed\n";
+    print STDERR "Records missed: " . ( $total - $grabbed ) . "\n";
+    print STDERR "Patterns used: " . ( scalar @{$patterns} ) . "\n" if defined $patterns;
+    print STDERR "Patterns used: " . ( scalar keys %lookup_hash ) . "\n" if %lookup_hash;
+}
+
 
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
@@ -147,7 +167,7 @@ sub grab_lookup
 
     if ( $keys )
     {
-        map { return 1 if exists $lookup_hash->{ $record->{ $_ } } } @{ $keys };
+        map { return 1 if defined $record->{ $_ } and exists $lookup_hash->{ $record->{ $_ } } } @{ $keys };
     }
     else
     {
@@ -156,7 +176,7 @@ sub grab_lookup
         }
 
         if ( not $keys_only ) {
-            map { return 1 if exists $lookup_hash->{ $record->{ $_ } } } keys %{ $record };
+            map { return 1 if defined $record->{ $_ } and exists $lookup_hash->{ $record->{ $_ } } } keys %{ $record };
         }
     }
 
@@ -180,13 +200,17 @@ sub grab_patterns
 
     # Returns boolean.
 
-    my ( $pattern );
+    my ( $pattern, $key );
 
     foreach $pattern ( @{ $patterns } )
     {
         if ( $keys )
         {
-            map { return 1 if index( $record->{ $_ }, $pattern ) >= 0 } @{ $keys };
+            foreach $key ( @{ $keys } )
+            {
+                return 0 if not exists $record->{ $key };
+                return 1 if index( $record->{ $key }, $pattern ) >= 0;
+            }
         }
         else
         {
@@ -222,7 +246,7 @@ sub grab_regex
 
     if ( $keys )
     {
-        map { return 1 if $record->{ $_ } =~ /$regex/ } @{ $keys };
+        map { return 1 if exists $record->{ $_ } and $record->{ $_ } =~ /$regex/ } @{ $keys };
     }
     else
     {