class BioDSL::AddKey

Add a key/value pair to all records in stream.

add_key can be used to add a fixed value to a specified key to all records in the stream, or add a numeric forth running number (zero-based) with a specified prefix.

Usage

add_key(<key: <string>[, value: <string> | prefix: <string>])

Options

Examples

To add a value to all records in the stream do:

add_key(key: "FOO", value: "BAR")

To add a forth running number to all records in the stream do:

add_key(key: :ID, prefix: "")

Finally, to add a forth running number with a prefix do:

add_key(key: :ID, prefix: "ID_")

Constants

STATS

Public Class Methods

new(options) click to toggle source

Constructor for AddKey.

@param [Hash] options Options hash. @option options [Symbol] :key Key to add or replace. @option options [String] :value Value to use with :key. @option options [String] :prefix Prefix to use with :key.

@return [Proc] Returns class instance.

# File lib/BioDSL/commands/add_key.rb, line 69
def initialize(options)
  @options = options

  check_options
end

Public Instance Methods

lmb() click to toggle source

Add a key or replace a key for all records with a specified value or a forthrunning number with a prefix.

@param [Hash] options Options hash. @option options [Symbol] :key Key to add or replace. @option options [String] :value Value to use with :key. @option options [String] :prefix Prefix to use with :key.

@return [Proc] Returns the command lambda.

# File lib/BioDSL/commands/add_key.rb, line 84
def lmb
  lambda do |input, output, status|
    status_init(status, STATS)

    input.each_with_index do |record, i|
      @status[:records_in] += 1

      record[@options[:key].to_sym] = @options[:value] ||
                                      "#{@options[:prefix]}#{i}"

      output << record

      @status[:records_out] += 1
    end
  end
end

Private Instance Methods

check_options() click to toggle source

Check all options.

# File lib/BioDSL/commands/add_key.rb, line 104
def check_options
  options_allowed(@options, :key, :value, :prefix)
  options_required(@options, :key)
  options_required_unique(@options, :value, :prefix)
end