Debian Codegraph @

atac-driver/chainer/localalign/GF_ALN_local.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 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <math.h>
23 #include <string.h>
24 #include <assert.h>
25 #include "GF_ALN_local.H"
26
27 // Note, KMERLEN 5, MINMATCH 20, MAXERROR 2, KTHRESH 6 is reasonable
28 // for performing fragment against fragment comparisons; it permits
29 // relatively small segments to be found; but it will not give
30 // acceptable run time for large comparisons such as a BAC against a
31 // BAC etc. So, ...
32
33 #define KMERLEN 6 // Must be >= 1
34 #define MINMATCH 20 // (MINMATCH-KMERLEN) is the maximum jump distance.
35 #define MAXERROR 2 // maximum slop in diagnols for chaining KMERLEN hits.
36
37 // The minimum number kmer hits that constitutes an acceptable chain.
38 #define KTHRESH (MINMATCH - (KMERLEN-1) - KMERLEN*MAXERROR)
39
40 #define min(a,b) (a<b?a:b)
41 #define max(a,b) (a>b?a:b)
42
43
44
45
46 /* D.P. extension alignment scoring */
47
48 /* N.B.: the larger MAXIGAP, the longer subpart of a trapezoid (potential
49 segment) can be missed; this occurs if (a) Align_Recursion starts
50 in the subpart, with a bad region on both sides of the subpart;
51 the forward pass extends across one of the low-quality regions
52 into a long high-quality region but then the reverse pass doesn't
53 get back across the low-quality region--or rather, it does, but the
54 best score doesn't.
55
56 Of course, the smaller MAXIGAP, the shorter the segments and the
57 less the chance of extending alignments across gaps between trapezoids.
58
59 Setting MAXIGAP small allows alignment extension to end relatively
60 early, so that a short high-quality region bounded by low-quality
61 regions can end up being its own segment. And, it also has the
62 effect that if a low-quality region is small enough to get through,
63 then it doesn't take that long a high-quality segment to increase
64 the score back up to a new maximum value.
65 */
66
67 #define MAXIGAP_DEFAULT 3
68
69 static int MAXIGAP=MAXIGAP_DEFAULT;
70
71
72 /*amount to subtract from score for mismatch */
73 //#define DIFFCOST 14
74 // we can define it to 14 in order not to extend the alignment
75 // only at a high level of stringency
76 #define DIFFCOST 3
77
78 /*amount to add to score for match*/
79 #define SAMECOST 1
80
81 static int diffcost=DIFFCOST;
82 static int samecost=SAMECOST;
83
84 /* Trapezoid merging padding */
85
86 #define DPADDING 2
87 #define BPADDING KMERLEN+2
88
89 static int BLOCKCOST = DIFFCOST*MAXIGAP_DEFAULT;
90 static int MATCHCOST = DIFFCOST+SAMECOST;
91
92
93 /* Major data types */
94
95 /* Hit Record: Description of indexed based seed-match */
96 typedef struct {
97 int diagonal; /* Diagonal of hit */
98 int bstart; /* B position of start of hit */
99 int bfinish; /* B position of end of hit */
100 } HitRecord;
101
102 /* Trapezoid Record: Description of trapezoidal match zone */
103 typedef struct _Trap_Tag {
104 struct _Trap_Tag *next; /* Organized in a list linked on this field */
105 int top, bot; /* B-coords of top and bottom of trapzoidal zone */
106 int lft, rgt; /* Left and right diagonals of trapzoidal zone */
107 } Trapezoid;
108
109
110 /*** UTILITY ROUTINES ***/
111
112 static
113 void
114 OutOfMemory(char const * const where) {
115 fprintf(stderr,"COMPARE_LOCAL: Out of memory (%s)\n",where);
116 exit (1);
117 }
118
119 static void Complement(char * const seq, int const len) {
120 static char WCinvert[256];
121 static int Firstime = 1;
122
123 if (Firstime) { /* Setup complementation array */
124 int i;
125
126 Firstime = 0;
127 for(i = 0; i < 256;i++){
128 WCinvert[i] = '?';
129 }
130 WCinvert[(int)'a'] = 't';
131 WCinvert[(int)'c'] = 'g';
132 WCinvert[(int)'g'] = 'c';
133 WCinvert[(int)'t'] = 'a';
134 WCinvert[(int)'n'] = 'n';
135 WCinvert[(int)'A'] = 'T';
136 WCinvert[(int)'C'] = 'G';
137 WCinvert[(int)'G'] = 'C';
138 WCinvert[(int)'T'] = 'A';
139 WCinvert[(int)'N'] = 'N';
140 WCinvert[(int)'-'] = '-'; // added this to enable alignment of gapped consensi
141 }
142
143 /* Complement and reverse sequence */
144
145 {
146 register char *s, *t;
147 int c;
148
149 s = seq;
150 t = seq + (len-1);
151 while (s < t)
152 { c = *s;
153 *s++ = WCinvert[(int) *t];
154 *t-- = WCinvert[c];
155 }
156 if (s == t)
157 *s = WCinvert[(int) *s];
158 }
159 }
160
161
162 /*** INDEX CONSTRUCTION AND APPLICATION TO FILTERING ***/
163
164 /* Shared index and filter arrays used in this subsection */
165
166 typedef struct {
167 int minim;
168 int maxim;
169 int count;
170 } DiagRecord;
171
172 static int Kmask = -1;
173 static int *Table = NULL; /* [0..Kmask+1] */
174 static int *Tuples = NULL; /* [0..<Seqlen>-KMERLEN] */
175 static int Map[128];
176
177 static DiagRecord *DiagVec; /* [-(Alen-KMERLEN)..(Blen-KMERLEN) + MAXERROR] */
178
179
180 /* Reverse complement sequences -- so we do not recompute them over and over */
181 static char *BrevC=NULL;
182
183 /* Build index table for sequence S of length Slen. */
184
185 static void TableBuild(char const * const S, int const Slen)
186 { int i, c;
187 int x, h;
188 char const * const s = S+(KMERLEN-1);
189
190 for (c = 0; c <= Kmask; c++)
191 Table[c] = 0;
192
193 h = -KMERLEN;
194 c = 0;
195 for (i = 0; i < KMERLEN-1; i++)
196 { x = Map[(int) (S[i])];
197 if (x >= 0)
198 c = (c << 2) | x;
199 else
200 { c <<= 2; h = i-(KMERLEN-1); }
201 }
202 for (i = 0; i <= Slen-KMERLEN; i++)
203 { x = Map[(int) (s[i])];
204 if (x >= 0)
205 c = ((c << 2) | x) & Kmask;
206 else
207 { c = (c << 2) & Kmask; h = i; }
208 if (i >= h+KMERLEN)
209 Table[c+1] += 1;
210 }
211
212 for (c = 2; c <= Kmask; c++)
213 Table[c] += Table[c-1];
214
215 h = -KMERLEN;
216 c = 0;
217 for (i = 0; i < KMERLEN-1; i++)
218 { x = Map[(int) (S[i])];
219 if (x >= 0)
220 c = (c << 2) | x;
221 else
222 { c <<= 2; h = i-(KMERLEN-1); }
223 }
224 for (i = 0; i <= Slen-KMERLEN; i++)
225 { x = Map[(int) (s[i])];
226 if (x >= 0)
227 c = ((c << 2) | x) & Kmask;
228 else
229 { c = (c << 2) & Kmask; h = i; }
230 if (i >= h+KMERLEN)
231 Tuples[Table[c]++] = i;
232 }
233
234 for (c = Kmask; c >= 0; c--)
235 Table[c+1] = Table[c];
236 Table[0] = 0;
237 }
238
239 /* Apply index to find filtered hits between sequences, returning pointer to
240 array of HitRecords of length in the integer pointed at by Hitlen */
241
242 static int HSORT(const void *l, const void *r)
243 { HitRecord *x, *y;
244 x = (HitRecord *) l;
245 y = (HitRecord *) r;
246 return (x->bstart - y->bstart);
247 }
248
249 static HitRecord *Find_Hits
250 (char const * const A, int const Alen,
251 char const * const B, int const Blen, int * const Hitlen)
252 { static int HitMax = -1;
253 static HitRecord *HitList;
254 int hits, disconnect;
255
256 if (HitMax < 0)
257 { HitMax = 10000;
258 HitList = (HitRecord *) malloc(sizeof(HitRecord)*HitMax);
259 if (HitList == NULL)
260 OutOfMemory("Hit list");
261 }
262
263 { int i, j, c;
264 int x, h;
265 char const * const b = B + (KMERLEN-1);
266
267 for (j = -Alen; j <= Blen+MAXERROR; j++)
268 { DiagRecord *dp;
269 dp = DiagVec + j;
270 dp->count = dp->maxim = 0;
271 }
272
273 hits = 0;
274 disconnect = MINMATCH - KMERLEN;
275 h = -KMERLEN;
276 c = 0;
277 for (i = 0; i < KMERLEN-1; i++)
278 { x = Map[(int) (B[i])];
279 if (x >= 0)
280 c = (c << 2) | x;
281 else
282 { c <<= 2; h = i-(KMERLEN-1); }
283 }
284
285 for (i = 0; i <= Blen-KMERLEN; i++)
286 { x = Map[(int) (b[i])];
287 if (x >= 0)
288 c = ((c << 2) | x) & Kmask;
289 else
290 { c = (c << 2) & Kmask; h = i; }
291 if (i >= h+KMERLEN)
292 for (j = Table[c]; j < Table[c+1]; j++)
293 { DiagRecord *dp;
294 int e, k;
295 k = i-Tuples[j];
296 dp = DiagVec + k;
297 for (e = 0; e <= MAXERROR; e++)
298 { if (dp->maxim < i-disconnect)
299 { if (dp->count >= KTHRESH)
300 { HitRecord *hp;
301 if (hits >= HitMax)
302 { HitMax = (int)(1.2*hits) + 5000;
303 HitList = (HitRecord *) realloc(HitList,
304 sizeof(HitRecord)*HitMax);
305 if (HitList == NULL)
306 OutOfMemory("Hit list");
307 }
308 hp = HitList + hits;
309 hp->diagonal = k;
310 hp->bstart = dp->minim;
311 hp->bfinish = dp->maxim + KMERLEN;
312 hits += 1;
313 }
314 dp->count = 0;
315 }
316 if (dp->count == 0)
317 dp->minim = i;
318 dp->count += 1;
319 dp->maxim = i;
320 dp += 1;
321 }
322 }
323 }
324
325 for (j = -Alen; j <= Blen+MAXERROR; j++)
326 { DiagRecord *dp;
327 dp = DiagVec + j;
328 if (dp->count >= KTHRESH)
329 { HitRecord *hp;
330 if (hits >= HitMax)
331 { HitMax = (int)(1.2*hits) + 5000;
332 HitList = (HitRecord *)realloc(HitList,sizeof(HitRecord)*HitMax);
333 if (HitList == NULL)
334 OutOfMemory("Hit list");
335 }
336 hp = HitList + hits;
337 hp->diagonal = j;
338 hp->bstart = dp->minim;
339 hp->bfinish = dp->maxim + KMERLEN;
340 hits += 1;
341 }
342 }
343 }
344
345 qsort(HitList,hits,sizeof(HitRecord),HSORT);
346
347 *Hitlen = hits;
348 return (HitList);
349 }
350
351
352 /*** FORWARD AND REVERSE D.P. EXTENSION ROUTINES ***/
353 /* Called at the mid-point of trapezoid -- mid X [lo,hi], the extension
354 is computed to an end point and the lowest and highest diagonals
355 are recorded. These are returned in a partially filled Local_Segment
356 record, that will be merged with that returned for extension in the
357 opposite direction.
358 */
359
360 Local_Segment *TraceForwardPath
361 ( char const * const A, int const Alen,
362 char const * const B, int const Blen,
363 int const mid, int lo, int hi)
364 { static Local_Segment rez;
365 int *V;
366 int mxv, mxl, mxr, mxi, mxj;
367 int i, j;
368 int *Base1, *Base2;
369
370 Base1 = ((int *) DiagVec);
371 Base2 = Base1 + (Blen+1);
372
373 /* Set basis from (mid,lo) .. (mid,hi) */
374
375 V = Base1;
376 if (lo < 0) lo = 0;
377 if (hi > Blen) hi = Blen;
378
379 for (j = lo; j <= hi; j++)
380 V[j] = 0;
381 hi += MAXIGAP;
382 if (hi > Blen) hi = Blen;
383 for (; j <= hi; j++)
384 V[j] = V[j-1] - diffcost;
385 mxv = 0;
386 mxr = mid - lo;
387 mxl = mid - hi;
388 mxi = mid;
389 mxj = lo;
390
391 /* Advance to next row */
392
393 for (i = mid; lo <= hi && i < Alen; i++)
394 { int c, v;
395 int *W;
396
397 W = V;
398 if (V == Base1)
399 V = Base2;
400 else
401 V = Base1;
402
403 v = W[lo];
404 c = V[lo] = v - diffcost;
405 for (j = lo+1; j <= hi; j++)
406 { int r, t;
407
408 t = c;
409 c = v;
410 v = W[j];
411 if (Map[(int)A[i]] == Map[(int)B[j-1]] && Map[(int) (A[i])] >= 0)
412 c += MATCHCOST;
413
414 r = c;
415 if (v > r) r = v;
416 if (t > r) r = t;
417
418 V[j] = c = r - diffcost;
419 if (c >= mxv)
420 { mxv = c;
421 mxi = i+1;
422 mxj = j;
423 //printf("reset mxv = %d at [%d,%d]\n",mxv,mxi,mxj);
424 }
425 }
426
427 if (j <= Blen)
428 { int r;
429
430 if (Map[(int)A[i]] == Map[(int)B[j-1]] && Map[(int) (A[i])] >= 0)
431 v += MATCHCOST;
432
433 r = v;
434 if (c > r) r = c;
435
436 V[j] = v = r - diffcost;
437 if (v > mxv)
438 { mxv = v;
439 mxi = i+1;
440 mxj = j;
441 //printf("reset mxv = %d at [%d,%d]\n",mxv,mxi,mxj);
442 }
443
444 for (j++; j <= Blen; j++)
445 { v -= diffcost;
446 if (v < mxv - BLOCKCOST) break;
447 V[j] = v;
448 }
449 }
450
451 hi = j-1;
452
453 while (lo <= hi && V[lo] < mxv - BLOCKCOST)
454 lo += 1;
455 while (lo <= hi && V[hi] < mxv - BLOCKCOST)
456 hi -= 1;
457
458 if ((i+1) - lo > mxr)
459 mxr = (i+1) - lo;
460 if ((i+1) - hi < mxl)
461 mxl = (i+1) - hi;
462 }
463
464 rez.aepos = mxj;
465 rez.bepos = mxi;
466 rez.ldiag = mxl;
467 rez.hdiag = mxr;
468 rez.score = mxv;
469 return (&rez);
470 }
471
472 Local_Segment *TraceReversePath(char const * const A, int const Alen,
473 char const * const B, int const Blen,
474 int const top, int lo, int hi,
475 int const bot, int xfactor) {
476 static Local_Segment rez;
477 int *V;
478 int mxv, mxl, mxr, mxi, mxj;
479 int i, j;
480 int *Base1, *Base2;
481
482 Base1 = ((int *) DiagVec);
483 Base2 = Base1 + (Blen+1);
484
485 /* Set basis from (top,lo) .. (top,hi) */
486
487 V = Base1;
488 if (lo < 0) lo = 0;
489 if (hi > Blen) hi = Blen;
490
491 for (j = hi; j >= lo; j--)
492 V[j] = 0;
493 lo -= MAXIGAP;
494 if (lo < 0) lo = 0;
495 for (; j >= lo; j--)
496 V[j] = V[j+1] - diffcost;
497 mxv = 0;
498 mxr = top - lo;
499 mxl = top - hi;
500 mxi = top;
501 mxj = lo;
502
503 /* Advance to next row */
504
505 if (top-1 <= bot) xfactor = BLOCKCOST;
506
507 for (i = top-1; lo <= hi && i >= 0; i--)
508 { int c, v;
509 int *W;
510
511 W = V;
512 if (V == Base1)
513 V = Base2;
514 else
515 V = Base1;
516
517
518 v = W[hi];
519 c = V[hi] = v - diffcost;
520 for (j = hi-1; j >= lo; j--)
521 { int r, t;
522
523 t = c;
524 c = v;
525 v = W[j];
526 if (Map[(int)A[i]] == Map[(int)B[j]] && Map[(int) (A[i])] >= 0)
527 c += MATCHCOST;
528
529 r = c;
530 if (v > r) r = v;
531 if (t > r) r = t;
532
533 V[j] = c = r - diffcost;
534 if (c >= mxv)
535 { mxv = c;
536 mxi = i;
537 mxj = j;
538 //printf("reset mxv = %d at [%d,%d]\n",mxv,mxi,mxj);
539 }
540 }
541
542 if (j >= 0)
543 { int r;
544
545 if (Map[(int)A[i]] == Map[(int)B[j]] && Map[(int) (A[i])] >= 0)
546 v += MATCHCOST;
547
548 r = v;
549 if (c > r) r = c;
550
551 V[j] = v = r - diffcost;
552 if (v > mxv)
553 { mxv = v;
554 mxi = i;
555 mxj = j;
556 //printf("reset mxv = %d at [%d,%d]\n",mxv,mxi,mxj);
557 }
558
559 for (j--; j >= 0; j--)
560 { v -= diffcost;
561 if (v < mxv - xfactor) break;
562 V[j] = v;
563 }
564 }
565
566 lo = j+1;
567
568 while (lo <= hi && V[lo] < mxv - xfactor)
569 lo += 1;
570 while (lo <= hi && V[hi] < mxv - xfactor)
571 hi -= 1;
572
573 if (i == bot) xfactor = BLOCKCOST;
574
575 if (i-lo > mxr)
576 mxr = i-lo;
577 if (i-hi < mxl)
578 mxl = i-hi;
579 }
580
581 rez.abpos = mxj;
582 rez.bbpos = mxi;
583 rez.ldiag = mxl;
584 rez.hdiag = mxr;
585 rez.score = mxv;
586 return (&rez);
587 }
588
589
590 /*** MERGING INDEX HITS INTO TRAPEZOIDAL ZONES ***/
591
592 static Trapezoid *Build_Trapezoids(char const * const A, int const Alen,
593 char const * const B, int const Blen,
594 HitRecord const * const list, int const Hitlen, int * const Traplen) {
595 static Trapezoid *free = NULL;
596
597 Trapezoid *traporder, *traplist, *tailend;
598 Trapezoid *b, *f, *t;
599 int i, inserted;
600 int trapcount, traparea;
601
602 trapcount = 0;
603 traparea = 0;
604 traporder = NULL;
605 traplist = NULL;
606 for (i = 0; i < Hitlen; i++) {
607 inserted = 0;
608 f = NULL;
609 for (b = traporder; b != NULL; b = t) {
610 t = b->next;
611 if (b->top < list[i].bstart - BPADDING) {
612 trapcount += 1;
613 traparea += (b->top - b->bot + 1) * (b->rgt - b->lft + 1);
614 if (f == NULL)
615 traporder = t;
616 else
617 f->next = t;
618 b->next = traplist;
619 traplist = b;
620 }
621 else if (list[i].diagonal > b->rgt + DPADDING)
622 f = b;
623 else if (list[i].diagonal >= b->lft - DPADDING) {
624 if (list[i].diagonal < b->lft)
625 b->lft = list[i].diagonal;
626 if (list[i].diagonal > b->rgt)
627 b->rgt = list[i].diagonal;
628 if (list[i].bfinish > b->top)
629 b->top = list[i].bfinish;
630
631 if (f != NULL && f->rgt + DPADDING >= b->lft) {
632 f->rgt = b->rgt;
633 if (f->bot > b->bot) f->bot = b->bot;
634 if (f->top < b->top) f->top = b->top;
635 f->next = t;
636 b->next = free;
637 free = b;
638 }
639 else if (t != NULL && t->lft - DPADDING <= b->rgt) {
640 b->rgt = t->rgt;
641 if (b->bot > t->bot) b->bot = t->bot;
642 if (b->top < t->top) b->top = t->top;
643 b->next = t->next;
644 t->next = free;
645 free = t;
646 t = b->next;
647 f = b;
648 }
649 else
650 f = b;
651 inserted = 1;
652 } else if (! inserted) {
653 if (free == NULL) {
654 free = (Trapezoid *)malloc(sizeof(Trapezoid));
655 if (free == NULL)
656 OutOfMemory("Trapezoid scan list");
657 free->next = NULL;
658 }
659 if (f == NULL)
660 f = traporder = free;
661 else
662 f = f->next = free;
663 free = f->next;
664 f->next = b;
665 f->top = list[i].bfinish;
666 f->bot = list[i].bstart;
667 f->lft = f->rgt = list[i].diagonal;
668 f = b;
669 inserted = 1;
670 } else
671 f = b;
672 }
673 if (! inserted) {
674 if (free == NULL) {
675 free = (Trapezoid *)malloc(sizeof(Trapezoid));
676 if (free == NULL)
677 OutOfMemory("Trapezoid scan list");
678 free->next = NULL;
679 }
680 if (f == NULL)
681 f = traporder = free;
682 else
683 f = f->next = free;
684 free = f->next;
685 f->next = b;
686 f->top = list[i].bfinish;
687 f->bot = list[i].bstart;
688 f->lft = f->rgt = list[i].diagonal;
689 }
690 }
691
692 for (b = traporder; b != NULL; b = t) {
693 t = b->next;
694 trapcount += 1;
695 traparea += (b->top - b->bot + 1) * (b->rgt - b->lft + 1);
696 b->next = traplist;
697 traplist = b;
698 }
699
700 {
701 int lag, lst, lclip;
702 int abot, atop;
703
704 for (b = traplist; b != NULL; b = b->next) {
705 lag = (b->bot-MAXIGAP)+1;
706 if (lag < 0) lag = 0;
707 lst = b->top+MAXIGAP;
708 if (lst > Blen)
709 lst = Blen;
710
711 for (i = lag; i < lst; i++) {
712 if (Map[(int) (B[i])] >= 0) {
713 if (i-lag >= MAXIGAP) {
714 if (lag - b->bot > 0) {
715 if (free == NULL) {
716 free = (Trapezoid *)malloc(sizeof(Trapezoid));
717 if (free == NULL)
718 OutOfMemory("Trapezoid cutter");
719 free->next = NULL;
720 }
721 t = free->next;
722 *free = *b;
723 b->next = free;
724 free = t;
725 b->top = lag;
726 b = b->next;
727 b->bot = i;
728 trapcount += 1;
729 }
730 else
731 b->bot = i;
732 }
733 lag = i+1;
734 }
735 }
736 if (i-lag >= MAXIGAP)
737 b->top = lag;
738 }
739
740 tailend = NULL;
741 for (b = traplist; b != NULL; b = b->next) {
742 if (b->top - b->bot < KMERLEN) continue;
743
744 abot = b->bot - b->rgt;
745 atop = b->top - b->lft;
746
747
748 lag = (abot - MAXIGAP) + 1;
749 if (lag < 0) lag = 0;
750 lst = atop + MAXIGAP;
751 if (lst > Alen) lst = Alen;
752
753 lclip = abot;
754 for (i = lag; i < lst; i++) {
755 if (Map[(int) (A[i])] >= 0) {
756 if (i-lag >= MAXIGAP) {
757 if (lag > lclip) {
758 if (free == NULL) {
759 free = (Trapezoid *)malloc(sizeof(Trapezoid));
760 if (free == NULL)
761 OutOfMemory("Trapezoid cutter");
762 free->next = NULL;
763 }
764 t = free->next;
765 *free = *b;
766 b->next = free;
767 free = t;
768
769 {
770 int x, m;
771 x = lclip + b->lft;
772 if (b->bot < x)
773 b->bot = x;
774 x = lag + b->rgt;
775 if (b->top > x)
776 b->top = x;
777 m = (b->bot + b->top) / 2;
778 x = m - lag;
779 if (b->lft < x)
780 b->lft = x;
781 x = m - lclip;
782 if (b->rgt > x)
783 b->rgt = x;
784 }
785
786 b = b->next;
787 trapcount += 1;
788 }
789 lclip = i;
790 }
791 lag = i+1;
792 }
793 }
794
795 if (i-lag < MAXIGAP)
796 lag = atop;
797
798 {
799 int x, m;
800 x = lclip + b->lft;
801 if (b->bot < x)
802 b->bot = x;
803 x = lag + b->rgt;
804 if (b->top > x)
805 b->top = x;
806 m = (b->bot + b->top) / 2;
807 x = m - lag;
808 if (b->lft < x)
809 b->lft = x;
810 x = m - lclip;
811 if (b->rgt > x)
812 b->rgt = x;
813 }
814
815 tailend = b;
816 }
817 }
818
819 if (tailend != NULL) {
820 tailend->next = free;
821 free = traplist;
822 }
823
824 *Traplen = trapcount;
825 return (traplist);
826 }
827
828
829 /*** FINDING ALIGNMENTS WITHIN A TRAPEZOIDAL ZONE ***/
830
831 static int TSORT(const void *l, const void *r) {
832 Trapezoid *x, *y;
833 x = *((Trapezoid **) l);
834 y = *((Trapezoid **) r);
835 return (x->bot - y->bot);
836 }
837
838 static int StSORT(const void *l, const void *r) {
839 Local_Segment *x, *y;
840 x = (Local_Segment *) l;
841 y = (Local_Segment *) r;
842 if (x->abpos < y->abpos)
843 return (-1);
844 else if (x->abpos > y->abpos)
845 return (1);
846 else
847 return (x->bbpos - y->bbpos);
848 }
849
850 static int FnSORT(const void *l, const void *r) {
851 Local_Segment *x, *y;
852 x = (Local_Segment *) l;
853 y = (Local_Segment *) r;
854 if (x->aepos < y->aepos)
855 return (-1);
856 else if (x->aepos > y->aepos)
857 return (1);
858 else
859 return (x->bepos - y->bepos);
860 }
861
862 static Trapezoid **Tarray = NULL;
863 static int *Covered;
864 static Local_Segment *SegSols = NULL;
865 static int SegMax = -1;
866 static int NumSegs;
867
868 static void Align_Recursion(char const * const A, int const Alen,
869 char const * const B, int const Blen,
870 Trapezoid const * const b, int const current, int const comp,
871 int const MinLen, double const MaxDiff, int const Traplen) {
872 int j, mid, indel;
873 double pcnt;
874 Local_Segment *hend, *lend;
875 Trapezoid ltrp, htrp;
876
877 mid = (b->bot + b->top) / 2;
878
879 lend = TraceForwardPath(B,Blen,A,Alen,mid,mid-b->rgt,mid-b->lft);
880
881 {
882 int x = 0;
883
884 do {
885 x += 1;
886
887 hend = TraceReversePath(B,Blen,A,Alen,
888 lend->bepos,lend->aepos,lend->aepos,
889 mid+MAXIGAP,BLOCKCOST+2*x*diffcost);
890 } while (hend->bbpos > mid + x*MAXIGAP && hend->score < lend->score);
891
892 hend->aepos = lend->aepos;
893 hend->bepos = lend->bepos;
894 }
895
896
897 ltrp = htrp = *b;
898 ltrp.top = min(b->top,hend->bbpos) - MAXIGAP;
899
900
901 htrp.bot = max(b->bot,hend->bepos) + MAXIGAP;
902
903 if (hend->bepos - hend->bbpos >= MinLen &&
904 hend->aepos - hend->abpos >= MinLen) {
905
906 indel = abs( (hend->abpos - hend->bbpos)
907 - (hend->aepos - hend->bepos) );
908
909 pcnt = (-hend->score+samecost*(hend->bepos-hend->bbpos))*1./
910 (1.*(MATCHCOST)*(hend->bepos-hend->bbpos));
911
912 if (pcnt <= MaxDiff) {
913 hend->error = pcnt;
914
915 for (j = current+1; j < Traplen; j++)
916 { Trapezoid *t;
917 int ta, tb, ua, ub;
918
919 t = Tarray[j];
920 if (t->bot >= hend->bepos) break;
921
922 tb = t->top - t->bot + 1;
923 ta = t->rgt - t->lft + 1;
924 if (t->lft < hend->ldiag)
925 ua = hend->ldiag;
926 else
927 ua = t->lft;
928 if (t->rgt > hend->hdiag)
929 ub = hend->hdiag;
930 else
931 ub = t->rgt;
932
933 if (ua > ub) continue;
934
935 ua = ub - ua + 1;
936 if (t->top > hend->bepos)
937 ub = hend->bepos - t->bot + 1;
938 else
939 ub = tb;
940
941 if (((1.*ua)/ta)*((1.*ub)/tb) > .99)
942 Covered[j] = 1;
943 }
944
945 if (NumSegs >= SegMax)
946 { SegMax = (int)(1.2*NumSegs) + 500;
947 SegSols = (Local_Segment *) realloc(SegSols,
948 sizeof(Local_Segment)*SegMax);
949 if (SegSols == NULL)
950 OutOfMemory("Segment Alignment array");
951 }
952
953 { int d;
954
955 d = hend->hdiag; /* Oops, diags to this point are b-a, not a-b. */
956 hend->hdiag = - (hend->ldiag);
957 hend->ldiag = - d;
958 if (comp)
959 { hend->bbpos = Blen - hend->bbpos;
960 hend->bepos = Blen - hend->bepos;
961 hend->ldiag = Blen + hend->ldiag;
962 hend->hdiag = Blen + hend->hdiag;
963 }
964 }
965
966 SegSols[NumSegs++] = *hend;
967 }
968 }
969
970 if (ltrp.top - ltrp.bot > MinLen && ltrp.top < b->top - MAXIGAP)
971 Align_Recursion(A,Alen,B,Blen,&ltrp,current,comp,MinLen,MaxDiff,Traplen);
972
973 if (htrp.top - htrp.bot > MinLen)
974 Align_Recursion(A,Alen,B,Blen,&htrp,current,comp,MinLen,MaxDiff,Traplen);
975 }
976
977
978
979 static Local_Segment *Align_Trapezoids(char const * const A, int const Alen,
980 char const * const B, int const Blen,
981 Trapezoid const * const Traplist, int const Traplen,
982 int const start, int const comp,
983 int const MinLen, double const MaxDiff, int * const Seglen) {
984 static int fseg;
985 static int TarMax = -1;
986
987 if (Traplen >= TarMax) {
988 TarMax = (int)(1.2*Traplen) + 500;
989 Tarray = (Trapezoid **)
990 realloc(Tarray,(sizeof(Trapezoid *) + sizeof(int))*TarMax);
991 if (Tarray == NULL)
992 OutOfMemory("Trapezoid array");
993 Covered = (int *) (Tarray + TarMax);
994 }
995 if (SegMax < 0) {
996 SegMax = 1000;
997 SegSols = (Local_Segment *) malloc(sizeof(Local_Segment)*SegMax);
998 if (SegSols == NULL)
999 OutOfMemory("Segment Alignment array");
1000 }
1001
1002 {
1003 Trapezoid * b = (Trapezoid *)Traplist;
1004 int i;
1005 for (i = 0; i < Traplen; i++) {
1006 Tarray[i] = b;
1007 Covered[i] = 0;
1008 b = b->next;
1009 }
1010 }
1011
1012 qsort(Tarray,Traplen,sizeof(Trapezoid *),TSORT);
1013
1014 if (start)
1015 NumSegs = 0;
1016 fseg = NumSegs;
1017 {
1018 int i;
1019 for (i = 0; i < Traplen; i++)
1020 if (! Covered[i]) {
1021 Trapezoid * b = Tarray[i];
1022 if (b->top - b->bot < KMERLEN) continue;
1023 //printf("Trying hit %d\n",i);
1024 Align_Recursion(A,Alen,B,Blen,b,i,comp,MinLen,MaxDiff,Traplen);
1025 }
1026 }
1027
1028 if (NumSegs > fseg) {
1029 int i;
1030 int j=0;
1031 qsort(SegSols+fseg,NumSegs-fseg,sizeof(Local_Segment),StSORT);
1032 assert(j==0);
1033 for (i = fseg; i < NumSegs; i = j) {
1034 for (j = i+1; j < NumSegs; j++) {
1035
1036 if (SegSols[j].abpos != SegSols[i].abpos) break;
1037 if (SegSols[j].bbpos != SegSols[i].bbpos) break;
1038 if (/* segments in opposite orientations */
1039 ((SegSols[j].bepos-SegSols[j].bbpos) > 0 &&
1040 (SegSols[i].bepos-SegSols[i].bbpos) < 0 )
1041 ||
1042 ((SegSols[j].bepos-SegSols[j].bbpos) < 0 &&
1043 (SegSols[i].bepos-SegSols[i].bbpos) > 0 ) )break;
1044
1045 if (SegSols[j].error <= MaxDiff &&
1046 SegSols[i].error <= MaxDiff){
1047
1048 if (abs(SegSols[i].bepos-SegSols[i].bbpos)+abs(SegSols[i].aepos-SegSols[i].abpos) <
1049 abs(SegSols[j].bepos-SegSols[j].bbpos)+abs(SegSols[j].aepos-SegSols[j].abpos)) {
1050 SegSols[i].score=-1;i=j;
1051 } else {
1052 SegSols[j].score=-1;
1053 }
1054 } else {
1055 if(SegSols[j].error<=MaxDiff){
1056 SegSols[i].score=-1;
1057 i=j;
1058 } else {
1059 SegSols[j].score=-1;
1060 }
1061 }
1062 }
1063 }
1064
1065 qsort(SegSols+fseg,NumSegs-fseg,sizeof(Local_Segment),FnSORT);
1066 for ( i = fseg; i < NumSegs; i = j) {
1067 for (j = i+1; j < NumSegs; j++) {
1068 if (SegSols[j].abpos != SegSols[i].abpos) break;
1069 if (SegSols[j].bbpos != SegSols[i].bbpos) break;
1070 if (SegSols[j].score > SegSols[i].score) {
1071 SegSols[i].score = -1;
1072 i = j;
1073 } else
1074 SegSols[j].score = -1;
1075 }
1076 }
1077
1078 for (i = fseg; i < NumSegs; i++)
1079 if (SegSols[i].score >= 0)
1080 SegSols[fseg++] = SegSols[i];
1081 NumSegs = fseg;
1082 }
1083
1084 *Seglen = NumSegs;
1085 return (SegSols);
1086 }
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099 /*** MASTER ROUTINE ***/
1100
1101 Local_Segment *Find_Local_Segments(char const * const A, int const Alen,
1102 char const * const B, int const Blen,
1103 int const Action,
1104 int const MinLen, double const MaxDiff, int * const Seglen) {
1105 static int DagMax = -1;
1106 static int BseqLen = -1;
1107
1108 int numhit = 0;
1109 HitRecord *hits = 0L;
1110 int numtrap = 0;
1111 Trapezoid *traps = 0L;
1112 int numseg = 0;
1113 Local_Segment *segs = 0L;
1114
1115
1116 // Defining this causes scoring to be set so that nearly all
1117 // extensions terminate within the user-defined error rate; this
1118 // means we don't completely miss a high-quality segment that is
1119 // part of a larger segment of lower quality--e.g., we could find
1120 // more-conserved portions of a repeat even if the repeat as a
1121 // whole was lower fidelity, due to varying selectional pressure at
1122 // different positions; the down-side to this is that we won't
1123 // chain together perfect matches across a few bases of
1124 // lower-fidelity sequence. Leaving it undefined uses the #define
1125 // values of DIFFCOST and SAMECOST
1126 //
1127 samecost = (int)ceil(100.0 * MaxDiff);
1128 diffcost = 100 - samecost;
1129 MATCHCOST = samecost + diffcost;
1130 BLOCKCOST = diffcost * MAXIGAP;
1131
1132 if (Action != LOCAL_FORW) {
1133 if(BseqLen<Blen){
1134 BrevC = (char*) realloc(BrevC,sizeof(char)*(Blen+1));
1135 BseqLen=Blen;
1136 if(BrevC==NULL){OutOfMemory("B sequence reverse complement");}
1137 }
1138 strcpy(BrevC,B);
1139 Complement(BrevC,Blen);
1140 }
1141
1142 if (Alen >= DagMax || Blen >= DagMax) {
1143 if (Kmask < 0) {
1144 int i;
1145 for (i = 0; i < 128; i++)
1146 Map[i] = -1;
1147 Map[(int)'a'] = Map[(int)'A'] = 0;
1148 Map[(int)'c'] = Map[(int)'C'] = 1;
1149 Map[(int)'g'] = Map[(int)'G'] = 2;
1150 Map[(int)'t'] = Map[(int)'T'] = 3;
1151 Kmask = (1 << (2*KMERLEN)) - 1;
1152 Table = (int *)malloc(sizeof(int)*(Kmask+2));
1153 if (Table == NULL)
1154 OutOfMemory("K-mer index");
1155 }
1156 if (Alen > Blen)
1157 DagMax = (int)(1.2*Alen) + 5000;
1158 else
1159 DagMax = (int)(1.2*Blen) + 5000;
1160 DagMax += sizeof(DiagRecord) - (DagMax % sizeof(DiagRecord));
1161 Tuples = (int *)realloc(Tuples,sizeof(int)*DagMax +
1162 sizeof(DiagRecord)*(2*DagMax+MAXERROR+1));
1163 if (Tuples == NULL)
1164 OutOfMemory("K-mer index");
1165 DiagVec = ((DiagRecord *) (Tuples + DagMax)) + (DagMax+1);
1166 }
1167
1168 TableBuild(A,Alen);
1169
1170 int start = 1;
1171
1172 if (Action != LOCAL_REVR) {
1173 hits = Find_Hits(A,Alen,B,Blen,&numhit);
1174 traps = Build_Trapezoids(A,Alen,B,Blen,hits,numhit,&numtrap);
1175 segs = Align_Trapezoids(A,Alen,B,Blen,traps,numtrap,
1176 start,0,MinLen,MaxDiff,&numseg);
1177 start = 0;
1178 }
1179
1180 if (Action != LOCAL_FORW) {
1181 hits = Find_Hits(A,Alen,BrevC,Blen,&numhit);
1182 traps = Build_Trapezoids(A,Alen,BrevC,Blen,hits,numhit,&numtrap);
1183 segs = Align_Trapezoids(A,Alen,BrevC,Blen,traps,numtrap,
1184 start,1,MinLen,MaxDiff,&numseg);
1185 }
1186
1187 *Seglen = numseg;
1188 return (segs);
1189 }