]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_ruby/lib/maasha/biopieces.rb
added orf finder to seq.rb
[biopieces.git] / code_ruby / lib / maasha / biopieces.rb
index 5fd6edf4a505154bb929976a86c58246ac5d3f85..6c54658ff6de280f0e050ce7ba7602e56bb5f3f7 100644 (file)
@@ -79,18 +79,18 @@ class Biopieces
   # records. Records are read from STDIN (default) or file (possibly gzipped)
   # and written to STDOUT (default) or file.
   def self.open(input = STDIN, output = STDOUT)
-    input  = self.open_input(input)
-    output = self.open_output(output)
+    io_in  = self.open_input(input)
+    io_out = self.open_output(output)
 
     if block_given?
       begin
-        yield input, output
+        yield io_in, io_out
       ensure
-        input.close
-        output.close
+        io_in.close
+        io_out.close
       end
     else
-      return input, output
+      return io_in, io_out
     end
   end
 
@@ -153,15 +153,15 @@ class Biopieces
       if STDIN.tty?
         input = self.new(StringIO.new)
       else
-        input = self.new(STDIN, stdio = true)
+        input = self.new(STDIN, true)
       end
     elsif File.exists? input
-      ios = File.open(input, mode='r')
+      ios = File.open(input, 'r')
 
       begin
-          ios = Zlib::GzipReader.new(ios)
+        ios = Zlib::GzipReader.new(ios)
       rescue
-          ios.rewind
+        ios.rewind
       end
 
       input = self.new(ios)
@@ -171,12 +171,12 @@ class Biopieces
   end
 
   # Class method for opening data stream for writing Biopiece records.
-  # Records are written to STDOU (default) or file.
+  # Records are written to STDOUT (default) or file.
   def self.open_output(output)
     if output.nil?
-      output = self.new(STDOUT, stdio = true)
+      output = self.new(STDOUT, true)
     elsif not output.is_a? IO
-      output = self.new(File.open(output, mode='w'))
+      output = self.new(File.open(output, 'w'))
     end
 
     output
@@ -209,10 +209,10 @@ class Casts < Array
 
   # Add ubiquitous options casts.
   def ubiquitous
-    @cast_list << {:long=>'help',       :short=>'?', :type=>'flag',   :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
-    @cast_list << {:long=>'stream_in',  :short=>'I', :type=>'file!',  :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
-    @cast_list << {:long=>'stream_out', :short=>'O', :type=>'file',   :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
-    @cast_list << {:long=>'verbose',    :short=>'v', :type=>'flag',   :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
+    @cast_list << {:long=>'help',       :short=>'?', :type=>'flag',  :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
+    @cast_list << {:long=>'stream_in',  :short=>'I', :type=>'file!', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
+    @cast_list << {:long=>'stream_out', :short=>'O', :type=>'file',  :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
+    @cast_list << {:long=>'verbose',    :short=>'v', :type=>'flag',  :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
   end
 
   # Check integrity of the casts.
@@ -334,14 +334,13 @@ class OptionHandler
     @argv        = argv
     @casts       = casts
     @script_path = script_path
+    @options     = {}
   end
 
   # Parse options from argv using OptionParser and casts denoting long and
   # short option names. Usage information is printed and exit called.
   # A hash with options is returned.
   def options_parse
-    @options = {}
-
     option_parser = OptionParser.new do |option|
       @casts.each do |cast|
         case cast[:type]
@@ -355,7 +354,7 @@ class OptionHandler
           end
         when 'string'
           option.on("-#{cast[:short]}", "--#{cast[:long]} S", String) do |s|
-            @options[cast[:long]] = s.to_sym  # TODO: this to_sym - is that needed?
+            @options[cast[:long]] = s
           end
         when REGEX_LIST
           option.on( "-#{cast[:short]}", "--#{cast[:long]} A", Array) do |a|
@@ -378,7 +377,7 @@ class OptionHandler
     option_parser.parse!(@argv)
 
     if print_usage_full?
-      print_usage_and_exit(full=true)
+      print_usage_and_exit(true)
     elsif print_usage_short?
       print_usage_and_exit
     end
@@ -567,7 +566,7 @@ class Status
   def set
     time0  = Time.new.strftime("%Y-%m-%d %X")
 
-    File.open(path, mode="w") do |fh|
+    File.open(path, "w") do |fh|
       fh.puts [time0, ARGV.join(" ")].join(";")
     end
   end
@@ -576,13 +575,13 @@ class Status
   def set_tmpdir(tmpdir_path)
     status = ""
 
-    File.open(path, mode="r") do |fh|
+    File.open(path, "r") do |fh|
       status = fh.read.chomp
     end
 
     status = "#{status};#{tmpdir_path}\n"
 
-    File.open(path, mode="w") do |fh|
+    File.open(path, "w") do |fh|
       fh << status
     end
   end
@@ -590,7 +589,7 @@ class Status
   # Extract the temporary directory path from the status file,
   # and return this or nil if not found.
   def get_tmpdir
-    File.open(path, mode="r") do |fh|
+    File.open(path, "r") do |fh|
       tmpdir_path = fh.read.chomp.split(";").last
       return tmpdir_path if File.directory?(tmpdir_path)
     end
@@ -605,14 +604,14 @@ class Status
     script  = File.basename($0)
 
     stream = File.open(path)
-    time0, args, tmp_dir = stream.first.split(";")
+    time0, args = stream.first.split(";")
     stream.close
 
     elap     = time_diff(time0, time1)
     command  = [script, args].join(" ") 
     log_file = File.join(ENV["BP_LOG"], "biopieces.log")
 
-    File.open(log_file, mode = "a") { |file| file.puts [time0, time1, elap, user, exit_status, command].join("\t") }
+    File.open(log_file, "a") { |file| file.puts [time0, time1, elap, user, exit_status, command].join("\t") }
   end
 
   # Delete status file.
@@ -628,6 +627,8 @@ class Status
     script = File.basename($0)
     pid    = $$
     path   = File.join(ENV["BP_TMP"], [user, script, pid, "status"].join("."))
+
+    path
   end
 
   # Get the elapsed time from the difference between two time stamps.
@@ -657,7 +658,7 @@ at_exit do
 
   status = Status.new
   tmpdir = status.get_tmpdir
-  FileUtils.remove_entry_secure(tmpdir) unless tmpdir.nil?
+  FileUtils.remove_entry_secure(tmpdir, true) unless tmpdir.nil?
   status.log(exit_status)
   status.delete
 end