]> git.donarmstrong.com Git - biopieces.git/blobdiff - bp_bin/write_fastq
adding bzip2 support in ruby
[biopieces.git] / bp_bin / write_fastq
index d242f374e16dbb727727b4e6d26c861975d92f3c..d15c96f9dd31eb0ac6792b6b70d092ddcdd29bb6 100755 (executable)
@@ -1,6 +1,6 @@
-#!/usr/bin/env perl
+#!/usr/bin/env ruby
 
-# Copyright (C) 2007-2009 Martin A. Hansen.
+# Copyright (C) 2007-2012 Martin A. Hansen.
 
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
 
 # http://www.gnu.org/copyleft/gpl.html
 
-
-# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
-
-# Write sequences from stream in FASTQ format.
-
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
+# This program is part of the Biopieces framework (www.biopieces.org).
 
-use warnings;
-use strict;
-use Maasha::Fastq;
-use Maasha::Biopieces;
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
+# Write sequences from stream in FASTQ format.
 
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
+require 'maasha/biopieces'
+require 'maasha/fastq'
 
-my ( $options, $in, $out, $record, $data_out, $entry );
-
-$options = Maasha::Biopieces::parse_options(
-    [
-        { long => 'no_stream', short => 'x', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
-        { long => 'data_out',  short => 'o', type => 'file', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
-        { long => 'compress',  short => 'Z', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
-    ]   
-);
-
-$in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
-$out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
+allowed_enc = 'base_33,base_64'
 
-$data_out = Maasha::Biopieces::write_stream( $options->{ "data_out" }, $options->{ "compress" } );
+casts = []
+casts << {long: 'no_stream', short: 'x', type: 'flag',   mandatory: false, default: nil,       allowed: nil,               disallowed: nil}
+casts << {long: 'data_out',  short: 'o', type: 'file',   mandatory: false, default: nil,       allowed: nil,               disallowed: nil}
+casts << {long: 'encoding',  short: 'e', type: 'string', mandatory: false, default: 'base_33', allowed: allowed_enc,       disallowed: nil}
+casts << {long: 'compress',  short: 'Z', type: 'string', mandatory: false, default: nil,       allowed: "gzip,bzip,bzip2", disallowed: nil}
 
-while ( $record = Maasha::Biopieces::get_record( $in ) ) 
-{
-    if ( $entry = Maasha::Fastq::biopiece2fastq( $record ) ) {
-        Maasha::Fastq::put_entry( $entry, $data_out, $options->{ "wrap" } );
-    }
+options = Biopieces.options_parse(ARGV, casts)
 
-    Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
-}
+encoding = options[:encoding].to_sym
+compress = options[:compress] ? options[:compress].to_sym : nil
 
-close $data_out;
+raise "--data_out is mandatory for compressed output" if compress and not options[:data_out]
 
-Maasha::Biopieces::close_stream( $in );
-Maasha::Biopieces::close_stream( $out );
-
-
-# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
+  fastq_out = options[:data_out] ? Fastq.open(options[:data_out], 'w', compress: compress) : STDOUT
 
+  input.each do |record|
+    if record[:SEQ_NAME] and record[:SEQ] and record[:SCORES]
+      entry = Seq.new_bp(record)
+      entry.qual_convert!(:base_33, encoding)
 
-BEGIN
-{
-    Maasha::Biopieces::status_set();
-}
+      fastq_out.puts entry.to_fastq
+    end
 
+    output.puts record unless options[:no_stream]
+  end
 
-END
-{
-    Maasha::Biopieces::status_log();
-}
+  fastq_out.close
+end
 
 
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<