1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
//Copyright 2017 Ryan Wick
//This file is part of Bandage
//Bandage is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//Bandage is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Bandage. If not, see <http://www.gnu.org/licenses/>.
#include "debruijnedge.h"
#include <math.h>
#include "../program/settings.h"
#include "ogdfnode.h"
#include <QApplication>
#include "../program/settings.h"
#include "../program/globals.h"
#include "assemblygraph.h"
DeBruijnEdge::DeBruijnEdge(DeBruijnNode *startingNode, DeBruijnNode *endingNode) :
m_startingNode(startingNode), m_endingNode(endingNode), m_graphicsItemEdge(0),
m_drawn(false), m_overlapType(UNKNOWN_OVERLAP), m_overlap(0)
{
}
//This function assumes that the parameter node pointer is one of the two nodes
//in this edge, and it returns the other one.
DeBruijnNode * DeBruijnEdge::getOtherNode(const DeBruijnNode * node) const
{
if (node == m_startingNode)
return m_endingNode;
else
return m_startingNode;
}
//This function determines whether the edge should be drawn to the screen.
bool DeBruijnEdge::edgeIsVisible() const
{
//If the program is in double mode, then draw any edge where both of its
//nodes are drawn.
if (g_settings->doubleMode)
return m_startingNode->isDrawn() && m_endingNode->isDrawn();
//If the program is in single mode, then draw any edge where both of its
//nodes or their reverse complements are drawn.
else
{
bool drawEdge = (m_startingNode->isDrawn() || m_startingNode->getReverseComplement()->isDrawn())
&& (m_endingNode->isDrawn() || m_endingNode->getReverseComplement()->isDrawn());
if (!drawEdge)
return false;
//But it is also necessary to avoid drawing both an edge and its
//reverse complement edge.
return isPositiveEdge();
}
}
//This function says whether an edge is 'positive'. This is used to distinguish
//an edge from its reverse complement - i.e. half of the graph edges are
//positive and their reverse complements are negative.
//When one node in the edge is positive and the other is negative, then the
//choice is somewhat arbitrary.
bool DeBruijnEdge::isPositiveEdge() const
{
//If both nodes have a positive number, show this edge, and not
//the reverse complement where both nodes are negative.
if (m_startingNode->isPositiveNode() && m_endingNode->isPositiveNode())
return true;
if (m_startingNode->isNegativeNode() && m_endingNode->isNegativeNode())
return false;
//Edges that are their own reverse complement are considered positive (but
//will not have a negative counterpart).
if (isOwnReverseComplement())
return true;
//If the code got here, then one node is positive and the other
//negative. In this case, just choose the one with the first name
//alphabetically - an arbitrary choice, but at least it is
//consistent.
return (m_startingNode->getName() > m_reverseComplement->m_startingNode->getName());
}
void DeBruijnEdge::addToOgdfGraph(ogdf::Graph * ogdfGraph, ogdf::EdgeArray<double> * edgeArray) const
{
ogdf::node firstEdgeOgdfNode;
ogdf::node secondEdgeOgdfNode;
if (m_startingNode->inOgdf())
firstEdgeOgdfNode = m_startingNode->getOgdfNode()->getLast();
else if (m_startingNode->getReverseComplement()->inOgdf())
firstEdgeOgdfNode = m_startingNode->getReverseComplement()->getOgdfNode()->getFirst();
else
return; //Ending node or its reverse complement isn't in OGDF
if (m_endingNode->inOgdf())
secondEdgeOgdfNode = m_endingNode->getOgdfNode()->getFirst();
else if (m_endingNode->getReverseComplement()->inOgdf())
secondEdgeOgdfNode = m_endingNode->getReverseComplement()->getOgdfNode()->getLast();
else
return; //Ending node or its reverse complement isn't in OGDF
//If this in an edge connected a single-segment node to itself, then we
//don't want to put it in the OGDF graph, because it would be redundant
//with the node segment (and created conflict with the node/edge length).
if (m_startingNode == m_endingNode)
{
if (m_startingNode->getNumberOfOgdfGraphEdges(m_startingNode->getDrawnNodeLength()) == 1)
return;
}
ogdf::edge newEdge = ogdfGraph->newEdge(firstEdgeOgdfNode, secondEdgeOgdfNode);
(*edgeArray)[newEdge] = g_settings->edgeLength;
}
//This function traces all possible paths from this edge.
//It proceeds a number of steps, as determined by a setting.
//If forward is true, it looks in a forward direction (starting nodes to
//ending nodes). If forward is false, it looks in a backward direction
//(ending nodes to starting nodes).
void DeBruijnEdge::tracePaths(bool forward,
int stepsRemaining,
std::vector< std::vector <DeBruijnNode *> > * allPaths,
DeBruijnNode * startingNode,
std::vector<DeBruijnNode *> pathSoFar) const
{
//This can go for a while, so keep the UI responsive.
QApplication::processEvents();
//Find the node in the direction we are tracing.
DeBruijnNode * nextNode;
if (forward)
nextNode = m_endingNode;
else
nextNode = m_startingNode;
//Add that node to the path so far.
pathSoFar.push_back(nextNode);
//If there are no steps left, then the path so far is done.
--stepsRemaining;
if (stepsRemaining == 0)
{
allPaths->push_back(pathSoFar);
return;
}
//If the code got here, then more steps remain.
//Find the edges that are in the correct direction.
std::vector<DeBruijnEdge *> nextEdges = findNextEdgesInPath(nextNode, forward);
//If there are no next edges, then we are finished with the
//path search, even though steps remain.
if (nextEdges.size() == 0)
{
allPaths->push_back(pathSoFar);
return;
}
//Call this function on all of the next edges.
//However, we also need to check to see if we are tracing a loop
//and stop if that is the case.
for (size_t i = 0; i < nextEdges.size(); ++i)
{
DeBruijnEdge * nextEdge = nextEdges[i];
//Determine the node that this next edge leads to.
DeBruijnNode * nextNextNode;
if (forward)
nextNextNode = nextEdge->m_endingNode;
else
nextNextNode = nextEdge->m_startingNode;
//If that node is the starting node, then we've made
//a full loop and the path should be considered complete.
if (nextNextNode == startingNode)
{
allPaths->push_back(pathSoFar);
continue;
}
//If that node is already in the path TWICE so far, that means
//we're caught in a loop, and we should throw this path out.
//If it appears 0 or 1 times, then continue the path search.
if (timesNodeInPath(nextNextNode, &pathSoFar) < 2)
nextEdge->tracePaths(forward, stepsRemaining, allPaths, startingNode, pathSoFar);
}
}
//This function counts how many times a node appears in a path
int DeBruijnEdge::timesNodeInPath(DeBruijnNode * node, std::vector<DeBruijnNode *> * path) const
{
int count = 0;
for (size_t i = 0; i < path->size(); ++i)
{
if ( (*path)[i] == node)
++count;
}
return count;
}
bool DeBruijnEdge::leadsOnlyToNode(bool forward,
int stepsRemaining,
DeBruijnNode * target,
std::vector<DeBruijnNode *> pathSoFar,
bool includeReverseComplement) const
{
//This can go for a while, so keep the UI responsive.
QApplication::processEvents();
//Find the node in the direction we are tracing.
DeBruijnNode * nextNode;
if (forward)
nextNode = m_endingNode;
else
nextNode = m_startingNode;
//Add that node to the path so far.
pathSoFar.push_back(nextNode);
//If this path has landed on the node from which the search began,
//that means we've followed a loop around. The search has therefore
//failed because this path could represent circular DNA that does
//not contain the target.
if (nextNode == pathSoFar[0])
return false;
//If the next node is the target, the search succeeded!
if (nextNode == target)
return true;
//If we are including reverse complements and the next node is
//the reverse complement of the target, the search succeeded!
if (includeReverseComplement && nextNode->getReverseComplement() == target)
return true;
//If there are no steps left, then the search failed.
--stepsRemaining;
if (stepsRemaining == 0)
return false;
//If the code got here, then more steps remain.
//Find the edges that are in the correct direction.
std::vector<DeBruijnEdge *> nextEdges = findNextEdgesInPath(nextNode, forward);
//If there are no next edges, then the search failed, even
//though steps remain.
if (nextEdges.size() == 0)
return false;
//In order for the search to succeed, this function needs to return true
//for all of the nextEdges.
//However, we also need to check to see if we are tracing a loop
//and stop if that is the case.
for (size_t i = 0; i < nextEdges.size(); ++i)
{
DeBruijnEdge * nextEdge = nextEdges[i];
//Determine the node that this next edge leads to.
DeBruijnNode * nextNextNode;
if (forward)
nextNextNode = nextEdge->m_endingNode;
else
nextNextNode = nextEdge->m_startingNode;
//If that node is already in the path TWICE so far, that means
//we're caught in a loop, and we should throw this path out.
//If it appears 0 or 1 times, then continue the path search.
if (timesNodeInPath(nextNextNode, &pathSoFar) < 2)
{
if ( !nextEdge->leadsOnlyToNode(forward, stepsRemaining, target, pathSoFar, includeReverseComplement) )
return false;
}
}
//If the code got here, then the search succeeded!
return true;
}
std::vector<DeBruijnEdge *> DeBruijnEdge::findNextEdgesInPath(DeBruijnNode * nextNode,
bool forward) const
{
std::vector<DeBruijnEdge *> nextEdges;
const std::vector<DeBruijnEdge *> * nextNodeEdges = nextNode->getEdgesPointer();
for (size_t i = 0; i < nextNodeEdges->size(); ++i)
{
DeBruijnEdge * edge = (*nextNodeEdges)[i];
//If forward, we're looking for edges that lead away from
//nextNode. If backward, we're looking for edges that lead
//into nextNode.
if ((forward && edge->m_startingNode == nextNode) ||
(!forward && edge->m_endingNode == nextNode))
nextEdges.push_back(edge);
}
return nextEdges;
}
//This function tries to automatically determine the overlap size
//between the two nodes. It tries each overlap size between the min
//to the max (in settings), assigning the first one it finds.
void DeBruijnEdge::autoDetermineExactOverlap()
{
m_overlap = 0;
m_overlapType = AUTO_DETERMINED_EXACT_OVERLAP;
//Find an appropriate search range
int minPossibleOverlap = std::min(m_startingNode->getLength(), m_endingNode->getLength());
if (minPossibleOverlap < g_settings->minAutoFindEdgeOverlap)
return;
int min = std::min(minPossibleOverlap, g_settings->minAutoFindEdgeOverlap);
int max = std::min(minPossibleOverlap, g_settings->maxAutoFindEdgeOverlap);
//Try each overlap in the range and set the first one found.
//However, we don't want the search to be biased towards larger
//or smaller overlaps, so start with a pseudorandom value and loop.
int testOverlap = min + (rand() % (max - min + 1));
for (int i = min; i <= max; ++i)
{
if (testExactOverlap(testOverlap))
{
m_overlap = testOverlap;
return;
}
++testOverlap;
if (testOverlap > max)
testOverlap = min;
}
}
//This function tries the given overlap between the two nodes.
//If the overlap works perfectly, it returns true.
bool DeBruijnEdge::testExactOverlap(int overlap) const
{
bool mismatchFound = false;
int seq1Offset = m_startingNode->getLength() - overlap;
//Look at each position in the overlap
for (int j = 0; j < overlap && !mismatchFound; ++j)
{
char a = m_startingNode->getBaseAt(seq1Offset + j);
char b = m_endingNode->getBaseAt(j);
if (a != b)
mismatchFound = true;
}
return !mismatchFound;
}
QByteArray DeBruijnEdge::getGfaLinkLine() const
{
DeBruijnNode * startingNode = getStartingNode();
DeBruijnNode * endingNode = getEndingNode();
QByteArray gfaLinkLine = "L\t";
gfaLinkLine += startingNode->getNameWithoutSign().toUtf8() + "\t";
gfaLinkLine += startingNode->getSign().toUtf8() + "\t";
gfaLinkLine += endingNode->getNameWithoutSign().toUtf8() + "\t";
gfaLinkLine += endingNode->getSign().toUtf8() + "\t";
//When Velvet graphs are saved to GFA, the sequences are extended to include
//the overlap. So even though this edge might have no overlap, the GFA link
//line should.
if (g_assemblyGraph->m_graphFileType == LAST_GRAPH)
gfaLinkLine += QString::number(g_assemblyGraph->m_kmer - 1).toUtf8() + "M";
else
gfaLinkLine += QString::number(getOverlap()).toUtf8() + "M";
gfaLinkLine += "\n";
return gfaLinkLine;
}
bool DeBruijnEdge::compareEdgePointers(DeBruijnEdge * a, DeBruijnEdge * b)
{
QString aStart = a->getStartingNode()->getName();
QString bStart = b->getStartingNode()->getName();
QString aStartNoSign = aStart;
aStartNoSign.chop(1);
QString bStartNoSign = bStart;
bStartNoSign.chop(1);
bool ok1;
long long aStartNumber = aStartNoSign.toLongLong(&ok1);
bool ok2;
long long bStartNumber = bStartNoSign.toLongLong(&ok2);
QString aEnd = a->getEndingNode()->getName();
QString bEnd = b->getEndingNode()->getName();
QString aEndNoSign = aEnd;
aEndNoSign.chop(1);
QString bEndNoSign = bEnd;
bEndNoSign.chop(1);
bool ok3;
long long aEndNumber = aEndNoSign.toLongLong(&ok3);
bool ok4;
long long bEndNumber = bEndNoSign.toLongLong(&ok4);
//If the node names are essentially numbers, then sort them as numbers.
if (ok1 && ok2 && ok3 && ok4)
{
if (aStartNumber != bStartNumber)
return aStartNumber < bStartNumber;
if (aStartNumber == bStartNumber)
return aEndNumber < bEndNumber;
}
//If the node names are strings, then just sort them as strings.
return aStart < bStart;
}
|