Class: Assemblotron::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/assemblotron.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeController

Return a new Assemblotron



20
21
22
23
24
25
26
27
# File 'lib/assemblotron.rb', line 20

def initialize
  @log = Logger.new(STDOUT)
  @log.level = Logger::INFO
  self.load_config
  self.init_settings
  @assemblers = []
  self.load_assemblers
end

Instance Attribute Details

#assembler_optsObject

Returns the value of attribute assembler_opts.



17
18
19
# File 'lib/assemblotron.rb', line 17

def assembler_opts
  @assembler_opts
end

#global_optsObject

Returns the value of attribute global_opts.



16
17
18
# File 'lib/assemblotron.rb', line 16

def global_opts
  @global_opts
end

Class Method Details

.class_from_type(type) ⇒ Object



144
145
146
147
148
149
150
151
152
153
# File 'lib/assemblotron.rb', line 144

def self.class_from_type type
  case type
  when 'string'
    String
  when 'int'
    Integer
  when 'float'
    Float
  end
end

.headerObject

initialize



29
30
31
# File 'lib/assemblotron.rb', line 29

def self.header
  "Assemblotron v#{VERSION::STRING.dup}"
end

Instance Method Details

#assemblersObject

Return an array of the names of available assemblers



84
85
86
87
88
89
90
91
# File 'lib/assemblotron.rb', line 84

def assemblers
  a = []
  @assemblers.each do |t|
    a << t.name
    a << t.shortname if t.shortname
  end
  a
end

#final_assembly(assembler, result) ⇒ Object



167
168
169
170
171
172
# File 'lib/assemblotron.rb', line 167

def final_assembly assembler, result
  Dir.mkdir('final_assembly')
  Dir.chdir('final_assembly') do
    assembler.run result
  end
end

#get_assembler(assembler) ⇒ Object

options_for_assembler



135
136
137
138
139
140
141
142
# File 'lib/assemblotron.rb', line 135

def get_assembler assembler
  ret = @assemblers.find do |a|
    a.name == assembler || 
    a.shortname == assembler
  end
  raise "couldn't find assembler #{assembler}" if ret.nil?
  ret
end

#init_settingsObject

Initialise the Biopsy settings



34
35
36
37
38
39
40
41
# File 'lib/assemblotron.rb', line 34

def init_settings
  s = Biopsy::Settings.instance
  s.set_defaults
  libdir = File.dirname(__FILE__)
  s.target_dir = [File.join(libdir, 'assemblotron/assemblers/')]
  s.objectives_dir = [File.join(libdir, 'assemblotron/objectives/')]
  @log.debug "initialised Biopsy settings"
end

#list_assemblersObject

assemblers



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/assemblotron.rb', line 93

def list_assemblers
  puts Controller.header
  puts "\nAvailable assemblers are listed below.\nShortnames are shown in brackets if available.\n\nUsage:\natron [global options] <assembler> [assembler options]\n\nAssemblers:\n"
  @assemblers.each do |a| 
    p = " - #{a.name}"
    p += " (#{a.shortname})" if a.respond_to? :shortname
    puts p
  end
end

#load_assemblersObject

Discover and load available assemblers.

Loads all assemblers provided by the program, and then searches any directories listed in the config file (+~/.assemblotron+) setting assembler_dirs.

Directories listed in assembler_dirs must contain:

definitions

Directory with one .yml definition per assembler. See the documentation for Definition.

constructors

Directory with one .rb file per assembler. See the documentation for Constructor.



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

def load_assemblers
  Biopsy::Settings.instance.target_dir.each do |dir|
    Dir.chdir dir do
      Dir['*.yml'].each do |file|
        name = File.basename(file, '.yml')
        target = Biopsy::Target.new
        target.load_by_name name
        @assemblers << target
      end
    end
  end
end

#load_configObject

Load global configuration from the config file at ~/.assemblotron, if it exists.



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/assemblotron.rb', line 45

def load_config
  config_file = File.join(Dir.home, ".assemblotron")
  if File.exists? config_file
    @log.debug "config file found at #{config_file}"
    config = YAML::load_file(config_file)
    if config.nil?
      @log.warn 'config file malformed or empty'
      return
    end
    @config = config.deep_symbolize
  end
end

#options_for_assembler(assembler) ⇒ Object

list_assemblers



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/assemblotron.rb', line 112

def options_for_assembler assembler
  a = self.get_assembler assembler
  parser = Trollop::Parser.new do
      banner "\#{Controller.header}\n\nOptions for assembler \#{assembler}\n"
    opt :reference, "Path to reference proteome file in FASTA format",
         :type => String,
         :required => true
    a.options.each_pair do |param, opts|
      opt param, 
          opts[:desc], 
          :type => Controller.class_from_type(opts[:type])
    end
  end
  Trollop::with_standard_exception_handling parser do
    raise Trollop::HelpNeeded if ARGV.empty? # show help screen
    parser.parse ARGV
  end
end

#run(assembler) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/assemblotron.rb', line 174

def run assembler
  # subsampling
  if @global_opts[:skip_subsample]
    @assembler_opts[:left_subset] = assembler_opts[:left]
    @assembler_opts[:right_subset] = assembler_opts[:right]
  else
    subsample_input
  end

  # load reference and create ublast DB
  @assembler_opts[:reference] = Transrate::Assembly.new(@assembler_opts[:reference])
  ra = Transrate::ReciprocalAnnotation.new(@assembler_opts[:reference], @assembler_opts[:reference])
  ra.make_reference_db

  # setup the assembler
  a = self.get_assembler assembler
  a.setup_optim(@global_opts, @assembler_opts)

  # run the optimisation
  e = Biopsy::Experiment.new(a, options: @assembler_opts, threads: @global_opts[:threads])
  res = e.run

  # write out the result
  File.open(@global_opts[:output_parameters], 'wb') do |f|
    f.write(JSON.pretty_generate(res))
  end

  # run the final assembly
  a.setup_final(@global_opts, @assembler_opts)
  unless @global_opts[:skip_final]
    final_assembly a, res
  end
end

#subsample_inputObject



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/assemblotron.rb', line 155

def subsample_input
  l = @assembler_opts[:left]
  r = @assembler_opts[:right]
  size = @global_opts[:subsample_size]

  s = Sample.new(l, r)
  ls, rs = s.subsample size
 
  @assembler_opts[:left_subset] = ls
  @assembler_opts[:right_subset] = rs
end