allow length based removal of seqs for non aligned msa
[jalview.git] / forester / ruby / evoruby / lib / evo / msa / msa.rb
1 #
2 # = lib/evo/msa/msa.rb - Msa class
3 #
4 # Copyright::  Copyright (C) 2006-2007 Christian M. Zmasek
5 # License::    GNU Lesser General Public License (LGPL)
6 #
7 # $Id: msa.rb,v 1.11 2009/01/03 00:42:08 cmzmasek Exp $
8 #
9
10
11 require 'lib/evo/util/constants'
12 require 'lib/evo/util/util'
13 require 'lib/evo/sequence/sequence'
14
15 module Evoruby
16
17   class Msa
18
19     def initialize()
20       @sequences = Array.new
21       @identical_seqs_detected = Array.new
22     end
23
24
25     def add_sequence( sequence )
26       @sequences.push( sequence )
27     end
28
29     def add( name, molecular_sequence_str )
30       add_sequence( Sequence.new( name, molecular_sequence_str ) )
31     end
32
33     def get_sequence( index )
34       if ( index < 0 || index > get_number_of_seqs() - 1 )
35         error_msg = "attempt to get sequence " <<
36          index.to_s << " in alignment of " << get_number_of_seqs().to_s <<
37          " sequences"
38         raise ArgumentError, error_msg
39       end
40       return @sequences[ index ]
41     end
42
43     def remove_sequence!( index )
44       if ( index < 0 || index > get_number_of_seqs() - 1 )
45         error_msg = "attempt to remove sequence " <<
46          index.to_s << " in alignment of " << get_number_of_seqs().to_s <<
47          " sequences"
48         raise ArgumentError, error_msg
49       end
50       @sequences.delete_at( index )
51     end
52
53     def get_identical_seqs_detected
54       @identical_seqs_detected
55     end
56
57
58     def is_aligned()
59       if ( get_number_of_seqs < 1 )
60         return false
61       else
62         l = @sequences[ 0 ].get_length()
63         for i in 0 ... get_number_of_seqs()
64           if ( get_sequence( i ).get_length() != l )
65             return false
66           end
67         end
68       end
69       return true
70     end
71
72     def find_by_name( name, case_sensitive, partial_match )
73       indices = Array.new()
74       for i in 0 ... get_number_of_seqs()
75         current_name = get_sequence( i ).get_name()
76         if !case_sensitive
77           current_name = current_name.downcase
78           name = name.downcase
79         end
80         if current_name == name ||
81            ( partial_match && current_name.include?( name ) )
82           indices.push( i )
83         end
84       end
85       indices
86     end
87
88     def find_by_name_pattern( name_re, avoid_similar_to = true )
89       indices = []
90       for i in 0 ... get_number_of_seqs()
91         if avoid_similar_to
92           m = name_re.match( get_sequence( i ).get_name() )
93           if m && !m.pre_match.downcase.include?( "similar to " )
94             indices.push( i )
95           end
96         else
97           if name_re.match( get_sequence( i ).get_name() )
98             indices.push( i )
99           end
100         end
101       end
102       indices
103     end
104
105     # throws ArgumentError
106     def get_by_name_pattern( name_re , avoid_similar_to = true )
107       indices = find_by_name_pattern( name_re, avoid_similar_to  )
108       if ( indices.length > 1 )
109         error_msg = "pattern  " + name_re.to_s + " not unique"
110         raise ArgumentError, error_msg
111       elsif ( indices.length < 1 )
112         error_msg = "pattern " + name_re.to_s + " not found"
113         raise ArgumentError, error_msg
114       end
115       get_sequence( indices[ 0 ] )
116     end
117
118     def find_by_name_start( name, case_sensitive )
119       indices = []
120       for i in 0 ... get_number_of_seqs()
121         get_sequence( i ).get_name() =~ /^\s*(\S+)/
122         current_name = $1
123         if !case_sensitive
124           current_name = current_name.downcase
125           name = name.downcase
126         end
127         if  ( current_name == name )
128           indices.push( i )
129         end
130       end
131       indices
132     end
133
134     def has?( name, case_sensitive = true, partial_match = false )
135       for i in 0 ... get_number_of_seqs()
136         current_name = get_sequence( i ).get_name()
137         if !case_sensitive
138           current_name = current_name.downcase
139           name = name.downcase
140         end
141         if current_name == name ||
142            ( partial_match && current_name.include?( name ) )
143           return true
144         end
145       end
146       false
147     end
148
149     # throws ArgumentError
150     def get_by_name( name, case_sensitive = true, partial_match = false )
151       indices = find_by_name( name, case_sensitive, partial_match )
152       if ( indices.length > 1 )
153         error_msg = "\"" + name + "\" not unique"
154         raise ArgumentError, error_msg
155       elsif ( indices.length < 1 )
156         error_msg = "\"" + name + "\" not found"
157         raise ArgumentError, error_msg
158       end
159       get_sequence( indices[ 0 ] )
160     end
161
162     # throws ArgumentError
163     def get_by_name_start( name, case_sensitive = true )
164       indices = find_by_name_start( name, case_sensitive )
165       if ( indices.length > 1 )
166         error_msg = "\"" + name + "\" not unique"
167         raise ArgumentError, error_msg
168       elsif ( indices.length < 1 )
169         error_msg = "\"" + name + "\" not found"
170         raise ArgumentError, error_msg
171       end
172       get_sequence( indices[ 0 ] )
173     end
174
175
176     def get_sub_alignment( seq_numbers )
177       msa = Msa.new()
178       for i in 0 ... seq_numbers.length()
179         msa.add_sequence( get_sequence( seq_numbers[ i ] ).copy() )
180       end
181       return msa
182     end
183
184     def get_number_of_seqs()
185       @sequences.length
186     end
187
188     def get_length
189       if !is_aligned()
190         error_msg = "attempt to get length of unaligned msa"
191         raise StandardError, error_msg, caller
192       end
193       if get_number_of_seqs() < 1
194         -1
195       else
196         @sequences[ 0 ].get_length()
197       end
198     end
199
200     def to_str
201       to_fasta
202     end
203
204     def to_fasta
205       s = String.new
206       for i in 0...get_number_of_seqs
207         s += @sequences[ i ].to_fasta + Constants::LINE_DELIMITER
208       end
209       s
210     end
211
212
213     def print_overlap_diagram( min_overlap = 1, io = STDOUT, max_name_length = 10 )
214       if ( !is_aligned() )
215         error_msg = "attempt to get overlap diagram of unaligned msa"
216         raise StandardError, error_msg, caller
217       end
218       for i in 0 ... get_number_of_seqs()
219         io.print( Util.normalize_seq_name( get_sequence( i ).get_name(), max_name_length ) )
220         for j in 0 ... get_number_of_seqs()
221           if i == j
222             io.print( " " )
223           else
224             if overlap?( i, j, min_overlap )
225               io.print( "+" )
226             else
227               io.print( "-" )
228             end
229           end
230         end
231         io.print( Evoruby::Constants::LINE_DELIMITER )
232       end
233     end
234
235     #returns array of Msa with an overlap of min_overlap
236     def split_into_overlapping_msa( min_overlap = 1 )
237       if ( !is_aligned() )
238         error_msg = "attempt to split into overlapping msas of unaligned msa"
239         raise StandardError, error_msg, caller
240       end
241       msas = Array.new()
242       bins = get_overlaps( min_overlap )
243       for i in 0 ... bins.length
244         msas.push( get_sub_alignment( bins[ i ] ) )
245       end
246       msas
247     end
248
249     def overlap?( index_1, index_2, min_overlap = 1 )
250       seq_1 = get_sequence( index_1 )
251       seq_2 = get_sequence( index_2 )
252       overlap_count = 0
253       for i in 0...seq_1.get_length()
254         if !Util.is_aa_gap_character?( seq_1.get_character_code( i ) ) &&
255            !Util.is_aa_gap_character?( seq_2.get_character_code( i ) )
256           overlap_count += 1
257           if overlap_count >= min_overlap
258             return true
259           end
260         end
261       end
262       return false
263     end
264
265     def calculate_overlap( index_1, index_2 )
266       seq_1 = get_sequence( index_1 )
267       seq_2 = get_sequence( index_2 )
268       overlap_count = 0
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 ) )
272           overlap_count += 1
273         end
274       end
275       overlap_count
276     end
277
278     def calculate_identities( index_1, index_2 )
279       seq_1 = get_sequence( index_1 )
280       seq_2 = get_sequence( index_2 )
281       identities_count = 0
282       for i in 0...seq_1.get_length
283         if !Util.is_aa_gap_character?( seq_1.get_character_code( i ) ) &&
284            !Util.is_aa_gap_character?( seq_2.get_character_code( i ) ) &&
285            seq_1.get_character_code( i ) != 63 &&
286            ( seq_1.get_residue( i ).downcase() ==
287              seq_2.get_residue( i ).downcase() )
288           identities_count += 1
289         end
290       end
291       identities_count
292     end
293
294     def remove_gap_only_columns!()
295       remove_columns!( get_gap_only_columns() )
296     end
297
298     def remove_gap_columns!()
299       remove_columns!( get_gap_columns() )
300     end
301
302     # removes columns for which seqs with gap / number of sequences > gap_ratio
303     def remove_gap_columns_w_gap_ratio!( gap_ratio )
304       remove_columns!( get_gap_columns_w_gap_ratio( gap_ratio ) )
305     end
306
307
308     def remove_sequences_by_gap_ratio!( gap_ratio )
309       if ( !is_aligned() )
310         error_msg = "attempt to remove sequences by gap ratio on unaligned msa"
311         raise StandardError, error_msg, caller
312       end
313       n = get_number_of_seqs
314       removed = Array.new
315       for s in 0 ... n
316         if ( get_sequence( ( n - 1 ) - s  ).get_gap_ratio() > gap_ratio )
317           if ( Evoruby::Constants::VERBOSE )
318             puts( "removed: " + get_sequence( ( n - 1 ) - s  ).get_name )
319           end
320           removed << get_sequence( ( n - 1 ) - s  ).get_name
321           remove_sequence!( ( n - 1 ) - s  )
322         end
323       end
324       removed
325     end
326
327
328     def remove_redundant_sequences!( consider_taxonomy = false, verbose = false )
329       n = get_number_of_seqs
330       removed = Array.new
331       to_be_removed = Set.new
332       @identical_seqs_detected = Array.new
333       for i in 0 ... ( n - 1 )
334         for j in ( i + 1 ) ... n
335           if !to_be_removed.include?( i ) && !to_be_removed.include?( j )
336             if  !consider_taxonomy ||
337                ( ( get_sequence( i ).get_taxonomy == nil && get_sequence( j ).get_taxonomy == nil ) ||
338                  ( get_sequence( i ).get_taxonomy == get_sequence( j ).get_taxonomy ) )
339               if Util.clean_seq_str( get_sequence( i ).get_sequence_as_string ) ==
340                  Util.clean_seq_str( get_sequence( j ).get_sequence_as_string )
341                 to_be_removed.add( j )
342
343                 tax_i = ""
344                 tax_j = ""
345                 if get_sequence( i ).get_taxonomy != nil
346                   tax_i = get_sequence( i ).get_taxonomy.get_name
347                 end
348                 if get_sequence( j ).get_taxonomy != nil
349                   tax_j = get_sequence( j ).get_taxonomy.get_name
350                 end
351                 identical_pair = get_sequence( i ).get_name + " [#{tax_i}] == " + get_sequence( j ).get_name + " [#{tax_j}]"
352                 @identical_seqs_detected.push( identical_pair )
353                 if verbose
354                   puts identical_pair
355                 end
356               end
357             end
358           end
359         end
360       end
361
362       to_be_removed_ary = to_be_removed.to_a.sort.reverse
363
364       to_be_removed_ary.each { | index |
365         removed.push( get_sequence( index ).get_name )
366         remove_sequence!( index )
367       }
368       removed
369     end
370
371
372     def remove_sequences_by_non_gap_length!( min_non_gap_length )
373       n = get_number_of_seqs
374       removed = Array.new
375       for s in 0 ... n
376         x = ( n - 1 ) - s
377         seq = get_sequence( x )
378         if ( ( seq.get_length - seq.get_gap_length ) < min_non_gap_length )
379           if ( Evoruby::Constants::VERBOSE )
380             puts( "removed: " + seq.get_name )
381           end
382           removed << seq.get_name
383           remove_sequence!( x )
384         end
385       end
386       removed
387     end
388
389     def trim!( first, last )
390       cols = Array.new()
391       for i in 0 ... get_length()
392         if ( i < first || i > last )
393           cols.push( i )
394         end
395       end
396       remove_columns!( cols )
397     end
398
399     def get_gap_only_columns()
400       if ( !is_aligned() )
401         error_msg = "attempt to get gap only columns of unaligned msa"
402         raise StandardError, error_msg, caller
403       end
404       cols = Array.new()
405       for c in 0 ... get_length
406         nogap_char_found = false
407         for s in 0 ... get_number_of_seqs
408           unless Util.is_aa_gap_character?( get_sequence( s ).get_character_code( c ) )
409             nogap_char_found = true
410             break
411           end
412         end
413         unless nogap_char_found
414           cols.push( c )
415         end
416       end
417       return cols
418     end
419
420     def calculate_gap_proportion()
421       if ( !is_aligned() )
422         error_msg = "attempt to get gap only columns of unaligned msa"
423         raise StandardError, error_msg, caller
424       end
425       total_sum = 0.0
426       gap_sum = 0.0
427       for c in 0 ... get_length
428         for s in 0 ... get_number_of_seqs
429           total_sum = total_sum + 1
430           if Util.is_aa_gap_character?( get_sequence( s ).get_character_code( c ) )
431             gap_sum = gap_sum  + 1
432           end
433         end
434
435       end
436       return gap_sum / total_sum
437     end
438
439     def get_gap_columns()
440       if ( !is_aligned() )
441         error_msg = "attempt to get gap columns of unaligned msa"
442         raise StandardError, error_msg, caller
443       end
444       cols = Array.new()
445       for c in 0 ... get_length
446         gap_char_found = false
447         for s in 0 ... get_number_of_seqs
448           if Util.is_aa_gap_character?( get_sequence( s ).get_character_code( c ) )
449             gap_char_found = true
450             break
451           end
452         end
453         if gap_char_found
454           cols.push( c )
455         end
456       end
457       return cols
458     end
459
460     # gap_ratio = seqs with gap / number of sequences
461     # returns column indices for which seqs with gap / number of sequences > gap_ratio
462     def get_gap_columns_w_gap_ratio( gap_ratio )
463       if ( !is_aligned() )
464         error_msg = "attempt to get gap columns with gap_ratio of unaligned msa"
465         raise StandardError, error_msg, caller
466       end
467       if ( gap_ratio < 0 || gap_ratio > 1 )
468         error_msg = "gap ratio must be between 0 and 1 inclusive"
469         raise ArgumentError, error_msg, caller
470       end
471       cols = Array.new()
472       for c in 0 ... get_length
473         gap_chars_found = 0
474         for s in 0 ... get_number_of_seqs
475           if Util.is_aa_gap_character?( get_sequence( s ).get_character_code( c ) )
476             gap_chars_found += 1
477           end
478         end
479         if ( ( gap_chars_found.to_f / get_number_of_seqs ) > gap_ratio )
480           cols.push( c )
481         end
482       end
483       return cols
484     end
485
486
487     # Split an alignment into n alignemnts of equal size, except last one
488     def split( n, verbose = false )
489       if ( n < 2 || n > get_number_of_seqs )
490         error_msg = "attempt to split into less than two or more than the number of sequences"
491         raise StandardError, error_msg, caller
492       end
493       msas = Array.new()
494       r = get_number_of_seqs % n
495       x = get_number_of_seqs / n
496       for i in 0 ... n
497         msa = Msa.new()
498         s = 0
499
500         if ( ( r > 0 ) && ( i == ( n - 1 ) ) )
501           y = x + r
502           if ( verbose )
503             puts( i.to_s + ": " + y.to_s )
504           end
505           for j in 0 ... y
506             msa.add_sequence( get_sequence( ( i * x ) + j ) )
507           end
508         else
509           if ( verbose )
510             puts( i.to_s + ": " + x.to_s )
511           end
512           for j in 0 ... x
513             msa.add_sequence( get_sequence( ( i * x ) + j ) )
514           end
515         end
516         msas.push( msa )
517       end
518       msas
519     end
520
521
522     private
523
524     def get_overlaps( min_overlap = 1 )
525       if ( !is_aligned() )
526         error_msg = "attempt to get overlaps of unaligned msa"
527         raise StandardError, error_msg, caller
528       end
529       bins = Array.new()
530       for i in 0 ... get_number_of_seqs()
531         found_bin = false
532         for j in 0 ... bins.length
533           current_bin = bins[ j ]
534           # does seq i overlap with all seqs in current_bin?
535           all_overlap = true
536           for z in 0 ... current_bin.length
537             unless overlap?( i, current_bin[ z ], min_overlap )
538               all_overlap = false
539               break
540             end
541           end
542           if all_overlap
543             current_bin.push( i )
544             found_bin = true
545           end
546         end
547         unless found_bin
548           new_bin = Array.new()
549           new_bin.push( i )
550           bins.push( new_bin )
551         end
552       end
553       return bins
554     end
555
556     def remove_columns!( cols )
557       if ( !is_aligned() )
558         error_msg = "attempt to remove columns of unaligned msa"
559         raise StandardError, error_msg, caller
560       end
561       cols.reverse!()
562       for c in 0 ... cols.length()
563         col = cols[ c ]
564         for s in 0 ... get_number_of_seqs()
565           get_sequence( s ).delete_residue!( col )
566         end
567       end
568       return self
569     end
570
571
572   end # class Msa
573
574 end # module Evoruby
575