1 #!/usr/local/bin/ruby -w
3 # = lib/evo/apps/phylogenies_decorator
5 # Copyright:: Copyright (C) 2006-2008 Christian M. Zmasek
6 # License:: GNU Lesser General Public License (LGPL)
8 # decoration of phylogenies with sequence/species names and domain architectures
10 # $Id: phylogenies_decorator.rb,v 1.34 2010/12/13 19:00:11 cmzmasek Exp $
12 # Environment variable FORESTER_HOME needs to point to the appropriate
13 # directory (e.g. setenv FORESTER_HOME $HOME/SOFTWARE_DEV/ECLIPSE_WORKSPACE/forester/)
15 require 'lib/evo/util/constants'
16 require 'lib/evo/util/util'
17 require 'lib/evo/util/command_line_arguments'
23 class PhylogeniesDecorator
25 #DECORATOR_OPTIONS_SEQ_NAMES = '-r=1 -mdn'
26 #DECORATOR_OPTIONS_SEQ_NAMES = '-p -t -sn'
27 DECORATOR_OPTIONS_SEQ_NAMES = '-p -t -c -tc'
28 # -mdn is a hidden expert option to rename e.g. "6_ORYLA3" to "6_[3]_ORYLA"
29 #DECORATOR_OPTIONS_SEQ_NAMES = '-sn -r=1'
30 #DECORATOR_OPTIONS_DOMAINS = '-r=1'
31 DECORATOR_OPTIONS_DOMAINS = '-p -t'
32 IDS_MAPFILE_SUFFIX = '.nim'
33 DOMAINS_MAPFILE_SUFFIX = '.dff'
37 LOG_FILE = '00_phylogenies_decorator.log'
38 FORESTER_HOME = ENV[Constants::FORESTER_HOME_ENV_VARIABLE]
39 JAVA_HOME = ENV[Constants::JAVA_HOME_ENV_VARIABLE]
41 PRG_NAME = "phylogenies_decorator"
42 PRG_DATE = "2012.10.11"
43 PRG_DESC = "decoration of phylogenies with sequence/species names and domain architectures"
45 COPYRIGHT = "2012 Christian M Zmasek"
46 CONTACT = "phylosoft@gmail.com"
47 WWW = "https://sites.google.com/site/cmzmasek/home/software/forester"
50 DOMAINS_ONLY_OPTION = "d"
51 HELP_OPTION_1 = "help"
54 NL = Constants::LINE_DELIMITER
58 Util.print_program_information( PRG_NAME,
67 if ( ARGV == nil || ARGV.length > 3 || ARGV.length < 2 )
72 if FORESTER_HOME == nil || FORESTER_HOME.length < 1
73 Util.fatal_error( PRG_NAME, "apparently environment variable #{Constants::FORESTER_HOME_ENV_VARIABLE} has not been set" )
75 if JAVA_HOME == nil || JAVA_HOME.length < 1
76 Util.fatal_error( PRG_NAME, "apparently environment variable #{Constants::JAVA_HOME_ENV_VARIABLE} has not been set" )
79 if !File.exist?( FORESTER_HOME )
80 Util.fatal_error( PRG_NAME, '[' + FORESTER_HOME + '] does not exist' )
82 if !File.exist?( JAVA_HOME )
83 Util.fatal_error( PRG_NAME, '[' + JAVA_HOME + '] does not exist' )
86 decorator = JAVA_HOME + '/bin/java -cp ' + FORESTER_HOME + '/java/forester.jar org.forester.application.decorator'
89 cla = CommandLineArguments.new( ARGV )
90 rescue ArgumentError => e
91 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
94 if ( cla.is_option_set?( HELP_OPTION_1 ) ||
95 cla.is_option_set?( HELP_OPTION_2 ) )
100 if File.exist?( LOG_FILE )
101 Util.fatal_error( PRG_NAME, 'logfile [' + LOG_FILE + '] already exists' )
104 allowed_opts = Array.new
105 allowed_opts.push( IDS_ONLY_OPTION )
106 allowed_opts.push( DOMAINS_ONLY_OPTION )
108 disallowed = cla.validate_allowed_options_as_str( allowed_opts )
109 if ( disallowed.length > 0 )
110 Util.fatal_error( PRG_NAME, "unknown option(s): " + disallowed )
116 in_suffix = cla.get_file_name( 0 )
117 out_suffix = cla.get_file_name( 1 )
119 if cla.is_option_set?( IDS_ONLY_OPTION )
122 if cla.is_option_set?( DOMAINS_ONLY_OPTION )
126 if ( ids_only && domains_only )
127 Util.fatal_error( PRG_NAME, 'attempt to use ids only and domains only at the same time' )
133 log << "Program : " + PRG_NAME + NL
134 log << "Version : " + PRG_VERSION + NL
135 log << "Program date : " + PRG_DATE + NL
136 log << "Options for seq names: " + DECORATOR_OPTIONS_SEQ_NAMES + NL
137 log << "Options for domains : " + DECORATOR_OPTIONS_DOMAINS + NL
138 log << "FORESTER_HOME : " + FORESTER_HOME + NL
139 log << "JAVA_HOME : " + JAVA_HOME + NL + NL
140 log << "Date/time: " + now.to_s + NL
141 log << "Directory: " + Dir.getwd + NL + NL
143 Util.print_message( PRG_NAME, 'input suffix : ' + in_suffix )
144 Util.print_message( PRG_NAME, 'output suffix : ' + out_suffix )
146 log << 'input suffix : ' + in_suffix + NL
147 log << 'output suffix : ' + out_suffix + NL
149 if ( File.exists?( TMP_FILE ) )
150 File.delete( TMP_FILE )
153 files = Dir.entries( "." )
157 files.each { | phylogeny_file |
158 if ( !File.directory?( phylogeny_file ) &&
159 phylogeny_file !~ /^\./ &&
160 phylogeny_file !~ /^00/ &&
161 phylogeny_file !~ /#{out_suffix}$/ &&
162 phylogeny_file =~ /#{in_suffix}$/ )
164 Util.check_file_for_readability( phylogeny_file )
166 Util.fatal_error( PRG_NAME, 'can not read from: ' + phylogeny_file + ': '+ $! )
171 outfile = phylogeny_file.sub( /#{in_suffix}$/, out_suffix )
174 outfile = outfile.sub( /_ni_/, '_' )
177 if File.exists?( outfile )
178 msg = counter.to_s + ': ' + phylogeny_file + ' -> ' + outfile +
179 ' : already exists, skipping'
180 Util.print_message( PRG_NAME, msg )
185 Util.print_message( PRG_NAME, counter.to_s + ': ' + phylogeny_file + ' -> ' + outfile )
186 log << counter.to_s + ': ' + phylogeny_file + ' -> ' + outfile + NL
188 phylogeny_id = get_id( phylogeny_file )
190 ids_mapfile_name = nil
191 domains_mapfile_name = nil
194 ids_mapfile_name = get_file( files, phylogeny_id, IDS_MAPFILE_SUFFIX )
196 domains_mapfile_name = get_file( files, phylogeny_id, DOMAINS_MAPFILE_SUFFIX )
198 ids_mapfile_name = get_file( files, phylogeny_id, IDS_MAPFILE_SUFFIX )
199 domains_mapfile_name = get_file( files, phylogeny_id, DOMAINS_MAPFILE_SUFFIX )
202 if domains_mapfile_name != nil
204 Util.check_file_for_readability( domains_mapfile_name )
206 Util.fatal_error( PRG_NAME, 'failed to read from [#{domains_mapfile_name}]: ' + $! )
210 if ids_mapfile_name != nil
212 Util.check_file_for_readability( ids_mapfile_name )
214 Util.fatal_error( PRG_NAME, 'failed to read from [#{ids_mapfile_name}]: ' + $! )
218 if domains_mapfile_name != nil
219 if ids_mapfile_name != nil
220 my_outfile = TMP_FILE
224 cmd = decorator + ' ' + DECORATOR_OPTIONS_DOMAINS + ' ' +
225 '-f=d ' + phylogeny_file + ' ' +
226 domains_mapfile_name + ' ' + my_outfile
228 execute_cmd( cmd, log )
231 if ids_mapfile_name != nil
232 if domains_mapfile_name != nil
235 my_infile = phylogeny_file
237 cmd = decorator + ' ' + DECORATOR_OPTIONS_SEQ_NAMES + ' ' +
238 '-f=n ' + my_infile + ' ' +
239 ids_mapfile_name + ' ' + outfile
241 execute_cmd( cmd, log )
244 if ( File.exists?( TMP_FILE ) )
245 File.delete( TMP_FILE )
249 open( LOG_FILE, 'w' ) do | f |
253 Util.print_message( PRG_NAME, 'OK' )
257 def execute_cmd( cmd, log )
258 log << 'excuting ' + cmd + NL
259 IO.popen( cmd , 'r+' ) do | pipe |
261 log << pipe.read + NL + NL
267 def get_id( phylogeny_file_name )
268 phylogeny_file_name =~ /^([^_]+)/
272 def get_file( files_in_dir, phylogeny_id, suffix_pattern )
273 matching_files = Array.new
274 matching_suffix_files = Array.new
275 files_in_dir.each { | file |
277 if ( !File.directory?( file ) &&
280 file =~ /^#{phylogeny_id}.*#{suffix_pattern}$/ )
281 matching_files << file
283 if ( !File.directory?( file ) &&
286 file =~ /#{suffix_pattern}$/ )
287 matching_suffix_files << file
290 if matching_files.length < 1 && matching_suffix_files.length == 1
291 return matching_suffix_files[ 0 ]
294 if matching_files.length < 1 && matching_suffix_files.length < 1
295 Util.fatal_error( PRG_NAME, 'no file matching [' + phylogeny_id +
296 '_] [' + suffix_pattern + '] present in current directory' )
298 if matching_files.length > 1
299 Util.fatal_error( PRG_NAME, 'more than one file matching [' + phylogeny_id +
300 '_] [' + suffix_pattern + '] present in current directory' )
308 puts( " " + PRG_NAME + ".rb [options] <suffix of intrees to be decorated> <suffix for decorated outtrees> " )
310 puts( " options: -" + IDS_ONLY_OPTION + ": decorate with sequence/species names only" )
311 puts( " -" + DOMAINS_ONLY_OPTION + ": decorate with domain structures" )
314 end # class PhylogenyiesDecorator