Class: BioDSL::SplitValues
- Inherits:
-
Object
- Object
- BioDSL::SplitValues
- Defined in:
- lib/BioDSL/commands/split_values.rb
Overview
Split the values of a key into new key/value pairs.
split_values
splits the value of a given key into multiple values that are added to the record. The keys used for the values are per default based on the given key with an added index, but using the keys
option allows specifying a list of keys to use instead.
Usage
split_values(<key>: <string>>[, delimiter: <string>[, keys: <list>]])
Options
# key: <string> - Key who's value to split.
-
keys: <list> - List of keys to use with split values.
-
delimiter: <string> - Delimiter (default='_').
Examples
Consider the following records:
{ID: "FOO:count=10", SEQ: "gataag"}
{ID: "FOO_10_20", SEQ: "gataag"}
To split the value belinging to ID do:
split_values(key: :ID)
{:ID=>"FOO:count=10", :SEQ=>"gataag"}
{:ID=>"FOO_10_20", :SEQ=>"gataag", :ID_0=>"FOO", :ID_1=>10, :ID_2=>20}
Using a different delimiter:
split_values(key: "ID", delimiter: ':count=')
{:ID=>"FOO:count=10", :SEQ=>"gataag", :ID_0=>"FOO", :ID_1=>10}
{:ID=>"FOO_10_20", :SEQ=>"gataag"}
Using a different delimiter and a list of keys:
split_values(key: "ID", keys: ["ID", :COUNT], delimiter: ':count=')
{:ID=>"FOO", :SEQ=>"gataag", :COUNT=>10}
{:ID=>"FOO_10_20", :SEQ=>"gataag"}
Constant Summary collapse
- STATS =
%i(records_in records_out)
Instance Method Summary collapse
-
#initialize(options) ⇒ SplitValues
constructor
Constructor for SplitValues.
-
#lmb ⇒ Proc
Return command lambda for split_values.
Constructor Details
#initialize(options) ⇒ SplitValues
Constructor for SplitValues.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/BioDSL/commands/split_values.rb', line 84 def initialize() @options = @first = true @convert = [] @keys = @options[:keys] @key = @options[:key].to_sym @delimiter = @options[:delimiter] || '_' end |
Instance Method Details
#lmb ⇒ Proc
Return command lambda for split_values.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/BioDSL/commands/split_values.rb', line 99 def lmb lambda do |input, output, status| status_init(status, STATS) input.each do |record| @status[:records_in] += 1 if (value = record[@key]) values = value.split(@delimiter) if values.size > 1 determine_types(values) if @first split_values(values, record) end end output << record @status[:records_out] += 1 end end end |