]> git.donarmstrong.com Git - biopieces.git/blobdiff - bp_bin/extract_seq
adding bzip2 support in ruby
[biopieces.git] / bp_bin / extract_seq
index bdd8104bf5855e6c0ff6f0bfd55af7b034cd146e..40260cc9e490b3629fb131f8435b8f6c3c81144c 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env perl -w
+#!/usr/bin/env perl
 
 # Copyright (C) 2007-2009 Martin A. Hansen.
 
@@ -26,6 +26,7 @@
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
+use warnings;
 use strict;
 use Maasha::Biopieces;
 
@@ -33,61 +34,93 @@ use Maasha::Biopieces;
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
-my ( $run_time_beg, $run_time_end, $options, $in, $out, $record, @vals, $i );
+my ( $options, $in, $out, $record, $beg, $end, $len );
 
 $options = Maasha::Biopieces::parse_options(
     [
-        { long => 'key',     short => 'k', type => 'string', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef },
-        { long => 'keys',    short => 'K', type => 'list',   mandatory => 'no',  default => undef, allowed => undef, disallowed => undef },
-        { long => 'delimit', short => 'd', type => 'string', mandatory => 'no',  default => '_',   allowed => undef, disallowed => undef },
+        { long => 'beg', short => 'b', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
+        { long => 'end', short => 'e', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
+        { long => 'len', short => 'l', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => 0 },
     ]   
 );
 
 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
 
-while ( $record = Maasha::Biopieces::get_record( $in ) )
+if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
+    $beg = 0;
+} else {
+    $beg = $options->{ "beg" } - 1;   # correcting for start offset
+}
+
+if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
+    $end = $beg - 1;
+} elsif ( defined $options->{ "end" } ) {
+    $end = $options->{ "end" } - 1;   # correcting for start offset
+}
+
+$len = $options->{ "len" };
+
+#    print "beg->$beg,  end->$end,  len->$len\n";
+
+while ( $record = Maasha::Biopieces::get_record( $in ) ) 
 {
-    if ( exists $options->{ 'key' } and exists $record->{ $options->{ 'key' } } )
+    if ( $record->{ "SEQ" } )
     {
-        @vals = split /$options->{ 'delimit' }/, $record->{ $options->{ 'key' } };
-
-        if ( scalar @vals > 1 )
+        if ( defined $beg and defined $end )
         {
-            for ( $i = 0; $i < @vals; $i++ )
+            if ( $end - $beg + 1 > length $record->{ "SEQ" } )
+            {
+                $record->{ "SEQ" }    = substr $record->{ "SEQ" }, $beg;
+                $record->{ "SCORES" } = substr $record->{ "SEQ" }, $beg if $record->{ "SCORES" };
+            }
+            else
             {
-                if ( defined $options->{ "keys" } and defined $options->{ "keys" }->[ $i ] ) {
-                    $record->{ $options->{ "keys" }->[ $i ] } = $vals[ $i ];
-                } else {
-                    $record->{ $options->{ 'key' } . "_$i" } = $vals[ $i ];
-                }
+                $record->{ "SEQ" }    = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
+                $record->{ "SCORES" } = substr $record->{ "SCORES" }, $beg, $end - $beg + 1 if $record->{ "SCORES" };
             }
         }
+        elsif ( defined $beg and defined $len )
+        {
+            if ( $len > length $record->{ "SEQ" } )
+            {
+                $record->{ "SEQ" }    = substr $record->{ "SEQ" }, $beg;
+                $record->{ "SCORES" } = substr $record->{ "SCORES" }, $beg if $record->{ "SCORES" };
+            }
+            else
+            {
+                $record->{ "SEQ" }    = substr $record->{ "SEQ" }, $beg, $len;
+                $record->{ "SCORES" } = substr $record->{ "SCORES" }, $beg, $len if $record->{ "SCORES" };
+            }
+        }
+        elsif ( defined $beg )
+        {
+            $record->{ "SEQ" }    = substr $record->{ "SEQ" }, $beg;
+            $record->{ "SCORES" } = substr $record->{ "SCORES" }, $beg if $record->{ "SCORES" };
+        }
+
+        $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
     }
 
     Maasha::Biopieces::put_record( $record, $out );
 }
 
+Maasha::Biopieces::close_stream( $in );
+Maasha::Biopieces::close_stream( $out );
+
 
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
 BEGIN
 {
-    $run_time_beg = Maasha::Biopieces::run_time();
-
-    Maasha::Biopieces::log_biopiece();
+    Maasha::Biopieces::status_set();
 }
 
 
 END
 {
-    Maasha::Biopieces::close_stream( $in );
-    Maasha::Biopieces::close_stream( $out );
-
-    $run_time_end = Maasha::Biopieces::run_time();
-
-    Maasha::Biopieces::run_time_print( $run_time_beg, $run_time_end, $options );
+    Maasha::Biopieces::status_log();
 }
 
 
@@ -95,4 +128,3 @@ END
 
 
 __END__
-