fixed issue with UTF8 encoding.
[jalview.git] / forester / ruby / scripts / rb_dir_qsub.rb
1 #!/usr/local/bin/ruby -w
2 #
3 # = rb_dir_qsub
4 #
5 # Copyright::  Copyright (C) 2006-2008 Christian M. Zmasek
6 # License::    GNU Lesser General Public License (LGPL)
7 #
8 # $Id: rb_dir_qsub.rb,v 1.15 2009/11/07 02:06:59 cmzmasek Exp $
9 #
10 # To execute qsub commands.
11 # Submits PRG for every file in the current directory.
12 #
13 # Examples for PARAMETER_FILE:
14 #
15 # PRG:     /home/user/SOFTWARE/HMMER/hmmer-2.3.2/src/hmmpfam
16 # OPT:     -E 20 -A 0 /home/user/DATA/PFAM/Pfam_ls
17 # SUFFIX:  _hmmpfam_22_20_ls
18 #
19 # PRG:     /home/user/SOFTWARE/WUBLAST/tblastn
20 # OPT:
21 # VOPT:    AMPQU
22 # VOPT:    HYDMA
23 # SUFFIX:  _blast
24 #
25 # PRG:     /home/czmasek/SOFTWARE/HMMER/hmmer-3.0b2/binaries/intel-linux-x86_64/hmmscan
26 # OPT:     -E 2 --notextw --qformat fasta /home/czmasek/DATA/PFAM/PFAM240/Pfam-A.hmm
27 # SUFFIX:  .hmmscan30b2_240
28 # OUTPUT:  --domtblout
29
30
31 module ForesterScripts
32
33   if RUBY_VERSION !~ /1.9/
34     puts( "Your ruby version is #{RUBY_VERSION}, expected 1.9.x " )
35     exit( -1 )
36   end
37
38   PARAMETER_FILE = 'parameters.rb_dir_qsub'
39   SLEEP          = 1.0
40   REMOVE_SUFFIX  = true
41   LIMIT_TO_TAX_CODE  = true
42
43   PRG         = 'PRG:'
44   OPT         = 'OPT:'
45   VOPT        = 'VOPT:'
46   OUTPUT_OPT  = 'OUTPUT:'
47   SUFFIX      = 'SUFFIX:'
48   INPUT_PART  = 'INPUT_PART:'
49
50
51   PBS_O_WORKDIR       = '$PBS_O_WORKDIR/'
52   TMP_CMD_FILE_SUFFIX = '__QSUB'
53   NAME                = 'rb_dir_qsub'
54
55   if ( !File.exists?( PARAMETER_FILE ) )
56     puts( '[' + NAME + '] > parameters file "' + PARAMETER_FILE + '" not found' )
57     Process.exit!
58   end
59   puts( '[' + NAME + '] > reading ' + PARAMETER_FILE )
60
61   prg = ''
62   opt = ''
63   vopts = Array.new
64   suffix = ''
65   input_part = ''
66   output_opt = ''
67   open( PARAMETER_FILE ).each { |line|
68     if ( line.length > 1 && line =~ /^[^#]\S+/ )
69       if line =~ /^#{PRG}\s+(\S+)/
70         prg = $1
71       end
72       if line =~ /^\s*#{OPT}\s+(\S+.+)/
73         opt = $1
74       end
75       if line =~ /^\s*#{VOPT}\s+(\S+.+)/
76         vopts.push( $1 )
77       end
78       if line =~ /^\s*#{SUFFIX}\s+(\S+)/
79         suffix = $1
80       end
81       if line =~ /^\s*#{INPUT_PART}\s+(\S+)/
82         input_part = $1
83       end
84       if line =~ /^\s*#{OUTPUT_OPT}\s+(\S+.+)/
85         output_opt = $1
86       end
87     end
88   }
89   if ( prg.length < 1 )
90     puts( '[' + NAME + '] > no program name found in parameters file "' + PARAMETER_FILE + '"' )
91     Process.exit!
92   end
93   puts( '[' + NAME + '] > program: ' + prg )
94   puts( '[' + NAME + '] > option :  ' + opt )
95   vopts.each { |vopt|
96     puts( '[' + NAME + '] > voption:  ' + vopt )
97   }
98   puts( '[' + NAME + '] > suffix :  ' + suffix )
99   if ( input_part.length > 0 )
100     puts( '[' + NAME + '] > input:  ' + input_part )
101   end
102   if ( output_opt.length > 0 )
103     puts( '[' + NAME + '] > output opt :  ' + output_opt )
104   end
105   if vopts.empty?
106     vopts.push( "" )
107   end
108
109   files = Dir.entries( "." )
110
111   files.each { |file|
112     if ( !File.directory?( file ) && file !~ /^\./ && file !~ /#{PARAMETER_FILE}/ )
113
114       if ( input_part.length > 0 && file !~ /#{input_part}/ )
115         next
116       end
117       vopts.each { |vopt|
118         cmd = ""
119         outputfile = file.to_str
120         if LIMIT_TO_TAX_CODE
121           if outputfile =~ /^([A-Z0-9]{3,5})[_\.]/
122             outputfile = $1
123           end
124         end
125         if REMOVE_SUFFIX
126           if outputfile =~ /(.+)\..{1,5}/
127             outputfile = $1
128           end
129         end
130         if output_opt.length > 0
131           cmd = prg + ' ' +
132            output_opt + ' ' + PBS_O_WORKDIR + outputfile + suffix + ' ' +
133            opt + ' ' +
134            PBS_O_WORKDIR + file.to_str +
135            ' > /dev/null'
136         elsif vopt.length > 0
137           cmd = prg + ' ' + opt + ' ' + vopt + ' ' + PBS_O_WORKDIR + file.to_str +
138            ' > ' + PBS_O_WORKDIR + vopt + "_" + outputfile + suffix
139         else
140           cmd = prg + ' ' + opt + ' ' + PBS_O_WORKDIR + file.to_str +
141            ' > ' + PBS_O_WORKDIR + outputfile + suffix
142         end
143         tmp_cmd_file = file.to_str + TMP_CMD_FILE_SUFFIX
144         if File.exists?( tmp_cmd_file )
145           File.delete( tmp_cmd_file )
146         end
147         open( tmp_cmd_file, 'w' ) do |f|
148           f.write( cmd )
149         end
150         puts( '[' + NAME + '] > excuting ' + cmd )
151         IO.popen( 'qsub ' + tmp_cmd_file , 'r+' ) do |pipe|
152           pipe.close_write
153           puts pipe.read
154         end
155         sleep( SLEEP )
156         if File.exists?( tmp_cmd_file )
157           File.delete( tmp_cmd_file )
158         end
159       }
160     end
161   }
162   puts( '[' + NAME + '] > OK.' )
163   puts
164
165 end