Class: Basil::Basil

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(barcodes) ⇒ Basil

Gets Basil ready, must specify a barcodes hash with Basil.new barcodes

Barcodes hash has regexp for values. If not, strings are converted to regexp Barcode names are keys, used to generate filenames.



11
12
13
14
15
16
17
# File 'lib/basil/basil.rb', line 11

def initialize(barcodes)
  @barcodes = Hash.new
  barcodes.each_pair do |k, v|
    @barcodes[k] = Regexp.new "^#{v}", true
  end
  @barcodes
end

Class Method Details

.parse_barcodes(handle, args = {}) ⇒ Object

Parses a barcodes file

barcodes are specified in CSV file (unless specified otherwise with :sep => ”) barcode_name,sequence the barcode_name will be used to generate the filename



52
53
54
55
56
57
58
# File 'lib/basil/basil.rb', line 52

def self.parse_barcodes(handle, args={})
  sep = args[:sep] || ","
  barcodes = handle.each.collect do |line|
    name, barcode = line.strip.split(sep)
  end
  Hash[*barcodes.flatten]
end

Instance Method Details

#recognize(string) ⇒ Object

Finds the barcode (if present)

If the barcode is present, returns ‘name’, and the sequence with the barcode removed If not, returns nil



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/basil/basil.rb', line 26

def recognize(string)
  matches = @barcodes.each_pair.collect do |k, v|
    k if string[v]
  end

  matches.compact!

  if matches.length > 1
    raise Exception, "sequence #{string} has more than one match"
  elsif matches.length == 0
    nil
  else
    barcode = matches.first
    sequence = string.gsub(@barcodes[barcode], '')
    [barcode, sequence]
  end
end