Debian Codegraph @

atac-driver/chainer/localalign/GF_ALN_loverlapper.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 <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <assert.h>
23
24 #include "GF_ALN_local.H"
25
26 #define min(a,b) (a<b?a:b)
27 #define max(a,b) (a>b?a:b)
28
29
30 /* Handle Local_Overlap pieces (local segments) which overlap,
31 * by trimming them back until they abut,
32 * such that the number of mismatches is minimized
33 */
34 void fix_overlapping_pieces(const char *aseq,
35 const char *bseq,
36 Local_Overlap *O,
37 int piece0,
38 int piece1);
39
40
41
42
43 //maximum number of matching segments that can be pieced together
44 int MaxGaps= 3;
45
46 //maximum allowed mismatch at end of overlap
47 int MaxBegGap= 200;
48
49 //maximum allowed mismatch at end of overlap
50 int MaxEndGap= 200;
51
52 //biggest gap internal to overlap allowed
53 int MaxInteriorGap=400;
54
55 //whether to treat the beginning of the b fragment and
56 // the end of the a fragment as allowed to have more error
57 int asymmetricEnds=0;
58
59 //amount of mismatch at end of the overlap that can cause
60 // an overlap to be rejected
61 int MaxFreeFlap=20;
62
63
64 // set useSizeToOrderBlocks to 1 to get block mismatches resolved such that
65 // the smaller block comes first:
66 //
67 // .........AA------.........
68 // .........--BBBBBB........
69 //
70 // or set it to 0 to get resolution with the A block always first
71 int useSizeToOrderBlocks = 1;
72
73
74
75 /* Create a trace to be interpreted as with DP_Compare_AS, but based
76 on a Local_Overlap record. A Local_Segment within the overlap
77 will be aligned using AS_ALN_OKNAlign(), generating a subtrace. Subtraces,
78 with their indices appropriately adjusted, will be spliced together
79 by an encoding of the gaps between segments; for now, we'll simply insert
80 gaps as follows:
81
82 A "gap" with x bases in A and y bases in B will become a section
83 of the alignment x+y positions long, with the A fragment first
84 and the B fragment second (with '=' indicating an aligned match):
85
86 ====AAAAAAAAAA--------------======
87 ====----------BBBBBBBBBBBBBB======
88
89 Obviously, a more compact treatment is possible, but this makes clear
90 the presumptive blocks involved in the mismatch; also, Karin says that it
91 will make Consensus happy. One slight change
92
93 Assumptions: (do they matter?)
94 By the usual conventions, the ahang should be nonnegative, the
95 bhang negative only if the ahang is positive, and both sequences
96 should be in the forward orientation.
97
98 */
99
100 int *AS_Local_Trace(Local_Overlap *O, const char *aseq, const char *bseq){
101 static int *TraceBuffer=NULL;
102 int i,j,k,segdiff,*segtrace;
103 int lastgood=-1;
104 static int allocatedspace=0;
105 int tracespace=0;
106 static char *aseg=NULL,*bseg=NULL;
107
108 // Not computing traces generates slight differences. Why?
109 //
110 const int computeTraceFlag = 0;
111
112 static int aseglen=0,bseglen=0;
113 int abeg=0,bbeg=0; /* begining of segment; overloaded */
114 int tracep=0; /* index into TraceBuffer */
115 int spnt=0; /* to pass to AS_ALN_OKNAlign */
116
117
118 {
119 int n=O->num_pieces;
120 assert(O->num_pieces>0);
121 O->chain[n].piece.abpos=O->chain[n].agap+O->chain[n-1].piece.aepos;
122 O->chain[n].piece.bbpos=O->chain[n].bgap+O->chain[n-1].piece.bepos;
123 }
124
125 if(computeTraceFlag){
126 /*Estimate length required to store trace*/
127 tracespace=0;
128 tracespace+=abs(O->begpos)+abs(O->endpos);
129 for(i=0;i<=O->num_pieces;i++){
130 tracespace+=max(O->chain[i].agap,1);
131 tracespace+=max(O->chain[i].bgap,1);
132 tracespace+=(int)((O->chain[i].piece.aepos
133 -O->chain[i].piece.abpos)
134 *1.5*O->chain[i].piece.error);
135 tracespace+=1000;
136 }
137
138 /*(Re)allocate space for the trace as necessary;
139 Note that this is persistent storage so ...
140 ... it doesn't need to get allocated on every call
141 ... it shouldn't get freed
142 ... results stored here need to be copied elsewhere if they
143 are to be saved
144 */
145 if(allocatedspace<tracespace){
146 allocatedspace=2*tracespace;
147 if(TraceBuffer==NULL){
148 TraceBuffer=(int*)ckalloc(sizeof(int)*allocatedspace);
149 } else {
150 TraceBuffer=(int*)ckrealloc(TraceBuffer,sizeof(int)*allocatedspace);
151 }
152 }
153 } // computeTraceFlag
154
155 /* for each Local_Overlap chain[i].piece,
156 need to handle the gap at the beginning and
157 (for all but the final piece) the associated aligned segment */
158 for(i=0;i<=O->num_pieces;i++){
159
160 /* if conditions indicate the segment was deleted in previous loop,
161 skip! */
162 if(O->chain[i].agap==0 &&
163 O->chain[i].bgap==0 &&
164 O->chain[i].piece.abpos==O->chain[i].piece.aepos &&
165 O->chain[i].piece.bbpos==O->chain[i].piece.bepos){
166 continue;
167 }
168
169 /* guesstimate the required number of diagonals/edits to consider to
170 get optimal alignment */
171 segdiff=1+(int)((O->chain[i].piece.aepos
172 -O->chain[i].piece.abpos)
173 *1.5*O->chain[i].piece.error);
174
175
176
177 /* Building an alignment/trace under the usual assumptions does not allow
178 a given position in one sequence to simultaneously align to two or more
179 positions in the other sequence. However, the Find_Local_Overlap()
180 routine can chain together local alignment segments that overlap.
181
182 In order to make the local overlaps compatible with everything else
183 we do, we need to trim back the overlaps.
184
185 Since we will "output" this segment at the end of the loop,
186 we need to fix its overlap with the following segment in this
187 cycle through the loop
188 */
189
190 k=i+1;
191 while(k<O->num_pieces){
192
193 /* if conditions indicate the segment was deleted previously, skip! */
194 if(O->chain[k].agap==0 &&
195 O->chain[k].bgap==0 &&
196 O->chain[k].piece.abpos==O->chain[k].piece.aepos &&
197 O->chain[k].piece.bbpos==O->chain[k].piece.bepos){
198 k++;
199 continue;
200 }
201
202 if(O->chain[k].piece.abpos<O->chain[i].piece.aepos||
203 O->chain[k].piece.bbpos<O->chain[i].piece.bepos){
204
205 /* handle possibility of the first segment being
206 contained within the second;originally simply asserted
207 against this; now, try to handle by deleting first segment */
208
209 if(O->chain[i].piece.abpos>O->chain[k].piece.abpos||
210 O->chain[i].piece.bbpos>O->chain[k].piece.bbpos){
211
212
213 O->chain[i].agap=0;
214 O->chain[i].bgap=0;
215 if(lastgood>=0){
216 O->chain[i].piece.abpos=O->chain[lastgood].piece.aepos;
217 O->chain[i].piece.aepos=O->chain[lastgood].piece.aepos;
218 O->chain[i].piece.bbpos=O->chain[lastgood].piece.bepos;
219 O->chain[i].piece.bepos=O->chain[lastgood].piece.bepos;
220 } else {
221 O->chain[i].piece.abpos=0;
222 O->chain[i].piece.aepos=0;
223 O->chain[i].piece.bbpos=0;
224 O->chain[i].piece.bepos=0;
225 }
226 O->chain[k].agap=O->chain[k].piece.abpos-
227 O->chain[i].piece.aepos;
228 O->chain[k].bgap=O->chain[k].piece.bbpos-
229 O->chain[i].piece.bepos;
230 if(lastgood<0){
231 //printf("Shrinking gaps for segment %d\n",k);
232 O->chain[k].agap--;
233 O->chain[k].bgap--;
234 }
235
236 } else /* otherwise, check for 2nd piece contained within first */
237 if(O->chain[i].piece.aepos>O->chain[k].piece.aepos||
238 O->chain[i].piece.bepos>O->chain[k].piece.bepos){
239
240 /* if the next piece is completely within current piece,
241 effectively remove it */
242
243 O->chain[k].agap = 0;
244 O->chain[k].bgap = 0;
245 O->chain[k].piece.abpos=O->chain[i].piece.aepos;
246 O->chain[k].piece.aepos=O->chain[i].piece.aepos;
247 O->chain[k].piece.bbpos=O->chain[i].piece.bepos;
248 O->chain[k].piece.bepos=O->chain[i].piece.bepos;
249 if(k+1<=O->num_pieces){
250 int l;
251 l=k-1;
252 while(O->chain[l].agap==0 &&
253 O->chain[l].bgap==0 &&
254 O->chain[l].piece.abpos==O->chain[l].piece.aepos &&
255 O->chain[l].piece.bbpos==O->chain[l].piece.bepos){
256 l--;
257 assert(l>=0);
258 }
259
260
261 O->chain[k+1].agap=O->chain[k+1].piece.abpos-
262 O->chain[l].piece.aepos;
263 O->chain[k+1].bgap=O->chain[k+1].piece.bbpos-
264 O->chain[l].piece.bepos;
265 }
266
267 /* else, fix the overlap */
268 } else {
269
270 fix_overlapping_pieces(aseq,
271 bseq,
272 O,i,k);
273
274
275 // if the second piece disappeared
276 if(O->chain[k].piece.abpos==O->chain[k].piece.aepos||
277 O->chain[k].piece.bbpos==O->chain[k].piece.bepos){
278
279 O->chain[k].agap = 0;
280 O->chain[k].bgap = 0;
281 O->chain[k].piece.abpos=O->chain[i].piece.aepos;
282 O->chain[k].piece.aepos=O->chain[i].piece.aepos;
283 O->chain[k].piece.bbpos=O->chain[i].piece.bepos;
284 O->chain[k].piece.bepos=O->chain[i].piece.bepos;
285 if(k+1<=O->num_pieces){
286 int l;
287 l=k-1;
288 while(O->chain[l].agap==0 &&
289 O->chain[l].bgap==0 &&
290 O->chain[l].piece.abpos==O->chain[l].piece.aepos &&
291 O->chain[l].piece.bbpos==O->chain[l].piece.bepos){
292 l--;
293 assert(l>=0);
294 }
295
296 O->chain[k+1].agap=O->chain[k+1].piece.abpos-
297 O->chain[l].piece.aepos;
298 O->chain[k+1].bgap=O->chain[k+1].piece.bbpos-
299 O->chain[l].piece.bepos;
300 }
301 } else {
302 // if the first piece disappeared
303 if (O->chain[i].piece.abpos==O->chain[i].piece.aepos||
304 O->chain[i].piece.bbpos==O->chain[i].piece.bepos){
305
306 O->chain[i].agap=0;
307 O->chain[i].bgap=0;
308 if(lastgood>=0){
309 O->chain[i].piece.abpos=O->chain[lastgood].piece.aepos;
310 O->chain[i].piece.aepos=O->chain[lastgood].piece.aepos;
311 O->chain[i].piece.bbpos=O->chain[lastgood].piece.bepos;
312 O->chain[i].piece.bepos=O->chain[lastgood].piece.bepos;
313 } else {
314 O->chain[i].piece.abpos=0;
315 O->chain[i].piece.aepos=0;
316 O->chain[i].piece.bbpos=0;
317 O->chain[i].piece.bepos=0;
318 }
319 O->chain[k].agap=O->chain[k].piece.abpos-
320 O->chain[i].piece.aepos;
321 O->chain[k].bgap=O->chain[k].piece.bbpos-
322 O->chain[i].piece.bepos;
323 if(lastgood<0){
324 //printf("Shrinking gaps for segment %d\n",k);
325 O->chain[k].agap--;
326 O->chain[k].bgap--;
327 }
328 }
329 }
330 }
331 }
332
333 k++;
334 }
335
336 /* if conditions indicate the segment was deleted previously, skip! */
337 if(O->chain[i].agap==0 &&
338 O->chain[i].bgap==0 &&
339 O->chain[i].piece.abpos==O->chain[i].piece.aepos &&
340 O->chain[i].piece.bbpos==O->chain[i].piece.bepos){
341 continue;
342 }
343
344 /* set up positions before which gaps are inserted to handle
345 the gap portion of a chain piece */
346
347 /* put gaps before beginning of aligned piece (but after the portion
348 of aseq in the gap); location is relative to the beginning of the
349 alignment (i.e., ignores ahang worth of positions) */
350
351 if(i!=O->num_pieces){
352 abeg=O->chain[i].piece.abpos;
353 } else {
354 assert(lastgood>=0&&lastgood<O->num_pieces);
355 abeg=O->chain[lastgood].piece.aepos+
356 O->chain[i].agap;
357 }
358
359 /*handle boundary case to prevent gaps preceding the b sequence*/
360 if((i==0||lastgood<0)&&O->chain[i].bgap>0){
361 assert(O->chain[i].agap>=0);
362
363 if(O->begpos>=0){
364 O->begpos=O->chain[i].piece.abpos-1;
365 assert(O->begpos>=0);
366 O->chain[i].agap=0;
367
368 // Instead of asserting, an ifdef previously printed stuff
369 // out and continued happily along.
370 //
371 assert( ( i==0&& O->chain[i].bgap==O->chain[i].piece.bbpos-1)
372 ||( i>0&&lastgood<0&&O->chain[i].bgap==O->chain[i].piece.bbpos-1)) ;
373
374 if(lastgood<0){
375 O->chain[i].bgap=O->chain[i].piece.bbpos-1;
376 }
377 } else {
378 if(i==0){
379 O->begpos-=O->chain[i].bgap;
380 } else{
381 O->begpos=-O->chain[i].bgap;
382 }
383 O->chain[i].bgap=0;
384 }
385
386 }
387
388 /* now prevent gaps at end of A sequence */
389 // if(i==O->num_pieces&&O->endpos>=0){
390 // O->endpos+=O->chain[i].bgap;
391 // O->chain[i].bgap=0;
392 // }
393
394 /* now make sure that end mismatches are treated by tucking
395 the shorter tail into a gap before the longer tail,
396 or, ifdef FORCEPOSITIVEBHANG, by tucking the A tail into
397 a gap before the B tail */
398 if(i==O->num_pieces){
399 if(O->endpos>=0){
400 O->endpos+=O->chain[i].bgap;
401 O->chain[i].bgap=0;
402 }else {
403 O->endpos-=O->chain[i].agap;
404 abeg-=O->chain[i].agap;
405 O->chain[i].agap=0;
406 }
407 }
408
409 /* put gaps before the portion of bseq in the gap; for the first
410 piece, this means before position 0 */
411 if(i==0 || lastgood<0){
412 bbeg = 1-min(O->begpos,0);
413 } else {
414 assert(lastgood<O->num_pieces);
415 bbeg = O->chain[lastgood].piece.bepos;
416 }
417
418 /* now insert the right number of gaps! */
419
420 if(i==O->num_pieces){
421
422 if(O->endpos<0){
423 O->chain[i].agap+=-O->endpos;
424 O->endpos=0;
425 } else {
426 O->chain[i].bgap+=O->endpos;
427 O->endpos=0;
428 }
429
430 if(O->chain[i].agap <= O->chain[i].bgap){
431 O->endpos=O->chain[i].bgap;
432 O->chain[i].bgap=0;
433 }else{
434 O->endpos=-O->chain[i].agap;
435 O->chain[i].agap=0;
436 }
437 }
438
439
440 if (computeTraceFlag) {
441 if(O->chain[i].agap <= O->chain[i].bgap || ! useSizeToOrderBlocks ){
442 /* start by putting len(agap) gaps before the chunk of B in the gap */
443 for(j=0; j<O->chain[i].agap ;j++)
444 TraceBuffer[tracep++]=bbeg;
445
446 /* then put len(bgap) gaps before the chunk of A in the gap */
447 for(j=0; j<O->chain[i].bgap ;j++)
448 TraceBuffer[tracep++]=-abeg;
449 } else { // if the bgap is smaller,
450 abeg-=O->chain[i].agap;
451 bbeg+=O->chain[i].bgap;
452
453 /* start by putting len(bgap) gaps before the chunk of A in the gap */
454 for(j=0;j<O->chain[i].bgap ;j++)
455 TraceBuffer[tracep++]=-abeg;
456
457 /* then put len(agap) gaps before the chunk of B in the gap */
458 for(j=0;j<O->chain[i].agap ;j++)
459 TraceBuffer[tracep++]=bbeg;
460 }
461 } else {
462 // Not computing traces!
463 if(O->chain[i].agap <= O->chain[i].bgap || ! useSizeToOrderBlocks ){
464 } else {
465 abeg-=O->chain[i].agap;
466 bbeg+=O->chain[i].bgap;
467 }
468 }
469
470
471 ///////////////////////////////////////
472
473 /* if last piece, there is no aligned segment */
474
475 if(i==O->num_pieces)break;
476
477 /* set bbeg to beginning of aligned segment for piece */
478
479 abeg=O->chain[i].piece.abpos;
480 bbeg=O->chain[i].piece.bbpos;
481
482 /* set lengths of segments */
483
484 int alen=O->chain[i].piece.aepos-abeg; /* check +1?? */
485 int blen=O->chain[i].piece.bepos-bbeg; /* check +1?? */
486
487 /* create strings for just the parts of the sequences in the
488 aligned segment */
489
490 /* make sure there is (persistant) space for the strings */
491 if(aseglen<alen+1){
492 aseglen=2*(alen+1);
493 if(aseg==NULL){
494 aseg=(char*)ckalloc(sizeof(char)*aseglen);
495 } else {
496 aseg=(char*)ckrealloc(aseg,sizeof(char)*aseglen);
497 }
498 }
499 if(bseglen<blen+1){
500 bseglen=2*(blen+1);
501 if(bseg==NULL){
502 bseg=(char*)ckalloc(sizeof(char)*bseglen);
503 } else {
504 bseg=(char*)ckrealloc(bseg,sizeof(char)*bseglen);
505 }
506 }
507
508 /* CMM we do not need the trace computed */
509 if (computeTraceFlag) {
510
511 /* copy the segments */
512
513 strncpy(aseg, aseq+abeg, alen);
514 strncpy(bseg, bseq+bbeg, blen);
515
516 aseg[alen] = 0;
517 bseg[blen] = 0;
518
519 if (((int)strlen(bseg) != blen) ||
520 ((int)strlen(aseg) != alen)) {
521 fprintf(stderr,"EXCEPTION strlen(aseg)=%d alen=%d abeg=%d\n", (int)strlen(aseg), alen, abeg);
522 fprintf(stderr,"EXCEPTION strlen(bseg)=%d blen=%d bbeg=%d\n", (int)strlen(bseg), blen, bbeg);
523
524 fprintf(stderr,"EXCEPTION aseg=<%s>\n", aseg);
525 fprintf(stderr,"EXCEPTION bseg=<%s>\n", bseg);
526
527 fprintf(stderr,"EXCEPTION aseq=<%s>\n", aseq + 1);
528 fprintf(stderr,"EXCEPTION bseq=<%s>\n", bseq + 1);
529
530 return NULL; // Return an exceptional value.
531 }
532
533 /* guesstimate the required number of diagonals/edits to consider to
534 get optimal alignment */
535 segdiff = 1 + (int)((O->chain[i].piece.aepos - O->chain[i].piece.abpos) * 1.5 * O->chain[i].piece.error);
536
537
538 /* get trace for the segment from AS_ALN_OKNAlign */
539 spnt=0;
540 /* subtract from aseg, bseg because Gene likes to index from 1, not 0 */
541 segtrace=AS_ALN_OKNAlign(aseg-1,alen,bseg-1,blen,&spnt,segdiff);
542 // This adjusts the beginning coordinates so that segment is
543 // consistent with the back-trace.
544
545 if(spnt>0){
546 O->chain[i].agap+=spnt;
547 O->chain[i].piece.abpos+=spnt;
548 } else {
549 O->chain[i].bgap-=spnt;
550 O->chain[i].piece.bbpos-=spnt;
551 }
552
553 /* get trace for the segment from AS_ALN_OKNAffine */
554 /* Seems like it should be a good idea, but doesn't work as well
555 as we might expect! */
556 //bpnt=0;
557 //epnt=0;
558 //segtrace=AS_ALN_OKNAffine(aseg,alen,bseg,blen,&bpnt,&epnt,segdiff);
559
560 assert(segtrace!=NULL);
561
562 /* Now copy the segment trace into master trace, adjusting positions */
563 j=0;
564
565 if(spnt<0){
566 for(int ctr=0;ctr<abs(spnt);ctr++)
567 TraceBuffer[tracep++]=-abeg;
568 } else {
569 for(int ctr=0;ctr<spnt;ctr++)
570 TraceBuffer[tracep++]=bbeg;
571 }
572
573 while(segtrace[j]!=0){
574 if(segtrace[j]<0){
575 TraceBuffer[tracep++]=-abeg+segtrace[j++]+1 /* -max(0,spnt) ?? */;
576 } else {
577 TraceBuffer[tracep++]=bbeg+segtrace[j++]-1 /* +max(0,-spnt) ?? */;
578 }
579 }
580 } // computeTraceFlag
581
582 /* set lastgood to this segment */
583
584 lastgood=i;
585
586 /* and back to the top of the loop for another overlap piece */
587 }
588
589 /* terminate the trace */
590 if(TraceBuffer != NULL) {
591 TraceBuffer[tracep]=0;
592
593 if (tracep >= allocatedspace)
594 fprintf(stderr,"ERROR memory is already corrupted in %s at %d.\n", __FILE__, __LINE__);
595 assert(tracep < allocatedspace);
596 }
597
598 return(TraceBuffer);
599 }