]> git.donarmstrong.com Git - biopieces.git/commitdiff
updated biopieces code
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Thu, 27 May 2010 10:09:40 +0000 (10:09 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Thu, 27 May 2010 10:09:40 +0000 (10:09 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@973 74ccb610-7750-0410-82ae-013aeee3265d

code_ruby/Maasha/lib/biopieces.rb

index f84f0b225a5a83cecf8c91078a1b6349edadf8e2..2828a4211f1c487f3aa5c1a59a5d8e91f81b7e35 100644 (file)
@@ -6,12 +6,17 @@ require 'pp'
 class CastError < StandardError
 end
 
-# Class using OptionParser to parse command line options according to a list of
-# casts. Each cast prescribes the long and short name of the option, the type,
-# if it is mandatory, the default value, and allowed and disallowed values. An
-# optional list of extra casts can be supplied, and the integrity of the casts
-# are checked. Following the command line parsing, the options are checked
-# according to the casts.
+# Class using for handling Biopieces the parsing and emitting of Biopiece records,
+# which are ASCII text records consisting of lines with a key/value pair seperated
+# by a colon and a white space ': '. Each record is separated by a line with three
+# dashes '---'.
+# 
+# Biopieces are command line scripts and uses OptionParser to parse command line
+# options according to a list of casts. Each cast prescribes the long and short
+# name of the option, the type, if it is mandatory, the default value, and allowed
+# and disallowed values. An optional list of extra casts can be supplied, and the
+# integrity of the casts are checked. Following the command line parsing, the
+# options are checked according to the casts.
 class Biopieces
   TYPES        = %w[flag string list int uint float file file! files files! dir dir! genome]
   MANDATORY    = %w[long short type mandatory default allowed disallowed]
@@ -19,6 +24,11 @@ class Biopieces
   REGEX_INT    = /^(int|uint)$/
   REGEX_STRING = /^(string|file|file!|dir|dir!|genome)$/
 
+  # Initialize a Biopiece and write the status to file.
+  def initialize
+    status_set
+  end
+
   # Check the integrity of a list of casts, followed by parsion options from argv
   # and finally checking the options according to the casts. Returns nil if
   # argv is empty, otherwise an options hash.
@@ -76,6 +86,52 @@ class Biopieces
     @options
   end
 
+  # Open Biopiece input stream if not open and iterate over all Biopiece
+  # records in the stream.
+  def each_record
+    @in = stream_in_open unless @in.is_a? IO
+
+    record = {}
+
+    @in.each_line do |line|
+      case line
+      when /^([^:]+): (.*)$/
+        record[$1] = $2
+      when /^---$/
+        yield record unless record.empty?
+        record = {}
+      else
+        raise "Bad record format: #{line}"
+      end
+    end
+
+    yield record unless record.empty?
+
+    self # conventionally
+  end
+
+  alias :each :each_record
+
+  # Open Biopiece output stream if not open and puts record to the stream.
+  def puts(record)
+    @out = stream_out_open unless @out.is_a? IO
+
+    record.each do |key,value|
+      @out.print "#{key}: #{value}\n"
+    end
+
+    @out.print "---\n"
+  end
+
+  # Close Biopiece streams, remove tmp_dir, end log status.
+  def clean
+    @in.close  if @in.respond_to?  :close
+    @out.close if @out.respond_to? :close
+    # remove tmpdir if found
+    status_log
+    # remove status file
+  end
+
   private
 
   # Given the script name determine the path of the wiki file with the usage info.
@@ -339,47 +395,6 @@ class Biopieces
       end
     end
   end
-end
-
-
-class Stream
-  def initialize(options)
-    @options = options
-    @in      = stream_in_open
-    @out     = stream_out_open
-  end
-
-  def each_record
-    record = {}
-
-    @in.each_line do |line|
-      case line
-      when /^([^:]+): (.*)$/
-        record[$1] = $2
-      when /^---$/
-        yield record unless record.empty?
-        record = {}
-      else
-        raise "Bad record format: #{line}"
-      end
-    end
-
-    yield record unless record.empty?
-
-    self # conventionally
-  end
-
-  alias :each :each_record
-
-  def puts(record)
-    record.each do |key,value|
-      @out.print "#{key}: #{value}\n"
-    end
-
-    @out.print "---\n"
-  end
-
-  private
 
   # Open Biopieces input data stream for reading from either
   # stdin or from a list of files specified in options["stream_in"].
@@ -469,14 +484,8 @@ class Stream
   def nwrite(file)
     File.open(file, mode="w")
   end
-end
-
-
-class Status
-  def initialize
-    status_set
-  end
 
+  # Write the status to a status file.
   def status_set
     now    = Time.new
     time   = now.strftime("%Y-%m-%d %X")
@@ -488,7 +497,8 @@ class Status
     File.open(path, mode="w") { |file| file.puts [time, ARGV.join(" ")].join(";") }
   end
 
-  def log(status="OK")
+  # Write the Biopiece status to the log file.
+  def status_log(status="OK")
     now     = Time.new
     time1   = now.strftime("%Y-%m-%d %X")
     user    = ENV["USER"]
@@ -508,8 +518,7 @@ class Status
     File.open(log_file, mode="a") { |file| file.puts [time0, time1, elap, user, status, command].join("\t") }
   end
 
-  private
-
+  # Get the elapsed time from the difference between two time stamps.
   def time_diff(t0, t1)
     t0 =~ /(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/
     year0 = $1.to_i
@@ -551,5 +560,4 @@ class Status
   end
 end
 
-
 __END__