-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllmax0.c
429 lines (386 loc) · 9.64 KB
/
llmax0.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
>HEADER
Copyright (c) 2004 Haixu Tang [email protected]
This file is part of the RepGraph package.
RepGraph is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RepGraph is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RepGraph. If not, see <http://www.gnu.org/licenses/>.
<HEADER
**/
/* A PACKAGE FOR SEQUENCE COMPARISON WITH AFFINE WEIGHTS */
/* Maximizes a similarity score and doesn't penalize end-gaps */
/* Globally passed params and macros */
#include <stdio.h>
#ifdef BIGMEM
#define NMAX 80000
#else
#define NMAX 3000
#endif
static int CHECK_SCORE();
static int (*w)[15]; /* w = W */
static int g, h, m; /* g = G, h = H, m = g+h */
#define gap(k) ((k) <= 0 ? 0 : g+h*(k)) /* k-symbol indel cost */
static int *sapp; /* Current script append ptr */
static int last; /* Last script op appended */
/* Append "Delete k" op */
#define DEL(k) \
{ if (last < 0) \
last = sapp[-1] -= (k); \
else \
last = *sapp++ = -(k); \
}
/* Append "Insert k" op */
#define INS(k) \
{ if (last < 0) \
{ sapp[-1] = (k); *sapp++ = last; } \
else \
last = *sapp++ = (k); \
}
#define REP { last = *sapp++ = 0; } /* Append "Replace" op */
static int CC[NMAX+1], DD[NMAX+1]; /* Forward cost-only vectors */
static int RR[NMAX+1], SS[NMAX+1]; /* Reverse cost-only vectors */
/* align(A,B,M,N,tb,te) returns the cost of an optimum conversion between
A[1..M] and B[1..N] that begins(ends) with a delete if tb(te) is zero
and appends such a conversion to the current script. */
static int align(A,B,M,N,tb,te,topr,botr,lc,rc) char *A, *B; int M, N;
int tb, te; char topr, botr, lc, rc;
{ int midi, midj, type; /* Midpoint, type, and cost */
int midc;
int c1, c2;
{ register int i, j;
register int c, e, d, s;
int t, *wa;
/* Boundary cases: M <= 1 or N == 0 */
if (N <= 0)
{ if (M > 0) DEL(M)
if (topr || botr) return 0;
else return -gap(M);
}
if (M <= 1)
{ if (M <= 0)
{ INS(N);
if (topr || botr) return 0;
else return -gap(N);
}
if (topr) {
if (rc) midc = 0;
else midc = te-h;
midj = 0;
wa = w[A[1]];
for (j = 1; j <= N; j++)
{ c = wa[B[j]] - gap(N-j);
if (c > midc)
{ midc = c;
midj = j;
}
}
} else if (botr) {
if (lc) midc = 0;
else midc = tb-h;
midj = 0;
wa = w[A[1]];
for (j = 1; j <= N; j++)
{ c = -gap(j-1) + wa[B[j]];
if (c > midc)
{ midc = c;
midj = j;
}
}
} else {
if (tb < te) tb = te;
if (lc || rc) midc = -gap(N);
else midc = (tb-h) - gap(N);
midj = 0;
wa = w[A[1]];
for (j = 1; j <= N; j++)
{ c = -gap(j-1) + wa[B[j]] - gap(N-j);
if (c > midc)
{ midc = c;
midj = j;
}
}
}
if (midj == 0)
{ INS(N) DEL(1) }
else
{ if (midj > 1) INS(midj-1)
REP
if (midj < N) INS(N-midj)
}
return midc;
}
/* Divide: Find optimum midpoint (midi,midj) of cost midc */
midi = M/2; /* Forward phase: */
CC[0] = 0; /* Compute C(M/2,k) & D(M/2,k) for all k */
if (topr) {
for (j = 1; j <= N; j++)
{ CC[j] = 0;
DD[j] = -g;
}
} else {
t = -g;
for (j = 1; j <= N; j++)
{ CC[j] = t = t-h;
DD[j] = t-g;
}
}
t = tb;
for (i = 1; i <= midi; i++)
{ s = CC[0];
if (lc) {
CC[0] = c = 0;
e = -g;
} else {
CC[0] = c = t = t-h;
e = t-g;
}
wa = w[A[i]];
for (j = 1; j <= N; j++)
{ if ((c = c - m) > (e = e - h)) e = c;
if ((j == N) && rc) {
if ((c = CC[j]) > (d = DD[j])) d = c;
} else {
if ((c = CC[j] - m) > (d = DD[j] - h)) d = c;
}
c = s + wa[B[j]];
if (e > c) c = e;
if (d > c) c = d;
s = CC[j];
CC[j] = c;
DD[j] = d;
}
}
DD[0] = CC[0];
RR[N] = 0; /* Reverse phase: */
/* Compute R(M/2,k) & S(M/2,k) for all k */
if (botr) {
for (j = N-1; j >= 0; j--)
{ RR[j] = 0;
SS[j] = -g;
}
} else {
t = -g;
for (j = N-1; j >= 0; j--)
{ RR[j] = t = t-h;
SS[j] = t-g;
}
}
t = te;
for (i = M-1; i >= midi; i--)
{ s = RR[N];
if (rc) {
RR[N] = c = 0;
e = -g;
} else {
RR[N] = c = t = t-h;
e = t-g;
}
wa = w[A[i+1]];
for (j = N-1; j >= 0; j--)
{ if ((c = c - m) > (e = e - h)) e = c;
if ((j == 0) && lc) {
if ((c = RR[j]) > (d = SS[j])) d = c;
} else {
if ((c = RR[j] - m) > (d = SS[j] - h)) d = c;
}
c = s + wa[B[j+1]];
if (e > c) c = e;
if (d > c) c = d;
s = RR[j];
RR[j] = c;
SS[j] = d;
}
}
SS[N] = RR[N];
midc = CC[0]+RR[0]; /* Find optimal midpoint */
midj = 0;
type = 1;
for (j = 0; j <= N; j++)
if ((c = CC[j] + RR[j]) >= midc)
if (c > midc || CC[j] != DD[j] && RR[j] == SS[j])
{ midc = c;
midj = j;
}
if (rc) {
if ((c = DD[N] + SS[N]) > midc)
{ midc = c;
midj = N;
type = 2;
}
} else {
if ((c = DD[N] + SS[N] + g) > midc)
{ midc = c;
midj = N;
type = 2;
}
}
for (j = N-1; j > 0; j--)
if ((c = DD[j] + SS[j] + g) > midc)
{ midc = c;
midj = j;
type = 2;
}
if (lc) {
if ((c = DD[0] + SS[0]) > midc)
{ midc = c;
midj = 0;
type = 2;
}
} else {
if ((c = DD[0] + SS[0] + g) > midc)
{ midc = c;
midj = 0;
type = 2;
}
}
}
/* Conquer: recursively around midpoint */
if (midj == 0 || midj == N) {
if (type == 1)
{ align(A,B,midi,midj,tb,-g,topr,0,lc,rc);
align(A+midi,B+midj,M-midi,N-midj,-g,te,0,botr,lc,rc);
}
else
{ align(A,B,midi-1,midj,tb,0,topr,0,lc,rc);
DEL(2);
align(A+midi+1,B+midj,M-midi-1,N-midj,0,te,0,botr,lc,rc);
}
} else {
if (type == 1)
{ align(A,B,midi,midj,tb,-g,topr,0,lc,0);
align(A+midi,B+midj,M-midi,N-midj,-g,te,0,botr,0,rc);
}
else
{ align(A,B,midi-1,midj,tb,0,topr,0,lc,0);
DEL(2);
align(A+midi+1,B+midj,M-midi-1,N-midj,0,te,0,botr,0,rc);
}
}
return midc;
}
/* Interface and top level of comparator */
int GALIGN0(A,B,M,N,W,G,H,S) char A[],B[]; int M,N; int W[][15],G,H; int S[];
{
int c, ck;
int t;
// if (N > NMAX) return -1; /* Error check */
w = W; /* Setup global parameters */
g = G;
h = H;
m = g+h;
sapp = S;
last = 0;
c = align(A,B,M,N,-g,-g,1,1,1,1); /* OK, do it */
if ((abs(S[0]) < abs(S[1])) && S[0] != 0) {
t = S[1];
S[1] = S[0];
S[0] = t;
}
if ((abs(sapp[0]) < abs(sapp[-1])) && sapp[0] != 0) {
t = sapp[-1];
sapp[-1] = sapp[0];
sapp[0] = t;
}
ck = CHECK_SCORE(A,B,M,N,S);
if (c != ck) printf("Check_score error. c=%d, ck=%d\n",c,ck);
return c;
}
/* Alignment display routine */
static char ALINE[51], BLINE[51], CLINE[51];
int DISPLAY(A,B,M,N,S,AP,BP) char A[], B[]; int M, N; int S[], AP, BP;
{ register char *a, *b, *c;
register int i, j, op;
int lines, ap, bp;
i = j = op = lines = 0;
ap = AP;
bp = BP;
a = ALINE;
b = BLINE;
c = CLINE;
while (i < M || j < N)
{ if (op == 0 && *S == 0)
{ op = *S++;
*a = A[++i];
*b = B[++j];
*c++ = (*a++ == *b++) ? '|' : ' ';
}
else
{ if (op == 0)
op = *S++;
if (op > 0)
{ *a++ = ' ';
*b++ = B[++j];
op--;
}
else
{ *a++ = A[++i];
*b++ = ' ';
op++;
}
*c++ = '-';
}
if (a >= ALINE+50 || i >= M && j >= N)
{ *a = *b = *c = '\0';
printf("\n%5d ",50*lines++);
for (b = ALINE+10; b <= a; b += 10)
printf(" . :");
if (b <= a+5)
printf(" .");
printf("\n%5d %s\n %s\n%5d %s\n",ap,ALINE,CLINE,bp,BLINE);
ap = AP + i;
bp = BP + j;
a = ALINE;
b = BLINE;
c = CLINE;
}
}
}
/* CHECK_SCORE - return the score of the alignment stored in S */
static int CHECK_SCORE(A,B,M,N,S) char A[], B[]; int M, N; int S[];
{
register int i, j, op;
int score;
score = i = j = op = 0;
while (i < M || j < N) {
op = *S++;
if (i == 0 && j == 0 && op != 0) {
if (op > 0) j = j+op;
else i = i-op;
} else if (i == M || j == N) {
i = M;
j = N;
} else if (op == 0)
score = w[A[++i]][B[++j]] + score;
else if (op > 0) {
score = score - (g+op*h);
j = j+op;
} else {
score = score - (g-op*h);
i = i-op;
}
}
return(score);
}
/* lib.c - library of C procedures. */
/* fatal - print message and die */
fatal(msg)
char *msg;
{
fprintf(stderr, "%s\n", msg);
exit(1);
}
/* fatalf - format message, print it, and die */
fatalf(msg, val)
char *msg, *val;
{
fprintf(stderr, msg, val);
putc('\n', stderr);
exit(1);
}