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 $
12 require 'lib/evo/util/constants'
13 require 'lib/evo/util/util'
14 require 'lib/evo/util/command_line_arguments'
15 require 'lib/evo/io/parser/hmmscan_parser'
23 PRG_DESC = "hmmscan summary"
25 COPYRIGHT = "2013 Christian M Zmasek"
26 CONTACT = "phyloxml@gmail.com"
27 WWW = "https://sites.google.com/site/cmzmasek/home/software/forester"
29 DELIMITER_OPTION = "d"
31 I_E_VALUE_THRESHOLD_OPTION = "ie"
32 FS_E_VALUE_THRESHOLD_OPTION = "pe"
33 HMM_FOR_PROTEIN_OUTPUT = "m"
34 IGNORE_DUF_OPTION = "i"
35 PARSE_OUT_DESCRIPITION_OPTION = "a"
36 HELP_OPTION_1 = "help"
40 AVOID_HHMS = [ "RRM_1", "RRM_2", "RRM_3", "RRM_4", "RRM_5", "RRM_6" ]
41 LIMIT_FOR_CLOSE_DOMAINS = 20
44 @domain_counts = Hash.new
49 Util.print_program_information( PRG_NAME,
59 cla = CommandLineArguments.new( ARGV )
60 rescue ArgumentError => e
61 Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
64 if ( cla.is_option_set?( HELP_OPTION_1 ) ||
65 cla.is_option_set?( HELP_OPTION_2 ) )
70 if ( cla.get_number_of_files != 2 )
75 allowed_opts = Array.new
76 allowed_opts.push( DELIMITER_OPTION )
77 allowed_opts.push( I_E_VALUE_THRESHOLD_OPTION )
78 allowed_opts.push( FS_E_VALUE_THRESHOLD_OPTION )
79 allowed_opts.push( IGNORE_DUF_OPTION )
80 allowed_opts.push( PARSE_OUT_DESCRIPITION_OPTION )
81 allowed_opts.push( HMM_FOR_PROTEIN_OUTPUT )
82 allowed_opts.push( SPECIES_OPTION )
84 disallowed = cla.validate_allowed_options_as_str( allowed_opts )
85 if ( disallowed.length > 0 )
86 Util.fatal_error( PRG_NAME,
87 "unknown option(s): " + disallowed,
91 inpath = cla.get_file_name( 0 )
92 outpath = cla.get_file_name( 1 )
94 column_delimiter = "\t"
95 if ( cla.is_option_set?( DELIMITER_OPTION ) )
97 column_delimiter = cla.get_option_value( DELIMITER_OPTION )
98 rescue ArgumentError => e
99 Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
103 i_e_value_threshold = -1.0
104 if ( cla.is_option_set?( I_E_VALUE_THRESHOLD_OPTION ) )
106 i_e_value_threshold = cla.get_option_value_as_float( I_E_VALUE_THRESHOLD_OPTION )
107 rescue ArgumentError => e
108 Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
110 if ( i_e_value_threshold < 0.0 )
111 Util.fatal_error( PRG_NAME, "attempt to use a negative i-E-value threshold", STDOUT )
115 fs_e_value_threshold = -1.0
116 if ( cla.is_option_set?( FS_E_VALUE_THRESHOLD_OPTION ) )
118 fs_e_value_threshold = cla.get_option_value_as_float( FS_E_VALUE_THRESHOLD_OPTION )
119 rescue ArgumentError => e
120 Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
122 if ( fs_e_value_threshold < 0.0 )
123 Util.fatal_error( PRG_NAME, "attempt to use a negative E-value threshold", STDOUT )
127 hmm_for_protein_output = ""
128 if ( cla.is_option_set?( HMM_FOR_PROTEIN_OUTPUT ) )
130 hmm_for_protein_output = cla.get_option_value( HMM_FOR_PROTEIN_OUTPUT )
131 rescue ArgumentError => e
132 Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
137 if ( cla.is_option_set?( SPECIES_OPTION ) )
139 species = cla.get_option_value( SPECIES_OPTION )
140 rescue ArgumentError => e
141 Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
146 if ( cla.is_option_set?( IGNORE_DUF_OPTION ) )
150 parse_descriptions = false
151 if ( cla.is_option_set?( PARSE_OUT_DESCRIPITION_OPTION ) )
152 parse_descriptions = true
156 puts( "hmmpfam outputfile : " + inpath )
157 puts( "outputfile : " + outpath )
158 puts( "species : " + species )
159 if ( i_e_value_threshold >= 0.0 )
160 puts( "i-E-value threshold : " + i_e_value_threshold.to_s )
162 puts( "i-E-value threshold : no threshold" )
164 if ( parse_descriptions )
165 puts( "parse descriptions : true" )
167 puts( "parse descriptions : false" )
170 puts( "ignore DUFs : true" )
172 puts( "ignore DUFs : false" )
174 if ( column_delimiter == "\t" )
175 puts( "column delimiter : TAB" )
177 puts( "column delimiter : " + column_delimiter )
179 if fs_e_value_threshold >= 0.0
180 puts( "E-value threshold : " + fs_e_value_threshold.to_s )
182 puts( "E-value threshold : no threshold" )
184 if !hmm_for_protein_output.empty?
185 puts( "HMM for proteins : " + hmm_for_protein_output )
196 fs_e_value_threshold,
197 hmm_for_protein_output,
200 Util.fatal_error( PRG_NAME, "error: " + e.to_s, STDOUT )
202 domain_counts = get_domain_counts()
205 puts( "domain counts (considering potential i-E-value threshold and ignoring of DUFs):" )
206 puts( "(number of different domains: " + domain_counts.length.to_s + ")" )
208 puts( Util.draw_histogram( domain_counts, "#" ) )
210 Util.print_message( PRG_NAME, 'OK' )
217 # raises ArgumentError, IOError
224 fs_e_value_threshold,
225 hmm_for_protein_output,
228 Util.check_file_for_readability( inpath )
229 Util.check_file_for_writability( outpath )
231 hmmscan_parser = HmmscanParser.new( inpath )
232 results = hmmscan_parser.parse
234 outfile = File.open( outpath, "a" )
243 hmmscan_results_per_protein = []
247 results.each do | r |
250 i_e_value = r.i_e_value
251 env_from = r.env_from
254 if ( ( i_e_value_threshold < 0.0 ) || ( i_e_value <= i_e_value_threshold ) ) &&
255 ( !ignore_dufs || ( model !~ /^DUF\d+/ ) )
257 outfile.print( query +
259 if ( get_descriptions )
260 outfile.print( desc +
263 outfile.print( model +
270 outfile.print( Constants::LINE_DELIMITER )
273 if !hmm_for_protein_output.empty?
274 if !prev_query.empty? && prev_query != query
275 if !hmmscan_results_per_protein.empty?
276 process_hmmscan_results_per_protein( hmmscan_results_per_protein,
277 fs_e_value_threshold,
278 hmm_for_protein_output,
282 hmmscan_results_per_protein.clear
287 if !AVOID_HHMS.include? r.model
288 hmmscan_results_per_protein << r
291 hmmscan_results_per_protein << r
296 if !hmm_for_protein_output.empty? && !hmmscan_results_per_protein.empty?
297 process_hmmscan_results_per_protein( hmmscan_results_per_protein,
298 fs_e_value_threshold,
299 hmm_for_protein_output,
309 if id =~ /(sp|tr)\|\S+\|(\S+)/
315 def count_model( model )
316 if ( @domain_counts.has_key?( model ) )
317 count = @domain_counts[ model ].to_i
319 @domain_counts[ model ] = count
321 @domain_counts[ model ] = 1
325 def process_hmmscan_results_per_protein( hmmscan_results_per_protein,
326 fs_e_value_threshold,
327 hmm_for_protein_output,
332 # filter according to i-Evalue threshold
333 # abort if fs Evalue too high
334 hmmscan_results_per_protein_filtered = []
336 hmmscan_results_per_protein.each do | r |
339 if r.model == hmm_for_protein_output
340 if fs_e_value_threshold > 0.0 && r.fs_e_value > fs_e_value_threshold
344 if i_e_value_threshold <= 0 || r.i_e_value <= i_e_value_threshold
345 hmmscan_results_per_protein_filtered << r
346 if r.model == hmm_for_protein_output
353 # passed on protein E-value, failed in per domain E-values
357 hmmscan_results_per_protein_filtered.sort! { |r1,r2| r1.env_from <=> r2.env_from }
360 hmmscan_results_per_protein_filtered.each do | r |
361 if r.model == hmm_for_protein_output
367 s << own.query + "\t"
369 s << own.fs_e_value.to_s + "\t"
370 s << own.qlen.to_s + "\t"
372 s << hmmscan_results_per_protein_filtered.length.to_s + "\t"
373 hmmscan_results_per_protein_filtered.each do | r |
378 overview = make_overview( hmmscan_results_per_protein_filtered, hmm_for_protein_output )
382 s << calc_linkers( hmmscan_results_per_protein_filtered, hmm_for_protein_output ) + "\t"
385 hmmscan_results_per_protein_filtered.each do | r |
387 s << make_interdomain_sequence( r.env_from - prev_r.env_to - 1 )
389 s << make_interdomain_sequence( r.env_from, false )
393 s << r.env_from.to_s << "-" << r.env_to.to_s
394 s << "|ie=" << r.i_e_value.to_s
395 s << "|ce=" << r.c_e_value.to_s
399 s << make_interdomain_sequence( own.qlen - prev_r.env_to, false )
403 def calc_linkers( hmmscan_results_per_protein_filtered, hmm_for_protein_output )
406 hmmscan_results_per_protein_filtered.each do | r |
407 if r.model == hmm_for_protein_output
409 linkers << ( r.env_from - prev_r.env_to - 1 ).to_s + " "
417 def get_domain_counts()
418 return @domain_counts
421 def make_overview( hmmscan_results_per_protein_filtered, hmm_for_protein_output )
424 hmmscan_results_per_protein_filtered.each do | r |
425 if r.model == hmm_for_protein_output
427 overview << hmm_for_protein_output
429 if ( r.env_from - prev_r.env_to - 1 ) <= LIMIT_FOR_CLOSE_DOMAINS
430 overview << "~" << hmm_for_protein_output
432 overview << "----" << hmm_for_protein_output
441 def make_interdomain_sequence( d, mark_short = true )
460 puts( " " + PRG_NAME + ".rb [options] <hmmscan outputfile> <outputfile>" )
462 puts( " options: -" + DELIMITER_OPTION + ": column delimiter for outputfile, default is TAB" )
463 puts( " -" + I_E_VALUE_THRESHOLD_OPTION + ": i-E-value threshold, default is no threshold" )
464 puts( " -" + PARSE_OUT_DESCRIPITION_OPTION + ": parse query description (in addition to query name)" )
465 puts( " -" + IGNORE_DUF_OPTION + ": ignore DUFs" )
466 puts( " -" + FS_E_VALUE_THRESHOLD_OPTION + ": E-value threshold for full protein sequences, only for protein summary" )
467 puts( " -" + HMM_FOR_PROTEIN_OUTPUT + ": HMM for protein summary" )
468 puts( " -" + SPECIES_OPTION + ": species for protein summary" )