v 1.04
[jalview.git] / forester / ruby / scripts / rb_dir_x.rb
1 #!/usr/local/bin/ruby -w
2 #
3 # = rb_x_qsub
4 #
5 # Copyright::  Copyright (C) 2006-2008 Christian M. Zmasek
6 # License::    GNU Lesser General Public License (LGPL)
7 #
8 # $Id: rb_dir_x.rb,v 1.8 2008/09/16 23:31:39 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
26 module ForesterScripts
27
28     if RUBY_VERSION !~ /1.9/
29         puts( "Your ruby version is #{RUBY_VERSION}, expected 1.9.x " )
30         exit( -1 )
31     end
32
33     PARAMETER_FILE    = 'parameters.rb_dir_x'
34     SLEEP = 1.0
35     SPAWN = true
36
37     PRG         = 'PRG:'
38     OPT         = 'OPT:'
39     VOPT        = 'VOPT:'
40     OUTPUT_OPT  = 'OUTPUT:' # TODO e.g. > or -o
41     SUFFIX      = 'SUFFIX:'
42     INPUT_PART  = 'INPUT_PART:'
43
44     NAME        = 'rb_dir_x'
45
46     if ( !File.exists?( PARAMETER_FILE ) )
47         puts( '[' + NAME + '] > parameters file "' + PARAMETER_FILE + '" not found' )
48         Process.exit!
49     end
50     puts( '[' + NAME + '] > reading ' + PARAMETER_FILE )
51
52     prg = ''
53     opt = ''
54     vopts = Array.new
55     suffix = ''
56     input_part = ''
57     open( PARAMETER_FILE ).each { |line|
58         if ( line.length > 1 && line =~ /^[^#]\S+/ )
59             if line =~ /^#{PRG}\s+(\S+)/
60                 prg = $1
61             end
62             if line =~ /^\s*#{OPT}\s+(\S+.+)/
63                 opt = $1
64             end
65             if line =~ /^\s*#{VOPT}\s+(\S+.+)/
66                 vopts.push( $1 )
67             end
68             if line =~ /^\s*#{SUFFIX}\s+(\S+)/
69                 suffix = $1
70             end
71             if line =~ /^\s*#{INPUT_PART}\s+(\S+)/
72                 input_part = $1
73             end
74         end
75     }
76     if ( prg.length < 1 )
77         puts( '[' + NAME + '] > no program name found in parameters file "' + PARAMETER_FILE + '"' )
78         Process.exit!
79     end
80     puts( '[' + NAME + '] > program: ' + prg )
81     puts( '[' + NAME + '] > option :  ' + opt )
82     vopts.each { |vopt|
83         puts( '[' + NAME + '] > voption:  ' + vopt )
84     }
85     puts( '[' + NAME + '] > suffix :  ' + suffix )
86     if ( input_part.length > 0 )
87         puts( '[' + NAME + '] > input  :  ' + input_part )
88     end
89     if vopts.empty?
90         vopts.push( "" )
91     end
92
93     files = Dir.entries( "." )
94
95     files.each { |file|
96         if ( !File.directory?( file ) && file !~ /^\./ && file !~ /#{PARAMETER_FILE}/ )
97
98             if ( input_part.length > 0 && file !~ /#{input_part}/ )
99                 next
100             end
101             vopts.each { |vopt|
102                 cmd = ""
103                 if vopt.length > 0
104                     cmd = 'nohup ' + prg + ' ' + opt + ' ' + vopt + ' ' + file.to_str +
105                      ' > ' + vopt + "_" + file.to_str + suffix + ' &'
106                 else
107                     cmd = 'nohup ' + prg + ' ' + opt + ' ' + file.to_str +
108                      ' > ' + file.to_str + suffix + ' &'
109                 end
110
111                 puts( '[' + NAME + '] > excuting ' + cmd )
112                 if SPAWN
113                     spawn( cmd, STDERR => "/dev/null" )
114                 else
115                     IO.popen( cmd , 'r+' ) do |pipe|
116                         pipe.close_write
117                         puts pipe.read
118                     end
119                 end
120                 sleep( SLEEP )
121
122             }
123         end
124     }
125     puts( '[' + NAME + '] > OK.' )
126     puts
127
128 end