Debian Codegraph @

src/poa/RangeFinder.cpp

raw
1 #include <pbcopper/poa/RangeFinder.h>
2
3 #include "PoaGraphImpl.h"
4
5 #include <boost/foreach.hpp>
6 #include <boost/graph/topological_sort.hpp>
7 #include <boost/range/adaptor/reversed.hpp>
8
9 #include <algorithm>
10 #include <map>
11 #include <optional>
12 #include <string>
13 #include <utility>
14 #include <vector>
15
16 #include <cstddef>
17
18 #define WIDTH 30
19 #define DEBUG_RANGE_FINDER 0
20
21 #if DEBUG_RANGE_FINDER
22 #include <iostream>
23 #endif // DEBUG_RANGE_FINDER
24
25 namespace PacBio {
26 namespace Poa {
27 namespace detail {
28 namespace {
29
30 bool compareAnchorsOnCssPos(const SdpAnchor& a1, const SdpAnchor& a2)
31 {
32 return a1.first < a2.first;
33 }
34
35 const SdpAnchor* binarySearchAnchors(const SdpAnchorVector& anchors, size_t cssPosition)
36 {
37 auto found = std::lower_bound(anchors.begin(), anchors.end(), std::make_pair(cssPosition, -1),
38 compareAnchorsOnCssPos);
39 if (found != anchors.end() && (*found).first == cssPosition) {
40 return &(*found);
41 } else {
42 return nullptr;
43 }
44 }
45
46 } // namespace
47
48 using IntervalPair = std::pair<int, int>;
49
50 std::string formatIntervalEndpoint(int i)
51 {
52 if (i == INT_MAX / 2) {
53 return "inf";
54 } else if (i == -INT_MAX / 2) {
55 return "-inf";
56 } else {
57 return std::to_string(i);
58 }
59 }
60
61 std::string formatInterval(const IntervalPair& ival)
62 {
63 return '[' + formatIntervalEndpoint(ival.first) + ", " + formatIntervalEndpoint(ival.second) +
64 ')';
65 }
66
67 // Canonical empty interval....
68 const IntervalPair emptyInterval = IntervalPair(INT_MAX / 2, -INT_MAX / 2);
69
70 IntervalPair RangeUnion(const IntervalPair& range1, const IntervalPair& range2)
71 {
72 return IntervalPair(std::min(range1.first, range2.first),
73 std::max(range1.second, range2.second));
74 }
75
76 IntervalPair RangeUnion(const std::vector<IntervalPair>& ranges)
77 {
78 IntervalPair result = emptyInterval;
79 for (const IntervalPair& r : ranges) {
80 result = RangeUnion(result, r);
81 }
82 return result;
83 }
84
85 IntervalPair next(const IntervalPair& v, int upperBound)
86 {
87 if (v == emptyInterval) {
88 return emptyInterval;
89 } else {
90 return IntervalPair(std::min(v.first + 1, upperBound), std::min(v.second + 1, upperBound));
91 }
92 }
93
94 IntervalPair prev(const IntervalPair& v, int lowerBound = 0)
95 {
96 if (v == emptyInterval) {
97 return emptyInterval;
98 } else {
99 return IntervalPair(std::max(v.first - 1, lowerBound), std::max(v.second - 1, lowerBound));
100 }
101 }
102
103 SdpRangeFinder::~SdpRangeFinder() = default;
104 void SdpRangeFinder::InitRangeFinder(const PoaGraphImpl& poaGraph,
105 const std::vector<Vertex>& consensusPath,
106 const std::string& consensusSequence,
107 const std::string& readSequence)
108 {
109 #if DEBUG_RANGE_FINDER
110 poaGraph.WriteGraphVizFile("debug-graph.dot", PoaGraph::VERBOSE_NODES, NULL);
111 std::map<Vertex, const SdpAnchor*> anchorByVertex;
112 #endif
113 // Clear prexisting state first!
114 alignableReadIntervalByVertex_.clear();
115
116 const int readLength = readSequence.size();
117 SdpAnchorVector anchors = FindAnchors(consensusSequence, readSequence);
118 #if DEBUG_RANGE_FINDER
119 std::cout << "Consensus: " << consensusSequence << std::endl;
120 std::cout << "Read : " << readSequence << std::endl;
121 std::cout << "RawAnchors length: " << anchors.size() << std::endl;
122 #endif
123
124 std::map<VD, std::optional<IntervalPair>> directRanges;
125 std::map<VD, IntervalPair> fwdMarks, revMarks;
126
127 std::vector<VD> sortedVertices(num_vertices(poaGraph.g_));
128 topological_sort(poaGraph.g_, sortedVertices.rbegin());
129 for (const VD v : sortedVertices) {
130 directRanges[v] = std::nullopt;
131 }
132
133 // Find the "direct ranges" implied by the anchors between the
134 // css and this read. Possibly null.
135 for (size_t cssPos = 0; cssPos < consensusPath.size(); cssPos++) {
136 Vertex vExt = consensusPath[cssPos];
137 VD v = poaGraph.internalize(vExt);
138 const SdpAnchor* anchor = binarySearchAnchors(anchors, cssPos);
139 if (anchor != nullptr) {
140 #if DEBUG_RANGE_FINDER
141 anchorByVertex[vExt] = anchor;
142 #endif
143 directRanges[v] = IntervalPair(std::max(int(anchor->second) - WIDTH, 0),
144 std::min(int(anchor->second) + WIDTH, readLength));
145 } else {
146 directRanges[v] = std::nullopt;
147 }
148 }
149
150 // Use the direct ranges as a seed and perform a forward recursion,
151 // letting a node with null direct range have a range that is the
152 // union of the "forward stepped" ranges of its predecessors
153 for (const VD v : sortedVertices) {
154 /* Vertex vExt = */ poaGraph.externalize(v); // DEBUGGING
155
156 std::optional<IntervalPair> directRange = directRanges.at(v);
157 if (directRange) {
158 fwdMarks[v] = *directRange;
159 } else {
160 std::vector<IntervalPair> predRangesStepped;
161 for (const ED& e : inEdges(v, poaGraph.g_)) {
162 VD pred = source(e, poaGraph.g_);
163 /* Vertex predExt = */ poaGraph.externalize(pred); // DEBUGGING
164 IntervalPair predRangeStepped = next(fwdMarks.at(pred), readLength);
165 predRangesStepped.push_back(predRangeStepped);
166 }
167 IntervalPair fwdInterval = RangeUnion(predRangesStepped);
168 fwdMarks[v] = fwdInterval;
169 }
170 }
171
172 // Do the same thing, but as a backwards recursion
173 for (const VD v : boost::adaptors::reverse(sortedVertices)) {
174 /* Vertex vExt = */ poaGraph.externalize(v); // DEBUGGING
175
176 std::optional<IntervalPair> directRange = directRanges.at(v);
177 if (directRange) {
178 revMarks[v] = *directRange;
179 } else {
180 std::vector<IntervalPair> succRangesStepped;
181 BOOST_FOREACH (const ED& e, out_edges(v, poaGraph.g_)) {
182 VD succ = target(e, poaGraph.g_);
183 /* Vertex succExt = */ poaGraph.externalize(succ); // DEBUGGING
184 IntervalPair succRangeStepped = prev(revMarks.at(succ), 0);
185 succRangesStepped.push_back(succRangeStepped);
186 }
187 IntervalPair revInterval = RangeUnion(succRangesStepped);
188 revMarks[v] = revInterval;
189 }
190 }
191
192 // take hulls of extents from forward and reverse recursions
193 for (const VD v : sortedVertices) {
194 Vertex vExt = poaGraph.externalize(v);
195 alignableReadIntervalByVertex_[vExt] = RangeUnion(fwdMarks.at(v), revMarks.at(v));
196 #if DEBUG_RANGE_FINDER
197 cout << vExt << "\t";
198 if (anchorByVertex.find(vExt) != anchorByVertex.end()) {
199 cout << " @ " << anchorByVertex.at(vExt)->second << "\t";
200 } else {
201 cout << "\t";
202 }
203 cout << "Fwd mark: " << formatInterval(fwdMarks[v]) << "\t"
204 << "Rev mark: " << formatInterval(revMarks[v]) << "\t"
205 << "Range: " << formatInterval(alignableReadIntervalByVertex_[vExt]) << endl;
206 #endif
207 }
208 }
209
210 IntervalPair SdpRangeFinder::FindAlignableRange(Vertex v)
211 {
212 return alignableReadIntervalByVertex_.at(v);
213 }
214
215 } // namespace detail
216 } // namespace Poa
217 } // namespace PacBio