2 # = lib/evo/io/parser/hmmscan_domain_extractor.rb - HmmscanDomainExtractor 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/msa/msa_factory'
12 require 'lib/evo/io/msa_io'
13 require 'lib/evo/io/writer/fasta_writer'
14 require 'lib/evo/io/parser/fasta_parser'
19 class HmmscanDomainExtractor
24 # raises ArgumentError, IOError, StandardError
39 Util.check_file_for_readability( hmmscan_output )
40 Util.check_file_for_readability( fasta_sequence_file )
41 Util.check_file_for_writability( outfile + ".fasta" )
42 Util.check_file_for_writability( passed_seqs_outfile )
43 Util.check_file_for_writability( failed_seqs_outfile )
46 factory = MsaFactory.new()
47 in_msa = factory.create_msa_from_file( fasta_sequence_file, FastaParser.new() )
49 if ( in_msa == nil || in_msa.get_number_of_seqs() < 1 )
50 error_msg = "could not find fasta sequences in " + fasta_sequence_file
51 raise IOError, error_msg
59 out_msa_isolated = nil
62 out_msa_pairs = Msa.new
63 out_msa_isolated = Msa.new
64 out_msa_singles = Msa.new
67 ld = Constants::LINE_DELIMITER
69 domain_pass_counter = 0
70 domain_fail_counter = 0
71 proteins_with_failing_domains = 0
72 max_domain_copy_number_per_protein = -1
73 max_domain_copy_number_sequence = ""
75 hmmscan_datas = Array.new
77 File.open( hmmscan_output ) do | file |
78 while line = file.gets
79 if !is_ignorable?( line ) && line =~ /^\S+\s+/
81 # tn acc tlen query acc qlen Evalue score bias # of c-E i-E score bias hf ht af at ef et acc desc
82 # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
83 line =~ /^(\S+)\s+(\S+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(.*)/
96 if ( ( ( e_value_threshold < 0.0 ) || ( i_e_value <= e_value_threshold ) ) &&
97 ( ( length_threshold <= 0 ) || ( env_to - env_from + 1 ) >= length_threshold.to_f ) )
98 hmmscan_datas << HmmsearchData.new( sequence, number, out_of, env_from, env_to, i_e_value )
99 if ( number > max_domain_copy_number_per_protein )
100 max_domain_copy_number_sequence = sequence
101 max_domain_copy_number_per_protein = number
104 print( domain_fail_counter.to_s + ": " + sequence.to_s + " did not meet threshold(s)" )
105 log << domain_fail_counter.to_s + ": " + sequence.to_s + " did not meet threshold(s)"
106 if ( ( e_value_threshold.to_f >= 0.0 ) && ( i_e_value > e_value_threshold ) )
107 print( " iE=" + i_e_value.to_s )
108 log << " iE=" + i_e_value.to_s
110 if ( ( length_threshold.to_f > 0 ) && ( env_to - env_from + 1 ) < length_threshold.to_f )
111 le = env_to - env_from + 1
112 print( " l=" + le.to_s )
113 log << " l=" + le.to_s
117 domain_fail_counter += 1
121 error_msg = "number > out_of ! (this should not have happened)"
122 raise StandardError, error_msg
126 if !hmmscan_datas.empty?
127 process_hmmscan_datas( hmmscan_datas,
137 domain_pass_counter += hmmscan_datas.length
138 if passed_seqs.find_by_name_start( sequence, true ).length < 1
139 add_sequence( sequence, in_msa, passed_seqs )
141 error_msg = "this should not have happened"
142 raise StandardError, error_msg
145 if failed_seqs.find_by_name_start( sequence, true ).length < 1
146 add_sequence( sequence, in_msa, failed_seqs )
147 proteins_with_failing_domains += 1
149 error_msg = "this should not have happened"
150 raise StandardError, error_msg
157 end # while line = file.gets
158 end # File.open( hmmsearch_output ) do | file |
162 if domain_pass_counter < 1
163 error_msg = "no domain sequences were extracted"
164 raise IOError, error_msg
168 puts( "Max domain copy number per protein : " + max_domain_copy_number_per_protein.to_s )
169 log << "Max domain copy number per protein : " + max_domain_copy_number_per_protein.to_s
172 if ( max_domain_copy_number_per_protein > 1 )
173 puts( "First protein with this copy number: " + max_domain_copy_number_sequence )
174 log << "First protein with this copy number: " + max_domain_copy_number_sequence
178 write_msa( out_msa, outfile + ".fasta" )
179 write_msa( passed_seqs, passed_seqs_outfile )
180 write_msa( failed_seqs, failed_seqs_outfile )
183 write_msa( out_msa_pairs, outfile +"_" + min_linker.to_s + ".fasta")
187 write_msa( out_msa_singles, outfile +"_singles" + ".fasta")
191 write_msa( out_msa_isolated, outfile + "_" + min_linker.to_s + "_isolated" + ".fasta");
196 log << "passing domains : " + domain_pass_counter.to_s + ld
198 log << "single domains : " + out_msa_singles.get_number_of_seqs.to_s + ld
199 log << "domains in close pairs : " + out_msa_pairs.get_number_of_seqs.to_s + ld
200 log << "isolated domains : " + out_msa_isolated.get_number_of_seqs.to_s + ld
202 log << "failing domains : " + domain_fail_counter.to_s + ld
203 log << "proteins with passing domains : " + passed_seqs.get_number_of_seqs.to_s + ld
204 log << "proteins with no passing domains: " + proteins_with_failing_domains.to_s + ld
207 return domain_pass_counter
214 def write_msa( msa, filename )
216 w = FastaWriter.new()
217 w.set_line_width( 60 )
220 io.write_to_file( msa, filename, w )
222 error_msg = "could not write to \"" + filename + "\""
223 raise IOError, error_msg
228 def add_sequence( sequence_name, in_msa, add_to_msa )
229 seqs = in_msa.find_by_name_start( sequence_name, true )
230 if ( seqs.length < 1 )
231 error_msg = "sequence \"" + sequence_name + "\" not found in sequence file"
232 raise StandardError, error_msg
234 if ( seqs.length > 1 )
235 error_msg = "sequence \"" + sequence_name + "\" not unique in sequence file"
236 raise StandardError, error_msg
238 seq = in_msa.get_sequence( seqs[ 0 ] )
239 add_to_msa.add_sequence( seq )
242 def process_hmmscan_datas( hmmscan_datas,
253 actual_out_of = hmmscan_datas.size
255 hmmscan_datas.each_with_index do |hmmscan_data, index|
256 if hmmscan_data.number < ( index + 1 )
257 error_msg = "hmmscan_data.number < ( index + 1 ) (this should not have happened)"
258 raise StandardError, error_msg
261 extract_domain( hmmscan_data.seq_name,
264 hmmscan_data.env_from,
273 if actual_out_of == 1
274 extract_domain( hmmscan_data.seq_name,
277 hmmscan_data.env_from,
286 last = index == hmmscan_datas.length - 1
288 if ( ( first && ( ( hmmscan_datas[ index + 1 ].env_from - hmmscan_data.env_to ) > min_linker) ) ||
289 ( last && ( ( hmmscan_data.env_from - hmmscan_datas[ index - 1 ].env_to ) > min_linker ) ) ||
290 ( !first && !last && ( ( hmmscan_datas[ index + 1 ].env_from - hmmscan_data.env_to ) > min_linker ) &&
291 ( ( hmmscan_data.env_from - hmmscan_datas[ index - 1 ].env_to ) > min_linker ) ) )
293 extract_domain( hmmscan_data.seq_name,
296 hmmscan_data.env_from,
305 extract_domain( hmmscan_data.seq_name,
306 index.to_s + "+" + ( index + 1 ).to_s,
308 hmmscan_datas[ index - 1 ].env_from,
319 end # def process_hmmscan_data
321 def extract_domain( sequence,
331 if number.is_a?( Fixnum ) && ( number < 1 || out_of < 1 || number > out_of )
332 error_msg = "impossible: number=" + number.to_s + ", out of=" + out_of.to_s
333 raise StandardError, error_msg
335 if seq_from < 1 || seq_to < 1 || seq_from >= seq_to
336 error_msg = "impossible: seq-f=" + seq_from.to_s + ", seq-t=" + seq_to.to_s
337 raise StandardError, error_msg
339 seqs = in_msa.find_by_name_start( sequence, true )
341 error_msg = "sequence \"" + sequence + "\" not found in sequence file"
342 raise IOError, error_msg
345 error_msg = "sequence \"" + sequence + "\" not unique in sequence file"
346 raise IOError, error_msg
348 # hmmsearch is 1 based, wheres sequences are 0 bases in this package.
349 seq = in_msa.get_sequence( seqs[ 0 ] ).get_subsequence( seq_from - 1, seq_to - 1 )
351 orig_name = seq.get_name
353 seq.set_name( orig_name.split[ 0 ] )
356 seq.set_name( seq.get_name + "_" + seq_from.to_s + "-" + seq_to.to_s )
359 if out_of != 1 && add_domain_number
360 seq.set_name( seq.get_name + "~" + number.to_s + "-" + out_of.to_s )
364 a = orig_name.rindex "["
365 b = orig_name.rindex "]"
367 error_msg = "species not found in " + orig_name
368 raise StandardError, error_msg
370 species = orig_name[ a .. b ]
371 seq.set_name( seq.get_name + " " + species )
373 out_msa.add_sequence( seq )
376 def is_ignorable?( line )
377 return ( line !~ /[A-Za-z0-9-]/ || line =~/^#/ )
380 end # class HmmscanDomainExtractor
383 def initialize( seq_name, number, out_of, env_from, env_to, i_e_value )
389 @i_e_value = i_e_value
391 attr_reader :seq_name, :number, :out_of, :env_from, :env_to, :i_e_value