Raptor 4.0.0-rc.1
A fast and space-efficient pre-filter
Loading...
Searching...
No Matches
file_reader.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 <seqan3/io/sequence_file/input.hpp>
13#include <seqan3/search/views/minimiser_hash.hpp>
14
17
18namespace raptor
19{
20
21enum class file_types : uint8_t
22{
23 sequence,
24 minimiser
25};
26
27template <file_types file_type>
29{};
30
31template <>
32class file_reader<file_types::sequence>
33{
34public:
35 file_reader() = default;
36 file_reader(file_reader const &) = default;
37 file_reader(file_reader &&) = default; // GCOVR_EXCL_LINE
38 file_reader & operator=(file_reader const &) = default;
39 file_reader & operator=(file_reader &&) = default;
40 ~file_reader() = default;
41
42 explicit file_reader(seqan3::shape const shape, uint32_t const window_size) :
43 minimiser_view{seqan3::views::minimiser_hash(shape,
44 seqan3::window_size{window_size},
45 seqan3::seed{adjust_seed(shape.count())})}
46 {}
47
48 template <std::output_iterator<uint64_t> it_t>
49 void hash_into(std::vector<std::string> const & filenames, it_t target) const
50 {
51 for (auto && filename : filenames)
52 hash_into(filename, target);
53 }
54
55 template <std::output_iterator<uint64_t> it_t>
56 void hash_into(std::string const & filename, it_t target) const
57 {
58 sequence_file_t fin{filename};
59 for (auto && record : fin)
60 std::ranges::copy(record.sequence() | minimiser_view, target);
61 }
62
63 template <std::output_iterator<uint64_t> it_t>
64 void hash_into_if(std::vector<std::string> const & filenames, it_t target, auto && pred) const
65 {
66 for (auto && filename : filenames)
67 hash_into_if(filename, target, pred);
68 }
69
70 template <std::output_iterator<uint64_t> it_t>
71 void hash_into_if(std::string const & filename, it_t target, auto && pred) const
72 {
73 sequence_file_t fin{filename};
74 for (auto && record : fin)
75 std::ranges::copy_if(record.sequence() | minimiser_view, target, pred);
76 }
77
78 void for_each_hash(std::vector<std::string> const & filenames, auto && callback) const
79 {
80 for (auto && filename : filenames)
81 for_each_hash(filename, callback);
82 }
83
84 void for_each_hash(std::string const & filename, auto && callback) const
85 {
86 sequence_file_t fin{filename};
87 for (auto && record : fin)
88 std::ranges::for_each(record.sequence() | minimiser_view, callback);
89 }
90
91private:
92 using sequence_file_t = seqan3::sequence_file_input<dna4_traits, seqan3::fields<seqan3::field::seq>>;
93 using view_t = decltype(seqan3::views::minimiser_hash(seqan3::shape{}, seqan3::window_size{}, seqan3::seed{}));
94 view_t minimiser_view = seqan3::views::minimiser_hash(seqan3::shape{}, seqan3::window_size{}, seqan3::seed{});
95};
96
97template <>
98class file_reader<file_types::minimiser>
99{
100 [[noreturn]] void io_error(std::string const & filepath, int const err = errno) const
101 {
103 oss << "Failed to open file: " << std::quoted(filepath);
104 throw std::system_error(err, std::generic_category(), oss.str());
105 }
106
107public:
108 file_reader() = default;
109 file_reader(file_reader const &) = default;
110 file_reader(file_reader &&) = default;
111 file_reader & operator=(file_reader const &) = default;
112 file_reader & operator=(file_reader &&) = default;
113 ~file_reader() = default;
114
115 explicit file_reader(seqan3::shape const, uint32_t const)
116 {}
117
118 template <std::output_iterator<uint64_t> it_t>
119 void hash_into(std::vector<std::string> const & filenames, it_t target) const
120 {
121 for (auto && filename : filenames)
122 hash_into(filename, target);
123 }
124
125 template <std::output_iterator<uint64_t> it_t>
126 void hash_into(std::string const & filename, it_t target) const
127 {
128 std::ifstream fin{filename, std::ios::binary};
129 if (!fin.is_open()) [[unlikely]]
130 io_error(filename);
131
132 uint64_t value;
133 while (fin.read(reinterpret_cast<char *>(&value), sizeof(value)))
134 {
135 *target = value;
136 ++target;
137 }
138 }
139
140 template <std::output_iterator<uint64_t> it_t>
141 void hash_into_if(std::vector<std::string> const & filenames, it_t target, auto && pred) const
142 {
143 for (auto && filename : filenames)
144 hash_into_if(filename, target, pred);
145 }
146
147 template <std::output_iterator<uint64_t> it_t>
148 void hash_into_if(std::string const & filename, it_t target, auto && pred) const
149 {
150 std::ifstream fin{filename, std::ios::binary};
151 if (!fin.is_open()) [[unlikely]]
152 io_error(filename);
153
154 uint64_t value;
155 while (fin.read(reinterpret_cast<char *>(&value), sizeof(value)))
156 if (pred(value))
157 {
158 *target = value;
159 ++target;
160 }
161 }
162
163 void for_each_hash(std::vector<std::string> const & filenames, auto && callback) const
164 {
165 for (auto && filename : filenames)
166 for_each_hash(filename, callback);
167 }
168
169 void for_each_hash(std::string const & filename, auto && callback) const
170 {
171 std::ifstream fin{filename, std::ios::binary};
172 if (!fin.is_open()) [[unlikely]]
173 io_error(filename);
174
175 uint64_t value;
176 while (fin.read(reinterpret_cast<char *>(&value), sizeof(value)))
177 callback(value);
178 }
179};
180
181} // namespace raptor
Provides raptor::adjust_seed.
Definition file_reader.hpp:29
T copy(T... args)
Provides raptor::dna4_traits.
T for_each(T... args)
T generic_category(T... args)
T quoted(T... args)
T str(T... args)
Hide me