2 # = lib/evo/msa/msa.rb - Msa class
4 # Copyright:: Copyright (C) 2006-2007 Christian M. Zmasek
5 # License:: GNU Lesser General Public License (LGPL)
7 # $Id: msa.rb,v 1.11 2009/01/03 00:42:08 cmzmasek Exp $
11 require 'lib/evo/util/constants'
12 require 'lib/evo/util/util'
13 require 'lib/evo/sequence/sequence'
20 @sequences = Array.new
21 @identical_seqs_detected = Array.new
22 @name_to_seq_indices = Hash.new
23 @namestart_to_seq_indices = Hash.new
27 def add_sequence( sequence )
28 @sequences.push( sequence )
31 def add( name, molecular_sequence_str )
32 add_sequence( Sequence.new( name, molecular_sequence_str ) )
35 def get_sequence( index )
36 if ( index < 0 || index > get_number_of_seqs() - 1 )
37 error_msg = "attempt to get sequence " <<
38 index.to_s << " in alignment of " << get_number_of_seqs().to_s <<
40 raise ArgumentError, error_msg
42 return @sequences[ index ]
45 def remove_sequence!( index )
46 if ( index < 0 || index > get_number_of_seqs() - 1 )
47 error_msg = "attempt to remove sequence " <<
48 index.to_s << " in alignment of " << get_number_of_seqs().to_s <<
50 raise ArgumentError, error_msg
52 @name_to_seq_indices.clear
53 @namestart_to_seq_indices.clear
54 @sequences.delete_at( index )
57 def get_identical_seqs_detected
58 @identical_seqs_detected
63 if ( get_number_of_seqs < 1 )
66 l = @sequences[ 0 ].get_length()
67 for i in 0 ... get_number_of_seqs()
68 if ( get_sequence( i ).get_length() != l )
76 def find_by_name( name, case_sensitive, partial_match )
77 if case_sensitive && !partial_match && @name_to_seq_indices.has_key?( name )
78 return @name_to_seq_indices[ name ]
81 for i in 0 ... get_number_of_seqs()
82 current_name = get_sequence( i ).get_name()
84 current_name = current_name.downcase
87 if current_name == name ||
88 ( partial_match && current_name.include?( name ) )
92 if case_sensitive && !partial_match
93 @name_to_seq_indices[ name ] = indices
98 def find_by_name_pattern( name_re, avoid_similar_to = true )
100 for i in 0 ... get_number_of_seqs()
102 m = name_re.match( get_sequence( i ).get_name() )
103 if m && !m.pre_match.downcase.include?( "similar to " )
107 if name_re.match( get_sequence( i ).get_name() )
115 # throws ArgumentError
116 def get_by_name_pattern( name_re , avoid_similar_to = true )
117 indices = find_by_name_pattern( name_re, avoid_similar_to )
118 if ( indices.length > 1 )
119 error_msg = "pattern " + name_re.to_s + " not unique"
120 raise ArgumentError, error_msg
121 elsif ( indices.length < 1 )
122 error_msg = "pattern " + name_re.to_s + " not found"
123 raise ArgumentError, error_msg
125 get_sequence( indices[ 0 ] )
128 def find_by_name_start( name, case_sensitive )
129 if case_sensitive && @namestart_to_seq_indices.has_key?( name )
130 return @namestart_to_seq_indices[ name ]
133 for i in 0 ... get_number_of_seqs()
134 get_sequence( i ).get_name() =~ /^\s*(\S+)/
137 current_name = current_name.downcase
140 if current_name == name
145 @namestart_to_seq_indices[ name ] = indices
150 def has?( name, case_sensitive = true, partial_match = false )
151 for i in 0 ... get_number_of_seqs()
152 current_name = get_sequence( i ).get_name()
154 current_name = current_name.downcase
157 if current_name == name ||
158 ( partial_match && current_name.include?( name ) )
165 # throws ArgumentError
166 def get_by_name( name, case_sensitive = true, partial_match = false )
167 indices = find_by_name( name, case_sensitive, partial_match )
168 if ( indices.length > 1 )
169 error_msg = "\"" + name + "\" not unique"
170 raise ArgumentError, error_msg
171 elsif ( indices.length < 1 )
172 error_msg = "\"" + name + "\" not found"
173 raise ArgumentError, error_msg
175 get_sequence( indices[ 0 ] )
178 # throws ArgumentError
179 def get_by_name_start( name, case_sensitive = true )
180 indices = find_by_name_start( name, case_sensitive )
181 if indices.length > 1
182 error_msg = "\"" + name + "\" not unique"
183 raise ArgumentError, error_msg
184 elsif indices.length < 1
185 error_msg = "\"" + name + "\" not found"
186 raise ArgumentError, error_msg
188 get_sequence( indices[ 0 ] )
192 def get_sub_alignment( seq_numbers )
194 for i in 0 ... seq_numbers.length()
195 msa.add_sequence( get_sequence( seq_numbers[ i ] ).copy() )
200 def get_number_of_seqs()
206 error_msg = "attempt to get length of unaligned msa"
207 raise StandardError, error_msg, caller
209 if get_number_of_seqs() < 1
212 @sequences[ 0 ].get_length()
222 for i in 0...get_number_of_seqs
223 s += @sequences[ i ].to_fasta + Constants::LINE_DELIMITER
229 def print_overlap_diagram( min_overlap = 1, io = STDOUT, max_name_length = 10 )
231 error_msg = "attempt to get overlap diagram of unaligned msa"
232 raise StandardError, error_msg, caller
234 for i in 0 ... get_number_of_seqs()
235 io.print( Util.normalize_seq_name( get_sequence( i ).get_name(), max_name_length ) )
236 for j in 0 ... get_number_of_seqs()
240 if overlap?( i, j, min_overlap )
247 io.print( Evoruby::Constants::LINE_DELIMITER )
251 #returns array of Msa with an overlap of min_overlap
252 def split_into_overlapping_msa( min_overlap = 1 )
254 error_msg = "attempt to split into overlapping msas of unaligned msa"
255 raise StandardError, error_msg, caller
258 bins = get_overlaps( min_overlap )
259 for i in 0 ... bins.length
260 msas.push( get_sub_alignment( bins[ i ] ) )
265 def overlap?( index_1, index_2, min_overlap = 1 )
266 seq_1 = get_sequence( index_1 )
267 seq_2 = get_sequence( index_2 )
269 for i in 0...seq_1.get_length()
270 if !Util.is_aa_gap_character?( seq_1.get_character_code( i ) ) &&
271 !Util.is_aa_gap_character?( seq_2.get_character_code( i ) )
273 if overlap_count >= min_overlap
281 def calculate_overlap( index_1, index_2 )
282 seq_1 = get_sequence( index_1 )
283 seq_2 = get_sequence( index_2 )
285 for i in 0...seq_1.get_length
286 if !Util.is_aa_gap_character?( seq_1.get_character_code( i ) ) &&
287 !Util.is_aa_gap_character?( seq_2.get_character_code( i ) )
294 def calculate_identities( index_1, index_2 )
295 seq_1 = get_sequence( index_1 )
296 seq_2 = get_sequence( index_2 )
298 for i in 0...seq_1.get_length
299 if !Util.is_aa_gap_character?( seq_1.get_character_code( i ) ) &&
300 !Util.is_aa_gap_character?( seq_2.get_character_code( i ) ) &&
301 seq_1.get_character_code( i ) != 63 &&
302 ( seq_1.get_residue( i ).downcase() ==
303 seq_2.get_residue( i ).downcase() )
304 identities_count += 1
310 def remove_gap_only_columns!()
311 remove_columns!( get_gap_only_columns() )
314 def remove_gap_columns!()
315 remove_columns!( get_gap_columns() )
318 # removes columns for which seqs with gap / number of sequences > gap_ratio
319 def remove_gap_columns_w_gap_ratio!( gap_ratio )
320 remove_columns!( get_gap_columns_w_gap_ratio( gap_ratio ) )
324 def remove_sequences_by_gap_ratio!( gap_ratio )
326 error_msg = "attempt to remove sequences by gap ratio on unaligned msa"
327 raise StandardError, error_msg, caller
329 n = get_number_of_seqs
332 if ( get_sequence( ( n - 1 ) - s ).get_gap_ratio() > gap_ratio )
333 if ( Evoruby::Constants::VERBOSE )
334 puts( "removed: " + get_sequence( ( n - 1 ) - s ).get_name )
336 removed << get_sequence( ( n - 1 ) - s ).get_name
337 remove_sequence!( ( n - 1 ) - s )
344 def remove_redundant_sequences!( consider_taxonomy = false, verbose = false )
345 n = get_number_of_seqs
347 to_be_removed = Set.new
348 @identical_seqs_detected = Array.new
349 for i in 0 ... ( n - 1 )
350 for j in ( i + 1 ) ... n
351 if !to_be_removed.include?( i ) && !to_be_removed.include?( j )
352 if !consider_taxonomy ||
353 ( ( get_sequence( i ).get_taxonomy == nil && get_sequence( j ).get_taxonomy == nil ) ||
354 ( get_sequence( i ).get_taxonomy == get_sequence( j ).get_taxonomy ) )
355 if Util.clean_seq_str( get_sequence( i ).get_sequence_as_string ) ==
356 Util.clean_seq_str( get_sequence( j ).get_sequence_as_string )
357 to_be_removed.add( j )
361 if get_sequence( i ).get_taxonomy != nil
362 tax_i = get_sequence( i ).get_taxonomy.get_name
364 if get_sequence( j ).get_taxonomy != nil
365 tax_j = get_sequence( j ).get_taxonomy.get_name
367 identical_pair = get_sequence( i ).get_name + " [#{tax_i}] == " + get_sequence( j ).get_name + " [#{tax_j}]"
368 @identical_seqs_detected.push( identical_pair )
378 to_be_removed_ary = to_be_removed.to_a.sort.reverse
380 to_be_removed_ary.each { | index |
381 removed.push( get_sequence( index ).get_name )
382 remove_sequence!( index )
388 def remove_sequences_by_non_gap_length!( min_non_gap_length )
389 n = get_number_of_seqs
393 seq = get_sequence( x )
394 if ( ( seq.get_length - seq.get_gap_length ) < min_non_gap_length )
395 if ( Evoruby::Constants::VERBOSE )
396 puts( "removed: " + seq.get_name )
398 removed << seq.get_name
399 remove_sequence!( x )
405 def trim!( first, last )
407 for i in 0 ... get_length()
408 if ( i < first || i > last )
412 remove_columns!( cols )
415 def get_gap_only_columns()
417 error_msg = "attempt to get gap only columns of unaligned msa"
418 raise StandardError, error_msg, caller
421 for c in 0 ... get_length
422 nogap_char_found = false
423 for s in 0 ... get_number_of_seqs
424 unless Util.is_aa_gap_character?( get_sequence( s ).get_character_code( c ) )
425 nogap_char_found = true
429 unless nogap_char_found
436 def calculate_gap_proportion()
438 error_msg = "attempt to get gap only columns of unaligned msa"
439 raise StandardError, error_msg, caller
443 for c in 0 ... get_length
444 for s in 0 ... get_number_of_seqs
445 total_sum = total_sum + 1
446 if Util.is_aa_gap_character?( get_sequence( s ).get_character_code( c ) )
447 gap_sum = gap_sum + 1
452 return gap_sum / total_sum
455 def get_gap_columns()
457 error_msg = "attempt to get gap columns of unaligned msa"
458 raise StandardError, error_msg, caller
461 for c in 0 ... get_length
462 gap_char_found = false
463 for s in 0 ... get_number_of_seqs
464 if Util.is_aa_gap_character?( get_sequence( s ).get_character_code( c ) )
465 gap_char_found = true
476 # gap_ratio = seqs with gap / number of sequences
477 # returns column indices for which seqs with gap / number of sequences > gap_ratio
478 def get_gap_columns_w_gap_ratio( gap_ratio )
480 error_msg = "attempt to get gap columns with gap_ratio of unaligned msa"
481 raise StandardError, error_msg, caller
483 if ( gap_ratio < 0 || gap_ratio > 1 )
484 error_msg = "gap ratio must be between 0 and 1 inclusive"
485 raise ArgumentError, error_msg, caller
488 for c in 0 ... get_length
490 for s in 0 ... get_number_of_seqs
491 if Util.is_aa_gap_character?( get_sequence( s ).get_character_code( c ) )
495 if ( ( gap_chars_found.to_f / get_number_of_seqs ) > gap_ratio )
503 # Split an alignment into n alignemnts of equal size, except last one
504 def split( n, verbose = false )
505 if ( n < 2 || n > get_number_of_seqs )
506 error_msg = "attempt to split into less than two or more than the number of sequences"
507 raise StandardError, error_msg, caller
510 r = get_number_of_seqs % n
511 x = get_number_of_seqs / n
516 if ( ( r > 0 ) && ( i == ( n - 1 ) ) )
519 puts( i.to_s + ": " + y.to_s )
522 msa.add_sequence( get_sequence( ( i * x ) + j ) )
526 puts( i.to_s + ": " + x.to_s )
529 msa.add_sequence( get_sequence( ( i * x ) + j ) )
540 def get_overlaps( min_overlap = 1 )
542 error_msg = "attempt to get overlaps of unaligned msa"
543 raise StandardError, error_msg, caller
546 for i in 0 ... get_number_of_seqs()
548 for j in 0 ... bins.length
549 current_bin = bins[ j ]
550 # does seq i overlap with all seqs in current_bin?
552 for z in 0 ... current_bin.length
553 unless overlap?( i, current_bin[ z ], min_overlap )
559 current_bin.push( i )
564 new_bin = Array.new()
572 def remove_columns!( cols )
574 error_msg = "attempt to remove columns of unaligned msa"
575 raise StandardError, error_msg, caller
578 for c in 0 ... cols.length()
580 for s in 0 ... get_number_of_seqs()
581 get_sequence( s ).delete_residue!( col )