Debian Codegraph @

src/iterate/kmer_collector.h

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 //
2 // Created by vout on 11/24/18.
3 //
4
5 #ifndef MEGAHIT_SPANNING_KMER_COLLECTOR_H
6 #define MEGAHIT_SPANNING_KMER_COLLECTOR_H
7
8 #include "parallel_hashmap/phmap.h"
9 #include "sdbg/sdbg_def.h"
10 #include "sequence/io/edge/edge_writer.h"
11 #include "sequence/kmer_plus.h"
12 #include "utils/mutex.h"
13
14 template <class KmerType>
15 class KmerCollector {
16 public:
17 using kmer_type = KmerType;
18 using kmer_plus = KmerPlus<KmerType, mul_t>;
19 using hash_set = phmap::parallel_flat_hash_set<
20 kmer_plus, KmerHash,
21 phmap::container_internal::hash_default_eq<kmer_plus>,
22 phmap::container_internal::Allocator<kmer_plus>, 12, SpinLock>;
23
24 KmerCollector(unsigned k, const std::string &out_prefix)
25 : k_(k), output_prefix_(out_prefix) {
26 last_shift_ = k_ % 16;
27 last_shift_ = (last_shift_ == 0 ? 0 : 16 - last_shift_) * 2;
28 words_per_kmer_ = DivCeiling(k_ * 2 + kBitsPerMul, 32);
29 buffer_.resize(words_per_kmer_);
30
31 writer_.SetFilePrefix(out_prefix);
32 writer_.SetUnordered();
33 writer_.SetKmerSize(k_ - 1);
34 writer_.InitFiles();
35 }
36
37 void Insert(const KmerType &kmer, mul_t mul) {
38 collection_.insert({kmer, mul});
39 }
40
41 const hash_set &collection() const { return collection_; }
42 void FlushToFile() {
43 for (const auto &item : collection_) {
44 WriteToFile(item.kmer, item.aux);
45 }
46 }
47
48 private:
49 void WriteToFile(const KmerType &kmer, mul_t mul) {
50 std::fill(buffer_.begin(), buffer_.end(), 0);
51
52 auto ptr = buffer_.begin();
53 uint32_t w = 0;
54
55 for (unsigned j = 0; j < k_; ++j) {
56 w = (w << 2) | kmer.GetBase(k_ - 1 - j);
57 if (j % 16 == 15) {
58 *ptr = w;
59 w = 0;
60 ++ptr;
61 }
62 }
63
64 assert(ptr - buffer_.begin() < words_per_kmer_);
65 *ptr = (w << last_shift_);
66 assert((buffer_.back() & kMaxMul) == 0);
67 buffer_.back() |= mul;
68 writer_.WriteUnordered(buffer_.data());
69 }
70
71 private:
72 unsigned k_;
73 std::string output_prefix_;
74 hash_set collection_;
75 EdgeWriter writer_;
76 unsigned last_shift_;
77 unsigned words_per_kmer_;
78 std::vector<uint32_t> buffer_;
79 };
80
81 #endif // MEGAHIT_SPANNING_KMER_COLLECTOR_H