1 #!/usr/local/bin/ruby -w
3 # = lib/evo/apps/phylogenies_decorator
5 # Copyright:: Copyright (C) 2017 Christian M. Zmasek
6 # License:: GNU Lesser General Public License (LGPL)
8 # Last modified: 2017/02/09
10 # decoration of phylogenies with sequence/species names and domain architectures
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'
21 class PhylogeniesDecorator
23 #DECORATOR_OPTIONS_SEQ_NAMES = '-r=1 -mdn'
24 #DECORATOR_OPTIONS_SEQ_NAMES = '-p -t -sn'
25 #DECORATOR_OPTIONS_SEQ_NAMES = '-p -t -tc -mp -or'
26 DECORATOR_OPTIONS_SEQ_NAMES = '-p -t -mp -or'
27 # -mdn is a hidden expert option to rename e.g. "6_ORYLA3" to "6_[3]_ORYLA"
28 #DECORATOR_OPTIONS_SEQ_NAMES = '-sn -r=1'
29 #DECORATOR_OPTIONS_DOMAINS = '-r=1'
30 DECORATOR_OPTIONS_DOMAINS = '-p -t'
31 IDS_MAPFILE_SUFFIX = '.nim'
32 DOMAINS_MAPFILE_SUFFIX = '_hmmscan_10.dff'
35 IDS_ONLY = false #TODO this should be a command line option
36 FIXED_NIM_FILE = 'all.nim' #TODO this should be a command line option
37 TMP_FILE_1 = '___PD1___'
38 TMP_FILE_2 = '___PD2___'
39 LOG_FILE = '00_phylogenies_decorator.log'
40 FORESTER_HOME = ENV[Constants::FORESTER_HOME_ENV_VARIABLE]
41 JAVA_HOME = ENV[Constants::JAVA_HOME_ENV_VARIABLE]
43 PRG_NAME = "phylogenies_decorator"
45 PRG_DESC = "decoration of phylogenies with sequence/species names and domain architectures"
47 WWW = "https://sites.google.com/site/cmzmasek/home/software/forester"
49 HELP_OPTION_1 = "help"
52 NL = Constants::LINE_DELIMITER
55 Util.print_program_information( PRG_NAME,
62 if ( ARGV == nil || ARGV.length > 3 || ARGV.length < 2 )
67 if FORESTER_HOME == nil || FORESTER_HOME.length < 1
68 Util.fatal_error( PRG_NAME, "apparently environment variable #{Constants::FORESTER_HOME_ENV_VARIABLE} has not been set" )
70 if JAVA_HOME == nil || JAVA_HOME.length < 1
71 Util.fatal_error( PRG_NAME, "apparently environment variable #{Constants::JAVA_HOME_ENV_VARIABLE} has not been set" )
74 if !File.exist?( FORESTER_HOME )
75 Util.fatal_error( PRG_NAME, '[' + FORESTER_HOME + '] does not exist' )
77 if !File.exist?( JAVA_HOME )
78 Util.fatal_error( PRG_NAME, '[' + JAVA_HOME + '] does not exist' )
81 decorator = JAVA_HOME + '/bin/java -cp ' + FORESTER_HOME + '/java/forester.jar org.forester.application.decorator'
84 cla = CommandLineArguments.new( ARGV )
85 rescue ArgumentError => e
86 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
89 if ( cla.is_option_set?( HELP_OPTION_1 ) ||
90 cla.is_option_set?( HELP_OPTION_2 ) )
95 if File.exist?( LOG_FILE )
96 Util.fatal_error( PRG_NAME, 'logfile [' + LOG_FILE + '] already exists' )
99 in_suffix = cla.get_file_name( 0 )
100 out_suffix = cla.get_file_name( 1 )
105 log << "Program : " + PRG_NAME + NL
106 log << "Version : " + PRG_VERSION + NL
107 log << "Program date : " + PRG_DATE + NL
108 log << "Options for seq names: " + DECORATOR_OPTIONS_SEQ_NAMES + NL
109 log << "Options for domains : " + DECORATOR_OPTIONS_DOMAINS + NL
110 log << "FORESTER_HOME : " + FORESTER_HOME + NL
111 log << "JAVA_HOME : " + JAVA_HOME + NL + NL
112 log << "Date/time: " + now.to_s + NL
113 log << "Directory: " + Dir.getwd + NL + NL
115 Util.print_message( PRG_NAME, 'input suffix : ' + in_suffix )
116 Util.print_message( PRG_NAME, 'output suffix : ' + out_suffix )
118 log << 'input suffix : ' + in_suffix + NL
119 log << 'output suffix : ' + out_suffix + NL
121 if ( File.exist?( TMP_FILE_1 ) )
122 File.delete( TMP_FILE_1 )
124 if ( File.exist?( TMP_FILE_2 ) )
125 File.delete( TMP_FILE_2 )
128 files = Dir.entries( "." )
132 files.each { | phylogeny_file |
133 if ( !File.directory?( phylogeny_file ) &&
134 phylogeny_file !~ /^\./ &&
135 phylogeny_file !~ /^00/ &&
136 phylogeny_file !~ /#{out_suffix}$/ &&
137 phylogeny_file =~ /#{in_suffix}$/ )
139 Util.check_file_for_readability( phylogeny_file )
141 Util.fatal_error( PRG_NAME, 'can not read from: ' + phylogeny_file + ': '+ $! )
146 outfile = phylogeny_file.sub( /#{in_suffix}$/, out_suffix )
149 outfile = outfile.sub( /_ni_/, '_' )
152 if File.exist?( outfile )
153 msg = counter.to_s + ': ' + phylogeny_file + ' -> ' + outfile +
154 ' : already exists, skipping'
155 Util.print_message( PRG_NAME, msg )
160 Util.print_message( PRG_NAME, counter.to_s + ': ' + phylogeny_file + ' -> ' + outfile )
161 log << counter.to_s + ': ' + phylogeny_file + ' -> ' + outfile + NL
163 phylogeny_id = get_id( phylogeny_file )
164 if phylogeny_id == nil || phylogeny_id.size < 1
165 Util.fatal_error( PRG_NAME, 'could not get id from ' + phylogeny_file.to_s )
168 Util.print_message( PRG_NAME, "id: " + phylogeny_id )
169 log << "id: " + phylogeny_id + NL
171 ids_mapfile_name = nil
172 domains_mapfile_name = nil
175 if ( FIXED_NIM_FILE == nil )
176 ids_mapfile_name = get_file( files, phylogeny_id, IDS_MAPFILE_SUFFIX )
178 ids_mapfile_name = FIXED_NIM_FILE
182 domains_mapfile_name = get_file( files, phylogeny_id, DOMAINS_MAPFILE_SUFFIX )
183 seqs_file_name = get_seq_file( files, phylogeny_id )
188 Util.check_file_for_readability( domains_mapfile_name )
190 Util.fatal_error( PRG_NAME, 'failed to read from [#{domains_mapfile_name}]: ' + $! )
193 Util.check_file_for_readability( seqs_file_name )
195 Util.fatal_error( PRG_NAME, 'failed to read from [#{seqs_file_name }]: ' + $! )
200 Util.check_file_for_readability( ids_mapfile_name )
202 Util.fatal_error( PRG_NAME, 'failed to read from [#{ids_mapfile_name}]: ' + $! )
207 ' -t -p -f=m ' + phylogeny_file + ' ' +
208 seqs_file_name + ' ' + TMP_FILE_1
211 execute_cmd( cmd, log )
213 Util.fatal_error( PRG_NAME, 'error: ' + $! )
216 cmd = decorator + ' ' + DECORATOR_OPTIONS_DOMAINS + ' ' +
217 '-f=d ' + TMP_FILE_1 + ' ' +
218 domains_mapfile_name + ' ' + TMP_FILE_2
221 execute_cmd( cmd, log )
223 Util.fatal_error( PRG_NAME, 'error: ' + $! )
228 cmd = decorator + ' ' + DECORATOR_OPTIONS_SEQ_NAMES + ' ' +
229 '-f=n ' + phylogeny_file + ' ' +
230 ids_mapfile_name + ' ' + outfile
233 execute_cmd( cmd, log )
235 Util.fatal_error( PRG_NAME, 'error: ' + $! )
238 cmd = decorator + ' ' + DECORATOR_OPTIONS_SEQ_NAMES + ' ' +
239 '-f=n ' + TMP_FILE_2 + ' ' +
240 ids_mapfile_name + ' ' + outfile
243 execute_cmd( cmd, log )
245 Util.fatal_error( PRG_NAME, 'error: ' + $! )
247 File.delete( TMP_FILE_1 )
248 File.delete( TMP_FILE_2 )
252 open( LOG_FILE, 'w' ) do | f |
256 Util.print_message( PRG_NAME, 'OK' )
260 def execute_cmd( cmd, log )
261 log << 'executing ' + cmd + NL
262 IO.popen( cmd , 'r+' ) do | pipe |
264 log << pipe.read + NL + NL
269 def get_id( phylogeny_file_name )
270 if phylogeny_file_name =~ /^(.+?_.+?)_/
272 elsif phylogeny_file_name =~ /^(.+?)__/
274 elsif phylogeny_file_name =~ /^(.+?)_/
280 def get_file( files_in_dir, phylogeny_id, suffix_pattern )
281 matching_files = Util.get_matching_files( files_in_dir, phylogeny_id, suffix_pattern )
282 if matching_files.length < 1
283 Util.fatal_error( PRG_NAME, 'no file matching [' + phylogeny_id +
284 '...' + suffix_pattern + '] present in current directory' )
286 if matching_files.length > 1
287 Util.fatal_error( PRG_NAME, 'more than one file matching [' +
288 phylogeny_id + '...' + suffix_pattern + '] present in current directory' )
293 def get_seq_file( files_in_dir, phylogeny_id )
294 matching_files = Array.new
296 files_in_dir.each { | file |
298 if ( !File.directory?( file ) &&
301 ( file =~ /^#{phylogeny_id}__.+\d$/ || file =~ /^#{phylogeny_id}_.*\.fasta$/ ) )
302 matching_files << file
306 if matching_files.length < 1
307 Util.fatal_error( PRG_NAME, 'no seq file matching [' +
308 phylogeny_id + '_] present in current directory' )
310 if matching_files.length > 1
311 Util.fatal_error( PRG_NAME, 'more than one seq file matching [' +
312 phylogeny_id + '_] present in current directory' )
320 puts( " " + PRG_NAME + ".rb <suffix of intrees to be decorated> <suffix for decorated outtrees> " )
324 end # class PhylogenyiesDecorator