class Basil::Basil

Public Class Methods

new(barcodes) click to toggle source

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.

# 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
parse_barcodes(handle, args={}) click to toggle source

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

# 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

Public Instance Methods

recognize(string) click to toggle source

Finds the barcode (if present)

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

# 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