3 # Copyright (C) 2007-2011 Martin A. Hansen.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23 # This program is part of the Biopieces framework (www.biopieces.org).
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27 # Replace values to a specied key for each record in the stream.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
31 require 'maasha/biopieces'
34 casts << {:long=>'key', :short=>'k', :type=>'string', :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}
35 casts << {:long=>'search', :short=>'s', :type=>'string', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
36 casts << {:long=>'replace', :short=>'r', :type=>'string', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
37 casts << {:long=>'file', :short=>'f', :type=>'file!', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
38 casts << {:long=>'search_col', :short=>'S', :type=>'uint', :mandatory=>false, :default=>1, :allowed=>nil, :disallowed=>"0"}
39 casts << {:long=>'replace_col', :short=>'R', :type=>'uint', :mandatory=>false, :default=>2, :allowed=>nil, :disallowed=>"0"}
40 casts << {:long=>'delimiter', :short=>'d', :type=>'string', :mandatory=>false, :default=>'\s+', :allowed=>nil, :disallowed=>nil}
42 options = Biopieces.options_parse(ARGV, casts)
43 key = options[:key].to_sym
44 delimiter = Regexp.new(options[:delimiter])
46 raise ArgumentError, "both --search and --replace must be specified" if options[:search] and not options[:replace]
47 raise ArgumentError, "both --search and --replace must be specified" if options[:replace] and not options[:search]
52 replace_hash[options[:search].to_s] = options[:replace].to_s
56 File.open(options[:file], "r") do |ios|
57 ios.each_line do |line|
59 fields = line.chomp.split(delimiter)
60 k = fields[options[:search_col] - 1]
61 v = fields[options[:replace_col] - 1]
63 raise RuntimeError, "duplicate key: #{k} found in file" if replace_hash.has_key? k
71 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
72 input.each_record do |record|
73 if record.has_key? key
74 if replace_hash.has_key? record[key]
75 record[key] = replace_hash[record[key]]
83 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<