Debian Codegraph @

src/pbmer/DbgNode.cpp

raw
1 #include <pbcopper/pbmer/DbgNode.h>
2 #include <pbcopper/utility/Intrinsics.h>
3
4 #include <array>
5
6 namespace PacBio {
7 namespace Pbmer {
8
9 /**
10 * The bases are duplicated for the iterator, avoiding strand checking
11 */
12 constexpr std::array<char, 8> BASES{'A', 'C', 'G', 'T', 'A', 'C', 'G', 'T'};
13 constexpr std::array<uint8_t, 256> CLZ_LOOKUP{
14 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
15 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
16 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
17 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
18 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
21 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
22
23 DbgNode::DbgNode(const DnaBit& d, uint8_t o, uint32_t n) : dna_{d}, edges_{o}
24 {
25 readIds2_.resize(n);
26 }
27
28 bool DbgNode::AddLoad(uint32_t rid)
29 {
30 readIds2_[rid - 1] = 1;
31 return true;
32 }
33 size_t DbgNode::FirstRId() const
34 {
35 if (readIds2_.none()) {
36 return 0;
37 }
38 return readIds2_.find_first() + 1;
39 }
40
41 uint64_t DbgNode::Kmer() const { return dna_.mer; }
42
43 int DbgNode::LeftEdgeCount() const
44 {
45 // 11110000 = 240
46 return Utility::PopCount(edges_ & 240);
47 }
48 int DbgNode::RightEdgeCount() const
49 {
50 // 00001111 = 15
51 return Utility::PopCount(edges_ & 15);
52 }
53
54 void DbgNode::SetEdges(uint8_t o) { edges_ |= o; }
55
56 int DbgNode::TotalEdgeCount() const { return Utility::PopCount(edges_ & 255); }
57
58 // -------------------------------------------
59 // iteration of DnaBits over node
60
61 DbgNode::iterator_base::iterator_base() = default;
62
63 DbgNode::iterator_base::iterator_base(DbgNode& node) : node_{&node} { LoadNext(); }
64
65 DbgNode::iterator_base::~iterator_base() = default;
66
67 bool DbgNode::iterator_base::operator==(const iterator_base& other) const noexcept
68 {
69 return node_ == other.node_;
70 }
71
72 bool DbgNode::iterator_base::operator!=(const iterator_base& other) const noexcept
73 {
74 return !(*this == other);
75 }
76
77 void DbgNode::iterator_base::LoadNext()
78 {
79 if (index_ < 8) {
80
81 // this is using a lookup table to find the current on bit. The 255
82 // shift is a mask. This saves time because we don't loop over all
83 // four bases x 2 strands.
84 index_ = CLZ_LOOKUP[((255 >> (index_)) & node_->edges_)];
85
86 DnaBit niby = node_->dna_;
87 if (index_ > 3) {
88 niby.PrependBase(BASES[7 - index_]);
89 } else {
90 niby.AppendBase(BASES[7 - index_]);
91 }
92 value_ = niby.LexSmallerEq();
93
94 index_ = CLZ_LOOKUP[((255 >> (index_ + 1)) & node_->edges_)];
95
96 } else {
97 node_ = nullptr;
98 }
99 }
100
101 DbgNode::iterator::iterator() = default;
102 DbgNode::iterator::iterator(DbgNode& node) : iterator_base{node} {}
103 DnaBit& DbgNode::iterator::operator*() noexcept { return iterator_base::value_; }
104 DnaBit* DbgNode::iterator::operator->() noexcept { return &(operator*()); }
105 DbgNode::iterator& DbgNode::iterator::operator++()
106 {
107 LoadNext();
108 return *this;
109 }
110
111 DbgNode::iterator DbgNode::iterator::operator++(int)
112 {
113 DbgNode::iterator result(*this);
114 ++(*this);
115 return result;
116 }
117
118 DbgNode::const_iterator::const_iterator() = default;
119 DbgNode::const_iterator::const_iterator(const DbgNode& node)
120 : iterator_base{const_cast<DbgNode&>(node)}
121 {}
122 const DnaBit& DbgNode::const_iterator::operator*() const noexcept { return iterator_base::value_; }
123 const DnaBit* DbgNode::const_iterator::operator->() const noexcept { return &(operator*()); }
124 DbgNode::const_iterator& DbgNode::const_iterator::operator++()
125 {
126 LoadNext();
127 return *this;
128 }
129
130 DbgNode::const_iterator DbgNode::const_iterator::operator++(int)
131 {
132 const_iterator result(*this);
133 ++(*this);
134 return result;
135 }
136
137 DbgNode::const_iterator DbgNode::begin() const { return const_iterator{*this}; }
138 DbgNode::const_iterator DbgNode::cbegin() const { return const_iterator{*this}; }
139 DbgNode::iterator DbgNode::begin() { return iterator{*this}; }
140
141 DbgNode::const_iterator DbgNode::end() const { return const_iterator{}; }
142 DbgNode::const_iterator DbgNode::cend() const { return const_iterator{}; }
143 DbgNode::iterator DbgNode::end() { return iterator{}; }
144
145 // -------------------------------------------
146
147 } // namespace Pbmer
148 } // namespace PacBio