



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Vol.4, n o. 1. 1988 Pages 1 1 - 1 7
Abstract Space, not time, is often the limiting factor when computing optimal sequence alignments, and a number of recent papers in the biology literature have proposed space-saving strategies. However, a 1975 computer science paper by Hirschberg presented a method that is superior to the new proposals, both in theory and in practice. The goal of this paper is to give Hirschberg's idea the visibility it deserves by developing a linear-space version ofGotoh's algorithm, which accommodates affine gap penalties. A portable C-software package implement- ing this algorithm is available on the BIONETfree of charge.
Introduction Consider the following problem. Given sequences A = a{a 2 ...aM and B = b\b 2 ...bN, find a set of 'evolutionary opera- tions' that converts A to B and minimizes the sum of the opera- tions' costs. The allowed operations are (i) replace one symbol by another, (ii) delete k consecutive symbols, or (iii) insert k consecutive symbols. In addition, the problem statement re- quires that every symbol of A must be either replaced or deleted. Replacement costs are specified by a table, w, where w(a,b) gives the cost of replacing a by b. Note that a symbol of A is effectively left unedited if it is replaced by itself at no cost, i.e. w(a,a) = 0. Two non-negative constants, g and h, specify an affine function, gap{k) = g + hk, for the cost of a ^-symbol indel (insertion or deletion). Informally, opening up a gap costs g and each symbol in the gap costs h. The problem is often formulated as maximizing the similari- ty score of an alignment, rather than minimizing the difference score of a conversion. A bonus a(a,b) is added for every aligned pair (a,b) and a 'gap penalty' q + rk is subtracted for every fc-symbol gap. This formulation is converted to a difference problem by the transformations
w(a,b) = ffmax - a{a,b) for all pairs (a,b) 8 = q h = r + '/2(Tmax
(^1) Department of Computer Science. University of Arizona, Tucson, AZ85721. USA Department of Computer Science, The Pennsylvania State University, Univer- sity Park, PA 16802. USA
where amax = maX(a y^a{a,b) (Smith et al., 1981). Thus, to produce an alignment that maximizes the similarity score, first apply these transformations and then run the program describ- ed in this paper with the resulting w, g and h. If the minimum conversion score is C, then the corresponding maximum align- ment score is xh(M + iV)crmax — C. Gotoh (1982) gave an algorithm that solves such problems in 0(MN) time. If only the minimum cost is desired, then it is easy to implement the algorithm in 0(/V) space, where N can be taken as the shorter sequence length. If one also desires a set of operations attaining the minimum cost, then straightfor- ward implementations need 0(MN) space. In practice, this space requirement often limits the method's applicability, and several papers (Taylor, 1984; Watanabe et al., 1985; Altschul and Erickson, 1986; Gotoh, 1986, 1987) have presented strategies that reduce space consumption by constant factors. These papers fail to note that Hirschberg (1975) showed how to produce an optima] conversion or alignment in O(M) space. When only a single optimal alignment of A and B is desired, Hirschberg's approach is superior to the others. For example, in one megabyte of memory, our program based on Hirschberg's method can align two sequences of length 62 500. Altschul and Erickson (1986) propose keeping 7 bits for each of MN entries, so the limit for their method is 77V^2 < 8 X 10^6 , or N < 1070. Moreover, any program that packs and unpacks bits or uses disk storage is doomed to be slow and, probably, non-portable. O(MN)-space methods permit the construction of all optimal alignments. However, the number of alignments that attain the minimum cost is often astronomical, in part because a brute force enumeration lists many arrangements whose differences are insignificant to the user. Moreover, when one is searching for a particular 'biologically meaningful' arrangement, it may be necessary to consider slightly sub-optimal alignments (Water- man, 1983; Waterman and Byers, 1985). One alternative to explicitly constructing all optimal alignments is to modify our linear-space program to produce 'left-most' and 'right-most' optimal alignments that delineate the range of possibilities. In any case, it is important to understand that a single optimal align- ment can be found in far less space than is needed to record 'traceback' information for finding all optimal alignments. Hirschberg's original presentation treats a simpler alignment problem, known as the longest common subsequence problem, where w(a,b) = 1 if a ^ b, w(a,a) = 0, and gap{k) = k. However, the approach is quite general. To the best of our
© IRL Press Limiled, Oxford, England 11
E.W.Myers and W.Miller
knowledge, any sequence comparison algorithm whose 'cost- only' version runs in O(N) space can be adapted to produce an optimal alignment in O(N) space. For example, Myers (1986) accomplished this for a 'greedy' alignment algorithm that is quite different from the traditional dynamic programming ap- proach. Miller and Myers (1988) applied Hirschberg's techni- que to a concave gap penalty algorithm that subsumes Gotoh's algorithm as a special case. In this note we apply Hirschberg's technique to Gotoh's algorithm. Limiting consideration to a relatively simple method yields a simple and novel development that we hope will bring Hirschberg's idea to a wider audience. Moreover, for affine indel costs, the more general concave-weights software (Miller and Myers, 1988) runs 3.0 times slower and uses 3.5 times more space than the program described in this paper.
System and methods
C software implementing the algorithm was written and tested on a Vax 11/780 running 4.3 BSD Unix. The program is port- able: setting an appropriate compilation constant adapts the soft- ware to a machine with a different memory capacity. The only requirement is an ANSI-standard C compiler and accompany- ing standard I/O library.
The Algorithm
Computing the cost in linear space Let Aj denote the /-symbol prefix a,a 2 -••«, of A and let B, denote b\b 2 ...bj. Define
QiJ) = minimum cost of a conversion of A,- to Bj D(iJ) = minimum cost of a conversion of /), to B that deletes a,- l(i,j) = minimum cost of a conversion of A/ to B. that inserts £>•
Note that D(i,j) is properly defined only when / > 0, and /(/,/) only fory > 0. Gotoh (1982) showed how to compute the C, D and / matrices in 0{MN) time. Below we present Gotoh's method, where we have treated the boundary conditions careful- ly by defining D(0,j) and /(/,0) appropriately. The values C(iJ) satisfy the recurrence relations:
QiJ) =
min\D( gap(j) gap(i) 0
_niahbj)_ if i > O a n d ; > 0 if / = 0 and j > 0 if i > 0 and; = 0 [*] if / = 0 and j = 0
For ij > 0, an optimal conversion of A-, to Bj ends with either (i) a delete, (ii) an insert or (iii) the replacement of a, by bj. Thus, the first line above follows readily. Fory > 0, an op- timal conversion of Ao (the empty sequence) to Bj must insert
ally symbols, so C(0,j) = gap(j) and the second line follows. The remaining two lines follow similarly. In the recurrence, and in Figures 1A and IB, certain lines are starred because they are subsequently modified. As noted earlier, we are free to pick a definition of D(0,J). It is convenient to set D(0,j) = C(0,j) + g fory > 0. More- over, we need not compute D(i,0) for / > 0, since other quan- tities do not depend on these values. Then
(m\n\D(i-,j). C(i-l.j) + g\ + h if i > 0 and; > 0 ~[C(0,J) + g if / = 0 and; > 0
If / > 1, then extending an optimal conversion of /4,, to Bj so that it deletes a, adds g 4- h to its cost, or h if it ends by deleting a,-,. This reasoning confirms the first line for /' >
/ /,;-D, _Qij-) +
+ h if / > 0 and ;' > 0 if / > 0 and ; = 0
The recurrence relations of C, D and / lead to the algorithm of Figure 1 A, which uses a variable t that runs through the se- quence of values gap(l), gap(2),.... Values in the (th rows of C and D depend only on values in rows / and i—l, while values in the ;th row of/depend only on values in row /. This means that a handful of row-sized vec- tors are adequate to compute successive rows. In fact, with a little care, two vectors suffice: if CC and DD contain the (/'-1 )st rows of C and D, then the /th rows may be computed by over- writing values for the (/— l)st rows in a left-to-right sweep with the aid of three scalars, e, c and s. Specifically, if ij > 0, then immediately before C(iJ), D(i,j) and /(/,/) are assigned to CC(j), DD(j) and e respectively, we have:
CC(k) =
DD(A-) =
(/,) if* k < j (/-I.A) if * > j (i.k) if k < j (i-l.k) if k > ; e = /(/,;-1) c = QiJ-I) s = Qi-lJ-)
With this loop-invariant condition in mind, the O(N) space cost- only variation of Figure IB is readily understood.
Example J. Let w(a,b) = 1 if a =£ b, w(a,a) = 0 and gap(k) = 2 4- 0.5k. The unique optimal conversion of agtac to aag
E.W.Myers and W.Miller
and 55(0), which are needed below. This is easily rectified by observing that DD(0) = CC(0) and 55(0) = RR(0). Given the vectors above, the midpoint of an optimal conver- sion can be found using the following observation. For any con- version of A to B, there exists a y € [0,/V] such that the conversion is the concatenation of either (1) a conversion of Aj* to Bj and a conversion of Af* to Bjor (2) a conversion of Aj* to Bj ending with a delete and a conversion of Af* to Bj beginning with a delete, in which case the deletions bracketing the concatenation point must be coalesced into a single opera- tion. For fixedy, the minimum cost of a type 1 conversion with midpoint (i*J) is CC(j)+RR(N-j), i.e. the minimum cost of a conversion of A/* to B. plus the minimum cost of a conver- sion of A'* to Bj. Similarly, the minimum cost of a type 2 con- version is DD(j) + SS(N—J) - g, where g is subtracted because bracketing deletes are coalesced into a single operation, i.e. gap{x+y) = gap(x) + gapiy) - g. Thus, the optimal cost of converting A to B is
min,e ,0.^1 min(CC(y) + RR(N-j),DD(J) 4- _SS(N-j) -g) _
If the minimum is attained aty, then _(iJ)_ is an optimal mid- point for the problem. When several values attain the minimum, the method of breaking ties determines whether the 'left-most' or 'right-most' optimal alignment is selected. Given an optimal midpoint _(iJ),_ an optimal conversion can then be delivered by (i) recursively finding an optimal conver- sion of _Aj_ to Bj*. (ii) recursively finding an optimal conver- sion of A-* to Bj,, and (iii) concatenating these two partial conversions, being sure to coalesce bracketing deletes in the type 2 case. The splitting of the comparison problem for A and B into two smaller problems is pictured in Figure 2. The outer rectangle is the M X Af C-matrix for A and B. The singly hat- ched rectangles depict the sub-problems whose solutions are to be concatenated, and the doubly hatched rectangles depict sub-sub-problems. The dashed line indicates the eventual op- timal alignment. With type 2 midpoints, one must further constrain the first recursive call to conversions that end with a delete, and the second to conversions that begin with a delete. For example, the second sub-problem may have a script not beginning with a delete that is better when considered in isolation. However, since an initial delete is not charged the gap initialization penalty g for type 2 midpoints, a conversion beginning with a delete is actually superior when concatenated with the conversion for the first sub-problem. Considering sub-sub-problems, it becomes apparent that, in general, a recursive call may be re- quired to consider only conversions that begin with, end with, or both begin and end with a delete. The most elegant solution for type 2 midpoints is to split the problem into three parts: (i) an optimal conversion of /!,•_ i to _Bj,_ where final deletes are not charged the gap initialization penalty g, (ii) deletion of a,rfj,«+|, and (iii) an optimal conver-
Fig. 2. Splitting the problem into sub-problems.
sion of Aj*+] to Bj*, where initial deletes are not charged g. For a cost-only problem where initial deletes are not charged for gap initialization, it suffices to simply subtract g from the starred line in the recurrence for C given in the previous sub- section. This is equivalent to setting / to 0, as opposed to g, in the starred lines of Figure 1A and B. Thus, in the forward phase, CC and DD are computed with this slight alteration if initial gaps are not to be penalized g. Because the latter half of a conversion is computed in the reverse phase, it suffices to use the same alteration when computing RR and 55 on the reversed sequences in order to not penalize final gaps. To im- plement these conditional alterations, the algorithm diff in Figure 3 has parameters tb and te that are used to initialize t in the starred lines for the forward and reverse phases. The caller passes g if initial/final deletes are to be charged for gap initializa- tion, and 0 otherwise. The recursion's boundary cases, N = 0 and M < 1, are handled by exhaustive examination of all possible optimal con- versions. When N = 0, the only possibility is to delete A. When M = 0, the only possibility is to insert B. When M = 1, an optimal conversion is the least costly of (i) inserting B and deleting A = a, or (ii) inserting By_|, replacing a, by bJt and inserting BJ, for some j 6 [ 1 ,N]. Conversion (i) costs gap{ 1)
Optimal alignments in linear space
shared vectors CC [O.JVf ], DD [O.M ], RR [O.JV], 55[0.JV] procedure DWF(A,B,M,N) { diff(A,B.M,N,g,g)} recursive procedure diff (A ,B,M,N,tb,te) ( UN = 0 t h e n { if M > 0 then write "delete A " } else if M = 0 then write "insert B" else if M = 1 then write conversion of cost min ( (min(tb ,te}t-h yi-gap {N), min {gap (j-1 (<» I A }+gap(N - j ))} else { i <- |M/2J Compute CC and I>D in a forward phase, replacing [] of Fig. IB with** "t *-tb* ". Compute RR and 55 in a reverse phase, replacing [] of Fig. IB with "t<-te". Find _je[0,N]_* minimizing min(CC(j)+RR(N-j),DD(j) + SS(N-j)-g) *if («,/•) is type 1 then
,',* _j.ib,g) J.M-i,N-j,g,te)_*
else ( _&B(Ai._l,Br ,i -l,f,tb,0)_* write "delete a^a^+i" (^1) ;I+1 _,BJ.,M -i_* - _l.N-j,O,te)_*
Fig. 3. Skeleton of Gotoh's algorithm in O(N) space.
delete operations bracketing a type 2 midpoint are not coalesc- ed. Our software package rectifies this deficiency by buffering the last operation to be written and coalescing it with the next as necessary.
Example 2. Given the cost and sequences of example 1, the algorithm of Figure 3 first applies diff to sequences agtac and aag, where M = 4, N = 3 and /* = 2. The computed vectors are
CC: 3.0 2.5 1.0 2. RR: 3.5 4.0 3.5 2.
DD: 3.0 2.5 5.0 5. SS: 3.5 4.0 3.5 6.
There are eight possible ways to divide the problem, i.e. two types of midpoints for eachy E [0,N]. The corresponding costs
are
7=0 j=\ j=2 7= type 1 midpoint 5.0 6.0 5.0 6. type 2 midpoint 7.0 4.0 7.0 7.
The optimum choice from among the eight possibilities is a type 2 midpoint at j * = 1. This corresponds to combining (i) a minimum-cost conversion of A,* = ag to B, = a that ends
with a delete and (ii) a minimum-cost conversion of AJ* = tac to Bj = ag that begins with a delete. Combining the two scripts and adjusting the sum of their costs to account for the fact that only one gap initialization penalty is required, gives the cost DD() + 55(2) - 2 = 4. The problem is thus decomposed into the problems of op- timally converting At = a to Bt = a, deleting a 2 a 3 = gt, and optimally converting _A_ = ac to B] = ag. These two required optimal subconversions are generated by recursive calls to diff. For the first call, M = N = 1 and the final 'boundary case' of diff produces the script of cost gap(0) + w(a,a) + gap(0) = 0, i.e. replacement of a by a. The second call divides the conversion of ac to ag into a conversion of a to a followed by a conversion of a to g, each of which is generated by a third- level call to diff with M = N = 1.
Performance. The algorithm uses O{N + log M) space: O(N) for the globally shared vectors and O(log M) for the implicit activation stack needed for no more than [log M]+ 1 levels of recursion. The time required is approximately twice that for the cost-only version. There exist constants IT and T such that the time taken in the body of diff for an M x N problem is not more than ir(M + N) for the boundary cases and TMN for the recursive cases. It follows by induction that the total time
Optimal alignments in linear space
... gives the editing operations in a left-to-right conversion where integers encode operations as follows:
0 = > replace —k=> delete k symbols +k = > insert k symbols.
The script is guaranteed to have the properties: (i) inserts are never followed by inserts; (ii) deletes are never follow- ed by deletes or inserts; (iii) a replacement followed by a &-indel is always preferred to a /:-indel followed by a replace- ment if both have the same cost. DIFF returns —1.0 if NMAX is not large enough.
int DISPLAY(A,B,M,N,S) int M,N; char A[], B[]; int S[]; DISPLA Y places on the standard output a display of the align- ment implied by the conversion S computed in the call DIFF(A,B,M,N,?,?,?,S). For example:
ggcgt (I cat accggcgagga ct agagat cccagat gcagcct cgat a
g cgt I cataaccggcgaggt acct agacal t cccagagc gcct cgat a
50
taggaagaa tc agcaacgal cggcat g
tggacagaaatcgagcaacga cgac tg
Discussion
This paper develops a linear-space algorithm for producing opti- mal sequence alignments with affine gap costs. It is superior in theory and practice to other approaches. By avoiding the use of secondary storage and bit operations, it yields fast and por- table software. The underlying divide-and-conquer strategy, taken from a 1975 paper of Hirschberg, is quite general. Many, perhaps all, cost-only alignment algorithms yield an alignment-delivering variation with identical asymptotic time and space complexities. When applied to certain other alignment algorithms, the space requirement becomes sublinear (Myers, 1986), linear (Wagner and Fischer, 1974; Masek and Paterson, 1980), or linear in expectation (Miller and Myers, 1988). Occasionally, the varia- tion is not space-efficient, as with the method of Waterman et al. (1976), whose cost-only version needs 0{MN) space.
In practice, employing the strategy at most doubles the time and space requirements of the cost-only version. Indeed, with greedy methods (Fickett, 1984; Ukkonen, 1985, Section 3; Miller and Myers, 1985; Myers, 1986), the midpoint computa- tion is twice as efficient as a one-pass cost-only computation, implying that the time overhead of the divide-and-conquer ap- proach is negligible.
Acknowledgments Stephen Altschul, David Lipman, and the referee made suggestions that im- proved the presentation of this paper. The work of E.W.M. was supported in part by NSF Grant DCR-8511455.
References
Altschul,S. and Erickson.B.W. (1986) Optimal sequence alignments using af- fine gap costs. Bull. Math. Biol., 48. 606-616. FicketU.W. (1984) Fast optimal alignment. Nucleic Acids Res.. 12. 175-179. Gotoh.O. (1982) An improved algorithm for matching biological sequences. J. Mol. Biol.. 162. 705-708. Gotoh.O. (1986) Alignment of three biological sequences with an efficient traceback procedure. J. Vieor. Biol.. 121, 327-337. Gotoh.O. (1987) Pattern matching of biological sequences with limited storage. CABIOS. 3, 17-20. Hirschberg,D.S. (1975) A linear space algorithm for computing longest com- mon subsequences. Commun. Assoc. Comput. Mach.. 18, 341-343. Masek,W.J. and Paterson.M.S. (1980) A faster algorithm for computing string- edit distances. J. Comput. System Sci., 20, 18—31. Miller,W. and Myers,E.W. (1985) A file comparison program. Software — Practice and Experience, 15, 1025—1040. Miller.W. and Myers,E.W. (1988) Sequence comparison with concave weighing functions. Bull. Math. Biol., in press. Myers,E.W. (1986) An 0(ND) difference algorithm and its variations. Algorithmica, 1, 251-266. Smith.T.F., Waterman,M.S. and Fitch,W.M. (1981) Comparative biosequence metrics. J. Mol. Evoi, 18, 38-46. Taylor,P. (1984) A fast homology program for aligning biological sequences. Nucleic Acids Res., 12, 447-455. Ukkonen,E. (1985) Algorithms for approximate string matching. Information and Control, 64, 100-118. Wagner,R.A. and Fischer.M.J. (1974) The string-to-string correction problem. J. ACM, 21, 168-173. Watanabe.K., Urano.Y. and Tamaoki.T. (1985) Optimal alignments of biological sequences on a microcomputer. CABIOS, 1, 83 — 87. Waterman.M.S. (1983) Sequence alignment in the neighborhood of the optimum. Proc. Natl. Acad. Sci. USA, 80, 3123-3124. Waterman.M.S. and Byers,T.H. (1985) A dynamic programming algorithm to find all solutions in a neighborhood of the optimum. Math. Biosciences, 77, 179-188. Waterman,M.S. Smith.T.F. and Beyer.W.A. (1976) Some biological sequence metrics. Adv. Math.. 20. 367-387.
Received on October 14, 1987; accepted on December 19. 1987
Circle No. 8 on Reader Enquiry Card