2 # = lib/evo/tool/hmmscan_summary.rb - HmmscanSummary class
4 # Copyright:: Copyright (C) 2012 Christian M. Zmasek
5 # License:: GNU Lesser General Public License (LGPL)
7 # $Id: hmmscan_parser.rb,v 1.5 2010/12/13 19:00:11 cmzmasek Exp $
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_parser'
14 require 'lib/evo/msa/msa'
15 require 'lib/evo/msa/msa_factory'
16 require 'lib/evo/io/msa_io'
17 require 'lib/evo/io/parser/fasta_parser'
25 PRG_DESC = "hmmscan analysis"
27 COPYRIGHT = "2013 Christian M Zmasek"
28 CONTACT = "phyloxml@gmail.com"
29 WWW = "https://sites.google.com/site/cmzmasek/home/software/forester"
31 I_E_VALUE_THRESHOLD_OPTION = "ie"
32 FS_E_VALUE_THRESHOLD_OPTION = "pe"
35 HELP_OPTION_1 = "help"
39 AVOID_HHMS = [ "RRM_1", "RRM_2", "RRM_3", "RRM_4", "RRM_5", "RRM_6" ]
40 LIMIT_FOR_CLOSE_DOMAINS = 20
45 Util.print_program_information( PRG_NAME,
55 cla = CommandLineArguments.new( ARGV )
56 rescue ArgumentError => e
57 Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
60 if ( cla.is_option_set?( HELP_OPTION_1 ) ||
61 cla.is_option_set?( HELP_OPTION_2 ) )
66 if ( cla.get_number_of_files != 1 && cla.get_number_of_files != 3 )
71 allowed_opts = Array.new
72 allowed_opts.push( I_E_VALUE_THRESHOLD_OPTION )
73 allowed_opts.push( FS_E_VALUE_THRESHOLD_OPTION )
74 allowed_opts.push( TARGET_MODELS )
75 allowed_opts.push( EXTRACTION )
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 inpath = cla.get_file_name( 0 )
85 Util.check_file_for_readability( inpath )
87 extraction_output = nil
89 if ( cla.get_number_of_files == 3 )
90 seq_file_path = cla.get_file_name( 1 )
91 Util.check_file_for_readability( seq_file_path )
92 extraction_output = cla.get_file_name( 2 )
93 if File.exist?( extraction_output )
94 Util.fatal_error( PRG_NAME, "error: [#{extraction_output}] already exists" )
100 if seq_file_path != nil
101 msa = read_fasta_file( seq_file_path )
104 i_e_value_threshold = -1
105 if ( cla.is_option_set?( I_E_VALUE_THRESHOLD_OPTION ) )
107 i_e_value_threshold = cla.get_option_value_as_float( I_E_VALUE_THRESHOLD_OPTION )
108 rescue ArgumentError => e
109 Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
111 if ( i_e_value_threshold < 0.0 )
112 Util.fatal_error( PRG_NAME, "attempt to use a negative i-E-value threshold", STDOUT )
116 fs_e_value_threshold = -1
117 if ( cla.is_option_set?( FS_E_VALUE_THRESHOLD_OPTION ) )
119 fs_e_value_threshold = cla.get_option_value_as_float( FS_E_VALUE_THRESHOLD_OPTION )
120 rescue ArgumentError => e
121 Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
123 if ( fs_e_value_threshold < 0.0 )
124 Util.fatal_error( PRG_NAME, "attempt to use a negative E-value threshold", STDOUT )
129 if ( cla.is_option_set?( TARGET_MODELS ) )
131 hmm_for_protein_output = cla.get_option_value( TARGET_MODELS )
132 target_models = hmm_for_protein_output.split( "/" );
133 rescue ArgumentError => e
134 Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
139 if ( cla.is_option_set?( EXTRACTION ) )
141 hmm_for_protein_output = cla.get_option_value( EXTRACTION )
142 x_models = hmm_for_protein_output.split( "~" );
143 rescue ArgumentError => e
144 Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
151 fs_e_value_threshold,
157 Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
165 def read_fasta_file( input )
169 msa = f.create_msa_from_file( input, FastaParser.new() )
170 rescue Exception => e
171 Util.fatal_error( PRG_NAME, "error: " + e.to_s )
177 # raises ArgumentError, IOError
180 fs_e_value_threshold,
186 extraction_output_file = nil
187 if extraction_output != nil
188 extraction_output_file = File.open( extraction_output, "a" )
191 hmmscan_parser = HmmscanParser.new( inpath )
193 results = hmmscan_parser.parse
195 hmmscan_results_per_protein = []
199 results.each do | r |
201 if !prev_query.empty? && prev_query != query
202 if !hmmscan_results_per_protein.empty?
203 process_hmmscan_results_per_protein( hmmscan_results_per_protein,
204 fs_e_value_threshold,
209 extraction_output_file )
211 hmmscan_results_per_protein.clear
216 if !AVOID_HHMS.include? r.model
217 hmmscan_results_per_protein << r
220 hmmscan_results_per_protein << r
225 if !hmmscan_results_per_protein.empty?
226 process_hmmscan_results_per_protein( hmmscan_results_per_protein,
227 fs_e_value_threshold,
232 extraction_output_file )
234 if extraction_output_file != nil
235 extraction_output_file.close
240 def process_hmmscan_results_per_protein( hmmscan_results_per_protein,
241 fs_e_value_threshold,
246 extraction_output_file )
248 raise StandardError, "target hmms is empty" if target_hmms.length < 1
249 raise StandardError, "results is empty" if hmmscan_results_per_protein.length < 1
251 # filter according to i-Evalue threshold
252 # abort if fs Evalue too high
254 if fs_e_value_threshold >= 0.0
255 hmmscan_results_per_protein.each do | r |
256 target_hmms.each do | hmm |
257 if r.model == hmm && r.fs_e_value > fs_e_value_threshold
264 hmmscan_results_per_protein_filtered = []
267 hmmscan_results_per_protein.each do | r |
268 if i_e_value_threshold < 0 || r.i_e_value <= i_e_value_threshold
269 hmmscan_results_per_protein_filtered << r
270 target_hmms.each do | hmm |
280 if matched.length < target_hmms.length
283 if hmmscan_results_per_protein_filtered.length < 1
287 hmmscan_results_per_protein_filtered.sort! { |r1,r2| r1.env_from <=> r2.env_from }
290 target_hmms.each do | hmm |
291 hmmscan_results_per_protein_filtered.each do | r |
306 raise StandardError, "failed sanity check" if query != own.query || qlen != own.qlen
307 raise StandardError, "failed sanity check: qlen != own.qlen" if qlen != own.qlen
313 seq = get_sequence( query, msa )
314 species = get_species( seq )
315 raise StandardError, "could not get species" if species == nil || species.empty?
316 if x_models != nil && x_models.length > 0
317 extract_linkers( hmmscan_results_per_protein_filtered, x_models, seq, extraction_output_file )
324 s << own.fs_e_value.to_s + "\t"
327 s << qlen.to_s + "\t"
329 s << hmmscan_results_per_protein_filtered.length.to_s + "\t"
330 hmmscan_results_per_protein_filtered.each do | r |
334 s << make_overview_da( hmmscan_results_per_protein_filtered )
336 s << make_detailed_da( hmmscan_results_per_protein_filtered, qlen )
342 def extract_linkers( hmmscan_results_per_protein_filtered, x_models, seq, extraction_output_file )
343 raise StandardError, "extraction output file is nil" if extraction_output_file == nil
345 hmmscan_results_per_protein_filtered.each do | r |
347 if ( x_models.length == 2 && prev_r.model == x_models[ 0 ] && r.model == x_models[ 1 ] )
348 extract_linker( prev_r.env_to, r.env_from, seq, extraction_output_file )
356 def get_sequence( query, msa )
358 indices = msa.find_by_name_start( query , true )
359 if indices.length != 1
360 if query[ -1, 1 ] == "|"
363 seq = msa.get_by_name_pattern( /\b#{Regexp.quote(query)}\b/ )
365 seq = msa.get_sequence( indices[ 0 ] )
371 def get_species( seq )
373 if seq.get_name =~ /\[([A-Z0-9]{3,5})\]/
380 def extract_linker( first, last , seq, output_file )
381 if ( last - first >= 1 )
382 output_file.print( ">" + seq.get_name + " [" + first.to_s + "-" + last.to_s + "]" + "\n")
383 output_file.print( seq.get_subsequence( first - 1 , last - 1 ).get_sequence_as_string + "\n" )
388 def make_detailed_da( hmmscan_results_per_protein_filtered , qlen )
391 hmmscan_results_per_protein_filtered.each do | r |
393 s << make_interdomain_sequence( r.env_from - prev_r.env_to - 1 )
395 s << make_interdomain_sequence( r.env_from, false )
399 s << r.env_from.to_s << "-" << r.env_to.to_s
400 s << " " << r.i_e_value.to_s
404 s << make_interdomain_sequence( qlen - prev_r.env_to, false )
409 def make_overview_da( hmmscan_results_per_protein_filtered )
412 hmmscan_results_per_protein_filtered.each do | r |
416 if ( r.env_from - prev_r.env_to - 1 ) <= LIMIT_FOR_CLOSE_DOMAINS
417 overview << "~" << r.model
419 overview << "----" << r.model
429 def make_interdomain_sequence( d, mark_short = true )
448 puts( " " + PRG_NAME + ".rb [options] <hmmscan outputfile> <outputfile>" )
450 puts( " options: -" + I_E_VALUE_THRESHOLD_OPTION + ": i-E-value threshold, default is no threshold" )
451 puts( " -" + FS_E_VALUE_THRESHOLD_OPTION + ": E-value threshold for full protein sequences, only for protein summary" )
452 puts( " -" + TARGET_MODELS + ": target HMMs" )