1 #!/usr/local/bin/ruby -w
5 # Copyright:: Copyright (C) 2006-2008 Christian M. Zmasek
6 # License:: GNU Lesser General Public License (LGPL)
8 # $Id: replace_id.rb,v 1.8 2008/08/28 17:09:07 cmzmasek Exp $
10 # To replace ()by way of example '123_CHI5' with '123_CHICK5'
11 # given a mapping file containing '123_CHICKEN'
12 # (in the form '123_CHICKEN: some description which is ignored').
14 # Note. This will break if the species id ends with a number (as is
15 # in the case for many bacteria).
18 module ForesterScripts
20 if RUBY_VERSION !~ /1.9/
21 puts( "Your ruby version is #{RUBY_VERSION}, expected 1.9.x " )
27 if ( ARGV == nil || ARGV.length != 3 )
28 puts( "usage: replace_id.rb <map-file> <infile> <outfile>" )
36 if ( File.exists?( outfile ) )
37 puts( "outfile [" + outfile + "] already exists" )
40 if ( !File.exists?( infile) )
41 puts( "infile [" + infile + "] does not exist" )
44 if ( !File.exists?( mapfile ) )
45 puts( "mapfile [" + mapfile + "] does not exist" )
49 number_to_complete_id_map = Hash.new
51 File.open( mapfile ) do | file |
52 while line = file.gets
53 if ( line =~ /(\d+_\S+)\s*:/ )
55 complete_id =~ /(\d+)_\S+/
56 number_to_complete_id_map[ $1 ] = complete_id
57 puts( $1 + ' => ' + complete_id )
62 if ( number_to_complete_id_map.size < 1 )
63 puts( "mapping file was empty" )
69 File.open( infile ) do | file |
70 while line = file.gets
71 data_str = data_str + line.chomp
76 number_to_complete_id_map.each_pair{ |number, id|
77 data_str.gsub!( /\b#{number}_[A-Z]{#{NUMBER_OF_LETTERS}}/, id )
80 open( outfile, 'w' ) do |file|
81 file.write( data_str )
84 puts( "wrote " + outfile )