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 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
|
#include "pilercr.h"
#undef TEST_DPREACH
#undef TEST_TRAPTRIM
#undef STAT_DPREACH
#undef SHOW_DIAGS
#undef REPORT_DPREACH
#undef REPORT_DPREACH
#define MAXIGAP 5
#define DIFFCOST 3
#define SAMECOST 1
extern Trapezoid **Tarray;
extern int *Covered;
extern int SegMax;
//extern DPHit *SegSols;
//extern int NumSegs;
static int BLOCKCOST = DIFFCOST*MAXIGAP;
static int MATCHCOST = DIFFCOST+SAMECOST;
static double RMATCHCOST = DIFFCOST+1.;
/*** FORWARD AND REVERSE D.P. EXTENSION ROUTINES ***/
/* Called at the mid-point of trapezoid -- mid X [lo,hi], the extension
is computed to an end point and the lowest and highest diagonals
are recorded. These are returned in a partially filled DPHit
record, that will be merged with that returned for extension in the
opposite direction.
*/
static int VecMax = -1;
static int *Vec1 = NULL;
static int *Vec2;
DPHit *TraceForwardPath(char *A, int Alen, char *B, int Blen,
int mid, int lo, int hi)
{ static DPHit rez;
int *V, odd;
int mxv, mxl, mxr, mxi, mxj;
int i, j;
#ifdef STAT_DPREACH
long dparea;
#endif
/* Set basis from (mid,lo) .. (mid,hi) */
if (lo < 0) lo = 0;
if (hi > Blen) hi = Blen;
if ((hi-lo)+MAXIGAP >= VecMax)
{ VecMax = (int) (1.2*((hi-lo) + MAXIGAP + 1) + 10000);
Vec1 = (int *) ckrealloc(Vec1,2*VecMax*sizeof(int),"Vector D.P. arrays");
Vec2 = Vec1 + VecMax;
}
V = Vec1 - lo;
odd = 1;
for (j = lo; j <= hi; j++)
V[j] = 0;
hi += MAXIGAP;
if (hi > Blen) hi = Blen;
for (; j <= hi; j++)
V[j] = V[j-1] - DIFFCOST;
mxv = 0;
mxr = mid - lo;
mxl = mid - hi;
mxi = mid;
mxj = lo;
#ifdef STAT_DPREACH
dparea = (hi-lo+1);
#endif
/* Advance to next row */
int maxi = mid + g_MaxDPHitLen - 1;
if (maxi >= Alen)
maxi = Alen;
for (i = mid; lo <= hi && i < maxi; i++)
// for (i = mid; lo <= hi && i < Alen; i++)
{ int c, v;
int *W;
W = V;
if (odd)
V = Vec2 - lo;
else
V = Vec1 - lo;
odd = 1-odd;
#ifdef TEST_DPREACH
Log("\n%d [%d,%d] x = %d\n ",i,lo,hi,mxv);
for (j = lo; j <= hi; j++)
Log(" %c ",B[j]);
Log("\n ");
for (j = lo; j <= hi; j++)
Log(" %3d",W[j]);
Log("\n%c",A[i]);
#endif
v = W[lo];
c = V[lo] = v - DIFFCOST;
#ifdef TEST_DPREACH
Log(" %3d",c);
#endif
for (j = lo+1; j <= hi; j++)
{ int r, t;
t = c;
c = v;
v = W[j];
if (A[i] == B[j-1] && CharToLetter[(unsigned char) (A[i])] >= 0) c += MATCHCOST;
r = c;
if (v > r) r = v;
if (t > r) r = t;
V[j] = c = r - DIFFCOST;
if (c >= mxv)
{ mxv = c;
mxi = i+1;
mxj = j;
}
#ifdef TEST_DPREACH
Log(" %3d",c);
#endif
}
if (j <= Blen)
{ int r;
if (A[i] == B[j-1] && CharToLetter[(unsigned char) (A[i])] >= 0) v += MATCHCOST;
r = v;
if (c > r) r = c;
V[j] = v = r - DIFFCOST;
if (v > mxv)
{ mxv = v;
mxi = i+1;
mxj = j;
}
#ifdef TEST_DPREACH
Log(" %3d",v);
#endif
for (j++; j <= Blen; j++)
{ v -= DIFFCOST;
if (v < mxv - BLOCKCOST) break;
V[j] = v;
#ifdef TEST_DPREACH
Log(" %3d",v);
#endif
}
}
#ifdef TEST_DPREACH
Log("\n");
#endif
hi = j-1;
while (lo <= hi && V[lo] < mxv - BLOCKCOST)
lo += 1;
while (lo <= hi && V[hi] < mxv - BLOCKCOST)
hi -= 1;
if ((hi-lo)+2 > VecMax)
{ VecMax = (int) (1.2*((hi-lo) + 2) + 10000);
Vec1 = (int *) ckrealloc(Vec1,2*VecMax*sizeof(int),"Vector D.P. arrays");
Vec2 = Vec1 + VecMax;
}
if ((i+1) - lo > mxr)
mxr = (i+1) - lo;
if ((i+1) - hi < mxl)
mxl = (i+1) - hi;
#ifdef STAT_DPREACH
dparea += (hi-lo+1);
#endif
}
#ifdef STAT_DPREACH
Log(" DP_Area = %ld Peak is %d @ (%d,%d) in [%d,%d]\n",
dparea,mxv,mxi,mxj,mxl,mxr);
#endif
rez.aHi = mxj;
rez.bHi = mxi;
rez.ldiag = mxl;
rez.hdiag = mxr;
rez.score = mxv;
return (&rez);
}
DPHit *TraceReversePath(char *A, int Alen, char *B, int Blen,
int top, int lo, int hi, int bot,
int xfactor)
{ static DPHit rez;
int *V, odd;
int mxv, mxl, mxr, mxi, mxj;
int i, j;
#ifdef STAT_DPREACH
long dparea;
#endif
/* Set basis from (top,lo) .. (top,hi) */
if (lo < 0) lo = 0;
if (hi > Blen) hi = Blen;
if ((hi-lo)+MAXIGAP >= VecMax)
{ VecMax = (int) (1.2*((hi-lo) + MAXIGAP + 1) + 10000);
Vec1 = (int *) ckrealloc(Vec1,2*VecMax*sizeof(int),"Vector D.P. arrays");
Vec2 = Vec1 + VecMax;
}
V = Vec1 + ((VecMax-1) - hi);
odd = 1;
for (j = hi; j >= lo; j--)
V[j] = 0;
lo -= MAXIGAP;
if (lo < 0) lo = 0;
for (; j >= lo; j--)
V[j] = V[j+1] - DIFFCOST;
mxv = 0;
mxr = top - lo;
mxl = top - hi;
mxi = top;
mxj = lo;
#ifdef STAT_DPREACH
dparea = (hi-lo+1);
#endif
/* Advance to next row */
if (top-1 <= bot) xfactor = BLOCKCOST;
int mini = top - g_MaxDPHitLen;
if (mini < 0)
mini = 0;
for (i = top-1; lo <= hi && i >= mini; i--)
// for (i = top-1; lo <= hi && i >= 0; i--)
{ int c, v;
int *W;
W = V;
if (odd)
V = Vec2 + ((VecMax-1) - hi);
else
V = Vec1 + ((VecMax-1) - hi);
odd = 1-odd;
#ifdef TEST_DPREACH
Log("\n%d [%d,%d] x = %d\n ",i,lo,hi,mxv);
for (j = hi; j >= lo; j--)
Log(" %c ",B[j-1]);
Log("\n ");
for (j = hi; j >= lo; j--)
Log(" %3d",W[j]);
Log("\n%c",A[i]);
#endif
v = W[hi];
c = V[hi] = v - DIFFCOST;
#ifdef TEST_DPREACH
Log(" %3d",c);
#endif
for (j = hi-1; j >= lo; j--)
{ int r, t;
t = c;
c = v;
v = W[j];
if (A[i] == B[j] && CharToLetter[(unsigned char) (A[i])] >= 0) c += MATCHCOST;
r = c;
if (v > r) r = v;
if (t > r) r = t;
V[j] = c = r - DIFFCOST;
if (c >= mxv)
{ mxv = c;
mxi = i;
mxj = j;
}
#ifdef TEST_DPREACH
Log(" %3d",c);
#endif
}
if (j >= 0)
{ int r;
if (A[i] == B[j] && CharToLetter[(unsigned char) (A[i])] >= 0) v += MATCHCOST;
r = v;
if (c > r) r = c;
V[j] = v = r - DIFFCOST;
if (v > mxv)
{ mxv = v;
mxi = i;
mxj = j;
}
#ifdef TEST_DPREACH
Log(" %3d",v);
#endif
for (j--; j >= 0; j--)
{ v -= DIFFCOST;
if (v < mxv - xfactor) break;
V[j] = v;
#ifdef TEST_DPREACH
Log(" %3d",v);
#endif
}
}
#ifdef TEST_DPREACH
Log("\n");
#endif
lo = j+1;
while (lo <= hi && V[lo] < mxv - xfactor)
lo += 1;
while (lo <= hi && V[hi] < mxv - xfactor)
hi -= 1;
if (i == bot) xfactor = BLOCKCOST;
if ((hi-lo)+2 > VecMax)
{ VecMax = (int) (1.2*((hi-lo) + 2) + 10000);
Vec1 = (int *) ckrealloc(Vec1,2*VecMax*sizeof(int),"Vector D.P. arrays");
Vec2 = Vec1 + VecMax;
}
if (i-lo > mxr)
mxr = i-lo;
if (i-hi < mxl)
mxl = i-hi;
#ifdef STAT_DPREACH
dparea += (hi-lo+1);
#endif
}
#ifdef STAT_DPREACH
Log(" DP_Area = %ld Peak is %d @ (%d,%d) in [%d,%d]\n",
dparea,mxv,mxi,mxj,mxl,mxr);
#endif
rez.aLo = mxj;
rez.bLo = mxi;
rez.ldiag = mxl;
rez.hdiag = mxr;
rez.score = mxv;
return (&rez);
}
/*** FINDING ALIGNMENTS WITHIN A TRAPEZOIDAL ZONE ***/
int TSORT(const void *l, const void *r)
{ Trapezoid *x, *y;
x = *((Trapezoid **) l);
y = *((Trapezoid **) r);
return (x->bot - y->bot);
}
int StSORT(const void *l, const void *r)
{ DPHit *x, *y;
x = (DPHit *) l;
y = (DPHit *) r;
if (x->aLo < y->aLo)
return (-1);
else if (x->aLo > y->aLo)
return (1);
else
return (x->bLo - y->bLo);
}
int FnSORT(const void *l, const void *r)
{ DPHit *x, *y;
x = (DPHit *) l;
y = (DPHit *) r;
if (x->aHi < y->aHi)
return (-1);
else if (x->aHi > y->aHi)
return (1);
else
return (x->bHi - y->bHi);
}
#ifdef REPORT_DPREACH
static int Al_depth;
#endif
void Align_Recursion(char *A, int Alen, char *B, int Blen,
Trapezoid *b, int current, int comp,
int MinLen, double MaxDiff, int Traplen)
{ int j, mid, indel;
float pcnt;
DPHit *hend, *lend;
Trapezoid ltrp, htrp;
mid = (b->bot + b->top) / 2;
#ifdef REPORT_DPREACH
Log(" [%d,%d]x[%d,%d] = %d (Depth = %d)\n",
b->bot,b->top,b->lft,b->rgt,b->top - b->bot + 1,Al_depth);
#endif
lend = TraceForwardPath(B,Blen,A,Alen,mid,mid-b->rgt,mid-b->lft);
{ int x;
x = 0;
do
{ x += 1;
hend = TraceReversePath(B,Blen,A,Alen,
lend->bHi,lend->aHi,lend->aHi,
mid+MAXIGAP,BLOCKCOST+2*x*DIFFCOST);
}
while (hend->bLo > mid + x*MAXIGAP && hend->score < lend->score);
}
hend->aHi = lend->aHi;
hend->bHi = lend->bHi;
#ifdef REPORT_DPREACH
Log(" Got [%d,%d]x[%d,%d] ([%d,%d]) at score = %d\n",
hend->bLo,hend->bHi,hend->ldiag,hend->hdiag,hend->aLo,hend->aHi,hend->score);
#endif
ltrp = htrp = *b;
ltrp.top = hend->bLo - MAXIGAP;
htrp.bot = hend->bHi + MAXIGAP;
if (hend->bHi - hend->bLo >= MinLen &&
hend->aHi - hend->aLo >= MinLen )
{ indel = abs( (hend->aLo - hend->bLo)
- (hend->aHi - hend->bHi) );
pcnt = (float)((1/RMATCHCOST)
- (hend->score - indel)
/ (RMATCHCOST*(hend->bHi - hend->bLo)));
if (pcnt <= MaxDiff)
{ hend->error = pcnt;
for (j = current+1; j < Traplen; j++)
{ Trapezoid *t;
int ta, tb, ua, ub;
t = Tarray[j];
if (t->bot >= hend->bHi) break;
tb = t->top - t->bot + 1;
ta = t->rgt - t->lft + 1;
if (t->lft < hend->ldiag)
ua = hend->ldiag;
else
ua = t->lft;
if (t->rgt > hend->hdiag)
ub = hend->hdiag;
else
ub = t->rgt;
if (ua > ub) continue;
ua = ub - ua + 1;
if (t->top > hend->bHi)
ub = hend->bHi - t->bot + 1;
else
ub = tb;
if (((1.*ua)/ta)*((1.*ub)/tb) > .99)
Covered[j] = 1;
}
//if (NumSegs >= SegMax)
// { SegMax = (int)(1.2*NumSegs + 500);
// SegSols = (DPHit *) ckrealloc(SegSols,
// sizeof(DPHit)*SegMax,
// "Segment Alignment array");
// }
{ int d;
d = hend->ldiag; /* Oops, diags to this point are b-a, not a-b. */
hend->ldiag = - (hend->hdiag);
hend->hdiag = - d;
if (comp)
{ hend->bLo = Blen - hend->bLo;
hend->bHi = Blen - hend->bHi;
hend->ldiag = Blen + hend->ldiag;
hend->hdiag = Blen + hend->hdiag;
}
}
if (AcceptHit(*hend))
g_Hits.push_back(*hend);
else
{
if (fDiscardedHits)
{
WriteDPHit(fDiscardedHits, *hend, false);
++g_DiscardedHitCount;
}
}
#ifdef REPORT_DPREACH
Log(" Hit from (%d,%d) to (%d,%d) within [%d,%d] score %d\n",
hend->aLo,hend->bLo,hend->aHi,hend->bHi,
hend->ldiag,hend->hdiag,hend->score);
#endif
}
}
#ifdef REPORT_DPREACH
Al_depth += 1;
#endif
if (ltrp.top - ltrp.bot > MinLen && ltrp.top < b->top - MAXIGAP)
Align_Recursion(A,Alen,B,Blen,<rp,current,comp,MinLen,MaxDiff,Traplen);
if (htrp.top - htrp.bot > MinLen)
Align_Recursion(A,Alen,B,Blen,&htrp,current,comp,MinLen,MaxDiff,Traplen);
#ifdef REPORT_DPREACH
Al_depth -= 1;
#endif
}
|