e07a6205d92de414c9fbca6d5b96b9ff682780c8
[jalview.git] / forester / ruby / evoruby / exe / run_phylo_pipeline_x.rb
1 #!/usr/local/bin/ruby -w
2 #
3 # = run_phylo_pipeline
4 #
5 # Copyright::  Copyright (C) 2010 Christian M. Zmasek
6 # License::    GNU Lesser General Public License (LGPL)
7 #
8 # $Id Exp $
9 #
10 #
11
12 require 'fileutils'
13
14 module Evoruby
15
16   class RunPhyloPipeline
17
18     LAUNCH_ANALYSIS = true
19     HOME          = "/home/czmasek/"
20     FORESTER_RUBY = "#{HOME}SOFTWARE/FORESTER/DEV/forester/forester/ruby/evoruby/exe/"
21     PFAM          = "#{HOME}DATA/PFAM/PFAM270X/"
22     HMMSCAN       = "#{HOME}SOFTWARE/HMMER/hmmer-3.0/src/hmmscan"
23     HSP           = "#{FORESTER_RUBY}hsp.rb"
24     D2F           = "#{FORESTER_RUBY}d2f.rb"
25     DSX           = "#{FORESTER_RUBY}dsx.rb"
26     TAP           = "#{FORESTER_RUBY}tap.rb"
27     PF            = "#{FORESTER_RUBY}phylogeny_factory.rb"
28     TEMPLATE_FILE = '00_phylogeny_factory.template'
29
30     def run
31       unless ARGV.length >= 2 && ARGV.length <= 4
32         error "arguments are:  <min-length> " +
33          "<neg E-value exponent for domain extraction> [E-value for hmmscan, default is 10] [hmmscan option, default is --nobias, --max for no heuristics]"
34       end
35
36       length      = ARGV[ 0 ].to_i
37       e_value_exp = ARGV[ 1 ].to_i
38
39       e_for_hmmscan = 10
40       hmmscan_option = "--nobias"
41
42       if ARGV.length == 4
43         hmmscan_option = ARGV[ 3 ]
44       end
45       if ARGV.length == 3 || ARGV.length == 4
46         e_for_hmmscan = ARGV[ 2 ].to_i
47       end
48
49       if e_value_exp < 0
50         error "E-value exponent for domain extraction cannot be negative"
51       end
52       if length <= 1
53         error "length cannot be smaller than or equal to 1"
54       end
55       if e_for_hmmscan < 1
56         error "E-value for hmmscan cannot be smaller than 1"
57       end
58
59       input_files = Dir.entries(".").select { |f| !File.directory?( f ) && f.downcase.end_with?( ".fasta" ) }
60
61       puts "Input files:"
62       input_files.each do | input |
63         puts input
64       end
65       puts
66
67       counter = 1
68       input_files.each do | input |
69
70         puts counter.to_s + "/" +  input_files.size.to_s + " " + input + ": "
71
72         counter += 1
73
74         hmm_name = ""
75         id_norm = false
76         orig_input = input
77
78         if input.downcase.end_with?( "_ni.fasta" )
79           hmm_name = input[ 0 .. input.length - 10 ]
80         elsif input.downcase.end_with?( ".fasta" )
81           hmm_name = input[ 0 .. input.length - 7 ]
82           unless File.exist? hmm_name
83             id_norm = true
84             puts
85             puts "a. identifier normalization:"
86             cmd = "#{TAP} #{input} #{hmm_name}_ni.fasta #{hmm_name}.nim"
87             run_command( cmd )
88             input = hmm_name + "_ni.fasta"
89           else
90             input = hmm_name + "/" + hmm_name + "_ni.fasta"
91             unless File.exist? input
92               error "expected to already exist: " + input
93             end
94             puts "a. identifier normalization already done:" + input
95           end
96         else
97           error "illegal name: " + input
98         end
99
100         unless File.exist? hmm_name
101           Dir.mkdir( hmm_name )
102         end
103
104         puts
105         hmmscan_output = hmm_name + "/" + hmm_name + "_hmmscan_" + e_for_hmmscan.to_s
106         unless File.exist? hmmscan_output
107           puts "b. hmmscan:"
108           cmd = "#{HMMSCAN} #{hmmscan_option} --domtblout #{hmmscan_output} -E #{e_for_hmmscan.to_s} #{PFAM}Pfam-A.hmm #{input}"
109           run_command( cmd )
110         else
111           puts "b. hmmscan output already exists: " + hmmscan_output
112         end
113         puts
114
115
116         hsp_output = hmm_name + "/" + hmm_name + "_hmmscan_#{e_for_hmmscan.to_s}_domain_table"
117         unless File.exist? hsp_output
118           puts "c. hmmscan to simple domain table:"
119           cmd = "#{HSP} #{hmmscan_output} #{hsp_output}"
120           run_command( cmd )
121         else
122           puts "c. hmmscan to simple domain table output already exists: " + hsp_output
123         end
124         puts
125
126         d2f_output = "#{hmm_name}/#{hmm_name}_hmmscan_#{e_for_hmmscan.to_s}.dff"
127         unless File.exist? d2f_output
128           puts "d. domain table to forester format:"
129           cmd = "#{D2F} -e=10 #{hsp_output} #{input} #{d2f_output}"
130           run_command( cmd )
131         else
132           puts "d. domain table to forester format output already exists: " + d2f_output
133         end
134         puts
135
136
137         dsx_output = "#{hmm_name}/#{hmm_name}__#{hmm_name}__ee#{e_value_exp.to_s}_#{length}"
138         unless File.exist? dsx_output
139           puts "e. dsx:"
140           cmd = "#{DSX} -d -e=1e-#{e_value_exp.to_s} -l=#{length} #{hmm_name} #{hmmscan_output} #{input} #{dsx_output}"
141           run_command( cmd )
142         else
143           puts "e. dsx output already exists: " + dsx_output
144         end
145         puts
146
147         if id_norm
148           FileUtils.mv "#{hmm_name}_ni.fasta", "#{hmm_name}/#{hmm_name}_ni.fasta"
149           FileUtils.mv "#{hmm_name}.nim", "#{hmm_name}/#{hmm_name}.nim"
150           FileUtils.cp orig_input, "#{hmm_name}/#{orig_input}"
151         end
152
153         msa_dir = hmm_name + "/msa_ee#{e_value_exp.to_s}_#{length}"
154         msa_100_dir =hmm_name + "/msa100_ee#{e_value_exp.to_s}_#{length}"
155
156         unless File.exist? msa_dir
157           Dir.mkdir( msa_dir )
158         end
159         unless File.exist? msa_100_dir
160           Dir.mkdir( msa_100_dir )
161         end
162
163         run_1 = false
164         run_100 = false
165
166         unless File.exist? "#{msa_dir}/#{dsx_output}"
167           run_1 = true
168           FileUtils.cp "#{dsx_output}.fasta", "#{msa_dir}/#{dsx_output}"
169         end
170
171         unless File.exist? "#{msa_100_dir}/#{dsx_output}"
172           run_100 = true
173           FileUtils.cp "#{dsx_output}.fasta", "#{msa_100_dir}/#{dsx_output}"
174         end
175
176         if File.exist?( TEMPLATE_FILE )
177           if run_1
178             FileUtils.cp TEMPLATE_FILE, msa_dir
179           end
180           if run_100
181             FileUtils.cp TEMPLATE_FILE, msa_100_dir
182           end
183
184           if LAUNCH_ANALYSIS
185             puts "f. analysis:"
186             if run_1
187               Dir.chdir msa_dir
188               run_command "#{PF} -b=1 -s"
189               Dir.chdir "../.."
190             end
191             if run_100
192               Dir.chdir msa_100_dir
193               run_command "#{PF} -b=100 -s"
194               Dir.chdir "../.."
195             end
196             puts
197           end
198         end
199
200       end
201
202     end
203
204     def run_command cmd
205       puts cmd
206       `#{cmd}`
207     end
208
209     def get_base_name n
210       if n.downcase.end_with?( "_ni.fasta" )
211         n[ 0 .. n.length - 10 ]
212       elsif n.downcase.end_with?( ".fasta" )
213         n[ 0 .. n.length - 7 ]
214       else
215         error "illegal name: " + n
216       end
217     end
218
219     def error msg
220       puts
221       puts msg
222       puts
223       exit
224     end
225
226   end
227
228   p = RunPhyloPipeline.new()
229
230   p.run()
231
232 end
233
234
235