Debian Codegraph @

src/C++/Poa/RangeFinder.cpp

raw
1
2 #include <ConsensusCore/Interval.hpp>
3 #include <ConsensusCore/Poa/RangeFinder.hpp>
4
5 #include <algorithm>
6 #include <boost/graph/topological_sort.hpp>
7 #include <boost/optional.hpp>
8 #include <map>
9 #include <string>
10 #include <utility>
11 #include <vector>
12
13 #include "PoaGraphImpl.hpp"
14
15 #define WIDTH 30
16 #define DEBUG_RANGE_FINDER 0
17
18 #if DEBUG_RANGE_FINDER
19 #include <iostream>
20 using std::cout;
21 using std::endl;
22 #endif // DEBUG_RANGE_FINDER
23
24 namespace ConsensusCore {
25 namespace detail {
26
27 using std::min;
28 using std::max;
29 using std::make_pair;
30 using boost::optional;
31
32 static inline bool compareAnchorsOnCssPos(const SdpAnchor& a1, const SdpAnchor& a2)
33 {
34 return a1.first < a2.first;
35 }
36
37 static const SdpAnchor* binarySearchAnchors(const SdpAnchorVector& anchors, size_t cssPosition)
38 {
39 typedef SdpAnchorVector::const_iterator iter_t;
40 iter_t found = std::lower_bound(anchors.begin(), anchors.end(), make_pair(cssPosition, -1),
41 compareAnchorsOnCssPos);
42 if (found != anchors.end() && (*found).first == cssPosition) {
43 return &(*found);
44 } else {
45 return NULL;
46 }
47 }
48
49 static inline Interval next(const Interval& v, int upperBound)
50 {
51 return Interval(min(v.Begin + 1, upperBound), min(v.End + 1, upperBound));
52 }
53
54 static inline Interval prev(const Interval& v, int lowerBound = 0)
55 {
56 return Interval(max(v.Begin - 1, lowerBound), max(v.End - 1, lowerBound));
57 }
58
59 inline Interval rangeIntersection(const Interval& range1, const Interval& range2)
60 {
61 return Interval(max(range1.Begin, range2.Begin), min(range1.End, range2.End));
62 }
63
64 SdpRangeFinder::~SdpRangeFinder() {}
65
66 void SdpRangeFinder::InitRangeFinder(const PoaGraphImpl& poaGraph,
67 const std::vector<Vertex>& consensusPath,
68 const std::string& consensusSequence,
69 const std::string& readSequence)
70 {
71 #if DEBUG_RANGE_FINDER
72 poaGraph.WriteGraphVizFile("debug-graph.dot", PoaGraph::VERBOSE_NODES, NULL);
73 #endif
74 // Clear prexisting state first!
75 alignableReadIntervalByVertex_.clear();
76
77 const int readLength = readSequence.size();
78
79 SdpAnchorVector anchors = FindAnchors(consensusSequence, readSequence);
80
81 std::map<VD, optional<Interval>> directRanges;
82 std::map<VD, Interval> fwdMarks, revMarks;
83
84 std::vector<VD> sortedVertices(num_vertices(poaGraph.g_));
85 topological_sort(poaGraph.g_, sortedVertices.rbegin());
86 foreach (VD v, sortedVertices) {
87 directRanges[v] = boost::none;
88 }
89
90 // Find the "direct ranges" implied by the anchors between the
91 // css and this read. Possibly null.
92 for (size_t cssPos = 0; cssPos < consensusPath.size(); cssPos++) {
93 Vertex vExt = consensusPath[cssPos];
94 VD v = poaGraph.internalize(vExt);
95 const SdpAnchor* anchor = binarySearchAnchors(anchors, cssPos);
96 if (anchor != NULL) {
97 #if DEBUG_RANGE_FINDER
98 cout << "Anchor: " << anchor->first << "-" << anchor->second << " (Vertex " << vExt
99 << ")" << endl;
100 #endif
101 directRanges[v] = Interval(max(int(anchor->second) - WIDTH, 0),
102 min(int(anchor->second) + WIDTH, readLength));
103 } else {
104 directRanges[v] = boost::none;
105 }
106 }
107
108 // Use the direct ranges as a seed and perform a forward recursion,
109 // letting a node with null direct range have a range that is the
110 // union of the "forward stepped" ranges of its predecessors
111 foreach (VD v, sortedVertices) {
112 optional<Interval> directRange = directRanges.at(v);
113 if (directRange) {
114 fwdMarks[v] = directRange.get();
115 } else {
116 std::vector<Interval> predRangesStepped;
117 foreach (ED e, inEdges(v, poaGraph.g_)) {
118 VD pred = source(e, poaGraph.g_);
119 Interval predRangeStepped = next(fwdMarks.at(pred), readLength);
120 predRangesStepped.push_back(predRangeStepped);
121 }
122 fwdMarks[v] = RangeUnion(predRangesStepped);
123 }
124 }
125
126 // Do the same thing, but as a backwards recursion
127 foreach (VD v, make_pair(sortedVertices.rbegin(), sortedVertices.rend())) {
128 optional<Interval> directRange = directRanges.at(v);
129 if (directRange) {
130 revMarks[v] = directRange.get();
131 } else {
132 std::vector<Interval> succRangesStepped;
133 foreach (ED e, out_edges(v, poaGraph.g_)) {
134 VD succ = target(e, poaGraph.g_);
135 Interval succRangeStepped = prev(revMarks.at(succ), 0);
136 succRangesStepped.push_back(succRangeStepped);
137 }
138 revMarks[v] = RangeUnion(succRangesStepped);
139 }
140 }
141
142 // take hulls of extents from forward and reverse recursions
143 foreach (VD v, sortedVertices) {
144 Vertex vExt = poaGraph.externalize(v);
145 alignableReadIntervalByVertex_[vExt] = RangeUnion(fwdMarks.at(v), revMarks.at(v));
146 #if DEBUG_RANGE_FINDER
147 cout << vExt << " range = [" << alignableReadIntervalByVertex_[v].Begin << ", "
148 << alignableReadIntervalByVertex_[v].End << ")" << endl;
149 #endif
150 }
151 }
152
153 Interval SdpRangeFinder::FindAlignableRange(Vertex v)
154 {
155 return alignableReadIntervalByVertex_.at(v);
156 }
157 }
158 }