Debian Codegraph @

atac-driver/uniqueFilter/uniqueFilter.C

raw symbols SCIP
1 // This file is part of A2Amapper.
2 // Copyright (c) 2005 J. Craig Venter Institute
3 // Author: Brian Walenz
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 <time.h>
22
23 #include "util++.H"
24 #include "atac.H"
25
26 // Kaz Kylheku <[email protected]> library.
27 #include "kazlib/dict.h"
28 #include "kazlib/except.h"
29 #include "kazlib/hash.h"
30 #include "kazlib/list.h"
31 #include "kazlib/sfx.h"
32
33 // Filters out matches that have non-unique pieces. Does not discard
34 // the whole match, but just trims out the non-unique section.
35 //
36 // Original implementation in Python by Clark Mobarry:
37 //
38 // sort the matches in X
39 // apply the mask to the X
40 // sort the matchs in Y
41 // apply the mask to the Y
42 // output the matches
43 //
44 // if we keep the coverage intervals in core, we get around sorting
45 // the matches. how big can they be -- especially if we only keep
46 // things with > 1 coverage! But, we also can't use an elegant
47 // algorithm for trimming/splitting.
48
49
50 // We can abuse this to subtract matches from other matches. The
51 // operation done for removing non-unique is to first find any
52 // overlapping intervals in the set, then subract those from the
53 // input. If we instead just find all intervals in a set of matches,
54 // and subtract those from the input, we get subtraction.
55
56
57 // Reads the input, builds an interval list of the regions
58 // that have coverage > 1.
59 //
60 struct coverage1_s {
61 uint32 axis;
62 uint32 position;
63 int increment;
64 };
65
66 struct coverage2_s {
67 uint32 axis;
68 uint32 beg;
69 uint32 end;
70 uint32 coverage;
71 };
72
73 struct match_s {
74 uint32 iid1, pos1, len1, ori1;
75 uint32 iid2, pos2, len2, ori2;
76 };
77
78 int
79 sortCoverage1(const void *a, const void *b) {
80 const coverage1_s *A = *((const coverage1_s * const *)a);
81 const coverage1_s *B = *((const coverage1_s * const *)b);
82
83 if (A->axis < B->axis) return(-1);
84 if (A->axis > B->axis) return(1);
85 if (A->position < B->position) return(-1);
86 if (A->position > B->position) return(1);
87 if (A->increment > B->increment) return(-1);
88 if (A->increment < B->increment) return(1);
89 return(0);
90 }
91
92
93 // Not a complete comparison, but we only use this for an interval
94 // list.
95 //
96 int
97 sortCoverage2(const void *a, const void *b) {
98 const coverage2_s *A = *((const coverage2_s * const *)a);
99 const coverage2_s *B = *((const coverage2_s * const *)b);
100
101 if (A->axis < B->axis) return(-1);
102 if (A->axis > B->axis) return(1);
103 if (A->beg < B->beg) return(-1);
104 if (A->beg > B->beg) return(1);
105 return(0);
106 }
107
108
109 // Same as sortCoverage2, but the array being sorted is not
110 // an array of pointers.
111 //
112 int
113 sortCoverage3(const void *a, const void *b) {
114 const coverage2_s *A = (const coverage2_s *)a;
115 const coverage2_s *B = (const coverage2_s *)b;
116
117 if (A->axis < B->axis) return(-1);
118 if (A->axis > B->axis) return(1);
119 if (A->beg < B->beg) return(-1);
120 if (A->beg > B->beg) return(1);
121 return(0);
122 }
123
124
125
126
127 // An interval list, searchable
128 //
129 class coverageIntervals {
130 public:
131 dict_t *_il;
132 dict_load_t _load;
133 public:
134 coverageIntervals() {
135 _il = dict_create(DICTCOUNT_T_MAX, sortCoverage2);
136 };
137
138 ~coverageIntervals() {
139 dict_free(_il);
140 pfree();
141 };
142
143
144 // We want to return the first node that is before our thing. Our comparison
145 // tests the start position. If there is no first node before our thing,
146 // return the next node.
147 //
148 dnode_t *lookup(void *thing) {
149 dnode_t *it = dict_upper_bound(_il, thing);
150 if (it == 0L)
151 it = dict_lower_bound(_il, thing);
152 return(it);
153 };
154
155
156 void addInterval(int axis, int beg, int end, int coverage) {
157 dnode_t *node = (dnode_t *)palloc(sizeof(dnode_t));
158 coverage2_s *cov = (coverage2_s *)palloc(sizeof(coverage2_s));
159
160 cov->axis = axis;
161 cov->beg = beg;
162 cov->end = end;
163 cov->coverage = coverage;
164
165 // initialize the node with the value
166 dnode_init(node, 0L);
167
168 // insert the node into the tree using the key
169 dict_insert(_il, node, (void *)cov);
170 };
171
172 void beginLoad(void) {
173 dict_load_begin(&_load, _il);
174 };
175 void endLoad(void) {
176 dict_load_end(&_load);
177 };
178 void loadInterval(int axis, int beg, int end, int coverage) {
179 dnode_t *node = (dnode_t *)palloc(sizeof(dnode_t));
180 coverage2_s *cov = (coverage2_s *)palloc(sizeof(coverage2_s));
181
182 cov->axis = axis;
183 cov->beg = beg;
184 cov->end = end;
185 cov->coverage = coverage;
186
187 // initialize the node with the value
188 dnode_init(node, 0L);
189
190 // insert the node into the tree using the key
191 dict_load_next(&_load, node, (void *)cov);
192 };
193 };
194
195
196
197
198 void
199 offsetsToCoverage(uint32 minCov, bigQueue *I, coverageIntervals *L) {
200 uint32 axis = ~uint32ZERO;
201 uint32 position = ~uint32ZERO;
202 uint32 coverage = 0;
203 uint64 covered = 0;
204
205
206 L->beginLoad();
207 speedCounter D(" %8.0f matches treed (%8.2f matches/sec)\r", 1, 511, false);
208 while (I->next()) {
209 coverage1_s *cov1 = (coverage1_s *)I->get();
210
211 if ((cov1->axis != axis) && (coverage != 0))
212 fprintf(stderr, "Sorting error -- have coverage at the end of an axis.\n"), exit(1);
213
214 int length = cov1->position - position;
215
216 if ((coverage >= minCov) && (length > 0)) {
217 D.tick();
218 L->loadInterval(axis, position, position+length, coverage);
219 covered += length;
220 }
221
222 // Occasionally, we get stung by insisting to use unsigned
223 // numbers. This is one of them.
224 //
225 if ((coverage == 0) && (cov1->increment == -1))
226 fprintf(stderr, "Sorting error -- have negative coverage (axis="uint32FMT" position="uint32FMT")!\n",
227 axis, position), exit(1);
228
229 coverage += cov1->increment;
230 axis = cov1->axis;
231 position = cov1->position;
232 }
233 D.finish();
234 L->endLoad();
235
236 fprintf(stderr, "offsetsToCoverage()-- Found "uint64FMT" bases at coverage "uint32FMT" or greater.\n",
237 covered, minCov);
238 }
239
240
241
242
243
244 void
245 findCoverageIntervals(char const *fileName,
246 uint32 minCov,
247 coverageIntervals *Fint,
248 coverageIntervals *Rint) {
249 bigQueue F(sortCoverage1, 0L, 0L, 0L, sizeof(coverage1_s), 128, 0L);
250 bigQueue R(sortCoverage1, 0L, 0L, 0L, sizeof(coverage1_s), 128, 0L);
251
252 //
253 // Read the input file, building a bigQueue of the interval offsets
254 //
255
256
257 atacFileStream AF(fileName);
258 atacMatch *m = AF.nextMatch('u');
259
260 while (m) {
261 coverage1_s *fbeg = (coverage1_s *)malloc(sizeof(coverage1_s));
262 coverage1_s *fend = (coverage1_s *)malloc(sizeof(coverage1_s));
263 coverage1_s *rbeg = (coverage1_s *)malloc(sizeof(coverage1_s));
264 coverage1_s *rend = (coverage1_s *)malloc(sizeof(coverage1_s));
265
266 fbeg->axis = m->iid1;
267 fbeg->position = m->pos1;
268 fbeg->increment = 1;
269
270 fend->axis = m->iid1;
271 fend->position = m->pos1 + m->len1;
272 fend->increment = -1;
273
274 rbeg->axis = m->iid2;
275 rbeg->position = m->pos2;
276 rbeg->increment = 1;
277
278 rend->axis = m->iid2;
279 rend->position = m->pos2 + m->len2;
280 rend->increment = -1;
281
282 F.add(fbeg);
283 F.add(fend);
284 R.add(rbeg);
285 R.add(rend);
286
287 m = AF.nextMatch('u');
288 }
289
290 // Sort each bigQueue
291 //
292 F.sort();
293 R.sort();
294
295 // Convert the interval offsets into a coverage interval list
296 //
297 offsetsToCoverage(minCov, &F, Fint);
298 offsetsToCoverage(minCov, &R, Rint);
299 }
300
301
302
303
304
305
306
307
308
309
310 void
311 intersectTest(match_s *matches,
312 uint32 matchesLen,
313 coverageIntervals *Fint,
314 coverageIntervals *Rint,
315 uint32 matchNumber) {
316 bool errors = false;
317
318 for (uint32 i=0; i<matchesLen; i++) {
319 coverage2_s thing;
320
321 // Query the tree for the first interval intersecting iid1
322 //
323 thing.axis = matches[i].iid1;
324 thing.beg = matches[i].pos1;
325 thing.end = matches[i].pos1 + matches[i].len1;
326 thing.coverage = 0;
327 dnode_t *node1 = Fint->lookup(&thing);
328
329 thing.axis = matches[i].iid2;
330 thing.beg = matches[i].pos2;
331 thing.end = matches[i].pos2 + matches[i].len2;
332 thing.coverage = 0;
333 dnode_t *node2 = Rint->lookup(&thing);
334
335
336 // Keep iterating until the node returned from the tree
337 // is empty, or it is after our region
338 //
339 while (node1 && node2) {
340 const coverage2_s *key1 = 0L, *key2 = 0L;
341
342 bool isect1=false, before1=false;
343 bool isect2=false, before2=false;
344
345 if (node1) {
346 key1 = (const coverage2_s *)dnode_getkey(node1);
347
348 isect1 = ((key1->axis == matches[i].iid1) &&
349 (matches[i].pos1 < key1->end) &&
350 (key1->beg < matches[i].pos1 + matches[i].len1));
351
352 before1 = ((key1->axis < matches[i].iid1) ||
353 ((key1->axis == matches[i].iid1) && (key1->beg < matches[i].pos1 + matches[i].len1)));
354 }
355
356
357 if (node2) {
358 key2 = (const coverage2_s *)dnode_getkey(node2);
359
360 isect2 = ((key2->axis == matches[i].iid2) &&
361 (matches[i].pos2 < key2->end) &&
362 (key2->beg < matches[i].pos2 + matches[i].len2));
363
364 before2 = ((key2->axis < matches[i].iid2) ||
365 ((key2->axis == matches[i].iid2) && (key2->beg < matches[i].pos2 + matches[i].len2)));
366 }
367
368
369 if (isect1) {
370 fprintf(stderr, "Got fwd intersection on i="uint32FMT" matchNumber="uint32FMT"\n", i, matchNumber);
371 fprintf(stdout, "--"uint32FMT" "uint32FMT" 1 "uint32FMT" "uint32FMT" %d\n",
372 matches[i].pos1, matches[i].pos1 + matches[i].len1,
373 matches[i].pos2, matches[i].pos2 + matches[i].len2, matches[i].ori2 ? 1 : -1);
374 fprintf(stdout, "--key1 beg="uint32FMT" end="uint32FMT"\n",
375 key1->beg, key1->end);
376 errors = true;
377 }
378 if (isect2) {
379 fprintf(stderr, "Got rev intersection on i="uint32FMT" matchNumber="uint32FMT"\n", i, matchNumber);
380 fprintf(stdout, "--"uint32FMT" "uint32FMT" 1 "uint32FMT" "uint32FMT" %d\n",
381 matches[i].pos1, matches[i].pos1 + matches[i].len1,
382 matches[i].pos2, matches[i].pos2 + matches[i].len2, matches[i].ori2 ? 1 : -1);
383 fprintf(stdout, "--key2 beg="uint32FMT" end="uint32FMT"\n",
384 key2->beg, key2->end);
385 errors = true;
386 }
387
388
389 // If we intersected or were before, move to the next, otherwise,
390 // stop
391 //
392 if (isect1 || before1)
393 node1 = dict_next(Fint->_il, node1);
394 else
395 node1 = 0L;
396
397 if (isect2 || before2)
398 node2 = dict_next(Rint->_il, node2);
399 else
400 node2 = 0L;
401 }
402 }
403
404 if (errors)
405 abort();
406 }
407
408
409
410
411
412
413
414
415
416 // This is used all over the place.
417 //
418 #define D08D2 uint32FMTW(8)" "uint32FMTW(8)
419 #define KEY1THING "key1 = "uint32FMTW(8)" "uint32FMTW(8)" "uint32FMTW(8)" thing = "D08D2" "D08D2"\n"
420 #define KEY2THING "key2 = "uint32FMTW(8)" "uint32FMTW(8)" "uint32FMTW(8)" thing = "D08D2" "D08D2"\n"
421
422 int
423 main(int argc, char **argv) {
424 char *inputName = 0L;
425 char *subtractName = 0L;
426
427 int arg = 1;
428 while (arg < argc) {
429 if (strcmp(argv[arg], "-i") == 0) {
430 inputName = argv[++arg];
431 } else if (strcmp(argv[arg], "-s") == 0) {
432 subtractName = argv[++arg];
433 } else {
434 fprintf(stderr, "usage: %s [-h] [-s subtractFile] [-i inputFile]\n", argv[0]);
435 fprintf(stderr, " -s instead of finding regions to remove by looking\n");
436 fprintf(stderr, " for duplicatd regions in inputFile, load them\n");
437 fprintf(stderr, " from subtractFile.\n");
438 exit(1);
439 }
440 arg++;
441 }
442
443 if (inputName == 0L)
444 fprintf(stderr, "usage: %s [-i inputfile] [-o outputfile] [-h]\n", argv[0]), exit(1);
445
446 coverageIntervals *Fint = new coverageIntervals;
447 coverageIntervals *Rint = new coverageIntervals;
448
449 if (subtractName)
450 findCoverageIntervals(subtractName, 1, Fint, Rint);
451 else
452 findCoverageIntervals(inputName, 2, Fint, Rint);
453
454
455
456 // The original implementation would then sort the matches in X,
457 // merge the sorted intervals and the sorted matches together, resort
458 // the matches by Y, and merge with Rcov. That is a lot of work to
459 // avoid keeping two interval lists in memory.
460 //
461 // We build an in-core interval list for both assemblies, and
462 // then stream the matches by it.
463
464 // we need to ask the interval list:
465 // return the intervals that are covered by this interval
466
467
468 uint32 matchesLen = 0;
469 uint32 matchesMax = 1024;
470 match_s *matches = new match_s [matchesMax];
471 match_s extent;
472
473 uint32 matchNumber = 0;
474
475 atacFileStream AF(inputName);
476 atacMatch *m = AF.nextMatch('u');
477
478 while (m) {
479 matches[0].iid1 = m->iid1;
480 matches[0].pos1 = m->pos1;
481 matches[0].len1 = m->len1;
482 matches[0].ori1 = m->fwd1;
483 matches[0].iid2 = m->iid2;
484 matches[0].pos2 = m->pos2;
485 matches[0].len2 = m->len2;
486 matches[0].ori2 = m->fwd2;
487
488 // Save the original, we'll use this to test for intersections.
489 //
490 memcpy(&extent, matches, sizeof(match_s));
491
492 bool fwd = (matches[0].ori1 == matches[0].ori2);
493
494
495 // Query the tree for the first interval intersecting iid1
496 // XXX: Should this be upper_bound instead?
497 //
498 // A scratch interval used for querying the list
499 //
500 coverage2_s thing;
501
502 thing.axis = extent.iid1;
503 thing.beg = extent.pos1;
504 thing.end = extent.pos1 + extent.len1;
505 thing.coverage = 0;
506 dnode_t *node1start = Fint->lookup(&thing);
507 dnode_t *node1 = node1start;
508
509
510 thing.axis = extent.iid2;
511 thing.beg = extent.pos2;
512 thing.end = extent.pos2 + extent.len2;
513 thing.coverage = 0;
514 dnode_t *node2start = Rint->lookup(&thing);
515 dnode_t *node2 = node2start;
516
517 // while the node intersects the match, trim or split it, then
518 // get the next node.
519 //
520 // any way I tried this, it's ugly.
521 //
522 // if there is one match, then it is in [0]. If there is more
523 // than one match, then the trimmed match is in [0], but the
524 // split matches are in [1] on.
525
526
527 // XXX the problem is that we split off things, then move
528 // to the next match, without checking previously split
529 // things against this match
530 //
531
532 // Keep iterating until the node returned from the tree
533 // is empty, or it is after our region
534 //
535 while (node1 || node2) {
536
537 bool before1=false;
538 bool before2=false;
539
540 bool modified=false;
541
542 const coverage2_s *key1 = 0L;
543 const coverage2_s *key2 = 0L;
544
545 if (node1) {
546 key1 = (const coverage2_s *)dnode_getkey(node1);
547 before1 = ((key1->axis < extent.iid1) ||
548 ((key1->axis == extent.iid1) && (key1->beg < extent.pos1 + extent.len1)));
549
550
551 // Three cases: (1) we trim off the front, (2) trim off the
552 // back or (3) split. And, (4) delete the whole damn thing.
553 //
554 // Further complicated by having multiple things to try.
555 //
556 // If anything is modified, reset the node to the start
557 //
558
559 for (uint32 i=0; i<matchesLen; i++) {
560 if (matches[i].len1 == 0)
561 continue;
562
563 if ((key1->beg <= matches[i].pos1) &&
564 ((matches[i].pos1 + matches[i].len1) <= key1->end)) {
565 modified = true;
566
567 // Trim the whole thing?
568 //
569 matches[i].pos1 = 0;
570 matches[i].len1 = 0;
571 matches[i].pos2 = 0;
572 matches[i].len2 = 0;
573 } else if ((matches[i].pos1 < key1->beg) &&
574 (key1->end < (matches[i].pos1 + matches[i].len1))) {
575 modified = true;
576
577 // Contained. Split it.
578 //
579
580 // The left half
581 //
582 int newLen = key1->beg - matches[i].pos1;
583
584 matches[matchesLen].iid1 = matches[i].iid1;
585 matches[matchesLen].pos1 = matches[i].pos1;
586 matches[matchesLen].len1 = newLen;
587 matches[matchesLen].ori1 = matches[i].ori1;
588
589 if (fwd) {
590 matches[matchesLen].iid2 = matches[i].iid2;
591 matches[matchesLen].pos2 = matches[i].pos2;
592 matches[matchesLen].len2 = newLen;
593 matches[matchesLen].ori2 = matches[i].ori2;
594 } else {
595 matches[matchesLen].iid2 = matches[i].iid2;
596 matches[matchesLen].pos2 = matches[i].pos2 + matches[i].len2 - newLen;
597 matches[matchesLen].len2 = newLen;
598 matches[matchesLen].ori2 = matches[i].ori2;
599 }
600
601 matchesLen++;
602
603 // The right half
604 //
605 newLen = matches[i].pos1 + matches[i].len1 - key1->end;
606
607 matches[matchesLen].iid1 = matches[i].iid1;
608 matches[matchesLen].pos1 = key1->end;
609 matches[matchesLen].len1 = newLen;
610 matches[matchesLen].ori1 = matches[i].ori1;
611
612 if (fwd) {
613 matches[matchesLen].iid2 = matches[i].iid2;
614 matches[matchesLen].pos2 = matches[i].pos2 + (key1->end - matches[i].pos1);
615 matches[matchesLen].len2 = newLen;
616 matches[matchesLen].ori2 = matches[i].ori2;
617 } else {
618 matches[matchesLen].iid2 = matches[i].iid2;
619 matches[matchesLen].pos2 = matches[i].pos2;
620 matches[matchesLen].len2 = newLen;
621 matches[matchesLen].ori2 = matches[i].ori2;
622 }
623
624 matchesLen++;
625
626 // Invalidate this match
627 //
628 matches[i].pos1 = 0;
629 matches[i].len1 = 0;
630 matches[i].pos2 = 0;
631 matches[i].len2 = 0;
632 } else if ((key1->beg <= matches[i].pos1) &&
633 (matches[i].pos1 < key1->end)) {
634 modified = true;
635
636 // Trim the begin?
637 //
638
639 int trimLen = key1->end - matches[i].pos1;
640 matches[i].pos1 += trimLen;
641 matches[i].len1 -= trimLen;
642
643 if (fwd == true)
644 matches[i].pos2 += trimLen;
645 matches[i].len2 -= trimLen;
646
647 } else if ((key1->beg < (matches[i].pos1 + matches[i].len1)) &&
648 ((matches[i].pos1 + matches[i].len1) <= key1->end)) {
649 modified = true;
650
651 // Trim the end?
652 //
653
654 int trimLen = matches[i].pos1 + matches[i].len1 - key1->beg;
655 matches[i].len1 -= trimLen;
656
657 if (fwd == false)
658 matches[i].pos2 += trimLen;
659 matches[i].len2 -= trimLen;
660 }
661 }
662 } // isect
663
664
665
666 if (node2) {
667 key2 = (const coverage2_s *)dnode_getkey(node2);
668 before2 = ((key2->axis < extent.iid2) ||
669 ((key2->axis == extent.iid2) && (key2->beg < extent.pos2 + extent.len2)));
670
671 for (uint32 i=0; i<matchesLen; i++) {
672 if (matches[i].len2 == 0)
673 continue;
674
675 if ((key2->beg <= matches[i].pos2) &&
676 ((matches[i].pos2 + matches[i].len2) <= key2->end)) {
677 modified = true;
678
679 // Trim the whole thing?
680 //
681 matches[i].pos1 = 0;
682 matches[i].len1 = 0;
683 matches[i].pos2 = 0;
684 matches[i].len2 = 0;
685 } else if ((matches[i].pos2 < key2->beg) &&
686 (key2->end < (matches[i].pos2 + matches[i].len2))) {
687 modified = true;
688
689 // Contained. Split it.
690 //
691
692 // The left (forward strand) half
693 //
694
695 if (fwd) {
696 int newLen = key2->beg - matches[i].pos2;
697
698 matches[matchesLen].iid1 = matches[i].iid1;
699 matches[matchesLen].pos1 = matches[i].pos1;
700 matches[matchesLen].len1 = newLen;
701 matches[matchesLen].ori1 = matches[i].ori1;
702
703 matches[matchesLen].iid2 = matches[i].iid2;
704 matches[matchesLen].pos2 = matches[i].pos2;
705 matches[matchesLen].len2 = newLen;
706 matches[matchesLen].ori2 = matches[i].ori2;
707 } else {
708 int newLen = matches[i].pos2 + matches[i].len2 - key2->end;
709
710 matches[matchesLen].iid1 = matches[i].iid1;
711 matches[matchesLen].pos1 = matches[i].pos1;
712 matches[matchesLen].len1 = newLen;
713 matches[matchesLen].ori1 = matches[i].ori1;
714
715 matches[matchesLen].iid2 = matches[i].iid2;
716 matches[matchesLen].pos2 = key2->end;
717 matches[matchesLen].len2 = newLen;
718 matches[matchesLen].ori2 = matches[i].ori2;
719 }
720
721 matchesLen++;
722
723 // The right (forward strand) half
724 //
725 if (fwd) {
726 int newLen = matches[i].pos2 + matches[i].len2 - key2->end;
727
728 matches[matchesLen].iid1 = matches[i].iid1;
729 matches[matchesLen].pos1 = matches[i].pos1 + key2->end - matches[i].pos2;
730 matches[matchesLen].len1 = newLen;
731 matches[matchesLen].ori1 = matches[i].ori1;
732
733 matches[matchesLen].iid2 = matches[i].iid2;
734 matches[matchesLen].pos2 = key2->end;
735 matches[matchesLen].len2 = newLen;
736 matches[matchesLen].ori2 = matches[i].ori2;
737 } else {
738 int newLen = key2->beg - matches[i].pos2;
739
740 matches[matchesLen].iid1 = matches[i].iid1;
741 matches[matchesLen].pos1 = matches[i].pos1 + matches[i].pos2 + matches[i].len2 - key2->beg;
742 matches[matchesLen].len1 = newLen;
743 matches[matchesLen].ori1 = matches[i].ori1;
744
745 matches[matchesLen].iid2 = matches[i].iid2;
746 matches[matchesLen].pos2 = matches[i].pos2;
747 matches[matchesLen].len2 = newLen;
748 matches[matchesLen].ori2 = matches[i].ori2;
749 }
750
751 matchesLen++;
752
753 // Invalidate this match
754 //
755 matches[i].pos1 = 0;
756 matches[i].len1 = 0;
757 matches[i].pos2 = 0;
758 matches[i].len2 = 0;
759 } else if ((key2->beg <= matches[i].pos2) &&
760 (matches[i].pos2 < key2->end)) {
761 modified = true;
762
763 // Trim the begin? fwdOK, revOK
764 //
765
766 int trimLen = key2->end - matches[i].pos2;
767 matches[i].pos2 += trimLen;
768 matches[i].len2 -= trimLen;
769
770 if (fwd == true)
771 matches[i].pos1 += trimLen;
772 matches[i].len1 -= trimLen;
773
774 } else if ((key2->beg < (matches[i].pos2 + matches[i].len2)) &&
775 ((matches[i].pos2 + matches[i].len2) <= key2->end)) {
776 modified = true;
777
778 // Trim the end?
779 //
780
781 int trimLen = matches[i].pos2 + matches[i].len2 - key2->beg;
782 matches[i].len1 -= trimLen;
783
784 if (fwd == false)
785 matches[i].pos1 += trimLen;
786 matches[i].len2 -= trimLen;
787 }
788 }
789 }
790
791 // If we intersected or were before, move to the next, otherwise,
792 // stop.
793 //
794 if (modified)
795 node1 = node1start;
796 else if (before1)
797 node1 = dict_next(Fint->_il, node1);
798 else
799 node1 = 0L;
800
801 if (modified)
802 node2 = node2start;
803 else if (before2)
804 node2 = dict_next(Rint->_il, node2);
805 else
806 node2 = 0L;
807 } // end of while (node1 || node2)
808
809
810 // Nobody should be outside the extent
811 //
812 for (uint32 i=0; i<matchesLen; i++) {
813 if ((matches[i].len1 > 0) && (matches[i].len2 > 0)) {
814 if ((matches[i].pos1 < extent.pos1) ||
815 (matches[i].pos1 + matches[i].len1 > extent.pos1 + extent.len1) ||
816 (matches[i].pos2 < extent.pos2) ||
817 (matches[i].pos2 + matches[i].len2 > extent.pos2 + extent.len2)) {
818 fprintf(stderr, "match "uint32FMT" is outside the extent!\n", i);
819 abort();
820 }
821 }
822 }
823
824 // Print out all the modified matches
825 //
826 for (uint32 i=0; i<matchesLen; i++) {
827 if ((matches[i].len1 > 0) && (matches[i].len2 > 0)) {
828 fprintf(stdout, "M %s %s."uint32FMT" . %s "uint32FMT" "uint32FMT" 1 %s "uint32FMT" "uint32FMT" %d\n",
829 m->matchuid, m->parentuid, i,
830 AF.labelA(), matches[i].pos1, matches[i].len1,
831 AF.labelB(), matches[i].pos2, matches[i].len2, matches[i].ori2 ? 1 : -1);
832 }
833 }
834
835 // Check that the modified matches do not intersect anything in
836 // the tree.
837 //
838 intersectTest(matches, matchesLen, Fint, Rint, matchNumber);
839
840 matchNumber++;
841 }
842 }