| 1 |
#ifndef EXISTDB_H |
| 2 |
#define EXISTDB_H |
| 3 |
|
| 4 |
// Used by wgs-assembler, to determine if a rather serious bug was patched. |
| 5 |
#define EXISTDB_H_VERSION 1960 |
| 6 |
|
| 7 |
#include "bio++.H" |
| 8 |
|
| 9 |
// Takes as input a list of mers (in a file) and builds a searchable |
| 10 |
// structure listing those mers. Duplicate mers are not removed and |
| 11 |
// will be stored multiple times. |
| 12 |
// |
| 13 |
// Using a compressed hash is allowed, but somewhat useless -- it is |
| 14 |
// really slow and doesn't save that much. |
| 15 |
// |
| 16 |
// If existDBcanonical is requested, this will store only the |
| 17 |
// canonical mer. It is up to the client to be sure that is |
| 18 |
// appropriate! See positionDB.H for more. |
| 19 |
|
| 20 |
//#define STATS |
| 21 |
|
| 22 |
typedef uint32 existDBflags; |
| 23 |
const existDBflags existDBnoFlags = 0x0000; |
| 24 |
const existDBflags existDBcompressHash = 0x0001; |
| 25 |
const existDBflags existDBcompressBuckets = 0x0002; |
| 26 |
const existDBflags existDBcompressCounts = 0x0004; |
| 27 |
const existDBflags existDBcanonical = 0x0008; |
| 28 |
const existDBflags existDBforward = 0x0010; |
| 29 |
const existDBflags existDBcounts = 0x0020; |
| 30 |
|
| 31 |
class existDB { |
| 32 |
public: |
| 33 |
|
| 34 |
// Read state from an existDB file |
| 35 |
existDB(char const *filename, |
| 36 |
bool loadData=true); |
| 37 |
|
| 38 |
// Load mers from an existing existDB file, a fastafile, or a meryl database |
| 39 |
existDB(char const *filename, |
| 40 |
uint32 merSize, |
| 41 |
existDBflags flags, |
| 42 |
uint32 lo, |
| 43 |
uint32 hi); |
| 44 |
|
| 45 |
// Load mers from a character string |
| 46 |
existDB(char const *sequence, |
| 47 |
uint32 merSize, |
| 48 |
existDBflags flags); |
| 49 |
|
| 50 |
~existDB(); |
| 51 |
|
| 52 |
void saveState(char const *filename); |
| 53 |
|
| 54 |
void printState(FILE *stream); |
| 55 |
|
| 56 |
bool isForward(void) { return(_isForward); }; |
| 57 |
bool isCanonical(void) { return(_isCanonical); }; |
| 58 |
|
| 59 |
bool exists(uint64 mer); |
| 60 |
uint64 count(uint64 mer); |
| 61 |
|
| 62 |
private: |
| 63 |
bool loadState(char const *filename, bool beNoisy=false, bool loadData=true); |
| 64 |
bool createFromFastA(char const *filename, |
| 65 |
uint32 merSize, |
| 66 |
uint32 flags); |
| 67 |
bool createFromMeryl(char const *filename, |
| 68 |
uint32 merSize, |
| 69 |
uint32 lo, |
| 70 |
uint32 hi, |
| 71 |
uint32 flags); |
| 72 |
bool createFromSequence(char const *sequence, |
| 73 |
uint32 merSize, |
| 74 |
uint32 flags); |
| 75 |
|
| 76 |
uint64 HASH(uint64 k) { |
| 77 |
return(((k >> _shift1) ^ (k >> _shift2) ^ k) & _mask1); |
| 78 |
}; |
| 79 |
|
| 80 |
uint64 CHECK(uint64 k) { |
| 81 |
return(k & _mask2); |
| 82 |
}; |
| 83 |
|
| 84 |
void insertMer(uint64 hsh, uint64 chk, uint64 cnt, uint64 *countingTable) { |
| 85 |
|
| 86 |
// If the mer is already here, just update the count. This only |
| 87 |
// works if not _compressedBucket, and only makes sense for loading from |
| 88 |
// fasta or sequence. |
| 89 |
|
| 90 |
if ((_compressedBucket == false) && |
| 91 |
(_searchForDupe)) { |
| 92 |
uint64 st = _hashTable[hsh]; |
| 93 |
uint64 ed = countingTable[hsh]; |
| 94 |
|
| 95 |
for (; st<ed; st++) { |
| 96 |
if (_buckets[st] == chk) { |
| 97 |
if (_counts) |
| 98 |
_counts[st] += cnt; |
| 99 |
return; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
if (_compressedBucket) |
| 105 |
setDecodedValue(_buckets, countingTable[hsh] * _chkWidth, _chkWidth, chk); |
| 106 |
else |
| 107 |
_buckets[countingTable[hsh]] = chk; |
| 108 |
|
| 109 |
if (_counts) { |
| 110 |
if (_compressedCounts) { |
| 111 |
setDecodedValue(_counts, countingTable[hsh] * _cntWidth, _cntWidth, cnt); |
| 112 |
} else { |
| 113 |
_counts[countingTable[hsh]] = cnt; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
countingTable[hsh]++; |
| 118 |
}; |
| 119 |
|
| 120 |
bool _compressedHash; |
| 121 |
bool _compressedBucket; |
| 122 |
bool _compressedCounts; |
| 123 |
bool _isForward; |
| 124 |
bool _isCanonical; |
| 125 |
|
| 126 |
bool _searchForDupe; |
| 127 |
|
| 128 |
uint32 _merSizeInBases; |
| 129 |
|
| 130 |
uint32 _shift1; |
| 131 |
uint32 _shift2; |
| 132 |
uint64 _mask1; |
| 133 |
uint64 _mask2; |
| 134 |
|
| 135 |
uint32 _hshWidth; // Only for the compressed hash |
| 136 |
uint32 _chkWidth; // Only for the compressed bucket |
| 137 |
uint32 _cntWidth; // Only for the compressed counts |
| 138 |
|
| 139 |
uint64 _hashTableWords; |
| 140 |
uint64 _bucketsWords; |
| 141 |
uint64 _countsWords; |
| 142 |
|
| 143 |
uint64 *_hashTable; |
| 144 |
uint64 *_buckets; |
| 145 |
uint64 *_counts; |
| 146 |
|
| 147 |
void clear(void) { |
| 148 |
}; |
| 149 |
}; |
| 150 |
|
| 151 |
#endif // EXISTDB_H |