-#!/usr/bin/env perl -w
+#!/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 FASTA format.
-
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+# This program is part of the Biopieces framework (www.biopieces.org).
-use strict;
-use Maasha::Fasta;
-use Maasha::Biopieces;
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+# Write sequences from stream in FASTA format.
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+require 'maasha/biopieces'
+require 'maasha/fasta'
-my ( $run_time_beg, $run_time_end, $options, $in, $out, $record, $data_out, $entry );
+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: 'wrap', short: 'w', type: 'uint', mandatory: false, default: nil, allowed: nil, disallowed: "0"}
+casts << {long: 'compress', short: 'Z', type: 'string', mandatory: false, default: nil, allowed: "gzip,bzip,bzip2", disallowed: nil}
-$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 => 'wrap', short => 'w', type => 'uint', mandatory => 'no', default => undef, allowed => undef, disallowed => '0' },
- { long => 'compress', short => 'Z', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
- ]
-);
+options = Biopieces.options_parse(ARGV, casts)
-$in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
-$out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
-
-$data_out = Maasha::Biopieces::write_stream( $options->{ "data_out" }, $options->{ "compress" } );
-
-while ( $record = Maasha::Biopieces::get_record( $in ) )
-{
- if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) ) {
- Maasha::Fasta::put_entry( $entry, $data_out, $options->{ "wrap" } );
- }
-
- Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
-}
-
-close $data_out;
-
-
-# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+compress = options[:compress] ? options[:compress].to_sym : nil
+raise "--data_out is mandatory for compressed output" if compress and not options[:data_out]
-BEGIN
-{
- $run_time_beg = Maasha::Biopieces::run_time();
+Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
+ fasta_out = options[:data_out] ? Fasta.open(options[:data_out], 'w', compress: compress) : STDOUT
- Maasha::Biopieces::log_biopiece();
-}
+ input.each do |record|
+ if record[:SEQ_NAME] and record[:SEQ]
+ entry = Seq.new_bp(record)
-END
-{
- Maasha::Biopieces::close_stream( $in );
- Maasha::Biopieces::close_stream( $out );
+ fasta_out.puts entry.to_fasta(options[:wrap])
+ end
- $run_time_end = Maasha::Biopieces::run_time();
+ output.puts record unless options[:no_stream]
+ end
- Maasha::Biopieces::run_time_print( $run_time_beg, $run_time_end, $options );
-}
+ fasta_out.close
+end
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<