| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
#include <errno.h> |
| 5 |
#include "existDB.H" |
| 6 |
#include "bio++.H" |
| 7 |
|
| 8 |
|
| 9 |
existDB::existDB(char const *filename, |
| 10 |
bool loadData) { |
| 11 |
clear(); |
| 12 |
|
| 13 |
_compressedHash = false; |
| 14 |
_compressedBucket = false; |
| 15 |
|
| 16 |
if (loadState(filename, true, loadData) == false) { |
| 17 |
fprintf(stderr, "existDB::existDB()-- Tried to read state from '%s', but failed.\n", filename); |
| 18 |
exit(1); |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
existDB::existDB(char const *filename, |
| 24 |
uint32 merSize, |
| 25 |
existDBflags flags, |
| 26 |
uint32 lo, |
| 27 |
uint32 hi) { |
| 28 |
clear(); |
| 29 |
|
| 30 |
_compressedHash = flags & existDBcompressHash; |
| 31 |
_compressedBucket = flags & existDBcompressBuckets; |
| 32 |
_compressedCounts = flags & existDBcompressCounts; |
| 33 |
|
| 34 |
_searchForDupe = false; |
| 35 |
|
| 36 |
// Try to read state from the filename. If successful, make sure |
| 37 |
// that the merSize is correct. |
| 38 |
// |
| 39 |
if (loadState(filename)) { |
| 40 |
bool fail = false; |
| 41 |
|
| 42 |
if (_merSizeInBases != merSize) { |
| 43 |
fprintf(stderr, "existDB::existDB()-- Read state from '%s', but got different mer sizes\n", filename); |
| 44 |
fprintf(stderr, "existDB::existDB()-- Got "uint32FMT", expected "uint32FMT"\n", _merSizeInBases, merSize); |
| 45 |
fail = true; |
| 46 |
} |
| 47 |
|
| 48 |
if (fail) |
| 49 |
exit(1); |
| 50 |
|
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
// If no direction flags are set, set the default direction of |
| 55 |
// forward. Stupid precedence rules. |
| 56 |
// |
| 57 |
if ((flags & (existDBcanonical | existDBforward)) == uint32ZERO) |
| 58 |
flags |= existDBforward; |
| 59 |
|
| 60 |
// If we can open 'filename' for reading, then we assume the file |
| 61 |
// is a multi-fasta, and we build an existDB/ |
| 62 |
// |
| 63 |
// Otherwise, we assume that 'filename' is really the prefix for a |
| 64 |
// meryl database. |
| 65 |
|
| 66 |
|
| 67 |
if (fileExists(filename)) |
| 68 |
createFromFastA(filename, merSize, flags); |
| 69 |
else |
| 70 |
createFromMeryl(filename, merSize, lo, hi, flags); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
existDB::existDB(char const *sequence, |
| 75 |
uint32 merSize, |
| 76 |
existDBflags flags) { |
| 77 |
clear(); |
| 78 |
|
| 79 |
_compressedHash = flags & existDBcompressHash; |
| 80 |
_compressedBucket = flags & existDBcompressBuckets; |
| 81 |
_compressedCounts = flags & existDBcompressCounts; |
| 82 |
|
| 83 |
if ((flags & (existDBcanonical | existDBforward)) == uint32ZERO) |
| 84 |
flags |= existDBforward; |
| 85 |
|
| 86 |
createFromSequence(sequence, merSize, flags); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
existDB::~existDB() { |
| 91 |
delete [] _hashTable; |
| 92 |
delete [] _buckets; |
| 93 |
delete [] _counts; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
bool |
| 101 |
existDB::exists(uint64 mer) { |
| 102 |
uint64 c, h, st, ed; |
| 103 |
|
| 104 |
if (_compressedHash) { |
| 105 |
h = HASH(mer) * _hshWidth; |
| 106 |
st = getDecodedValue(_hashTable, h, _hshWidth); |
| 107 |
ed = getDecodedValue(_hashTable, h + _hshWidth, _hshWidth); |
| 108 |
} else { |
| 109 |
h = HASH(mer); |
| 110 |
st = _hashTable[h]; |
| 111 |
ed = _hashTable[h+1]; |
| 112 |
} |
| 113 |
|
| 114 |
if (st == ed) |
| 115 |
return(false); |
| 116 |
|
| 117 |
c = CHECK(mer); |
| 118 |
|
| 119 |
if (_compressedBucket) { |
| 120 |
st *= _chkWidth; |
| 121 |
ed *= _chkWidth; |
| 122 |
|
| 123 |
for (; st<ed; st += _chkWidth) { |
| 124 |
if (getDecodedValue(_buckets, st, _chkWidth) == c) |
| 125 |
return(true); |
| 126 |
} |
| 127 |
} else { |
| 128 |
for (; st<ed; st++) { |
| 129 |
if (_buckets[st] == c) |
| 130 |
return(true); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
return(false); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
uint64 |
| 139 |
existDB::count(uint64 mer) { |
| 140 |
uint64 c, h, st, ed; |
| 141 |
|
| 142 |
if (_counts == 0L) |
| 143 |
return(0); |
| 144 |
|
| 145 |
if (_compressedHash) { |
| 146 |
h = HASH(mer) * _hshWidth; |
| 147 |
st = getDecodedValue(_hashTable, h, _hshWidth); |
| 148 |
ed = getDecodedValue(_hashTable, h + _hshWidth, _hshWidth); |
| 149 |
} else { |
| 150 |
h = HASH(mer); |
| 151 |
st = _hashTable[h]; |
| 152 |
ed = _hashTable[h+1]; |
| 153 |
} |
| 154 |
|
| 155 |
if (st == ed) |
| 156 |
return(0); |
| 157 |
|
| 158 |
c = CHECK(mer); |
| 159 |
|
| 160 |
if (_compressedBucket) { |
| 161 |
st *= _chkWidth; |
| 162 |
ed *= _chkWidth; |
| 163 |
|
| 164 |
for (; st<ed; st += _chkWidth) { |
| 165 |
if (getDecodedValue(_buckets, st, _chkWidth) == c) |
| 166 |
goto returncount; |
| 167 |
} |
| 168 |
} else { |
| 169 |
for (; st<ed; st++) { |
| 170 |
if (_buckets[st] == c) |
| 171 |
goto returncount; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
return(0); |
| 176 |
|
| 177 |
returncount: |
| 178 |
if (_compressedCounts) |
| 179 |
return(getDecodedValue(_counts, st * _cntWidth, _cntWidth)); |
| 180 |
else |
| 181 |
return(_counts[st]); |
| 182 |
} |