|
3 | 3 | // SPDX-License-Identifier: BSD-3-Clause |
4 | 4 |
|
5 | 5 | #include <fmt/format.h> |
| 6 | +#include <fmt/ranges.h> |
6 | 7 |
|
7 | 8 | #include <seqan3/io/sequence_file/input.hpp> |
8 | 9 |
|
|
17 | 18 | namespace search |
18 | 19 | { |
19 | 20 |
|
20 | | -fmc::BiFMIndex<4> load_index(config const & config, size_t const id) |
| 21 | +template <typename Index> |
| 22 | +auto myReconstruct(Index const & index, size_t seqNbr) -> std::vector<uint8_t> |
21 | 23 | { |
22 | | - fmc::BiFMIndex<4> index{}; |
| 24 | + //TODO: möglicher weise ist das identisch zu einfach index.C[1] |
| 25 | + auto totalNumberOfSeq = index.bwt.rank(index.size(), 0) + index.C[0]; |
| 26 | + for (size_t i{0}; i < totalNumberOfSeq; ++i) |
| 27 | + { |
| 28 | + auto idx = std::get<0>(std::get<0>(index.locate(i))); |
| 29 | + if (idx == seqNbr) |
| 30 | + { |
| 31 | + return reconstructText(index, i); |
| 32 | + } |
| 33 | + } |
| 34 | + throw std::runtime_error{"unknown sequence number"}; |
| 35 | +} |
| 36 | + |
| 37 | +fmc::BiFMIndex<5> load_index(config const & config, size_t const id) |
| 38 | +{ |
| 39 | + fmc::BiFMIndex<5> index{}; |
23 | 40 |
|
24 | 41 | { |
25 | 42 | std::ifstream os{fmt::format("{}.{}.fmindex", config.input_path.c_str(), id), std::ios::binary}; |
26 | 43 | cereal::BinaryInputArchive iarchive{os}; |
27 | 44 | iarchive(index); |
28 | 45 | } |
29 | 46 |
|
| 47 | + // { |
| 48 | + // fmt::println(" === Reconstruct all ==="); |
| 49 | + // auto text = fmc::reconstructText(index); |
| 50 | + // for (size_t i = 0; i < text.size(); ++i) |
| 51 | + // fmt::print("### {} ###\n{}\n", i, fmt::join(text[i], "")); |
| 52 | + // } |
| 53 | + // { |
| 54 | + // fmt::println(" === Reconstruct single ==="); |
| 55 | + // auto text = fmc::reconstructText(index, 0); |
| 56 | + // fmt::print("### 0 ###\n{}\n", fmt::join(text, "")); |
| 57 | + // text = fmc::reconstructText(index, 1); |
| 58 | + // fmt::print("### 1 ###\n{}\n", fmt::join(text, "")); |
| 59 | + // } |
| 60 | + // { |
| 61 | + // fmt::println(" === Reconstruct alternative ==="); |
| 62 | + // auto text = myReconstruct(index, 0); |
| 63 | + // fmt::print("### 0 ###\n{}\n", fmt::join(text, "")); |
| 64 | + // text = myReconstruct(index, 1); |
| 65 | + // fmt::print("### 1 ###\n{}\n", fmt::join(text, "")); |
| 66 | + // } |
| 67 | + |
30 | 68 | return index; |
31 | 69 | } |
32 | 70 |
|
33 | | -void fmindex(config const & config, std::vector<hit> hits, size_t const todo_bin_count) |
| 71 | +std::vector<wip_alignment> fmindex(config const & config, std::vector<hit> hits, size_t const todo_bin_count) |
34 | 72 | { |
35 | 73 | // todo bin count |
36 | 74 | // todo capacity |
37 | 75 | // each slot = 1 bin |
38 | 76 | // a cart is full if it has 5 elements (hits) |
39 | | - scq::slotted_cart_queue<size_t> queue{{.slots = todo_bin_count, .carts = todo_bin_count, .capacity = 5}}; |
40 | | - size_t thread_id{}; |
41 | | - |
42 | | - auto get_thread = [&]() |
| 77 | + alignment_vector res; |
43 | 78 | { |
44 | | - return std::jthread( |
45 | | - [&, thread_id = thread_id++]() |
46 | | - { |
47 | | - while (true) |
| 79 | + scq::slotted_cart_queue<size_t> queue{{.slots = todo_bin_count, .carts = todo_bin_count, .capacity = 5}}; |
| 80 | + size_t thread_id{}; |
| 81 | + |
| 82 | + auto get_thread = [&]() |
| 83 | + { |
| 84 | + return std::jthread( |
| 85 | + [&, thread_id = thread_id++]() |
48 | 86 | { |
49 | | - scq::cart_future<size_t> cart = queue.dequeue(); |
50 | | - if (!cart.valid()) |
51 | | - return; |
52 | | - auto [slot, span] = cart.get(); |
53 | | - auto index = load_index(config, slot.value); |
54 | | - for (auto idx : span) |
| 87 | + while (true) |
55 | 88 | { |
56 | | - auto & [id, seq, bins] = hits[idx]; |
57 | | - |
58 | | - auto callback = [&](auto cursor, size_t) |
| 89 | + scq::cart_future<size_t> cart = queue.dequeue(); |
| 90 | + if (!cart.valid()) |
| 91 | + return; |
| 92 | + auto [slot, span] = cart.get(); |
| 93 | + auto index = load_index(config, slot.value); |
| 94 | + for (auto idx : span) |
59 | 95 | { |
60 | | - for (auto j : cursor) |
| 96 | + auto & [id, seq, bins] = hits[idx]; |
| 97 | + |
| 98 | + auto callback = [&](auto cursor, size_t) |
61 | 99 | { |
62 | | - auto [entry, offset] = index.locate(j); |
63 | | - auto [seqId, pos] = entry; |
| 100 | + for (auto j : cursor) |
64 | 101 | { |
65 | | - fmt::print("[{}][{}] found hit in bin {} in seqNo {} at Pos {}\n", |
66 | | - thread_id, |
67 | | - id, |
68 | | - slot.value, |
69 | | - seqId, |
70 | | - pos + offset); |
| 102 | + auto [entry, offset] = index.locate(j); |
| 103 | + auto [seqId, pos] = entry; |
| 104 | + // fmt::print("[{}][{}] found hit in bin {} in seqNo {} at Pos {}\n", |
| 105 | + // thread_id, |
| 106 | + // id, |
| 107 | + // slot.value, |
| 108 | + // seqId, |
| 109 | + // pos + offset); |
| 110 | + res.emplace_back(wip_alignment{.bin = slot.value, |
| 111 | + .sequence_number = seqId, |
| 112 | + .position = pos + offset, |
| 113 | + .seq = seq, |
| 114 | + .ref = myReconstruct(index, seqId), |
| 115 | + .id = id}); // todo seq is copied |
71 | 116 | } |
72 | | - } |
73 | | - }; |
| 117 | + }; |
74 | 118 |
|
75 | | - fmc::search<true>(index, seq, config.errors, callback); |
| 119 | + fmc::search<true>(index, seq, config.errors, callback); |
| 120 | + } |
76 | 121 | } |
77 | | - } |
78 | | - }); |
79 | | - }; |
| 122 | + }); |
| 123 | + }; |
| 124 | + |
| 125 | + std::vector<std::jthread> worker(config.threads); |
| 126 | + std::ranges::generate(worker, get_thread); |
80 | 127 |
|
81 | | - std::vector<std::jthread> worker(config.threads); |
82 | | - std::ranges::generate(worker, get_thread); |
| 128 | + for (auto && [idx, hit] : seqan::stl::views::enumerate(hits)) |
| 129 | + for (auto bin : hit.bins) |
| 130 | + queue.enqueue(scq::slot_id{bin}, idx); |
83 | 131 |
|
84 | | - for (auto && [idx, hit] : seqan::stl::views::enumerate(hits)) |
85 | | - for (auto bin : hit.bins) |
86 | | - queue.enqueue(scq::slot_id{bin}, idx); |
| 132 | + queue.close(); |
| 133 | + } // Wait for threads to finish |
87 | 134 |
|
88 | | - queue.close(); |
| 135 | + return res.get(); |
89 | 136 | } |
90 | 137 |
|
91 | 138 | } // namespace search |
0 commit comments