| 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 <assert.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include "GF_ALN_local.H" |
| 26 | |
| 27 | #define max(x,y) ((x<y) ? (y):(x)) |
| 28 | #define min(x,y) ((x>y) ? (y):(x)) |
| 29 | |
| 30 | |
| 31 | #define MIN_USABLE 3 /* Smallest subpart of a segment usable for chaining */ |
| 32 | |
| 33 | |
| 34 | int MIN_ALIGNED_COLS=30; /* minimum length of a local overlap in the following |
| 35 | sense: an overlap is defined by a set of segments; |
| 36 | each segment has a length (ignoring minor troubles |
| 37 | in determining length in the presence of small |
| 38 | indels); let the sum of the overlap for this |
| 39 | purpose be the sum of the lengths of the segments |
| 40 | that make up the overlap */ |
| 41 | |
| 42 | #define BIG_INT 0x7FFFFFFF |
| 43 | |
| 44 | typedef struct { |
| 45 | int start; |
| 46 | int base; |
| 47 | int segment; |
| 48 | int best; |
| 49 | } Candidate; |
| 50 | |
| 51 | typedef struct { |
| 52 | Local_Segment *item; |
| 53 | int isadd; |
| 54 | } Event; |
| 55 | |
| 56 | typedef struct { |
| 57 | int value; |
| 58 | int source; |
| 59 | int start; |
| 60 | int colsAligned; |
| 61 | } TraceElement; |
| 62 | |
| 63 | /*** AVL-TREE LIST ROUTINES ***/ |
| 64 | |
| 65 | static void OutOfMemory(char *where) |
| 66 | { fprintf(stderr,"COMPARE_LOCAL: Out of memory (%s)\n",where); |
| 67 | exit (1); |
| 68 | } |
| 69 | |
| 70 | typedef struct _AVLnode { |
| 71 | int RC, LN; |
| 72 | short H; |
| 73 | struct _AVLnode *L, *R; |
| 74 | Candidate V; |
| 75 | } AVLnode; |
| 76 | |
| 77 | #define CP(v) ((v)->L->LN) |
| 78 | |
| 79 | static AVLnode *freept; |
| 80 | static AVLnode *NIL; |
| 81 | |
| 82 | #define INC AVLinc |
| 83 | #define DEC AVLdec |
| 84 | #define SEL AVLselect |
| 85 | #define RNK AVLrank |
| 86 | #define ADD AVLinsert |
| 87 | #define DEL AVLdelete |
| 88 | |
| 89 | static void AVLinit(void) |
| 90 | { freept = NULL; |
| 91 | NIL = (AVLnode *) malloc(sizeof(AVLnode)); |
| 92 | if (NIL == NULL) |
| 93 | OutOfMemory("Candidate list"); |
| 94 | NIL->LN = NIL->RC = 1; |
| 95 | NIL->H = 0; |
| 96 | NIL->V.base = BIG_INT; |
| 97 | NIL->V.best = BIG_INT; |
| 98 | } |
| 99 | |
| 100 | static AVLnode *AVLinc(AVLnode *v) |
| 101 | { v->RC++; |
| 102 | return (v); |
| 103 | } |
| 104 | |
| 105 | static void AVLdec(AVLnode *v) |
| 106 | { v->RC--; |
| 107 | if (v->RC == 0) |
| 108 | { DEC(v->L); |
| 109 | DEC(v->R); |
| 110 | v->L = freept; |
| 111 | freept = v; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | static int AVLlength(AVLnode *v) |
| 116 | { DEC(v);return (v->LN - 1); } |
| 117 | |
| 118 | static AVLnode *NEW(AVLnode *l, Candidate *x, AVLnode *r) |
| 119 | { AVLnode *v; |
| 120 | int b; |
| 121 | |
| 122 | if (freept == NULL) |
| 123 | { v = (AVLnode *) malloc(sizeof(AVLnode)); |
| 124 | if (v == NULL) |
| 125 | OutOfMemory("Candidate list"); |
| 126 | } |
| 127 | else |
| 128 | { v = freept; |
| 129 | freept = v->L; |
| 130 | } |
| 131 | |
| 132 | v->RC = 1; |
| 133 | v->V = *x; |
| 134 | v->L = l; |
| 135 | v->R = r; |
| 136 | v->LN = l->LN + r->LN; |
| 137 | v->H = (l->H < r->H ? r->H : l->H) + 1; |
| 138 | |
| 139 | b = v->V.base; |
| 140 | if (v->L->V.best < b) |
| 141 | b = v->L->V.best; |
| 142 | if (v->R->V.best < b) |
| 143 | b = v->R->V.best; |
| 144 | v->V.best = b; |
| 145 | |
| 146 | return (v); |
| 147 | } |
| 148 | |
| 149 | static AVLnode *BAL(AVLnode *l, Candidate *x, AVLnode *r) |
| 150 | { AVLnode *t; |
| 151 | if (l->H - r->H >= -1 && l->H - r->H <= 1) |
| 152 | t = NEW(INC(l),x,INC(r)); |
| 153 | else if (l->H > r->H) |
| 154 | if (l->L->H >= l->R->H) |
| 155 | t = NEW(INC(l->L),&(l->V),NEW(INC(l->R),x,INC(r))); |
| 156 | else |
| 157 | t = NEW(NEW(INC(l->L),&(l->V),INC(l->R->L)),&(l->R->V), |
| 158 | NEW(INC(l->R->R),x,INC(r))); |
| 159 | else |
| 160 | if (r->R->H >= r->L->H) |
| 161 | t = NEW(NEW(INC(l),x,INC(r->L)),&(r->V),INC(r->R)); |
| 162 | else |
| 163 | t = NEW(NEW(INC(l),x,INC(r->L->L)),&(r->L->V), |
| 164 | NEW(INC(r->L->R),&(r->V),INC(r->R))); |
| 165 | DEC(l); DEC(r); |
| 166 | return (t); |
| 167 | } |
| 168 | |
| 169 | static Candidate *AVLselect(AVLnode *v, int k) |
| 170 | { Candidate *x; |
| 171 | if (k < CP(v)) |
| 172 | x = SEL(INC(v->L),k); |
| 173 | else if (k > CP(v)) |
| 174 | x = SEL(INC(v->R),k-CP(v)); |
| 175 | else |
| 176 | x = &(v->V); |
| 177 | DEC(v); |
| 178 | return (x); |
| 179 | } |
| 180 | |
| 181 | static int AVLrank(AVLnode *v, int pos) |
| 182 | { int k; |
| 183 | if (v == NIL) |
| 184 | k = 0; |
| 185 | else if (pos < v->V.start) |
| 186 | k = RNK(INC(v->L),pos); |
| 187 | else |
| 188 | k = CP(v) + RNK(INC(v->R),pos); |
| 189 | DEC(v); |
| 190 | return (k); |
| 191 | } |
| 192 | |
| 193 | static AVLnode *AVLminprf(AVLnode *v, int hgh, int bst) |
| 194 | { AVLnode *r; |
| 195 | int b = 0; |
| 196 | |
| 197 | if (v == NIL) |
| 198 | r = v; |
| 199 | else if (hgh < v->V.start) |
| 200 | r = AVLminprf(INC(v->L),hgh,bst); |
| 201 | else |
| 202 | { if (v->L->V.best < bst) |
| 203 | b = v->L->V.best; |
| 204 | if (v->V.base < bst) |
| 205 | b = v->V.base; |
| 206 | r = AVLminprf(INC(v->R),hgh,b); |
| 207 | if (r->V.base < bst) |
| 208 | bst = r->V.base; |
| 209 | if (v->V.base < bst) |
| 210 | { r = v; bst = v->V.base; } |
| 211 | if (v->L->V.best < bst) |
| 212 | r = AVLminprf(INC(v->L),hgh,bst); |
| 213 | } |
| 214 | DEC(v); |
| 215 | return (r); |
| 216 | } |
| 217 | |
| 218 | static AVLnode *AVLminsuf(AVLnode *v, int low, int bst) |
| 219 | { AVLnode *r; |
| 220 | int b = 0; |
| 221 | |
| 222 | if (v == NIL) |
| 223 | r = v; |
| 224 | else if (low > v->V.start) |
| 225 | r = AVLminsuf(INC(v->R),low,bst); |
| 226 | else |
| 227 | { if (v->R->V.best < bst) |
| 228 | b = v->R->V.best; |
| 229 | if (v->V.base < bst) |
| 230 | b = v->V.base; |
| 231 | r = AVLminsuf(INC(v->L),low,b); |
| 232 | if (r->V.base < bst) |
| 233 | bst = r->V.base; |
| 234 | if (v->V.base < bst) |
| 235 | { r = v; bst = v->V.base; } |
| 236 | if (v->R->V.best < bst) |
| 237 | r = AVLminsuf(INC(v->R),low,bst); |
| 238 | } |
| 239 | DEC(v); |
| 240 | return (r); |
| 241 | } |
| 242 | |
| 243 | static AVLnode *AVLminrng(AVLnode *v, int low, int hgh) |
| 244 | { AVLnode *r, *t; |
| 245 | |
| 246 | if (v == NIL) |
| 247 | r = v; |
| 248 | else if (hgh < v->V.start) |
| 249 | r = AVLminrng(INC(v->L),low,hgh); |
| 250 | else if (low > v->V.start) |
| 251 | r = AVLminrng(INC(v->R),low,hgh); |
| 252 | else |
| 253 | { r = v; |
| 254 | t = AVLminprf(INC(v->R),hgh,r->V.base); |
| 255 | if (t->V.base < r->V.base) |
| 256 | r = t; |
| 257 | t = AVLminsuf(INC(v->L),low,r->V.base); |
| 258 | if (t->V.base < r->V.base) |
| 259 | r = t; |
| 260 | } |
| 261 | DEC(v); |
| 262 | return (r); |
| 263 | } |
| 264 | |
| 265 | static AVLnode *AVLinsert(AVLnode *v, int k, Candidate *x) |
| 266 | { AVLnode *t; |
| 267 | if (v == NIL) |
| 268 | t = BAL(INC(NIL),x,INC(NIL)); |
| 269 | else if (k < CP(v)) |
| 270 | t = BAL(ADD(INC(v->L),k,x),&(v->V),INC(v->R)); |
| 271 | else |
| 272 | t = BAL(INC(v->L),&(v->V),ADD(INC(v->R),k-CP(v),x)); |
| 273 | DEC(v); |
| 274 | return (t); |
| 275 | } |
| 276 | |
| 277 | static AVLnode *AVLdelete(AVLnode *v, int k) |
| 278 | { AVLnode *t; |
| 279 | if (v->L == NIL && v->R == NIL) |
| 280 | t = INC(NIL); |
| 281 | else if (k <= CP(v) && v->L != NIL) |
| 282 | if (k == CP(v)) |
| 283 | t = BAL(DEL(INC(v->L),k-1),SEL(INC(v->L),k-1),INC(v->R)); |
| 284 | else |
| 285 | t = BAL(DEL(INC(v->L),k),&(v->V),INC(v->R)); |
| 286 | else |
| 287 | if (k == CP(v)) |
| 288 | t = BAL(INC(v->L),SEL(INC(v->R),1),DEL(INC(v->R),1)); |
| 289 | else |
| 290 | t = BAL(INC(v->L),&(v->V),DEL(INC(v->R),k-CP(v))); |
| 291 | DEC(v); |
| 292 | return (t); |
| 293 | } |
| 294 | |
| 295 | |
| 296 | |
| 297 | static int SSORT(const void *l, const void *r) |
| 298 | { Event *x, *y; |
| 299 | int ax, ay, bx, by; |
| 300 | |
| 301 | x = (Event *) l; |
| 302 | y = (Event *) r; |
| 303 | if (x->isadd) |
| 304 | { ax = x->item->abpos; |
| 305 | bx = x->item->bbpos; |
| 306 | } |
| 307 | else |
| 308 | { ax = x->item->aepos; |
| 309 | bx = x->item->bepos; |
| 310 | } |
| 311 | if (y->isadd) |
| 312 | { ay = y->item->abpos; |
| 313 | by = y->item->bbpos; |
| 314 | } |
| 315 | else |
| 316 | { ay = y->item->aepos; |
| 317 | by = y->item->bepos; |
| 318 | } |
| 319 | if (ax < ay) |
| 320 | return (-1); |
| 321 | else if (ax > ay) |
| 322 | return (1); |
| 323 | else if (x->isadd != y->isadd) |
| 324 | return (x->isadd - y->isadd); |
| 325 | else |
| 326 | return (bx - by); |
| 327 | } |
| 328 | |
| 329 | |
| 330 | static void convert_segs(Local_Segment *Segs,int NumSegs,int comp, int Alen,int Blen) |
| 331 | { int i; /* Mark and reverse all complemented local segs */ |
| 332 | |
| 333 | if (comp) |
| 334 | for (i = 0; i < NumSegs; i++) |
| 335 | { Segs[i].bbpos = Blen - Segs[i].bbpos; |
| 336 | Segs[i].bepos = Blen - Segs[i].bepos; |
| 337 | } |
| 338 | |
| 339 | for (i = 0; i < NumSegs; i++) |
| 340 | if (Segs[i].bbpos > Segs[i].bepos) |
| 341 | { int x; |
| 342 | x = Segs[i].bbpos; |
| 343 | Segs[i].bbpos = Segs[i].bepos; |
| 344 | Segs[i].bepos = x; |
| 345 | Segs[i].score = -Segs[i].score-1; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | static void restore_segs(Local_Segment *Segs,int NumSegs,int comp,int Alen,int Blen) |
| 350 | { int i; /* Unmark and reverse all complemented local segs */ |
| 351 | |
| 352 | for (i = 0; i < NumSegs; i++) |
| 353 | if (Segs[i].score < 0) |
| 354 | { int x; |
| 355 | x = Segs[i].bbpos; |
| 356 | Segs[i].bbpos = Segs[i].bepos; |
| 357 | Segs[i].bepos = x; |
| 358 | Segs[i].score = -Segs[i].score-1; |
| 359 | } |
| 360 | |
| 361 | if (comp) |
| 362 | { for (i = 0; i < NumSegs; i++) |
| 363 | { Segs[i].bbpos = Blen - Segs[i].bbpos; |
| 364 | Segs[i].bepos = Blen - Segs[i].bepos; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | |
| 370 | Local_Overlap *Find_Local_Overlap(int Alen, int Blen, int comp, int nextbest, |
| 371 | Local_Segment *Segs, int NumSegs, |
| 372 | int MinorThresh, double GapThresh) |
| 373 | { static Candidate Cvals; |
| 374 | static int MaxTrace = -1; |
| 375 | static TraceElement *Trace = NULL; |
| 376 | static Event *EventList; |
| 377 | Local_Overlap *Descriptor; |
| 378 | Local_Chain *Chain; |
| 379 | |
| 380 | if (NumSegs == 0) return (NULL); |
| 381 | |
| 382 | if (nextbest) |
| 383 | { if (Trace == NULL) return (NULL); |
| 384 | convert_segs(Segs,NumSegs,comp,Alen,Blen); |
| 385 | goto Gen_Overlap; |
| 386 | } |
| 387 | |
| 388 | if (MaxTrace < 0) |
| 389 | AVLinit(); |
| 390 | |
| 391 | if (NumSegs > MaxTrace) |
| 392 | { MaxTrace = (int)(1.3*NumSegs) + 500; |
| 393 | Trace = (TraceElement *) |
| 394 | realloc(Trace,(sizeof(Event)+2*sizeof(TraceElement))*MaxTrace); |
| 395 | if (Trace == NULL) |
| 396 | OutOfMemory("Overlap Trace Array"); |
| 397 | EventList = (Event *) (Trace + MaxTrace); |
| 398 | { // We have to make sure that EventList is aligned on an appropriate boundary. |
| 399 | // It is derived from Trace which has looser alignment constraints. |
| 400 | long address = (long)EventList; |
| 401 | // By convention "long" int is big as the size of a pointer |
| 402 | long offset = (address % sizeof(void *)); |
| 403 | int pad = sizeof(void *) - offset; |
| 404 | // This is how much we need to add to get things aligned. |
| 405 | if(offset){ |
| 406 | // fprintf(stderr,"* Eventlist is %p adding %d up to ", EventList, pad); |
| 407 | EventList = (Event *)(((char *)EventList) + pad); |
| 408 | // fprintf(stderr," %p\n", EventList); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | convert_segs(Segs,NumSegs,comp,Alen,Blen); |
| 414 | |
| 415 | { int i; |
| 416 | for (i = 0; i < NumSegs; i++) |
| 417 | { EventList[2*i].item = Segs+i; |
| 418 | EventList[2*i].isadd = 1; |
| 419 | EventList[2*i+1].item = Segs+i; |
| 420 | EventList[2*i+1].isadd = 0; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | qsort(EventList,2*NumSegs,sizeof(Event),SSORT); |
| 425 | |
| 426 | { int e; |
| 427 | AVLnode *elist, *ilist, *olist; |
| 428 | |
| 429 | elist = AVLinc(NIL); |
| 430 | ilist = AVLinc(NIL); |
| 431 | olist = AVLinc(NIL); |
| 432 | |
| 433 | for (e = 0; e < 2*NumSegs; e++) |
| 434 | { int i, bb, be, ab, ae; |
| 435 | double err; |
| 436 | |
| 437 | /* Determine least gapped path to i'th segment */ |
| 438 | |
| 439 | i = EventList[e].item - Segs; |
| 440 | bb = Segs[i].bbpos; |
| 441 | be = Segs[i].bepos; |
| 442 | ab = Segs[i].abpos; |
| 443 | ae = Segs[i].aepos; |
| 444 | err = Segs[i].error; |
| 445 | |
| 446 | if (EventList[e].isadd) /* Segment begins */ |
| 447 | { int clen, best, srce; |
| 448 | |
| 449 | // this definition of best differs from the original (below) |
| 450 | // it is designed to encourage global alignment |
| 451 | // |
| 452 | best = ab+bb; /* Best from boundary */ |
| 453 | |
| 454 | //best = ab; /* Best from boundary */ |
| 455 | //if (best > bb) |
| 456 | // best = bb; |
| 457 | //best *= 2; |
| 458 | |
| 459 | |
| 460 | srce = -1; |
| 461 | clen = AVLlength(AVLinc(elist)); |
| 462 | |
| 463 | { int p; /* Examine bests from elist */ |
| 464 | |
| 465 | p = AVLrank(AVLinc(elist),bb); /* Best @ start of seg */ |
| 466 | if (p > 0) |
| 467 | { Candidate *cand; |
| 468 | int altr; |
| 469 | |
| 470 | cand = AVLselect(AVLinc(elist),p); |
| 471 | altr = cand->base + (ab + bb); |
| 472 | if (altr < best) |
| 473 | { best = altr; |
| 474 | srce = cand->segment; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | while (++p <= clen) /* Bests @ midpoints of seg */ |
| 479 | { Candidate *cand; |
| 480 | int altr; |
| 481 | |
| 482 | cand = AVLselect(AVLinc(elist),p); |
| 483 | if (cand->start > be - MIN_USABLE) break; |
| 484 | altr = cand->base + 2*cand->start + (ab - bb); |
| 485 | if (altr < best) |
| 486 | { best = altr; |
| 487 | srce = cand->segment; |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | /* Examine bests from ilist and olist */ |
| 493 | |
| 494 | { AVLnode *m; |
| 495 | int bdiag, ldiag, altr; |
| 496 | |
| 497 | bdiag = bb - ab; |
| 498 | ldiag = bdiag + ((ae-ab) - MIN_USABLE); |
| 499 | m = AVLminprf(AVLinc(ilist),bdiag,BIG_INT); |
| 500 | if (m != NIL) |
| 501 | { altr = m->V.base + bdiag; |
| 502 | if (altr < best) |
| 503 | { srce = m->V.segment; |
| 504 | best = altr; |
| 505 | } |
| 506 | } |
| 507 | m = AVLminrng(AVLinc(olist),-ldiag,-bdiag); |
| 508 | if (m != NIL) |
| 509 | { altr = m->V.base - bdiag; |
| 510 | if (altr < best) |
| 511 | { srce = m->V.segment; |
| 512 | best = altr; |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | /* Record best linkage for segment */ |
| 518 | |
| 519 | Trace[i].value = best; |
| 520 | Trace[i].source = srce; |
| 521 | |
| 522 | Trace[i].colsAligned = (int)((1.-err)*(double)(min(ae-ab,be-bb)+1)); |
| 523 | if (srce >= 0){ |
| 524 | Trace[i].start = Trace[srce].start; |
| 525 | Trace[i].colsAligned += Trace[srce].colsAligned; |
| 526 | } else |
| 527 | Trace[i].start = i; |
| 528 | |
| 529 | /* Add segment to ilist and olist */ |
| 530 | |
| 531 | { int p, d; |
| 532 | |
| 533 | d = be - ae; |
| 534 | Cvals.segment = i; |
| 535 | |
| 536 | Cvals.start = d; |
| 537 | Cvals.base = best - d; |
| 538 | p = AVLrank(AVLinc(ilist),d); |
| 539 | ilist = AVLinsert(ilist,p,&Cvals); |
| 540 | |
| 541 | d = -d; |
| 542 | Cvals.start = d; |
| 543 | Cvals.base = best - d; |
| 544 | p = AVLrank(AVLinc(olist),d); |
| 545 | olist = AVLinsert(olist,p,&Cvals); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | else /* Segment ends */ |
| 550 | { int best, clen; |
| 551 | |
| 552 | best = Trace[i].value; |
| 553 | clen = AVLlength(AVLinc(elist)); |
| 554 | |
| 555 | /* Add candidate (if any) created by i'th segment */ |
| 556 | |
| 557 | { Candidate *cand; |
| 558 | int p, off; |
| 559 | |
| 560 | off = be + Segs[i].aepos; |
| 561 | p = AVLrank(AVLinc(elist),be); |
| 562 | if (p != 0) |
| 563 | cand = AVLselect(AVLinc(elist),p); |
| 564 | if (p == 0 || best < cand->base + off) |
| 565 | { p += 1; |
| 566 | while (p <= clen) |
| 567 | { cand = AVLselect(AVLinc(elist),p); |
| 568 | if (cand->base + off < best) break; |
| 569 | elist = AVLdelete(elist,p); |
| 570 | clen -= 1; |
| 571 | } |
| 572 | p -= 1; |
| 573 | if (p > 0) |
| 574 | { cand = AVLselect(AVLinc(elist),p); |
| 575 | if (cand->start == be) |
| 576 | elist = AVLdelete(elist,p--); |
| 577 | } |
| 578 | Cvals.start = be; |
| 579 | Cvals.base = best - off; |
| 580 | Cvals.segment = i; |
| 581 | elist = AVLinsert(elist,p,&Cvals); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | /* Remove candidates from ilist and olist */ |
| 586 | |
| 587 | { int p, d; |
| 588 | |
| 589 | d = be-ae; |
| 590 | p = AVLrank(AVLinc(ilist),d); |
| 591 | while (AVLselect(AVLinc(ilist),p)->segment != i) |
| 592 | p -= 1; |
| 593 | ilist = AVLdelete(ilist,p); |
| 594 | |
| 595 | p = AVLrank(AVLinc(olist),-d); |
| 596 | while (AVLselect(AVLinc(olist),p)->segment != i) |
| 597 | p -= 1; |
| 598 | olist = AVLdelete(olist,p); |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | } |
| 603 | |
| 604 | AVLdec(elist); |
| 605 | AVLdec(ilist); |
| 606 | AVLdec(olist); |
| 607 | } |
| 608 | |
| 609 | |
| 610 | |
| 611 | Gen_Overlap: |
| 612 | |
| 613 | { int i, npiece; |
| 614 | int best, end, beg; |
| 615 | |
| 616 | best = BIG_INT; /* Determine best overall overlap */ |
| 617 | end = -1; |
| 618 | for (i = 0; i < NumSegs; i++){ |
| 619 | // if (Trace[i].start >= 0) |
| 620 | if (Trace[i].start >= 0&&Trace[i].colsAligned >= MIN_ALIGNED_COLS) { |
| 621 | int sfx; |
| 622 | |
| 623 | // this definition of sfx differs from the original (below) |
| 624 | // it is designed to encourage global alignment |
| 625 | // |
| 626 | sfx = Alen - Segs[i].aepos + Blen - Segs[i].bepos; |
| 627 | |
| 628 | //sfx = Alen - Segs[i].aepos; |
| 629 | //if (Blen - Segs[i].bepos < sfx) |
| 630 | // sfx = Blen - Segs[i].bepos; |
| 631 | //sfx *= 2; |
| 632 | |
| 633 | // The "- 2 * Trace[i].colsAligned" makes us encourage longer alignments |
| 634 | // |
| 635 | if (Trace[i].value + sfx - 2*Trace[i].colsAligned < best) { |
| 636 | best = Trace[i].value - 2*Trace[i].colsAligned + sfx; |
| 637 | end = i; |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | if (end < 0) { |
| 643 | restore_segs(Segs,NumSegs,comp,Alen,Blen); |
| 644 | return (NULL); |
| 645 | } |
| 646 | |
| 647 | beg = Trace[end].start; |
| 648 | |
| 649 | /* How many segments in the best overlap? */ |
| 650 | |
| 651 | npiece = 0; |
| 652 | for (i = end; i >= 0; i = Trace[i].source) |
| 653 | npiece += 1; |
| 654 | |
| 655 | |
| 656 | |
| 657 | /* Allocate result data structures in a single memory block */ |
| 658 | |
| 659 | Descriptor = (Local_Overlap *) malloc(sizeof(Local_Overlap) + |
| 660 | (npiece+1)*sizeof(Local_Chain)); |
| 661 | if (Descriptor == NULL) |
| 662 | OutOfMemory("Overlap descriptor"); |
| 663 | Chain = (Local_Chain *) (Descriptor + 1); |
| 664 | |
| 665 | /* Fill out the description of the chain */ |
| 666 | |
| 667 | { int n; |
| 668 | n = npiece; |
| 669 | for (i = end; i >= 0; i = Trace[i].source) |
| 670 | Chain[--n].piece = Segs[i]; |
| 671 | } |
| 672 | |
| 673 | |
| 674 | #define ALLOW_DUP_SEGS_IN_NEXT /* allow all but the first segment to be |
| 675 | used in later attempts */ |
| 676 | #ifndef ALLOW_DUP_SEGS_IN_NEXT |
| 677 | for (i = 0; i < NumSegs; i++) |
| 678 | if (Trace[i].start == beg) |
| 679 | Trace[i].start = -1; /* this seems to prevent reuse of segments |
| 680 | in subsequent calls, and/or if we reject |
| 681 | this segment as too noisy and jump back |
| 682 | to the top */ |
| 683 | #else |
| 684 | Trace[end].start = -1; /* this seems to prevent reuse of segments |
| 685 | in subsequent calls, and/or if we reject |
| 686 | this segment as too noisy and jump back |
| 687 | to the top */ |
| 688 | #define REUSE_CURRENT_LAST_AS_NONTERMINAL_SEG |
| 689 | #ifndef REUSE_CURRENT_LAST_AS_NONTERMINAL_SEG |
| 690 | for (i = 0; i < NumSegs; i++) |
| 691 | if (Trace[i].source == beg) |
| 692 | Trace[i].source = -1; |
| 693 | #endif |
| 694 | |
| 695 | #endif |
| 696 | |
| 697 | |
| 698 | {// The last segment doesn't describe an alignment, only a gap. Initialize it to reasonable values |
| 699 | Local_Segment *lastseg = &Chain[npiece].piece; |
| 700 | lastseg->abpos = lastseg->bbpos = -1; |
| 701 | lastseg->aepos = lastseg->bepos = -1; |
| 702 | lastseg->ldiag = lastseg->hdiag = -1; |
| 703 | lastseg->score = -1; |
| 704 | lastseg->error = -1.0; |
| 705 | } |
| 706 | |
| 707 | { int gl; |
| 708 | /* there's basically a bug here: abpos = 1 means starts at first char of A; |
| 709 | so, agap should be 0, but gets set to 1; i.e., every first gap size |
| 710 | gets set to one too many; but there's existing code that relies on |
| 711 | this fact, so leave it alone for now */ |
| 712 | gl = Chain[0].piece.abpos; |
| 713 | if (gl > Chain[0].piece.bbpos) |
| 714 | gl = Chain[0].piece.bbpos; |
| 715 | Chain[0].agap = gl; |
| 716 | Chain[0].bgap = gl; |
| 717 | } |
| 718 | |
| 719 | for (i = 1; i < npiece; i++) |
| 720 | { Chain[i].agap = Chain[i].piece.abpos - Chain[i-1].piece.aepos; |
| 721 | Chain[i].bgap = Chain[i].piece.bbpos - Chain[i-1].piece.bepos; |
| 722 | } |
| 723 | |
| 724 | { int gl; |
| 725 | gl = Alen - Chain[npiece-1].piece.aepos; |
| 726 | if (gl > Blen - Chain[npiece-1].piece.bepos) |
| 727 | gl = Blen - Chain[npiece-1].piece.bepos; |
| 728 | Chain[npiece].agap = gl; |
| 729 | Chain[npiece].bgap = gl; |
| 730 | } |
| 731 | |
| 732 | for (i = 0; i <= npiece; i++) |
| 733 | { if (abs(Chain[i].agap) <= MinorThresh) |
| 734 | { if (abs(Chain[i].bgap) <= MinorThresh) |
| 735 | { if (Chain[i].agap != 0 || Chain[i].bgap != 0) |
| 736 | Chain[i].type = LOCAL_MINOR; |
| 737 | else |
| 738 | Chain[i].type = LOCAL_BOUNDARY; |
| 739 | } |
| 740 | else if (Chain[i].bgap < 0) |
| 741 | Chain[i].type = LOCAL_REPEAT; |
| 742 | else if (Chain[i].bgap > 4*Chain[i].agap) |
| 743 | Chain[i].type = LOCAL_INDEL; |
| 744 | else |
| 745 | Chain[i].type = LOCAL_DISAGREE; |
| 746 | } |
| 747 | else if (Chain[i].agap < 0) |
| 748 | { if (Chain[i].bgap < MinorThresh) |
| 749 | Chain[i].type = LOCAL_REPEAT; |
| 750 | else |
| 751 | Chain[i].type = LOCAL_REPnDEL; |
| 752 | } |
| 753 | else |
| 754 | { if (abs(Chain[i].bgap) < MinorThresh) |
| 755 | if (Chain[i].agap > 4*Chain[i].bgap) |
| 756 | Chain[i].type = LOCAL_INDEL; |
| 757 | else |
| 758 | Chain[i].type = LOCAL_DISAGREE; |
| 759 | else if (Chain[i].bgap < 0) |
| 760 | Chain[i].type = LOCAL_REPnDEL; |
| 761 | else |
| 762 | Chain[i].type = LOCAL_DISAGREE; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | /* Fill out overlap descriptor */ |
| 767 | |
| 768 | Descriptor->num_pieces = npiece; |
| 769 | Descriptor->score = best; |
| 770 | Descriptor->chain = Chain; |
| 771 | Descriptor->comp = comp; |
| 772 | |
| 773 | { Local_Segment *sg; |
| 774 | int ln; |
| 775 | |
| 776 | Descriptor->indif = 0; |
| 777 | |
| 778 | for (i = 0; i < npiece; i++) |
| 779 | { sg = &(Chain[i].piece); |
| 780 | ln = ((sg->aepos - sg->abpos) + (sg->bepos - sg->bbpos)) / 2; |
| 781 | if (i > 0 && Chain[i-1].piece.error < sg->error) |
| 782 | { if (Chain[i].agap < Chain[i].bgap) |
| 783 | { if (Chain[i].agap < 0) |
| 784 | ln += Chain[i].agap; |
| 785 | } |
| 786 | else |
| 787 | { if (Chain[i].bgap < 0) |
| 788 | ln += Chain[i].bgap; |
| 789 | } |
| 790 | } |
| 791 | if (i < npiece-1 && Chain[i+1].piece.error <= sg->error) |
| 792 | { if (Chain[i+1].agap < Chain[i+1].bgap) |
| 793 | { if (Chain[i+1].agap < 0) |
| 794 | ln += Chain[i+1].agap; |
| 795 | } |
| 796 | else |
| 797 | { if (Chain[i+1].bgap < 0) |
| 798 | ln += Chain[i+1].bgap; |
| 799 | } |
| 800 | } |
| 801 | if (ln > 0) |
| 802 | Descriptor->indif += (int)(ln * sg->error); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | Descriptor->diffs = Descriptor->indif; |
| 807 | |
| 808 | for (i = 0; i <= npiece; i++) |
| 809 | { int d; |
| 810 | if (Chain[i].agap < 0 || Chain[i].bgap < 0) |
| 811 | d = abs( (Chain[i].piece.bbpos - Chain[i].piece.abpos) - |
| 812 | (Chain[i-1].piece.bepos - Chain[i-1].piece.aepos)); |
| 813 | else |
| 814 | { d = Chain[i].agap; |
| 815 | if (d < Chain[i].bgap) d = Chain[i].bgap; |
| 816 | } |
| 817 | Descriptor->diffs += d; |
| 818 | } |
| 819 | |
| 820 | { int overa, overb; |
| 821 | overa = (Chain[npiece-1].piece.aepos + Chain[npiece].agap) |
| 822 | - (Chain[0].piece.abpos - Chain[0].agap); |
| 823 | overb = (Chain[npiece-1].piece.bepos + Chain[npiece].bgap) |
| 824 | - (Chain[0].piece.bbpos - Chain[0].bgap); |
| 825 | Descriptor->length = (overa + overb) / 2; |
| 826 | } |
| 827 | |
| 828 | Descriptor->begpos = Chain[0].piece.abpos - Chain[0].piece.bbpos; |
| 829 | Descriptor->endpos = (Blen - Chain[npiece-1].piece.bepos) |
| 830 | - (Alen - Chain[npiece-1].piece.aepos); |
| 831 | |
| 832 | for (i = 0; i < npiece; i++) |
| 833 | if (Chain[i].piece.score < 0) |
| 834 | { int x; |
| 835 | x = Chain[i].piece.bbpos; |
| 836 | Chain[i].piece.bbpos = Chain[i].piece.bepos; |
| 837 | Chain[i].piece.bepos = x; |
| 838 | Chain[i].piece.score = - Chain[i].piece.score-1; |
| 839 | Chain[i].reversed = 1; |
| 840 | } |
| 841 | else |
| 842 | Chain[i].reversed = 0; |
| 843 | } |
| 844 | |
| 845 | restore_segs(Segs,NumSegs,comp,Alen,Blen); /* undo comp and rc changes */ |
| 846 | |
| 847 | return (Descriptor); |
| 848 | } |