File: abi.c

package info (click to toggle)
lucy 1.20-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,516 kB
  • sloc: ansic: 3,161; makefile: 30; awk: 21
file content (358 lines) | stat: -rw-r--r-- 10,313 bytes parent folder | download | duplicates (6)
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
/****************************************************************************
*
* Copyright (c) 2003, The Institute for Genomic Research (TIGR), Rockville,
* Maryland, U.S.A.  All rights reserved.
*
****************************************************************************/

#define VERYBAD 16

static struct stack_struct {
  int i, j, m, c;
} stack[100];
static int abi_size=16;
static unsigned abi_mask;

struct abi_struct {
  unsigned tag;
  int index;
} *abi_well;
struct hit_struct {
  int diff, count;
} *hit_well;

static condition[]={
-1, -1, -1, -1, -1, -1, -1, -1, -1,  0, /*  0-19 */
 0,  0,  0,  0,  0,  0,  0,  0,  0,  3,
 3,  3,  3,  3,  3,  3,  3,  3,  3,  3, /* 20-39 */
 3,  3,  3,  3,  3,  3,  3,  3,  3,  6,
 6,  6,  6,  6,  6,  6,  6,  6,  6,  6, /* 40-59 */
 6,  6,  6,  6,  6,  6,  6,  6,  6,  9,
 9,  9,  9,  9,  9,  9,  9,  9,  9,  9, /* 60-79 */
 9,  9,  9,  9,  9,  9,  9,  9,  9, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, /* 80-99 */
12, 12, 12, 12, 12, 12, 12, 12, 12, 15};

static int badness[16][16]= {
/*       A   C   G   T   U   R   Y   M   W   S   K   D   H   V   B   N */
/*A*/ {  0,  3,  3,  3,  3,  1,  3,  1,  1,  3,  3,  1,  1,  1,  3,  2},
/*C*/ {  3,  0,  3,  3,  3,  3,  1,  1,  3,  1,  3,  3,  1,  1,  1,  2},
/*G*/ {  3,  3,  0,  3,  3,  1,  3,  3,  3,  1,  1,  1,  3,  1,  1,  2},
/*T*/ {  3,  3,  3,  0,  0,  3,  1,  3,  1,  3,  1,  1,  1,  3,  1,  2},
/*U*/ {  3,  3,  3,  0,  0,  3,  1,  3,  1,  3,  1,  1,  1,  3,  1,  2},
/*R*/ {  1,  3,  1,  3,  3,  1,  3,  2,  2,  2,  2,  2,  3,  2,  3,  3},
/*Y*/ {  3,  1,  3,  1,  1,  3,  1,  2,  2,  2,  2,  3,  2,  3,  2,  3},
/*M*/ {  1,  1,  3,  3,  3,  2,  2,  1,  2,  2,  3,  3,  2,  2,  3,  3},
/*W*/ {  1,  3,  3,  1,  1,  2,  2,  2,  1,  3,  2,  2,  2,  3,  3,  3},
/*S*/ {  3,  1,  1,  3,  3,  2,  2,  2,  3,  1,  2,  3,  3,  2,  2,  3},
/*K*/ {  3,  3,  1,  1,  1,  2,  2,  3,  2,  2,  1,  2,  3,  3,  2,  3},
/*D*/ {  1,  3,  1,  1,  1,  2,  3,  3,  2,  3,  2,  2,  3,  3,  3,  3},
/*H*/ {  1,  1,  3,  1,  1,  3,  2,  2,  2,  3,  3,  3,  2,  3,  3,  3},
/*V*/ {  1,  1,  1,  3,  3,  2,  3,  2,  3,  2,  3,  3,  3,  2,  3,  3},
/*B*/ {  3,  1,  1,  1,  1,  3,  2,  3,  3,  2,  2,  3,  3,  3,  2,  3},
/*N*/ {  2,  2,  2,  2,  2,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3},
/*       A   C   G   T   U   R   Y   M   W   S   K   D   H   V   B   N */
};

static int Dlist[]={ 0, 2, 3}, Hlist[]={ 0, 1, 3};

int abi_code(c)
int c;
{
  register int tmp;

  switch (c) {
  case 0: tmp=0; break;
  case 1: tmp=1; break;
  case 2: tmp=2; break;
  case 3: 
  case 4: tmp=3; break;
  case 5: tmp=random()%2 ? 2 : 0; break;
  case 6: tmp=random()%2 ? 3 : 1; break;
  case 7: tmp=random()%2        ; break;
  case 8: tmp=random()%2 ? 3 : 0; break;
  case 9: tmp=random()%2 ? 2 : 1; break;
  case 10: tmp=random()%2 ? 3 : 2; break;
  case 11: tmp=Dlist[random()%3]; break;
  case 12: tmp=Hlist[random()%3]; break;
  case 13: tmp=random()%3; break;
  case 14: tmp=random()%3+1; break;
  case 15: tmp=random()%4; break;
  default:
    giveup("how can other cases happen in tag_code?");
  }
  return tmp;
}

