| 1 |
// Author: David Alexander |
| 2 |
|
| 3 |
#include <limits> |
| 4 |
|
| 5 |
#include <ConsensusCore/Matrix/VectorL.hpp> |
| 6 |
#include <ConsensusCore/Poa/PoaGraph.hpp> |
| 7 |
#include <ConsensusCore/Utils.hpp> |
| 8 |
|
| 9 |
#include <boost/graph/topological_sort.hpp> |
| 10 |
|
| 11 |
#include "PoaGraphImpl.hpp" |
| 12 |
|
| 13 |
namespace ConsensusCore { |
| 14 |
namespace detail { |
| 15 |
|
| 16 |
std::string sequenceAlongPath(const BoostGraph&, const VertexInfoMap& vertexInfoMap, |
| 17 |
const std::vector<VD>& path) |
| 18 |
{ |
| 19 |
std::stringstream ss; |
| 20 |
foreach (VD v, path) { |
| 21 |
ss << vertexInfoMap[v].Base; |
| 22 |
} |
| 23 |
return ss.str(); |
| 24 |
} |
| 25 |
|
| 26 |
void PoaGraphImpl::tagSpan(VD start, VD end) |
| 27 |
{ |
| 28 |
// cout << "Tagging span " << start << " to " << end << endl; |
| 29 |
std::list<VD> sortedVertices(num_vertices(g_)); |
| 30 |
topological_sort(g_, sortedVertices.rbegin()); |
| 31 |
bool spanning = false; |
| 32 |
foreach (VD v, sortedVertices) { |
| 33 |
if (v == start) { |
| 34 |
spanning = true; |
| 35 |
} |
| 36 |
if (v == end) { |
| 37 |
break; |
| 38 |
} |
| 39 |
if (spanning) { |
| 40 |
vertexInfoMap_[v].SpanningReads++; |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
std::vector<VD> PoaGraphImpl::consensusPath(AlignMode mode, int minCoverage) const |
| 46 |
{ |
| 47 |
// Pat's note on the approach here: |
| 48 |
// |
| 49 |
// "A node gets a score of NumReads if all reads go through |
| 50 |
// it, and a score of -NumReads if no reads go through it The |
| 51 |
// shift of -0.0001 breaks ties in favor of skipping |
| 52 |
// half-full nodes. In the 2 reads case this will get rid of |
| 53 |
// insertions which are the more common error." |
| 54 |
// |
| 55 |
// The interpretation of minCoverage (which is applicable only |
| 56 |
// for LOCAL, SEMIGLOBAL modes) is that it represents |
| 57 |
// application-specific knowledge of the basal coverage level |
| 58 |
// of reads in the template, such that if a node is contained |
| 59 |
// in fewer than minCoverage reads, it will be penalized |
| 60 |
// against inclusion in the consensus. |
| 61 |
int totalReads = NumReads(); |
| 62 |
|
| 63 |
std::list<VD> path; |
| 64 |
std::list<VD> sortedVertices(num_vertices(g_)); |
| 65 |
topological_sort(g_, sortedVertices.rbegin()); |
| 66 |
unordered_map<VD, VD> bestPrevVertex; |
| 67 |
|
| 68 |
// ignore ^ and $ |
| 69 |
// TODO(dalexander): find a cleaner way to do this |
| 70 |
vertexInfoMap_[sortedVertices.front()].ReachingScore = 0; |
| 71 |
sortedVertices.pop_back(); |
| 72 |
sortedVertices.pop_front(); |
| 73 |
|
| 74 |
VD bestVertex = null_vertex; |
| 75 |
float bestReachingScore = -FLT_MAX; |
| 76 |
foreach (VD v, sortedVertices) { |
| 77 |
PoaNode& vInfo = vertexInfoMap_[v]; |
| 78 |
int containingReads = vInfo.Reads; |
| 79 |
int spanningReads = vInfo.SpanningReads; |
| 80 |
float score = |
| 81 |
(mode != GLOBAL) |
| 82 |
? (2 * containingReads - 1 * std::max(spanningReads, minCoverage) - 0.0001f) |
| 83 |
: (2 * containingReads - 1 * totalReads - 0.0001f); |
| 84 |
vInfo.Score = score; |
| 85 |
vInfo.ReachingScore = score; |
| 86 |
bestPrevVertex[v] = null_vertex; |
| 87 |
foreach (ED e, inEdges(v, g_)) { |
| 88 |
VD sourceVertex = source(e, g_); |
| 89 |
float rsc = score + vertexInfoMap_[sourceVertex].ReachingScore; |
| 90 |
if (rsc > vInfo.ReachingScore) { |
| 91 |
vInfo.ReachingScore = rsc; |
| 92 |
bestPrevVertex[v] = sourceVertex; |
| 93 |
} |
| 94 |
if (rsc > bestReachingScore) { |
| 95 |
bestVertex = v; |
| 96 |
bestReachingScore = rsc; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
assert(bestVertex != null_vertex); |
| 101 |
|
| 102 |
// trace back from best-scoring vertex |
| 103 |
VD v = bestVertex; |
| 104 |
while (v != null_vertex) { |
| 105 |
path.push_front(v); |
| 106 |
v = bestPrevVertex[v]; |
| 107 |
} |
| 108 |
return std::vector<VD>(path.begin(), path.end()); |
| 109 |
} |
| 110 |
|
| 111 |
void PoaGraphImpl::threadFirstRead(std::string sequence, std::vector<Vertex>* outputPath) |
| 112 |
{ |
| 113 |
// first sequence in the alignment |
| 114 |
VD u = null_vertex, v; |
| 115 |
VD startSpanVertex = null_vertex, endSpanVertex; |
| 116 |
int readPos = 0; |
| 117 |
|
| 118 |
if (outputPath) { |
| 119 |
outputPath->clear(); |
| 120 |
} |
| 121 |
|
| 122 |
foreach (char base, sequence) { |
| 123 |
v = addVertex(base); |
| 124 |
if (outputPath) { |
| 125 |
outputPath->push_back(externalize(v)); |
| 126 |
} |
| 127 |
if (readPos == 0) { |
| 128 |
add_edge(enterVertex_, v, g_); |
| 129 |
startSpanVertex = v; |
| 130 |
} else { |
| 131 |
add_edge(u, v, g_); |
| 132 |
} |
| 133 |
u = v; |
| 134 |
readPos++; |
| 135 |
} |
| 136 |
assert(startSpanVertex != null_vertex); |
| 137 |
assert(u != null_vertex); |
| 138 |
endSpanVertex = u; |
| 139 |
add_edge(u, exitVertex_, g_); // terminus -> $ |
| 140 |
tagSpan(startSpanVertex, endSpanVertex); |
| 141 |
} |
| 142 |
|
| 143 |
void PoaGraphImpl::tracebackAndThread(std::string sequence, |
| 144 |
const AlignmentColumnMap& alignmentColumnForVertex, |
| 145 |
AlignMode alignMode, std::vector<Vertex>* outputPath) |
| 146 |
{ |
| 147 |
const int I = sequence.length(); |
| 148 |
|
| 149 |
// perform traceback from (I,$), threading the new sequence into the graph as |
| 150 |
// we go. |
| 151 |
int i = I; |
| 152 |
const AlignmentColumn* curCol; |
| 153 |
VD v = null_vertex, forkVertex = null_vertex; |
| 154 |
VD u = exitVertex_; |
| 155 |
VD startSpanVertex; |
| 156 |
VD endSpanVertex = alignmentColumnForVertex.at(exitVertex_)->PreviousVertex[I]; |
| 157 |
|
| 158 |
if (outputPath) { |
| 159 |
outputPath->resize(I); |
| 160 |
std::fill(outputPath->begin(), outputPath->end(), std::numeric_limits<size_t>::max()); |
| 161 |
} |
| 162 |
|
| 163 |
#define READPOS (i - 1) |
| 164 |
#define VERTEX_ON_PATH(readPos, v) \ |
| 165 |
if (outputPath) { \ |
| 166 |
(*outputPath)[(readPos)] = externalize(v); \ |
| 167 |
} |
| 168 |
|
| 169 |
while (!(u == enterVertex_ && i == 0)) { |
| 170 |
// u -> v |
| 171 |
// u: current vertex |
| 172 |
// v: vertex last visited in traceback (could be == u) |
| 173 |
// forkVertex: the vertex that will be the target of a new edge |
| 174 |
curCol = alignmentColumnForVertex.at(u); |
| 175 |
assert(curCol != NULL); |
| 176 |
PoaNode& curNodeInfo = vertexInfoMap_[u]; |
| 177 |
VD prevVertex = curCol->PreviousVertex[i]; |
| 178 |
MoveType reachingMove = curCol->ReachingMove[i]; |
| 179 |
|
| 180 |
if (reachingMove == StartMove) { |
| 181 |
assert(v != null_vertex); |
| 182 |
|
| 183 |
if (forkVertex == null_vertex) { |
| 184 |
forkVertex = v; |
| 185 |
} |
| 186 |
// In local model thread read bases, adjusting i (should stop at 0) |
| 187 |
while (i > 0) { |
| 188 |
assert(alignMode == LOCAL); |
| 189 |
VD newForkVertex = addVertex(sequence[READPOS]); |
| 190 |
add_edge(newForkVertex, forkVertex, g_); |
| 191 |
VERTEX_ON_PATH(READPOS, newForkVertex); |
| 192 |
forkVertex = newForkVertex; |
| 193 |
i--; |
| 194 |
} |
| 195 |
} else if (reachingMove == EndMove) { |
| 196 |
assert(forkVertex == null_vertex && u == exitVertex_ && v == null_vertex); |
| 197 |
|
| 198 |
forkVertex = exitVertex_; |
| 199 |
|
| 200 |
if (alignMode == LOCAL) { |
| 201 |
// Find the row # we are coming from, walk |
| 202 |
// back to there, threading read bases onto |
| 203 |
// graph via forkVertex, adjusting i. |
| 204 |
const AlignmentColumn* prevCol = alignmentColumnForVertex.at(prevVertex); |
| 205 |
int prevRow = ArgMax(prevCol->Score); |
| 206 |
|
| 207 |
while (i > prevRow) { |
| 208 |
VD newForkVertex = addVertex(sequence[READPOS]); |
| 209 |
add_edge(newForkVertex, forkVertex, g_); |
| 210 |
VERTEX_ON_PATH(READPOS, newForkVertex); |
| 211 |
forkVertex = newForkVertex; |
| 212 |
i--; |
| 213 |
} |
| 214 |
} |
| 215 |
} else if (reachingMove == MatchMove) { |
| 216 |
VERTEX_ON_PATH(READPOS, u); |
| 217 |
// if there is an extant forkVertex, join it |
| 218 |
if (forkVertex != null_vertex) { |
| 219 |
add_edge(u, forkVertex, g_); |
| 220 |
forkVertex = null_vertex; |
| 221 |
} |
| 222 |
// add to existing node |
| 223 |
curNodeInfo.Reads++; |
| 224 |
i--; |
| 225 |
} else if (reachingMove == DeleteMove) { |
| 226 |
if (forkVertex == null_vertex) { |
| 227 |
forkVertex = v; |
| 228 |
} |
| 229 |
} else if (reachingMove == ExtraMove || reachingMove == MismatchMove) { |
| 230 |
// begin a new arc with this read base |
| 231 |
VD newForkVertex = addVertex(sequence[READPOS]); |
| 232 |
if (forkVertex == null_vertex) { |
| 233 |
forkVertex = v; |
| 234 |
} |
| 235 |
add_edge(newForkVertex, forkVertex, g_); |
| 236 |
VERTEX_ON_PATH(READPOS, newForkVertex); |
| 237 |
forkVertex = newForkVertex; |
| 238 |
i--; |
| 239 |
} else { |
| 240 |
ShouldNotReachHere(); |
| 241 |
} |
| 242 |
|
| 243 |
v = u; |
| 244 |
u = prevVertex; |
| 245 |
} |
| 246 |
startSpanVertex = v; |
| 247 |
if (startSpanVertex != exitVertex_) { |
| 248 |
tagSpan(startSpanVertex, endSpanVertex); |
| 249 |
} |
| 250 |
|
| 251 |
// if there is an extant forkVertex, join it to enterVertex |
| 252 |
if (forkVertex != null_vertex) { |
| 253 |
add_edge(enterVertex_, forkVertex, g_); |
| 254 |
forkVertex = null_vertex; |
| 255 |
} |
| 256 |
|
| 257 |
// all filled in? |
| 258 |
assert(outputPath == NULL || |
| 259 |
std::find(outputPath->begin(), outputPath->end(), std::numeric_limits<size_t>::max()) == |
| 260 |
outputPath->end()); |
| 261 |
|
| 262 |
#undef READPOS |
| 263 |
#undef VERTEX_ON_PATH |
| 264 |
} |
| 265 |
|
| 266 |
static boost::unordered_set<VD> childVertices(VD v, const BoostGraph& g) |
| 267 |
{ |
| 268 |
boost::unordered_set<VD> result; |
| 269 |
foreach (ED e, out_edges(v, g)) { |
| 270 |
result.insert(target(e, g)); |
| 271 |
} |
| 272 |
return result; |
| 273 |
} |
| 274 |
|
| 275 |
static boost::unordered_set<VD> parentVertices(VD v, const BoostGraph& g) |
| 276 |
{ |
| 277 |
boost::unordered_set<VD> result; |
| 278 |
foreach (ED e, in_edges(v, g)) { |
| 279 |
result.insert(source(e, g)); |
| 280 |
} |
| 281 |
return result; |
| 282 |
} |
| 283 |
|
| 284 |
vector<ScoredMutation>* PoaGraphImpl::findPossibleVariants( |
| 285 |
const std::vector<Vertex>& bestPath) const |
| 286 |
{ |
| 287 |
std::vector<VD> bestPath_ = internalizePath(bestPath); |
| 288 |
|
| 289 |
// Return value will be deallocated by PoaConsensus destructor. |
| 290 |
vector<ScoredMutation>* variants = new vector<ScoredMutation>(); |
| 291 |
|
| 292 |
for (int i = 2; i < static_cast<int>(bestPath_.size()) - 2; i++) // NOLINT |
| 293 |
{ |
| 294 |
VD v = bestPath_[i]; |
| 295 |
boost::unordered_set<VD> children = childVertices(v, g_); |
| 296 |
|
| 297 |
// Look for a direct edge from the current node to the node |
| 298 |
// two spaces down---suggesting a deletion with respect to |
| 299 |
// the consensus sequence. |
| 300 |
if (children.find(bestPath_[i + 2]) != children.end()) { |
| 301 |
float score = -vertexInfoMap_[bestPath_[i + 1]].Score; |
| 302 |
variants->push_back(Mutation(DELETION, i + 1, '-').WithScore(score)); |
| 303 |
} |
| 304 |
|
| 305 |
// Look for a child node that connects immediately back to i + 1. |
| 306 |
// This indicates we should try inserting the base at i + 1. |
| 307 |
|
| 308 |
// Parents of (i + 1) |
| 309 |
boost::unordered_set<VD> lookBack = parentVertices(bestPath_[i + 1], g_); |
| 310 |
|
| 311 |
// (We could do this in STL using std::set sorted on score, which would then |
| 312 |
// provide an intersection mechanism (in <algorithm>) but that actually ends |
| 313 |
// up being more code. Sad.) |
| 314 |
float bestInsertScore = -FLT_MAX; |
| 315 |
VD bestInsertVertex = null_vertex; |
| 316 |
|
| 317 |
foreach (VD vi, children) { |
| 318 |
boost::unordered_set<VD>::iterator found = lookBack.find(vi); |
| 319 |
if (found != lookBack.end()) { |
| 320 |
float score = vertexInfoMap_[*found].Score; |
| 321 |
if (score > bestInsertScore) { |
| 322 |
bestInsertScore = score; |
| 323 |
bestInsertVertex = *found; |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
if (bestInsertVertex != null_vertex) { |
| 329 |
char base = vertexInfoMap_[bestInsertVertex].Base; |
| 330 |
variants->push_back(Mutation(INSERTION, i + 1, base).WithScore(bestInsertScore)); |
| 331 |
} |
| 332 |
|
| 333 |
// Look for a child node not in the consensus that connects immediately |
| 334 |
// to i + 2. This indicates we should try mismatching the base i + 1. |
| 335 |
|
| 336 |
// Parents of (i + 2) |
| 337 |
lookBack = parentVertices(bestPath_[i + 2], g_); |
| 338 |
|
| 339 |
float bestMismatchScore = -FLT_MAX; |
| 340 |
VD bestMismatchVertex = null_vertex; |
| 341 |
|
| 342 |
foreach (VD vi, children) { |
| 343 |
if (vi == bestPath_[i + 1]) continue; |
| 344 |
|
| 345 |
boost::unordered_set<VD>::iterator found = lookBack.find(vi); |
| 346 |
if (found != lookBack.end()) { |
| 347 |
float score = vertexInfoMap_[*found].Score; |
| 348 |
if (score > bestMismatchScore) { |
| 349 |
bestMismatchScore = score; |
| 350 |
bestMismatchVertex = *found; |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
if (bestMismatchVertex != null_vertex) { |
| 356 |
// TODO(dalexander): As implemented (compatibility), this returns |
| 357 |
// the score of the mismatch node. I think it should return the score |
| 358 |
// difference, no? |
| 359 |
char base = vertexInfoMap_[bestMismatchVertex].Base; |
| 360 |
variants->push_back(Mutation(SUBSTITUTION, i + 1, base).WithScore(bestMismatchScore)); |
| 361 |
} |
| 362 |
} |
| 363 |
return variants; |
| 364 |
} |
| 365 |
} |
| 366 |
} // ConsensusCore::detail |