to download from ensembl ftp site
[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
42     PRG         = 'PRG:'
43     OPT         = 'OPT:'
44     VOPT        = 'VOPT:'
45     OUTPUT_OPT  = 'OUTPUT:'
46     SUFFIX      = 'SUFFIX:'
47     INPUT_PART  = 'INPUT_PART:'
48
49
50     PBS_O_WORKDIR       = '$PBS_O_WORKDIR/'
51     TMP_CMD_FILE_SUFFIX = '__QSUB'
52     NAME                = 'rb_dir_qsub'
53
54     if ( !File.exists?( PARAMETER_FILE ) )
55         puts( '[' + NAME + '] > parameters file "' + PARAMETER_FILE + '" not found' )
56         Process.exit!
57     end
58     puts( '[' + NAME + '] > reading ' + PARAMETER_FILE )
59
60     prg = ''
61     opt = ''
62     vopts = Array.new
63     suffix = ''
64     input_part = ''
65     output_opt = ''
66     open( PARAMETER_FILE ).each { |line|
67         if ( line.length > 1 && line =~ /^[^#]\S+/ )
68             if line =~ /^#{PRG}\s+(\S+)/
69                 prg = $1
70             end
71             if line =~ /^\s*#{OPT}\s+(\S+.+)/
72                 opt = $1
73             end
74             if line =~ /^\s*#{VOPT}\s+(\S+.+)/
75                 vopts.push( $1 )
76             end
77             if line =~ /^\s*#{SUFFIX}\s+(\S+)/
78                 suffix = $1
79             end
80             if line =~ /^\s*#{INPUT_PART}\s+(\S+)/
81                 input_part = $1
82             end
83             if line =~ /^\s*#{OUTPUT_OPT}\s+(\S+.+)/
84                 output_opt = $1
85             end
86         end
87     }
88     if ( prg.length < 1 )
89         puts( '[' + NAME + '] > no program name found in parameters file "' + PARAMETER_FILE + '"' )
90         Process.exit!
91     end
92     puts( '[' + NAME + '] > program: ' + prg )
93     puts( '[' + NAME + '] > option :  ' + opt )
94     vopts.each { |vopt|
95         puts( '[' + NAME + '] > voption:  ' + vopt )
96     }
97     puts( '[' + NAME + '] > suffix :  ' + suffix )
98     if ( input_part.length > 0 )
99         puts( '[' + NAME + '] > input:  ' + input_part )
100     end
101     if ( output_opt.length > 0 )
102         puts( '[' + NAME + '] > output opt :  ' + output_opt )
103     end
104     if vopts.empty?
105         vopts.push( "" )
106     end
107
108     files = Dir.entries( "." )
109
110     files.each { |file|
111         if ( !File.directory?( file ) && file !~ /^\./ && file !~ /#{PARAMETER_FILE}/ )
112
113             if ( input_part.length > 0 && file !~ /#{input_part}/ )
114                 next
115             end
116             vopts.each { |vopt|
117                 cmd = ""
118                 outputfile = file.to_str
119                 if REMOVE_SUFFIX
120                     if outputfile =~ /(.+)\..{1,5}/
121                         outputfile = $1
122                     end
123                 end
124                 if output_opt.length > 0
125                     cmd = prg + ' ' +
126                      output_opt + ' ' + PBS_O_WORKDIR + outputfile + suffix + ' ' +
127                      opt + ' ' +
128                      PBS_O_WORKDIR + file.to_str +
129                      ' > /dev/null'
130                 elsif vopt.length > 0
131                     cmd = prg + ' ' + opt + ' ' + vopt + ' ' + PBS_O_WORKDIR + file.to_str +
132                      ' > ' + PBS_O_WORKDIR + vopt + "_" + outputfile + suffix
133                 else
134                     cmd = prg + ' ' + opt + ' ' + PBS_O_WORKDIR + file.to_str +
135                      ' > ' + PBS_O_WORKDIR + outputfile + suffix
136                 end
137                 tmp_cmd_file = file.to_str + TMP_CMD_FILE_SUFFIX
138                 if File.exists?( tmp_cmd_file )
139                     File.delete( tmp_cmd_file )
140                 end
141                 open( tmp_cmd_file, 'w' ) do |f|
142                     f.write( cmd )
143                 end
144                 puts( '[' + NAME + '] > excuting ' + cmd )
145                 IO.popen( 'qsub ' + tmp_cmd_file , 'r+' ) do |pipe|
146                     pipe.close_write
147                     puts pipe.read
148                 end
149                 sleep( SLEEP )
150                 if File.exists?( tmp_cmd_file )
151                     File.delete( tmp_cmd_file )
152                 end
153             }
154         end
155     }
156     puts( '[' + NAME + '] > OK.' )
157     puts
158
159 end