| 1 |
#include <pbcopper/pbmer/KFGraph.h> |
| 2 |
|
| 3 |
#include <pbcopper/utility/MoveAppend.h> |
| 4 |
|
| 5 |
#include <algorithm> |
| 6 |
#include <fstream> |
| 7 |
#include <iostream> |
| 8 |
#include <set> |
| 9 |
#include <sstream> |
| 10 |
#include <unordered_set> |
| 11 |
|
| 12 |
#include <cassert> |
| 13 |
|
| 14 |
namespace PacBio { |
| 15 |
namespace Pbmer { |
| 16 |
|
| 17 |
KFG::KFG(uint8_t k, size_t nr) : kmerSize_{k}, nReads_{nr} {} |
| 18 |
|
| 19 |
bool KFG::HasNode(const DnaBit& bit) const { return (kfg_.find(bit.HashedKmer()) != kfg_.cend()); } |
| 20 |
|
| 21 |
bool KFG::MoreRCHits(const std::vector<DnaBit>& bits) const |
| 22 |
{ |
| 23 |
|
| 24 |
uint32_t fs = 0; |
| 25 |
uint32_t rs = 0; |
| 26 |
|
| 27 |
for (const auto& k : bits) { |
| 28 |
auto rc = k; |
| 29 |
rc.ReverseComp(); |
| 30 |
if (HasNode(k)) { |
| 31 |
++fs; |
| 32 |
} |
| 33 |
if (HasNode(rc)) { |
| 34 |
++rs; |
| 35 |
} |
| 36 |
} |
| 37 |
if (rs > fs) { |
| 38 |
return true; |
| 39 |
} |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
uint64_t KFG::Knock(const DnaBit& b, uint64_t hashedKmer) const |
| 44 |
{ |
| 45 |
auto node = kfg_.find(hashedKmer); |
| 46 |
if (node == kfg_.end()) { |
| 47 |
return 0; |
| 48 |
} |
| 49 |
// if the keys match so too must the kmers; |
| 50 |
if (node->second.Kmer() != b.mer) { |
| 51 |
throw std::runtime_error{"[pbmer] knock graph ERROR: kmer.a != kmer.b, but same hash."}; |
| 52 |
} |
| 53 |
return hashedKmer; |
| 54 |
} |
| 55 |
|
| 56 |
uint64_t KFG::OpenDoor(const std::vector<DnaBit>& bits, uint64_t hashedKmer, uint32_t rid, size_t i, |
| 57 |
size_t j) const |
| 58 |
{ |
| 59 |
if (j == 0) { |
| 60 |
return hashedKmer; |
| 61 |
} |
| 62 |
|
| 63 |
auto hit = Knock(bits[i], hashedKmer); |
| 64 |
if (hit == 0) { |
| 65 |
return hashedKmer; |
| 66 |
} |
| 67 |
j = j - 1; |
| 68 |
if (kfg_.at(hit).ContainsSeq(rid)) { |
| 69 |
auto newHashed = (hashedKmer) ^ bits[j].mer; |
| 70 |
if (newHashed == 0) { |
| 71 |
throw std::runtime_error{"[pbmer] knock graph ERROR: zero hash value"}; |
| 72 |
} |
| 73 |
return OpenDoor(bits, newHashed, rid, i, j); |
| 74 |
} |
| 75 |
return hashedKmer; |
| 76 |
} |
| 77 |
|
| 78 |
void KFG::AddSeq(const std::vector<DnaBit>& bits, const size_t rid, const std::string& rn) |
| 79 |
{ |
| 80 |
|
| 81 |
nameToId_[rn] = rid; |
| 82 |
idToName_[rid] = rn; |
| 83 |
|
| 84 |
uint64_t last = 0; |
| 85 |
for (size_t i = 0; i < bits.size(); ++i) { |
| 86 |
uint64_t nextHashedKmer = OpenDoor(bits, bits[i].HashedKmer(), rid, i, i); |
| 87 |
if (nextHashedKmer == 0) { |
| 88 |
throw std::runtime_error{"[pbcopper] knock graph ERROR: zero hash value"}; |
| 89 |
} |
| 90 |
if (Knock(bits[i], nextHashedKmer) == 0) { |
| 91 |
kfg_.emplace(nextHashedKmer, KFNode{bits[i], nReads_, nextHashedKmer}); |
| 92 |
kfg_.at(nextHashedKmer).AddLoad(rid); |
| 93 |
} else { |
| 94 |
kfg_.at(nextHashedKmer).AddLoad(rid); |
| 95 |
} |
| 96 |
if (i > 0) { |
| 97 |
kfg_.at(last).AddOutEdge(nextHashedKmer); |
| 98 |
kfg_.at(nextHashedKmer).AddInEdge(last); |
| 99 |
} |
| 100 |
last = nextHashedKmer; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
int32_t KFG::NNodes() const { return kfg_.size(); } |
| 105 |
|
| 106 |
int32_t KFG::InEdgeCount() const |
| 107 |
{ |
| 108 |
size_t edgeCount = 0; |
| 109 |
for (const auto& x : kfg_) { |
| 110 |
edgeCount += x.second.inEdges_.size(); |
| 111 |
} |
| 112 |
return edgeCount; |
| 113 |
} |
| 114 |
|
| 115 |
int32_t KFG::OutEdgeCount() const |
| 116 |
{ |
| 117 |
size_t edgeCount = 0; |
| 118 |
for (const auto& x : kfg_) { |
| 119 |
edgeCount += x.second.outEdges_.size(); |
| 120 |
} |
| 121 |
return edgeCount; |
| 122 |
} |
| 123 |
|
| 124 |
bool KFG::ValidateLoad() const |
| 125 |
{ |
| 126 |
for (const auto& x : kfg_) { |
| 127 |
if (x.second.SeqCount() == 0) { |
| 128 |
return false; |
| 129 |
} |
| 130 |
} |
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
void KFG::CleanUpEdges() |
| 135 |
{ |
| 136 |
std::vector<uint64_t> toRemove; |
| 137 |
for (auto& node : kfg_) { |
| 138 |
toRemove.clear(); |
| 139 |
for (const auto& e : node.second.inEdges_) { |
| 140 |
if (kfg_.find(e) == kfg_.end()) { |
| 141 |
toRemove.push_back(e); |
| 142 |
} |
| 143 |
} |
| 144 |
for (const auto& e : node.second.outEdges_) { |
| 145 |
if (kfg_.find(e) == kfg_.end()) { |
| 146 |
toRemove.push_back(e); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
for (const auto& re : toRemove) { |
| 151 |
node.second.inEdges_.erase(re); |
| 152 |
node.second.outEdges_.erase(re); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
void KFG::SeqCountFilter(int n, bool gt) |
| 158 |
{ |
| 159 |
std::vector<uint64_t> toRemove; |
| 160 |
|
| 161 |
auto filterDirection = [&](const auto count) { |
| 162 |
if (gt) { |
| 163 |
return (count > n); |
| 164 |
} else { |
| 165 |
return (count < n); |
| 166 |
} |
| 167 |
}; |
| 168 |
|
| 169 |
for (const auto& node : kfg_) { |
| 170 |
if (filterDirection(node.second.SeqCount())) { |
| 171 |
toRemove.push_back(node.first); |
| 172 |
} |
| 173 |
} |
| 174 |
for (const auto x : toRemove) { |
| 175 |
kfg_.erase(x); |
| 176 |
} |
| 177 |
|
| 178 |
CleanUpEdges(); |
| 179 |
} |
| 180 |
|
| 181 |
std::vector<KFNode> KFG::LinearPath(uint64_t x) const { return LinearPath(kfg_.at(x)); } |
| 182 |
|
| 183 |
std::vector<KFNode> KFG::LinearPath(const KFNode& node) const |
| 184 |
{ |
| 185 |
std::vector<KFNode> result; |
| 186 |
|
| 187 |
if (node.InEdgeCount() > 1 || node.OutEdgeCount() > 1) { |
| 188 |
return result; |
| 189 |
} |
| 190 |
|
| 191 |
// lookup for which nodes we've seen to prevent loops |
| 192 |
std::unordered_set<uint64_t> seen; |
| 193 |
|
| 194 |
uint64_t past = node.Key(); |
| 195 |
|
| 196 |
while (1) { |
| 197 |
if (seen.find(past) != seen.end()) { |
| 198 |
break; |
| 199 |
} |
| 200 |
seen.insert(past); |
| 201 |
result.push_back(kfg_.at(past)); |
| 202 |
for (const auto& y : kfg_.at(past)) { |
| 203 |
if (kfg_.at(y).OutEdgeCount() > 1 || kfg_.at(y).InEdgeCount() > 1) { |
| 204 |
continue; |
| 205 |
} |
| 206 |
if (seen.find(y) == seen.end()) { |
| 207 |
past = y; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
return result; |
| 212 |
} |
| 213 |
|
| 214 |
Bubbles KFG::FindBubbles() const |
| 215 |
{ |
| 216 |
// returned container describing which reads traverse which forks |
| 217 |
Bubbles result; |
| 218 |
|
| 219 |
// keeping track of read id counts over linear paths |
| 220 |
// these variables are reused |
| 221 |
robin_hood::unordered_map<uint32_t, int> left_reads; |
| 222 |
robin_hood::unordered_map<uint32_t, int> right_reads; |
| 223 |
|
| 224 |
// keep track of the used head/tails of bubbles. |
| 225 |
std::unordered_set<uint64_t> used_branch_node; |
| 226 |
|
| 227 |
for (const auto& node : kfg_) { |
| 228 |
|
| 229 |
// this node is already part of a bubble and should be ignored. |
| 230 |
if (used_branch_node.find(node.second.dna_.HashedKmer()) != used_branch_node.end()) { |
| 231 |
continue; |
| 232 |
} |
| 233 |
|
| 234 |
// simple bubbles contain 2 out edges |
| 235 |
if (node.second.OutEdgeCount() != 2) { |
| 236 |
continue; |
| 237 |
} |
| 238 |
|
| 239 |
std::unordered_set<uint64_t>::const_iterator paths = node.second.begin(); |
| 240 |
|
| 241 |
std::vector<KFNode> left = LinearPath(*paths); |
| 242 |
++paths; |
| 243 |
std::vector<KFNode> right = LinearPath(*paths); |
| 244 |
|
| 245 |
bool hasBubble = false; |
| 246 |
|
| 247 |
if (!left.empty() && !right.empty() && !left.back().outEdges_.empty()) { |
| 248 |
if (left.back().outEdges_ == right.back().outEdges_) { |
| 249 |
hasBubble = true; |
| 250 |
} |
| 251 |
} |
| 252 |
if (!hasBubble) { |
| 253 |
continue; |
| 254 |
} |
| 255 |
|
| 256 |
// reuse the same temp data struct. |
| 257 |
left_reads.clear(); |
| 258 |
right_reads.clear(); |
| 259 |
|
| 260 |
for (auto const& l : left) { |
| 261 |
size_t i = l.readIds_.find_first(); |
| 262 |
while (i != l.readIds_.npos) { |
| 263 |
++left_reads[i + 1]; |
| 264 |
i = l.readIds_.find_next(i); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
for (auto const& r : right) { |
| 269 |
size_t i = r.readIds_.find_first(); |
| 270 |
while (i != r.readIds_.npos) { |
| 271 |
++right_reads[i + 1]; |
| 272 |
i = r.readIds_.find_next(i); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
BubbleInfo bubble; |
| 277 |
bubble.LSeq = Vec2String(left); |
| 278 |
bubble.RSeq = Vec2String(right); |
| 279 |
bubble.LKmerCount = left.size(); |
| 280 |
bubble.RKmerCount = right.size(); |
| 281 |
|
| 282 |
for (const auto& kv : left_reads) { |
| 283 |
bubble.LData.push_back(std::make_pair(kv.first, kv.second)); |
| 284 |
} |
| 285 |
|
| 286 |
for (const auto& kv : right_reads) { |
| 287 |
bubble.RData.push_back(std::make_pair(kv.first, kv.second)); |
| 288 |
} |
| 289 |
result.emplace_back(std::move(bubble)); |
| 290 |
} |
| 291 |
return result; |
| 292 |
} |
| 293 |
|
| 294 |
std::string KFG::Graph2StringDot() const |
| 295 |
{ |
| 296 |
std::ostringstream ss; |
| 297 |
ss << "digraph DBGraph {\n"; |
| 298 |
for (const auto& x : kfg_) { |
| 299 |
ss << " " << x.second.Key(); |
| 300 |
ss << " [fillcolor=grey, style=\"rounded,filled\", shape=ellipse, label=\"" |
| 301 |
<< x.second.Bit().KmerToStr() << "\"]" |
| 302 |
<< "\n"; |
| 303 |
} |
| 304 |
for (const auto& x : kfg_) { |
| 305 |
for (const auto& y : x.second) { |
| 306 |
ss << " " << x.second.Key() << " -> " << y << ";\n"; |
| 307 |
} |
| 308 |
} |
| 309 |
ss << "}"; |
| 310 |
|
| 311 |
return ss.str(); |
| 312 |
} |
| 313 |
|
| 314 |
void KFG::WriteDot(const std::filesystem::path& filename) const |
| 315 |
{ |
| 316 |
std::ofstream outfile{filename}; |
| 317 |
outfile << Graph2StringDot(); |
| 318 |
} |
| 319 |
|
| 320 |
void KFG::WriteUtgsGFA(const std::filesystem::path& filename) const |
| 321 |
{ |
| 322 |
std::ofstream outfile{filename}; |
| 323 |
outfile << DumpGFAUtgs(); |
| 324 |
} |
| 325 |
|
| 326 |
int32_t KFG::MatchCount(const std::vector<DnaBit>& bits) const |
| 327 |
{ |
| 328 |
uint32_t nHits = 0; |
| 329 |
for (const auto& b : bits) { |
| 330 |
if (HasNode(b)) { |
| 331 |
++nHits; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
return nHits; |
| 336 |
} |
| 337 |
|
| 338 |
void KFG::DumpHeader() const |
| 339 |
{ |
| 340 |
for (const auto& i : nameToId_) { |
| 341 |
std::cerr << i.first << " " << i.second << "\n"; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
bool KFG::NextUtg(uint64_t currentNode, std::unordered_set<uint64_t>& seen, |
| 346 |
std::vector<std::string>& segments, std::vector<std::string>& links) const |
| 347 |
{ |
| 348 |
if (seen.find(currentNode) != seen.end()) { |
| 349 |
return false; |
| 350 |
} |
| 351 |
|
| 352 |
std::vector<KFNode> fullPath; |
| 353 |
|
| 354 |
// first node is a branch node, but we need to include the branch node |
| 355 |
std::vector<KFNode> linearPath = LinearPath(currentNode); |
| 356 |
|
| 357 |
Utility::MoveAppend(linearPath, fullPath); |
| 358 |
|
| 359 |
if (fullPath.empty()) { |
| 360 |
fullPath.push_back(kfg_.at(currentNode)); |
| 361 |
} |
| 362 |
|
| 363 |
// We need to get the branch node. |
| 364 |
if (fullPath.back().OutEdgeCount() == 1) { |
| 365 |
uint64_t tail = (*fullPath.back().outEdges_.begin()); |
| 366 |
if (tail != fullPath.back().Key() && seen.find(fullPath.back().Key()) == seen.end()) { |
| 367 |
fullPath.push_back(kfg_.at(tail)); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
for (const auto& p : fullPath) { |
| 372 |
seen.insert(p.Key()); |
| 373 |
} |
| 374 |
|
| 375 |
auto seq = Vec2String(fullPath); |
| 376 |
|
| 377 |
if (fullPath.front().InEdgeCount() != 0) { |
| 378 |
|
| 379 |
auto start = seq.size() - fullPath.size(); |
| 380 |
|
| 381 |
seq = seq.substr(start, fullPath.size()); |
| 382 |
} |
| 383 |
|
| 384 |
double KmerCovSum = 0; |
| 385 |
for (const auto& n : fullPath) { |
| 386 |
KmerCovSum += n.SeqCount(); |
| 387 |
} |
| 388 |
|
| 389 |
std::stringstream segLine; |
| 390 |
|
| 391 |
segLine << "S\t" << currentNode << "\t" << seq << "\tKC:i:" << fullPath.size() |
| 392 |
<< "\tLN:i:" << seq.size() << "\tRC:i:" << KmerCovSum << "\n"; |
| 393 |
|
| 394 |
segments.push_back(segLine.str()); |
| 395 |
|
| 396 |
for (const auto& nn : fullPath.back()) { |
| 397 |
|
| 398 |
std::stringstream linkLine; |
| 399 |
linkLine << "L\t" << currentNode << "\t+\t" << nn << "\t+\t*\n"; |
| 400 |
links.push_back(linkLine.str()); |
| 401 |
|
| 402 |
NextUtg(nn, seen, segments, links); |
| 403 |
} |
| 404 |
|
| 405 |
return true; |
| 406 |
} |
| 407 |
|
| 408 |
std::string KFG::DumpGFAUtgs() const |
| 409 |
{ |
| 410 |
std::unordered_set<uint64_t> seen; |
| 411 |
|
| 412 |
std::vector<std::string> segments; |
| 413 |
std::vector<std::string> links; |
| 414 |
|
| 415 |
std::set<uint64_t> keys; |
| 416 |
|
| 417 |
//maintain an order for testing and consistency |
| 418 |
for (const auto& node : kfg_) { |
| 419 |
keys.insert(node.first); |
| 420 |
} |
| 421 |
|
| 422 |
for (const auto& k : keys) { |
| 423 |
if (seen.find(k) != seen.end()) { |
| 424 |
continue; |
| 425 |
} |
| 426 |
auto node = kfg_.at(k); |
| 427 |
if (node.InEdgeCount() != 0) { |
| 428 |
continue; |
| 429 |
} |
| 430 |
NextUtg(node.Key(), seen, segments, links); |
| 431 |
} |
| 432 |
|
| 433 |
std::ostringstream ss; |
| 434 |
ss << "H\tVN:Z:1.0\n"; |
| 435 |
|
| 436 |
std::sort(segments.begin(), segments.end()); |
| 437 |
std::sort(links.begin(), links.end()); |
| 438 |
|
| 439 |
for (const auto& seg : segments) { |
| 440 |
ss << seg; |
| 441 |
} |
| 442 |
|
| 443 |
for (const auto& lnk : links) { |
| 444 |
ss << lnk; |
| 445 |
} |
| 446 |
|
| 447 |
return ss.str(); |
| 448 |
} |
| 449 |
|
| 450 |
std::unordered_map<std::string, uint32_t> KFG::Header() const { return nameToId_; } |
| 451 |
|
| 452 |
} // namespace Pbmer |
| 453 |
} // namespace PacBio |