Debian Codegraph @

atac-driver/chainer/localalign/GF_ALN_dpaligner.C

raw symbols SCIP
1 // This file is part of A2Amapper.
2 // Copyright (c) 2004 Applera Corporation
3 // Author: Clark Mobarry
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
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.
14 //
15 // You should have received (LICENSE.txt) a copy of the GNU General Public
16 // License along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19
20 /* Dynamic programming sequence comparison of two fragments. General
21 purpose utility that uses bit-vector d.p. for detection (see, "A Fast
22 Bit-Vector Algorithm for Approximate String Matching on Dynamic
23 Programming" J. ACM., to appear, by Gene Myers.) and the O(kn) greedy
24 algorithm for alignment delivery (see "An O(ND) Difference Algorithm
25 and Its Variations" Algorithmica 1 (1986), 251-266, by Gene Myers.)
26 Both papers can be downloaded from
27 "http://www.cs.arizona.edu/people/gene/vita.html"
28 */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <math.h>
34 #include <assert.h>
35
36 #include "GF_ALN_local.H"
37
38 /* O(kn) identity-based alignment algorithm. Find alignment between
39 a and b (of lengths alen and blen), that begins at finishing
40 boundary position *spnt. Return at *spnt the diagonal at which the
41 alignment starts. */
42
43 int *AS_ALN_OKNAlign(const char *a, int alen, const char *b, int blen, int *spnt, int diff) {
44 int diag, wpos, level;
45 int fcell, infinity;
46
47 static int Wtop = -1;
48 static int *Wave;
49 static int *TraceBuffer;
50
51 if (diff >= Wtop) /* Space for diff wave? */
52 { int max, del, *newp;
53
54 max = (int)(1.2*diff) + 50;
55 del = (max+5)*(max+1);
56 //fprintf(stderr,"DP_COMPARE (AS_ALN_OKNAlign): reallocing %ld bytes\n",del*sizeof(int)+(max+1)*sizeof(int));
57 newp = (int *) realloc(Wave,del*sizeof(int) + (max+1)*sizeof(int));
58 if (newp == NULL) return (NULL);
59 Wtop = max-1;
60 Wave = newp;
61 TraceBuffer = (int *) (Wave + del);
62 }
63
64 diag = (alen-blen) + (*spnt); /* Finish diagonal. */
65 infinity = blen+2;
66
67 /* Process 0-wave. */
68
69 { int i, j;
70
71 if (diff == 0) goto zeroscript;
72
73 if ((*spnt) < 0) /* (i,j) = initial boundary pt. */
74 j = blen;
75 else
76 j = blen - (*spnt);
77 i = diag + j;
78
79 while (1)
80 { if (i <= 0 || j <= 0) goto zeroscript;
81 if (a[i] != b[j]) break;
82 i -= 1;
83 j -= 1;
84 }
85
86 Wave[0] = Wave[1] = infinity;
87 Wave[2] = j;
88 Wave[3] = Wave[4] = infinity;
89 }
90
91 /* Compute waves 1 through d-1 do, each wave has
92 two boundary cells at each of its ends. */
93
94 { int m, n, k;
95
96 m = 5;
97 n = 0;
98 for (level = 1; 1; level++)
99 { Wave[m++] = infinity;
100 Wave[m++] = infinity;
101 n += 1;
102 for (k = -level; k <= level; k++)
103 { int i, j;
104
105 j = Wave[n] - 1;
106 if ((i = Wave[n-1]-1) < j)
107 j = i;
108 if ((i = Wave[n+1]) < j)
109 j = i;
110 i = (diag+k) + j;
111 while (1)
112 { if (i <= 0 || j <= 0)
113 { if (i <= 0)
114 *spnt = -j;
115 else
116 *spnt = i;
117 goto madeit;
118 }
119 if (a[i] != b[j]) break;
120 i -= 1;
121 j -= 1;
122 }
123 Wave[m++] = j;
124 n += 1;
125 }
126 Wave[m++] = infinity;
127 Wave[m++] = infinity;
128 n += 1;
129 }
130
131 madeit:
132
133
134 fcell = n;
135 wpos = k;
136 }
137
138 /* Trace back through wave structure and record
139 trace of the alignment traced. */
140
141 { int d, n, k, t;
142
143 t = 0;
144 n = fcell;
145 k = wpos;
146 for (d = level-1; d >= 0; d--)
147 { int i, j, m;
148
149 j = Wave[m=n]-1;
150 if ((i = Wave[n-1]-1) < j)
151 { j = i; m = n-1; }
152 if ((i = Wave[n+1]) < j)
153 { j = i; m = n+1; }
154 if (m < n)
155 { TraceBuffer[t++] = - ((diag+k) + (j+1));
156 k -= 1;
157 }
158 else if (m > n)
159 { TraceBuffer[t++] = j+1;
160 k += 1;
161 }
162 n = m - (2*d+4);
163 }
164 TraceBuffer[t] = 0;
165 }
166
167 return (TraceBuffer);
168
169 /* If perfect match, your done. */
170
171 zeroscript:
172 TraceBuffer[0] = 0;
173 *spnt = diag;
174 return (TraceBuffer);
175 }