]> git.donarmstrong.com Git - biopieces.git/blobdiff - bp_bin/calc_N50
adding bzip2 support in ruby
[biopieces.git] / bp_bin / calc_N50
index ed4fd0e6dbf081876f7583132acff70162e9c53c..13c8240e29274b9adef5e6860187154646c3914f 100755 (executable)
@@ -1,7 +1,5 @@
 #!/usr/bin/env ruby
 
-#arg!
-
 # Copyright (C) 2007-2011 Martin A. Hansen.
 
 # This program is free software; you can redistribute it and/or
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
-require 'biopieces'
-require 'pp'
+require 'maasha/biopieces'
 
 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}
 
-bp = Biopieces.new
-
-options = bp.parse(ARGV, casts)
+options = Biopieces.options_parse(ARGV, casts)
 
 total   = 0
 lengths = []
 
-bp.each_record do |record|
-  bp.puts record unless options[:no_stream]
+Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
+  input.each_record do |record|
+    output.puts record unless options[:no_stream]
 
-  if record.has_key? :SEQ
-    total   += record[:SEQ].length
-    lengths << record[:SEQ].length
+    if record[:SEQ]
+      total   += record[:SEQ].length
+      lengths << record[:SEQ].length
+    end
   end
 end
 
-bp.out = Stream.write(options[:data_out])
-
-count = 0
+new_record = {}
+count      = 0
 
 lengths.sort.reverse.each do |length|
   count += length
 
   if count >= total * 0.50
-    bp.puts "N50" => length
+    new_record["N50"] = length
     break
   end
 end
 
+Biopieces.open(nil, options[:data_out]) do |input, output|
+  output.puts new_record
+end
+
+
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<