Class: Basil::Basil
- Inherits:
-
Object
- Object
- Basil::Basil
- Defined in:
- lib/basil/basil.rb
Class Method Summary collapse
-
.parse_barcodes(handle, args = {}) ⇒ Object
Parses a barcodes file.
Instance Method Summary collapse
-
#initialize(barcodes) ⇒ Basil
constructor
Gets Basil ready, must specify a barcodes hash with Basil.new barcodes.
-
#recognize(string) ⇒ Object
Finds the barcode (if present).
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 = Hash.new .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.(handle, args={}) sep = args[:sep] || "," = handle.each.collect do |line| name, = line.strip.split(sep) end Hash[*.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 = matches.first sequence = string.gsub(@barcodes[], '') [, sequence] end end |