2 # = lib/evo/util/util.rb - Util class
4 # Copyright:: Copyright (C) 2017 Christian M. Zmasek
5 # License:: GNU Lesser General Public License (LGPL)
7 # Last modified: 2017/02/07
9 require 'lib/evo/util/constants'
13 def Util.get_matching_files( files, prefix_pattern, suffix_pattern )
14 matching_files = Array.new
16 if ( !File.directory?( file ) &&
18 file =~ /^#{prefix_pattern}.*#{suffix_pattern}$/ )
19 matching_files << file
25 def Util.normalize_seq_name( name, length, exception_if_too_long = false )
26 if name.length > length
27 if exception_if_too_long
28 error_msg = "sequence name \"#{name}\" is too long (>#{length})"
29 raise StandardError, error_msg
31 name = name[ 0, length ]
32 elsif name.length < length
33 t = length - name.length
41 # Returns true if char_code corresponds to: space * - . _
42 def Util.is_aa_gap_character?( char_code )
43 return ( char_code <= 32 || char_code == 42 || char_code == 45 || char_code == 46 ||char_code == 95 )
46 # Deletes *, digits, and whitespace, replaces BJOUZ? with X, and replaces non-(letters, -) with -
47 def Util.clean_seq_str( seq_str )
48 seq_str = seq_str.upcase
49 seq_str = seq_str.gsub( /\s+/, '' )
50 seq_str = seq_str.gsub( /\d+/, '' )
51 seq_str = seq_str.gsub( '*', '' )
52 seq_str = seq_str.gsub( /[BJOUZ?]/, 'X' )
53 seq_str = seq_str.gsub( /[^A-Z\-]/, '-' )
57 # raises ArgumentError
58 def Util.check_file_for_readability( path )
59 unless ( File.exist?( path ) )
60 error_msg = "file [#{path}] does not exist"
61 raise IOError, error_msg
63 unless ( File.file?( path ) )
64 error_msg = "file [#{path}] is not a regular file"
65 raise IOError, error_msg
67 unless ( File.readable?( path ) )
68 error_msg = "file [#{path}] is not a readable file"
69 raise IOError, error_msg
71 if ( File.zero?( path ) )
72 error_msg = "file [#{path}] is empty"
73 raise IOError, error_msg
77 # raises ArgumentError
78 def Util.check_file_for_writability( path )
79 if File.directory?( path )
80 error_msg = "file [#{path}] is an existing directory"
81 raise IOError, error_msg
82 elsif File.exist?( path )
83 error_msg = "file [#{path}] already exists"
84 raise IOError, error_msg
85 elsif File.writable?( path )
86 error_msg = "file [#{path}] is not writeable"
87 raise IOError, error_msg
91 def Util.fatal_error_if_not_writable( prg_name, path )
93 Util.check_file_for_writability( path )
95 Util.fatal_error( prg_name, e.to_s )
99 def Util.fatal_error_if_not_readable( prg_name, path )
101 Util.check_file_for_readability( path )
103 Util.fatal_error( prg_name, e.to_s )
107 def Util.get_env_variable_value( env_variable )
108 value = ENV[env_variable]
109 if value == nil || value.empty?
110 error_msg = "apparently environment variable #{env_variable} has not been set"
111 raise StandardError, error_msg
116 # raises ArgumentError
117 def Util.file2array( path, split_by_semicolon )
118 Util.check_file_for_readability( path )
121 File.open( path ) do | file |
122 while line = file.gets
123 if ( line =~ /^\s*(\S.*?)\s*$/ )
125 if ( split_by_semicolon && s =~/;/ )
127 for i in 0 ... sa.length()
128 a[ c ] = sa[ i ].strip!
140 def Util.print_program_information( prg_name,
147 ruby_version = RUBY_VERSION
148 l = prg_name.length + prg_version.length + date.length + ruby_version.length + 12
149 io.print( Evoruby::Constants::LINE_DELIMITER )
150 io.print( prg_name + " " + prg_version + " [" + date + "] [ruby " + ruby_version + "]")
151 io.print( Evoruby::Constants::LINE_DELIMITER )
155 io.print( Constants::LINE_DELIMITER )
156 io.print( Constants::LINE_DELIMITER )
158 io.print( Constants::LINE_DELIMITER )
159 io.print( Constants::LINE_DELIMITER )
160 io.print( "Website: " + www )
161 io.print( Constants::LINE_DELIMITER )
162 io.print( Constants::LINE_DELIMITER )
165 def Util.fatal_error( prg_name, message, io = STDOUT )
166 io.print( Constants::LINE_DELIMITER )
167 if ( !Util.is_string_empty?( prg_name ) )
168 io.print( "[" + prg_name + "] > " + message )
170 io.print( " > " + message )
172 io.print( Constants::LINE_DELIMITER )
173 io.print( Constants::LINE_DELIMITER )
177 def Util.print_message( prg_name, message, io = STDOUT )
178 if ( !Util.is_string_empty?( prg_name ) )
179 io.print( "[" + prg_name + "] > " + message )
181 io.print( " > " + message )
183 io.print( Constants::LINE_DELIMITER )
186 def Util.print_warning_message( prg_name, message, io = STDOUT )
187 if ( !Util.is_string_empty?( prg_name ) )
188 io.print( "[" + prg_name + "] > WARNING: " + message )
190 io.print( " > " + message )
192 io.print( Constants::LINE_DELIMITER )
195 def Util.is_string_empty?( s )
196 return ( s == nil || s.length < 1 )
199 # From "Ruby Cookbook"
200 # counts_hash: key is a "name", value is the count (integer)
201 def Util.draw_histogram( counts_hash, char = "#" )
202 pairs = counts_hash.keys.collect { |x| [ x.to_s, counts_hash[ x ] ] }.sort
203 largest_key_size = pairs.max { |x, y| x[ 0 ].size <=> y[ 0 ].size }[ 0 ].size
204 pairs.inject( "" ) do | s, kv |
205 s << "#{ kv[ 0 ].ljust( largest_key_size ) } | #{ char*kv[ 1 ] }" + Constants::LINE_DELIMITER
209 def Util.looks_like_fasta?( path )
210 Util.check_file_for_readability( path )
211 File.open( path ) do | file |
212 while line = file.gets
213 if ( line !~ /\S/ || line =~ /^\s*#/ )
214 elsif line =~ /^\s*>\s*(.+)/
221 error_msg = "unexpected format"
222 raise IOError, error_msg