2 # = lib/evo/util/util.rb - Util class
4 # Copyright:: Copyright (C) 2006-2007 Christian M. Zmasek
5 # License:: GNU Lesser General Public License (LGPL)
7 # $Id: util.rb,v 1.17 2009/10/06 22:22:46 cmzmasek Exp $
9 # last modified: 05/15/2007
11 require 'lib/evo/util/constants'
17 def Util.normalize_seq_name( name, length )
18 if name.length > length
19 name = name[ 0, length ]
20 elsif name.length < length
21 for i in 0 ... length - name.length
28 # def Util.normalize_mol_sequence( seq )
29 # new_seq = String.new()
30 # for i in 0 ... seq.length
31 # c = seq.get_slice( i )
32 # if is_aa_gap_character?( c )
33 # new_seq = new_seq + "-"
35 # new_seq = new_seq + c
42 # Returns true if char_code corresponds to: space * - . _
43 def Util.is_aa_gap_character?( char_code )
44 return ( char_code <= 32 || char_code == 42 || char_code == 45 || char_code == 46 ||char_code == 95 )
47 # Deletes *, digits, and whitespace, replaces BJOUZ? with X, and replaces non-(letters, -) with -
48 def Util.clean_seq_str( seq_str )
49 seq_str = seq_str.upcase
50 seq_str = seq_str.gsub( /\s+/, '' )
51 seq_str = seq_str.gsub( /\d+/, '' )
52 seq_str = seq_str.gsub( '*', '' )
53 seq_str = seq_str.gsub( /[BJOUZ?]/, 'X' )
54 seq_str = seq_str.gsub( /[^A-Z\-]/, '-' )
58 # raises ArgumentError
59 def Util.check_file_for_readability( path )
60 unless ( File.exist?( path ) )
61 error_msg = "file [#{path}] does not exist"
62 raise ArgumentError, error_msg
64 unless ( File.file?( path ) )
65 error_msg = "file [#{path}] is not a regular file"
66 raise ArgumentError, error_msg
68 unless ( File.readable?( path ) )
69 error_msg = "file [#{path}] is not a readable file"
70 raise ArgumentError, error_msg
72 if ( File.zero?( path ) )
73 error_msg = "file [#{path}] is empty"
74 raise ArgumentError, error_msg
78 # raises ArgumentError
79 def Util.check_file_for_writability( path )
80 if File.directory?( path )
81 error_msg = "file [#{path}] is an existing directory"
82 raise ArgumentError, error_msg
83 elsif File.exist?( path )
84 error_msg = "file [#{path}] already exists"
85 raise ArgumentError, error_msg
86 elsif File.writable?( path )
87 error_msg = "file [#{path}] is not writeable"
88 raise ArgumentError, error_msg
92 def Util.fatal_error_if_not_writable( prg_name, path )
94 Util.check_file_for_writability( path )
95 rescue ArgumentError => e
96 Util.fatal_error( prg_name, e.to_s )
100 def Util.fatal_error_if_not_readable( prg_name, path )
102 Util.check_file_for_readability( path )
103 rescue ArgumentError => e
104 Util.fatal_error( prg_name, e.to_s )
108 def Util.get_env_variable_value( env_variable )
109 value = ENV[env_variable]
110 if value == nil || value.empty?
111 error_msg = "apparently environment variable #{env_variable} has not been set"
112 raise StandardError, error_msg
118 # raises ArgumentError
119 def Util.file2array( path, split_by_semicolon )
120 Util.check_file_for_readability( path )
123 File.open( path ) do | file |
124 while line = file.gets
125 if ( line =~ /^\s*(\S.*?)\s*$/ )
127 if ( split_by_semicolon && s =~/;/ )
129 for i in 0 ... sa.length()
130 a[ c ] = sa[ i ].strip!
142 def Util.print_program_information( prg_name,
151 if RUBY_VERSION !~ /1.9/
152 puts( "Your ruby version is #{RUBY_VERSION}, expected 1.9.x " )
156 ruby_version = RUBY_VERSION
157 l = prg_name.length + prg_version.length + date.length + ruby_version.length + 12
158 io.print( Evoruby::Constants::LINE_DELIMITER )
159 io.print( prg_name + " " + prg_version + " [" + date + "] [ruby " + ruby_version + "]")
160 io.print( Evoruby::Constants::LINE_DELIMITER )
164 io.print( Constants::LINE_DELIMITER )
165 io.print( Constants::LINE_DELIMITER )
167 io.print( Constants::LINE_DELIMITER )
168 io.print( Constants::LINE_DELIMITER )
169 io.print( "Copyright (C) " + copyright )
170 io.print( Constants::LINE_DELIMITER )
171 io.print( "Contact: " + contact )
172 io.print( Constants::LINE_DELIMITER )
173 io.print( " " + www )
174 io.print( Constants::LINE_DELIMITER )
175 io.print( Constants::LINE_DELIMITER )
178 def Util.fatal_error( prg_name, message, io = STDOUT )
179 io.print( Constants::LINE_DELIMITER )
180 if ( !Util.is_string_empty?( prg_name ) )
181 io.print( "[" + prg_name + "] > " + message )
183 io.print( " > " + message )
185 io.print( Constants::LINE_DELIMITER )
186 io.print( Constants::LINE_DELIMITER )
190 def Util.print_message( prg_name, message, io = STDOUT )
191 if ( !Util.is_string_empty?( prg_name ) )
192 io.print( "[" + prg_name + "] > " + message )
194 io.print( " > " + message )
196 io.print( Constants::LINE_DELIMITER )
199 def Util.print_warning_message( prg_name, message, io = STDOUT )
200 if ( !Util.is_string_empty?( prg_name ) )
201 io.print( "[" + prg_name + "] > WARNING: " + message )
203 io.print( " > " + message )
205 io.print( Constants::LINE_DELIMITER )
208 def Util.is_string_empty?( s )
209 return ( s == nil || s.length < 1 )
212 # From "Ruby Cookbook"
213 # counts_hash: key is a "name", value is the count (integer)
214 def Util.draw_histogram( counts_hash, char = "#" )
215 pairs = counts_hash.keys.collect { |x| [ x.to_s, counts_hash[ x ] ] }.sort
216 largest_key_size = pairs.max { |x, y| x[ 0 ].size <=> y[ 0 ].size }[ 0 ].size
217 pairs.inject( "" ) do | s, kv |
218 s << "#{ kv[ 0 ].ljust( largest_key_size ) } | #{ char*kv[ 1 ] }" + Constants::LINE_DELIMITER
222 def Util.looks_like_fasta?( path )
223 Util.check_file_for_readability( path )
224 File.open( path ) do | file |
225 while line = file.gets
226 if ( line !~ /\S/ || line =~ /^\s*#/ )
227 elsif line =~ /^\s*>\s*(.+)/
234 error_msg = "unexpected format"
235 raise IOError, error_msg