Debian Codegraph @

atac-driver/gapShifter/gapShifter.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 <unistd.h>
22
23 #include "atac.H"
24 #include "bio++.H"
25 #include "seqCache.H"
26
27 #define MAXPRINT 90
28
29 #if 0
30 #define REPORT_RESULTS
31 #define REPORT_UNSHIFTABLE
32 #define REPORT_SHIFTING
33 #endif
34
35 // Global statistics, reset on each iteration
36 //
37 uint32 numShifted; // valid to be shifted, and were shifted
38 uint32 numNotShifted; // but valid to be shifted
39 uint32 numDiffSeq; // all the rest are not valid to be shifted
40 uint32 numDiffOri;
41 uint32 numZeroLen;
42 uint32 numOutOfOrder;
43 uint32 numNotAdjacent;
44 uint32 numNoGap;
45 uint32 numGapTooBig;
46 uint32 numOverlapping;
47 uint32 amountShifted[1024];
48
49 FILE *logFile = 0L;
50
51 // XXXXXX outer loop needs to skip empty matches!!
52
53 // Reads a set of matches and shifts the location of all the gaps to
54 // one side or the other.
55 //
56 // Example:
57 //
58 // A middle gap:
59 // GGGGGGGGGGATATATATATATATATATATATATGGGGGGGGG
60 // GGGGGGGGGGATAT--ATATATATATATATATATGGGGGGGGG
61 //
62 // A left-most gap:
63 // GGGGGGGGGGATATATATATATATATATATATATGGGGGGGGG
64 // GGGGGGGGGG--ATATATATATATATATATATATGGGGGGGGG
65 //
66 // A right-most gap:
67 // GGGGGGGGGGATATATATATATATATATATATATGGGGGGGGG
68 // GGGGGGGGGGATATATATATATATATATATAT--GGGGGGGGG
69 //
70 // Shifting is done for both assembly-axes.
71
72
73
74 // Returns true if these two matches have a potentially shiftable gap
75 // between them. Potentially shiftable means that the matches are
76 // contiguous on one axis, and consecutive (no matches between) on
77 // the other axis.
78 //
79 // Assume the matches are sorted by the first sequence.
80 //
81 bool
82 isPotentiallyShiftable(atacMatch *ma,
83 atacMatch *mb,
84 atacMatchOrder &MOB,
85 uint32 gapLimit) {
86
87 #ifdef REPORT_UNSHIFTABLE
88 fprintf(stderr, "isPotentiallyShiftable()\n");
89 ma->print(stderr, "A", "B");
90 mb->print(stderr, "A", "B");
91 #endif
92
93 // Not shiftable if on different sequences
94 //
95 if ((ma->iid1 != mb->iid1) ||
96 (ma->iid2 != mb->iid2)) {
97 #ifdef REPORT_UNSHIFTABLE
98 fprintf(stderr, "UNSHIFTABLE different sequences\n");
99 #endif
100 numDiffSeq++;
101 return(false);
102 }
103
104 // Not shiftable if the orientation of the two matches is
105 // different. This is probably not a gap we want to muck with.
106 //
107 if ((ma->fwd1 != mb->fwd1) ||
108 (ma->fwd2 != mb->fwd2)) {
109 #ifdef REPORT_UNSHIFTABLE
110 fprintf(stderr, "UNSHIFTABLE different orientation\n");
111 #endif
112 numDiffOri++;
113 return(false);
114 }
115
116 // Not shiftable if any length is zero. This isn't a gap, it's a
117 // dead match.
118 //
119 if ((ma->len1 == 0) ||
120 (ma->len2 == 0) ||
121 (mb->len1 == 0) ||
122 (mb->len2 == 0)) {
123 #ifdef REPORT_UNSHIFTABLE
124 fprintf(stderr, "UNSHIFTABLE zero length\n");
125 #endif
126 numZeroLen++;
127 return(false);
128 }
129
130 atacMatch *bl = (ma->fwd2) ? ma : mb; // the left match on B, relative to forward orientation
131 atacMatch *br = (ma->fwd2) ? mb : ma; // the right
132
133 // Not shiftable if the B matches are out of order
134 //
135 if (bl->pos2 > br->pos2) {
136 #ifdef REPORT_UNSHIFTABLE
137 fprintf(stderr, "UNSHIFTABLE misordered on B\n");
138 #endif
139 numOutOfOrder++;
140 return(false);
141 }
142
143 uint32 magap = mb->pos1 - (ma->pos1 + ma->len1);
144 uint32 mbgap = br->pos2 - (bl->pos2 + bl->len2);
145
146 // Not shiftable if there is no zero size gap
147 //
148 if ((magap > 0) && (mbgap > 0)) {
149 #ifdef REPORT_UNSHIFTABLE
150 fprintf(stderr, "UNSHIFTABLE no zero size gap ("uint32FMT", "uint32FMT")\n", magap, mbgap);
151 #endif
152 numNotAdjacent++;
153 return(false);
154 }
155
156 // Not shiftabe if there is no gap
157 //
158 if ((magap == 0) && (mbgap == 0)) {
159 #ifdef REPORT_UNSHIFTABLE
160 fprintf(stderr, "UNSHIFTABLE no gap on both sequences ("uint32FMT", "uint32FMT")\n", magap, mbgap);
161 #endif
162 numNoGap++;
163 return(false);
164 }
165
166 // Not shiftable if the gap is big
167 //
168 if ((magap > gapLimit) || (mbgap > gapLimit)) {
169 #ifdef REPORT_UNSHIFTABLE
170 fprintf(stderr, "UNSHIFTABLE gap too big ("uint32FMT", "uint32FMT")\n", magap, mbgap);
171 #endif
172 numGapTooBig++;
173 return(false);
174 }
175
176 // Not shiftable if they overlap
177 //
178 if (ma->pos1 + ma->len1 > mb->pos1) {
179 #ifdef REPORT_UNSHIFTABLE
180 fprintf(stderr, "UNSHIFTABLE overlap on sequence A\n");
181 #endif
182 numOverlapping++;
183 return(false);
184 }
185 if (bl->pos2 + bl->len2 > br->pos2) {
186 #ifdef REPORT_UNSHIFTABLE
187 fprintf(stderr, "UNSHIFTABLE overlap on sequence B\n");
188 #endif
189 numOverlapping++;
190 return(false);
191 }
192
193 uint32 iid1 = ma->matchiid;
194 uint32 iid2 = mb->matchiid;
195
196 // Check that there isn't another match stuck in the middle on
197 // the B axis.
198 //
199 if (ma->fwd2 == true) {
200 if ((MOB.index(iid1)+1) != MOB.index(iid2)) {
201 fprintf(stderr, "WARNING: Match inbetween! (forward)\n");
202
203 fprintf(stderr, "iid1 "uint32FMT", iid2 "uint32FMT"\n", iid1, iid2);
204 ma->print(stderr, "A", "B");
205 mb->print(stderr, "A", "B");
206
207 fprintf(stderr, "before, iid1, after\n");
208 MOB[MOB.index(iid1)-1]->print(stderr, "A", "B");
209 MOB[MOB.index(iid1) ]->print(stderr, "A", "B");
210 MOB[MOB.index(iid1)+1]->print(stderr, "A", "B");
211
212 fprintf(stderr, "before, iid2, after\n");
213 MOB[MOB.index(iid2)-1]->print(stderr, "A", "B");
214 MOB[MOB.index(iid2) ]->print(stderr, "A", "B");
215 MOB[MOB.index(iid2)+1]->print(stderr, "A", "B");
216
217 return(false);
218 }
219 } else {
220 if ((MOB.index(iid1)-1) != MOB.index(iid2)) {
221 fprintf(stderr, "WARNING: Match inbetween! (reverse-complement)\n");
222
223 fprintf(stderr, "iid1 "uint32FMT", iid2 "uint32FMT"\n", iid1, iid2);
224 ma->print(stderr, "A", "B");
225 mb->print(stderr, "A", "B");
226
227 fprintf(stderr, "before, iid1, after\n");
228 MOB[MOB.index(iid1)-1]->print(stderr, "A", "B");
229 MOB[MOB.index(iid1) ]->print(stderr, "A", "B");
230 MOB[MOB.index(iid1)+1]->print(stderr, "A", "B");
231
232 fprintf(stderr, "before, iid2, after\n");
233 MOB[MOB.index(iid2)-1]->print(stderr, "A", "B");
234 MOB[MOB.index(iid2) ]->print(stderr, "A", "B");
235 MOB[MOB.index(iid2)+1]->print(stderr, "A", "B");
236
237 return(false);
238 }
239 }
240
241 return(true);
242 }
243
244
245
246 void
247 dumpAgap(atacMatch *ma, atacMatch *mb,
248 atacMatchOrder &MOB,
249 seqCache *C1, seqCache *C2,
250 uint32 gapLimit,
251 bool shiftRight) {
252 }
253
254
255 void
256 dumpBgap(atacMatch *ma, atacMatch *mb,
257 atacMatchOrder &MOB,
258 seqCache *C1, seqCache *C2,
259 uint32 gapLimit,
260 bool shiftRight) {
261 }
262
263
264 // Returns the beginning of the sequence from pos to pos+len
265 char *
266 getSequenceBeg(char *str, uint32 pos, uint32 len, FastAAccessor &it) {
267 uint32 i = 0;
268
269 it.setRange(pos, len);
270
271 if (len > MAXPRINT)
272 len = MAXPRINT;
273
274 if (len > 0) {
275 it.setPosition(pos);
276
277 for (i=0; i<len; i++, ++it)
278 str[i] = *it;
279 }
280 str[i] = 0;
281
282 return(str);
283 }
284
285
286 // Returns all sequence from pos to pos+len
287 char *
288 getSequenceAll(char *str, uint32 pos, uint32 len, FastAAccessor &it) {
289 uint32 i = 0;
290
291 it.setRange(pos, len);
292
293 if (len > 0) {
294 it.setPosition(pos);
295
296 for (i=0; i<len; i++, ++it)
297 str[i] = *it;
298 }
299 str[i] = 0;
300
301 return(str);
302 }
303
304
305 // Returns the end of the sequence from pos to pos+len
306 char *
307 getSequenceEnd(char *str, uint32 pos, uint32 len, FastAAccessor &it) {
308 uint32 i = 0;
309
310 it.setRange(pos, len);
311
312 pos += len;
313 if (len > MAXPRINT)
314 len = MAXPRINT;
315
316 if (len > 0) {
317 it.setPosition(pos - len);
318
319 for (i=0; i<len; i++, ++it)
320 str[i] = *it;
321 }
322 str[i] = 0;
323
324 return(str);
325 }
326
327
328
329 uint32
330 shiftGap(atacFile &AF,
331 atacMatchList &ML,
332 atacMatch *ma, atacMatch *mb,
333 atacMatchOrder &MOB,
334 seqCache *C1, seqCache *C2,
335 uint32 gapLimit,
336 bool shiftRight) {
337
338 #ifdef REPORT_RESULTS
339 fprintf(stderr, "----------------------------------------\n");
340 #endif
341
342 if (isPotentiallyShiftable(ma, mb, MOB, gapLimit) == false)
343 return(0);
344
345 // Save a copy of the original matches
346 atacMatch macopy = *ma;
347 atacMatch mbcopy = *mb;
348
349 // Grab the sequences we use, and make the accesors
350 //
351 seqInCore *s1 = C1->getSequenceInCore(ma->iid1);
352 seqInCore *s2 = C2->getSequenceInCore(ma->iid2);
353
354 FastAAccessor mas1(s1, ma->fwd1 == false);
355 FastAAccessor mas2(s2, ma->fwd2 == false);
356 FastAAccessor mbs1(s1, mb->fwd1 == false);
357 FastAAccessor mbs2(s2, mb->fwd2 == false);
358
359 mas1.setRange(ma->pos1, ma->len1);
360 mas2.setRange(ma->pos2, ma->len2);
361 mbs1.setRange(mb->pos1, mb->len1);
362 mbs2.setRange(mb->pos2, mb->len2);
363
364 uint32 shifted = 0;
365
366 // We want to extend ma to the right, this will shift the gap to
367 // the right-most position (relative to the forward genomic).
368 //
369 // While there is a match after ma, extend ma to the right, and
370 // decrease mb from the left.
371 //
372 if (shiftRight == false) {
373
374 // Similar to above. The accessor hides most of the pain caused
375 // by reverse complement.
376
377 mas1.setPosition(ma->pos1 + ma->len1 - 1);
378 mas2.setPosition(ma->pos2 + ma->len2 - 1);
379
380 mbs1.setPosition(mb->pos1); --mbs1;
381 mbs2.setPosition(mb->pos2); --mbs2;
382
383
384 #ifdef REPORT_DEBUG
385 // Dump out some sequence to see where we really are
386 //
387 fprintf(stderr, "A: ");
388 for (uint32 i=0; i<50; i++) {
389 fprintf(stderr, "%c", *mas1);
390 --mas1;
391 }
392 fprintf(stderr, "\n");
393
394 fprintf(stderr, "B: ");
395 for (uint32 i=0; i<50; i++) {
396 fprintf(stderr, "%c", *mbs1);
397 --mbs1;
398 }
399 fprintf(stderr, "\n");
400
401 // Reset the iterators
402 //
403 mas1.setPosition(ma->pos1 + ma->len1 - 1);
404 mas2.setPosition(ma->pos2 + ma->len2 - 1);
405
406 mbs1.setPosition(mb->pos1); --mbs1;
407 mbs2.setPosition(mb->pos2); --mbs2;
408 #endif
409
410
411
412 while (mas1.isValid() &&
413 mas2.isValid() &&
414 mbs1.isValid() &&
415 mbs2.isValid() &&
416 (ma->len1 > 0) &&
417 (ma->len2 > 0) &&
418 (letterToBits[(int)*mbs1] != 0xff) &&
419 (letterToBits[(int)*mbs2] != 0xff) &&
420 IUPACidentity[(int)*mbs1][(int)*mbs2]) {
421
422 #ifdef REPORT_SHIFTING
423 fprintf(stderr, "EXTENDrev: MA %c/%c ----- %c/%c MB\n",
424 *mas1, *mas2, *mbs1, *mbs2);
425 #endif
426
427 mas1.extendRight(-1); ma->len1--; --mas1;
428 mas2.extendRight(-1); ma->len2--; --mas2;
429
430 mbs1.extendLeft(1); mb->len1++; --mbs1;
431 mbs2.extendLeft(1); mb->len2++; --mbs2;
432
433 shifted++;
434 }
435 } else {
436
437 // A wants to be the first thing after ma -- the first base in
438 // the gap. Set the position to the last thing in the range,
439 // then use the increment operator to extend past that. The spec
440 // on FastAAccessor says we can't directly go somewhere outside
441 // the range.
442 //
443 mas1.setPosition(ma->pos1 + ma->len1 - 1); ++mas1;
444 mas2.setPosition(ma->pos2 + ma->len2 - 1); ++mas2;
445
446 // B can be set to the first thing in the match with no problem.
447 //
448 mbs1.setPosition(mb->pos1);
449 mbs2.setPosition(mb->pos2);
450
451 // While we're still in sequence (isValid()) and we haven't
452 // obliterated the match we're shifting the gap into, and we can
453 // extend the other match (being both validSymbols and identity),
454 // shift the gap to the right.
455 //
456 while (mas1.isValid() &&
457 mas2.isValid() &&
458 mbs1.isValid() &&
459 mbs2.isValid() &&
460 (mb->len1 > 0) &&
461 (mb->len2 > 0) &&
462 (letterToBits[(int)*mas1]) &&
463 (letterToBits[(int)*mas2]) &&
464 IUPACidentity[(int)*mas1][(int)*mas2]) {
465
466 #ifdef REPORT_SHIFTING
467 fprintf(stderr, "EXTENDfwd: MA %c/%c ----- %c/%c MB\n",
468 *mas1, *mas2, *mbs1, *mbs2);
469 #endif
470
471 mas1.extendRight(1); ma->len1++; ++mas1;
472 mas2.extendRight(1); ma->len2++; ++mas2;
473
474 mbs1.extendLeft(-1); mb->len1--; ++mbs1;
475 mbs2.extendLeft(-1); mb->len2--; ++mbs2;
476
477 shifted++;
478 }
479 }
480
481 // Finally, update the two matches with the shifted results.
482
483 ma->pos1 = mas1.getRangeBegin();
484 ma->len1 = mas1.getRangeLength();
485 ma->pos2 = mas2.getRangeBegin();
486 ma->len2 = mas2.getRangeLength();
487 mb->pos1 = mbs1.getRangeBegin();
488 mb->len1 = mbs1.getRangeLength();
489 mb->pos2 = mbs2.getRangeBegin();
490 mb->len2 = mbs2.getRangeLength();
491
492
493 //
494 // The rest is just error checking.
495 //
496
497 if (shifted)
498 numShifted++;
499 else
500 numNotShifted++;
501
502 if (shifted < 1024)
503 amountShifted[shifted]++;
504
505 // leftmatch origend newend rightmatch origbegin newbegin
506 if (shifted && logFile) {
507 if (ma->fwd2) {
508 // Forward matches are easy.
509 //
510 fprintf(logFile, "%s\t%s\t%s:"uint32FMT"\t"uint32FMT"\t"uint32FMT"\t->\t"uint32FMT"\t"uint32FMT"\t",
511 ma->matchuid, mb->matchuid,
512 AF.labelA(), ma->iid1,
513 macopy.pos1 + macopy.len1, ma->pos1 + ma->len1,
514 mbcopy.pos1, mb->pos1);
515 fprintf(logFile, "%s:"uint32FMT"\t"uint32FMT"\t"uint32FMT"\t->\t"uint32FMT"\t"uint32FMT"\n",
516 AF.labelB(), ma->iid2,
517 macopy.pos2 + macopy.len2, ma->pos2 + ma->len2,
518 mbcopy.pos2, mb->pos2);
519 } else {
520 // Reverse matches are painful. The gap on B is between the
521 // right edge of mb, and the the left edge of ma.
522 //
523 fprintf(logFile, "%s\t%s\t%s:"uint32FMT"\t"uint32FMT"\t"uint32FMT"\t->\t"uint32FMT"\t"uint32FMT"\t",
524 ma->matchuid, mb->matchuid,
525 AF.labelA(), ma->iid1,
526 macopy.pos1 + macopy.len1, ma->pos1 + ma->len1,
527 mbcopy.pos1, mb->pos1);
528 fprintf(logFile, "%s:"uint32FMT"\t"uint32FMT"\t"uint32FMT"\t->\t"uint32FMT"\t"uint32FMT"\n",
529 AF.labelB(), ma->iid2,
530 mbcopy.pos2 + mbcopy.len2, mb->pos2 + mb->len2,
531 macopy.pos2, ma->pos2);
532 }
533 }
534
535
536 #ifdef REPORT_RESULTS
537 if (shifted)
538 fprintf(stderr, "SHIFTED "uint32FMT" bases.\n", shifted);
539 else
540 fprintf(stderr, "NOT SHIFTED.\n");
541
542 fprintf(stderr, uint32FMTW(9)"-"uint32FMTW(9)" -- "uint32FMTW(9)"-"uint32FMTW(9)"\n",
543 macopy.pos1, macopy.pos1 + macopy.len1,
544 mbcopy.pos1, mbcopy.pos1 + mbcopy.len1);
545 fprintf(stderr, uint32FMTW(9)"-"uint32FMTW(9)" -- "uint32FMTW(9)"-"uint32FMTW(9)"\n",
546 macopy.pos2, macopy.pos2 + macopy.len2,
547 mbcopy.pos2, mbcopy.pos2 + mbcopy.len2);
548 fprintf(stderr, "shifted "uint32FMT" bases (fwd1=%d fwd2=%d fwd1=%d fwd2=%d)\n", shifted, ma->fwd1, ma->fwd2, mb->fwd1, mb->fwd2);
549 fprintf(stderr, uint32FMTW(9)"-"uint32FMTW(9)" -- "uint32FMTW(9)"-"uint32FMTW(9)"\n",
550 ma->pos1, ma->pos1 + ma->len1,
551 mb->pos1, mb->pos1 + mb->len1);
552 fprintf(stderr, uint32FMTW(9)"-"uint32FMTW(9)" -- "uint32FMTW(9)"-"uint32FMTW(9)"\n",
553 ma->pos2, ma->pos2 + ma->len2,
554 mb->pos2, mb->pos2 + mb->len2);
555 #endif
556
557 uint32 errors = 0;
558
559 if (macopy.pos1 != ma->pos1)
560 fprintf(stderr, "WARNING: begin of assembly 1 moved!\n"), errors++;
561 if (mbcopy.pos1 + mbcopy.len1 != mb->pos1 + mb->len1)
562 fprintf(stderr, "WARNING: end of assembly 1 moved!\n"), errors++;
563
564 if ((ma->fwd2 == true) && (macopy.pos2 != ma->pos2))
565 fprintf(stderr, "WARNING: begin of assembly 2 moved!\n"), errors++;
566 if ((ma->fwd2 == false) && (mbcopy.pos2 != mb->pos2))
567 fprintf(stderr, "WARNING: begin of assembly 2 moved (rc)!\n"), errors++;
568
569 if ((ma->fwd2 == true) && (mbcopy.pos2 + mbcopy.len2 != mb->pos2 + mb->len2))
570 fprintf(stderr, "WARNING: end of assembly 1 moved!\n"), errors++;
571 if ((ma->fwd2 == false) && (macopy.pos2 + macopy.len2 != ma->pos2 + ma->len2))
572 fprintf(stderr, "WARNING: end of assembly 2 moved (rc)!\n"), errors++;
573
574 // For debugging, claim there were errors if we shifted something.
575 #ifdef REPORT_RESULTS
576 errors++;
577 #endif
578
579 if (errors > 0) {
580 atacMatch *l = 0L;
581 atacMatch *r = 0L;
582 char str1[1024];
583 char str2[1024];
584 char str3[1024];
585
586 // Print the sequence. We could print each piece separately
587 // (and, indeed, we tried that initially) but that's difficult
588 // because we need to remember which match is first on B,
589
590 macopy.print(stderr, "A", "B");
591 mbcopy.print(stderr, "A", "B");
592 l = &macopy;
593 r = &mbcopy;
594
595 getSequenceEnd(str1, l->pos1, l->len1, mas1);
596 getSequenceAll(str2, l->pos1 + l->len1, r->pos1 - l->pos1 - l->len1, mas1);
597 getSequenceBeg(str3, r->pos1, r->len1, mas1);
598 fprintf(stderr, "SEQA: %s -- %s -- %s\n", str1, str2, str3);
599
600 if (macopy.fwd2) {
601 // We're forward, so l is really first on B.
602 getSequenceEnd(str1, l->pos2, l->len2, mas2);
603 getSequenceAll(str2, l->pos2 + l->len2, r->pos2 - l->pos2 - l->len2, mas2);
604 getSequenceBeg(str3, r->pos2, r->len2, mas2);
605 } else {
606 // Nope, reverse complement, so r is really first on B. This
607 // only changes how we get the gap.
608 //
609 getSequenceEnd(str1, l->pos2, l->len2, mas2);
610 getSequenceAll(str2, r->pos2 + r->len2, l->pos2 - r->pos2 - r->len2, mas2);
611 getSequenceBeg(str3, r->pos2, r->len2, mas2);
612 }
613 fprintf(stderr, "SEQB: %s -- %s -- %s\n", str1, str2, str3);
614
615
616 // Do the same thing (same getSequence calls) for the after picture.
617
618 ma->print(stderr, "A", "B");
619 mb->print(stderr, "A", "B");
620 l = ma;
621 r = mb;
622
623 getSequenceEnd(str1, l->pos1, l->len1, mas1);
624 getSequenceAll(str2, l->pos1 + l->len1, r->pos1 - l->pos1 - l->len1, mas1);
625 getSequenceBeg(str3, r->pos1, r->len1, mas1);
626 fprintf(stderr, "SEQA: %s -- %s -- %s\n", str1, str2, str3);
627
628 if (ma->fwd2) {
629 getSequenceEnd(str1, l->pos2, l->len2, mas2);
630 getSequenceAll(str2, l->pos2 + l->len2, r->pos2 - l->pos2 - l->len2, mas2);
631 getSequenceBeg(str3, r->pos2, r->len2, mas2);
632 } else {
633 getSequenceEnd(str1, l->pos2, l->len2, mas2);
634 getSequenceAll(str2, r->pos2 + r->len2, l->pos2 - r->pos2 - r->len2, mas2);
635 getSequenceBeg(str3, r->pos2, r->len2, mas2);
636 }
637 fprintf(stderr, "SEQB: %s -- %s -- %s\n", str1, str2, str3);
638 }
639
640 //if (errors)
641 // exit(1);
642
643 return(shifted);
644 }
645
646
647
648
649
650 int
651 main(int argc, char *argv[]) {
652
653 if (argc == 1) {
654 fprintf(stderr, "usage: %s [options] < matches > matches\n", argv[0]);
655 fprintf(stderr, " Instead of the usual switch based options to enable behavior\n");
656 fprintf(stderr, " gapShifter iterates of a list of shift directions and sizes.\n");
657 fprintf(stderr, " l -- shift gaps to the left\n");
658 fprintf(stderr, " r -- shift gaps to the right\n");
659 fprintf(stderr, " # -- set the maximum size of a gap to shift\n");
660 fprintf(stderr, " log x -- open a logfile 'x' for results of the next shift\n");
661 fprintf(stderr, "\n");
662 fprintf(stderr, " for example\n");
663 fprintf(stderr, " gapShifter 1 l r l r 10 l r l log X r < some.atac > shifted.atac\n");
664 fprintf(stderr, " would shift 1bp gaps to the left, then to the right, then left,\n");
665 fprintf(stderr, " then set the gap size to 10bp and repeat. The last shift is logged\n");
666 fprintf(stderr, " into fle 'X'.\n");
667 fprintf(stderr, " \n");
668 fprintf(stderr, " This is useful since shifting gaps can obliterate matches, but possibly.\n");
669 fprintf(stderr, " when both left and right shifts are used.\n");
670 fprintf(stderr, " GCTAATTAGACG\n");
671 fprintf(stderr, " GCT-AT-AGACG\n");
672 fprintf(stderr, " The second gap can be shifted to the left, and the first gap can be\n");
673 fprintf(stderr, " shifted right, resulting in\n");
674 fprintf(stderr, " GCTAATTAGACG\n");
675 fprintf(stderr, " GCTA--TAGACG\n");
676 fprintf(stderr, " Thus, two one base gaps were merged into a two base gap, which might\n");
677 fprintf(stderr, " then be able to be shifted. e.g.:\n");
678 fprintf(stderr, " atgatcatcttatc\n");
679 fprintf(stderr, " at---c-t--tatc\n");
680 exit(1);
681 }
682
683 atacFile AF("-");
684 atacMatchList &ML = *AF.matches();
685 atacMatchOrder MOA(ML);
686 atacMatchOrder MOB(ML);
687
688 MOA.sortA();
689 MOB.sortB();
690
691 // second to last == loadAll
692 // last == report loading
693 //
694 seqCache *C1 = new seqCache(AF.assemblyFileA(), 2, false);
695 seqCache *C2 = new seqCache(AF.assemblyFileB(), 1024, false);
696
697 bool shiftRight = true;
698 uint32 gapLimit = 5;
699
700 char *logFileName = 0L;
701
702 int arg=1;
703 while (arg < argc) {
704 bool doShift = false;
705
706 if (strcmp(argv[arg], "log") == 0) {
707 logFileName = argv[++arg];
708 errno = 0;
709 logFile = fopen(logFileName, "w");
710 if (errno)
711 fprintf(stderr, "gapShifter: can't open log file '%s': %s\n", logFileName, strerror(errno)), exit(1);
712 } else if (strcmp(argv[arg], "l") == 0) {
713 shiftRight = false;
714 doShift = true;
715 } else if (strcmp(argv[arg], "r") == 0) {
716 shiftRight = true;
717 doShift = true;
718 } else {
719 gapLimit = strtouint32(argv[arg], 0L);
720 }
721
722 if (doShift) {
723
724 for (uint32 x=0; x<1024; x++)
725 amountShifted[x] = 0;
726
727 numShifted = 0;
728 numNotShifted = 0;
729 numDiffSeq = 0;
730 numDiffOri = 0;
731 numZeroLen = 0;
732 numOutOfOrder = 0;
733 numNotAdjacent = 0;
734 numNoGap = 0;
735 numGapTooBig = 0;
736 numOverlapping = 0;
737
738 fprintf(stderr, "Shifting gaps of length at most "uint32FMT" bases, to the %s.\n", gapLimit, (shiftRight) ? "right" : "left");
739
740 uint32 gapsShifted = 0;
741 for (uint32 i=1; i<ML.numMatches(); i++) {
742
743 if (shiftGap(AF, ML, MOA[i-1], MOA[i], MOB, C1, C2, gapLimit, shiftRight)) {
744 gapsShifted++;
745 //fprintf(stderr, "shifted "uint32FMT" out of "uint32FMT" (%6.2f%%)\r", gapsShifted, i, (double)gapsShifted / (double)i * 100.0);
746 //fflush(stderr);
747 }
748 }
749
750 fprintf(stderr, "numShifted = "uint32FMT"\n", numShifted);
751 fprintf(stderr, "numNotShifted = "uint32FMT"\n", numNotShifted);
752 fprintf(stderr, "numDiffSeq = "uint32FMT"\n", numDiffSeq);
753 fprintf(stderr, "numDiffOri = "uint32FMT"\n", numDiffOri);
754 fprintf(stderr, "numZeroLen = "uint32FMT"\n", numZeroLen);
755 fprintf(stderr, "numOutOfOrder = "uint32FMT"\n", numOutOfOrder);
756 fprintf(stderr, "numNotAdjacent = "uint32FMT"\n", numNotAdjacent);
757 fprintf(stderr, "numNoGap = "uint32FMT"\n", numNoGap);
758 fprintf(stderr, "numGapTooBig = "uint32FMT"\n", numGapTooBig);
759 fprintf(stderr, "numOverlapping = "uint32FMT"\n", numOverlapping);
760
761 for (uint32 x=0; x<50; x++)
762 fprintf(stderr, "amountShifted["uint32FMT"] = "uint32FMT" (number of gaps shifted by [number of bases])\n", x, amountShifted[x]);
763
764 fprintf(stderr, "shifted "uint32FMT" out of "uint32FMT" (%6.2f%%)\n", gapsShifted, ML.numMatches(), (double)gapsShifted / (double)ML.numMatches() * 100.0);
765
766 if (logFile) {
767 fclose(logFile);
768 logFileName = 0L;
769 logFile = 0L;
770 }
771 }
772
773 arg++;
774 }
775
776
777 for (uint32 i=0; i<ML.numMatches(); i++) {
778 atacMatch *ma = ML[i];
779 if ((ma->len1 > 0) && (ma->len2 > 0))
780 fprintf(stdout, "M u %s %s %s:"uint32FMT" "uint32FMT" "uint32FMT" 1 %s:"uint32FMT" "uint32FMT" "uint32FMT" %d\n",
781 ma->matchuid, ma->parentuid,
782 AF.labelA(), ma->iid1, ma->pos1, ma->len1,
783 AF.labelB(), ma->iid2, ma->pos2, ma->len2, ma->fwd2 ? 1 : -1);
784 }
785
786 return(0);
787 }