Class: MiGA::Result

Inherits:
MiGA
  • Object
show all
Defined in:
lib/miga/result.rb

Overview

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

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 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, 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

#pathObject (readonly)

Path to the JSON file describing the result.



26
27
28
# File 'lib/miga/result.rb', line 26

def path
  @path
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)


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

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.



17
18
19
20
# File 'lib/miga/result.rb', line 17

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.



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

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

#[]=(k, v) ⇒ Object

Adds value v to entry with symbol k.



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

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.



79
80
81
82
83
84
85
# File 'lib/miga/result.rb', line 79

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.



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

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.



159
160
161
162
# File 'lib/miga/result.rb', line 159

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.



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

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

#dirObject

Directory containing the result.



53
54
55
# File 'lib/miga/result.rb', line 53

def dir
  File.dirname(path)
end

#each_file(&blk) ⇒ Object

Iterate blk for each registered file. If blk calls for one argument, the relative path to the file is passed. If it calls for two arguments, the symbol describing the file is passed first and the path second. Note that multiple files may have the same symbol, since arrays of files are supported.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/miga/result.rb', line 141

def each_file(&blk)
  @data[:files] ||= {}
  self[:files].each do |k,files|
    files = [files] unless files.kind_of? Array
    files.each do |file|
      if blk.arity==1
        blk.call(file)
      elsif blk.arity==2
        blk.call(k, file)
      else
        raise "Wrong number of arguments: #{blk.arity} for one or two"
      end
    end
  end
end

#file_path(k) ⇒ Object

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



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

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.



113
114
115
116
117
118
119
# File 'lib/miga/result.rb', line 113

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

#remove!Object

Remove result, including all associated files.



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/miga/result.rb', line 123

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).



102
103
104
105
106
107
108
109
# File 'lib/miga/result.rb', line 102

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