Introduction to Sequence Alignment PENCE Bioinformatics Research Group
Introduction to Sequence Alignment PENCE Bioinformatics Research Group University of Alberta May 2001 ©Duane Szafron 2000
2 Outline l l l Sequence Alignment Full Matrix Algorithms Hirschberg’s Algorithm The Fast. LSA Algorithm Leading and Trailing Blanks ©Duane Szafron 2000
3 Sequence Alignment Sequence alignment reduces to a problem of matching two strings by introducing gaps to maximize a scoring function. l The scoring function favors similar characters in the same position, penalizes dissimilar characters and penalizes gaps. l AGT-ATGCA AGTATGCA ATTGAT--A ©Duane Szafron 2000
4 Scoring Function There are many different scoring functions. l Here is a simple one suitable for illustration, but not actually used: l – Exact match: +2 points – Different characters: -1 point – Gap: -2 points AGT-ATGCA ATTGAT--A 2 -1 +2 -2+2+2 -2 -2+2 = 3 ©Duane Szafron 2000
5 Scoring Ties There can be several optimal alignment solutions due to scoring ties. l There actually three optimal solutions in our example alignment: AGT-ATGCA 2 -1+2 -2+2+2 -2 -2+2 = 3 ATTGAT--A AGTATG-CA 2 -2+2+2 -2 -1+2 = 3 A-T-TGATA AGTATGC-A 2 -2+2+2 -1 -2+2 = 3 A-T-TGATA l ©Duane Szafron 2000
6 Alignment Algorithms The goal is to find an optimal alignment for a given scoring function as quickly as possible, using a minimum amount of storage. l We will look at three different kinds of algorithms: l – Full Matrix algorithms like Needleman-Wunch and Smith-Waterman – The Hirschberg Algorithm – Fast linear space alignment (Fast. LSA) ©Duane Szafron 2000
7 Matrix Representation A matrix is used to represent all possible alignments for a pair of sequences. l There is a sequence along each axis. l Each path from the top left corner to the bottom right corner represents an alignment solution. l ©Duane Szafron 2000
8 Alignments as Matrix Paths - A G T - A T G C A A T T G A T - - A A T T G A T A ©Duane Szafron 2000
9 Other Alignment Matrix Paths - A G T - A T G C A A T T G A T - - A A T A G T A T G - C A A - T G A T A ©Duane Szafron 2000
10 Other Alignment Matrix Paths - A G T - A T G C A A T T G A T - - A A T A G T A T G - C A A - T G A T A G T A T G C - A A - T G A T A ©Duane Szafron 2000
11 Matrix Alignment Algorithms A matrix algorithm uses a dynamic programming matrix to find an optimal solution. l There are two phases to the algorithms: l – Find. Score – Find. Path ©Duane Szafron 2000
12 Find. Score Description The Find. Score phase applies the scoring matrix to all paths from the upper left to the lower right. l Values are propagated left-to-right, from top -to-bottom. l At the end, the lower right corner is the optimal score. l ©Duane Szafron 2000
13 Find. Score Example A T T G A T A 0 -2 -2 -4 -4 -6 -6 -8 -8 -10 -12 -14 A -2 +2 -4 -3 -6 -5 -4 -7 -10 -6 -12 -11 -14 -10 -16 -2 -4 +2 0 0 -2 -2 -4 -4 -6 -6 -8 -8 -10 G -4 -3 0 +1 -2 -1 -4 0 -6 -5 -8 -7 -10 -9 -12 T -4 -6 -6 -6 -5 -8 0 -2 -2 -2 +2 -4 +1 -1 +2 -1 +3 0 -1 -3 +3 -3 -2 +1 0 -2 +1 -2 -1 -1 -2 -4 -1 -4 0 -3 -4 -6 0 -6 -5 -2 -6 -8 -2 ©Duane Szafron 2000 A T -8 -8 -10 -4 -10 -9 -4 -4 -6 -3 -6 -2 0 0 -2 +1 -2 +2 +1 +1 -1 +2 -1 0 -1 +2 0 +3 0 +1 -3 +3 +1 -2 +1 +5 -2 +1 -1 +2 -1 0 -4 +2 0 -12 -6 -8 -2 -4 +2 0 0 -2 +1 -1 +5 +3 0 G -12 -11 -8 -7 -4 -3 0 +4 -2 -1 -1 0 +3 +4 -2 -14 -8 -10 -4 -6 0 -2 +4 +2 +2 0 +3 +1 +4 C -14 -13 -10 -9 -6 -5 -2 -1 +2 +3 0 +1 +1 +2 +2 -14 -16 -10 -12 -6 -8 -2 -4 +2 0 +3 +1 +1 -1 +2 A -16 -12 -11 -8 -7 -4 -3 0 +4 +1 +2 -1 +3 0 -16 -18 -12 -14 -8 -10 -4 -6 0 -2 +4 +2 +2 0 3
14 Find. Path Description The Find. Path phase starts in the lower right corner. l At each box, a direction is picked: up, left or diagonal based on the highest score that entered the box from those three directions. l If two (or three) directions have equal scores both (all) are optimal paths. l ©Duane Szafron 2000
15 Find. Path Example A T T G A T A 0 -2 -2 -4 -4 -6 -6 -8 -8 -10 -12 -14 A -2 +2 -4 -3 -6 -5 -4 -7 -10 -6 -12 -11 -14 -10 -16 -2 -4 +2 0 0 -2 -2 -4 -4 -6 -6 -8 -8 -10 G -4 -3 0 +1 -2 -1 -4 0 -6 -5 -8 -7 -10 -9 -12 T -4 -6 -6 -6 -5 -8 0 -2 -2 -2 +2 -4 +1 -1 +2 -1 +3 0 -1 -3 +3 -3 -2 +1 0 -2 +1 -2 -1 -1 -2 -4 -1 -4 0 -3 -4 -6 0 -6 -5 -2 -6 -8 -2 ©Duane Szafron 2000 A T -8 -8 -10 -4 -10 -9 -4 -4 -6 -3 -6 -2 0 0 -2 +1 -2 +2 +1 +1 -1 +2 -1 0 -1 +2 0 +3 0 +1 -3 +3 +1 -2 +1 +5 -2 +1 -1 +2 -1 0 -4 +2 0 -12 -6 -8 -2 -4 +2 0 0 -2 +1 -1 +5 +3 0 G -12 -11 -8 -7 -4 -3 0 +4 -2 -1 -1 0 +3 +4 -2 -14 -8 -10 -4 -6 0 -2 +4 +2 +2 0 +3 +1 +4 C -14 -13 -10 -9 -6 -5 -2 -1 +2 +3 0 +1 +1 +2 +2 -14 -16 -10 -12 -6 -8 -2 -4 +2 0 +3 +1 +1 -1 +2 A -16 -12 -11 -8 -7 -4 -3 0 +4 +1 +2 -1 +3 0 -16 -18 -12 -14 -8 -10 -4 -6 0 -2 +4 +2 +2 0 3
16 Cost of Full Matrix Algorithms l l l A full matrix algorithm maintains the entire matrix in memory during both phases (Find. Score and Find. Path) of the algorithm. For sequences of length n and m, this takes nxm entries in memory. Find. Score takes nxm operations (time). Find. Path takes m+n operations (time). If we want to align two sequences of length 10, 000, the storage space is prohibitive (100, 000 entries). ©Duane Szafron 2000
17 The Hirschberg Algorithm - 1 The Hirschberg algorithm is designed to take less space, but find the same optimal solutions. l It splits one sequence into two and performs the Find. Score algorithm on each half, working backwards on the second half sequence. l It does not store all of the results in memory, just the current row of each half matrix (2 xn entries instead of mxn entries). l ©Duane Szafron 2000
18 Hirschberg’s Algorithm At the end of the two Find. Score computations, the final rows of each half matrix are used to find the optimal “crossing-point” of the two “halfalignments”. l The complete algorithm is then called again on the two pairs of half sequences. l This recursion continues until the lengths of the sequences being aligned is 1. l ©Duane Szafron 2000
19 Hirschberg Find. Score Example A T T G A T A - A G T A T G C A - 0 +2 +1 0 -1 +1 -3 -4 -8 -12 -10 -8 -6 -4 -16 -14 -12 -10 -8 -2 -8 -6 ©Duane Szafron 2000 -2 -5 -6 0 -6 -4 0 -3 -4 +2 -4 -2 -4 +2 -2 -2 -2 0
20 Hirschberg Find. Score Example A T T G A T A - 0 A -2 -6 -2 -4 -2 -6 0 0 G T A T G C A -4 -6 -8 -10 -12 -14 -16 -1 +3 +1 +2 0 -2 -4 -3 -6 -4 -12 -8 +2 0 ©Duane Szafron 2000 +1 +3 0 0 -1 +3 +1 -3 -
21 Hirschberg Example Sub-problems There are two optimal splits of the sequences, colored pink and blue. l However, the blue split generates two different optimal solutions, blue and white. AGT ATGCA -ATGCA ATT GATA GAT--A AGTAT G-CA GCA ATT A-T-T GATA l AGTAT GC-A GCA ATT A-T-T GATA ©Duane Szafron 2000
22 Hirschberg Recursion ©Duane Szafron 2000
23 Hirschberg’s Algorithm Hirschberg’s algorithm takes only linear space - 2 xn, instead of quadratic space mxn. l This means that aligning two sequences of length 10, 000 would only require 20, 000 entries instead of 100, 000 entries. l The disadvantage of this algorithm is that the time goes from mxn operations to about 2 xmxn operations since many matrix computations must be redone. l ©Duane Szafron 2000
24 Fast. LSA Idea Fast. LSA improves Hirschberg by reducing the number of re-computations that need to be done. l This makes the algorithm faster. l There are three improvements to reduce computations: l – Sequences are split on both axes, not just one. – Sequences are not just bisected, they are cut into several smaller pieces. – Scores on splitting lines are maintained. ©Duane Szafron 2000
25 Fast. LSA - Algorithm l l l Each sequences is split on both axes. Find. Score is called on a region consisting of 3 quadrants (excluding the lower right). Scores are kept only on the bisecting lines. Fast. LSA is called recursively on the lower right quadrant and the optimal path is eventually returned for this quadrant. Recursive calls are made on part of 1 or 2 of the other 3 quadrants, depending on the path returned from the lower right quadrant. ©Duane Szafron 2000
26 Fast. LSA - Stopping the Recursion l When a block has size u*v < some B, stop the recursion and apply a full matrix algorithm to solve the block. ©Duane Szafron 2000
27 1 Fast. LSA - Using Bisection ©Duane Szafron 2000 2 3 4 6 5 7 9 8 10 11 1 2 3 4 5 6 7 8 9 Fast. LSA(DPM, rs, re, cs, ce) if ((re-rs)*(ce-cs) < B) Full. Matrix(DPM, rs, re, cs, ce); 10 11 3 4 5 7 8 return; 10 E 11 E 3 E 4 5 7 8 else rm = (rs+re)/2; 1 2 6 9 cm = (cs+ce)/2; Find. Scores(DPM, rs, rm, re, cs, cm, ce); 9 1 2 6 Fast. LSA(DPM, rm, re, cm, ce); 1 2 6 9 2 1 6 9 if (direction == diagonal) Fast. LSA(rs, rm, cs, cm) 9 9 E else if (direction == side) 2 6 re = path. end. row; 2 6 Fast. LSA(rm, re, cs, cm); 2 6 if (direction == up) ce = path. end. column; 2 Fast. LSA(rs, rm, cs, ce) 2 2 E 6 else // direction == up 1 ce = path. end. column; 1 Fast. LSA(rs, rm, ce); if (direction == side) 1 re = path. end. row; 1 Fast. LSA(rs, re, cs, cm); 1 1 E
28 Fast. LSA - cuts (k) = 4 ©Duane Szafron 2000
29 Using Fast. LSA If you don’t have enough memory to run a full-matrix algorithm, use Fast. LSA and pick your k-value based on your available memory. l It will run faster than Hirschberg’s algorithm. l ©Duane Szafron 2000
30 Aligning Sub-sequences Sometimes you are trying to align a subsequence with a large sequence. l In this case there should many leading and trailing gaps. l AGATCTGATCGTAAGTCATTCGCATAATGCGT. . . -----GTACGTC-------Score = 25*(-2) + 1*(-1) + 6*2 = -39 AGATCTGATCGTAAGTCATTCGCATAATGCGT. . . -----GTA---C----G--T----C-Score = 25*(-2) + 7*2 = -36 ©Duane Szafron 2000
31 Leading and Trailing Gaps l To score this properly, we assign zero penalties to leading and trailing gaps. AGATCTGATCGTAAGTCATTCGCATAATGCGT. . . -----GTACGTC-------- Score = 25*(0) + 1*(-1) + 6*2 = 11 AGATCTGATCGTAAGTCATTCGCATAATGCGT. . . -----GTA---C----G--T----C-Score = 12*(0) 13*(-2) + 7*2 = -8 ©Duane Szafron 2000
32 Implementing Leading Gaps A T T G A T A 0 0 0 0 A 0 +2 0 -1 -2 +2 -2 0 -2 +2 0 0 -2 -2 -4 -4 -6 +2 0 0 -2 +2 G 0 -1 0 +1 -2 -1 -4 0 -6 -5 0 +1 -2 -1 0 T 0 0 0 -2 -1 -2 +2 -3 +1 -1 +2 -1 +3 0 -1 -3 +3 -3 -2 +1 0 -2 +1 -2 -1 -1 0 -2 -1 -2 +2 -3 +1 -1 +2 -1 0 0 0 -2 0 ©Duane Szafron 2000 A 0 +2 -3 -2 0 +1 +1 +2 -1 +3 -3 -1 0 +4 -2 0 -2 +2 0 0 -2 +1 -1 +2 0 +3 +1 +1 -1 +4 T 0 -1 0 +4 -2 +2 -1 0 0 +1 +1 +5 -1 0 +2 0 -2 +4 +2 +2 0 0 -2 +1 -1 +5 +3 +3 G 0 -1 -2 -1 +2 +3 0 +4 -2 -1 -1 0 +3 +4 +1 0 -2 -1 -3 +2 0 +3 +1 +4 +2 +2 0 +3 +1 +4 C 0 -1 -3 -2 0 +1 +1 +2 +2 +3 0 +1 +1 +2 +2 0 -2 -1 -3 0 -2 +1 -1 +2 0 +3 +1 +1 -1 +2 A 0 +2 -3 -2 -2 -1 -1 0 0 +4 +1 +2 -1 +3 0 0 -2 +2 0 -2 -4 -1 -3 0 -2 +4 +2 +2 0 3
33 New optimal path - same score 3 A T T G A T A 0 0 0 0 A 0 +2 0 -1 -2 +2 -2 0 -2 +2 0 0 -2 -2 -4 -4 -6 +2 0 0 -2 +2 G 0 -1 0 +1 -2 -1 -4 0 -6 -5 0 +1 -2 -1 0 T 0 0 0 -2 -1 -2 +2 -3 +1 -1 +2 -1 +3 0 -1 -3 +3 -3 -2 +1 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