Class: MiGA::Cli::Action::Get

Inherits:
MiGA::Cli::Action show all
Defined in:
lib/miga/cli/action/get.rb

Constant Summary

Constants included from MiGA

MiGA::CITATION, VERSION, VERSION_DATE, VERSION_NAME

Instance Attribute Summary

Attributes inherited from MiGA::Cli::Action

#cli

Instance Method Summary collapse

Methods inherited from MiGA::Cli::Action

#complete, #empty_action, #initialize, #launch, load, #name

Methods inherited from MiGA

CITATION, DEBUG, DEBUG_OFF, DEBUG_ON, DEBUG_TRACE_OFF, DEBUG_TRACE_ON, FULL_VERSION, LONG_VERSION, VERSION, VERSION_DATE, initialized?, #result_files_exist?

Methods included from MiGA::Common::Path

#root_path, #script_path

Methods included from MiGA::Common::Format

#clean_fasta_file, #seqs_length, #tabulate

Constructor Details

This class inherits a constructor from MiGA::Cli::Action

Instance Method Details

#parse_cliObject



9
10
11
12
13
14
15
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
# File 'lib/miga/cli/action/get.rb', line 9

def parse_cli
  cli.defaults = {query: false, universe: :ncbi, db: :nuccore,
    get_md: false, only_md: false}
  cli.parse do |opt|
    cli.opt_object(opt, [:project, :dataset, :dataset_type])
    opt.on(
      '-I', '--ids ID1,ID2,...', Array,
      '(Mandatory unless -F) IDs in the remote database separated by commas'
      ){ |v| cli[:ids] = v }
    opt.on(
      '-U', '--universe STRING',
      "Universe of the remote database. By default: #{cli[:universe]}"
      ){ |v| cli[:universe] = v.to_sym }
    opt.on(
      '--db STRING',
      "Name of the remote database. By default: #{cli[:db]}"
      ){ |v| cli[:db] = v.to_sym }
    opt.on(
      '-F', '--file PATH',
      'Tab-delimited file (with header) listing the datasets to download',
      'The long form of most options are supported as header (without --)',
      'including: dataset, ids, universe, db, metadata',
      'For flags without value (like query) use true/false',
      'Unsupported values are: project, file, verbose, help, and debug'
      ){ |v| cli[:file] = v }
    opt.on(
      '-q', '--query',
      'Register the dataset as a query, not a reference dataset'
      ){ |v| cli[:query] = v }
    opt.on('--ignore-dup',
      'Ignore datasets that already exist'
      ){ |v| cli[:ignore_dup] = v }
    opt.on(
      '-d', '--description STRING',
      'Description of the dataset'
      ){ |v| cli[:description] = v }
    opt.on(
      '-c', '--comments STRING',
      'Comments on the dataset'
      ){ |v| cli[:comments] = v }
    opt.on(
      '-m', '--metadata STRING',
      'Metadata as key-value pairs separated by = and delimited by comma',
      'Values are saved as strings except for booleans (true / false) or nil'
      ){ |v| cli[:metadata] = v }
    opt.on(
      '--get-metadata',
      'Only download and update metadata for existing datasets'
      ){ |v| cli[:get_md] = v }
    opt.on(
      '--only-metadata',
      'Create datasets without input data but retrieve all metadata'
      ){ |v| cli[:only_md] = v }
    opt.on(
      '--api-key STRING',
      'API key for the given universe'
      ){ |v| cli[:api_key] = v }
  end
end

#performObject



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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/miga/cli/action/get.rb', line 69

def perform
  glob = [cli]
  unless cli[:file].nil?
    glob = []
    File.open(cli[:file], 'r') do |fh|
      h = nil
      fh.each do |ln|
        r = ln.chomp.split(/\t/)
        if h.nil?
           h = r
        else
          argv_i = [self.name]
          h.each_with_index do |field, k|
            case field.downcase
            when *%w[query ignore-dup get-metadata only-metadata]
              argv_i << "--#{field.downcase}" if r[k].downcase == 'true'
            when *%w[project file verbose help debug]
              raise "Unsupported header: #{field}"
            else
              argv_i += ["--#{field.downcase}", r[k]]
            end
          end
          sub_cli = MiGA::Cli.new(argv_i)
          sub_cli.defaults = cli.data
          sub_cli.action.parse_cli
          glob << sub_cli
        end
      end
    end
  end

  p = cli.load_project
  glob.each do |sub_cli|
    sub_cli.ensure_par(dataset: '-D', ids: '-I')
    unless sub_cli[:api_key].nil?
      ENV["#{sub_cli[:universe].to_s.upcase}_API_KEY"] = sub_cli[:api_key]
    end

    sub_cli.say "Dataset: #{sub_cli[:dataset]}"
    if sub_cli[:ignore_dup] && !sub_cli[:get_md]
      next if Dataset.exist?(p, sub_cli[:dataset])
    end

    sub_cli.say 'Locating remote dataset'
    rd = RemoteDataset.new(sub_cli[:ids], sub_cli[:db], sub_cli[:universe])

    if sub_cli[:get_md]
      sub_cli.say 'Updating dataset'
      d = p.dataset(sub_cli[:dataset])
      next if d.nil?
      md = sub_cli.(d)..data
      rd.(d, md)
    else
      sub_cli.say 'Creating dataset'
      dummy_d = Dataset.new(p, sub_cli[:dataset])
      md = sub_cli.(dummy_d)..data
      md[:metadata_only] = true if cli[:only_md]
      dummy_d.remove!
      rd.save_to(p, sub_cli[:dataset], !sub_cli[:query], md)
      p.add_dataset(sub_cli[:dataset])
    end
  end
end