| 1 |
#include <pbcopper/algorithm/KMerIndex.h> |
| 2 |
#include <pbcopper/pbmer/DnaBit.h> |
| 3 |
#include <pbcopper/utility/Random.h> |
| 4 |
#include <pbcopper/utility/Ssize.h> |
| 5 |
|
| 6 |
#include <gtest/gtest.h> |
| 7 |
|
| 8 |
#include <algorithm> |
| 9 |
#include <iostream> |
| 10 |
#include <random> |
| 11 |
|
| 12 |
using namespace PacBio; |
| 13 |
using namespace Algorithm; |
| 14 |
|
| 15 |
// clang-format off |
| 16 |
|
| 17 |
template<typename IndexType> |
| 18 |
void TestKMerIndex(int bottomKValue=-1) { |
| 19 |
static constexpr int NUMBER_OF_FOUNDERS = 2500; |
| 20 |
constexpr int K = 31; |
| 21 |
std::vector<uint64_t> kmerFounders(NUMBER_OF_FOUNDERS); |
| 22 |
uint64_t seed = 0; |
| 23 |
const uint64_t kmerMask = uint64_t(-1) >> ((32 - K) * 2); |
| 24 |
for(uint64_t& x: kmerFounders) { |
| 25 |
x = Utility::WyHash64Step(seed) & kmerMask; |
| 26 |
} |
| 27 |
const std::vector<SubMerSelection> submerSelection{SubMerSelection(K, 0x3fffffff)}; |
| 28 |
Algorithm::KMerIndex index(K, submerSelection, bottomKValue); |
| 29 |
index.Insert(kmerFounders.begin(), kmerFounders.end()); |
| 30 |
EXPECT_EQ(index.IsSorted(), true); |
| 31 |
{ |
| 32 |
Algorithm::KMerIndex index2(K, submerSelection, bottomKValue); |
| 33 |
index2.Insert(kmerFounders); |
| 34 |
EXPECT_EQ(index, index2); |
| 35 |
} |
| 36 |
constexpr int NUM_CHOICES = 128; |
| 37 |
for(int i = 0; i < NUM_CHOICES; ++i) { |
| 38 |
const int pos = Utility::WyHash64Step(seed) % kmerFounders.size(); |
| 39 |
uint64_t kmer = kmerFounders[pos]; |
| 40 |
auto ret = index.Query(kmer); |
| 41 |
EXPECT_EQ(static_cast<int>(ret.front().first), pos); |
| 42 |
} |
| 43 |
for(const int numThreads: {8, 16}) { |
| 44 |
Algorithm::KMerIndex parallelIndex(K, submerSelection, bottomKValue); |
| 45 |
parallelIndex.InsertParallel(kmerFounders.begin(), kmerFounders.end(), numThreads); |
| 46 |
parallelIndex.Sort(); |
| 47 |
EXPECT_EQ(parallelIndex.IsSorted(), true); |
| 48 |
EXPECT_EQ(parallelIndex, index); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
template<typename IndexType> |
| 53 |
void TestFlexibleIndex(int bottomKValue=-1) { |
| 54 |
static constexpr int NUMBER_OF_FOUNDERS = 2500; |
| 55 |
constexpr int K = 31; |
| 56 |
constexpr int SUBK = 17; |
| 57 |
constexpr int NUM_SPACINGS = 32; |
| 58 |
using Algorithm::SubMerSelection; |
| 59 |
std::vector<uint64_t> kmerFounders(NUMBER_OF_FOUNDERS); |
| 60 |
uint64_t seed = 0; |
| 61 |
const uint64_t kmerMask = uint64_t(-1) >> ((32 - K) * 2); |
| 62 |
for(uint64_t& x: kmerFounders) { |
| 63 |
x = Utility::WyHash64Step(seed) & kmerMask; |
| 64 |
} |
| 65 |
std::vector<SubMerSelection> submerSelection{Algorithm::FlatSubMer(K, SUBK)}; |
| 66 |
{ |
| 67 |
const auto submers = Algorithm::GenerateRandomSubsequences(K, SUBK, NUM_SPACINGS); |
| 68 |
submerSelection.insert(submerSelection.end(), submers.begin(), submers.end()); |
| 69 |
} |
| 70 |
// Uniquify |
| 71 |
std::sort(submerSelection.begin(), submerSelection.end()); |
| 72 |
submerSelection.erase(std::unique(submerSelection.begin(), submerSelection.end()), submerSelection.end()); |
| 73 |
Algorithm::KMerIndex index(K, submerSelection, bottomKValue); |
| 74 |
index.Insert(kmerFounders.begin(), kmerFounders.end()); |
| 75 |
int numNotFound = 0; |
| 76 |
constexpr int NUM_CHOICES = 128; |
| 77 |
constexpr int MAX_NEIGHBORS = 10; |
| 78 |
for(int i = 0; i < NUM_CHOICES; ++i) { |
| 79 |
const int pos = Utility::WyHash64Step(seed) % kmerFounders.size(); |
| 80 |
const uint64_t kmer = kmerFounders[pos]; |
| 81 |
auto ret = index.Query(kmer); |
| 82 |
EXPECT_EQ(static_cast<int>(ret.front().first), pos); |
| 83 |
int nNeighborsQueried = 0; |
| 84 |
for(const auto neighbor: Pbmer::DnaBit(kmer, 0, K).Neighbors()) { |
| 85 |
auto nret = index.Query(neighbor.mer); |
| 86 |
if(nret.empty()) { |
| 87 |
++numNotFound; |
| 88 |
} else if(nret.size() == 1) { |
| 89 |
EXPECT_EQ(static_cast<int>(nret.front().first), pos); |
| 90 |
} else { |
| 91 |
const bool inList = std::find_if(nret.begin(), nret.end(), [pos](auto x) {return int(x.first) == pos;}) != nret.end(); |
| 92 |
EXPECT_EQ(inList, true); |
| 93 |
} |
| 94 |
if(++nNeighborsQueried == MAX_NEIGHBORS) { |
| 95 |
break; |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
EXPECT_EQ(numNotFound, 0); |
| 100 |
} |
| 101 |
|
| 102 |
TEST(Algorithm_kmer_index, kmer_index_construction_and_accuracy32_exact) { |
| 103 |
for(const int bk: {137, 731, -1}) { |
| 104 |
TestKMerIndex<Algorithm::KMerIndex>(bk); |
| 105 |
TestKMerIndex<Algorithm::KMerLSH32>(bk); |
| 106 |
TestKMerIndex<Algorithm::KMerLSH32_64>(bk); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
TEST(Algorithm_kmer_index, kmer_index_construction_and_accuracy64_exact) { |
| 111 |
for(const int bk: {137, 731, -1}) { |
| 112 |
TestKMerIndex<Algorithm::KMerIndex>(bk); |
| 113 |
TestKMerIndex<Algorithm::KMerLSH64>(bk); |
| 114 |
TestKMerIndex<Algorithm::KMerLSH64_32>(bk); |
| 115 |
} |
| 116 |
} |
| 117 |
TEST(Algorithm_kmer_index, kmer_index_construction_and_accuracy32_flexible) { |
| 118 |
for(const int bk: {137, 731, -1}) { |
| 119 |
TestFlexibleIndex<Algorithm::KMerIndex>(bk); |
| 120 |
TestFlexibleIndex<Algorithm::KMerLSH32>(bk); |
| 121 |
TestFlexibleIndex<Algorithm::KMerLSH32_64>(bk); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
TEST(Algorithm_kmer_index, kmer_index_construction_and_accuracy64_flexible) { |
| 126 |
for(const int bk: {137, 731, -1}) { |
| 127 |
TestFlexibleIndex<Algorithm::KMerIndex>(bk); |
| 128 |
TestFlexibleIndex<Algorithm::KMerLSH64>(bk); |
| 129 |
TestFlexibleIndex<Algorithm::KMerLSH64_64>(bk); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
template<typename Index> |
| 134 |
void TestThrowKGreaterThan32() |
| 135 |
{ |
| 136 |
auto func = [&]() { |
| 137 |
Index index(73, Algorithm::FlatSubMer(73, 31)); |
| 138 |
}; |
| 139 |
EXPECT_THROW(func(), std::invalid_argument); |
| 140 |
} |
| 141 |
|
| 142 |
TEST(Algorithm_kmer_index, kmer_index_throws_if_k_gt32) { |
| 143 |
TestThrowKGreaterThan32<Algorithm::KMerIndex>(); |
| 144 |
TestThrowKGreaterThan32<Algorithm::KMerLSH64>(); |
| 145 |
TestThrowKGreaterThan32<Algorithm::KMerLSH64_32>(); |
| 146 |
TestThrowKGreaterThan32<Algorithm::KMerLSH32>(); |
| 147 |
TestThrowKGreaterThan32<Algorithm::KMerLSH32_32>(); |
| 148 |
} |