Method: Basil::CLI.run!

Defined in:
lib/basil/cli.rb

.run!Object



5
6
7
8
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
# File 'lib/basil/cli.rb', line 5

def run!

  opts           = parse_arguments
  barcodes       = parse_barcodes opts[:barcodes]
  out_dir        = opts[:out]
  buffer         = Buffer.new
  basil          = Basil.new(barcodes)
  handles        = Array.new

  if opts[:illumina]
    if pretrim
      raise "pretrim is not supported with paired-end data"
    end
  else
    reads_handle   = File.open(opts[:reads])
    handles       << reads_handle
    records_format = records.format
    pretrim        = opts[:pretrim] || 0
    afttrim        = -1*opts[:afttrim] || -1
  end

  if File.exist? out_dir
    $stderr.puts "#{out_dir} already exists -- delete or move!"
    exit
  else
    Dir.mkdir(out_dir)
  end

  records.each do |record|
    
    match = basil.recognize record.sequence
    barcode, sequence = match if match

    unless match
      barcode = 'unknown'
      trimmed_sequence = record.sequence
    end
    
    trimmed_sequence = record.sequence[pretrim..afttrim]

    new_record = Fasta.new :name => record.name, :sequence => trimmed_sequence
    buffer.add_to File.join(out_dir, barcode + ".#{records_format}"), new_record
  end

  # Finish up
  buffer.finalize
  handles.collect { |x| x.close }
end