| 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 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <unistd.h> |
| 23 | #include <string.h> |
| 24 | #include <assert.h> |
| 25 | #include <stdlib.h> |
| 26 | |
| 27 | #include "halign.H" |
| 28 | |
| 29 | #if defined (__SVR4) && defined (__sun) |
| 30 | // Solaris defines SS in sys/regset.h |
| 31 | #undef SS |
| 32 | #endif |
| 33 | |
| 34 | #define min(x,y) ((x)<=(y) ? (x):(y)) |
| 35 | #define max(x,y) ((x)>=(y) ? (x):(y)) |
| 36 | |
| 37 | typedef enum { DEL, INS, SUB, MOV } OpType; |
| 38 | |
| 39 | #define START 1 // The position of the first character. |
| 40 | #define GAP_OPEN 1 // These are costs. |
| 41 | #define GAP_EXTEND 1 |
| 42 | #define MISMATCH 1 |
| 43 | |
| 44 | class EditScript { |
| 45 | public: |
| 46 | EditScript(OpType op, int nm, EditScript *nx) { |
| 47 | op_type = op; |
| 48 | num = nm; |
| 49 | next = nx; |
| 50 | }; |
| 51 | |
| 52 | OpType op_type; // SUB, MOV, INS, or DEL |
| 53 | int num; // Number of operations |
| 54 | EditScript *next; |
| 55 | }; |
| 56 | |
| 57 | |
| 58 | void |
| 59 | convertScriptToAlignment(EditScript *head, |
| 60 | H_Alignment_t *aln) { |
| 61 | |
| 62 | EditScript *tp = head; |
| 63 | EditScript *tp1; |
| 64 | int scriptLen = 3; |
| 65 | |
| 66 | |
| 67 | // Three sets of comments, from three different sources: |
| 68 | // |
| 69 | // Condense_script - merge contiguous operations of the same type |
| 70 | // together. Remove the leftmost dummy script (Ascript[0][0]) |
| 71 | // |
| 72 | // Condense the script in block operations; this will modify the |
| 73 | // script and will cut off the dummy edit op from the linked list |
| 74 | // |
| 75 | // Condense any repeat operations. For example, condense this |
| 76 | // script: "DEL 5; DEL 3" into this array representation: "DEL 8". |
| 77 | |
| 78 | while (tp && tp->next) { |
| 79 | scriptLen += 2; |
| 80 | |
| 81 | while (((tp1 = tp->next) != NULL) && |
| 82 | (tp->op_type == tp1->op_type)) { |
| 83 | tp->num += tp1->num; |
| 84 | tp->next = tp1->next; |
| 85 | free(tp1); |
| 86 | } |
| 87 | |
| 88 | tp = tp->next; |
| 89 | } |
| 90 | |
| 91 | // Allocate space for the alignment |
| 92 | // |
| 93 | if (aln->scriptAsArrayMax <= scriptLen) { |
| 94 | free(aln->scriptAsArray); |
| 95 | |
| 96 | aln->scriptAsArrayMax = scriptLen; |
| 97 | aln->scriptAsArray = (int *)malloc(scriptLen * sizeof(int)); |
| 98 | |
| 99 | assert(aln->scriptAsArray != NULL); |
| 100 | } |
| 101 | |
| 102 | aln->scriptAsArray[0] = 0; |
| 103 | |
| 104 | // Convert. |
| 105 | // |
| 106 | int arySize = 0; |
| 107 | EditScript *tpdel = 0L; |
| 108 | |
| 109 | tp = head; |
| 110 | while (tp != NULL) { |
| 111 | aln->scriptAsArray[++arySize] = tp->op_type; |
| 112 | aln->scriptAsArray[++arySize] = tp->num; |
| 113 | |
| 114 | tpdel = tp; |
| 115 | tp = tp->next; |
| 116 | delete tpdel; |
| 117 | } |
| 118 | |
| 119 | aln->scriptAsArray[0] = arySize; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | |
| 124 | static |
| 125 | int |
| 126 | diff(const char *s1, |
| 127 | const char *s2, |
| 128 | int len1, |
| 129 | int len2, |
| 130 | int *CC, |
| 131 | int *DD, |
| 132 | int *RR, |
| 133 | int *SS, |
| 134 | int g, |
| 135 | int h, |
| 136 | int x, |
| 137 | int start_cgap, |
| 138 | int end_cgap, |
| 139 | int free_start, |
| 140 | int free_end, |
| 141 | int tb, |
| 142 | int te, |
| 143 | EditScript **head, |
| 144 | EditScript **tail) { |
| 145 | |
| 146 | int i, j, s, t, c, e, tmp; |
| 147 | const char *a, *b; |
| 148 | int mincost, mintype, midi, midj; |
| 149 | EditScript *tmp_head = 0L; |
| 150 | EditScript *tmp_tail = 0L; |
| 151 | |
| 152 | if (len1==0 && len2==0) { |
| 153 | |
| 154 | *head = *tail = NULL; |
| 155 | return 0; |
| 156 | |
| 157 | } else if (len2 == 0) { |
| 158 | int tmpb, tmpe; |
| 159 | |
| 160 | *head = *tail = new EditScript(DEL,len1,NULL); |
| 161 | tmpb = (len1 <= start_cgap) ? 0 : tb+h*(len1-start_cgap); |
| 162 | tmpe = (len1 <= end_cgap) ? 0 : te+h*(len1-end_cgap); |
| 163 | |
| 164 | return min(tmpb,tmpe); |
| 165 | |
| 166 | } else if (len1 == 0) { |
| 167 | |
| 168 | *head = *tail = new EditScript(INS,len2,NULL); |
| 169 | return ((free_start || free_end) ? 0 : g+(len2*h)); |
| 170 | |
| 171 | } else if (len1 == 1) { |
| 172 | int tmpcost; |
| 173 | char ch; |
| 174 | |
| 175 | /* insert B, delete A; or delete A, insert B */ |
| 176 | mincost = (start_cgap ? 0 : min(tb,te)+h) + ((free_start || free_end) ? 0 : g+len2*h); |
| 177 | mintype = 2; midj = (free_start ? len2 : 0); |
| 178 | |
| 179 | /* ... or insert some B, substitute A, insert the rest of B */ |
| 180 | for (j=0, ch=*s1; j<len2; j++) { |
| 181 | tmpcost = (ch==s2[j]) ? 0 : x; |
| 182 | if (!free_start && j) tmpcost += g+j*h; |
| 183 | if (!free_end && j+1<len2) tmpcost += g+(len2-j-1)*h; |
| 184 | if (tmpcost<mincost) { mincost = tmpcost; mintype = 1; midj = j; } |
| 185 | } |
| 186 | if (mintype==2) { |
| 187 | /* delete A */ |
| 188 | if (free_start) { |
| 189 | *tail = new EditScript(DEL,1,NULL); |
| 190 | *head = new EditScript(INS,len2,*tail); |
| 191 | } else { |
| 192 | *tail = new EditScript(INS,len2,NULL); |
| 193 | *head = new EditScript(DEL,1,*tail); |
| 194 | } |
| 195 | } else { /* substitute A */ |
| 196 | EditScript *aux; |
| 197 | *tail = (midj<len2-1) ? new EditScript(INS,len2-midj-1,NULL) : NULL; |
| 198 | aux = new EditScript((ch==s2[midj] ? MOV : SUB),1,*tail); |
| 199 | if (*tail==NULL) *tail = aux; |
| 200 | *head = (midj>0) ? new EditScript(INS,midj,aux) : aux; |
| 201 | } |
| 202 | return mincost; |
| 203 | |
| 204 | } else { |
| 205 | int tmph, tmpg; |
| 206 | |
| 207 | midi = (int)(len1/2); |
| 208 | |
| 209 | /* compute CC and DD in the forward phase */ |
| 210 | tmph = free_start ? 0 : h; |
| 211 | tmpg = free_start ? 0 : g; |
| 212 | for (CC[0]=0, t=tmpg, j=1; j<=len2; j++) { |
| 213 | /* if free_start, allow gap-free ends in the genomic sequence */ |
| 214 | CC[j] = DD[j] = t = t+tmph; |
| 215 | DD[j] += tmpg; |
| 216 | } |
| 217 | |
| 218 | for (a=s1, i=1; i<=midi; i++, a++) { |
| 219 | |
| 220 | s = CC[0]; |
| 221 | CC[0] = c = t = max(i-start_cgap,0)*h + (i>start_cgap)*tb; |
| 222 | |
| 223 | e = t + g; |
| 224 | for (b=s2, j=1; j<=len2; j++, b++) { |
| 225 | |
| 226 | e = min(e, c+g) + h; |
| 227 | |
| 228 | DD[j] = (j==len2 && i>=len1-end_cgap+1) ? |
| 229 | min(DD[j], CC[j]) : (min(DD[j]+(i==start_cgap+1)*g, CC[j]+g) + h); |
| 230 | |
| 231 | c = min(DD[j], min(e, s+x*(*a!=*b))); |
| 232 | s = CC[j]; CC[j] = c; |
| 233 | } |
| 234 | } |
| 235 | DD[0] = CC[0]; |
| 236 | |
| 237 | /* compute RR and SS in the reverse phase */ |
| 238 | tmph = free_end ? 0 : h; |
| 239 | tmpg = free_end ? 0 : g; |
| 240 | for (RR[len2]=0, t=tmpg, j=len2-1; j>=0; --j) { |
| 241 | /* if free_end, allow gap-free ends in the genomic sequence */ |
| 242 | RR[j] = SS[j] = t = t+tmph; |
| 243 | SS[j] += tmpg; |
| 244 | } |
| 245 | |
| 246 | for (a=s1+len1-1, i=len1-1; i>=midi; --i, --a) { |
| 247 | |
| 248 | s = RR[len2]; |
| 249 | RR[len2] = c = t = max((len1-end_cgap)-i,0)*h + (i<len1-end_cgap)*te; |
| 250 | |
| 251 | e = t + g; |
| 252 | for (b=s2+len2-1, j=len2-1; j>=0; --j, --b) { |
| 253 | |
| 254 | e = min(e, c+g) + h; |
| 255 | |
| 256 | SS[j] = (j==0 && i<start_cgap) ? |
| 257 | min(SS[j], RR[j]) : (min(SS[j]+(i==len1-end_cgap-1)*g, RR[j]+g) + h); |
| 258 | c = min(SS[j], min(e, s+x*(*a!=*b))); |
| 259 | s = RR[j]; RR[j] = c; |
| 260 | |
| 261 | |
| 262 | } |
| 263 | } |
| 264 | SS[len2] = RR[len2]; |
| 265 | } |
| 266 | |
| 267 | /* find midj that minimizes the sum */ |
| 268 | /* special cases: columns 0 and len2 */ |
| 269 | midj = 0; |
| 270 | if (CC[0]+RR[0]<=DD[0]+SS[0]-g*(midi>start_cgap)) { |
| 271 | mincost = CC[0]+RR[0]; |
| 272 | mintype = 1; |
| 273 | } else { |
| 274 | mincost = DD[0]+SS[0]-g*(midi>start_cgap); |
| 275 | mintype = 2; |
| 276 | } |
| 277 | |
| 278 | for (j=1; j<len2; j++) { |
| 279 | tmp = min(CC[j]+RR[j],DD[j]+SS[j]-g); |
| 280 | if (mincost > tmp) { |
| 281 | mincost = tmp; |
| 282 | midj = j; |
| 283 | mintype = (tmp == CC[j]+RR[j]) ? 1:2; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | tmp = min(CC[len2]+RR[len2],DD[len2]+SS[len2]-g*(midi<len1-end_cgap)); |
| 288 | if (mincost > tmp) { |
| 289 | mincost = tmp; |
| 290 | midj = len2; |
| 291 | mintype = (tmp==CC[len2]+RR[len2]) ? 1:2; |
| 292 | } |
| 293 | /* compute recursively in the two subregions */ |
| 294 | if (mintype==1) { |
| 295 | int cost1, cost2; |
| 296 | |
| 297 | cost1 = diff(s1, |
| 298 | s2, |
| 299 | midi, |
| 300 | midj, |
| 301 | CC, |
| 302 | DD, |
| 303 | RR, |
| 304 | SS, |
| 305 | g, |
| 306 | h, |
| 307 | x, |
| 308 | min(start_cgap,midi), |
| 309 | max(end_cgap-len1+midi,0), |
| 310 | free_start, |
| 311 | 0, |
| 312 | tb, |
| 313 | g, |
| 314 | head, |
| 315 | &tmp_tail); |
| 316 | cost2 = diff(s1+midi, |
| 317 | s2+midj, |
| 318 | len1-midi, |
| 319 | len2-midj, |
| 320 | CC, |
| 321 | DD, |
| 322 | RR, |
| 323 | SS, |
| 324 | g, |
| 325 | h, |
| 326 | x, |
| 327 | max(0,start_cgap-midi), |
| 328 | min(end_cgap,len1-midi), |
| 329 | 0, |
| 330 | free_end, |
| 331 | g, |
| 332 | te, |
| 333 | &tmp_head, |
| 334 | tail); |
| 335 | |
| 336 | if (*head) |
| 337 | tmp_tail->next = tmp_head; |
| 338 | else |
| 339 | *head = tmp_head; |
| 340 | |
| 341 | assert(NULL != *tail); |
| 342 | |
| 343 | } else { |
| 344 | EditScript *aux; |
| 345 | int cost1, cost2; |
| 346 | |
| 347 | cost1 = diff(s1, |
| 348 | s2, |
| 349 | midi-1, |
| 350 | midj, |
| 351 | CC, |
| 352 | DD, |
| 353 | RR, |
| 354 | SS, |
| 355 | g, |
| 356 | h, |
| 357 | x, |
| 358 | min(start_cgap,midi-1), |
| 359 | max(end_cgap-len1+midi-1,0), |
| 360 | free_start, |
| 361 | 0, |
| 362 | tb, |
| 363 | 0, |
| 364 | head, |
| 365 | &tmp_tail); |
| 366 | |
| 367 | aux = new EditScript(DEL,2,NULL); |
| 368 | |
| 369 | if (*head) |
| 370 | tmp_tail->next = aux; |
| 371 | else |
| 372 | tmp_tail = *head = aux; |
| 373 | |
| 374 | cost2 = diff(s1+midi+1, |
| 375 | s2+midj, |
| 376 | len1-midi-1, |
| 377 | len2-midj, |
| 378 | CC, |
| 379 | DD, |
| 380 | RR, |
| 381 | SS, |
| 382 | g, |
| 383 | h, |
| 384 | x, |
| 385 | max(0,start_cgap-midi-1), |
| 386 | min(end_cgap,len1-midi-1), |
| 387 | 0, |
| 388 | free_end, |
| 389 | 0, |
| 390 | te, |
| 391 | &tmp_head, |
| 392 | tail); |
| 393 | aux->next = tmp_head; |
| 394 | |
| 395 | if (*tail==NULL) |
| 396 | *tail = aux; |
| 397 | } |
| 398 | |
| 399 | return mincost; |
| 400 | } |
| 401 | |
| 402 | |
| 403 | |
| 404 | void |
| 405 | halignStart(char *s1, |
| 406 | char *s2, |
| 407 | H_Alignment_t *alignment) { |
| 408 | |
| 409 | int const offset1 = 0; // Sequence coordinates are base-based, starting from 0 |
| 410 | int const offset2 = 0; // but start from 1 in Liliana's code. |
| 411 | |
| 412 | if ((s1[0] == 0) || (s2[0] == 0)) |
| 413 | return; |
| 414 | |
| 415 | int len1 = strlen(s1); |
| 416 | int len2 = strlen(s2); |
| 417 | |
| 418 | int start_cgap = 0; |
| 419 | int end_cgap = 0; |
| 420 | int free_start = 0; |
| 421 | int free_end = 0; |
| 422 | int score = 0; |
| 423 | |
| 424 | EditScript *Script_head=NULL; |
| 425 | EditScript *Script_tail=NULL; |
| 426 | |
| 427 | int *CC = (int *)malloc(4 * (len2+1) * sizeof(int)); |
| 428 | assert(NULL != CC); |
| 429 | |
| 430 | score = diff(s1, |
| 431 | s2, |
| 432 | len1, |
| 433 | len2, |
| 434 | CC, |
| 435 | CC+1*(len2+1), |
| 436 | CC+2*(len2+1), |
| 437 | CC+3*(len2+1), |
| 438 | GAP_OPEN, |
| 439 | GAP_EXTEND, |
| 440 | MISMATCH, |
| 441 | start_cgap, |
| 442 | end_cgap, |
| 443 | free_start, |
| 444 | free_end, |
| 445 | GAP_OPEN, |
| 446 | GAP_OPEN, |
| 447 | &Script_head, |
| 448 | &Script_tail); |
| 449 | |
| 450 | free(CC); |
| 451 | |
| 452 | assert(NULL != Script_head); |
| 453 | assert(NULL != Script_tail); |
| 454 | |
| 455 | Script_tail->next = NULL; |
| 456 | |
| 457 | convertScriptToAlignment(Script_head, alignment); |
| 458 | |
| 459 | alignment->offset1 = offset1+START; // Convert from zero to one start sequence. |
| 460 | alignment->offset2 = offset2+START; // Convert from zero to one start sequence. |
| 461 | alignment->len1 = len1; |
| 462 | alignment->len2 = len2; |
| 463 | alignment->score = score; |
| 464 | alignment->first = 1; |
| 465 | } |
| 466 | |
| 467 | |
| 468 | int |
| 469 | iterateUngappedAlignSharpEnds(H_Alignment_t *aln, |
| 470 | int &bgn1, |
| 471 | int &bgn2, |
| 472 | int &len1, |
| 473 | int &len2, |
| 474 | int &nmatInSeg) { |
| 475 | |
| 476 | // Returns zero when exhasted. |
| 477 | // Returns one when the args are valid. |
| 478 | |
| 479 | static int *lastS, *endS; |
| 480 | static int i, j; |
| 481 | |
| 482 | nmatInSeg = 0; |
| 483 | |
| 484 | if(aln == NULL) return 0; // not valid output |
| 485 | |
| 486 | if(aln->first){ |
| 487 | aln->first = 0; |
| 488 | i = aln->offset1; |
| 489 | j = aln->offset2; |
| 490 | |
| 491 | lastS = aln->scriptAsArray + 1; |
| 492 | endS = aln->scriptAsArray + aln->scriptAsArray[0]; |
| 493 | } |
| 494 | |
| 495 | while (lastS <= endS) { |
| 496 | int b1, l1, b2, l2; |
| 497 | int nmat; |
| 498 | switch (*lastS) { |
| 499 | case SUB: |
| 500 | ++lastS; |
| 501 | i += *(lastS); |
| 502 | j += *(lastS); |
| 503 | ++lastS; |
| 504 | break; |
| 505 | |
| 506 | case MOV: |
| 507 | nmat = *(lastS+1); |
| 508 | nmatInSeg ++; |
| 509 | b1 = i; |
| 510 | b2 = j; |
| 511 | lastS++; |
| 512 | i += *lastS; |
| 513 | j += *lastS; |
| 514 | l1 = i-b1; |
| 515 | l2 = j-b2; |
| 516 | lastS++; |
| 517 | |
| 518 | while (lastS<=endS && (*lastS==SUB || *lastS==MOV)) { |
| 519 | nmat += (*lastS==MOV) ? *(lastS+1) : 0; |
| 520 | nmatInSeg += (*lastS==MOV) ? *(lastS+1) : 0; |
| 521 | lastS++; |
| 522 | i += *lastS; |
| 523 | j += *lastS; |
| 524 | if (*(lastS-1) == MOV) { |
| 525 | l1 = i-b1; |
| 526 | l2 = j-b2; |
| 527 | } |
| 528 | lastS++; |
| 529 | } |
| 530 | |
| 531 | bgn1=b1-START; |
| 532 | bgn2=b2-START; |
| 533 | len1=l1; |
| 534 | len2=l2; |
| 535 | |
| 536 | return 1; // valid output |
| 537 | break; |
| 538 | |
| 539 | case INS: |
| 540 | j += *(++lastS); |
| 541 | ++lastS; |
| 542 | break; |
| 543 | |
| 544 | case DEL: |
| 545 | i += *(++lastS); |
| 546 | ++lastS; |
| 547 | break; |
| 548 | |
| 549 | default : |
| 550 | fprintf(stderr, "Unrecognized opcode in alignment.\n"); |
| 551 | exit(1); |
| 552 | break; |
| 553 | } |
| 554 | } |
| 555 | return 0; // not valid output |
| 556 | } |