Class: MiGA::Result

Inherits:
MiGA
  • Object
show all
Includes:
Dates
Defined in:
lib/miga/result.rb,
lib/miga/result/base.rb

Overview

The result from a task run. It can be project-wide or dataset-specific.

Defined Under Namespace

Modules: Base, Dates

Constant Summary

Constants included from MiGA

CITATION, VERSION, VERSION_DATE, VERSION_NAME

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dates

#done_at, #running_time, #started_at

Methods inherited from MiGA

CITATION, DEBUG, DEBUG_OFF, DEBUG_ON, DEBUG_TRACE_OFF, DEBUG_TRACE_ON, FULL_VERSION, LONG_VERSION, VERSION, VERSION_DATE, clean_fasta_file, initialized?, #result_files_exist?, root_path, script_path, seqs_length, tabulate

Constructor Details

#initialize(path) ⇒ Result

Load or create the MiGA::Result described by the JSON file path.


38
39
40
41
# File 'lib/miga/result.rb', line 38

def initialize(path)
  @path = path
  MiGA::Result.exist?(path) ? self.load : create
end

Instance Attribute Details

#dataObject (readonly)

Hash with the result metadata.


30
31
32
# File 'lib/miga/result.rb', line 30

def data
  @data
end

#resultsObject (readonly)

Array of MiGA::Result objects nested within the result (if any).


34
35
36
# File 'lib/miga/result.rb', line 34

def results
  @results
end

Class Method Details

.exist?(path) ⇒ Boolean

Check if the result described by the JSON in path already exists.

Returns:

  • (Boolean)

16
# File 'lib/miga/result.rb', line 16

def self.exist?(path) File.exist? path end

.load(path) ⇒ Object

Load the result described by the JSON in path. Returns MiGA::Result if it already exists, nil otherwise.


21
22
23
24
# File 'lib/miga/result.rb', line 21

def self.load(path)
  return nil unless MiGA::Result.exist? path
  MiGA::Result.new(path)
end

Instance Method Details

#[](k) ⇒ Object

Entry with symbol k.


85
# File 'lib/miga/result.rb', line 85

def [](k) data[k.to_sym] ; end

#[]=(k, v) ⇒ Object

Adds value v to entry with symbol k.


89
# File 'lib/miga/result.rb', line 89

def []=(k,v) data[k.to_sym]=v ; end

#add_file(k, file) ⇒ Object

Register file (path relative to #dir) with the symbol k. If the file doesn't exist but the .gz extension does, the gzipped file is registered instead. If neither exists, nothing is registered.


95
96
97
98
99
100
101
# File 'lib/miga/result.rb', line 95

def add_file(k, file)
  k = k.to_sym
  @data[:files] ||= {}
  @data[:files][k] = file if File.exist? File.expand_path(file, dir)
  @data[:files][k] = "#{file}.gz" if
    File.exist? File.expand_path("#{file}.gz", dir)
end

#add_files(files) ⇒ Object

#add_file for each key-value pair in the files Hash.


105
106
107
# File 'lib/miga/result.rb', line 105

def add_files(files)
  files.each { |k, v| add_file(k, v) }
end

#add_result(result) ⇒ Object

Add the MiGA::Result result as part of the current result.


180
181
182
183
# File 'lib/miga/result.rb', line 180

def add_result(result)
  @data[:results] << result.path
  save
end

#clean!Object

Register the result as cleaned.


49
# File 'lib/miga/result.rb', line 49

def clean! ; self[:clean] = true ; end

#clean?Boolean

Is the result clean? Returns Boolean.

Returns:

  • (Boolean)

45
# File 'lib/miga/result.rb', line 45

def clean? ; !! self[:clean] ; end

#createObject

Initialize and #save empty result.


111
112
113
114
# File 'lib/miga/result.rb', line 111

def create
  @data = {:created=>Time.now.to_s, :results=>[], :stats=>{}, :files=>{}}
  save
end

#dirObject

Directory containing the result.


69
70
71
# File 'lib/miga/result.rb', line 69

def dir
  File.dirname(path)
end

#each_file(&blk) ⇒ Object

Iterate blk for each registered file. Depending on the number of arguments of blk (arity), it's called as:

  • blk

  • blk[file_sym, file_rel]

  • blk[file_sym, file_rel, file_abs]

Note that multiple files may have the same symbol (file_sym), since arrays of files are supported.


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/miga/result.rb', line 159

def each_file(&blk)
  @data[:files] ||= {}
  self[:files].each do |k,files|
    files = [files] unless files.kind_of? Array
    files.each do |file|
      case blk.arity
      when 1
        blk.call(file)
      when 2
        blk.call(k, file)
      when 3
        blk.call(k, file, File.expand_path(file, dir))
      else
        raise "Wrong number of arguments: #{blk.arity} for 1..3"
      end
    end
  end
end

#file_path(k) ⇒ Object

Absolute path to the file(s) defined by symbol k.


75
76
77
78
79
80
81
# File 'lib/miga/result.rb', line 75

def file_path(k)
  k = k.to_sym
  f = self[:files].nil? ? nil : self[:files][k]
  return nil if f.nil?
  return File.expand_path(f, dir) unless f.is_a? Array
  f.map{ |fi| File.expand_path(fi, dir) }
end

#loadObject

Load (or reload) result data in the JSON file #path.


129
130
131
132
133
134
135
# File 'lib/miga/result.rb', line 129

def load
  json = File.read(path)
  raise "Impossible to load result, empty descriptor: #{path}." if json.empty?
  @data = JSON.parse(json, {:symbolize_names=>true})
  @data[:files] ||= {}
  @results = (self[:results] || []).map{ |rs| MiGA::Result.new rs }
end

#path(which = :json) ⇒ Object

Path to the standard files of the result. which must be one of:

  • :json (default) : JSON file describing the result.

  • :start : File with the date when the processing started.

  • :done : File with the date when the processing ended.


56
57
58
59
60
61
62
63
64
65
# File 'lib/miga/result.rb', line 56

def path(which=:json)
  case which.to_sym
  when :json
    @path
  when :start
    @path.sub(/\.json$/, ".start")
  when :done
    @path.sub(/\.json$/, ".done")
  end
end

#remove!Object

Remove result, including all associated files.


139
140
141
142
143
144
145
146
147
148
149
# File 'lib/miga/result.rb', line 139

def remove!
  each_file do |file|
    f = File.expand_path(file, dir)
    FileUtils.rm_rf(f) if File.exist? f
  end
  %w(.start .done).each do |ext|
    f = path.sub(/\.json$/, ext)
    File.unlink f if File.exist? f
  end
  File.unlink path
end

#saveObject

Save the result persistently (in the JSON file #path).


118
119
120
121
122
123
124
125
# File 'lib/miga/result.rb', line 118

def save
  @data[:updated] = Time.now.to_s
  json = JSON.pretty_generate data
  ofh = File.open(path, "w")
  ofh.puts json
  ofh.close
  self.load
end