void abi_sort(l, r)
struct abi_struct *l, *r;
{
  register unsigned v;
  register struct abi_struct *i, *j, tmp;
 
  if (r>l) {
    v = r->tag; i = l-1; j = r;
    while (1) {
      while ((++i)->tag < v) ;
      while (j>l && (--j)->tag > v) ;
      if (i>=j) break;
      tmp = *i; *i = *j; *j = tmp;
    }
    tmp = *i; *i = *r; *r = tmp;
    abi_sort(l, i-1);
    abi_sort(i+1, r);
  }
}

void prepare_abi_mask()
{
  register int i;
  register unsigned acc;

  /* construct abi_mask */
  for (acc=i=0; i<abi_size; i++) {
    acc<<=2;
    acc|=3;
  }
  abi_mask=acc;
}

void abi_align(a, width, b, height, start, end)
char *a, *b;
int width, height;
int *start, *end;
{
  register int i, j, l, r, m, diff, count;
  register unsigned x;
  int left, span, abi_len, hit_len;

  /* construct 'a' sequence tags */
  for (x=i=0; i<abi_size-1; i++, x<<=2)
    x|=abi_code(a[i]);
  for (j=0; i<width; i++, j++, x<<=2) {
    x|=abi_code(a[i]);
    x&=abi_mask;
    abi_well[j].tag=x;
    abi_well[j].index=i;
  }
  abi_len=j;
  abi_sort(abi_well, &abi_well[abi_len-1]);
  for (i=j=0; j<abi_len; j++)
    if (abi_well[i].tag!=abi_well[j].tag)
      abi_well[++i]=abi_well[j];
  abi_len=i+1;

  /* construct 'b' sequence tags, and search against 'a' sequence tags */
  for (x=i=0; i<abi_size-1; i++, x<<=2)
    x|=abi_code(b[i]);
  for (count=hit_len=0; i<height; i++, x<<=2) {
    x|=abi_code(b[i]);
    x&=abi_mask;
    for (l=0, r=abi_len-1; l<=r; ) {
      m=(l+r)/2;
      if (x<abi_well[m].tag)
	r=m-1;
      else if (x>abi_well[m].tag)
	l=m+1;
      else {
	l=abi_well[m].index-i;
	for (j=0; j<hit_len; j++)
	  if (hit_well[j].diff==l) {
	    hit_well[j].count++;
	    if (hit_well[j].count>count) {
	      count=hit_well[j].count;
	      diff=hit_well[j].diff;
	      if (j) {
		hit_well[j]=hit_well[0];
		hit_well[0].count=count;
		hit_well[0].diff=diff;
	      }
	    }
	    break;
	  }
	if (j>=hit_len) {
	  hit_well[j].count=1;
	  hit_well[j].diff=l;
	  hit_len=j+1;
	}
	break;
      }
    }
  }

  /* calculate starting points for sequence a and b */
  if (count<=0) {
    *start=*end=0;
    return;
  } else if (diff>=0) {
    i=diff; j=0;
  } else {
    i=0; j=-diff;
  }

  /* find the primary alignment region first */
  for (span=0, left=x=i; i<width && j<height; i++, j++)
    if (badness[a[i]][b[j]]) {
      /*
      for (count=0, m=0, l=i+1, r=j+1; 
	   l<width && r<height && count<VERYBAD && count>condition[m];  
	   m++, l++, r++)
	if (badness[a[l]][b[r]]) count+=badness[a[l]][b[r]];
      if (l<width && r<height && count<VERYBAD)
	continue;
	*/
      if (i-x>span) {
	left=x;
	span=i-x;
      }
      x=i+1;
    }
  if (i-x>span) {
    left=x;
    span=i-x;
  }
  if (span<=0) {
    *start=*end=0;
    return;
  }
  
  /* extend alignment region toward the right, if possible */
  for (i=left+span, j=i-diff; i<width && j<height; i++, j++) 
    if (badness[a[i]][b[j]]) {
      x=0;
      stack[x].i=i+1; stack[x].j=j+1; stack[x].m=0; stack[x++].c=0;
      while (x>0) {
	for (count=stack[--x].c, m=stack[x].m, l=stack[x].i, r=stack[x].j; 
	     l<width && r<height && badness[a[l]][b[r]]==0
	       && count>condition[m];  
	     m++, l++, r++) ;
	if (l>=width || r>=height)
	  continue;
	if (count<=condition[m])
	  break;
	if ((count+=badness[a[l]][b[r]])<VERYBAD) {
	  stack[x].i=l; stack[x].j=r+1; stack[x].m=m; stack[x++].c=count;
	  stack[x].i=l+1; stack[x].j=r; stack[x].m=m+1; stack[x++].c=count;
	  stack[x].i=l+1; stack[x].j=r+1; stack[x].m=m+1; stack[x++].c=count;
	}
      }
      if (l<width && r<height && count<=condition[m])
	continue;
      x=0;
      stack[x].i=i+1; stack[x].j=j; stack[x].m=0; stack[x++].c=0;
      while (x>0) {
	for (count=stack[--x].c, m=stack[x].m, l=stack[x].i, r=stack[x].j; 
	     l<width && r<height && badness[a[l]][b[r]]==0
	       && count>condition[m];  
	     m++, l++, r++) ;
	if (l>=width || r>=height)
	  continue;
	if (count<=condition[m])
	  break;
	if ((count+=badness[a[l]][b[r]])<VERYBAD) {
	  stack[x].i=l; stack[x].j=r+1; stack[x].m=m; stack[x++].c=count;
	  stack[x].i=l+1; stack[x].j=r; stack[x].m=m+1; stack[x++].c=count;
	  stack[x].i=l+1; stack[x].j=r+1; stack[x].m=m+1; stack[x++].c=count;
	}
      }
      if (l<width && r<height && count<=condition[m]) {
	j--;
	continue;
      }
      x=0;
      stack[x].i=i; stack[x].j=j+1; stack[x].m=0; stack[x++].c=0;
      while (x>0) {
	for (count=stack[--x].c, m=stack[x].m, l=stack[x].i, r=stack[x].j; 
	     l<width && r<height && badness[a[l]][b[r]]==0
	       && count>condition[m];  
	     m++, l++, r++) ;
	if (l>=width || r>=height)
	  continue;
	if (count<=condition[m])
	  break;
	if ((count+=badness[a[l]][b[r]])<VERYBAD) {
	  stack[x].i=l; stack[x].j=r+1; stack[x].m=m; stack[x++].c=count;
	  stack[x].i=l+1; stack[x].j=r; stack[x].m=m+1; stack[x++].c=count;
	  stack[x].i=l+1; stack[x].j=r+1; stack[x].m=m+1; stack[x++].c=count;
	}
      }
      if (l<width && r<height && count<=condition[m]) {
	i--;
	continue;
      }
      break;
    }
  span=i-left;

  /* extend toward the left, if possible */
  for (i=left-1, j=i-diff; i>=0 && j>=0; i--, j--) 
    if (badness[a[i]][b[j]]) {
      x=0;
      stack[x].i=i-1; stack[x].j=j-1; stack[x].m=0; stack[x++].c=0;
      while (x>0) {
	for (count=stack[--x].c, m=stack[x].m, l=stack[x].i, r=stack[x].j; 
	     l>=0 && r>=0 && badness[a[l]][b[r]]==0
	       && count>condition[m];  
	     m++, l--, r--) ;
	if (l<0 || r<0)
	  continue;
	if (count<=condition[m])
	  break;
	if ((count+=badness[a[l]][b[r]])<VERYBAD) {
	  stack[x].i=l; stack[x].j=r-1; stack[x].m=m; stack[x++].c=count;
	  stack[x].i=l-1; stack[x].j=r; stack[x].m=m+1; stack[x++].c=count;
	  stack[x].i=l-1; stack[x].j=r-1; stack[x].m=m+1; stack[x++].c=count;
	}
      }
      if (l>=0 && r>=0 && count<=condition[m])
	continue;
      x=0;
      stack[x].i=i-1; stack[x].j=j; stack[x].m=0; stack[x++].c=0;
      while (x>0) {
	for (count=stack[--x].c, m=stack[x].m, l=stack[x].i, r=stack[x].j; 
	     l>=0 && r>=0 && badness[a[l]][b[r]]==0
	       && count>condition[m];  
	     m++, l--, r--) ;
	if (l<0 || r<0)
	  continue;
	if (count<=condition[m])
	  break;
	if ((count+=badness[a[l]][b[r]])<VERYBAD) {
	  stack[x].i=l; stack[x].j=r-1; stack[x].m=m; stack[x++].c=count;
	  stack[x].i=l-1; stack[x].j=r; stack[x].m=m+1; stack[x++].c=count;
	  stack[x].i=l-1; stack[x].j=r-1; stack[x].m=m+1; stack[x++].c=count;
	}
      }
      if (l>=0 && r>=0 && count<=condition[m]) {
	j++;
	continue;
      }
      x=0;
      stack[x].i=i; stack[x].j=j-1; stack[x].m=0; stack[x++].c=0;
      while (x>0) {
	for (count=stack[--x].c, m=stack[x].m, l=stack[x].i, r=stack[x].j; 
	     l>=0 && r>=0 && badness[a[l]][b[r]]==0
	       && count>condition[m];  
	     m++, l--, r--) ;
	if (l<0 || r<0)
	  continue;
	if (count<=condition[m])
	  break;
	if ((count+=badness[a[l]][b[r]])<VERYBAD) {
	  stack[x].i=l; stack[x].j=r-1; stack[x].m=m; stack[x++].c=count;
	  stack[x].i=l-1; stack[x].j=r; stack[x].m=m+1; stack[x++].c=count;
	  stack[x].i=l-1; stack[x].j=r-1; stack[x].m=m+1; stack[x++].c=count;
	}
      }
      if (l>=0 && r>=0 && count<=condition[m]) {
	i++;
	continue;
      }
      break;
    }
  span=left+span-i-1;
  left=i+1;
  
  *start=left;
  *end=left+span-1;
}