in progress
[jalview.git] / forester / ruby / evoruby / lib / evo / apps / domain_sequence_extractor_new.rb
1 #
2 # = lib/evo/apps/domain_sequence_extractor.rb - DomainSequenceExtractor class
3 #
4 # Copyright::  Copyright (C) 2012 Christian M. Zmasek
5 # License::    GNU Lesser General Public License (LGPL)
6 #
7 # $Id:Exp $
8
9
10 require 'lib/evo/util/constants'
11 require 'lib/evo/util/util'
12 require 'lib/evo/util/command_line_arguments'
13 require 'lib/evo/io/parser/hmmscan_domain_extractor'
14
15 module Evoruby
16
17   class DomainSequenceExtractor
18
19     PRG_NAME       = "dsx"
20     PRG_VERSION    = "1.000"
21     PRG_DESC       = "extraction of domain sequences from hmmscan output"
22     PRG_DATE       = "20120906"
23     COPYRIGHT      = "2012 Christian M Zmasek"
24     CONTACT        = "phylosoft@gmail.com"
25     WWW            = "www.phylosoft.org"
26
27     E_VALUE_THRESHOLD_OPTION           = 'e'
28     LENGTH_THRESHOLD_OPTION            = 'l'
29     ADD_POSITION_OPTION                = 'p'
30     ADD_DOMAIN_NUMBER_OPTION           = 'd'
31     ADD_DOMAIN_NUMBER_OPTION_AS_DIGIT  = 'dd'
32     ADD_DOMAIN_NUMBER_OPTION_AS_LETTER = 'dl'
33     ADD_SPECIES                        = 's'
34     TRIM_OPTION                        = 't'
35     LOG_FILE_SUFFIX                    = '_domain_seq_extr.log'
36     PASSED_SEQS_SUFFIX                 = '_domain_seq_extr_passed'
37     FAILED_SEQS_SUFFIX                 = '_domain_seq_extr_failed'
38     HELP_OPTION_1                      = 'help'
39     HELP_OPTION_2                      = 'h'
40
41     def run()
42
43       Util.print_program_information( PRG_NAME,
44         PRG_VERSION,
45         PRG_DESC ,
46         PRG_DATE,
47         COPYRIGHT,
48         CONTACT,
49         WWW,
50         STDOUT )
51
52       ld = Constants::LINE_DELIMITER
53
54       begin
55         cla = CommandLineArguments.new( ARGV )
56       rescue ArgumentError
57         Util.fatal_error( PRG_NAME, "error: " + $!, STDOUT )
58       end
59
60       if ( cla.is_option_set?( HELP_OPTION_1 ) ||
61            cla.is_option_set?( HELP_OPTION_2 ) )
62         print_help
63         exit( 0 )
64       end
65
66       if ( cla.get_number_of_files != 4 )
67         print_help
68         exit( -1 )
69       end
70
71       allowed_opts = Array.new
72       allowed_opts.push( E_VALUE_THRESHOLD_OPTION )
73       allowed_opts.push( ADD_POSITION_OPTION )
74       allowed_opts.push( ADD_DOMAIN_NUMBER_OPTION )
75       allowed_opts.push( LENGTH_THRESHOLD_OPTION )
76       allowed_opts.push( ADD_DOMAIN_NUMBER_OPTION_AS_DIGIT )
77       allowed_opts.push( ADD_DOMAIN_NUMBER_OPTION_AS_LETTER )
78       allowed_opts.push( TRIM_OPTION )
79
80       disallowed = cla.validate_allowed_options_as_str( allowed_opts )
81       if ( disallowed.length > 0 )
82         Util.fatal_error( PRG_NAME,
83           "unknown option(s): " + disallowed,
84           STDOUT )
85       end
86
87       domain_id           = cla.get_file_name( 0 )
88       hmmsearch_output    = cla.get_file_name( 1 )
89       fasta_sequence_file = cla.get_file_name( 2 )
90       outfile             = cla.get_file_name( 3 )
91
92       add_position = false
93       if ( cla.is_option_set?( ADD_POSITION_OPTION ) )
94         add_position = true
95       end
96
97       trim = false
98       if ( cla.is_option_set?( TRIM_OPTION ) )
99         trim = true
100       end
101
102       add_domain_number           = false
103       add_domain_number_as_letter = false
104       add_domain_number_as_digit  = false
105
106       if ( cla.is_option_set?( ADD_DOMAIN_NUMBER_OPTION ) )
107         add_domain_number = true
108       end
109       if ( cla.is_option_set?( ADD_DOMAIN_NUMBER_OPTION_AS_LETTER ) )
110         add_domain_number_as_letter = true
111       end
112       if ( cla.is_option_set?( ADD_DOMAIN_NUMBER_OPTION_AS_DIGIT ) )
113         add_domain_number_as_digit = true
114       end
115
116       add_species = false
117       if cla.is_option_set? ADD_SPECIES 
118         add_species = true
119       end
120       
121       if ( add_domain_number_as_letter && add_domain_number_as_digit )
122         puts( "attempt to add domain number as letter and digit at the same time" )
123         print_help
124         exit( -1 )
125       end
126
127       e_value_threshold = -1.0
128       if ( cla.is_option_set?( E_VALUE_THRESHOLD_OPTION ) )
129         begin
130           e_value_threshold = cla.get_option_value_as_float( E_VALUE_THRESHOLD_OPTION )
131         rescue ArgumentError => e
132           Forester::Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
133         end
134         if ( e_value_threshold < 0.0 )
135           Forester::Util.fatal_error( PRG_NAME, "attempt to use a negative E-value threshold", STDOUT )
136         end
137       end
138
139       length_threshold = -1
140       if ( cla.is_option_set?( LENGTH_THRESHOLD_OPTION ) )
141         begin
142           length_threshold = cla.get_option_value_as_int( LENGTH_THRESHOLD_OPTION )
143         rescue ArgumentError => e
144           Forester::Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
145         end
146         if ( length_threshold < 0)
147           Forester::Util.fatal_error( PRG_NAME, "attempt to use a negative length threshold", STDOUT )
148         end
149       end
150
151       log = String.new
152
153       puts()
154       puts( "Domain                                 : " + domain_id )
155       log << "Domain                                 : " + domain_id + ld
156       puts( "Hmmscan outputfile                     : " + hmmsearch_output )
157       log << "Hmmscan outputfile                     : " + hmmsearch_output + ld
158       puts( "Fasta sequencefile (complete sequences): " + fasta_sequence_file )
159       log << "Fasta sequencefile (complete sequences): " + fasta_sequence_file + ld
160       puts( "Outputfile                             : " + outfile )
161       log << "Outputfile                             : " + outfile + ld
162       puts( "Passed sequences outfile (fasta)       : " + outfile + PASSED_SEQS_SUFFIX )
163       log << "Passed sequences outfile (fasta)       : " + outfile + PASSED_SEQS_SUFFIX + ld
164       puts( "Failed sequences outfile (fasta)       : " + outfile + FAILED_SEQS_SUFFIX )
165       log << "Failed sequences outfile (fasta)       : " + outfile + FAILED_SEQS_SUFFIX + ld
166       puts( "Logfile                                : " + outfile + LOG_FILE_SUFFIX )
167       log <<  "Logfile                                : " + outfile + LOG_FILE_SUFFIX + ld
168       if ( e_value_threshold >= 0.0 )
169         puts( "E-value threshold : " + e_value_threshold.to_s )
170         log << "E-value threshold : " + e_value_threshold.to_s + ld
171       else
172         puts( "E-value threshold : no threshold" )
173         log << "E-value threshold : no threshold" + ld
174       end
175       if ( length_threshold > 0 )
176         puts( "Length threshold  : " + length_threshold.to_s )
177         log << "Length threshold  : " + length_threshold.to_s + ld
178       else
179         puts( "Length threshold  : no threshold" )
180         log << "Length threshold  : no threshold" + ld
181       end
182
183       if ( trim )
184         puts( "Trim last 2 chars : true" )
185         log << "Trim last 2 chars : true" + ld
186       else
187         puts( "Trim names        : false" )
188         log << "Trim names        : false" + ld
189       end
190
191
192       if ( add_position )
193         puts( "Add positions (rel to complete seq) to extracted domains: true" )
194         log << "Add positions (rel to complete seq) to extracted domains: true" + ld
195       else
196         puts( "Add positions (rel to complete seq) to extracted domains: false" )
197         log << "Add positions (rel to complete seq) to extracted domains: false" + ld
198       end
199
200       if ( add_domain_number || add_domain_number_as_digit || add_domain_number_as_letter )
201         puts( "Add numbers to extracted domains (in case of more than one domain per complete seq): true" )
202         log << "Add numbers to extracted domains (in case of more than one domain per complete seq): true" + ld
203       else
204         puts( "Add numbers to extracted domains (in case of more than one domain per complete seq): false" )
205         log << "Add numbers to extracted domains (in case of more than one domain per complete seq): false" + ld
206       end
207
208       puts
209
210       domain_count = 0
211       begin
212         parser = HmmscanDomainExtractor.new()
213         domain_count = parser.parse( domain_id,
214           hmmsearch_output,
215           fasta_sequence_file,
216           outfile,
217           outfile + PASSED_SEQS_SUFFIX,
218           outfile + FAILED_SEQS_SUFFIX,
219           e_value_threshold,
220           length_threshold,
221           add_position,
222           add_domain_number,
223           add_domain_number_as_digit,
224           add_domain_number_as_letter,
225           trim,
226            add_species,
227           log )
228       rescue ArgumentError, IOError, StandardError => e
229         Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
230       rescue Exception => e
231         Util.fatal_error( PRG_NAME, "unexpected exception!: " + e.to_s, STDOUT )
232       end
233
234       puts
235       Util.print_message( PRG_NAME, "extracted a total of " + domain_count.to_s + " domains" )
236       Util.print_message( PRG_NAME, "wrote;               " + outfile )
237       Util.print_message( PRG_NAME, "wrote:               " + outfile + LOG_FILE_SUFFIX )
238       Util.print_message( PRG_NAME, "(wrote:              " + outfile + PASSED_SEQS_SUFFIX + ")" )
239       Util.print_message( PRG_NAME, "(wrote:              " + outfile + FAILED_SEQS_SUFFIX + ")" )
240
241       begin
242         f = File.open( outfile + LOG_FILE_SUFFIX, 'a' )
243         f.print( log )
244         f.close
245       rescue Exception => e
246         Util.fatal_error( PRG_NAME, "error: " + e.to_s )
247       end
248
249       puts
250       Util.print_message( PRG_NAME, "OK" )
251       puts
252
253     end
254
255     def print_help()
256       puts()
257       puts( "Usage:" )
258       puts()
259       puts( "  " + PRG_NAME + ".rb [options] <domain> <hmmscan outputfile> <file containing complete sequences in fasta format> <outputfile>" )
260       puts()
261       puts( "  options: -" + E_VALUE_THRESHOLD_OPTION  + "=<f>: E-value threshold, default is no threshold" )
262       puts( "           -" + LENGTH_THRESHOLD_OPTION   + "=<i>: length threshold, default is no threshold" )
263       puts( "           -" + ADD_POSITION_OPTION  + ": to add positions (rel to complete seq) to extracted domains" )
264       puts( "           -" + ADD_DOMAIN_NUMBER_OPTION  + ": to add numbers to extracted domains (in case of more than one domain per complete seq) (example \"domain~2-3\")" )
265       puts( "           -" + ADD_DOMAIN_NUMBER_OPTION_AS_DIGIT  + ": to add numbers to extracted domains as digit (example \"domain2\")" )
266       puts( "           -" + ADD_DOMAIN_NUMBER_OPTION_AS_LETTER  + ": to add numbers to extracted domains as letter (example \"domaina\")" )
267       puts( "           -" + TRIM_OPTION  + ": to remove the last 2 characters from sequence names" )
268       puts( "           -" + ADD_SPECIES  + ": to add species [in brackets]" )
269       puts()
270     end
271
272   end # class DomainSequenceExtractor
273
274 end # module Evoruby