2 # = lib/evo/io/parser/hmmsearch_domain_extractor.rb - HmmsearchDomainExtractor class
4 # Copyright:: Copyright (C) 2006-2008 Christian M. Zmasek
5 # License:: GNU Lesser General Public License (LGPL)
7 # $Id: hmmsearch_domain_extractor.rb,v 1.24 2009/11/25 06:30:24 cmzmasek Exp $
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 HmmsearchDomainExtractor
26 # raises ArgumentError, IOError, StandardError
27 def parse( hmmsearch_output,
36 add_domain_number_as_digit,
37 add_domain_number_as_letter,
41 Util.check_file_for_readability( hmmsearch_output )
42 Util.check_file_for_readability( fasta_sequence_file )
43 Util.check_file_for_writability( outfile )
44 Util.check_file_for_writability( passed_seqs_outfile )
45 Util.check_file_for_writability( failed_seqs_outfile )
48 factory = MsaFactory.new()
49 in_msa = factory.create_msa_from_file( fasta_sequence_file, FastaParser.new() )
51 if ( in_msa == nil || in_msa.get_number_of_seqs() < 1 )
52 error_msg = "could not find fasta sequences in " + fasta_sequence_file
53 raise IOError, error_msg
60 ld = Constants::LINE_DELIMITER
62 domain_pass_counter = 0
63 domain_fail_counter = 0
64 proteins_with_passing_domains = 0
65 proteins_with_failing_domains = 0
66 max_domain_copy_number_per_protein = -1
67 max_domain_copy_number_sequence = ''
68 failed_species_counts = Hash.new
69 passed_species_counts = Hash.new
71 File.open( hmmsearch_output ) do | file |
72 while line = file.gets
73 if !is_ignorable?( line ) && line =~ /^\S+\s+/
75 # tn acc tlen query acc qlen Evalue score bias # of c-E i-E score bias hf ht af at ef et acc desc
76 # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
77 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+(.*)/
85 if ( number > max_domain_copy_number_per_protein )
86 max_domain_copy_number_sequence = sequence
87 max_domain_copy_number_per_protein = number
89 if ( ( ( e_value_threshold.to_f < 0.0 ) || ( i_e_value <= e_value_threshold ) ) &&
90 ( ( length_threshold.to_f <= 0 ) || ( env_to - env_from + 1 ) >= length_threshold.to_f ) )
91 HmmsearchDomainExtractor.extract_domain( sequence,
100 add_domain_number_as_digit,
101 add_domain_number_as_letter,
103 domain_pass_counter += 1
104 count_species( sequence, passed_species_counts )
105 if !passed_seqs.has?( sequence, true, false )
106 HmmsearchDomainExtractor.add_sequence( sequence, in_msa, passed_seqs )
107 proteins_with_passing_domains += 1
110 print( domain_fail_counter.to_s + ": " + sequence.to_s + " did not meet threshold(s)" )
111 log << domain_fail_counter.to_s + ": " + sequence.to_s + " did not meet threshold(s)"
112 if ( ( e_value_threshold.to_f >= 0.0 ) && ( i_e_value > e_value_threshold ) )
113 print( " iE=" + i_e_value.to_s )
114 log << " iE=" + i_e_value.to_s
116 if ( ( length_threshold.to_f > 0 ) && ( env_to - env_from + 1 ) < length_threshold.to_f )
117 le = env_to - env_from + 1
118 print( " l=" + le.to_s )
119 log << " l=" + le.to_s
121 print( Constants::LINE_DELIMITER )
122 log << Constants::LINE_DELIMITER
123 domain_fail_counter += 1
124 count_species( sequence, failed_species_counts )
125 if !failed_seqs.has?( sequence, true, false )
126 HmmsearchDomainExtractor.add_sequence( sequence, in_msa, failed_seqs )
127 proteins_with_failing_domains += 1
134 if domain_pass_counter < 1
135 error_msg = "no domain sequences were extracted"
136 raise StandardError, error_msg
139 log << Constants::LINE_DELIMITER
140 puts( "Max domain copy number per protein : " + max_domain_copy_number_per_protein.to_s )
141 log << "Max domain copy number per protein : " + max_domain_copy_number_per_protein.to_s
142 log << Constants::LINE_DELIMITER
144 if ( max_domain_copy_number_per_protein > 1 )
145 puts( "First protein with this copy number: " + max_domain_copy_number_sequence )
146 log << "First protein with this copy number: " + max_domain_copy_number_sequence
147 log << Constants::LINE_DELIMITER
151 w = FastaWriter.new()
152 w.set_line_width( 60 )
156 io.write_to_file( out_msa, outfile, w )
158 error_msg = "could not write to \"" + outfile + "\""
159 raise IOError, error_msg
163 io.write_to_file( passed_seqs, passed_seqs_outfile, w )
165 error_msg = "could not write to \"" + passed_seqs_outfile + "\""
166 raise IOError, error_msg
170 io.write_to_file( failed_seqs, failed_seqs_outfile, w )
172 error_msg = "could not write to \"" + failed_seqs_outfile + "\""
173 raise IOError, error_msg
177 log << "passing domains : " + domain_pass_counter.to_s + ld
178 log << "failing domains : " + domain_fail_counter.to_s + ld
179 log << "proteins with passing domains: " + proteins_with_passing_domains.to_s + ld
180 log << "proteins with failing domains: " + proteins_with_failing_domains.to_s + ld
182 log << 'passing domains counts per species: ' << ld
183 passed_species_counts.each_pair { | species, count | log << "#{species}: #{count}" << ld }
185 log << 'failing domains counts per species: ' << ld
186 failed_species_counts.each_pair { | species, count | log << "#{species}: #{count}" << ld }
188 return domain_pass_counter
195 def HmmsearchDomainExtractor.add_sequence( sequence_name, in_msa, add_to_msa )
196 seqs = in_msa.find_by_name( sequence_name, true, false )
197 if ( seqs.length < 1 )
198 error_msg = "sequence \"" + sequence_name + "\" not found in sequence file"
199 raise StandardError, error_msg
201 if ( seqs.length > 1 )
202 error_msg = "sequence \"" + sequence_name + "\" not unique in sequence file"
203 raise StandardError, error_msg
205 seq = in_msa.get_sequence( seqs[ 0 ] )
206 add_to_msa.add_sequence( seq )
209 # raises ArgumentError, StandardError
210 def HmmsearchDomainExtractor.extract_domain( sequence,
219 add_domain_number_as_digit,
220 add_domain_number_as_letter,
222 if ( number < 1 || out_of < 1 || number > out_of )
223 error_msg = "impossible: number=" + number.to_s + ", out of=" + out_of.to_s
224 raise ArgumentError, error_msg
226 if ( seq_from < 1 || seq_to < 1 || seq_from >= seq_to )
227 error_msg = "impossible: seq-f=" + seq_from.to_s + ", seq-t=" + seq_to.to_s
228 raise ArgumentError, error_msg
230 seqs = in_msa.find_by_name( sequence, true, false )
231 if ( seqs.length < 1 )
232 error_msg = "sequence \"" + sequence + "\" not found in sequence file"
233 raise StandardError, error_msg
235 if ( seqs.length > 1 )
236 error_msg = "sequence \"" + sequence + "\" not unique in sequence file"
237 raise StandardError, error_msg
239 # hmmsearch is 1 based, wheres sequences are 0 bases in this package.
240 seq = in_msa.get_sequence( seqs[ 0 ] ).get_subsequence( seq_from - 1, seq_to - 1 )
242 seq.set_name( seq.get_name + "_" + seq_from.to_s + "-" + seq_to.to_s )
246 seq.set_name( seq.get_name[ 0, seq.get_name.length - TRIM_BY ] )
250 if ( add_domain_number_as_digit )
251 seq.set_name( seq.get_name + number.to_s )
252 elsif ( add_domain_number_as_letter )
254 error_msg = 'too many identical domains per sequence, cannot use letters to distinguish them'
255 raise StandardError, error_msg
257 seq.set_name( seq.get_name + ( number + 96 ).chr )
258 elsif ( add_domain_number )
259 seq.set_name( seq.get_name + "~" + number.to_s + "-" + out_of.to_s )
263 if ( seq.get_name.length > 10 )
264 error_msg = "sequence name [" + seq.get_name + "] is longer than 10 characters"
265 raise StandardError, error_msg
268 out_msa.add_sequence( seq )
271 def count_species( sequence, species_counts_map )
272 species = get_species( sequence )
274 if !species_counts_map.has_key?( species )
275 species_counts_map[ species ] = 1
277 species_counts_map[ species ] = species_counts_map[ species ] + 1
282 def get_species( sequence_name )
283 if sequence_name =~ /^.+_(.+)$/
290 def is_ignorable?( line )
291 return ( line !~ /[A-Za-z0-9-]/ || line =~/^#/ )
294 end # class HmmsearchDomainExtractor