| 1 |
#include <pbcopper/third-party/sparc/sparc.hpp> |
| 2 |
|
| 3 |
#include <cmath> |
| 4 |
#include <cstdint> |
| 5 |
#include <cstdio> |
| 6 |
#include <cstdlib> |
| 7 |
#include <cstring> |
| 8 |
#include <ctime> |
| 9 |
|
| 10 |
#include <algorithm> |
| 11 |
#include <iostream> |
| 12 |
#include <list> |
| 13 |
#include <map> |
| 14 |
#include <memory> |
| 15 |
#include <string> |
| 16 |
#include <vector> |
| 17 |
|
| 18 |
#include <boost/container/small_vector.hpp> |
| 19 |
|
| 20 |
namespace Sparc { |
| 21 |
namespace SparcSimpleImpl { |
| 22 |
|
| 23 |
struct EdgeNode; |
| 24 |
struct Node; |
| 25 |
|
| 26 |
using PtrEdgeNode = EdgeNode*; |
| 27 |
using PtrNode = Node*; |
| 28 |
|
| 29 |
struct EdgeNode |
| 30 |
{ |
| 31 |
int32_t EdgeCov = 0; |
| 32 |
PtrNode NodePtr = nullptr; |
| 33 |
PtrEdgeNode NextEdge = nullptr; |
| 34 |
}; |
| 35 |
|
| 36 |
struct Node |
| 37 |
{ |
| 38 |
char Base = 0; |
| 39 |
int32_t Score = 0; |
| 40 |
int32_t Cov = 0; |
| 41 |
bool InBackbone = false; |
| 42 |
PtrEdgeNode Left = nullptr; |
| 43 |
PtrEdgeNode Right = nullptr; |
| 44 |
PtrNode LastNode = nullptr; |
| 45 |
}; |
| 46 |
|
| 47 |
struct Backbone |
| 48 |
{ |
| 49 |
std::vector<PtrNode> Nodes; |
| 50 |
std::vector<int32_t> Covs; |
| 51 |
}; |
| 52 |
|
| 53 |
template <typename T> |
| 54 |
struct Bucket |
| 55 |
{ |
| 56 |
~Bucket() |
| 57 |
{ |
| 58 |
for (auto& item : Data) { |
| 59 |
if (item) { |
| 60 |
delete item; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
void Add(T item) { Data.emplace_back(item); } |
| 66 |
|
| 67 |
std::vector<T> Data; |
| 68 |
}; |
| 69 |
|
| 70 |
using EdgeBucket = Bucket<PtrEdgeNode>; |
| 71 |
using NodeBucket = Bucket<PtrNode>; |
| 72 |
|
| 73 |
void ConsensusKmerGraphConstruction(const std::string& ref, Backbone& backbone, |
| 74 |
EdgeBucket& edgeBucket, NodeBucket& nodeBucket) |
| 75 |
{ |
| 76 |
const int32_t refLen = ref.size(); |
| 77 |
PtrNode previous_node = NULL; |
| 78 |
PtrNode current_node = NULL; |
| 79 |
backbone.Nodes.resize(refLen); |
| 80 |
|
| 81 |
for (int32_t j = 0; j < refLen; ++j) { |
| 82 |
previous_node = current_node; |
| 83 |
current_node = new Node(); |
| 84 |
nodeBucket.Add(current_node); |
| 85 |
backbone.Nodes[j] = current_node; |
| 86 |
current_node->Base = ref[j]; |
| 87 |
current_node->InBackbone = true; |
| 88 |
++current_node->Cov; |
| 89 |
if (j >= 1) { |
| 90 |
previous_node->Right = new EdgeNode(); |
| 91 |
edgeBucket.Add(previous_node->Right); |
| 92 |
previous_node->Right->NodePtr = current_node; |
| 93 |
|
| 94 |
current_node->Left = new EdgeNode(); |
| 95 |
edgeBucket.Add(current_node->Left); |
| 96 |
current_node->Left->NodePtr = previous_node; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
void NormalizeAlignment(Query& query) |
| 102 |
{ |
| 103 |
std::string qAlignedSeqNew; |
| 104 |
std::string tAlignedSeqNew; |
| 105 |
const int32_t refLen = query.tAlignedSeq.size(); |
| 106 |
const int32_t qryLen = query.qAlignedSeq.size(); |
| 107 |
qAlignedSeqNew.resize(2 * qryLen); |
| 108 |
tAlignedSeqNew.resize(2 * qryLen); |
| 109 |
int32_t curPos = 0; |
| 110 |
for (int32_t i = 0; i < qryLen; ++i) { |
| 111 |
char& curQry = query.qAlignedSeq[i]; |
| 112 |
char& curRef = query.tAlignedSeq[i]; |
| 113 |
if (curQry == curRef) { |
| 114 |
qAlignedSeqNew[curPos] = curQry; |
| 115 |
tAlignedSeqNew[curPos] = curRef; |
| 116 |
++curPos; |
| 117 |
} else { |
| 118 |
if (curQry != '-' && curRef != '-') { |
| 119 |
tAlignedSeqNew[curPos] = curRef; |
| 120 |
++curPos; |
| 121 |
bool replace = false; |
| 122 |
for (int32_t j = i + 1; j < refLen; ++j) { |
| 123 |
auto& curJRef = query.tAlignedSeq[j]; |
| 124 |
if (curJRef != '-') { |
| 125 |
if (curJRef == curQry) { |
| 126 |
replace = true; |
| 127 |
curJRef = '-'; |
| 128 |
tAlignedSeqNew[curPos] = curQry; |
| 129 |
} |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
if (!replace) { |
| 134 |
tAlignedSeqNew[curPos] = '-'; |
| 135 |
} |
| 136 |
--curPos; |
| 137 |
|
| 138 |
qAlignedSeqNew[curPos] = '-'; |
| 139 |
++curPos; |
| 140 |
qAlignedSeqNew[curPos] = curQry; |
| 141 |
++curPos; |
| 142 |
} else { |
| 143 |
if (curQry == '-') { |
| 144 |
for (int32_t j = i + 1; j < qryLen; ++j) { |
| 145 |
auto& curJQry = query.qAlignedSeq[j]; |
| 146 |
if (curJQry != '-') { |
| 147 |
if (curJQry == curRef) { |
| 148 |
curQry = curJQry; |
| 149 |
curJQry = '-'; |
| 150 |
} |
| 151 |
break; |
| 152 |
} |
| 153 |
} |
| 154 |
} else { |
| 155 |
for (int32_t j = i + 1; j < qryLen; ++j) { |
| 156 |
auto& curJRef = query.tAlignedSeq[j]; |
| 157 |
if (curJRef != '-') { |
| 158 |
if (curJRef == curQry) { |
| 159 |
curRef = curJRef; |
| 160 |
curJRef = '-'; |
| 161 |
} |
| 162 |
break; |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
qAlignedSeqNew[curPos] = curQry; |
| 168 |
tAlignedSeqNew[curPos] = curRef; |
| 169 |
++curPos; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
qAlignedSeqNew.resize(curPos); |
| 174 |
tAlignedSeqNew.resize(curPos); |
| 175 |
query.qAlignedSeq = std::move(qAlignedSeqNew); |
| 176 |
query.tAlignedSeq = std::move(tAlignedSeqNew); |
| 177 |
} |
| 178 |
|
| 179 |
void AddPathToBackbone(Backbone& backbone, Query& query, EdgeBucket& edgeBucket, |
| 180 |
NodeBucket& nodeBucket) |
| 181 |
{ |
| 182 |
NormalizeAlignment(query); |
| 183 |
|
| 184 |
int32_t target_position = query.tStart; |
| 185 |
Node* previous_node = NULL; |
| 186 |
Node* current_node = NULL; |
| 187 |
int32_t MatchPosition = -100; |
| 188 |
int32_t backboneLength = backbone.Nodes.size(); |
| 189 |
|
| 190 |
const int32_t qryLen = query.qAlignedSeq.size(); |
| 191 |
for (int32_t i = 0; i < qryLen; ++i) { |
| 192 |
if (query.qAlignedSeq[i] == '-') { |
| 193 |
MatchPosition = -1; |
| 194 |
if (query.tAlignedSeq[i] != '-') { |
| 195 |
++target_position; |
| 196 |
} |
| 197 |
continue; |
| 198 |
} |
| 199 |
MatchPosition = target_position; |
| 200 |
if (query.qAlignedSeq[i] != query.tAlignedSeq[i]) { |
| 201 |
MatchPosition = -1; |
| 202 |
} |
| 203 |
|
| 204 |
char base{' '}; |
| 205 |
for (int32_t j = i; j < qryLen; ++j) { |
| 206 |
if (query.qAlignedSeq[j] != '-') { |
| 207 |
base = query.qAlignedSeq[j]; |
| 208 |
break; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
if (base == ' ') { |
| 213 |
break; |
| 214 |
} |
| 215 |
|
| 216 |
if (MatchPosition >= backboneLength) { |
| 217 |
break; |
| 218 |
} |
| 219 |
|
| 220 |
if (MatchPosition >= 0) { |
| 221 |
current_node = backbone.Nodes[MatchPosition]; |
| 222 |
|
| 223 |
if (previous_node != NULL) { |
| 224 |
++current_node->Cov; |
| 225 |
} |
| 226 |
|
| 227 |
// link previous node to the current one |
| 228 |
if (previous_node != NULL) { |
| 229 |
PtrEdgeNode* edge_p2p = &(previous_node->Right); |
| 230 |
|
| 231 |
while (*edge_p2p != NULL) { |
| 232 |
if ((*edge_p2p)->NodePtr == current_node) { |
| 233 |
++(*edge_p2p)->EdgeCov; |
| 234 |
break; |
| 235 |
} |
| 236 |
edge_p2p = &((*edge_p2p)->NextEdge); |
| 237 |
} |
| 238 |
|
| 239 |
if (*edge_p2p == NULL) { |
| 240 |
(*edge_p2p) = new EdgeNode(); |
| 241 |
edgeBucket.Add(*edge_p2p); |
| 242 |
(*edge_p2p)->NextEdge = NULL; |
| 243 |
(*edge_p2p)->NodePtr = current_node; |
| 244 |
++(*edge_p2p)->EdgeCov; |
| 245 |
} |
| 246 |
|
| 247 |
edge_p2p = &(current_node->Left); |
| 248 |
while (*edge_p2p != NULL) { |
| 249 |
if ((*edge_p2p)->NodePtr == previous_node) { |
| 250 |
++(*edge_p2p)->EdgeCov; |
| 251 |
break; |
| 252 |
} |
| 253 |
edge_p2p = &((*edge_p2p)->NextEdge); |
| 254 |
} |
| 255 |
if (*edge_p2p == NULL) { |
| 256 |
(*edge_p2p) = new EdgeNode(); |
| 257 |
edgeBucket.Add(*edge_p2p); |
| 258 |
(*edge_p2p)->NextEdge = NULL; |
| 259 |
(*edge_p2p)->NodePtr = previous_node; |
| 260 |
++(*edge_p2p)->EdgeCov; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
} else { |
| 265 |
|
| 266 |
//link previous node to the current one |
| 267 |
|
| 268 |
if (previous_node == NULL) { |
| 269 |
current_node = new Node(); |
| 270 |
nodeBucket.Add(current_node); |
| 271 |
current_node->Base = base; |
| 272 |
++current_node->Cov; |
| 273 |
previous_node = current_node; |
| 274 |
} else { |
| 275 |
PtrEdgeNode* edge_p2p = &(previous_node->Right); |
| 276 |
while (*edge_p2p != NULL) { |
| 277 |
if ((*edge_p2p)->NodePtr->Base == base && !(*edge_p2p)->NodePtr->InBackbone) { |
| 278 |
++(*edge_p2p)->EdgeCov; |
| 279 |
current_node = (*edge_p2p)->NodePtr; |
| 280 |
++current_node->Cov; |
| 281 |
break; |
| 282 |
} |
| 283 |
edge_p2p = &((*edge_p2p)->NextEdge); |
| 284 |
} |
| 285 |
|
| 286 |
if (*edge_p2p == NULL) { |
| 287 |
(*edge_p2p) = new EdgeNode(); |
| 288 |
edgeBucket.Add(*edge_p2p); |
| 289 |
(*edge_p2p)->NextEdge = NULL; |
| 290 |
current_node = new Node(); |
| 291 |
nodeBucket.Add(current_node); |
| 292 |
current_node->Base = base; |
| 293 |
++current_node->Cov; |
| 294 |
(*edge_p2p)->NodePtr = current_node; |
| 295 |
++(*edge_p2p)->EdgeCov; |
| 296 |
} |
| 297 |
|
| 298 |
// link current node to previous |
| 299 |
edge_p2p = &(current_node->Left); |
| 300 |
while (*edge_p2p != NULL) { |
| 301 |
if ((*edge_p2p)->NodePtr->Base == previous_node->Base) { |
| 302 |
++(*edge_p2p)->EdgeCov; |
| 303 |
break; |
| 304 |
} |
| 305 |
edge_p2p = &((*edge_p2p)->NextEdge); |
| 306 |
} |
| 307 |
|
| 308 |
if (*edge_p2p == NULL) { |
| 309 |
(*edge_p2p) = new EdgeNode(); |
| 310 |
edgeBucket.Add(*edge_p2p); |
| 311 |
(*edge_p2p)->NextEdge = NULL; |
| 312 |
(*edge_p2p)->NodePtr = previous_node; |
| 313 |
++(*edge_p2p)->EdgeCov; |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
previous_node = current_node; |
| 319 |
|
| 320 |
if (query.tAlignedSeq[i] != '-') { |
| 321 |
++target_position; |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
void BFSFindBestPath(Backbone& backbone, int32_t node_idx, int32_t minCov, double threshold) |
| 327 |
{ |
| 328 |
PtrNode begin_node = backbone.Nodes[node_idx]; |
| 329 |
std::list<PtrNode> node_list; |
| 330 |
node_list.push_back(begin_node); |
| 331 |
int32_t max_cov = backbone.Covs[node_idx]; |
| 332 |
while (node_list.size() > 0) { |
| 333 |
PtrNode current_node = node_list.front(); |
| 334 |
node_list.pop_front(); |
| 335 |
PtrEdgeNode edge_ptr = current_node->Right; |
| 336 |
while (edge_ptr != NULL) { |
| 337 |
bool update = 0; |
| 338 |
int32_t newScore = 0; |
| 339 |
int32_t newThreshold = |
| 340 |
std::max(static_cast<int32_t>(std::round((max_cov * threshold))), minCov); |
| 341 |
newScore = (current_node->Score + edge_ptr->EdgeCov - newThreshold); |
| 342 |
if (edge_ptr->NodePtr->Score < newScore) { |
| 343 |
update = 1; |
| 344 |
} |
| 345 |
|
| 346 |
if (update) { |
| 347 |
edge_ptr->NodePtr->Cov = current_node->Cov + 1; |
| 348 |
edge_ptr->NodePtr->Score = newScore; |
| 349 |
edge_ptr->NodePtr->LastNode = current_node; |
| 350 |
if (!edge_ptr->NodePtr->InBackbone) { |
| 351 |
node_list.push_back(edge_ptr->NodePtr); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
edge_ptr = edge_ptr->NextEdge; |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
void FindBestPath(Backbone& backbone, int32_t minCov, double threshold) |
| 361 |
{ |
| 362 |
backbone.Nodes[0]->Cov = 1; //depth |
| 363 |
const int32_t numNodex = backbone.Nodes.size(); |
| 364 |
for (int32_t i = 0; i + 1 < numNodex; ++i) { |
| 365 |
BFSFindBestPath(backbone, i, minCov, threshold); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
} // namespace SparcSimpleImpl |
| 370 |
std::string SparcSimpleConsensus(const std::string& backboneSeq, std::vector<Query>& queries, |
| 371 |
const int32_t minCov, const double threshold) |
| 372 |
{ |
| 373 |
SparcSimpleImpl::Backbone backbone; |
| 374 |
|
| 375 |
SparcSimpleImpl::EdgeBucket edgeBucket; |
| 376 |
SparcSimpleImpl::NodeBucket nodeBucket; |
| 377 |
|
| 378 |
SparcSimpleImpl::ConsensusKmerGraphConstruction(backboneSeq, backbone, edgeBucket, nodeBucket); |
| 379 |
|
| 380 |
for (Query& q : queries) { |
| 381 |
SparcSimpleImpl::AddPathToBackbone(backbone, q, edgeBucket, nodeBucket); |
| 382 |
} |
| 383 |
|
| 384 |
const int32_t backboneSeqLen = backboneSeq.size(); |
| 385 |
// hopefully never goes zero |
| 386 |
size_t radius = std::min(std::min<size_t>(200, backbone.Nodes.size()), |
| 387 |
static_cast<size_t>(backboneSeqLen - 1)); |
| 388 |
std::map<int, int> cov_cnt; |
| 389 |
backbone.Covs.resize(backbone.Nodes.size()); |
| 390 |
|
| 391 |
for (size_t i = 0; i < radius; ++i) { |
| 392 |
if (backbone.Nodes[i] != NULL) { |
| 393 |
++cov_cnt[backbone.Nodes[i]->Cov]; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
for (size_t i = 0; i < backbone.Nodes.size(); ++i) { |
| 398 |
if (i >= radius) { |
| 399 |
if (backbone.Nodes[i - radius] != NULL) { |
| 400 |
--cov_cnt[backbone.Nodes[i - radius]->Cov]; |
| 401 |
if (cov_cnt[backbone.Nodes[i - radius]->Cov] <= 0) { |
| 402 |
cov_cnt.erase(backbone.Nodes[i - radius]->Cov); |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
if ((i + radius < backbone.Nodes.size()) && (backbone.Nodes[i + radius] != NULL)) { |
| 407 |
++cov_cnt[backbone.Nodes[i + radius]->Cov]; |
| 408 |
} |
| 409 |
|
| 410 |
if (cov_cnt.size() > 0) { |
| 411 |
backbone.Covs[i] = cov_cnt.rbegin()->first; |
| 412 |
} else { |
| 413 |
backbone.Covs[i] = 0; |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
SparcSimpleImpl::FindBestPath(backbone, minCov, threshold); |
| 418 |
|
| 419 |
std::string consensus; |
| 420 |
int64_t maxScore = 0; |
| 421 |
size_t position = 0; |
| 422 |
|
| 423 |
for (size_t i = 0; i < backbone.Nodes.size(); ++i) { |
| 424 |
if (backbone.Nodes[i]->Score > maxScore) { |
| 425 |
maxScore = backbone.Nodes[i]->Score; |
| 426 |
position = i; |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
if (maxScore == 0) { |
| 431 |
return ""; |
| 432 |
} |
| 433 |
|
| 434 |
SparcSimpleImpl::PtrNode current_node = backbone.Nodes[position]; |
| 435 |
|
| 436 |
if (current_node != NULL) { |
| 437 |
char base = current_node->Base; |
| 438 |
consensus = base; |
| 439 |
current_node = current_node->LastNode; |
| 440 |
while (current_node != NULL) { |
| 441 |
consensus.push_back(current_node->Base); |
| 442 |
current_node = current_node->LastNode; |
| 443 |
} |
| 444 |
|
| 445 |
std::reverse(consensus.begin(), consensus.end()); |
| 446 |
} |
| 447 |
|
| 448 |
current_node = backbone.Nodes[position]; |
| 449 |
backbone.Nodes.clear(); |
| 450 |
backbone.Nodes.push_back(current_node); |
| 451 |
if (current_node != NULL) { |
| 452 |
current_node = current_node->LastNode; |
| 453 |
while (current_node != NULL) { |
| 454 |
backbone.Nodes.push_back(current_node); |
| 455 |
current_node = current_node->LastNode; |
| 456 |
} |
| 457 |
} |
| 458 |
std::reverse(backbone.Nodes.begin(), backbone.Nodes.end()); |
| 459 |
|
| 460 |
return consensus; |
| 461 |
} |
| 462 |
} // namespace Sparc |