Module: MiGA::Cli::Action::Init::FilesHelper

Included in:
MiGA::Cli::Action::Init
Defined in:
lib/miga/cli/action/init/files_helper.rb

Overview

Helper module with files configuration functions for MiGA::Cli::Action::Init

Instance Method Summary collapse

Instance Method Details

#check_additional_files(paths) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/miga/cli/action/init/files_helper.rb', line 55

def check_additional_files(paths)
  if cli[:mytaxa]
    check_mytaxa_scores(paths)
    check_mytaxa_database(paths)
  end
  check_rdp_classifier if cli[:rdp]
  check_phyla_lite
  cli.puts ''
end

#check_configuration_script(rc_fh) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/miga/cli/action/init/files_helper.rb', line 37

def check_configuration_script(rc_fh)
  unless File.exist? cli[:config]
    cli[:config] = cli.ask_user(
      'Is there a script I need to load at startup?',
      cli[:config]
    )
  end
  if File.exist? cli[:config]
    cli[:config] = File.expand_path(cli[:config])
    cli.puts "Found bash configuration script: #{cli[:config]}"
    rc_fh.puts "MIGA_STARTUP='#{cli[:config]}'"
    rc_fh.puts '. "$MIGA_STARTUP"'
  else
    cli[:config] = '/dev/null'
  end
  cli.puts ''
end

#check_mytaxa_database(paths) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/miga/cli/action/init/files_helper.rb', line 96

def check_mytaxa_database(paths)
  cli.print 'Looking for MyTaxa DB... '
  mt = File.dirname(paths['MyTaxa'])
  dmnd_db = 'AllGenomes.faa.dmnd'
  miga_db = File.join(ENV['MIGA_HOME'], '.miga_db')
  home_db = File.join(miga_db, dmnd_db)
  mt_db = File.join(mt, 'AllGenomes.faa.dmnd')
  if File.exist?(home_db)
    cli.puts 'yes'
  elsif File.exist?(mt_db)
    cli.puts 'yes, sym-linking'
    FileUtils.mkdir_p(miga_db)
    File.symlink(mt_db, home_db)
  else
    cli.puts 'no, downloading'
    MiGA::MiGA.download_file_ftp(:miga_dist, dmnd_db, home_db) do |n, size|
      cli.advance("#{dmnd_db}:", n, size)
    end
    cli.puts
  end
end

#check_mytaxa_scores(paths) ⇒ Object



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
# File 'lib/miga/cli/action/init/files_helper.rb', line 65

def check_mytaxa_scores(paths)
  cli.print 'Looking for MyTaxa scores... '
  arch    = 'mytaxa_db.tar.gz'
  mt      = File.dirname(paths['MyTaxa'])
  mt_db   = File.join(mt, 'db')
  mt_db_1 = File.join(mt_db, 'ncbiSciNames.lib')
  miga_db = File.join(ENV['MIGA_HOME'], '.miga_db', 'mytaxa')
  home_db = File.join(miga_db, 'db')
  home_db_1 = File.join(home_db, 'ncbiSciNames.lib')

  if File.exist?(home_db_1)
    cli.puts 'yes'
  elsif File.exist?(mt_db_1)
    cli.puts 'yes, sym-linking'
    FileUtils.mkdir_p(miga_db)
    File.symlink(mt_db, home_db)
  else
    cli.puts 'no, downloading'
    MiGA::MiGA.download_file_ftp(
      :miga_dist, arch, File.join(miga_db, arch)
    ) { |n, size| cli.advance("#{arch}:", n, size) }
    cmd = <<~CMD
      cd #{miga_db.shellescape} \
        && tar zxf #{arch.shellescape} \
        && rm #{arch.shellescape}
    CMD
    run_cmd(cli, cmd, source: nil)
    cli.puts
  end
end

#check_phyla_liteObject



141
142
143
144
145
# File 'lib/miga/cli/action/init/files_helper.rb', line 141

def check_phyla_lite
  cli.puts 'Looking for Phyla Lite... '
  cmd = ['get_db', '-n', 'Phyla_Lite', '--no-overwrite']
  MiGA::Cli.new(cmd).launch(true)
end

#check_rdp_classifierObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/miga/cli/action/init/files_helper.rb', line 118

def check_rdp_classifier
  cli.print 'Looking for RDP classifier... '
  miga_db = File.join(ENV['MIGA_HOME'], '.miga_db')
  file = 'classifier.jar'
  path = File.join(miga_db, file)
  if File.size?(path)
    cli.puts 'yes'
  else
    cli.puts 'no, downloading'
    arch = 'classifier.tar.gz'
    MiGA::MiGA.download_file_ftp(
      :miga_dist, arch, File.join(miga_db, arch)
    ) { |n, size| cli.advance("#{arch}:", n, size) }
    cmd = <<~CMD
      cd #{miga_db.shellescape} \
        && tar zxf #{arch.shellescape} \
        && rm #{arch.shellescape}
    CMD
    run_cmd(cli, cmd, source: nil)
    cli.puts
  end
end

#close_rc_file(rc_fh) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/miga/cli/action/init/files_helper.rb', line 26

def close_rc_file(rc_fh)
  rc_fh.puts <<~FOOT

    MIGA_CONFIG_VERSION='#{MiGA::MiGA.FULL_VERSION}'
    MIGA_CONFIG_LONGVERSION='#{MiGA::MiGA.LONG_VERSION}'
    MIGA_CONFIG_DATE='#{Time.now}'

  FOOT
  rc_fh.close
end

#open_rc_fileObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/miga/cli/action/init/files_helper.rb', line 6

def open_rc_file
  rc_path = File.expand_path('.miga_rc', ENV['MIGA_HOME'])
  if File.exist? rc_path
    if cli.ask_user(
      'I found a previous configuration. Do you want to continue?',
      'yes', %w(yes no)
    ) == 'no'
      cli.puts 'OK, see you soon!'
      exit(0)
    end
  end
  rc_fh = File.open(rc_path, 'w')
  rc_fh.puts <<~BASH
    #!/bin/bash
    # `miga init` made this on #{Time.now}

  BASH
  rc_fh
end