| 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 "filterObj.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 | |
| 40 | class hitMatrix { |
| 41 | public: |
| 42 | hitMatrix(uint32 qsLen, |
| 43 | uint32 qsIdx); |
| 44 | ~hitMatrix(); |
| 45 | |
| 46 | void addHits(uint32 qi, |
| 47 | uint64 *ps, |
| 48 | uint64 cn); |
| 49 | |
| 50 | void sort_diagonal(void); |
| 51 | void sort_dsPos(void); |
| 52 | |
| 53 | void processMatrix(char direction, filterObj *FO); |
| 54 | |
| 55 | private: |
| 56 | uint32 _qsLen; // Seq Len of Q |
| 57 | uint32 _qsIdx; // Index of Q in the FastA |
| 58 | |
| 59 | // Instead of building the lines during add(), we store |
| 60 | // the information used to build lines, and then build them |
| 61 | // in chain(). This was done to reduce simultaneous memory |
| 62 | // usage, as the lineArrayMap and etc take up considerable space. |
| 63 | // |
| 64 | uint32 _hitsLen; |
| 65 | uint32 _hitsMax; |
| 66 | diagonalLine *_hits; |
| 67 | }; |
| 68 | |
| 69 | |
| 70 | inline |
| 71 | void |
| 72 | hitMatrix::addHits(uint32 qi, |
| 73 | uint64 *ps, |
| 74 | uint64 cn) { |
| 75 | |
| 76 | if ((_hitsLen + cn) >= _hitsMax) { |
| 77 | _hitsMax = _hitsMax + _hitsMax + (uint32)cn; |
| 78 | |
| 79 | diagonalLine *h; |
| 80 | try { |
| 81 | h = new diagonalLine [_hitsMax]; |
| 82 | } catch (std::bad_alloc) { |
| 83 | fprintf(stderr, "hitMatrix::addHits()-- caught std::bad_alloc in %s at line %d.\n", __FILE__, __LINE__); |
| 84 | fprintf(stderr, "hitMatrix::addHits()-- have "uint32FMT" hits, tried to add "uint64FMT" more\n", _hitsLen, cn); |
| 85 | exit(1); |
| 86 | } |
| 87 | |
| 88 | for (uint32 z=_hitsLen; z--; ) { |
| 89 | h[z]._qsPos = _hits[z]._qsPos; |
| 90 | h[z]._dsPos = _hits[z]._dsPos; |
| 91 | #ifndef WITHOUT_DIAGONALID |
| 92 | h[z]._diagonalID = _hits[z]._diagonalID; |
| 93 | #endif |
| 94 | } |
| 95 | |
| 96 | delete [] _hits; |
| 97 | |
| 98 | _hits = h; |
| 99 | } |
| 100 | |
| 101 | for (uint64 i=0; i<cn; i++) { |
| 102 | _hits[_hitsLen]._qsPos = (uint32)(qi); |
| 103 | _hits[_hitsLen]._dsPos = (uint32)(ps[i]); |
| 104 | #ifndef WITHOUT_DIAGONALID |
| 105 | _hits[_hitsLen]._diagonalID = (uint32)(_qsLen - qi - 1 + ps[i]); |
| 106 | #endif |
| 107 | _hitsLen++; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | |
| 112 | #endif // HITMATRIX_H |