Module: MiGA::Cli::Action::Wf

Included in:
ClassifyWf, DerepWf, IndexWf, PreprocWf, QualityWf
Defined in:
lib/miga/cli/action/wf.rb

Overview

Helper module for workflows

Instance Method Summary collapse

Instance Method Details

#call_cli(cmd) ⇒ Object



208
209
210
211
212
# File 'lib/miga/cli/action/wf.rb', line 208

def call_cli(cmd)
  cmd << '-v' if cli[:verbose]
  MiGA::MiGA.DEBUG "Cli::Action::Wf.call_cli #{cmd}"
  MiGA::Cli.new(cmd.map(&:to_s)).launch(true)
end

#cleanupObject



198
199
200
201
202
203
204
205
206
# File 'lib/miga/cli/action/wf.rb', line 198

def cleanup
  return if cli[:prepare_and_exit]
  return unless cli[:clean]

  cli.say 'Cleaning up intermediate files'
  %w[data daemon metadata miga.project.json].each do |f|
    FileUtils.rm_rf(File.expand_path(f, cli[:outdir]))
  end
end

#create_project(stage, p_metadata = {}, d_metadata = {}) ⇒ Object



125
126
127
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/miga/cli/action/wf.rb', line 125

def create_project(stage,  = {},  = {})
  cli.ensure_par(
    outdir: '-o',
    project_type: '--project-type',
    dataset_type: '--dataset-type'
  )
  paired = cli[:input_type].to_s.include?('_paired')
  cli[:regexp] ||= MiGA::Cli.FILE_REGEXP(paired)

  # Create empty project
  call_cli(
    ['new', '-P', cli[:outdir], '-t', cli[:project_type]]
  ) unless MiGA::Project.exist? cli[:outdir]

  # Define project metadata
  p = cli.load_project(:outdir, '-o')
  [:type] = cli[:project_type]
  (p, )
  %i[haai_p aai_p ani_p ess_coll min_qual].each do |i|
    p.set_option(i, cli[i])
  end

  # Download datasets from NCBI
  unless cli[:ncbi_taxon].nil?
    what = cli[:ncbi_draft] ? '--all' : '--complete'
    cmd = ['ncbi_get', '-P', cli[:outdir], '-T', cli[:ncbi_taxon], what]
    cmd += ['--max', cli[:ncbi_max]] if cli[:ncbi_max]
    call_cli(cmd)
  end

  # Download datasets from GTDB
  unless cli[:gtdb_taxon].nil?
    cmd = ['gtdb_get', '-P', cli[:outdir], '-T', cli[:gtdb_taxon]]
    cmd << '--reference' if cli[:gtdb_ref]
    cmd += ['--max', cli[:ncbi_max]] if cli[:ncbi_max]
    call_cli(cmd)
  end

  # Add datasets
  call_cli(
    [
      'add',
      '--ignore-dups',
      '-P', cli[:outdir],
      '-t', cli[:dataset_type],
      '-i', stage,
      '-R', cli[:regexp]
    ] + cli.files
  ) unless cli.files.empty?

  # Define datasets metadata
  p.load
  [:type] = cli[:dataset_type]
  p.each_dataset { |d| (d, ) }
  p
end

#default_opts_for_wfObject



7
8
9
10
11
12
13
14
# File 'lib/miga/cli/action/wf.rb', line 7

def default_opts_for_wf
  cli.expect_files = true
  cli.defaults = {
    clean: false, project_type: :genomes, dataset_type: :popgenome,
    ncbi_draft: true, min_qual: MiGA::Project.OPTIONS[:min_qual][:default],
    prepare_and_exit: false
  }
end

#opts_for_wf(opt, files_desc, params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
# File 'lib/miga/cli/action/wf.rb', line 16

