Class: MiGA::MiGA

Inherits:
Object
  • Object
show all
Extended by:
Common::Format, Common::Net, Common::Path, Common::SystemCall
Includes:
MiGA, Common
Defined in:
lib/miga/common.rb,
lib/miga/version.rb,
lib/miga/common/base.rb

Overview

Generic class used to handle system-wide information and methods, and parent of all other MiGA::* classes.

Constant Summary

Constants included from MiGA

CITATION, VERSION, VERSION_DATE, VERSION_NAME

Instance Attribute Summary

Attributes included from Common::Net

#remote_connection_uri

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common::Path

root_path, script_path

Methods included from Common::Format

clean_fasta_file, seqs_length, tabulate

Methods included from Common::Net

data_server, download_file_ftp, http_request, known_hosts, main_server, net_method, normalize_encoding, remote_connection

Methods included from Common::SystemCall

run_cmd, run_cmd_opts

Class Method Details

.CITATIONObject

Reference of MiGA



70
71
72
# File 'lib/miga/version.rb', line 70

def self.CITATION
  CITATION.map { |i| "- #{i}" }.join
end

.CITATION_ARRAYObject



74
75
76
# File 'lib/miga/version.rb', line 74

def self.CITATION_ARRAY
  CITATION
end

.DEBUG(*args) ⇒ Object

Send debug message



31
32
33
34
35
36
# File 'lib/miga/common/base.rb', line 31

def DEBUG(*args)
  $stderr.puts(*args) if debug?
  $stderr.puts(
    caller.map { |v| v.gsub(/^/, '     ') }.join("\n")
  ) if debug_trace?
end

.debug?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/miga/common/base.rb', line 38

def debug?
  @@DEBUG ||= false
end

.DEBUG_OFFObject

Turn off debugging



12
13
14
# File 'lib/miga/common/base.rb', line 12

def DEBUG_OFF
  @@DEBUG = false
end

.DEBUG_ONObject

Turn on debugging



6
7
8
# File 'lib/miga/common/base.rb', line 6

def DEBUG_ON
  @@DEBUG = true
end

.debug_trace?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/miga/common/base.rb', line 42

def debug_trace?
  @@DEBUG_TRACE ||= false
end

.DEBUG_TRACE_OFFObject

Turn off debug tracing (but not debugging)



25
26
27
# File 'lib/miga/common/base.rb', line 25

def DEBUG_TRACE_OFF
  @@DEBUG_TRACE = false
end

.DEBUG_TRACE_ONObject

Turn on debug tracing (and debugging)



18
19
20
21
# File 'lib/miga/common/base.rb', line 18

def DEBUG_TRACE_ON
  @@DEBUG_TRACE = true
  DEBUG_ON()
end

.FULL_VERSIONObject

Complete version as string



52
53
54
# File 'lib/miga/version.rb', line 52

def self.FULL_VERSION
  VERSION.join('.')
end

.initialized?Boolean

Has MiGA been initialized?

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/miga/common.rb', line 36

def self.initialized?
  File.exist?(rc_path) &&
    File.exist?(File.join(ENV['MIGA_HOME'], '.miga_daemon.json'))
end

.LONG_VERSIONObject

Complete version with nickname and date as string



58
59
60
# File 'lib/miga/version.rb', line 58

def self.LONG_VERSION
  "MiGA #{VERSION.join('.')} - #{VERSION_NAME} - #{VERSION_DATE}"
end

.rc_pathObject

Path to the .miga_rc file



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

def self.rc_path
  File.join(ENV['MIGA_HOME'], '.miga_rc')
end

.VERSIONObject

Major.minor version as Float



46
47
48
# File 'lib/miga/version.rb', line 46

def self.VERSION
  VERSION[0]
end

.VERSION_DATEObject

Date of the current gem release



64
65
66
# File 'lib/miga/version.rb', line 64

def self.VERSION_DATE
  VERSION_DATE
