inprogress
[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             cmd = subst_hmm( cmd, id )
157             cmd = subst_min_length( cmd, id, min_lengths )
158             cmd = subst_options( cmd, options )
159             if use_job_submission_system
160               cmd = subst_aln_name( cmd, PBS_O_WORKDIR + aln_name )
161             else
162               cmd = subst_aln_name( cmd, aln_name )
163             end
164
165             if ( cmd =~ /%/ )
166               cmd =~ /(%.*?%)/
167               problem = $1
168               puts( '[' + PRG_NAME + '] > WARNING    : [' + id + '] command still contains placeholder: ' + problem )
169               log << "WARNING: command still contains placeholder: " + cmd + NL
170             else
171               tmp_cmd_file = file.to_str[ 0..4 ] + TMP_CMD_FILE_SUFFIX
172               if ( File.exists?( tmp_cmd_file ) )
173                 File.delete( tmp_cmd_file )
174               end
175               if use_job_submission_system
176                 open( tmp_cmd_file, 'w' ) do |f|
177                   f.write( cmd )
178                 end
179               end
180
181               log << cmd + NL
182
183               if use_job_submission_system
184                 IO.popen( 'qsub -q ' + QUEUE  + ' -l walltime=' + WALLTIME + ' ' + tmp_cmd_file , 'r+' ) do | pipe |
185                   pipe.close_write
186                 end
187               else
188                 spawn( 'nohup ' + cmd + ' &', STDERR => "/dev/null" )
189               end
190
191               sleep( WAIT )
192               if ( File.exists?( tmp_cmd_file ) )
193                 File.delete( tmp_cmd_file )
194               end
195             end
196           end
197         end
198       }
199
200       open( LOG_FILE, 'w' ) do | f |
201         f.write( log )
202       end
203
204       puts()
205       puts( '[' + PRG_NAME + '] > OK' )
206       puts()
207
208     end # def run
209
210     def prepare( command, paths )
211       paths.each_pair{ | name, full |
212         command = command.gsub( name, full )
213       }
214       command
215     end
216
217     def subst_options( command, options )
218       opt_placeholders = command.scan( /%\[\S+\]%/ )
219       opt_placeholders.each { | opt_placeholder |
220         opt_placeholder = opt_placeholder.gsub( OPTION_OPEN , '' )
221         opt_placeholder = opt_placeholder.gsub( OPTION_CLOSE, '' )
222         opt_value = options[ opt_placeholder ]
223         if ( opt_value != nil && opt_value.size > 0 )
224           command = command.gsub( OPTION_OPEN + opt_placeholder + OPTION_CLOSE, opt_value )
225         end
226       }
227       command
228     end
229
230     def subst_aln_name( command, aln_name )
231       command = command.gsub( '$', aln_name )
232       command
233     end
234
235     def subst_hmm( command, id )
236       if id != nil && id.length > 0
237         hmm = PFAM_HHMS + id + ".hmm"
238         command = command.gsub( OPTION_OPEN + HMM + OPTION_CLOSE, hmm )
239       end
240       command
241     end
242
243     def subst_min_length( command, id, min_lengths )
244       min_length = nil
245       if id != nil && id.length > 0
246         min_length = min_lengths[ id ]
247       end
248       if  min_length != nil && min_length.size > 0
249         command = command.gsub( OPTION_OPEN + RSL + OPTION_CLOSE, min_length )
250       else
251         command = command.gsub( OPTION_OPEN + RSL + OPTION_CLOSE, MIN_LENGTH_DEFAULT.to_s )
252       end
253       command
254     end
255
256     def get_id( aln_name )
257       id = nil
258       if aln_name.include? "__"
259         id = aln_name[ 0, aln_name.index( "__" ) ]
260       end
261       if id == nil || id.length < 1
262         puts( '[' + PRG_NAME + '] > WARNING: could not get id from [' + aln_name + ']' )
263       end
264       id
265     end
266
267   end # class PhylogenyFactory
268
269 end # module Evoruby