Raptor
A fast and space-efficient pre-filter
All Classes Namespaces Files Functions Variables Macros Pages Concepts
index.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <cereal/types/string.hpp>
13#include <cereal/types/vector.hpp>
14
15#include <sharg/exceptions.hpp>
16
17#include <hibf/hierarchical_interleaved_bloom_filter.hpp>
18
21
22namespace raptor
23{
24
25namespace index_structure
26{
27
28using ibf = seqan::hibf::interleaved_bloom_filter;
29using hibf = seqan::hibf::hierarchical_interleaved_bloom_filter;
30
31template <typename index_t>
33
34template <typename index_t>
36
37template <typename index_t>
39
40} // namespace index_structure
41
42class index_upgrader;
43
44template <index_structure::is_valid data_t = index_structure::ibf>
46{
47private:
48 template <index_structure::is_valid friend_data_t>
49 friend class raptor_index;
50
51 uint64_t window_size_{};
52 seqan3::shape shape_{};
53 uint8_t parts_{};
55 double fpr_{};
57 data_t ibf_{};
58
59public:
60 static constexpr uint32_t version{3u};
61
62 raptor_index() = default;
63 raptor_index(raptor_index const &) = default;
64 raptor_index(raptor_index &&) = default;
65 raptor_index & operator=(raptor_index const &) = default;
66 raptor_index & operator=(raptor_index &&) = default;
67 ~raptor_index() = default;
68
69 explicit raptor_index(window const window_size,
70 seqan3::shape const shape,
71 uint8_t const parts,
72 std::vector<std::vector<std::string>> const & bin_path,
73 double const fpr,
74 data_t && ibf) :
75 window_size_{window_size.v},
76 shape_{shape},
77 parts_{parts},
78 bin_path_{bin_path},
79 fpr_{fpr},
80 ibf_{std::move(ibf)}
81 {}
82
83 explicit raptor_index(build_arguments const & arguments) :
84 window_size_{arguments.window_size},
85 shape_{arguments.shape},
86 parts_{arguments.parts},
87 bin_path_{arguments.bin_path},
88 fpr_{arguments.fpr},
89 ibf_{seqan::hibf::bin_count{arguments.bins},
90 seqan::hibf::bin_size{arguments.bits / arguments.parts},
91 seqan::hibf::hash_function_count{arguments.hash}}
92 {}
93
94 uint64_t window_size() const
95 {
96 return window_size_;
97 }
98
99 seqan3::shape shape() const
100 {
101 return shape_;
102 }
103
104 uint8_t parts() const
105 {
106 return parts_;
107 }
108
109 std::vector<std::vector<std::string>> const & bin_path() const
110 {
111 return bin_path_;
112 }
113
114 double fpr() const
115 {
116 return fpr_;
117 }
118
119 bool is_hibf() const
120 {
121 return is_hibf_;
122 }
123
124 data_t & ibf()
125 {
126 return ibf_;
127 }
128
129 data_t const & ibf() const
130 {
131 return ibf_;
132 }
133
142 template <seqan3::cereal_archive archive_t>
143 void CEREAL_SERIALIZE_FUNCTION_NAME(archive_t & archive)
144 {
145 uint32_t parsed_version{raptor_index<>::version};
146 archive(parsed_version);
147 if (parsed_version == raptor_index<>::version)
148 {
149 try
150 {
151 archive(window_size_);
152 archive(shape_);
153 archive(parts_);
154 archive(bin_path_);
155 archive(fpr_);
156 archive(is_hibf_);
157 archive(ibf_);
158 }
159 // GCOVR_EXCL_START
160 catch (std::exception const & e)
161 {
162 throw sharg::parser_error{"Cannot read index: " + std::string{e.what()}};
163 }
164 // GCOVR_EXCL_STOP
165 }
166 else
167 {
168 throw sharg::parser_error{"Unsupported index version. Check raptor upgrade."}; // GCOVR_EXCL_LINE
169 }
170 }
171
172 /* \brief Serialisation support function. Do not load the actual data.
173 * \tparam archive_t Type of `archive`; must satisfy seqan3::cereal_input_archive.
174 * \param[in] archive The archive being serialised from/to.
175 * \param[in] version Index version.
176 *
177 * \attention These functions are never called directly.
178 * \sa https://docs.seqan.de/seqan/3.2.0/group__io.html#serialisation
179 */
180 template <seqan3::cereal_input_archive archive_t>
181 void load_parameters(archive_t & archive)
182 {
183 uint32_t parsed_version{};
184 archive(parsed_version);
185 if (parsed_version == version)
186 {
187 try
188 {
189 archive(window_size_);
190 archive(shape_);
191 archive(parts_);
192 archive(bin_path_);
193 archive(fpr_);
194 archive(is_hibf_);
195 }
196 // GCOVR_EXCL_START
197 catch (std::exception const & e)
198 {
199 throw sharg::parser_error{"Cannot read index: " + std::string{e.what()}};
200 }
201 // GCOVR_EXCL_STOP
202 }
203 else
204 {
205 throw sharg::parser_error{"Unsupported index version. Check raptor upgrade."}; // GCOVR_EXCL_LINE
206 }
207 }
209};
210
211} // namespace raptor
Provides raptor::build_arguments.
Definition index.hpp:46
Definition index.hpp:35
Definition index.hpp:32
Definition index.hpp:38
T min(T... args)
Provides raptor::window.
Definition build_arguments.hpp:23
Strong type for passing the window size.
Definition strong_types.hpp:19
T what(T... args)
Hide me