1 package Maasha::AlignTwoSeq;
3 # Copyright (C) 2007 Martin A. Hansen.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
25 # Routines to create a pair-wise alignment.
28 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
36 use vars qw ( @ISA @EXPORT );
38 @ISA = qw( Exporter );
41 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
46 # Martin A. Hansen, August 2006.
48 # Generates an alignment by chaining matches, which are subsequences
49 # shared between two sequences. The routine functions by considering
50 # only matches within a given search space. If no matches are given
51 # these will be generated, if long matches are found these will be
52 # included in the alignment, otherwise matches will be included depending
53 # on a calculated score. New search spaces spanning the spaces between
54 # matches and the search space boundaries will be cast and recursed into.
56 # Usage: align_two_seq( { Q_SEQ => \$q_seq, S_SEQ => \$s_seq }, [] );
58 my ( $space, # search space
59 $matches, # list of matches
64 my ( @chain, $best_match, $left_space, $right_space );
68 matches_select( $matches, $space );
70 if ( scalar @{ $matches } == 0 ) # no matches - find some!
72 $matches = matches_find( $space );
74 if ( scalar @{ $matches } > 0 ) {
75 push @chain, align_two_seq( $space, $matches );
78 else # matches are included according to score
80 $matches = matches_filter( $space, $matches );
82 $best_match = shift @{ $matches };
86 push @chain, $best_match;
88 $left_space = new_space_left( $best_match, $space );
89 $right_space = new_space_right( $best_match, $space );
91 push @chain, align_two_seq( $left_space, $matches ) if defined $left_space;
92 push @chain, align_two_seq( $right_space, $matches ) if defined $right_space;
96 return wantarray ? @chain : \@chain;
102 # Martin A. Hansen, August 2009.
104 # Define a new search space if not previously
105 # defined. This occurs if align_two_seq() is
106 # called initially with only the sequences.
108 my ( $space, # search space
113 if ( not defined $space->{ 'Q_MAX' } )
115 $space->{ 'Q_MIN' } = 0;
116 $space->{ 'S_MIN' } = 0;
117 $space->{ 'Q_MAX' } = length( ${ $space->{ 'Q_SEQ' } } ) - 1,
118 $space->{ 'S_MAX' } = length( ${ $space->{ 'S_SEQ' } } ) - 1,
125 # Martin A. Hansen, August 2009.
127 # Create a new search space between the beginning of
128 # the current search space and the best match.
130 my ( $best_match, # best match
131 $space, # search space
138 if ( $best_match->{ 'Q_BEG' } - $space->{ 'Q_MIN' } >= 2 and $best_match->{ 'S_BEG' } - $space->{ 'S_MIN' } >= 2 )
141 Q_SEQ => $space->{ 'Q_SEQ' },
142 S_SEQ => $space->{ 'S_SEQ' },
143 Q_MIN => $space->{ 'Q_MIN' },
144 Q_MAX => $best_match->{ 'Q_BEG' } - 1,
145 S_MIN => $space->{ 'S_MIN' },
146 S_MAX => $best_match->{ 'S_BEG' } - 1,
150 return wantarray ? %{ $new_space } : $new_space;
156 # Martin A. Hansen, August 2009.
158 # Create a new search space between the best match
159 # and the end of the current search space.
161 my ( $best_match, # best match
162 $space, # search space
169 if ( $space->{ 'Q_MAX' } - $best_match->{ 'Q_END' } >= 2 and $space->{ 'S_MAX' } - $best_match->{ 'S_END' } >= 2 )
172 Q_SEQ => $space->{ 'Q_SEQ' },
173 S_SEQ => $space->{ 'S_SEQ' },
174 Q_MIN => $best_match->{ 'Q_END' } + 1,
175 Q_MAX => $space->{ 'Q_MAX' },
176 S_MIN => $best_match->{ 'S_END' } + 1,
177 S_MAX => $space->{ 'S_MAX' },
181 return wantarray ? %{ $new_space } : $new_space;
187 # Martin A. Hansen, August 2006.
189 # Given a list of matches and a search space,
190 # include only those matches contained within
193 my ( $matches, # list of matches
194 $space, # search space
199 @{ $matches } = grep { $_->{ 'Q_BEG' } >= $space->{ 'Q_MIN' } and
200 $_->{ 'S_BEG' } >= $space->{ 'S_MIN' } and
201 $_->{ 'Q_END' } <= $space->{ 'Q_MAX' } and
202 $_->{ 'S_END' } <= $space->{ 'S_MAX' } } @{ $matches };
208 # Martin A. Hansen, August 2008.
210 # Given a search space calculates the wordsize for a word so a match
211 # occurs only a few times. More matches may be needed at low similarity in
212 # order to avoid starting with a wrong match.
214 my ( $space, # search space
219 my ( $factor, $word_size );
223 if ( $space->{ 'Q_MAX' } - $space->{ 'Q_MIN' } < $space->{ 'S_MAX' } - $space->{ 'S_MIN' } ) {
224 $word_size = int( $factor * ( $space->{ 'Q_MAX' } - $space->{ 'Q_MIN' } ) ) + 1;
226 $word_size = int( $factor * ( $space->{ 'S_MAX' } - $space->{ 'S_MIN' } ) ) + 1;
235 # Martin A. Hansen, August 2009.
237 # Indexes a query sequence in a specified search space
238 # Overlapping words are saved as keys in an index hash,
239 # and a list of word postions are saved as value.
241 my ( $space, # search space
242 $word_size, # word size
247 my ( $i, $word, %index );
249 for ( $i = $space->{ 'Q_MIN' }; $i <= $space->{ 'Q_MAX' } - $word_size + 1; $i++ )
251 $word = uc substr ${ $space->{ 'Q_SEQ' } }, $i, $word_size;
253 next if $word =~ tr/N//; # Skip sequences with Ns.
255 push @{ $index{ $word } }, $i;
258 return wantarray ? %index : \%index;
264 # Martin A. Hansen, August 2009.
266 my ( $index, # sequence index
267 $space, # search space
268 $word_size, # word size
273 my ( $i, $word, $pos, $match, @matches, $redundant );
277 for ( $i = $space->{ 'S_MIN' }; $i <= $space->{ 'S_MAX' } - $word_size + 1; $i++ )
279 $word = uc substr ${ $space->{ 'S_SEQ' } }, $i, $word_size;
281 if ( exists $index->{ $word } )
283 foreach $pos ( @{ $index->{ $word } } )
287 'Q_END' => $pos + $word_size - 1,
289 'S_END' => $i + $word_size - 1,
294 if ( not match_redundant( $match, $redundant ) )
296 match_expand( $match, $space );
298 push @matches, $match;
300 match_redundant_add( $match, $redundant );
306 return wantarray ? @matches : \@matches;
312 # Martin A. Hansen, November 2006
314 # Given two sequences, find all maximum expanded matches between these.
316 my ( $space, # search space
319 # returns list of matches
321 my ( $word_size, $index, $matches );
323 $word_size = word_size_calc( $space );
327 while ( scalar @{ $matches } == 0 and $word_size > 0 )
329 $index = seq_index( $space, $word_size );
330 $matches = seq_scan( $index, $space, $word_size );
335 return wantarray ? @{ $matches } : $matches;
341 # Martin A. Hansen, August 2009.
343 # Given a match and a search space hash,
344 # expand the match maximally both forward and
345 # backward direction.
347 my ( $match, # match to expand
348 $space, # search space
353 match_expand_forward( $match, $space );
354 match_expand_backward( $match, $space );
358 sub match_expand_forward
360 # Martin A. Hansen, August 2009.
362 # Given a match and a search space hash,
363 # expand the match maximally in the forward
366 my ( $match, # match to expand
367 $space, # search space
372 while ( $match->{ 'Q_END' } <= $space->{ 'Q_MAX' } and
373 $match->{ 'S_END' } <= $space->{ 'S_MAX' } and
374 lc substr( ${ $space->{ 'Q_SEQ' } }, $match->{ 'Q_END' }, 1 ) eq lc substr( ${ $space->{ 'S_SEQ' } }, $match->{ 'S_END' }, 1 ) )
376 $match->{ 'Q_END' }++;
377 $match->{ 'S_END' }++;
381 $match->{ 'Q_END' }--;
382 $match->{ 'S_END' }--;
387 sub match_expand_backward
389 # Martin A. Hansen, August 2009.
391 # Given a match and a search space hash,
392 # expand the match maximally in the backward
395 my ( $match, # match to expand
396 $space, # search space
401 while ( $match->{ 'Q_BEG' } >= $space->{ 'Q_MIN' } and
402 $match->{ 'S_BEG' } >= $space->{ 'S_MIN' } and
403 lc substr( ${ $space->{ 'Q_SEQ' } }, $match->{ 'Q_BEG' }, 1 ) eq lc substr( ${ $space->{ 'S_SEQ' } }, $match->{ 'S_BEG' }, 1 ) )
405 $match->{ 'Q_BEG'}--;
406 $match->{ 'S_BEG'}--;
410 $match->{ 'Q_BEG'}++;
411 $match->{ 'S_BEG'}++;
418 # Martin A. Hansen, August 2009
420 my ( $new_match, # match
421 $redundant, # hashref
428 foreach $match ( @{ $redundant->{ $new_match->{ 'Q_BEG' } } } )
430 if ( $new_match->{ 'S_BEG' } >= $match->{ 'S_BEG' } and $new_match->{ 'S_END' } <= $match->{ 'S_END' } ) {
439 sub match_redundant_add
441 # Martin A. Hansen, August 2009
444 $redundant, # hash ref
449 map { push @{ $redundant->{ $_ } }, $match } ( $match->{ 'Q_BEG' } .. $match->{ 'Q_END' } );
455 # Martin A. Hansen, August 2009.
457 my ( $space, # search space
458 $matches, # list of matches
463 my ( $best_match, $match, @new_matches );
465 $best_match = shift @{ $matches };
467 match_score( $best_match, $space );
469 foreach $match ( @{ $matches } )
471 match_score( $match, $space );
473 if ( $match->{ 'SCORE' } > 0 )
475 if ( $match->{ 'SCORE' } > $best_match->{ 'SCORE' } ) {
476 $best_match = $match;
478 push @new_matches, $match;
483 unshift @new_matches, $best_match if $best_match->{ 'SCORE' } > 0;
485 return wantarray ? @new_matches : \@new_matches;
491 # Martin A. Hansen, August 2009
493 # Given a match and a search space scores the match according to three criteria:
495 # 1) length of match - the longer the better.
496 # 2) distance to closest corner - the shorter the better.
497 # 3) distance to closest narrow end of the search space - the shorter the better.
499 # Each of these scores are divided by search space dimensions, and the total score
500 # is calculated: total = score_len - score_corner - score_narrow.
502 # The higher the score, the better the match.
505 $space, # search space
510 match_score_len( $match );
511 match_score_diag( $match, $space );
512 match_score_narrow( $match, $space );
518 # Martin A. Hansen, August 2009
520 # Score according to match length.
527 $match->{ 'SCORE' } = $match->{ 'LEN' } ** 3;
533 # Martin A. Hansen, August 2009
535 # Score according to distance from diagonal.
538 $space, # search space
543 my ( $q_dim, $s_dim, $beg_diag_dist, $end_diag_dist, $min_diag_dist, $score_diag );
545 $q_dim = $space->{ 'Q_MAX' } - $space->{ 'Q_MIN' } + 1;
546 $s_dim = $space->{ 'S_MAX' } - $space->{ 'S_MIN' } + 1;
548 if ( $q_dim >= $s_dim ) # s_dim is the narrow end
550 $beg_diag_dist = Maasha::Calc::dist_point2line( $match->{ 'Q_BEG' },
554 $space->{ 'Q_MIN' } + $s_dim,
555 $space->{ 'S_MIN' } + $s_dim );
557 $end_diag_dist = Maasha::Calc::dist_point2line( $match->{ 'Q_BEG' },
559 $space->{ 'Q_MAX' } - $s_dim,
560 $space->{ 'S_MAX' } - $s_dim,
562 $space->{ 'S_MAX' } );
566 $beg_diag_dist = Maasha::Calc::dist_point2line( $match->{ 'Q_BEG' },
570 $space->{ 'Q_MIN' } + $q_dim,
571 $space->{ 'S_MIN' } + $q_dim );
573 $end_diag_dist = Maasha::Calc::dist_point2line( $match->{ 'Q_BEG' },
575 $space->{ 'Q_MAX' } - $q_dim,
576 $space->{ 's_max' } - $q_dim,
578 $space->{ 'S_MAX' } );
581 $min_diag_dist = Maasha::Calc::min( $beg_diag_dist, $end_diag_dist );
583 $score_diag = 2 * $min_diag_dist ** 2;
585 $match->{ 'SCORE' } -= $score_diag;
589 sub match_score_narrow
591 # Martin A. Hansen, August 2009
593 # Score according to distance to the narrow end of the search space.
596 $space, # search space
601 my ( $max_narrow_dist, $q_dim, $s_dim, $beg_narrow_dist, $end_narrow_dist, $score_narrow );
603 $max_narrow_dist = 0;
605 $q_dim = $space->{ 'Q_MAX' } - $space->{ 'Q_MIN' } + 1;
606 $s_dim = $space->{ 'S_MAX' } - $space->{ 'S_MIN' } + 1;
608 if ( $q_dim > $s_dim ) # s_dim is the narrow end
610 $beg_narrow_dist = $match->{ 'Q_BEG' } - $space->{ 'Q_MIN' };
611 $end_narrow_dist = $space->{ 'Q_MAX' } - $match->{ 'Q_BEG' };
613 $max_narrow_dist = Maasha::Calc::max( $beg_narrow_dist, $end_narrow_dist );
615 elsif ( $q_dim < $s_dim ) # q_dim is the narrow end
617 $beg_narrow_dist = $match->{ 'S_BEG' } - $space->{ 'S_MIN' };
618 $end_narrow_dist = $space->{ 'S_MAX' } - $match->{ 'S_BEG' };
620 $max_narrow_dist = Maasha::Calc::max( $beg_narrow_dist, $end_narrow_dist );
623 $score_narrow = $max_narrow_dist;
625 $match->{ 'SCORE' } -= $score_narrow;
634 # Martin A. Hansen, June 2009.
636 # Given a list of matches and references to q_seq and s_seq,
637 # insert indels to form aligned sequences.
639 my ( $matches, # list of matches
640 $q_seq, # ref to query sequence
641 $s_seq, # ref to subject sequence
646 my ( $q_indels, $s_indels, $match, $diff, $first );
648 @{ $matches } = sort { $a->{ Q_BEG ] <=> $b->[ Q_BEG ] } @{ $matches };
654 # ---- initial indels ----
656 $match = shift @{ $matches };
658 substr ${ $q_seq }, $match->[ Q_BEG ] + $q_indels, $match->[ LEN ], uc substr ${ $q_seq }, $match->[ Q_BEG ] + $q_indels, $match->[ LEN ];
659 substr ${ $s_seq }, $match->[ S_BEG ] + $s_indels, $match->[ LEN ], uc substr ${ $s_seq }, $match->[ S_BEG ] + $s_indels, $match->[ LEN ];
661 $diff = ( $match->[ Q_BEG ] + $q_indels ) - ( $match->[ S_BEG ] + $s_indels );
665 substr ${ $q_seq }, 0, 0, '-' x abs $diff;
667 $q_indels += abs $diff;
671 substr ${ $s_seq }, 0, 0, '-' x abs $diff;
673 $s_indels += abs $diff;
676 # ---- internal indels ----
678 foreach $match ( @{ $matches } )
680 substr ${ $q_seq }, $match->[ Q_BEG ] + $q_indels, $match->[ LEN ], uc substr ${ $q_seq }, $match->[ Q_BEG ] + $q_indels, $match->[ LEN ];
681 substr ${ $s_seq }, $match->[ S_BEG ] + $s_indels, $match->[ LEN ], uc substr ${ $s_seq }, $match->[ S_BEG ] + $s_indels, $match->[ LEN ];
683 $diff = ( $match->[ Q_BEG ] + $q_indels ) - ( $match->[ S_BEG ] + $s_indels );
687 substr ${ $q_seq }, $match->[ Q_BEG ] + $q_indels, 0, '-' x abs $diff;
689 $q_indels += abs $diff;
693 substr ${ $s_seq }, $match->[ S_BEG ] + $s_indels, 0, '-' x abs $diff;
695 $s_indels += abs $diff;
699 # ---- last indels ----
701 $diff = length( ${ $s_seq } ) - length( ${ $q_seq } );
704 ${ $q_seq } .= '-' x abs $diff;
706 ${ $s_seq } .= '-' x abs $diff;
713 # Martin A. Hansen, May 2007.
715 # Calculate the local similarity of an alignment based on
716 # an alignment chain. The similarity is calculated as
717 # the number of matching residues divided by the overall
718 # length of the alignment chain. This means that a short
719 # but "good" alignment will yield a high similarity, while
720 # a long "poor" alignment will yeild a low similarity.
722 my ( $chain, # list of matches in alignment
727 my ( $match, $match_tot, $q_beg, $q_end, $s_beg, $s_end, $q_diff, $s_diff, $max, $sim );
729 return 0 if not @{ $chain };
737 foreach $match ( @{ $chain } )
739 $match_tot += $match->[ LEN ];
741 $q_beg = $match->[ Q_BEG ] if $match->[ Q_BEG ] < $q_beg;
742 $s_beg = $match->[ S_BEG ] if $match->[ S_BEG ] < $s_beg;
744 $q_end = $match->[ Q_END ] if $match->[ Q_END ] > $q_end;
745 $s_end = $match->[ S_END ] if $match->[ S_END ] > $s_end;
748 $q_diff = $q_end - $q_beg + 1;
749 $s_diff = $s_end - $s_beg + 1;
751 $max = Maasha::Calc::max( $q_diff, $s_diff );
753 $sim = sprintf( "%.2f", ( $match_tot / $max ) * 100 );
761 # Martin A. Hansen, June 2007.
763 # Calculate the global similarity of an alignment based on
764 # an alignment chain. The similarity is calculated as
765 # the number of matching residues divided by the
766 # length of the shortest sequence.
768 my ( $chain, # list of matches in alignment
769 $q_seq, # ref to query sequence
770 $s_seq, # ref to subject sequence
775 my ( $match_tot, $min, $sim );
777 return 0 if not @{ $chain };
781 map { $match_tot += $_->[ LEN ] } @{ $chain };
783 $min = Maasha::Calc::min( length( ${ $q_seq } ), length( ${ $s_seq } ) );
785 $sim = sprintf( "%.2f", ( $match_tot / $min ) * 100 );
793 # Martin A. Hansen, June 2006.
795 # Given an alignment as a list of FASTA entries,
796 # generates a consensus sequences based on the
797 # entropies for each column similar to the way
798 # a sequence logo i calculated. Returns the
799 # consensus sequence as a FASTA entry.
801 my ( $entries, # list of aligned FASTA entries
802 $type, # residue type - OPTIONAL
803 $min_sim, # minimum similarity - OPTIONAL
808 my ( $bit_max, $data, $pos, $char, $score, $entry );
810 $type ||= Maasha::Seq::seq_guess_type( $entries->[ 0 ] );
813 if ( $type =~ /protein/ ) {
819 $data = Maasha::Seq::seqlogo_calc( $bit_max, $entries );
821 foreach $pos ( @{ $data } )
823 ( $char, $score ) = @{ $pos->[ -1 ] };
825 if ( ( $score / $bit_max ) * 100 >= $min_sim ) {
826 $entry->[ SEQ ] .= $char;
828 $entry->[ SEQ ] .= "-";
832 $entry->[ HEAD ] = "Consensus: $min_sim%";
834 return wantarray ? @{ $entry } : $entry;
838 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<