Debian Codegraph @

atac-driver/matchExtender/matchExtender-func.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 <vector>
24 #include <algorithm>
25
26 #include "bio++.H"
27 #include "atac.H"
28 #include "match.H"
29
30 using namespace std;
31
32
33 extern uint32 minEndRunLen;
34 extern uint32 maxMMBlock;
35 extern uint32 minBlockSep;
36 extern double minIdentity;
37 extern uint32 maxNbrSep;
38 extern uint32 maxNbrPathMM;
39
40
41 //#define DEBUG_TRACE
42 //#define DEBUG_TRIMTOPERCENT
43 //#define DEBUG_EXTEND
44 //#define DEBUG_EXTEND_CONSUME
45 //#define DEBUG_EXTEND_BACK
46 //#define DEBUG_EXTEND_FORWARD
47
48
49
50 // Return true if c1 and c2 are identities, false otherwise.
51 //
52 bool
53 isIdentity(char c1, char c2) {
54 return((letterToBits[(int)c1] != 0xff) &&
55 (letterToBits[(int)c2] != 0xff) &&
56 IUPACidentity[(int)c1][(int)c2]);
57 }
58
59
60
61 // Finds the largest block >= 'pct' (95%) identity.
62 //
63 bool
64 trim_to_pct(vector<match_s *>& matches, uint32 midx, double pct) {
65 #ifdef DEBUG_TRACE
66 fprintf(stderr, "trim_to_pct()\n");
67 #endif
68
69 uint32 best_start = 0;
70 uint32 best_len = 0;
71 match_s *m = matches[midx];
72
73 FastAAccessor &A = *m->_acc1;
74 FastAAccessor &B = *m->_acc2;
75
76 A.setPosition(m->pos1());
77 B.setPosition(m->pos2());
78
79 #ifdef DEBUG_TRIMTOPERCENT
80 //m->dump(stderr, "TrimToPercent", false);
81 #endif
82
83 // For all starting positions:
84 //
85 // We could short-circuit here - once (m->len() - start) becomes
86 // shorter than our best_len, we have no hope in finding a better
87 // one.
88 //
89 for (uint32 start=0;
90 (start< m->len()) && (m->len() - start > best_len);
91 ++start) {
92 uint32 best_run_len = 0;
93 uint32 sum = 0;
94
95 A.setPosition(m->pos1() + start);
96 B.setPosition(m->pos2() + start);
97
98 // And all ending positions:
99 //
100 // Compute the number of identities we've seen, and remember the
101 // length of the highest identity.
102 //
103 for (uint32 len = 1; start + len <= m->len(); ++len) {
104 char c1 = *A;
105 char c2 = *B;
106
107 // We just extend the last result by one, rather than recompute
108 // the whole value for our new range (start, len).
109 //
110 if (isIdentity(c1, c2))
111 sum++;
112
113 // If the sum is more than 'pct' identities, we are by
114 // construction the longest run at this starting point, so
115 // remember it.
116 //
117 if (sum >= pct * len)
118 best_run_len = len;
119
120 ++A;
121 ++B;
122 }
123
124 // Special case: if the whole string is okay, don't check any
125 // subranges
126 //
127 if ((start == 0) && (best_run_len == m->len()))
128 return(false);
129
130 // If we've just found a longer subrange, remember it.
131 //
132 if (best_run_len > best_len) {
133 best_start = start;
134 best_len = best_run_len;
135 }
136 }
137
138 if (best_len < m->len()) {
139 #ifdef DEBUG_TRIMTOPERCENT
140 fprintf(stderr, "============================================================\n");
141 fprintf(stderr, "Trimming to substring with start="uint32FMT" and len="uint32FMT" for percent identity\n",
142 best_start, best_len);
143 m->dump(stderr, "BEFORE", true);
144 #endif
145
146 m->extendLeft(-(int32)best_start);
147 m->extendRight(-(int32)(m->len() - best_len));
148
149 #ifdef DEBUG_TRIMTOPERCENT
150 m->dump(stderr, "AFTER", true);
151 fprintf(stderr, "============================================================\n");
152 #endif
153
154 return(true);
155 }
156
157 return(false);
158 }
159
160
161 void
162 extend_match_backward(vector<match_s *>& matches,
163 uint32 midx,
164 uint32 min_start_pos) {
165 #ifdef DEBUG_TRACE
166 fprintf(stderr, "extend_match_backward()-- min_start_pos="uint32FMT"\n", min_start_pos);
167 #endif
168
169 // Assumes when traveling backwards that we will never run into
170 // another match (otherwise, that match would have been forward
171 // extended previously).
172
173 uint32 num_recent_mismatches = 0;
174 match_s *m = matches[midx];
175 uint32 good_run_len = (int) m->len();
176 uint32 num_pending = 0;
177
178 FastAAccessor &A = *m->_acc1;
179 FastAAccessor &B = *m->_acc2;
180
181 A.setPosition(m->_acc1->getRangeBegin());
182 B.setPosition(m->_acc2->getRangeBegin());
183
184 // Decrement, instead of subtract one from the position above, to
185 // avoid any issues with overflow (e.g., 0 - 1).
186 //
187 --A;
188 --B;
189
190 while ((A.getPosition() > min_start_pos) && A.isValid() && B.isValid()) {
191 char c1 = *A;
192 char c2 = *B;
193
194 if (isIdentity(c1, c2)) {
195 good_run_len++;
196
197 // If we've gone long enough, erase our mismatch record
198 //
199 if (good_run_len == minBlockSep) // 20 by default
200 num_recent_mismatches = 0;
201
202 // If we're in the middle of a long good run, add the character
203 // to the match (END_RUN_LEN=10)
204 //
205 // Otherwise, if we just made the minimum extension length, add
206 // all of the pending characters.
207 //
208 // Otherwise, this character is pending. However, still do
209 // output if we're run out of sequence.
210 //
211 if (good_run_len > minEndRunLen) {
212 m->extendLeft(1);
213 } else if (good_run_len == minEndRunLen) {
214 m->extendLeft(num_pending + 1);
215 num_pending = 0;
216 } else {
217 num_pending++;
218 }
219 } else {
220 good_run_len = 0;
221 num_pending++;
222 num_recent_mismatches++;
223 if (num_recent_mismatches > maxMMBlock) // 3 by default
224 break;
225 }
226
227 --A;
228 --B;
229 }
230
231 // If we hit the end of the sequence, and are good, do extension
232 //
233 // if ((A.getPosition() == min_start_pos) || (B.getPosition() == 0))
234 //
235 if (!A.isValid() || !B.isValid() || (A.getPosition() <= min_start_pos))
236 m->extendLeft(num_pending);
237
238
239 #ifdef DEBUG_EXTEND_BACK
240 fprintf(stderr, "extend_back()-- M u %s . %s %d %d 1 %s %d %d 1\n",
241 m->_matchId,
242 m->_id1, m->_acc1->getRangeBegin(), m->_acc1->getRangeLength(),
243 m->_id2, m->_acc2->getRangeBegin(), m->_acc2->getRangeLength());
244 #endif
245 }
246
247
248 bool
249 can_reach_nearby_match(match_s *src, match_s *dest) {
250 #ifdef DEBUG_TRACE
251 fprintf(stderr, "can_reach_nearby_match()\n");
252 #endif
253
254 if (dest->pos1() - (src->pos1() + src->len()) > (uint32) maxNbrSep) // 100
255 return false;
256
257 #if 0
258 src->dump(stderr, "src:");
259 dest->dump(stderr, "dst:");
260 #endif
261
262 FastAAccessor &A = *src->_acc1;
263 FastAAccessor &B = *src->_acc2;
264
265 A.setPosition(A.getRangeEnd() - 1);
266 B.setPosition(B.getRangeEnd() - 1);
267
268 ++A;
269 ++B;
270
271 uint32 num_mismatch = 0;
272
273 while ((num_mismatch <= maxNbrPathMM) && // 5
274 (A.getPosition() < dest->pos1()) &&
275 (A.isValid()) &&
276 (B.isValid())) {
277 if (!isIdentity(*A, *B))
278 num_mismatch++;
279
280 ++A;
281 ++B;
282 }
283
284 #if 0
285 fprintf(stderr, "num_mismatch=%d pos: %d %d valid: A:%d B:%d\n",
286 num_mismatch, A.getPosition(), dest->pos1(), A.isValid(), B.isValid());
287 #endif
288
289 return(num_mismatch <= maxNbrPathMM); // 5
290 }
291
292
293
294 // Stops and returns true if we hit the next match
295 //
296 bool
297 extend_match_forward(vector<match_s *>& matches, uint32 midx, match_s *target) {
298 #ifdef DEBUG_TRACE
299 fprintf(stderr, "extend_match_forward()\n");
300 #endif
301
302 match_s *m = matches[midx];
303 uint32 num_recent_mismatches = 0;
304 uint32 num_pending = 0;
305
306 uint32 good_run_len = (int) m->len();
307
308 FastAAccessor &A = *m->_acc1;
309 FastAAccessor &B = *m->_acc2;
310
311 #ifdef DEBUG_EXTEND_FORWARD
312 fprintf(stderr, "extend_match_forward()-- A:%4d-%4d B:%4d-%4d\n",
313 A.getRangeBegin(), A.getRangeLength(),
314 B.getRangeBegin(), B.getRangeLength());
315 #endif
316
317 // Set our position to the last valid base in the range, then move
318 // to the next one.
319 //
320 A.setPosition(A.getRangeEnd() - 1);
321 B.setPosition(B.getRangeEnd() - 1);
322
323 ++A;
324 ++B;
325
326 while (A.isValid() && B.isValid()) {
327 char c1 = *A;
328 char c2 = *B;
329
330 if (isIdentity(c1, c2)) {
331 good_run_len++;
332
333 //fprintf(stderr, "extend-forward %c %c\n", c1, c2);
334
335 // Pass Go and collect $200
336 //
337 if (good_run_len == minBlockSep)
338 num_recent_mismatches = 0;
339
340 // If not enough good characters yet, increase the length
341 // pending. We used to check for the hitting the end of the
342 // sequence here.
343 //
344 // Otherwise, if we have just made the minumum good run length,
345 // do the extension.
346 //
347 // Otherwise, if we're above the minimum good length, extend by
348 // another character.
349 //
350 if (good_run_len < minEndRunLen) {
351 num_pending++;
352 } else if (good_run_len == minEndRunLen) {
353 m->extendRight(num_pending + 1);
354 num_pending = 0;
355 } else if (good_run_len > minEndRunLen) {
356 m->extendRight(1);
357 }
358
359 // If we've run into (and possibly over) another seed match,
360 // return so the main loop can consume and restart.
361 //
362 if (m->canMergeWith(target))
363 return(true);
364 } else {
365 good_run_len = 0;
366 num_pending++;
367 num_recent_mismatches++;
368
369 if (num_recent_mismatches > maxMMBlock)
370 return(false);
371 }
372
373 ++A;
374 ++B;
375 }
376
377 // If we've got a short good run but have hit the end of
378 // a sequence, do extension.
379 //
380 if ((!A.isValid() || !B.isValid()) && (good_run_len < minEndRunLen))
381 m->extendRight(num_pending);
382
383 #ifdef DEBUG_EXTEND_FORWARD
384 fprintf(stderr, "extend_match_forward(finish)-- A:%4d-%4d B:%4d-%4d\n",
385 A.getRangeBegin(), A.getRangeLength(),
386 B.getRangeBegin(), B.getRangeLength());
387 #endif
388
389 return(false);
390 }
391
392
393
394
395
396
397
398 uint32
399 extend_matches_on_diagonal(vector<match_s *>& matches, uint32 diag_start) {
400 #ifdef DEBUG_TRACE
401 fprintf(stderr, "extend_matches_on_diagonal()\n");
402 #endif
403
404 uint32 diag_id = matches[diag_start]->_diagonal;
405 uint32 idx;
406 uint32 prev_end = 0;
407 match_s *m;
408 match_s *next_m = NULL;
409
410 // Back extend each match as far as possible (but never over the
411 // preceding match
412 //
413 for (idx = diag_start;
414 (idx < matches.size()) && (matches[idx]->_diagonal == diag_id);
415 ++idx) {
416
417 m = matches[idx];
418
419 #ifdef DEBUG_EXTEND_BACK
420 m->dump(stderr, "Before back extension:", true);
421 #endif
422
423 extend_match_backward(matches, idx, prev_end);
424
425 #ifdef DEBUG_EXTEND_BACK
426 m->dump(stderr, "After back extension:", true);
427 #endif
428
429 #ifdef DEBUG_EXTEND
430 fprintf(stderr, "1M u %s . %s %d %d 1 %s %d %d 1\n",
431 matches[idx]->_matchId,
432 matches[idx]->_id1, matches[idx]->_acc1->getRangeBegin(), matches[idx]->_acc1->getRangeLength(),
433 matches[idx]->_id2, matches[idx]->_acc2->getRangeBegin(), matches[idx]->_acc2->getRangeLength());
434 #endif
435
436 prev_end = m->pos1() + m->len();
437
438 if ((m->pos1() > m->seq1()->sequenceLength()) || (m->pos2() > m->seq2()->sequenceLength()))
439 m->dump(stderr, "NEGATIVE after back extend!\n", true), abort();
440 }
441
442
443 // Now forward extend each match
444
445
446 idx = diag_start;
447 while ((idx < matches.size()) &&
448 (matches[idx]->_diagonal == diag_id)) {
449
450 if (matches[idx]->isDeleted()) {
451 idx++;
452 continue;
453 }
454
455 #ifdef DEBUG_EXTEND
456 fprintf(stderr, "2M u %s . %s %d %d 1 %s %d %d 1\n",
457 matches[idx]->_matchId,
458 matches[idx]->_id1, matches[idx]->_acc1->getRangeBegin(), matches[idx]->_acc1->getRangeLength(),
459 matches[idx]->_id2, matches[idx]->_acc2->getRangeBegin(), matches[idx]->_acc2->getRangeLength());
460 #endif
461
462 m = matches[idx];
463 next_m = 0L;
464
465 for (uint32 next_idx=idx+1; ((next_idx < matches.size()) &&
466 (matches[next_idx]->_diagonal == diag_id) &&
467 (next_m == 0L)); next_idx++)
468 if (matches[next_idx]->isDeleted() == false)
469 next_m = matches[next_idx];
470
471 // First, try to reach the next match with the simple "maximum of
472 // k mismatches" rule. If we made it, consume the next match and
473 // start the loop again with the same match (now extended)
474 //
475 if (next_m && can_reach_nearby_match(m, next_m)) {
476 #ifdef DEBUG_EXTEND_CONSUME
477 m->dump(stderr, "I can_reach_nearby_match and extend this", true);
478 next_m->dump(stderr, "with this", true);
479 #endif
480 m->consume(next_m);
481 next_m->setDeleted();
482 #ifdef DEBUG_EXTEND_CONSUME
483 m->dump(stderr, "Extended through next match via neighbor search:", true);
484 #endif
485 continue;
486 }
487
488 // Otherwise, try to make it to the next match with the
489 // character-at- a-time extension rules. If we make it, restart
490 // the loop with the same match (now extended). Otherwise, trim
491 // the extended match as necessary and move on to the next
492 // match.
493 //
494 if (extend_match_forward(matches, idx, next_m)) {
495 #ifdef DEBUG_EXTEND_CONSUME
496 m->dump(stderr, "I extend_match_forward and extend this", true);
497 next_m->dump(stderr, "with this", true);
498 #endif
499 m->consume(next_m);
500 next_m->setDeleted();
501 #ifdef DEBUG_EXTEND_CONSUME
502 m->dump(stderr, "Extended through next match via forward extension:", true);
503 #endif
504 continue;
505 }
506
507 #ifdef DEBUG_EXTEND
508 //m->dump(stderr, "Failed to make next match. Final extended version:", true);
509 #endif
510
511 #ifdef DEBUG_EXTEND
512 fprintf(stderr, "3M u %s . %s %d %d 1 %s %d %d 1\n",
513 matches[idx]->_matchId,
514 matches[idx]->_id1, matches[idx]->_acc1->getRangeBegin(), matches[idx]->_acc1->getRangeLength(),
515 matches[idx]->_id2, matches[idx]->_acc2->getRangeBegin(), matches[idx]->_acc2->getRangeLength());
516 #endif
517
518 // Didn't make it, so trim and move on
519 //
520 if (trim_to_pct(matches, idx, minIdentity)) {
521 #ifdef DEBUG_EXTEND_TRIMMING
522 m->dump(stderr, "After trimming:", true);
523 #endif
524 } else {
525 #ifdef DEBUG_EXTEND_TRIMMING
526 fprintf(stderr, "No trimming done.\n");
527 #endif
528 }
529
530 #ifdef DEBUG_EXTEND
531 fprintf(stderr, "4M u %s . %s %d %d 1 %s %d %d 1\n",
532 matches[idx]->_matchId,
533 matches[idx]->_id1, matches[idx]->_acc1->getRangeBegin(), matches[idx]->_acc1->getRangeLength(),
534 matches[idx]->_id2, matches[idx]->_acc2->getRangeBegin(), matches[idx]->_acc2->getRangeLength());
535 #endif
536
537 #ifdef DEBUG_EXTEND
538 if ((m->pos1() > m->seq1()->sequenceLength()) || (m->pos2() > m->seq2()->sequenceLength()))
539 m->dump(stderr, "NEGATIVE after forward extend!", true), abort();
540
541 fprintf(stderr, "\n==============\n\n");
542 #endif
543
544 ++idx;
545 }
546
547 return idx;
548 }
549
550
551