generated from seqan/library-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// SPDX-FileCopyrightText: 2006-2024, Knut Reinert & Freie Universität Berlin | ||
// SPDX-FileCopyrightText: 2016-2024, Knut Reinert & MPI für molekulare Genetik | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
#include <algorithm> | ||
#include <cassert> | ||
|
||
#include <hibf/sketch/minHashes.hpp> | ||
|
||
namespace seqan::hibf::sketch | ||
{ | ||
|
||
minHashes::minHashes(std::vector<uint64_t> const & smallest_values) | ||
{ | ||
table.resize(num_sketches); | ||
|
||
for (auto & elem : table) | ||
elem.reserve(sketch_size); | ||
|
||
for (uint64_t const hash : smallest_values) | ||
{ | ||
auto & hash_table = table[hash & register_id_mask]; | ||
if (hash_table.size() < sketch_size) | ||
hash_table.push_back(hash >> 4); | ||
} | ||
} | ||
|
||
bool minHashes::is_valid() const | ||
{ | ||
return table.size() == num_sketches | ||
&& std::ranges::all_of(table, | ||
[](auto const & minHash_sketch) | ||
{ | ||
return minHash_sketch.size() == sketch_size; | ||
}); | ||
} | ||
|
||
void minHashes::fill_up_sketches(std::span<uint64_t> const & more_smallest_values) | ||
{ | ||
for (uint64_t const hash : more_smallest_values) | ||
{ | ||
auto & hash_table = table[hash & register_id_mask]; | ||
assert(std::ranges::find(hash_table, hash) == hash_table.end()); // hashes should be unique | ||
if (hash_table.size() < sketch_size) | ||
hash_table.push_back(hash >> 4); | ||
} | ||
} | ||
|
||
void minHashes::update_heap_with(std::vector<uint64_t> & heap, uint64_t const k_hash) | ||
{ | ||
// Do nothing if k_hash is bigger than the current biggest element in the (max) heap. | ||
if (k_hash >= heap[0]) | ||
return; | ||
|
||
// we do not need a guard (hash table) to check for duplications because `kmers` is already a set | ||
std::ranges::pop_heap(heap); // max elements move to end of vector | ||
heap.back() = k_hash; // replace last elements instead of physically popping and pushing | ||
std::ranges::push_heap(heap); // last elements is rearranged in the heap to be pushed | ||
} | ||
|
||
} // namespace seqan::hibf::sketch |