Class: MiGA::MiGA

Inherits:
Object
  • Object
show all
Includes:
MiGA
Defined in:
lib/miga/common.rb,
lib/miga/version.rb

Overview

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

Constant Summary collapse

@@DEBUG =

Should debugging information be reported?

false
@@DEBUG_TRACE =

Should the trace of debugging information be reported?

false

Constants included from MiGA

CITATION, VERSION, VERSION_DATE, VERSION_NAME

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.CITATIONObject

Reference of MiGA.



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

def self.CITATION ; CITATION ; end

.clean_fasta_file(file) ⇒ Object

Cleans a FastA file in place.



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
117
118
119
# File 'lib/miga/common.rb', line 84

def self.clean_fasta_file(file)
  tmp_fh = nil
  begin
    if file =~ /\.gz/
      tmp_path = Tempfile.new('MiGA.gz').tap(&:close).path
      tmp_fh = Zlib::GzipWriter.open(tmp_path)
      fh = Zlib::GzipReader.open(file)
    else
      tmp_fh = Tempfile.new('MiGA')
      tmp_path = tmp_fh.path
      fh = File.open(file, 'r')
    end
    buffer = ''
    fh.each_line do |ln|
      ln.chomp!
      if ln =~ /^>\s*(\S+)(.*)/
        (id, df) = [$1, $2]
        tmp_fh.print buffer.wrap_width(80)
        buffer = ''
        tmp_fh.puts ">#{id.gsub(/[^A-Za-z0-9_\|\.]/, "_")}#{df}"
      else
        buffer << ln.gsub(/[^A-Za-z\.\-]/, '')
      end
    end
    tmp_fh.print buffer.wrap_width(80)
    tmp_fh.close
    fh.close
    FileUtils.cp(tmp_path, file)
  ensure
    begin
      tmp_fh.close unless tmp_fh.nil?
      File.unlink(tmp_path) unless tmp_path.nil?
    rescue
    end
  end
end

.DEBUG(*args) ⇒ Object

Send debug message.



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

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

.DEBUG_OFFObject

Turn off debugging.



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

def self.DEBUG_OFF() @@DEBUG=false end

.DEBUG_ONObject

Turn on debugging.



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

def self.DEBUG_ON() @@DEBUG=true end

.DEBUG_TRACE_OFFObject

Turn off debug tracing (but not debugging).



47
48
49
# File 'lib/miga/common.rb', line 47

def self.DEBUG_TRACE_OFF
  @@DEBUG_TRACE=false
end

.DEBUG_TRACE_ONObject

Turn on debug tracing (and debugging).



40
41
42
43
# File 'lib/miga/common.rb', line 40

def self.DEBUG_TRACE_ON
  @@DEBUG_TRACE=true
  self.DEBUG_ON
end

.FULL_VERSIONObject

Complete version as string.



40
# File 'lib/miga/version.rb', line 40

def self.FULL_VERSION ; VERSION.join(".") ; end

.initialized?Boolean

Has MiGA been initialized?

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/miga/common.rb', line 62

def self.initialized?
  File.exist?(File.expand_path('.miga_rc', ENV['MIGA_HOME'])) and
    File.exist?(File.expand_path('.miga_daemon.json', ENV['MIGA_HOME']))
end

.LONG_VERSIONObject

Complete version with nickname and date as string.



44
45
46
# File 'lib/miga/version.rb', line 44

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

.root_pathObject

Root path to MiGA (as estimated from the location of the current file).



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

def self.root_path
  File.expand_path('../../..', __FILE__)
end

.script_path(task, opts = {}) ⇒ Object

Path to a script to be executed for task. Supported opts are:

  • :miga Path to the MiGA home to use. If not passed, the home of the library is used).

  • :project MiGA::Project object to check within plugins. If not passed, only core scripts are supported.



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/miga/common.rb', line 170

def self.script_path(task, opts={})
  opts[:miga] ||= root_path
  unless opts[:project].nil?
    opts[:project].plugins.each do |pl|
      if File.exist? File.expand_path("scripts/#{task}.bash", pl)
        opts[:miga] = pl
      end
    end
  end
  File.expand_path("scripts/#{task}.bash", opts[:miga])
end

.seqs_length(file, format, opts = {}) ⇒ Object

Calculates the average and standard deviation of the sequence lengths in a FastA or FastQ file (supports gzipped files). The format must be a Symbol, one of :fasta or :fastq. Additional estimations can be controlled via the opts Hash. Supported options include:

  • :n50: If true, it also returns the N50 and the median (in bp).

  • gc: If true, it also returns the G+C content (in %).



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/miga/common.rb', line 128

def self.seqs_length(file, format, opts={})
  fh = (file =~ /\.gz/) ? Zlib::GzipReader.open(file) : File.open(file, 'r')
  l = []
  gc = 0
  i = 0 # <- Zlib::GzipReader doesn't set $.
  fh.each_line do |ln|
    i += 1
    if (format==:fasta and ln =~ /^>/) or (format==:fastq and (i % 4)==1)
      l << 0
    elsif format==:fasta or (i % 4)==2
      l[l.size-1] += ln.chomp.size
      gc += ln.scan(/[GCgc]/).count if opts[:gc]
    end
  end
  fh.close
  
  o = { n: l.size, tot: l.inject(:+) }
  o[:avg] = o[:tot].to_f/l.size
  o[:var] = l.map{ |a| a ** 2 }.inject(:+).to_f/l.size - o[:avg]**2
  o[:sd]  = Math.sqrt o[:var]
  o[:gc]  = 100.0*gc/o[:tot] if opts[:gc]
  if opts[:n50]
    l.sort!
    thr = o[:tot]/2
    pos = 0
    l.each do |a|
      pos += a
      o[:n50] = a
      break if pos >= thr
    end
    o[:med] = o[:n].even? ?
      0.5*l[o[:n]/2-1,2].inject(:+) : l[(o[:n]-1)/2]
  end
  o
end

.tabulate(header, values) ⇒ Object

Tabulates an values, and Array of Arrays, all with the same number of entries as header. Returns an Array of String, one per line.



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/miga/common.rb', line 70

def self.tabulate(header, values)
  fields = [header.map(&:to_s)]
  fields << fields.first.map{ |h| h.gsub(/\S/, '-') }
  fields += values.map{ |row| row.map{ |cell| cell.nil? ? '?' : cell.to_s } }
  clen = fields.map{ |row| row.map(&:length) }.transpose.map(&:max)
  fields.map do |row|
    (0 .. clen.size-1).map do |col_n|
      col_n==0 ? row[col_n].rjust(clen[col_n]) : row[col_n].ljust(clen[col_n])
    end.join('  ')
  end
end

.VERSIONObject

Major.minor version as Float.



36
# File 'lib/miga/version.rb', line 36

def self.VERSION ; VERSION[0] ; end

.VERSION_DATEObject

Date of the current gem release.



50
# File 'lib/miga/version.rb', line 50

def self.VERSION_DATE ; VERSION_DATE ; end

Instance Method Details

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


185
186
187
188
189
190
# File 'lib/miga/common.rb', line 185

def result_files_exist?(base, ext)
  ext = [ext] unless ext.is_a? Array
  ext.all? do |f|
    File.exist?(base + f) or File.exist?("#{base}#{f}.gz")
  end
end