| 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 <stdlib.h> |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <assert.h> |
| 23 | |
| 24 | #include "GF_ALN_local.H" |
| 25 | |
| 26 | |
| 27 | typedef struct { |
| 28 | char *aseg; |
| 29 | char *bseg; |
| 30 | } PAIRALIGN; |
| 31 | |
| 32 | |
| 33 | |
| 34 | // safely copy a substring of a string into static space which is |
| 35 | // enlarged as needed |
| 36 | // |
| 37 | static |
| 38 | int |
| 39 | safe_substr(char **seg, |
| 40 | int *segspace, |
| 41 | const char *seq, |
| 42 | int beg, |
| 43 | int end){ |
| 44 | |
| 45 | if(*segspace<end-beg+1){ |
| 46 | *segspace=2*(end-beg)+1; |
| 47 | *seg=(char*)ckrealloc(*seg,sizeof(char)*(*segspace)); |
| 48 | } |
| 49 | strncpy(*seg,seq+beg,end-beg); |
| 50 | (*seg)[end-beg]='\0'; |
| 51 | return(strlen(*seg) - (end-beg) == 0); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | |
| 56 | /* construct a trace (with AS_ALN_OKNAlign) for the first local segment, |
| 57 | copying the result into its own static location (so that we |
| 58 | can call AS_ALN_OKNAlign on the second local segment without losing the |
| 59 | result */ |
| 60 | |
| 61 | static int *get_trace(const char *aseq, const char *bseq,Local_Overlap *O,int piece, |
| 62 | int which){ |
| 63 | static char *aseg=NULL, *bseg=NULL; |
| 64 | static int asegspace=0,bsegspace=0; |
| 65 | static int *segtrace[2], tracespace[2]={0,0}; |
| 66 | int alen = 0; |
| 67 | int blen = 0; |
| 68 | int spnt=0, *tmptrace=0L; |
| 69 | int segdiff=0; |
| 70 | int i=0; |
| 71 | int iret=0; |
| 72 | |
| 73 | assert(which==0||which==1); |
| 74 | |
| 75 | if(segtrace[which]==NULL){ |
| 76 | tracespace[which]=100; |
| 77 | segtrace[which]=(int*)ckalloc(sizeof(int)*tracespace[which]); |
| 78 | } |
| 79 | |
| 80 | iret = safe_substr(&aseg,&asegspace,aseq,O->chain[piece].piece.abpos, |
| 81 | O->chain[piece].piece.aepos); |
| 82 | if(iret == 0){ |
| 83 | fprintf(stderr,"EXCEPTION get_trace: For aseg: len(aseg)=%d, len(bseg)=%d, alen=%d, blen=%d\n", |
| 84 | (int)strlen(aseg),(int)strlen(bseg), alen,blen); |
| 85 | return NULL; |
| 86 | } |
| 87 | iret = safe_substr(&bseg,&bsegspace,bseq,O->chain[piece].piece.bbpos, |
| 88 | O->chain[piece].piece.bepos); |
| 89 | if(iret == 0){ |
| 90 | fprintf(stderr,"EXCEPTION get_trace: For bseg: len(aseg)=%d, len(bseg)=%d, alen=%d, blen=%d\n", |
| 91 | (int)strlen(aseg),(int)strlen(bseg), alen,blen); |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | alen=O->chain[piece].piece.aepos-O->chain[piece].piece.abpos; |
| 96 | blen=O->chain[piece].piece.bepos-O->chain[piece].piece.bbpos; |
| 97 | |
| 98 | //printf("get_trace: len(aseg)=%d, len(bseg)=%d, alen=%d, blen=%d\n", |
| 99 | // strlen(aseg),strlen(bseg), alen,blen); |
| 100 | |
| 101 | /* get trace for the segment from AS_ALN_OKNAlign */ |
| 102 | spnt=0; |
| 103 | /* subtract because Gene likes to point to one before string start */ |
| 104 | aseg--; |
| 105 | bseg--; |
| 106 | segdiff=(int)((O->chain[piece].piece.aepos-O->chain[piece].piece.abpos) |
| 107 | *(1.5*O->chain[piece].piece.error) +10); |
| 108 | |
| 109 | tmptrace=AS_ALN_OKNAlign(aseg,alen,bseg,blen,&spnt,segdiff); |
| 110 | |
| 111 | if(spnt!=0){ |
| 112 | if(spnt>0){ |
| 113 | O->chain[piece].agap+=spnt; |
| 114 | O->chain[piece].piece.abpos+=spnt; |
| 115 | i=0; |
| 116 | while(tmptrace[i]!=0){ |
| 117 | if(tmptrace[i]<0){ |
| 118 | tmptrace[i]+=spnt; |
| 119 | } |
| 120 | i++; |
| 121 | } |
| 122 | } else { |
| 123 | O->chain[piece].bgap+=-spnt; |
| 124 | O->chain[piece].piece.bbpos+=-spnt; |
| 125 | i=0; |
| 126 | while(tmptrace[i]!=0){ |
| 127 | if(tmptrace[i]>0){ |
| 128 | tmptrace[i]+=spnt; |
| 129 | } |
| 130 | i++; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | aseg++; /* restore because need to know where memory block is allocated, |
| 135 | and so that next time around strncpy will work right! */ |
| 136 | bseg++; |
| 137 | i=0; |
| 138 | while(tmptrace[i]!=0){ |
| 139 | segtrace[which][i]=tmptrace[i]; |
| 140 | i++; |
| 141 | if(i==tracespace[which]){ |
| 142 | tracespace[which]*=2; |
| 143 | segtrace[which]=(int*)ckrealloc(segtrace[which], |
| 144 | sizeof(int)*tracespace[which]); |
| 145 | } |
| 146 | } |
| 147 | segtrace[which][i]=0; |
| 148 | return(segtrace[which]); |
| 149 | |
| 150 | } |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | static void safe_add_to_seg(char **seg,int pos,char c,int *len){ |
| 159 | if(pos==*len){ |
| 160 | (*len)=(*len)*2; |
| 161 | *seg=(char*)ckrealloc(*seg,sizeof(char)*((*len)+1)); |
| 162 | } |
| 163 | (*seg)[pos]=c; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | |
| 168 | static PAIRALIGN *construct_pair_align(const char *aseq, |
| 169 | const char *bseq, |
| 170 | Local_Overlap *O, |
| 171 | int piece, |
| 172 | int *trace, |
| 173 | int which){ |
| 174 | static char *aseg[2]={NULL,NULL},*bseg[2]={NULL,NULL}; |
| 175 | static int alen[2]={0,0},blen[2]={0,0}; |
| 176 | static PAIRALIGN pairalign[2]; |
| 177 | |
| 178 | int starta,startb; |
| 179 | int offseta,offsetb; |
| 180 | int tpos,apos,bpos; |
| 181 | |
| 182 | |
| 183 | if(aseg[which]==NULL){ |
| 184 | alen[which]=blen[which]=1000; |
| 185 | aseg[which]=(char*)ckalloc((alen[which]+1)*sizeof(char)); |
| 186 | bseg[which]=(char*)ckalloc((blen[which]+1)*sizeof(char)); |
| 187 | } |
| 188 | starta=offseta=O->chain[piece].piece.abpos; |
| 189 | startb=offsetb=O->chain[piece].piece.bbpos; |
| 190 | tpos=0; |
| 191 | apos=0; |
| 192 | bpos=0; |
| 193 | |
| 194 | while(trace[tpos]!=0){ |
| 195 | if(trace[tpos]<0){ |
| 196 | for(;offseta<-trace[tpos]+starta-1;apos++,offseta++){ |
| 197 | safe_add_to_seg(&(aseg[which]),apos,aseq[offseta],&(alen[which])); |
| 198 | } |
| 199 | safe_add_to_seg(&(aseg[which]),apos,'-',&(alen[which])); |
| 200 | apos++; |
| 201 | } else { |
| 202 | for(;offsetb<trace[tpos]+startb-1;bpos++,offsetb++){ |
| 203 | safe_add_to_seg(&(bseg[which]),bpos,bseq[offsetb],&(blen[which])); |
| 204 | } |
| 205 | safe_add_to_seg(&(bseg[which]),bpos,'-',&(blen[which])); |
| 206 | bpos++; |
| 207 | } |
| 208 | tpos++; |
| 209 | } |
| 210 | for(;offseta<O->chain[piece].piece.aepos;apos++,offseta++){ |
| 211 | safe_add_to_seg(&(aseg[which]),apos,aseq[offseta],&(alen[which])); |
| 212 | } |
| 213 | for(;offsetb<O->chain[piece].piece.bepos;bpos++,offsetb++){ |
| 214 | safe_add_to_seg(&(bseg[which]),bpos,bseq[offsetb],&(blen[which])); |
| 215 | } |
| 216 | |
| 217 | assert(offseta==O->chain[piece].piece.aepos); |
| 218 | assert(offsetb==O->chain[piece].piece.bepos); |
| 219 | assert(offseta-O->chain[piece].piece.abpos+ |
| 220 | offsetb-O->chain[piece].piece.bbpos+ |
| 221 | tpos == |
| 222 | apos+bpos); |
| 223 | safe_add_to_seg(&(aseg[which]),apos,'\0',&(alen[which])); |
| 224 | safe_add_to_seg(&(bseg[which]),bpos,'\0',&(blen[which])); |
| 225 | |
| 226 | pairalign[which].aseg=aseg[which]; |
| 227 | pairalign[which].bseg=bseg[which]; |
| 228 | return(pairalign+which); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | static PAIRALIGN *get_align(const char *aseq,const char *bseq,Local_Overlap *O,int piece, |
| 233 | int which){ |
| 234 | int *trace=get_trace(aseq,bseq,O,piece,which); |
| 235 | if(trace == NULL) return NULL; |
| 236 | |
| 237 | PAIRALIGN *pairalign = construct_pair_align(aseq,bseq,O,piece,trace,which); |
| 238 | |
| 239 | return(pairalign); |
| 240 | } |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | void fix_overlapping_pieces(const char *aseq, const char *bseq, |
| 254 | Local_Overlap *O,int piece0, int piece1){ |
| 255 | |
| 256 | PAIRALIGN *pair_align1,*pair_align2; |
| 257 | |
| 258 | int offseta1,offsetb1,offseta2,offsetb2; |
| 259 | int bestend1a,bestend1b,bestbeg2a,bestbeg2b; |
| 260 | int into1,into2,bestinto2=0; |
| 261 | int errs1,errs2,minerrs; |
| 262 | |
| 263 | |
| 264 | assert(O->chain[piece0].piece.aepos>=O->chain[piece1].piece.abpos|| |
| 265 | O->chain[piece0].piece.bepos>=O->chain[piece1].piece.bbpos); |
| 266 | |
| 267 | assert(O->chain[piece0].piece.aepos<=O->chain[piece1].piece.aepos); |
| 268 | assert(O->chain[piece0].piece.bepos<=O->chain[piece1].piece.bepos); |
| 269 | |
| 270 | /* create alignments for the two segments */ |
| 271 | |
| 272 | pair_align1=get_align(aseq,bseq,O,piece0,0); |
| 273 | pair_align2=get_align(aseq,bseq,O,piece1,1); |
| 274 | |
| 275 | if(pair_align1 == NULL || pair_align2 == NULL){ |
| 276 | fprintf(stderr,"EXCEPTION pair_align1=%p pair_align2=%p\n", pair_align1, pair_align2); |
| 277 | fprintf(stderr,"EXCEPTION while fixing gap(%d,%d) (%d,%d)---(%d,%d) vs. gap(%d,%d) (%d,%d)---(%d,%d)\n", |
| 278 | O->chain[piece0].agap, O->chain[piece0].bgap, |
| 279 | O->chain[piece0].piece.abpos,O->chain[piece0].piece.bbpos, |
| 280 | O->chain[piece0].piece.aepos,O->chain[piece0].piece.bepos, |
| 281 | O->chain[piece1].agap, O->chain[piece1].bgap, |
| 282 | O->chain[piece1].piece.abpos,O->chain[piece1].piece.bbpos, |
| 283 | O->chain[piece1].piece.aepos,O->chain[piece1].piece.bepos); |
| 284 | } |
| 285 | |
| 286 | if(pair_align1 == NULL){ |
| 287 | fprintf(stderr,"EXCEPTION Fixing by pseudo-deleting piece0.\n"); |
| 288 | O->chain[piece0].agap=0; |
| 289 | O->chain[piece0].bgap=0; |
| 290 | if(piece0>0){ |
| 291 | O->chain[piece0].piece.abpos=O->chain[piece0-1].piece.aepos; |
| 292 | O->chain[piece0].piece.aepos=O->chain[piece0-1].piece.aepos; |
| 293 | O->chain[piece0].piece.bbpos=O->chain[piece0-1].piece.bepos; |
| 294 | O->chain[piece0].piece.bepos=O->chain[piece0-1].piece.bepos; |
| 295 | } else { |
| 296 | O->chain[piece0].piece.abpos=0; |
| 297 | O->chain[piece0].piece.aepos=0; |
| 298 | O->chain[piece0].piece.bbpos=0; |
| 299 | O->chain[piece0].piece.bepos=0; |
| 300 | } |
| 301 | O->chain[piece1].agap = O->chain[piece1].piece.abpos - O->chain[piece0].piece.aepos; |
| 302 | O->chain[piece1].bgap = O->chain[piece1].piece.bbpos - O->chain[piece0].piece.bepos; |
| 303 | return; |
| 304 | } |
| 305 | if(pair_align2 == NULL){ |
| 306 | fprintf(stderr,"EXCEPTION Fixing by pseudo-deleting piece1.\n"); |
| 307 | O->chain[piece1].agap=0; |
| 308 | O->chain[piece1].bgap=0; |
| 309 | O->chain[piece1].piece.abpos = O->chain[piece0].piece.aepos; |
| 310 | O->chain[piece1].piece.aepos = O->chain[piece0].piece.aepos; |
| 311 | O->chain[piece1].piece.bbpos = O->chain[piece0].piece.bepos; |
| 312 | O->chain[piece1].piece.bepos = O->chain[piece0].piece.bepos; |
| 313 | |
| 314 | if(piece1+1<=O->num_pieces){ |
| 315 | O->chain[piece1+1].agap=O->chain[piece1+1].piece.abpos - O->chain[piece0].piece.aepos; |
| 316 | O->chain[piece1+1].bgap=O->chain[piece1+1].piece.bbpos - O->chain[piece0].piece.bepos; |
| 317 | } |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | /* if, in finding the alignments, we shift the ends of the |
| 323 | alignment of the first segment to after the starts of the |
| 324 | alignment of the second segment, then the overlap has been |
| 325 | resolved, so we do nothing more */ |
| 326 | |
| 327 | if(!(O->chain[piece0].piece.aepos>=O->chain[piece1].piece.abpos|| |
| 328 | O->chain[piece0].piece.bepos>=O->chain[piece1].piece.bbpos)){ |
| 329 | |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | /* if, in finding the alignments, we shift the end of the |
| 334 | alignment of the second segment to before the start of the |
| 335 | alignment of the first segment, then the second is contained |
| 336 | in the first and we need to do something exceptional; |
| 337 | the most heuristic, but consistent with the practice elsewhere |
| 338 | in the local overlapper, is to pseudo-delete the second segment */ |
| 339 | |
| 340 | if(!(O->chain[piece0].piece.aepos<=O->chain[piece1].piece.aepos)|| |
| 341 | !(O->chain[piece0].piece.bepos<=O->chain[piece1].piece.bepos)){ |
| 342 | |
| 343 | O->chain[piece1].agap=0; |
| 344 | O->chain[piece1].bgap=0; |
| 345 | O->chain[piece1].piece.abpos=O->chain[piece0].piece.aepos; |
| 346 | O->chain[piece1].piece.aepos=O->chain[piece0].piece.aepos; |
| 347 | O->chain[piece1].piece.bbpos=O->chain[piece0].piece.bepos; |
| 348 | O->chain[piece1].piece.bepos=O->chain[piece0].piece.bepos; |
| 349 | |
| 350 | if(piece1+1<=O->num_pieces){ |
| 351 | O->chain[piece1+1].agap=O->chain[piece1+1].piece.abpos- |
| 352 | O->chain[piece0].piece.aepos; |
| 353 | O->chain[piece1+1].bgap=O->chain[piece1+1].piece.bbpos- |
| 354 | O->chain[piece0].piece.bepos; |
| 355 | } |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | |
| 360 | /* if, in finding the alignments, we shift the start of the |
| 361 | alignment of the first segment to after the start of the |
| 362 | alignment of the second segment, then the first is contained |
| 363 | in the second and we need to do something exceptional; |
| 364 | the most heuristic, but consistent with the practice elsewhere |
| 365 | in the local overlapper, is to pseudo-delete the first segment */ |
| 366 | |
| 367 | if(O->chain[piece0].piece.abpos>O->chain[piece1].piece.abpos|| |
| 368 | O->chain[piece0].piece.bbpos>O->chain[piece1].piece.bbpos){ |
| 369 | |
| 370 | O->chain[piece0].agap=0; |
| 371 | O->chain[piece0].bgap=0; |
| 372 | if(piece0>0){ |
| 373 | O->chain[piece0].piece.abpos=O->chain[piece0-1].piece.aepos; |
| 374 | O->chain[piece0].piece.aepos=O->chain[piece0-1].piece.aepos; |
| 375 | O->chain[piece0].piece.bbpos=O->chain[piece0-1].piece.bepos; |
| 376 | O->chain[piece0].piece.bepos=O->chain[piece0-1].piece.bepos; |
| 377 | } else { |
| 378 | O->chain[piece0].piece.abpos=0; |
| 379 | O->chain[piece0].piece.aepos=0; |
| 380 | O->chain[piece0].piece.bbpos=0; |
| 381 | O->chain[piece0].piece.bepos=0; |
| 382 | } |
| 383 | O->chain[piece1].agap=O->chain[piece1].piece.abpos- |
| 384 | O->chain[piece0].piece.aepos; |
| 385 | O->chain[piece1].bgap=O->chain[piece1].piece.bbpos- |
| 386 | O->chain[piece0].piece.bepos; |
| 387 | |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | /* find start of region for evaluation in first alignment */ |
| 392 | /* when done, |
| 393 | offseta1 and offsetb1 should be the offsets into the sequences |
| 394 | such that they correspond to a column in the alignment of the |
| 395 | first segment and that column contains the first possible |
| 396 | overlap with the second segment */ |
| 397 | |
| 398 | offseta1=O->chain[piece0].piece.abpos; |
| 399 | offsetb1=O->chain[piece0].piece.bbpos; |
| 400 | into1=0; |
| 401 | while(offseta1<O->chain[piece1].piece.abpos&& |
| 402 | offsetb1<O->chain[piece1].piece.bbpos){ |
| 403 | assert(pair_align1->aseg[into1]!='\0'); |
| 404 | assert(pair_align1->bseg[into1]!='\0'); |
| 405 | if(pair_align1->aseg[into1]!='-')offseta1++; |
| 406 | if(pair_align1->bseg[into1]!='-')offsetb1++; |
| 407 | into1++; |
| 408 | } |
| 409 | |
| 410 | |
| 411 | // if(pair_align1->aseg[into1-1]!='-')offseta1--; |
| 412 | // if(pair_align1->bseg[into1-1]!='-')offsetb1--; |
| 413 | |
| 414 | /* count mismatches in the second alignment */ |
| 415 | |
| 416 | into2=0; |
| 417 | errs2=0; |
| 418 | while(pair_align2->aseg[into2]!='\0'){ |
| 419 | assert(pair_align2->bseg[into2]!='\0'); |
| 420 | if(pair_align2->aseg[into2]!=pair_align2->bseg[into2]){ |
| 421 | errs2++; |
| 422 | } |
| 423 | into2++; |
| 424 | } |
| 425 | |
| 426 | /* initialize solution variables and auxiliaries */ |
| 427 | into2=0; |
| 428 | errs1 = (pair_align1->aseg[into1]!=pair_align1->bseg[into1] ? 1 : 0); |
| 429 | minerrs=errs2; |
| 430 | offseta2=O->chain[piece1].piece.abpos; |
| 431 | offsetb2=O->chain[piece1].piece.bbpos; |
| 432 | bestend1a=offseta1 - (pair_align1->aseg[into1-1]!='-' ? 1 : 0); |
| 433 | bestend1b=offsetb1 - (pair_align1->bseg[into1-1]!='-' ? 1 : 0); |
| 434 | bestbeg2a=offseta2; |
| 435 | bestbeg2b=offsetb2; |
| 436 | |
| 437 | /* while there is potential overlap still to come ... */ |
| 438 | |
| 439 | while(pair_align1->aseg[into1]!='\0'&&pair_align2->aseg[into2]!='\0'){ |
| 440 | |
| 441 | // Once, we did the following assert, assuming that the alignment |
| 442 | // of pair_align2 would not run out before pair_align1, since otherwise |
| 443 | // there would be a containment or some such that shouldn't happen; |
| 444 | // But, as luck would have it, alignment trimming quirks etc can |
| 445 | // make it happen. So ... no more assert |
| 446 | // |
| 447 | // assert(pair_align2->aseg[into2]!='\0'); |
| 448 | |
| 449 | /* while a position in the second segment is no greater than |
| 450 | the position in the first segment, |
| 451 | check for mismatch in second segment, |
| 452 | counting errors, |
| 453 | incrementing the sequence position counters as appropriate; |
| 454 | advance the second segment |
| 455 | position */ |
| 456 | |
| 457 | while(offseta1>=offseta2||offsetb1>=offsetb2){ |
| 458 | errs2-= (pair_align2->aseg[into2]!=pair_align2->bseg[into2] ? 1 : 0); |
| 459 | offseta2+= ( pair_align2->aseg[into2]!='-' ? 1 : 0 ); |
| 460 | offsetb2+= ( pair_align2->bseg[into2]!='-' ? 1 : 0 ); |
| 461 | into2++; |
| 462 | if(pair_align2->aseg[into2]=='\0'){ |
| 463 | break; |
| 464 | } |
| 465 | // assert(pair_align2->aseg[into2]!='\0'); |
| 466 | // assert(pair_align2->bseg[into2]!='\0'); |
| 467 | } |
| 468 | |
| 469 | if(errs1+errs2<=minerrs&& |
| 470 | pair_align1->aseg[into1]==pair_align1->bseg[into1]){ |
| 471 | minerrs=errs1+errs2; |
| 472 | bestend1a=offseta1 /* -(pair_align1->aseg[into1-1]!='-' ? 1 : 0 )*/; |
| 473 | bestend1b=offsetb1 /* -(pair_align1->bseg[into1-1]!='-' ? 1 : 0 )*/; |
| 474 | bestbeg2a=offseta2; |
| 475 | bestbeg2b=offsetb2; |
| 476 | bestinto2=into2; |
| 477 | } |
| 478 | |
| 479 | /* while the positions in the first segment are no greater than |
| 480 | the positions in the second segment, |
| 481 | check for mismatch in first segment, |
| 482 | counting errors, |
| 483 | incrementing the sequence position counters as appropriate; |
| 484 | advance the first segment |
| 485 | position */ |
| 486 | |
| 487 | while(offseta1<offseta2&&offsetb1<offsetb2){ |
| 488 | offseta1+= ( pair_align1->aseg[into1]!='-' ? 1 : 0 ); |
| 489 | offsetb1+= ( pair_align1->bseg[into1]!='-' ? 1 : 0 ); |
| 490 | into1++; |
| 491 | errs1+= (pair_align1->aseg[into1]!=pair_align1->bseg[into1] ? 1 : 0); |
| 492 | |
| 493 | if(pair_align1->aseg[into1]=='\0'){ |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | if(bestend1a<O->chain[piece0].piece.aepos) |
| 500 | bestend1a++; |
| 501 | if(bestend1b<O->chain[piece0].piece.bepos) |
| 502 | bestend1b++; |
| 503 | O->chain[piece0].piece.aepos=bestend1a; |
| 504 | O->chain[piece0].piece.bepos=bestend1b; |
| 505 | O->chain[piece1].piece.abpos=bestbeg2a; |
| 506 | O->chain[piece1].piece.bbpos=bestbeg2b; |
| 507 | O->chain[piece1].agap=bestbeg2a-bestend1a; |
| 508 | O->chain[piece1].bgap=bestbeg2b-bestend1b; |
| 509 | |
| 510 | assert(O->chain[piece1].agap>=0); |
| 511 | assert(O->chain[piece1].bgap>=0); |
| 512 | assert(O->chain[piece1].agap==0||O->chain[piece1].bgap==0); |
| 513 | |
| 514 | |
| 515 | // now, adjust the beginning of the second piece to skip any mismatches |
| 516 | while(pair_align2->aseg[bestinto2]!=pair_align2->bseg[bestinto2]&& |
| 517 | pair_align2->aseg[bestinto2]!='\0'){ |
| 518 | bestbeg2a += ( pair_align2->aseg[bestinto2]!='-' ? 1 : 0 ); |
| 519 | bestbeg2b += ( pair_align2->bseg[bestinto2]!='-' ? 1 : 0 ); |
| 520 | bestinto2++; |
| 521 | } |
| 522 | O->chain[piece1].piece.abpos=bestbeg2a; |
| 523 | O->chain[piece1].piece.bbpos=bestbeg2b; |
| 524 | O->chain[piece1].agap=bestbeg2a-bestend1a; |
| 525 | O->chain[piece1].bgap=bestbeg2b-bestend1b; |
| 526 | |
| 527 | assert(O->chain[piece1].piece.abpos<=O->chain[piece1].piece.aepos); |
| 528 | assert(O->chain[piece1].piece.bbpos<=O->chain[piece1].piece.bepos); |
| 529 | } |