digest
Loading...
Searching...
No Matches
mod_minimizer.hpp
1#ifndef MOD_MINIMIZER_HPP
2#define MOD_MINIMIZER_HPP
3
4#include "digest/digester.hpp"
5#include <cassert>
6#include <cstdint>
7
8namespace digest {
9
16class BadModException : public std::exception {
17 const char *what() const throw() {
18 return "mod must be greater than congruence.";
19 }
20};
21
30template <BadCharPolicy P> class ModMin : public Digester<P> {
31 public:
46 ModMin(const char *seq, size_t len, unsigned k, uint32_t mod,
47 uint32_t congruence = 0, size_t start = 0,
49 : Digester<P>(seq, len, k, start, minimized_h), mod(mod),
50 congruence(congruence) {
51 if (congruence >= mod) {
52 throw BadModException();
53 }
54 }
55
68 ModMin(const std::string &seq, unsigned k, uint32_t mod,
69 uint32_t congruence = 0, size_t start = 0,
71 : ModMin<P>(seq.c_str(), seq.size(), k, mod, congruence, start,
72 minimized_h) {}
73
82 void roll_minimizer(unsigned amount, std::vector<uint32_t> &vec) override {
83 if (!this->is_valid_hash)
84 return;
85
87 do {
88 if ((uint32_t)this->chash % mod == congruence) {
89 vec.emplace_back(this->get_pos());
90 }
91 } while (this->roll_one() && vec.size() < amount);
92 return;
93 }
94
96 do {
97 if ((uint32_t)this->fhash % mod == congruence) {
98 vec.emplace_back(this->get_pos());
99 }
100 } while (this->roll_one() && vec.size() < amount);
101 return;
102 }
103
104 // reverse
105 do {
106 if ((uint32_t)this->rhash % mod == congruence) {
107 vec.emplace_back(this->get_pos());
108 }
109 } while (this->roll_one() && vec.size() < amount);
110 }
111
120 void
121 roll_minimizer(unsigned amount,
122 std::vector<std::pair<uint32_t, uint32_t>> &vec) override {
123 if (!this->is_valid_hash)
124 return;
125
127 do {
128 if ((uint32_t)this->chash % mod == congruence) {
129 vec.emplace_back(this->get_pos(), this->chash);
130 }
131 } while (this->roll_one() && vec.size() < amount);
132 return;
133 }
134
136 do {
137 if ((uint32_t)this->fhash % mod == congruence) {
138 vec.emplace_back(this->get_pos(), this->fhash);
139 }
140 } while (this->roll_one() && vec.size() < amount);
141 return;
142 }
143
144 // reverse
145 do {
146 if ((uint32_t)this->rhash % mod == congruence) {
147 vec.emplace_back(this->get_pos(), this->rhash);
148 }
149 } while (this->roll_one() && vec.size() < amount);
150 }
151
155 uint32_t get_mod() { return mod; }
156
160 uint32_t get_congruence() { return congruence; }
161
162 private:
163 uint32_t mod;
164 uint32_t congruence;
165};
166
167} // namespace digest
168
169#endif // MOD_MINIMIZER_HPP
Exception thrown when initializing a mod minimizer object where the target value after modding is gre...
Definition mod_minimizer.hpp:16
an abstract class for Digester objects.
Definition digester.hpp:75
bool roll_one()
moves the internal pointer to the next valid k-mer. Time Complexity: O(1)
Definition digester.hpp:138
MinimizedHashType get_minimized_h()
Definition digester.hpp:289
size_t get_pos()
Definition digester.hpp:177
Child class of Digester that defines a minimizer as a kmer whose hash is equal to some target value a...
Definition mod_minimizer.hpp:30
void roll_minimizer(unsigned amount, std::vector< std::pair< uint32_t, uint32_t > > &vec) override
adds up to amount of positions and hashes of minimizers into vec. Here a k-mer is considered a minimi...
Definition mod_minimizer.hpp:121
ModMin(const char *seq, size_t len, unsigned k, uint32_t mod, uint32_t congruence=0, size_t start=0, MinimizedHashType minimized_h=MinimizedHashType::CANON)
Definition mod_minimizer.hpp:46
uint32_t get_congruence()
Definition mod_minimizer.hpp:160
void roll_minimizer(unsigned amount, std::vector< uint32_t > &vec) override
adds up to amount of positions of minimizers into vec. Here a k-mer is considered a minimizer if its ...
Definition mod_minimizer.hpp:82
uint32_t get_mod()
Definition mod_minimizer.hpp:155
ModMin(const std::string &seq, unsigned k, uint32_t mod, uint32_t congruence=0, size_t start=0, MinimizedHashType minimized_h=MinimizedHashType::CANON)
Definition mod_minimizer.hpp:68
digest code.
Definition data_structure.hpp:27
MinimizedHashType
Enum values for the type of hash to minimize.
Definition digester.hpp:41