class BioDSL::MergeValues

Merge values of specified keys.

merge_values merges the values of a list of keys using a given delimiter and saves the new value as the value of the first key.

Usage

merge_values(<keys: <list>>[, delimiter: <string>])

Options

Examples

Consider the following record:

{ID: "FOO", COUNT: 10, SEQ: "gataag"}

To merge the values so that the COUNT and ID is merged in that order do:

merge_values(keys: [:COUNT, :ID])

{:ID=>"FOO", :COUNT=>"10_FOO", :SEQ=>"gataag"}

Changing the delimiter and order:

merge_values(keys: [:ID, :COUNT], delimiter: ':count=')

{:ID=>"FOO:count=10", :COUNT=>10, :SEQ=>"gataag"}

Constants

STATS

Public Class Methods

new(options) click to toggle source

Constructor for MergeValues.

@param options [Hash] Options hash. @option options [Array] :keys Keys whos values to merge. @option options [String] :delimiter Delimiter for joining.

@return [MergeValues] Class instance of MergeValues.

# File lib/BioDSL/commands/merge_values.rb, line 70
def initialize(options)
  @options = options
  check_options
  defaults

  @keys      = options[:keys]
  @delimiter = options[:delimiter]
end

Public Instance Methods

lmb() click to toggle source

Return command lambda for merge_values.

@return [Proc] Command lambda.

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

    input.each do |record|
      @status[:records_in] += 1

      if @keys.all? { |key| record.key? key }
        values = @keys.inject([]) { |a, e| a << record[e.to_sym] }
        record[@keys.first] = values.join(@delimiter)
      end

      output << record
      @status[:records_out] += 1
    end
  end
end

Private Instance Methods

check_options() click to toggle source

Check options.

# File lib/BioDSL/commands/merge_values.rb, line 103
def check_options
  options_allowed(@options, :keys, :delimiter)
  options_required(@options, :keys)
end
defaults() click to toggle source

Set default options.

# File lib/BioDSL/commands/merge_values.rb, line 109
def defaults
  @options[:delimiter] ||= '_'
end