]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/replace_vals
refactoring of ruby code s/has_key?/[]/
[biopieces.git] / bp_bin / replace_vals
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2007-2011 Martin A. Hansen.
4
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.
9
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.
14
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.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
22
23 # This program is part of the Biopieces framework (www.biopieces.org).
24
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26
27 # Replace values to a specied key for each record in the stream.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31 require 'maasha/biopieces'
32
33 casts = []
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}
41
42 options   = Biopieces.options_parse(ARGV, casts)
43 key       = options[:key].to_sym
44 delimiter = Regexp.new(options[:delimiter])
45
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]
48
49 replace_hash = {}
50
51 if options[:search]
52   replace_hash[options[:search].to_s] = options[:replace].to_s
53 end
54
55 if options[:file]
56   File.open(options[:file], "r") do |ios|
57     ios.each_line do |line|
58       unless line[0] == '#'
59         fields = line.chomp.split(delimiter)
60         k      = fields[options[:search_col] - 1]
61         v      = fields[options[:replace_col] - 1]
62
63         raise RuntimeError, "duplicate key: #{k} found in file" if replace_hash[k]
64
65         replace_hash[k] = v
66       end
67     end
68   end
69 end
70
71 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
72   input.each_record do |record|
73     if record[key]
74       if replace_hash[record[key]]
75         record[key] = replace_hash[record[key]]
76       end
77     end
78
79     output.puts record
80   end
81 end
82
83 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
84
85
86 __END__