| 1 |
#include <pbcopper/poa/PoaGraph.h> |
| 2 |
|
| 3 |
#include "PoaGraphImpl.h" |
| 4 |
#include "VectorL.h" |
| 5 |
|
| 6 |
#include <boost/foreach.hpp> |
| 7 |
#include <boost/graph/topological_sort.hpp> |
| 8 |
#include <boost/unordered_set.hpp> |
| 9 |
|
| 10 |
#include <limits> |
| 11 |
#include <sstream> |
| 12 |
|
| 13 |
#include <cstddef> |
| 14 |
|
| 15 |
namespace PacBio { |
| 16 |
namespace Poa { |
| 17 |
namespace detail { |
| 18 |
|
| 19 |
std::string sequenceAlongPath(const BoostGraph& /*unused*/, const VertexInfoMap& vertexInfoMap, |
| 20 |
const std::vector<VD>& path) |
| 21 |
{ |
| 22 |
std::ostringstream ss; |
| 23 |
for (const VD v : path) { |
| 24 |
ss << vertexInfoMap[v].Base; |
| 25 |
} |
| 26 |
return ss.str(); |
| 27 |
} |
| 28 |
|
| 29 |
boost::unordered_set<VD> SpanningDFS(const VD start, const VD end, const BoostGraph& g) |
| 30 |
{ |
| 31 |
std::vector<VD> stack; |
| 32 |
boost::unordered_set<VD> fwd; |
| 33 |
boost::unordered_set<VD> rev; |
| 34 |
// find all vertices reachable from start |
| 35 |
stack.push_back(start); |
| 36 |
do { |
| 37 |
VD v = stack.back(); |
| 38 |
stack.pop_back(); |
| 39 |
// mark those we've already visited, |
| 40 |
// if so, skip |
| 41 |
if (fwd.find(v) != fwd.end()) { |
| 42 |
continue; |
| 43 |
} |
| 44 |
fwd.insert(v); |
| 45 |
BOOST_FOREACH (const ED& e, out_edges(v, g)) { |
| 46 |
stack.push_back(target(e, g)); |
| 47 |
} |
| 48 |
} while (!stack.empty()); |
| 49 |
// find all vertices that can reach end |
| 50 |
// FROM start |
| 51 |
stack.push_back(end); |
| 52 |
do { |
| 53 |
VD v = stack.back(); |
| 54 |
stack.pop_back(); |
| 55 |
// if it's not been visited in the forward pass, |
| 56 |
// or we've already visited it here, skip |
| 57 |
if (fwd.find(v) == fwd.end() || rev.find(v) != rev.end()) { |
| 58 |
continue; |
| 59 |
} |
| 60 |
rev.insert(v); |
| 61 |
BOOST_FOREACH (const ED& e, in_edges(v, g)) { |
| 62 |
stack.push_back(source(e, g)); |
| 63 |
} |
| 64 |
} while (!stack.empty()); |
| 65 |
return rev; |
| 66 |
} |
| 67 |
|
| 68 |
std::vector<VD> PoaGraphImpl::sortedVertices() const |
| 69 |
{ |
| 70 |
std::vector<VD> sv(num_vertices(g_)); |
| 71 |
topological_sort(g_, sv.rbegin()); |
| 72 |
return sv; |
| 73 |
} |
| 74 |
|
| 75 |
void PoaGraphImpl::tagSpan(VD start, VD end) |
| 76 |
{ |
| 77 |
boost::unordered_set<VD> vertices = SpanningDFS(start, end, g_); |
| 78 |
for (const VD v : vertices) { |
| 79 |
vertexInfoMap_[v].SpanningReads++; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
std::vector<VD> PoaGraphImpl::consensusPath(Align::AlignMode mode, int minCoverage) const |
| 84 |
{ |
| 85 |
// Pat's note on the approach here: |
| 86 |
// |
| 87 |
// "A node gets a score of NumReads if all reads go through |
| 88 |
// it, and a score of -NumReads if no reads go through it The |
| 89 |
// shift of -0.0001 breaks ties in favor of skipping |
| 90 |
// half-full nodes. In the 2 reads case this will get rid of |
| 91 |
// insertions which are the more common error." |
| 92 |
// |
| 93 |
// The interpretation of minCoverage (which is applicable only |
| 94 |
// for LOCAL, SEMIGLOBAL modes) is that it represents |
| 95 |
// application-specific knowledge of the basal coverage level |
| 96 |
// of reads in the template, such that if a node is contained |
| 97 |
// in fewer than minCoverage reads, it will be penalized |
| 98 |
// against inclusion in the consensus. |
| 99 |
int totalReads = NumReads(); |
| 100 |
|
| 101 |
std::list<VD> path; |
| 102 |
std::list<VD> sortedVerticesLocal(num_vertices(g_)); |
| 103 |
topological_sort(g_, sortedVerticesLocal.rbegin()); |
| 104 |
boost::unordered_map<VD, VD> bestPrevVertex; |
| 105 |
|
| 106 |
// ignore ^ and $ |
| 107 |
// TODO(dalexander): find a cleaner way to do this |
| 108 |
vertexInfoMap_[sortedVerticesLocal.front()].ReachingScore = 0; |
| 109 |
sortedVerticesLocal.pop_back(); |
| 110 |
sortedVerticesLocal.pop_front(); |
| 111 |
|
| 112 |
VD bestVertex = null_vertex; |
| 113 |
float bestReachingScore = -FLT_MAX; |
| 114 |
for (const VD v : sortedVerticesLocal) { |
| 115 |
PoaNode& vInfo = vertexInfoMap_[v]; |
| 116 |
int containingReads = vInfo.Reads; |
| 117 |
int spanningReads = vInfo.SpanningReads; |
| 118 |
float score = |
| 119 |
(mode != Align::AlignMode::GLOBAL) |
| 120 |
? (2 * containingReads - 1 * std::max(spanningReads, minCoverage) - 0.0001F) |
| 121 |
: (2 * containingReads - 1 * totalReads - 0.0001F); |
| 122 |
vInfo.Score = score; |
| 123 |
vInfo.ReachingScore = score; |
| 124 |
bestPrevVertex[v] = null_vertex; |
| 125 |
for (const ED& e : inEdges(v, g_)) { |
| 126 |
VD sourceVertex = source(e, g_); |
| 127 |
float rsc = score + vertexInfoMap_[sourceVertex].ReachingScore; |
| 128 |
if (rsc > vInfo.ReachingScore) { |
| 129 |
vInfo.ReachingScore = rsc; |
| 130 |
bestPrevVertex[v] = sourceVertex; |
| 131 |
} |
| 132 |
if (rsc > bestReachingScore) { |
| 133 |
bestVertex = v; |
| 134 |
bestReachingScore = rsc; |
| 135 |
} |
| 136 |
// if the score is the same, the order we've encountered vertices |
| 137 |
// might not be deterministic. Fix this by comparing on |
| 138 |
// vertex_index |
| 139 |
else if ((rsc == bestReachingScore) && |
| 140 |
(get(boost::vertex_index, g_, v) < get(boost::vertex_index, g_, bestVertex))) { |
| 141 |
bestVertex = v; |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
assert(bestVertex != null_vertex); |
| 146 |
|
| 147 |
// trace back from best-scoring vertex |
| 148 |
VD v = bestVertex; |
| 149 |
while (v != null_vertex) { |
| 150 |
path.push_front(v); |
| 151 |
v = bestPrevVertex[v]; |
| 152 |
} |
| 153 |
return std::vector<VD>(path.begin(), path.end()); |
| 154 |
} |
| 155 |
|
| 156 |
void PoaGraphImpl::threadFirstRead(std::string sequence, std::vector<Vertex>* outputPath) |
| 157 |
{ |
| 158 |
// first sequence in the alignment |
| 159 |
VD u = null_vertex; |
| 160 |
VD v; |
| 161 |
VD startSpanVertex = null_vertex; |
| 162 |
VD endSpanVertex; |
| 163 |
int readPos = 0; |
| 164 |
|
| 165 |
if (outputPath) { |
| 166 |
outputPath->clear(); |
| 167 |
} |
| 168 |
|
| 169 |
for (const char base : sequence) { |
| 170 |
v = addVertex(base); |
| 171 |
if (outputPath) { |
| 172 |
outputPath->push_back(externalize(v)); |
| 173 |
} |
| 174 |
if (readPos == 0) { |
| 175 |
add_edge(enterVertex_, v, g_); |
| 176 |
startSpanVertex = v; |
| 177 |
} else { |
| 178 |
add_edge(u, v, g_); |
| 179 |
} |
| 180 |
u = v; |
| 181 |
readPos++; |
| 182 |
} |
| 183 |
assert(startSpanVertex != null_vertex); |
| 184 |
assert(u != null_vertex); |
| 185 |
endSpanVertex = u; |
| 186 |
add_edge(u, exitVertex_, g_); // terminus -> $ |
| 187 |
tagSpan(startSpanVertex, endSpanVertex); |
| 188 |
} |
| 189 |
|
| 190 |
void PoaGraphImpl::tracebackAndThread(std::string sequence, |
| 191 |
const AlignmentColumnMap& alignmentColumnForVertex, |
| 192 |
Align::AlignMode alignMode, std::vector<Vertex>* outputPath) |
| 193 |
{ |
| 194 |
const int I = sequence.length(); |
| 195 |
|
| 196 |
// perform traceback from (I,$), threading the new sequence into |
| 197 |
// the graph as we go. |
| 198 |
int i = I; |
| 199 |
VD v = null_vertex; |
| 200 |
VD forkVertex = null_vertex; |
| 201 |
VD u = exitVertex_; |
| 202 |
int span = 0; |
| 203 |
VD startSpanVertex; |
| 204 |
VD endSpanVertex = alignmentColumnForVertex.at(exitVertex_)->PreviousVertex[I]; |
| 205 |
|
| 206 |
if (outputPath) { |
| 207 |
outputPath->resize(I); |
| 208 |
std::fill(outputPath->begin(), outputPath->end(), std::numeric_limits<size_t>::max()); |
| 209 |
} |
| 210 |
|
| 211 |
#define READPOS (i - 1) |
| 212 |
#define VERTEX_ON_PATH(readPos, v) \ |
| 213 |
if (outputPath) { \ |
| 214 |
(*outputPath)[(readPos)] = externalize(v); \ |
| 215 |
} |
| 216 |
|
| 217 |
while (!(u == enterVertex_ && i == 0)) { |
| 218 |
// u -> v |
| 219 |
// u: current vertex |
| 220 |
// v: vertex last visited in traceback (could be == u) |
| 221 |
// forkVertex: the vertex that will be the target of a new edge |
| 222 |
|
| 223 |
/* Vertex uExt = */ this->externalize(u); // DEBUGGING |
| 224 |
/* Vertex vExt = */ this->externalize(v); // DEBUGGING |
| 225 |
|
| 226 |
const AlignmentColumn* const curCol = alignmentColumnForVertex.at(u).get(); |
| 227 |
assert(curCol != nullptr); |
| 228 |
PoaNode& curNodeInfo = vertexInfoMap_[u]; |
| 229 |
VD prevVertex = curCol->PreviousVertex[i]; |
| 230 |
MoveType reachingMove = curCol->ReachingMove[i]; |
| 231 |
|
| 232 |
if (reachingMove == StartMove) { |
| 233 |
assert(v != null_vertex); |
| 234 |
|
| 235 |
if (forkVertex == null_vertex) { |
| 236 |
forkVertex = v; |
| 237 |
} |
| 238 |
// In local model thread read bases, adjusting i (should stop at 0) |
| 239 |
while (i > 0) { |
| 240 |
assert(alignMode == Align::AlignMode::LOCAL); |
| 241 |
VD newForkVertex = addVertex(sequence[READPOS], 1, span); |
| 242 |
add_edge(newForkVertex, forkVertex, g_); |
| 243 |
VERTEX_ON_PATH(READPOS, newForkVertex); |
| 244 |
forkVertex = newForkVertex; |
| 245 |
i--; |
| 246 |
} |
| 247 |
} else if (reachingMove == EndMove) { |
| 248 |
assert(forkVertex == null_vertex && u == exitVertex_ && v == null_vertex); |
| 249 |
|
| 250 |
forkVertex = exitVertex_; |
| 251 |
|
| 252 |
if (alignMode == Align::AlignMode::LOCAL) { |
| 253 |
// Find the row # we are coming from, walk |
| 254 |
// back to there, threading read bases onto |
| 255 |
// graph via forkVertex, adjusting i. |
| 256 |
const AlignmentColumn* const prevCol = |
| 257 |
alignmentColumnForVertex.at(prevVertex).get(); |
| 258 |
int prevRow = ArgMax(prevCol->Score); |
| 259 |
|
| 260 |
while (i > prevRow) { |
| 261 |
VD newForkVertex = addVertex(sequence[READPOS], 1, span); |
| 262 |
add_edge(newForkVertex, forkVertex, g_); |
| 263 |
VERTEX_ON_PATH(READPOS, newForkVertex); |
| 264 |
forkVertex = newForkVertex; |
| 265 |
i--; |
| 266 |
} |
| 267 |
} |
| 268 |
} else if (reachingMove == MatchMove) { |
| 269 |
VERTEX_ON_PATH(READPOS, u); |
| 270 |
// if there is an extant forkVertex, join it |
| 271 |
if (forkVertex != null_vertex) { |
| 272 |
add_edge(u, forkVertex, g_); |
| 273 |
forkVertex = null_vertex; |
| 274 |
} |
| 275 |
// add to existing node |
| 276 |
curNodeInfo.Reads++; |
| 277 |
i--; |
| 278 |
} else if (reachingMove == DeleteMove) { |
| 279 |
if (forkVertex == null_vertex) { |
| 280 |
forkVertex = v; |
| 281 |
} |
| 282 |
} else if (reachingMove == ExtraMove || reachingMove == MismatchMove) { |
| 283 |
// begin a new arc with this read base |
| 284 |
VD newForkVertex = addVertex(sequence[READPOS], 1, span); |
| 285 |
if (forkVertex == null_vertex) { |
| 286 |
forkVertex = v; |
| 287 |
} |
| 288 |
add_edge(newForkVertex, forkVertex, g_); |
| 289 |
VERTEX_ON_PATH(READPOS, newForkVertex); |
| 290 |
forkVertex = newForkVertex; |
| 291 |
i--; |
| 292 |
} else { |
| 293 |
throw std::runtime_error("unreachable"); |
| 294 |
} |
| 295 |
|
| 296 |
v = u; |
| 297 |
u = prevVertex; |
| 298 |
span = curNodeInfo.SpanningReads; |
| 299 |
} |
| 300 |
startSpanVertex = v; |
| 301 |
|
| 302 |
// if there is an extant forkVertex, join it to enterVertex |
| 303 |
if (forkVertex != null_vertex) { |
| 304 |
add_edge(enterVertex_, forkVertex, g_); |
| 305 |
startSpanVertex = forkVertex; |
| 306 |
forkVertex = null_vertex; |
| 307 |
} |
| 308 |
|
| 309 |
if (startSpanVertex != exitVertex_) { |
| 310 |
tagSpan(startSpanVertex, endSpanVertex); |
| 311 |
} |
| 312 |
|
| 313 |
// all filled in? |
| 314 |
assert(outputPath == nullptr || |
| 315 |
std::find(outputPath->begin(), outputPath->end(), std::numeric_limits<size_t>::max()) == |
| 316 |
outputPath->end()); |
| 317 |
|
| 318 |
#undef READPOS |
| 319 |
#undef VERTEX_ON_PATH |
| 320 |
} |
| 321 |
|
| 322 |
} // namespace detail |
| 323 |
} // namespace Poa |
| 324 |
} // namespace PacBio |