8bc325a76fd20ee7288adc74db8b3b0ea74edf36
[jalview.git] / forester / ruby / evoruby / lib / evo / tool / phylogeny_factory.rb
1 #
2 # = lib/evo/apps/phylogeny_factory - PhylogenyFactory class
3 #
4 # Copyright::  Copyright (C) 2006-2007 Christian M. Zmasek
5 # License::    GNU Lesser General Public License (LGPL)
6 #
7 # $Id: phylogeny_factory.rb,v 1.32 2010/12/13 19:00:11 cmzmasek Exp $
8
9 require 'lib/evo/util/constants'
10 require 'lib/evo/util/util'
11 require 'lib/evo/util/command_line_arguments'
12
13 require 'set'
14 require 'date'
15
16 module Evoruby
17
18   class PhylogenyFactory
19
20     PRG_NAME       = "phylogeny_factory"
21     PRG_DATE       = "130402"
22     PRG_DESC       = "automated phylogeny reconstruction using queing system"
23     PRG_VERSION    = "1.002"
24     COPYRIGHT      = "2013 Christian M Zmasek"
25     CONTACT        = "phylosoft@gmail.com"
26     WWW            = "www.phylosoft.org"
27
28     USE_JOB_SUBMISSION_SYSTEM_OPTION  = 's'
29     LOG_FILE                          = '00_phylogeny_factory.log'
30     TEMPLATE_FILE                     = '00_phylogeny_factory.template'
31     PBS_O_WORKDIR                     = '$PBS_O_WORKDIR/'
32     MIN_LENGTH_DEFAULT                = 50
33     PFAM_HHMS                         = "/home/czmasek/DATA/PFAM/PFAM260X/PFAM_A_HMMs/"
34     WALLTIME                          = '100:00:00'
35     QUEUE                             = 'default'
36
37     TMP_CMD_FILE_SUFFIX = '_QSUB'
38
39     RSL                 = 'RSL'
40     HMM                 = 'HMM'
41
42     OPTION_OPEN          = '%['
43     OPTION_CLOSE          = ']%'
44
45     WAIT                 = 1.0
46
47     NL = Constants::LINE_DELIMITER
48
49     def run
50
51       Util.print_program_information( PRG_NAME,
52         PRG_VERSION,
53         PRG_DESC,
54         PRG_DATE,
55         COPYRIGHT,
56         CONTACT,
57         WWW,
58         STDOUT )
59
60       begin
61         cla = CommandLineArguments.new( ARGV )
62       rescue ArgumentError => e
63         Util.fatal_error( PRG_NAME, "error: " + e.to_s )
64       end
65
66       allowed_opts = Array.new
67       allowed_opts.push( USE_JOB_SUBMISSION_SYSTEM_OPTION )
68
69       disallowed = cla.validate_allowed_options_as_str( allowed_opts )
70       if ( disallowed.length > 0 )
71         Util.fatal_error( PRG_NAME,
72           "unknown option(s): " + disallowed,
73           STDOUT )
74       end
75
76       if File.exists?( LOG_FILE )
77         puts( '[' + PRG_NAME + '] > log file [' + LOG_FILE + '] already exists' )
78         exit( -1 )
79       end
80
81       if !File.exists?( TEMPLATE_FILE )
82         puts( '[' + PRG_NAME + '] > template file [' + TEMPLATE_FILE + '] not found' )
83         exit( -1 )
84       end
85
86       use_job_submission_system = false
87       if cla.is_option_set?( USE_JOB_SUBMISSION_SYSTEM_OPTION )
88         use_job_submission_system = true
89       end
90
91       log = String.new
92
93       now = DateTime.now
94       log << "Program     : " + PRG_NAME + NL
95       log << "Version     : " + PRG_VERSION + NL
96       log << "Program date: " + PRG_DATE + NL + NL
97       log << "Date/time   : " + now.to_s + NL
98       log << "Directory   : " + Dir.getwd  + NL + NL
99
100       puts( '[' + PRG_NAME + '] > reading ' + TEMPLATE_FILE )
101
102       paths       = Hash.new  # path placeholder -> full path
103       min_lengths = Hash.new  # alignment id -> minimal length
104       options     = Hash.new  # option placeholder -> option
105       ids         = Set.new
106
107       commands    = Array.new
108
109       log <<  "////////////////////////////////////////////////////////////////// #{NL}"
110       log << "Template file [" + TEMPLATE_FILE + "]:#{NL}"
111
112       command = String.new
113
114       open( TEMPLATE_FILE ).each { | line |
115         log << line
116         if ( line =~ /^#/ )
117         elsif ( line =~ /^\$\s*(\S+)\s*=\s*(\S+)/ )
118           paths[ $1 ] = $2
119           puts( '[' + PRG_NAME + '] > paths      : ' + $1 + ' => ' + $2 )
120
121         elsif ( line =~ /^%\s*#{RSL}\s*(\S+)\s*=\s*(\S+)/ )
122           min_lengths[ $1 ] = $2
123           puts( '[' + PRG_NAME + '] > min lengths: ' + $1 + ' => ' + $2 )
124
125         elsif ( line =~ /^%\s*(\S+)\s*=\s*(\S+)/ )
126           options[ $1 ] = $2
127           puts( '[' + PRG_NAME + '] > options    : ' + $1 + ' => ' + $2 )
128
129         elsif ( line =~ /^>\s*(.+)/ )
130           command = command + $1 + ";#{NL}"
131
132         elsif ( line =~ /^-/  )
133           commands << prepare( command, paths )
134           command = String.new
135         end
136       }
137       log << "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ #{NL}#{NL}"
138
139       files = Dir.entries( "." )
140
141       files.each { | file |
142         if ( !File.directory?( file ) &&
143              file !~ /^\./ &&
144              file !~ /#{TEMPLATE_FILE}/ &&
145              file !~ /.bck$/ &&
146              file !~ /.log$/ &&
147              file !~ /nohup/ &&
148              file !~ /^00/ )
149           aln_name = file.to_str
150           id = get_id( aln_name )
151           if !ids.include?( id )
152             ids.add( id )
153           end
154           puts( '[' + PRG_NAME + '] > file [id]  : ' + aln_name + ' [' + id + ']' )
155           commands.each do | cmd |
156             id = get_id( aln_name )
157             cmd = subst_hmm( cmd, id )
158             cmd = subst_min_length( cmd, id, min_lengths )
159             cmd = subst_options( cmd, options )
160             if use_job_submission_system
161               cmd = subst_aln_name( cmd, PBS_O_WORKDIR + aln_name )
162             else
163               cmd = subst_aln_name( cmd, aln_name )
164             end
165
166             if ( cmd =~ /%/ )
167               cmd =~ /(%.*?%)/
168               problem = $1
169               puts( '[' + PRG_NAME + '] > WARNING    : [' + id + '] command still contains placeholder: ' + problem )
170               log << "WARNING: command still contains placeholder: " + cmd + NL
171             else
172               tmp_cmd_file = file.to_str[ 0..4 ] + TMP_CMD_FILE_SUFFIX
173               if ( File.exists?( tmp_cmd_file ) )
174                 File.delete( tmp_cmd_file )
175               end
176               if use_job_submission_system
177                 open( tmp_cmd_file, 'w' ) do |f|
178                   f.write( cmd )
179                 end
180               end
181
182               log << cmd + NL
183
184               if use_job_submission_system
185                 IO.popen( 'qsub -q ' + QUEUE  + ' -l walltime=' + WALLTIME + ' ' + tmp_cmd_file , 'r+' ) do | pipe |
186                   pipe.close_write
187                 end
188               else
189                 spawn( 'nohup ' + cmd + ' &', STDERR => "/dev/null" )
190               end
191
192               sleep( WAIT )
193               if ( File.exists?( tmp_cmd_file ) )
194                 File.delete( tmp_cmd_file )
195               end
196             end
197           end
198         end
199       }
200
201       open( LOG_FILE, 'w' ) do | f |
202         f.write( log )
203       end
204
205       puts()
206       puts( '[' + PRG_NAME + '] > OK' )
207       puts()
208
209     end # def run
210
211     def prepare( command, paths )
212       paths.each_pair{ | name, full |
213         command = command.gsub( name, full )
214       }
215       command
216     end
217
218     def subst_options( command, options )
219       opt_placeholders = command.scan( /%\[\S+\]%/ )
220       opt_placeholders.each { | opt_placeholder |
221         opt_placeholder = opt_placeholder.gsub( OPTION_OPEN , '' )
222         opt_placeholder = opt_placeholder.gsub( OPTION_CLOSE, '' )
223         opt_value = options[ opt_placeholder ]
224         if ( opt_value != nil && opt_value.size > 0 )
225           command = command.gsub( OPTION_OPEN + opt_placeholder + OPTION_CLOSE, opt_value )
226         end
227       }
228       command
229     end
230
231     def subst_aln_name( command, aln_name )
232       command = command.gsub( '$', aln_name )
233       command
234     end
235
236     def subst_hmm( command, id )
237       if id != nil && id.length > 1
238         hmm = PFAM_HHMS + id + ".hmm"
239         command = command.gsub( OPTION_OPEN + HMM + OPTION_CLOSE, hmm )
240       end
241       command
242     end
243
244     def subst_min_length( command, id, min_lengths )
245       min_length = nil
246       if id != nil && id.length > 1
247         min_length = min_lengths[ id ]
248       end
249       if  min_length != nil && min_length.size > 0
250         command = command.gsub( OPTION_OPEN + RSL + OPTION_CLOSE, min_length )
251       else
252         command = command.gsub( OPTION_OPEN + RSL + OPTION_CLOSE, MIN_LENGTH_DEFAULT.to_s )
253       end
254       command
255     end
256
257     def get_id( aln_name )
258       id = aln_name[ 0, aln_name.index( "__" ) ]
259       if id == nil || id.length < 1
260         puts( '[' + PRG_NAME + '] > WARNING: could not get id from [' + aln_name + ']' )
261       end
262       id
263     end
264
265   end # class PhylogenyFactory
266
267 end # module Evoruby