end

Instance Method Details

#advance(step, n = 0, total = nil, bin = true) ⇒ Object

Reports the advance of a task at step (String), the n out of total. The advance is reported in powers of 1,024 if bin is true, or powers of 1,000 otherwise. The report goes to $stderr iff –verbose



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/miga/common.rb', line 65

def advance(step, n = 0, total = nil, bin = true)
  # Initialize advance timing
  @_advance_time ||= { last: nil, n: 0, avg: nil, total: total }
  if @_advance_time[:n] > n || total != @_advance_time[:total]
    @_advance_time[:last] = nil
    @_advance_time[:n] = 0
    @_advance_time[:avg]  = nil
    @_advance_time[:total] = total
  end

  # Estimate timing
  adv_n = n - @_advance_time[:n]
  if total.nil? || @_advance_time[:last].nil? || adv_n.negative?
    # Initial report
    @_advance_time[:last] = Time.now
    @_advance_time[:n] = n
  elsif adv_n > 0.001 * total
    # Advance report (if change > 0.1% change and time > 1 second)
    this_time = (Time.now - @_advance_time[:last]).to_f
    return if this_time < 1.0 && n < total

    this_avg = this_time / adv_n
    @_advance_time[:avg] ||= this_avg
    @_advance_time[:avg] = 0.9 * @_advance_time[:avg] + 0.1 * this_avg
    @_advance_time[:last] = Time.now
    @_advance_time[:n] = n
  else
    # Final report (if the last update was too small) or ignore update
    return if n < total
  end

  # Report
  adv =
    if total.nil?
      (n == 0 ? '' : num_suffix(n, bin))
    else
      vals = [100.0 * n / total, num_suffix(n, bin), num_suffix(total, bin)]
      ('%.1f%% (%s/%s)' % vals)
    end
  left =
    if @_advance_time[:avg].nil?
      ''
    else
      left_time = @_advance_time[:avg] * (total - n) / 60 # <- in minutes
      left_time   < 0.01 ? '' :
        left_time < 1    ? ('%.0fs left' % (left_time * 60))   :
        left_time > 1440 ? ('%.1fd left' % (left_time / 1440)) :
        left_time > 60   ? ('%.1fh left' % (left_time / 60))   :
        ('%.1fm left' % left_time)
    end
  $stderr.print("[%s] %s %s %-12s   \r" % [Time.now, step, adv, left])
end

#like_io?(obj) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/miga/common.rb', line 134

def like_io?(obj)
  obj.is_a?(IO) || obj.is_a?(StringIO)
end

#num_suffix(n, bin = false) ⇒ Object

Return formatted number n with the appropriate units as powers of 1,000 (if bin if false) or 1,024 (otherwise)



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

def num_suffix(n, bin = false)
  p = ''
  { T: 4, G: 3, M: 2, K: 1 }.each do |k, x|
    v = (bin ? 1024 : 1e3)**x
    if n > v
      n = '%.1f' % (n.to_f / v)
      p = k
      break
    end
  end
  "#{n}#{p}"
end

#result_files_exist?(base, ext) ⇒ Boolean

Check if the result files exist with base name (String) followed by the ext values (Array of String).

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/miga/common.rb', line 44

def result_files_exist?(base, ext)
  ext = [ext] unless ext.is_a? Array
  MiGA::MiGA.DEBUG("Assserting files for result: #{ext}")
  ext.all? { |f| File.exist?(base + f) or File.exist?("#{base}#{f}.gz") }
end

#say(*par) ⇒ Object

Print par ensuring new line at the end. Date/time-stamp each line. If the first parameter is IO or StringIO the output is sent there, otherwise it’s sent to $stderr



55
56
57
58
# File 'lib/miga/common.rb', line 55

def say(*par)
  io = like_io?(par.first) ? par.shift : $stderr
  io.puts(*par.map { |i| "[#{Time.now}] #{i}" })
end