Debian Codegraph @

seagen/hitMatrix.C

raw symbols SCIP
1 #include "searchGENOME.H"
2 #include "aHit.H"
3
4 #define TRACE 0
5
6 hitMatrix::hitMatrix(uint32 qsLen, uint32 qsMers, uint32 qsIdx) {
7 _qsLen = qsLen;
8 _qsMers = qsMers;
9 _qsIdx = qsIdx;
10
11 _hitsLen = 0;
12 _hitsMax = 128;
13 _hits = new diagonalLine [_hitsMax];
14
15 _matches = 0L;
16 }
17
18
19 hitMatrix::~hitMatrix() {
20 delete [] _hits;
21 }
22
23 void
24 hitMatrix::addMatch(uint32 qsLo,
25 uint32 qsHi,
26 uint32 dsLo,
27 uint32 dsHi,
28 merCovering *IL) {
29 uint32 offset = 0;
30
31 // Extend the match
32 //
33 // Two methods: the first uses hardcoded parameters, has two
34 // plateau's, and is the one to use for ESTs and mRNA in ESTmapper.
35 // The second is paramterized, and has a single plateau.
36 //
37
38 if (config._extendAlternate) {
39 offset = config._extendWeight * qsLo;
40
41 if (offset < config._extendMinimum)
42 offset = config._extendMinimum;
43
44 if (dsLo < offset)
45 dsLo = 0;
46 else
47 dsLo -= offset;
48
49 offset = config._extendWeight * (_qsLen - qsHi);
50
51 if (offset < config._extendMinimum)
52 offset = config._extendMinimum;
53
54 dsHi += offset;
55 } else {
56 // If the start of the match is near the start of the EST, we do
57 // not need to search very far in the genome.
58 //
59 offset = 0;
60 if (qsLo < 50)
61 offset = 2000;
62 else
63 if (qsLo < 100)
64 offset = 5000;
65 else
66 offset = 50 * qsLo;
67
68 if (dsLo < offset)
69 dsLo = 0;
70 else
71 dsLo -= offset;
72
73 // Likewise, if the match is near the end of the EST, extend. We
74 // don't know the length of the genomic sequence, so we can't check
75 // for "overflow".
76 //
77 offset = _qsLen - qsHi;
78 if (offset < 50)
79 dsHi += 2000;
80 else
81 if (offset < 100)
82 dsHi += 5000;
83 else
84 dsHi += 50 * offset;
85 }
86
87
88
89 // Create a new match
90 //
91 // n = new match
92 // m = current match
93 // l = last match
94 //
95 trapMatch *n = new trapMatch(qsLo, qsHi, dsLo, dsHi, IL);
96
97
98 // And find a home for it in the list. No merging of matches is done here. It's
99 // too hard.
100 //
101 if ((_matches == 0L) || (n->_dsHi > _matches->_dsHi)) {
102 n->_next = _matches;
103 _matches = n;
104 } else {
105 trapMatch *l = _matches;
106 trapMatch *m = _matches->_next;
107
108 while ((m) && (n->_dsHi < m->_dsHi)) {
109 l = m;
110 m = m->_next;
111 }
112
113 n->_next = m;
114 l->_next = n;
115 }
116 }
117
118
119 // Utility for sorting the diagonal lines in the hitMatrix
120 //
121 // The two comparison functions return true if the first line
122 // is less than the second line.
123
124 #ifdef WITHOUT_DIAGONALID
125
126 inline
127 int
128 compareLines(diagonalLine *A, diagonalLine *B, uint32 qsLen) {
129 uint32 a = qsLen - A->_qsPos - 1 + A->_dsPos;
130 uint32 b = qsLen - B->_qsPos - 1 + B->_dsPos;
131
132 return(((a < b)) ||
133 ((a == b) && (A->_qsPos < B->_qsPos)));
134 }
135
136 inline
137 int
138 compareLines(uint32 l, uint32 q, diagonalLine *B, uint32 qsLen) {
139 uint32 b = qsLen - B->_qsPos - 1 + B->_dsPos;
140
141 return(((l < b)) ||
142 ((l == b) && (q < B->_qsPos)));
143 }
144
145 inline
146 void
147 adjustHeap(diagonalLine *L, int32 p, int32 n, uint32 qsLen) {
148 uint32 q = L[p]._qsPos;
149 uint32 d = L[p]._dsPos;
150 uint32 l = qsLen - q - 1 + d;
151 int32 c = (p << 1) + 1; // let c be the left child of p
152
153 while (c < n) {
154
155 // Find the larger of the two children
156 //
157 if ((c+1 < n) && compareLines(L+c, L+c+1, qsLen))
158 c++;
159
160 // Does the node in question fit here?
161 //
162 if (compareLines(l, q, L+c, qsLen) == false)
163 break;
164
165 // Else, swap the parent and the child
166 //
167 L[p]._qsPos = L[c]._qsPos;
168 L[p]._dsPos = L[c]._dsPos;
169
170 // Move down the tree
171 //
172 p = c;
173 c = (p << 1) + 1;
174 }
175
176 L[p]._qsPos = q;
177 L[p]._dsPos = d;
178 }
179
180
181 #else // WITH_DIAGONALID
182
183
184 inline
185 int
186 compareLines(diagonalLine *A, diagonalLine *B) {
187 return(((A->_diagonalID < B->_diagonalID)) ||
188 ((A->_diagonalID == B->_diagonalID) && (A->_qsPos < B->_qsPos)));
189 }
190
191 inline
192 int
193 compareLines(uint32 l, uint32 q, diagonalLine *B) {
194 return(((l < B->_diagonalID)) ||
195 ((l == B->_diagonalID) && (q < B->_qsPos)));
196 }
197
198 inline
199 void
200 adjustHeap(diagonalLine *L, int32 p, int32 n) {
201 uint32 q = L[p]._qsPos;
202 uint32 d = L[p]._dsPos;
203 uint32 l = L[p]._diagonalID;
204 int32 c = (p << 1) + 1; // let c be the left child of p
205
206 while (c < n) {
207
208 // Find the larger of the two children
209 //
210 if ((c+1 < n) && compareLines(L+c, L+c+1))
211 c++;
212
213 // Does the node in question fit here?
214 //
215 if (compareLines(l, q, L+c) == false)
216 break;
217
218 // Else, swap the parent and the child
219 //
220 L[p]._qsPos = L[c]._qsPos;
221 L[p]._dsPos = L[c]._dsPos;
222 L[p]._diagonalID = L[c]._diagonalID;
223
224 // Move down the tree
225 //
226 p = c;
227 c = (p << 1) + 1;
228 }
229
230 L[p]._qsPos = q;
231 L[p]._dsPos = d;
232 L[p]._diagonalID = l;
233 }
234
235
236 #endif
237
238 void
239 hitMatrix::filter(encodedQuery *query, bool isReverse) {
240
241 if (_hitsLen == 0)
242 return;
243
244 // Decide on the minimum quality values; we pick the larger of
245 // the fixed lengths, and the sequence length * coverage.
246 //
247 uint32 minLengthSingle = (uint32)(config._minCoverageSingle * _qsLen);
248 uint32 minLengthMultiple = (uint32)(config._minCoverageMultiple * _qsLen);
249
250 if (minLengthSingle < config._minLengthSingle)
251 minLengthSingle = config._minLengthSingle;
252
253 if (minLengthMultiple < config._minLengthMultiple)
254 minLengthMultiple = config._minLengthMultiple;
255
256
257
258 // First, sort by the dsPos. This is done so that we can find all the hits for
259 // a specific scaffold.
260 //
261 sort_dsPos();
262
263
264 // Now, while there are hits left....
265 //
266 uint32 firstHit = 0;
267 uint32 lastHit = 0;
268 uint32 currentSeq = 0;
269
270 while (firstHit < _hitsLen) {
271
272 // Move the currentSeq until the firstHit is below it.
273 //
274 while ((currentSeq < config._dbSTREAM->numberOfSequences()) &&
275 (config._dbSTREAM->startOf(currentSeq) <= _hits[firstHit]._dsPos))
276 currentSeq++;
277
278 //
279 // currentSeq is now the sequence AFTER the one that we want hits in.
280 //
281
282 // Find the first hit that is in currentSeq. If this is the last sequence,
283 // then, of course, all remaining hits are in it.
284 //
285 if (currentSeq < config._dbSTREAM->numberOfSequences()) {
286 lastHit = firstHit + 1;
287 while ((lastHit < _hitsLen) &&
288 (_hits[lastHit]._dsPos < config._dbSTREAM->startOf(currentSeq)))
289 lastHit++;
290 } else {
291 lastHit = _hitsLen;
292 }
293
294 // Drop back one sequence; this is the sequence the hits are in.
295 //
296 currentSeq--;
297
298 #if TRACE
299 fprintf(stdout, "Hits are in sequence %d\n", config._dbSTREAM->IIDOf(currentSeq));
300 fprintf(stdout, "filtering %u hits -- first = %u last = %u.\n", _hitsLen, firstHit, lastHit);
301
302 #if 0
303 fprintf(stdout, "UNSORTED\n");
304 for (uint32 i=firstHit; i<lastHit; i++)
305 fprintf(stdout, "hit at qs=%4u ds=%6u diag=%6u\n",
306 _hits[i]._qsPos,
307 _hits[i]._dsPos,
308 _hits[i]._diagonalID);
309 #endif
310 #endif
311
312 // Adjust the hits to be relative to the start of this sequence
313 //
314 for (uint32 i=firstHit; i<lastHit; i++)
315 _hits[i]._dsPos -= config._dbSTREAM->startOf(currentSeq);
316
317 // Sort them, if needed.
318 //
319 if (lastHit - firstHit > 1) {
320
321 // We cheat; heapsort isn't too friendly to sorting the middle of
322 // an array, so we make a new array in the middle!
323 //
324 diagonalLine *hitsToSort = _hits + firstHit;
325
326 // Build the heap. I initially thought this could be done at the
327 // same time as the scan for the last hit, but it can't (easily)
328 //
329 for (int32 i=(lastHit - firstHit)/2 - 1; i>=0; i--)
330 #ifdef WITHOUT_DIAGONALID
331 adjustHeap(hitsToSort, i, lastHit - firstHit, _qsLen);
332 #else
333 adjustHeap(hitsToSort, i, lastHit - firstHit);
334 #endif
335
336 // Sort the hits be diagonal. This is the second part of
337 // heap sort -- Interchange the new maximum with the element
338 // at the end of the tree
339 //
340 for (uint32 i=lastHit - firstHit - 1; i>0; i--) {
341 uint32 q = hitsToSort[i]._qsPos;
342 uint32 d = hitsToSort[i]._dsPos;
343 #ifndef WITHOUT_DIAGONALID
344 uint32 l = hitsToSort[i]._diagonalID;
345 #endif
346
347 hitsToSort[i]._qsPos = hitsToSort[0]._qsPos;
348 hitsToSort[i]._dsPos = hitsToSort[0]._dsPos;
349 #ifndef WITHOUT_DIAGONALID
350 hitsToSort[i]._diagonalID = hitsToSort[0]._diagonalID;
351 #endif
352
353 hitsToSort[0]._qsPos = q;
354 hitsToSort[0]._dsPos = d;
355 #ifndef WITHOUT_DIAGONALID
356 hitsToSort[0]._diagonalID = l;
357 #endif
358
359 #ifdef WITHOUT_DIAGONALID
360 adjustHeap(hitsToSort, 0, i, _qsLen);
361 #else
362 adjustHeap(hitsToSort, 0, i);
363 #endif
364 }
365 }
366
367
368 // Check the sorting
369 //
370 #if 0
371 #if 0
372 fprintf(stderr, "sort by diagonal:\n");
373 for (uint32 i=firstHit; i<lastHit; i++)
374 fprintf(stderr, "%8u %8u %8u\n", _hits[i]._diagonalID, _hits[i]._qsPos, _hits[i]._dsPos);
375 #endif
376 for (uint32 i=firstHit; i<lastHit-1; i++) {
377 if (_hits[i]._diagonalID > _hits[i+1]._diagonalID) {
378 fprintf(stderr, "sort by diagonal failed.\n");
379 exit(1);
380 }
381 }
382 #endif
383
384
385
386 #if TRACE
387 #if 0
388 fprintf(stdout, "SORTED\n");
389 for (uint32 i=firstHit; i<lastHit; i++)
390 fprintf(stdout, "hit at qs=%4u ds=%6u diag=%6u\n",
391 _hits[i]._qsPos,
392 _hits[i]._dsPos,
393 _hits[i]._diagonalID);
394 #endif
395
396 fprintf(stdout, "FILTERED\n");
397 #endif
398
399 // Filter them
400 //
401 #ifdef WITHOUT_DIAGONALID
402 uint32 frstDiagonal = _qsLen - _hits[firstHit]._qsPos - 1 + _hits[firstHit]._dsPos;
403 uint32 lastDiagonal = frstDiagonal;
404 #else
405 uint32 frstDiagonal = _hits[firstHit]._diagonalID;
406 uint32 lastDiagonal = _hits[firstHit]._diagonalID;
407 #endif
408 uint32 qsLow = _hits[firstHit]._qsPos;
409 uint32 qsHigh = _hits[firstHit]._qsPos;
410 uint32 dsLow = _hits[firstHit]._dsPos;
411 uint32 dsHigh = _hits[firstHit]._dsPos;
412
413 // Create a new merCovering, and space to count the number of mers in a match
414 //
415 merCovering *IL = new merCovering(config._merSize);
416
417 for (uint32 i=firstHit; i<lastHit; i++) {
418 #ifdef WITHOUT_DIAGONALID
419 uint32 thisDiagonalID = _qsLen - _hits[i]._qsPos - 1 + _hits[i]._dsPos;
420 #else
421 uint32 thisDiagonalID = _hits[i]._diagonalID;
422 #endif
423
424
425
426 #if TRACE
427 fprintf(stdout, "hit[qs=%6u ds=%7u d=%7u] box[qs=%6u-%6u ds=%7u-%7u d=%7u-%7u] ",
428 _hits[i]._qsPos,
429 _hits[i]._dsPos,
430 thisDiagonalID,
431 qsLow, qsHigh, dsLow, dsHigh, frstDiagonal, lastDiagonal);
432 #endif
433
434 // Unconditionally extend if the diagonal difference is small.
435 //
436 if (lastDiagonal + config._maxDiagonal >= thisDiagonalID) {
437 lastDiagonal = thisDiagonalID;
438 if (qsLow > _hits[i]._qsPos) qsLow = _hits[i]._qsPos;
439 if (qsHigh < _hits[i]._qsPos) qsHigh = _hits[i]._qsPos;
440 if (dsLow > _hits[i]._dsPos) dsLow = _hits[i]._dsPos;
441 if (dsHigh < _hits[i]._dsPos) dsHigh = _hits[i]._dsPos;
442 IL->addMer(_hits[i]._qsPos);
443 #if TRACE
444 fprintf(stdout, "extend qs=%9u-%9u ds=%9u-%9u diag=%9u-%9u (diagonal)\n",
445 qsLow, qsHigh, dsLow, dsHigh, frstDiagonal, lastDiagonal);
446 #endif
447 continue;
448 }
449
450
451 // XXX: Prototype for extending only if the next hit is near
452 // the last hit.
453 //
454 if (((dsHigh <= _hits[i]._dsPos) && (_hits[i]._dsPos - dsHigh <= config._maxIntronLength)) ||
455 ((dsHigh >= _hits[i]._dsPos) && (dsHigh - _hits[i]._dsPos <= config._maxIntronLength))) {
456
457 // Extend into multiple-exon like things only if the input
458 // sequence is long.
459 //
460 if (_qsLen > config._smallSequenceCutoff) {
461
462 // Extend if the qsOverlap is small (or nonexistant)
463 //
464 if ((qsHigh + config._merSize) < (_hits[i]._qsPos + config._qsOverlap)) {
465 lastDiagonal = thisDiagonalID;
466 if (qsLow > _hits[i]._qsPos) qsLow = _hits[i]._qsPos;
467 if (qsHigh < _hits[i]._qsPos) qsHigh = _hits[i]._qsPos;
468 if (dsLow > _hits[i]._dsPos) dsLow = _hits[i]._dsPos;
469 if (dsHigh < _hits[i]._dsPos) dsHigh = _hits[i]._dsPos;
470 IL->addMer(_hits[i]._qsPos);
471 #if TRACE
472 fprintf(stdout, "extend qs=%9u-%9u ds=%9u-%9u diag=%9u-%9u (qsOverlap)\n",
473 qsLow, qsHigh, dsLow, dsHigh, frstDiagonal, lastDiagonal);
474 #endif
475 continue;
476 }
477
478 // Extend if the dsOverlap is small (or nonexistant)
479 //
480 if (_hits[i]._dsPos < (dsLow + config._dsOverlap)) {
481 lastDiagonal = thisDiagonalID;
482 if (qsLow > _hits[i]._qsPos) qsLow = _hits[i]._qsPos;
483 if (qsHigh < _hits[i]._qsPos) qsHigh = _hits[i]._qsPos;
484 if (dsLow > _hits[i]._dsPos) dsLow = _hits[i]._dsPos;
485 if (dsHigh < _hits[i]._dsPos) dsHigh = _hits[i]._dsPos;
486 IL->addMer(_hits[i]._qsPos);
487 #if TRACE
488 fprintf(stdout, "extend qs=%9u-%9u ds=%9u-%9u diag=%9u-%9u (dsOverlap)\n",
489 qsLow, qsHigh, dsLow, dsHigh, frstDiagonal, lastDiagonal);
490 #endif
491 continue;
492 }
493 }
494 } // XXX: End prototype
495
496 #if TRACE
497 fprintf(stdout, "close current cluster.\nGOOD? qsCov=%u; >= %u or %u? diag: %u < 25?\n",
498 qsHigh - qsLow,
499 minLengthSingle,
500 minLengthMultiple,
501 lastDiagonal - frstDiagonal);
502 #endif
503
504
505 // Save the current cluster and start a new one?
506 //
507 uint32 qCov = IL->sumOfLengths();
508 if ((qCov >= minLengthMultiple) ||
509 ((lastDiagonal - frstDiagonal < 25) && (qCov >= minLengthSingle))) {
510 #if TRACE
511 fprintf(stdout, "add match!\n");
512 #endif
513 addMatch(qsLow,
514 qsHigh + config._merSize,
515 dsLow,
516 dsHigh + config._merSize,
517 IL);
518 IL = new merCovering(config._merSize);
519 }
520
521 if (IL)
522 IL->clear();
523
524 #if TRACE
525 fprintf(stdout, "reset!\n");
526 #endif
527
528 frstDiagonal = thisDiagonalID;
529 lastDiagonal = thisDiagonalID;
530 qsLow = _hits[i]._qsPos;
531 qsHigh = _hits[i]._qsPos;
532 dsLow = _hits[i]._dsPos;
533 dsHigh = _hits[i]._dsPos;
534
535 #if TRACE
536 fprintf(stdout, "hit[qs=%6u ds=%7u d=%7u] box[qs=%6u-%6u ds=%7u-%7u d=%7u-%7u] (initial hit)\n",
537 _hits[i]._qsPos,
538 _hits[i]._dsPos,
539 _qsLen - _hits[i]._qsPos - 1 + _hits[i]._dsPos,
540 qsLow, qsHigh, dsLow, dsHigh, frstDiagonal, lastDiagonal);
541 #endif
542
543 IL->addMer(_hits[i]._qsPos);
544 }
545
546 // Save the final cluster?
547 //
548 uint32 qCov = IL->sumOfLengths();
549 if ((qCov >= minLengthMultiple) ||
550 ((lastDiagonal - frstDiagonal < 21) && (qCov >= minLengthSingle))) {
551 addMatch(qsLow,
552 qsHigh + config._merSize,
553 dsLow,
554 dsHigh + config._merSize,
555 IL);
556 IL = 0;
557 }
558
559 // Delete any remaining IL
560 //
561 delete IL;
562
563
564
565 // Merge and print the matches
566 //
567 trapMatch *n = 0L;
568 uint32 ML = 0;
569
570 while (_matches) {
571
572 // Save the current match, then delete it.
573 //
574 dsLow = _matches->_dsLo;
575 dsHigh = _matches->_dsHi;
576 IL = _matches->_IL;
577 ML = IL->sumOfLengths();
578
579 n = _matches;
580 _matches = _matches->_next;
581 delete n;
582
583 #if TRACE
584 fprintf(stdout, "Merge: %8u %8u\n", dsLow, dsHigh);
585 #endif
586
587 // Assimilate as many of the remaining matches as possible.
588 //
589 // Think of this as first reversing the list, then merging as
590 // long as (dsHigh + 1000 > _matches->_dsLo). But since we
591 // don't reverse the list, we can map:
592 // dsHigh --> _matches->dsHi
593 // _matches->_dsLo --> dsLow
594 // where dsHigh and dsLow are the values for the extended match.
595 //
596 while (_matches && (dsLow < _matches->_dsHi + 5000)) {
597
598 // Combine the two merCoverings
599 //
600 IL->merge(_matches->_IL);
601 ML += _matches->_IL->sumOfLengths();
602
603 // The start of the new match might be after the start of the
604 // merged region. (Only rarely is it before)
605 //
606 if (dsLow > _matches->_dsLo)
607 dsLow = _matches->_dsLo;
608
609 // The end of current match is always greater than the end of the
610 // new match!
611 //
612 //dsHigh = _matches->_dsHi;
613
614 #if TRACE
615 fprintf(stdout, "Merge: %8u %8u -> %8u %8u\n", _matches->_dsLo, _matches->_dsHi, dsLow, dsHigh);
616 #endif
617
618 n = _matches;
619 _matches = _matches->_next;
620 delete n->_IL;
621 delete n;
622 }
623
624
625 if (config._binaryOutput) {
626 aHit a;
627
628 a._forward = !isReverse;
629 a._merged = false;
630 a._qsIdx = _qsIdx;
631 a._dsIdx = config._dbSTREAM->IIDOf(currentSeq);
632 a._dsLo = dsLow;
633 a._dsHi = dsHigh;
634 a._covered = IL->sumOfLengths();
635 a._matched = ML;
636 a._numMers = _qsMers;
637
638 query->addOutput(&a, sizeof(aHit));
639 } else {
640 char line[128];
641
642 sprintf(line, "-%c -e "uint32FMT" -D "uint32FMT" "uint32FMT" "uint32FMT" -M "uint32FMT" "uint32FMT" "uint32FMT"\n",
643 isReverse ? 'r' : 'f', _qsIdx,
644 config._dbSTREAM->IIDOf(currentSeq),
645 dsLow, dsHigh, IL->sumOfLengths(), ML, _qsMers);
646
647 query->addOutput(line, 0);
648 }
649
650 delete IL;
651 }
652
653 // All done with these hits. Move to the next set.
654 //
655 firstHit = lastHit;
656 }
657 }
658