def opts_for_wf(opt, files_desc, params = {})
  {
    multi: false, cleanup: true, project_type: false, ncbi: true, qual: true
  }.each { |k, v| params[k] = v if params[k].nil? }
  opt.on(
    '-o', '--out_dir PATH',
    '(Mandatory) Directory to be created with all output data'
  ) { |v| cli[:outdir] = v }
  opt.separator ''
  opt.separator "    FILES...: #{files_desc}"
  opt.separator ''
  opt.separator 'Workflow Control Options'
  opt.on(
    '-C', '--collection STRING',
    'Collection of essential genes to use as reference',
    'One of: dupont_2012 (default), lee_2019'
  ) { |v| cli[:ess_coll] = v }
  if params[:ncbi]
    opt.on(
      '-T', '--ncbi-taxon STRING',
      'Download all the genomes in NCBI classified as this taxon'
    ) { |v| cli[:ncbi_taxon] = v }
    opt.on(
      '-G', '--gtdb-taxon STRING',
      'Download all the genomes in GTDB classified as this taxon'
    ) { |v| cli[:gtdb_taxon] = v }
    opt.on(
      '--no-draft',
      'Only download complete genomes, not drafts (requires -T)'
    ) { |v| cli[:ncbi_draft] = v }
    opt.on(
      '--gtdb-ref',
      'Only download reference anchor genomes in GTDB (requires -G)'
    ) { |v| cli[:gtdb_ref] = v }
    opt.on(
      '--max-download INT', Integer,
      'Maximum number of genomes to download (by default: unlimited)'
    ) { |v| cli[:ncbi_max] = v }
  end
  if params[:qual]
    opt.on(
      '--min-qual FLOAT',
      'Minimum genome quality to include in analysis',
      "By default: #{cli[:min_qual]}"
    ) { |v| cli[:min_qual] = v == 'no' ? v : v.to_f }
  end
  if params[:cleanup]
    opt.on(
      '-c', '--clean',
      'Clean all intermediate files after generating the reports'
    ) { |v| cli[:clean] = v }
  end
  opt.on(
    '-R', '--name-regexp REGEXP', Regexp,
    'Regular expression indicating how to extract the name from the path',
    "By default: '#{MiGA::Cli.FILE_REGEXP}'"
  ) { |v| cli[:regexp] = v }
  opt_object_type(opt, :dataset, params[:multi])
  opt_object_type(opt, :project, params[:multi]) if params[:project_type]
  opt.on(
    '--daemon PATH',
    'Use custom daemon configuration in JSON format',
    'By default: ~/.miga_daemon.json'
  ) { |v| cli[:daemon_json] = v }
  opt.on(
    '-j', '--jobs INT', Integer,
    'Number of parallel jobs to execute',
    'By default controlled by the daemon configuration (maxjobs)'
  ) { |v| cli[:jobs] = v }
  opt.on(
    '-t', '--threads INT', Integer,
    'Number of CPUs to use per job',
    'By default controlled by the daemon configuration (ppn)'
  ) { |v| cli[:threads] = v }
  opt.on(
    '--threads-project INT', Integer,
    'Number of CPUs to use per project-wide job',
    'By default controlled by the daemon configuration (ppn_project or ppn)'
  ) { |v| cli[:threads_project] = v }
  opt.on(
    '--prepare-and-exit',
    'Create project and import datasets, but do not run any analyses'
  ) { |v| cli[:prepare_and_exit] = v }
end

#opts_for_wf_distances(opt) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/miga/cli/action/wf.rb', line 101

def opts_for_wf_distances(opt)
  opt.on('--sensitive', 'Alias to: --aai-p blast+ --ani-p blast+') do
    cli[:aai_p] = 'blast+'
    cli[:ani_p] = 'blast+'
  end
  opt.on('--fast', 'Alias to: --aai-p diamond --ani-p fastani (default)') do
    cli[:aai_p] = 'diamond'
    cli[:ani_p] = 'fastani'
  end
  opt.on(
    '--haai-p STRING',
    'hAAI search engine. One of: blast+, fastaai, blat, diamond, fastaai, no',
    'The default is "no" for clade projects and "fastaai" otherwise'
  ) { |v| cli[:haai_p] = v }
  opt.on(
    '--aai-p STRING',
    'AAI search engine. One of: blast+, blat, diamond (default)'
  ) { |v| cli[:aai_p] = v }
  opt.on(
    '--ani-p STRING',
    'ANI search engine. One of: blast+, blat, fastani (default)'
  ) { |v| cli[:ani_p] = v }
end

#run_daemonObject



214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/miga/cli/action/wf.rb', line 214

def run_daemon
  return if cli[:prepare_and_exit]

  cmd  = ['daemon', 'run', '-P', cli[:outdir], '--shutdown-when-done']
  cmd += ['--json', cli[:daemon_json]] if cli[:daemon_json]
  cmd += ['--max-jobs', cli[:jobs]] if cli[:jobs]
  cmd += ['--ppn', cli[:threads]] if cli[:threads]
  cmd += ['--ppn-project', cli[:threads_project]] if cli[:threads_project]
  cmd += ['--debug', MiGA::MiGA.debug_trace? ? '2' : '1'] if MiGA::MiGA.debug?
  cwd = Dir.pwd
  call_cli(cmd)
  Dir.chdir(cwd)
end

#summarize(which = %w[cds assembly essential_genes ssu]) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/miga/cli/action/wf.rb', line 182

def summarize(which = %w[cds assembly essential_genes ssu])
  return if cli[:prepare_and_exit]

  which.each do |r|
    cli.say "Summary: #{r}"
    call_cli(
      [
        'summary',
        '-P', cli[:outdir], '-r', r, '--tab', '--ref', '--active',
        '-o', File.join(cli[:outdir], "#{r}.tsv")
      ]
    )
  end
  call_cli(['browse', '-P', cli[:outdir]])
end

#transfer_metadata(obj, md) ⇒ Object



228
229
230
231
232
233
234
235
236
# File 'lib/miga/cli/action/wf.rb', line 228

def (obj, md)
  # Clear old metadata
  obj..each do |k, v|
    obj.[k] = nil if k.to_s =~ /^run_/ || obj.option?(k)
  end
  # Transfer and save
  md.each { |k, v| obj.[k] = v }
  obj.save
end