| 1 | #ifndef HITMATRIX_H |
| 2 | #define HITMATRIX_H |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <new> |
| 7 | |
| 8 | #include "bio++.H" |
| 9 | #include "positionDB.H" |
| 10 | #include "encodedQuery.H" |
| 11 | |
| 12 | // Define this to cut the space required for storing hits by 1/3 -- |
| 13 | // from 12 byyes to 8 bytes -- at a slight computational expense -- |
| 14 | // negligible on real hardware, I hope. |
| 15 | // |
| 16 | // The original definition of diagonalID was |
| 17 | // qsLen - qsPos - 1 + dsPos |
| 18 | // but qsLen is fixed for everyone, so we could reduce it to |
| 19 | // dsPos - qsPos |
| 20 | // but that's not unsigned. |
| 21 | // |
| 22 | // Results: on a human mapping, using chromosomes as the stream and |
| 23 | // the whole human as the table (so we need to actually store a large |
| 24 | // number of hits), we see a savings of 2GB and a small drop in |
| 25 | // runtime. Process size went from 20.7GB to 18.7GB, CPU time from |
| 26 | // 20578 to 20193 seconds (833MHz EV6.8AL (21264B)). |
| 27 | // |
| 28 | #define WITHOUT_DIAGONALID |
| 29 | |
| 30 | |
| 31 | struct diagonalLine { |
| 32 | uint32 _qsPos; |
| 33 | uint32 _dsPos; |
| 34 | #ifndef WITHOUT_DIAGONALID |
| 35 | uint32 _diagonalID; |
| 36 | #endif |
| 37 | }; |
| 38 | |
| 39 | class trapMatch { |
| 40 | public: |
| 41 | uint32 _qsLo, _qsHi; |
| 42 | uint32 _dsLo, _dsHi; |
| 43 | merCovering *_IL; |
| 44 | trapMatch *_next; |
| 45 | |
| 46 | trapMatch(uint32 qsLo, |
| 47 | uint32 qsHi, |
| 48 | uint32 dsLo, |
| 49 | uint32 dsHi, |
| 50 | merCovering *IL) { |
| 51 | _qsLo = qsLo; |
| 52 | _qsHi = qsHi; |
| 53 | _dsLo = dsLo; |
| 54 | _dsHi = dsHi; |
| 55 | _IL = IL; |
| 56 | _next = 0L; |
| 57 | }; |
| 58 | }; |
| 59 | |
| 60 | class hitMatrix { |
| 61 | public: |
| 62 | hitMatrix(uint32 qsLen, |
| 63 | uint32 qsMers, |
| 64 | uint32 qsIdx); |
| 65 | ~hitMatrix(); |
| 66 | |
| 67 | void addHits(uint32 qi, |
| 68 | uint64 *ps, |
| 69 | uint64 cn); |
| 70 | |
| 71 | void sort_diagonal(void); |
| 72 | void sort_dsPos(void); |
| 73 | |
| 74 | void filter(encodedQuery *query, bool isReverse); |
| 75 | private: |
| 76 | uint32 _qsLen; // Seq Len of Q |
| 77 | uint32 _qsMers; // Valid mers in Q |
| 78 | uint32 _qsIdx; // Index of Q in the FastA |
| 79 | |
| 80 | // Instead of building the lines during add(), we store |
| 81 | // the information used to build lines, and then build them |
| 82 | // in chain(). This was done to reduce simultaneous memory |
| 83 | // usage, as the lineArrayMap and etc take up considerable space. |
| 84 | // |
| 85 | uint32 _hitsLen; |
| 86 | uint32 _hitsMax; |
| 87 | diagonalLine *_hits; |
| 88 | |
| 89 | |
| 90 | // Making sense of the raw output from the search is not a trivial |
| 91 | // task for perl. SMALL searches (dbEST vs 0.5MB sequence) used more |
| 92 | // than 4GB of memory in perl. |
| 93 | // |
| 94 | // So, we bite the bullet and do it here. |
| 95 | // |
| 96 | // _matches is a sorted linked list of the regions we have found. |
| 97 | // The list is kept in REVERSE order, as we usually add regions |
| 98 | // in the correct order (correct reverse order), occasionally |
| 99 | // we need to swap the last two. |
| 100 | // |
| 101 | // The list is deleted in filter() |
| 102 | // |
| 103 | trapMatch *_matches; |
| 104 | |
| 105 | void addMatch(uint32 qsLo, |
| 106 | uint32 qsHi, |
| 107 | uint32 dsLo, |
| 108 | uint32 dsHi, |
| 109 | merCovering *IL); |
| 110 | |
| 111 | }; |
| 112 | |
| 113 | |
| 114 | inline |
| 115 | void |
| 116 | hitMatrix::addHits(uint32 qi, |
| 117 | uint64 *ps, |
| 118 | uint64 cn) { |
| 119 | |
| 120 | if ((_hitsLen + cn) >= _hitsMax) { |
| 121 | _hitsMax = _hitsMax + _hitsMax + (uint32)cn; |
| 122 | |
| 123 | diagonalLine *h; |
| 124 | try { |
| 125 | h = new diagonalLine [_hitsMax]; |
| 126 | } catch (std::bad_alloc) { |
| 127 | fprintf(stderr, "hitMatrix::addHits()-- caught std::bad_alloc in %s at line %d.\n", __FILE__, __LINE__); |
| 128 | fprintf(stderr, "hitMatrix::addHits()-- have "uint32FMT" hits, tried to add "uint64FMT" more\n", _hitsLen, cn); |
| 129 | exit(1); |
| 130 | } |
| 131 | |
| 132 | for (uint32 z=_hitsLen; z--; ) { |
| 133 | h[z]._qsPos = _hits[z]._qsPos; |
| 134 | h[z]._dsPos = _hits[z]._dsPos; |
| 135 | #ifndef WITHOUT_DIAGONALID |
| 136 | h[z]._diagonalID = _hits[z]._diagonalID; |
| 137 | #endif |
| 138 | } |
| 139 | |
| 140 | delete [] _hits; |
| 141 | |
| 142 | _hits = h; |
| 143 | } |
| 144 | |
| 145 | for (uint64 i=0; i<cn; i++) { |
| 146 | _hits[_hitsLen]._qsPos = (uint32)(qi); |
| 147 | _hits[_hitsLen]._dsPos = (uint32)(ps[i]); |
| 148 | #ifndef WITHOUT_DIAGONALID |
| 149 | _hits[_hitsLen]._diagonalID = (uint32)(_qsLen - qi - 1 + ps[i]); |
| 150 | #endif |
| 151 | _hitsLen++; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | |
| 156 | #endif // HITMATRIX_H |