Raptor 4.0.0-rc.1
A fast and space-efficient pre-filter
Loading...
Searching...
No Matches
index.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2026 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2026 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <algorithm>
13#include <cassert>
14
15#include <cereal/types/string.hpp>
16
17#include <sharg/exceptions.hpp>
18
19#include <hibf/hierarchical_interleaved_bloom_filter.hpp>
20
23
24namespace raptor
25{
26
27namespace index_structure
28{
29
30using ibf = seqan::hibf::interleaved_bloom_filter;
31using hibf = seqan::hibf::hierarchical_interleaved_bloom_filter;
32
33template <typename index_t>
35
36template <typename index_t>
38
39template <typename index_t>
41
42} // namespace index_structure
43
44class index_upgrader;
45
46template <index_structure::is_valid data_t = index_structure::ibf>
48{
49private:
50 template <index_structure::is_valid friend_data_t>
51 friend class raptor_index;
52
53 uint64_t window_size_{};
54 seqan3::shape shape_{};
55 uint8_t parts_{};
58 double fpr_{};
59 seqan::hibf::config config_{};
60 data_t ibf_{};
74 seqan::hibf::bit_vector was_resized_{};
75
76public:
77 static constexpr uint32_t version{3u};
78
79 raptor_index() = default;
80 raptor_index(raptor_index const &) = default;
81 raptor_index(raptor_index &&) = default;
82 raptor_index & operator=(raptor_index const &) = default;
83 raptor_index & operator=(raptor_index &&) = default;
84 ~raptor_index() = default;
85
86 explicit raptor_index(window const window_size,
87 seqan3::shape const shape,
88 uint8_t const parts,
89 std::vector<std::vector<std::string>> const & bin_path,
90 seqan::hibf::config const & config,
91 data_t && ibf)
92 requires index_structure::is_hibf<data_t>
93 :
94 window_size_{window_size.v},
95 shape_{shape},
96 parts_{parts},
97 bin_path_{bin_path},
98 fpr_{config.maximum_fpr},
99 config_{config},
100 ibf_{std::move(ibf)},
101 original_number_of_ibfs_{ibf_.ibf_vector.size()},
102 was_resized_(ibf_.ibf_vector.size())
103 {}
104
105 explicit raptor_index(build_arguments const & arguments)
106 requires index_structure::is_ibf<data_t>
107 :
108 window_size_{arguments.window_size},
109 shape_{arguments.shape},
110 parts_{arguments.parts},
111 bin_path_{arguments.bin_path},
112 fpr_{arguments.fpr},
113 ibf_{seqan::hibf::bin_count{arguments.bins},
114 seqan::hibf::bin_size{arguments.bits / arguments.parts},
115 seqan::hibf::hash_function_count{arguments.hash}}
116 {}
117
118 uint64_t window_size() const
119 {
120 return window_size_;
121 }
122
123 seqan3::shape shape() const
124 {
125 return shape_;
126 }
127
128 uint8_t parts() const
129 {
130 return parts_;
131 }
132
133 void append_bin_path(std::vector<std::string> const & path)
134 {
135 bin_path_.push_back(path);
136 }
137
138 std::vector<std::vector<std::string>> const & bin_path() const
139 {
140 return bin_path_;
141 }
142
143 double fpr() const
144 {
145 return fpr_;
146 }
147
148 auto const & config() const
149 requires index_structure::is_hibf<data_t>
150 {
151 return config_;
152 }
153
154 bool is_hibf() const
155 {
156 return is_hibf_;
157 }
158
159 data_t & ibf()
160 {
161 return ibf_;
162 }
163
164 data_t const & ibf() const
165 {
166 return ibf_;
167 }
168
171 requires index_structure::is_hibf<data_t>
172 {
174 }
175
181 size_t number_of_ibfs() const
182 requires index_structure::is_hibf<data_t>
183 {
184 return was_resized_.size();
185 }
186
189 requires index_structure::is_hibf<data_t>
190 {
191 return was_resized_.count();
192 }
193
195 bool was_resized(size_t const ibf_idx) const
197 {
198 assert(ibf_idx < was_resized_.size());
199 return was_resized_[ibf_idx];
200 }
201
203 void mark_resized(size_t const ibf_idx)
205 {
206 assert(ibf_idx < was_resized_.size());
207 was_resized_[ibf_idx] = true;
208 }
209
214 void sync_resized(bool const value_for_new_ibfs)
216 {
217 was_resized_.resize(ibf_.ibf_vector.size(), value_for_new_ibfs);
218 }
219
228 template <seqan3::cereal_archive archive_t>
229 void CEREAL_SERIALIZE_FUNCTION_NAME(archive_t & archive)
230 {
231 uint32_t parsed_version{raptor_index<>::version};
232 archive(parsed_version);
233 if (parsed_version == raptor_index<>::version)
234 {
235 try
236 {
237 archive(window_size_);
238 archive(shape_);
239 archive(parts_);
240 archive(bin_path_);
241 archive(fpr_);
242 archive(is_hibf_);
243 archive(config_);
245 archive(was_resized_);
246 archive(ibf_);
247 }
248 // GCOVR_EXCL_START
249 catch (std::exception const & e)
250 {
251 throw sharg::parser_error{"Cannot read index: " + std::string{e.what()}};
252 }
253 // GCOVR_EXCL_STOP
254 }
255 else
256 {
257 throw sharg::parser_error{"Unsupported index version. Check raptor upgrade."}; // GCOVR_EXCL_LINE
258 }
259 }
260
261 /* \brief Serialisation support function. Do not load the actual data.
262 * \tparam archive_t Type of `archive`; must satisfy seqan3::cereal_input_archive.
263 * \param[in] archive The archive being serialised from/to.
264 * \param[in] version Index version.
265 * \details
266 * Reads everything that precedes `ibf_` in the archive. The resize bookkeeping is small: one integer and one bit
267 * per IBF.
268 *
269 * \attention These functions are never called directly.
270 * \sa https://docs.seqan.de/seqan/3.2.0/group__io.html#serialisation
271 */
272 template <seqan3::cereal_input_archive archive_t>
273 void load_parameters(archive_t & archive)
274 {
275 uint32_t parsed_version{};
276 archive(parsed_version);
277 if (parsed_version == version)
278 {
279 try
280 {
281 archive(window_size_);
282 archive(shape_);
283 archive(parts_);
284 archive(bin_path_);
285 archive(fpr_);
286 archive(is_hibf_);
287 archive(config_);
289 archive(was_resized_);
290 }
291 // GCOVR_EXCL_START
292 catch (std::exception const & e)
293 {
294 throw sharg::parser_error{"Cannot read index: " + std::string{e.what()}};
295 }
296 // GCOVR_EXCL_STOP
297 }
298 else
299 {
300 throw sharg::parser_error{"Unsupported index version. Check raptor upgrade."}; // GCOVR_EXCL_LINE
301 }
302 }
304};
305
306} // namespace raptor
Provides raptor::build_arguments.
Definition index.hpp:48
seqan::hibf::bit_vector was_resized_
Whether an IBF has already been grown beyond the size its layout assigned to it. Indexed by IBF index...
Definition index.hpp:74
size_t number_of_ibfs() const
The number of IBFs, including the ones retired by an update.
Definition index.hpp:181
bool was_resized(size_t const ibf_idx) const
Whether the IBF at ibf_idx has already been grown beyond the size its layout assigned to it.
Definition index.hpp:195
void mark_resized(size_t const ibf_idx)
Records that the IBF at ibf_idx has been grown beyond the size its layout assigned to it.
Definition index.hpp:203
size_t original_number_of_ibfs() const
The number of IBFs the index had when it was built from a layout.
Definition index.hpp:170
size_t number_of_resized_ibfs() const
The number of IBFs that have been grown beyond the size their layout assigned to them.
Definition index.hpp:188
void sync_resized(bool const value_for_new_ibfs)
Matches the resize bookkeeping to the current number of IBFs.
Definition index.hpp:214
size_t original_number_of_ibfs_
The number of IBFs the index had when it was built from a layout.
Definition index.hpp:67
Definition index.hpp:37
Definition index.hpp:34
Definition index.hpp:40
Provides raptor::window.
T min(T... args)
T push_back(T... args)
T what(T... args)
Hide me