class MiGA::Cli::Action::Doctor

Public Class Methods

OPERATIONS() click to toggle source
# File lib/miga/cli/action/doctor.rb, line 55
def OPERATIONS
  @@OPERATIONS
end

Public Instance Methods

check_db(cli) click to toggle source
# File lib/miga/cli/action/doctor.rb, line 60
def check_db(cli)
  cli.say 'Checking databases integrity'
  cli.load_project.each_dataset do |d|
    [:distances, :taxonomy].each do |r_key|
      r = d.result(r_key) or next
      {haai_db: :aai, aai_db: :aai, ani_db: :ani}.each do |db_key, metric|
        db_file = r.file_path(db_key) or next
        check_sqlite3_database(db_file, metric) do
          cli.say(
            "  > Removing #{db_key} #{r_key} table for #{d.name}")
          [db_file, r.path(:done), r.path].each do |f|
            File.unlink(f) if File.exist? f
          end # each |f|
        end # check_sqlite3_database
      end # each |db_key, metric|
    end # each |r_key|
  end # each |d|
end
check_dist(cli) click to toggle source
# File lib/miga/cli/action/doctor.rb, line 79
def check_dist(cli)
  p = cli.load_project
  [:ani, :aai].each do |dist|
    res = p.result("#{dist}_distances")
    next if res.nil?
    cli.say "Checking #{dist} table for consistent datasets"
    notok, fix = check_dist_eval(cli, p, res)
    check_dist_fix(cli, p, fix)
    check_dist_recompute(cli, res, notok)
  end
end
check_ess(cli) click to toggle source
# File lib/miga/cli/action/doctor.rb, line 111
def check_ess(cli)
  cli.say 'Looking for unarchived essential genes'
  cli.load_project.each_dataset do |d|
    res = d.result(:essential_genes)
    next if res.nil?
    dir = res.file_path(:collection)
    if dir.nil?
      cli.say "  > Removing #{d.name}:essential_genes"
      res.remove!
      next
    end
    next if Dir["#{dir}/*.faa"].empty?
    cli.say "  > Fixing #{d.name}"
    cmdo = %xcd '#{dir}' && tar -zcf proteins.tar.gz *.faa && rm *.faa`.chomp
    warn(cmdo) unless cmdo.empty?
  end
end
check_files(cli) click to toggle source
# File lib/miga/cli/action/doctor.rb, line 91
def check_files(cli)
  cli.say 'Looking for outdated files in results'
  p = cli.load_project
  p.each_dataset do |d|
    d.each_result do |r_k, r|
      ok = true
      r.each_file do |_f_sym, _f_rel, f_abs|
        unless File.exist? f_abs
          ok = false
          break
        end
      end
      unless ok
        cli.say "  > Registering again #{d.name}:#{r_k}"
        d.add_result(r_k, true, force: true)
      end
    end
  end
end
check_mts(cli) click to toggle source
# File lib/miga/cli/action/doctor.rb, line 129
def check_mts(cli)
  cli.say 'Looking for unarchived MyTaxa Scan runs'
  cli.load_project.each_dataset do |d|
    res = d.result(:mytaxa_scan)
    next if res.nil?
    dir = res.file_path(:regions)
    fix = false
    unless dir.nil?
      if Dir.exist? dir
        cmdo = %xcd '#{dir}/..' \
              && tar -zcf '#{d.name}.reg.tar.gz' '#{d.name}.reg' \
              && rm -r '#{d.name}.reg'`.chomp
        warn(cmdo) unless cmdo.empty?
      end
      fix = true
    end
    %w[blast mytaxain wintax gene_ids region_ids].each do |ext|
      file = res.file_path(ext.to_sym)
      unless file.nil?
        FileUtils.rm(file) if File.exist? file
        fix = true
      end
    end
    if fix
      cli.say "  > Fixing #{d.name}"
      d.add_result(:mytaxa_scan, true, force: true)
    end
  end
end
check_sqlite3_database(db_file, metric) { || ... } click to toggle source
# File lib/miga/cli/action/doctor.rb, line 30
def check_sqlite3_database(db_file, metric)
  SQLite3::Database.new(db_file) do |conn|
    conn.execute("select count(*) from #{metric}").first
  end
rescue SQLite3::SQLException
  yield
end
check_start(cli) click to toggle source
# File lib/miga/cli/action/doctor.rb, line 159
def check_start(cli)
  cli.say 'Looking for legacy .start files lingering'
  cli.load_project.each_dataset do |d|
    d.each_result do |r_k, r|
      if File.exist? r.path(:start)
        cli.say "  > Registering again #{d.name}:#{r_k}"
        r.save
      end
    end
  end
end
check_tax(cli) click to toggle source
# File lib/miga/cli/action/doctor.rb, line 171
def check_tax(cli)
  #cli.say 'o Checking for taxonomy/distances consistency'
  # TODO: Find 95%ANI clusters with entries from different species
end
parse_cli() click to toggle source
# File lib/miga/cli/action/doctor.rb, line 9
def parse_cli
  @@OPERATIONS.keys.each { |i| cli.defaults = { i => true } }
  cli.parse do |opt|
    operation_n = Hash[@@OPERATIONS.map { |k,v| [v[0], k] }]
    cli.opt_object(opt, [:project])
    opt.on(
      '--ignore TASK1,TASK2', Array,
      'Do not perform the task(s) listed. Available tasks are:',
      * @@OPERATIONS.values.map { |v| "~ #{v[0]}: #{v[1]}" }
    ) { |v| v.map { |i| cli[operation_n[i]] = false } }
    opt.on(
      '--only TASK',
      'Perform only the specified task (see --ignore)'
    ) do |v|
      op_k = @@OPERATIONS.find { |_, i| i[0] == v.downcase }.first
      @@OPERATIONS.keys.each { |i| cli[i] = false }
      cli[op_k] = true
    end
  end
end
perform() click to toggle source
# File lib/miga/cli/action/doctor.rb, line 38
def perform
  p = cli.load_project
  @@OPERATIONS.keys.each do |k|
    send("check_#{k}", cli) if cli[k]
  end
end