2 # = lib/evo/apps/domain_sequence_extractor.rb - DomainSequenceExtractor class
4 # Copyright:: Copyright (C) 2012 Christian M. Zmasek
5 # License:: GNU Lesser General Public License (LGPL)
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'
17 class DomainSequenceExtractor
21 PRG_DESC = "extraction of domain sequences from hmmscan output"
23 COPYRIGHT = "2012 Christian M Zmasek"
24 CONTACT = "phylosoft@gmail.com"
25 WWW = "www.phylosoft.org"
27 E_VALUE_THRESHOLD_OPTION = 'e'
28 LENGTH_THRESHOLD_OPTION = 'l'
29 ADD_POSITION_OPTION = 'p'
30 ADD_DOMAIN_NUMBER_OPTION = 'd'
33 LOG_FILE_SUFFIX = '_domain_seq_extr.log'
34 PASSED_SEQS_SUFFIX = '_with_passing_domains.fasta'
35 FAILED_SEQS_SUFFIX = '_with_no_passing_domains.fasta'
36 HELP_OPTION_1 = 'help'
41 Util.print_program_information( PRG_NAME,
50 ld = Constants::LINE_DELIMITER
53 cla = CommandLineArguments.new( ARGV )
55 Util.fatal_error( PRG_NAME, "error: " + $!, STDOUT )
58 if ( cla.is_option_set?( HELP_OPTION_1 ) ||
59 cla.is_option_set?( HELP_OPTION_2 ) )
64 if ( cla.get_number_of_files != 4 )
69 allowed_opts = Array.new
70 allowed_opts.push( E_VALUE_THRESHOLD_OPTION )
71 allowed_opts.push( ADD_POSITION_OPTION )
72 allowed_opts.push( ADD_DOMAIN_NUMBER_OPTION )
73 allowed_opts.push( LENGTH_THRESHOLD_OPTION )
74 allowed_opts.push( ADD_SPECIES )
75 allowed_opts.push( MIN_LINKER_OPT )
77 disallowed = cla.validate_allowed_options_as_str( allowed_opts )
78 if ( disallowed.length > 0 )
79 Util.fatal_error( PRG_NAME,
80 "unknown option(s): " + disallowed,
84 domain_id = cla.get_file_name( 0 )
85 hmmsearch_output = cla.get_file_name( 1 )
86 fasta_sequence_file = cla.get_file_name( 2 )
87 outfile = cla.get_file_name( 3 )
89 if outfile.downcase.end_with?( ".fasta" )
90 outfile = outfile[ 0 .. outfile.length - 7 ]
91 elsif outfile.downcase.end_with?( ".fsa" )
92 outfile = outfile[ 0 .. outfile.length - 5 ]
97 if ( cla.is_option_set?( ADD_POSITION_OPTION ) )
101 add_domain_number = false
102 if ( cla.is_option_set?( ADD_DOMAIN_NUMBER_OPTION ) )
103 add_domain_number = true
107 if cla.is_option_set? ADD_SPECIES
111 e_value_threshold = -1.0
112 if ( cla.is_option_set?( E_VALUE_THRESHOLD_OPTION ) )
114 e_value_threshold = cla.get_option_value_as_float( E_VALUE_THRESHOLD_OPTION )
115 rescue ArgumentError => e
116 Forester::Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
118 if ( e_value_threshold < 0.0 )
119 Forester::Util.fatal_error( PRG_NAME, "attempt to use a negative E-value threshold", STDOUT )
123 length_threshold = -1
124 if ( cla.is_option_set?( LENGTH_THRESHOLD_OPTION ) )
126 length_threshold = cla.get_option_value_as_int( LENGTH_THRESHOLD_OPTION )
127 rescue ArgumentError => e
128 Forester::Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
130 if ( length_threshold < 0)
131 Forester::Util.fatal_error( PRG_NAME, "attempt to use a negative length threshold", STDOUT )
137 if ( cla.is_option_set?( MIN_LINKER_OPT ) )
139 min_linker = cla.get_option_value_as_int( MIN_LINKER_OPT )
140 rescue ArgumentError => e
141 Forester::Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
143 if ( !min_linker || min_linker > 100 || min_linker < -100 )
144 Forester::Util.fatal_error( PRG_NAME, "unexpected value for min linker " + min_linker.to_s, STDOUT )
152 puts( "Domain : " + domain_id )
153 log << "Domain : " + domain_id + ld
154 puts( "Hmmscan outputfile : " + hmmsearch_output )
155 log << "Hmmscan outputfile : " + hmmsearch_output + ld
156 puts( "Fasta sequencefile (complete sequences): " + fasta_sequence_file )
157 log << "Fasta sequencefile (complete sequences): " + fasta_sequence_file + ld
158 puts( "Outputfile : " + outfile + ".fasta" )
159 log << "Outputfile : " + outfile + ld
160 puts( "Passed sequences outfile (fasta) : " + outfile + PASSED_SEQS_SUFFIX )
161 log << "Passed sequences outfile (fasta) : " + outfile + PASSED_SEQS_SUFFIX + ld
162 puts( "Failed sequences outfile (fasta) : " + outfile + FAILED_SEQS_SUFFIX )
163 log << "Failed sequences outfile (fasta) : " + outfile + FAILED_SEQS_SUFFIX + ld
164 puts( "Logfile : " + outfile + LOG_FILE_SUFFIX )
165 log << "Logfile : " + outfile + LOG_FILE_SUFFIX + ld
166 if ( e_value_threshold >= 0.0 )
167 puts( "E-value threshold : " + e_value_threshold.to_s )
168 log << "E-value threshold : " + e_value_threshold.to_s + ld
170 puts( "E-value threshold : no threshold" )
171 log << "E-value threshold : no threshold" + ld
173 if ( length_threshold > 0 )
174 puts( "Length threshold : " + length_threshold.to_s )
175 log << "Length threshold : " + length_threshold.to_s + ld
177 puts( "Length threshold : no threshold" )
178 log << "Length threshold : no threshold" + ld
181 puts( "Min linker : " + min_linker.to_s )
182 log << "Min linker : " + min_linker.to_s + ld
188 puts( "Add positions (rel to complete seq) to extracted domains: true" )
189 log << "Add positions (rel to complete seq) to extracted domains: true" + ld
191 puts( "Add positions (rel to complete seq) to extracted domains: false" )
192 log << "Add positions (rel to complete seq) to extracted domains: false" + ld
195 if ( add_domain_number )
196 puts( "Add numbers to extracted domains (in case of more than one domain per complete seq): true" )
197 log << "Add numbers to extracted domains (in case of more than one domain per complete seq): true" + ld
199 puts( "Add numbers to extracted domains (in case of more than one domain per complete seq): false" )
200 log << "Add numbers to extracted domains (in case of more than one domain per complete seq): false" + ld
207 parser = HmmscanDomainExtractor.new()
208 domain_count = parser.parse( domain_id,
212 outfile + PASSED_SEQS_SUFFIX,
213 outfile + FAILED_SEQS_SUFFIX,
221 rescue ArgumentError, IOError => e
222 Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
224 rescue Exception => e
226 Util.fatal_error( PRG_NAME, "unexpected exception: " + e.to_s, STDOUT )
231 Util.print_message( PRG_NAME, "extracted a total of " + domain_count.to_s + " domains" )
232 Util.print_message( PRG_NAME, "wrote; " + outfile + ".fasta")
233 Util.print_message( PRG_NAME, "wrote: " + outfile + LOG_FILE_SUFFIX )
234 Util.print_message( PRG_NAME, "wrote: " + outfile + PASSED_SEQS_SUFFIX )
235 Util.print_message( PRG_NAME, "wrote: " + outfile + FAILED_SEQS_SUFFIX )
238 f = File.open( outfile + LOG_FILE_SUFFIX, 'a' )
241 rescue Exception => e
242 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
246 Util.print_message( PRG_NAME, "OK" )
255 puts( " " + PRG_NAME + ".rb [options] <domain> <hmmscan outputfile> <file containing complete sequences in fasta format> <outputfile>" )
257 puts( " options: -" + E_VALUE_THRESHOLD_OPTION + "=<f>: E-value threshold, default is no threshold" )
258 puts( " -" + LENGTH_THRESHOLD_OPTION + "=<i>: length threshold, default is no threshold" )
259 puts( " -" + ADD_POSITION_OPTION + ": to add positions (rel to complete seq) to extracted domains" )
260 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\")" )
261 puts( " -" + ADD_SPECIES + ": to add species [in brackets]" )
262 puts( " -" + MIN_LINKER_OPT + "=<i>: to extract pairs of same domains with a distance inbetween shorter than a given value" )
266 end # class DomainSequenceExtractor