Raptor 4.0.0-rc.1
A fast and space-efficient pre-filter
Loading...
Searching...
No Matches
search_singular_ibf.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 <future>
13#include <random>
14
15#include <seqan3/search/views/minimiser_hash.hpp>
16
17#include <hibf/contrib/std/chunk_view.hpp>
18
25
26namespace raptor
27{
28
29template <typename index_t>
30void search_singular_ibf(search_arguments const & arguments, index_t && index)
31{
33
34 auto cereal_future = std::async(std::launch::async,
35 [&]()
36 {
37 load_index(index, arguments);
38 });
39
40 seqan3::sequence_file_input<dna4_traits, seqan3::fields<seqan3::field::id, seqan3::field::seq>> fin{
41 arguments.query_file};
42 using record_type = typename decltype(fin)::record_type;
44
45 sync_out synced_out{arguments};
46
47 raptor::threshold::threshold const thresholder{arguments.make_threshold_parameters()};
48
49 auto worker = [&](size_t const start, size_t const extent)
50 {
51 seqan::hibf::serial_timer local_compute_minimiser_timer{};
52 seqan::hibf::serial_timer local_query_ibf_timer{};
53 seqan::hibf::serial_timer local_generate_results_timer{};
54
55 auto agent = index.ibf().membership_agent();
56
57 std::string result_string{};
59 std::vector<uint64_t> minimiser;
60
61 auto hash_adaptor = seqan3::views::minimiser_hash(arguments.shape,
62 seqan3::window_size{arguments.window_size},
63 seqan3::seed{adjust_seed(arguments.shape_weight)});
64
65 for (auto && [id, seq] : std::span{records.data() + start, extent})
66 {
67 result_string.clear();
68 result_string += id;
69 result_string += '\t';
70
71 auto minimiser_view = seq | hash_adaptor | std::views::common;
72 local_compute_minimiser_timer.start();
73 minimiser.assign(minimiser_view.begin(), minimiser_view.end());
74 local_compute_minimiser_timer.stop();
75
76 size_t const minimiser_count{minimiser.size()};
77 size_t const threshold = thresholder.get(minimiser_count);
78
79 local_query_ibf_timer.start();
80 auto & user_bin_ids = agent.membership_for(minimiser, threshold);
81 local_query_ibf_timer.stop();
82 local_generate_results_timer.start();
83 for (auto && user_bin : user_bin_ids)
84 {
85 auto conv = std::to_chars(buffer.data(), buffer.data() + buffer.size(), user_bin);
86 assert(conv.ec == std::errc{});
87 std::string_view sv{buffer.data(), conv.ptr};
88 result_string += sv;
89 result_string += ',';
90 }
91
92 if (auto & last_char = result_string.back(); last_char == ',')
93 last_char = '\n';
94 else
95 result_string += '\n';
96
97 synced_out.write(result_string);
98 local_generate_results_timer.stop();
99 }
100
101 arguments.compute_minimiser_timer += local_compute_minimiser_timer;
102 arguments.query_ibf_timer += local_query_ibf_timer;
103 arguments.generate_results_timer += local_generate_results_timer;
104 };
105
106 auto write_header = [&]()
107 {
108 if constexpr (is_ibf)
109 return synced_out.write_header(arguments, index.ibf().hash_function_count());
110 else
111 return synced_out.write_header(arguments, index.ibf().ibf_vector[0].hash_function_count());
112 };
113
114 for (auto && chunked_records : fin | seqan::stl::views::chunk((1ULL << 20) * 10))
115 {
116 records.clear();
117 arguments.query_file_io_timer.start();
118 std::ranges::move(chunked_records, std::back_inserter(records));
119 // Very fast, improves parallel processing when chunks of the query belong to the same bin.
121 arguments.query_file_io_timer.stop();
122
123 cereal_future.get();
124 [[maybe_unused]] static bool header_written = write_header(); // called exactly once
125
126 arguments.parallel_search_timer.start();
127 do_parallel(worker, records.size(), arguments.threads);
128 arguments.parallel_search_timer.stop();
129 }
130}
131
132} // namespace raptor
Provides raptor::adjust_seed.
T async(T... args)
T back_inserter(T... args)
Definition threshold.hpp:19
T data(T... args)
Provides raptor::dna4_traits.
Provides raptor::do_parallel.
Provides raptor::load_index.
T min(T... args)
T move(T... args)
T shuffle(T... args)
Provides raptor::sync_out.
Provides raptor::threshold::threshold.
T to_chars(T... args)
Hide me