| 1 |
#include <pbcopper/pbmer/Dbg.h> |
| 2 |
|
| 3 |
#include <pbcopper/container/Unordered.h> |
| 4 |
|
| 5 |
#include <fstream> |
| 6 |
#include <iostream> |
| 7 |
#include <numeric> |
| 8 |
#include <sstream> |
| 9 |
|
| 10 |
#include <cassert> |
| 11 |
|
| 12 |
namespace PacBio { |
| 13 |
namespace Pbmer { |
| 14 |
|
| 15 |
Dbg::Dbg(uint8_t k, uint32_t nr) : kmerSize_{k}, nReads_{nr} {} |
| 16 |
|
| 17 |
int Dbg::AddKmers(const PacBio::Pbmer::Mers& m, const uint32_t rid) |
| 18 |
{ |
| 19 |
// cover the cases where the kmers are not suitable for the Dbg. |
| 20 |
if ((m.kmerSize > 31)) { |
| 21 |
return -1; |
| 22 |
} |
| 23 |
|
| 24 |
if ((m.kmerSize % 2) == 0) { |
| 25 |
return -2; |
| 26 |
} |
| 27 |
|
| 28 |
for (const auto& x : m.forward) { |
| 29 |
DnaBit niby{x.mer, static_cast<uint8_t>(x.strand == Data::Strand::FORWARD ? 0 : 1), |
| 30 |
kmerSize_}; |
| 31 |
|
| 32 |
niby.MakeLexSmaller(); |
| 33 |
|
| 34 |
if (dbg_.find(niby.mer) != dbg_.end()) { |
| 35 |
dbg_.at(niby.mer).AddLoad(rid); |
| 36 |
} else { |
| 37 |
DbgNode eg{niby, 0, nReads_}; |
| 38 |
eg.AddLoad(rid); |
| 39 |
dbg_.emplace(niby.mer, std::move(eg)); |
| 40 |
} |
| 41 |
} |
| 42 |
return 1; |
| 43 |
} |
| 44 |
|
| 45 |
void Dbg::AddVerifedKmerPairs(std::vector<DnaBit>& bits, const uint32_t rid) |
| 46 |
{ |
| 47 |
|
| 48 |
for (auto& bit : bits) { |
| 49 |
bit.MakeLexSmaller(); |
| 50 |
} |
| 51 |
|
| 52 |
auto edges = BuildVerifiedEdges(bits); |
| 53 |
|
| 54 |
for (size_t i = 0; i < bits.size(); ++i) { |
| 55 |
|
| 56 |
if (dbg_.find(bits[i].mer) != dbg_.end()) { |
| 57 |
dbg_.at(bits[i].mer).AddLoad(rid); |
| 58 |
} else { |
| 59 |
|
| 60 |
DbgNode eg{bits[i], 0, nReads_}; |
| 61 |
|
| 62 |
eg.AddLoad(rid); |
| 63 |
|
| 64 |
dbg_.emplace(bits[i].mer, std::move(eg)); |
| 65 |
} |
| 66 |
dbg_.at(bits[i].mer).SetEdges(edges[i]); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
uint8_t SetRevEdge(const DnaBit& a, const DnaBit& b) |
| 71 |
{ |
| 72 |
uint8_t c = (a.strand << 1) | (b.strand); |
| 73 |
uint8_t es = 1; |
| 74 |
|
| 75 |
DnaBit newNib = a; |
| 76 |
|
| 77 |
switch (c) { |
| 78 |
case 0: { |
| 79 |
es <<= (b.FirstBaseIdx() + 0); |
| 80 |
newNib.PrependBase(b.FirstBaseIdx()); |
| 81 |
break; |
| 82 |
} |
| 83 |
case 1: { |
| 84 |
es <<= (b.FirstBaseRCIdx() + 0); |
| 85 |
newNib.PrependBase(b.FirstBaseRCIdx()); |
| 86 |
newNib.MakeLexSmaller(); |
| 87 |
break; |
| 88 |
} |
| 89 |
case 2: { |
| 90 |
es <<= (b.LastBaseRCIdx() + 4); |
| 91 |
newNib.AppendBase(b.LastBaseRCIdx()); |
| 92 |
newNib.MakeLexSmaller(); |
| 93 |
break; |
| 94 |
} |
| 95 |
case 3: { |
| 96 |
es <<= (b.LastBaseIdx() + 4); |
| 97 |
newNib.AppendBase(b.LastBaseIdx()); |
| 98 |
break; |
| 99 |
} |
| 100 |
|
| 101 |
default: { |
| 102 |
break; |
| 103 |
} |
| 104 |
} |
| 105 |
if (a.mer == newNib.mer) { |
| 106 |
return 0; |
| 107 |
} |
| 108 |
return es; |
| 109 |
} |
| 110 |
|
| 111 |
uint8_t SetForEdge(const DnaBit& a, const DnaBit& b) |
| 112 |
{ |
| 113 |
uint8_t c = (a.strand << 1) | (b.strand); |
| 114 |
uint8_t es = 1; |
| 115 |
|
| 116 |
DnaBit newNib = a; |
| 117 |
|
| 118 |
switch (c) { |
| 119 |
case 0: { |
| 120 |
es <<= (b.LastBaseIdx() + 4); |
| 121 |
newNib.AppendBase(b.LastBaseIdx()); |
| 122 |
break; |
| 123 |
} |
| 124 |
case 1: { |
| 125 |
es <<= (b.LastBaseRCIdx() + 4); |
| 126 |
newNib.AppendBase(b.LastBaseRCIdx()); |
| 127 |
newNib.MakeLexSmaller(); |
| 128 |
break; |
| 129 |
} |
| 130 |
case 2: { |
| 131 |
es <<= (b.FirstBaseRCIdx() + 0); |
| 132 |
newNib.PrependBase(b.FirstBaseRCIdx()); |
| 133 |
newNib.MakeLexSmaller(); |
| 134 |
break; |
| 135 |
} |
| 136 |
case 3: { |
| 137 |
es <<= (b.FirstBaseIdx() + 0); |
| 138 |
newNib.PrependBase(b.FirstBaseIdx()); |
| 139 |
break; |
| 140 |
} |
| 141 |
|
| 142 |
default: { |
| 143 |
break; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if (a.mer == newNib.mer) { |
| 148 |
return 0; |
| 149 |
} |
| 150 |
return es; |
| 151 |
} |
| 152 |
|
| 153 |
std::vector<uint8_t> Dbg::BuildVerifiedEdges(const std::vector<PacBio::Pbmer::DnaBit>& bits) |
| 154 |
{ |
| 155 |
|
| 156 |
std::vector<uint8_t> edges(bits.size(), 0); |
| 157 |
|
| 158 |
// set back edges |
| 159 |
for (size_t i = 1; i < bits.size(); ++i) { |
| 160 |
edges[i] |= SetRevEdge(bits[i], bits[i - 1]); |
| 161 |
} |
| 162 |
|
| 163 |
// set forward edges |
| 164 |
for (size_t i = 0; i < bits.size() - 1; ++i) { |
| 165 |
edges[i] |= SetForEdge(bits[i], bits[i + 1]); |
| 166 |
} |
| 167 |
|
| 168 |
return edges; |
| 169 |
} |
| 170 |
|
| 171 |
void Dbg::BuildEdges() |
| 172 |
{ |
| 173 |
for (auto& x : dbg_) { |
| 174 |
// all 8 possible edges |
| 175 |
for (uint8_t y = 0; y < 8; ++y) { |
| 176 |
|
| 177 |
DnaBit niby = x.second.dna_; |
| 178 |
// pre-prending base |
| 179 |
if (y <= 3) { |
| 180 |
niby.PrependBase(y); |
| 181 |
} |
| 182 |
// appending base |
| 183 |
else { |
| 184 |
niby.AppendBase(y); |
| 185 |
} |
| 186 |
|
| 187 |
// generate new lex smallest |
| 188 |
niby.MakeLexSmaller(); |
| 189 |
|
| 190 |
// this is a self loop |
| 191 |
// TODO validate this should not be skipped. |
| 192 |
if (x.second.dna_.mer == niby.mer) { |
| 193 |
continue; |
| 194 |
} |
| 195 |
|
| 196 |
if (dbg_.find(niby.mer) != dbg_.end()) { |
| 197 |
//setting the edges |
| 198 |
x.second.SetEdges((uint8_t(1) << y)); |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
void Dbg::DumpNodes() const |
| 205 |
{ |
| 206 |
for (const auto& x : dbg_) { |
| 207 |
std::cout << " " << x.second.dna_.KmerToStr() << " n out:" << x.second.TotalEdgeCount() |
| 208 |
<< " e val: " << static_cast<int>(x.second.edges_) |
| 209 |
<< " n ids: " << x.second.readIds2_.count() |
| 210 |
<< " n left eg: " << x.second.LeftEdgeCount() |
| 211 |
<< " n right eg: " << x.second.RightEdgeCount() |
| 212 |
<< " n total eg: " << x.second.TotalEdgeCount() << "\n"; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
void Dbg::FrequencyFilterNodes(unsigned long n) |
| 217 |
{ |
| 218 |
this->ResetEdges(); |
| 219 |
|
| 220 |
std::vector<uint64_t> toRemove; |
| 221 |
|
| 222 |
for (auto& x : dbg_) { |
| 223 |
if (x.second.readIds2_.count() < n) { |
| 224 |
toRemove.push_back(x.first); |
| 225 |
} |
| 226 |
} |
| 227 |
for (const auto x : toRemove) { |
| 228 |
dbg_.erase(x); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
void Dbg::FrequencyFilterNodes2(unsigned long n) { Dbg::FrequencyFilterNodes2(n, false); } |
| 233 |
|
| 234 |
void Dbg::FrequencyFilterNodes2(unsigned long n, bool gt) |
| 235 |
{ |
| 236 |
|
| 237 |
std::vector<uint64_t> toRemove; |
| 238 |
|
| 239 |
auto filterDirection = [&](const auto count) { return gt ? (count > n) : (count < n); }; |
| 240 |
|
| 241 |
for (auto& x : dbg_) { |
| 242 |
if (filterDirection(x.second.readIds2_.count())) { |
| 243 |
toRemove.push_back(x.first); |
| 244 |
} |
| 245 |
} |
| 246 |
for (const auto x : toRemove) { |
| 247 |
dbg_.erase(x); |
| 248 |
} |
| 249 |
|
| 250 |
uint64_t lexSmall = 0; |
| 251 |
|
| 252 |
for (auto& x : dbg_) { |
| 253 |
for (uint8_t y = 0; y < 8; ++y) { |
| 254 |
if (((1 << y) & x.second.edges_) == 0) { |
| 255 |
continue; |
| 256 |
} |
| 257 |
DnaBit niby = x.second.dna_; |
| 258 |
// pre-prending base |
| 259 |
if (y <= 3) { |
| 260 |
niby.PrependBase(y); |
| 261 |
} |
| 262 |
// appending base |
| 263 |
else { |
| 264 |
niby.AppendBase(y); |
| 265 |
} |
| 266 |
|
| 267 |
// generate new lex smallest |
| 268 |
lexSmall = niby.LexSmallerEq64(); |
| 269 |
|
| 270 |
if (dbg_.find(lexSmall) == dbg_.end()) { |
| 271 |
uint8_t turnOff = ~(uint8_t(1) << y); |
| 272 |
x.second.edges_ &= turnOff; |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
Bubbles Dbg::FindBubbles() const |
| 279 |
{ |
| 280 |
// returned container describing which reads traverse which forks |
| 281 |
Bubbles result; |
| 282 |
|
| 283 |
// keeping track of read id counts over linear paths |
| 284 |
// these variables are reused |
| 285 |
Container::UnorderedMap<uint32_t, int> leftReads; |
| 286 |
Container::UnorderedMap<uint32_t, int> rightReads; |
| 287 |
|
| 288 |
// keep track of the used head/tails of bubbles. |
| 289 |
Container::UnorderedSet<uint64_t> usedBranchNode; |
| 290 |
|
| 291 |
// reused variables |
| 292 |
|
| 293 |
for (const auto& x : dbg_) { |
| 294 |
|
| 295 |
// this node is already part of a bubble and should be ignored. |
| 296 |
if (usedBranchNode.find(x.second.dna_.mer) != usedBranchNode.end()) { |
| 297 |
continue; |
| 298 |
} |
| 299 |
|
| 300 |
// valid bubbles contain 3 or more paths |
| 301 |
if (x.second.TotalEdgeCount() < 3) { |
| 302 |
continue; |
| 303 |
} |
| 304 |
std::vector<std::tuple<uint64_t, uint64_t>> pathInfo; |
| 305 |
|
| 306 |
// loop over neighboring nodes collecting the start and end node of |
| 307 |
// linear paths. I.E. looping over all linear paths coming out of a node. |
| 308 |
for (const auto& out : x.second) { |
| 309 |
const auto linearPath = LinearPath(out); |
| 310 |
if (!linearPath.empty()) { |
| 311 |
pathInfo.emplace_back(out.mer, linearPath.back().mer); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
// The left and right path of a bubble. |
| 316 |
std::vector<DnaBit> left; |
| 317 |
std::vector<DnaBit> right; |
| 318 |
|
| 319 |
bool hasBubble = false; |
| 320 |
|
| 321 |
// Comparing all linear paths to check if they converge. The first |
| 322 |
// two paths to converge are considered a bubble. subsequent bubbles are |
| 323 |
// ignored. |
| 324 |
for (const auto& [s1, e1] : pathInfo) { |
| 325 |
hasBubble = false; |
| 326 |
for (const auto& [s2, e2] : pathInfo) { |
| 327 |
// check if the paths converge on a common neighboring node. |
| 328 |
if (uint64_t shared; OneIntermediateNode(e1, e2, &shared)) { |
| 329 |
// the paths are not stored in the first loop, maybe the should? |
| 330 |
left = LinearPath(s1); |
| 331 |
right = LinearPath(s2); |
| 332 |
// set the used incoming node so we don't get 2x n bubbles |
| 333 |
usedBranchNode.insert(shared); |
| 334 |
usedBranchNode.insert(x.second.dna_.mer); |
| 335 |
hasBubble = true; |
| 336 |
break; |
| 337 |
} |
| 338 |
} |
| 339 |
// only keep the first valid bubble |
| 340 |
if (hasBubble) { |
| 341 |
break; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
if (!hasBubble) { |
| 346 |
continue; |
| 347 |
} |
| 348 |
// reuse the same temp data struct. |
| 349 |
leftReads.clear(); |
| 350 |
rightReads.clear(); |
| 351 |
|
| 352 |
for (const auto& l : left) { |
| 353 |
size_t i = dbg_.at(l.mer).readIds2_.find_first(); |
| 354 |
while (i != dbg_.at(l.mer).readIds2_.npos) { |
| 355 |
++leftReads[i + 1]; |
| 356 |
i = dbg_.at(l.mer).readIds2_.find_next(i); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
for (const auto& r : right) { |
| 361 |
size_t i = dbg_.at(r.mer).readIds2_.find_first(); |
| 362 |
while (i != dbg_.at(r.mer).readIds2_.npos) { |
| 363 |
++rightReads[i + 1]; |
| 364 |
i = dbg_.at(r.mer).readIds2_.find_next(i); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
BubbleInfo bubble; |
| 369 |
bubble.LSeq = DnaBitVec2String(left); |
| 370 |
bubble.RSeq = DnaBitVec2String(right); |
| 371 |
bubble.LKmerCount = left.size(); |
| 372 |
bubble.RKmerCount = right.size(); |
| 373 |
bubble.LVec = std::move(left); |
| 374 |
bubble.RVec = std::move(right); |
| 375 |
|
| 376 |
for (const auto& kv : leftReads) { |
| 377 |
bubble.LData.emplace_back(kv.first, kv.second); |
| 378 |
} |
| 379 |
|
| 380 |
for (const auto& kv : rightReads) { |
| 381 |
bubble.RData.emplace_back(kv.first, kv.second); |
| 382 |
} |
| 383 |
result.emplace_back(std::move(bubble)); |
| 384 |
} |
| 385 |
return result; |
| 386 |
} // namespace Pbmer |
| 387 |
|
| 388 |
std::vector<DnaBit> Dbg::LinearPath(uint64_t x) const { return LinearPath(dbg_.at(x).dna_); } |
| 389 |
|
| 390 |
std::vector<DnaBit> Dbg::LinearPath(const DnaBit& niby) const |
| 391 |
{ |
| 392 |
std::vector<DnaBit> result; |
| 393 |
|
| 394 |
if (dbg_.at(niby.mer).TotalEdgeCount() > 2) { |
| 395 |
return result; |
| 396 |
} |
| 397 |
|
| 398 |
// lookup for which nodes we've seen to prevent loops |
| 399 |
Container::UnorderedSet<uint64_t> seen; |
| 400 |
|
| 401 |
uint64_t past = niby.mer; |
| 402 |
|
| 403 |
while (seen.find(past) == seen.end()) { |
| 404 |
seen.insert(past); |
| 405 |
result.push_back(dbg_.at(past).dna_); |
| 406 |
for (const auto& y : dbg_.at(past)) { |
| 407 |
if (dbg_.at(y.mer).TotalEdgeCount() > 2) { |
| 408 |
continue; |
| 409 |
} |
| 410 |
if (seen.find(y.mer) == seen.end()) { |
| 411 |
past = y.mer; |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
return result; |
| 416 |
} |
| 417 |
|
| 418 |
std::string Dbg::Graph2StringDot() |
| 419 |
{ |
| 420 |
std::ostringstream ss; |
| 421 |
ss << "digraph DBGraph {\n"; |
| 422 |
for (auto& x : dbg_) { |
| 423 |
ss << " " << x.second.dna_.KmerToStr(); |
| 424 |
if (x.second.dna_.strand) { |
| 425 |
ss << " [fillcolor=red, style=\"rounded,filled\", shape=diamond]\n"; |
| 426 |
} else { |
| 427 |
ss << " [fillcolor=grey, style=\"rounded,filled\", shape=ellipse]\n"; |
| 428 |
} |
| 429 |
} |
| 430 |
for (auto& x : dbg_) { |
| 431 |
for (const auto& y : x.second) { |
| 432 |
const DnaBit niby = y; |
| 433 |
|
| 434 |
ss << " " << x.second.dna_.KmerToStr() << " -> " << niby.KmerToStr() << ";\n"; |
| 435 |
} |
| 436 |
} |
| 437 |
ss << "}"; |
| 438 |
|
| 439 |
return ss.str(); |
| 440 |
} |
| 441 |
|
| 442 |
size_t Dbg::NNodes() const { return dbg_.size(); } |
| 443 |
|
| 444 |
size_t Dbg::NEdges() const |
| 445 |
{ |
| 446 |
return std::accumulate( |
| 447 |
std::begin(dbg_), std::end(dbg_), size_t(0), |
| 448 |
[](size_t sum, const auto& x) { return sum + x.second.TotalEdgeCount(); }); |
| 449 |
} |
| 450 |
|
| 451 |
bool Dbg::OneIntermediateNode(uint64_t n1, uint64_t n2, uint64_t* shared) const |
| 452 |
{ |
| 453 |
Container::UnorderedSet<uint64_t> seen; |
| 454 |
if (n1 == n2) { |
| 455 |
return false; |
| 456 |
} |
| 457 |
for (const auto& nout : dbg_.at(n1)) { |
| 458 |
seen.insert(nout.mer); |
| 459 |
} |
| 460 |
for (const auto& nout : dbg_.at(n2)) { |
| 461 |
if (seen.find(nout.mer) != seen.end()) { |
| 462 |
*shared = nout.mer; |
| 463 |
return true; |
| 464 |
} |
| 465 |
} |
| 466 |
return false; |
| 467 |
} |
| 468 |
|
| 469 |
int Dbg::RemoveSpurs(unsigned int maxLength) |
| 470 |
{ |
| 471 |
int nSpurs = 0; |
| 472 |
|
| 473 |
Container::UnorderedSet<uint64_t> toDelete; |
| 474 |
|
| 475 |
for (auto& node : dbg_) { |
| 476 |
// Starting at tip nodes with a degree of one. |
| 477 |
if (node.second.TotalEdgeCount() != 1) { |
| 478 |
continue; |
| 479 |
} |
| 480 |
|
| 481 |
// Including tip node in the linear path. |
| 482 |
const auto linearPath = LinearPath(node.second.dna_.mer); |
| 483 |
|
| 484 |
if (linearPath.size() > maxLength) { |
| 485 |
continue; |
| 486 |
} |
| 487 |
for (const auto& x : linearPath) { |
| 488 |
toDelete.insert(x.mer); |
| 489 |
} |
| 490 |
|
| 491 |
++nSpurs; |
| 492 |
} |
| 493 |
for (const auto x : toDelete) { |
| 494 |
dbg_.erase(x); |
| 495 |
} |
| 496 |
|
| 497 |
this->ResetEdges(); |
| 498 |
this->BuildEdges(); |
| 499 |
|
| 500 |
return nSpurs; |
| 501 |
} |
| 502 |
|
| 503 |
void Dbg::ResetEdges() |
| 504 |
{ |
| 505 |
for (auto& x : dbg_) { |
| 506 |
x.second.edges_ = 0; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
bool Dbg::ValidateEdges() const |
| 511 |
{ |
| 512 |
const auto& dbg = dbg_; |
| 513 |
return std::all_of(std::begin(dbg_), std::end(dbg_), [&dbg](const auto& x) { |
| 514 |
return std::all_of(std::begin(x.second), std::end(x.second), |
| 515 |
[&dbg](const auto& y) { return dbg.find(y.mer) != dbg.end(); }); |
| 516 |
}); |
| 517 |
} |
| 518 |
|
| 519 |
bool Dbg::ValidateLoad() const |
| 520 |
{ |
| 521 |
return std::all_of(std::begin(dbg_), std::end(dbg_), |
| 522 |
[](const auto& x) { return x.second.readIds2_.count() != 0; }); |
| 523 |
} |
| 524 |
|
| 525 |
void Dbg::WriteGraph(const std::filesystem::path& filename) |
| 526 |
{ |
| 527 |
std::ofstream outfile{filename}; |
| 528 |
outfile << Dbg::Graph2StringDot(); |
| 529 |
} |
| 530 |
|
| 531 |
} // namespace Pbmer |
| 532 |
} // namespace PacBio |