-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebruijn_graph.cc
1583 lines (1406 loc) · 47.8 KB
/
debruijn_graph.cc
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* =====================================================================================
*
* Filename: hashutil.cc
*
* Description:
*
* Version: 1.0
* Created: 04/18/2016 04:49:32 PM
* Revision: none
* Compiler: gcc
*
* Author: Prashant Pandey (ppandey@cs.stonybrook.edu)
* Rob Johnson (rob@cs.stonybrook.edu)
* Rob Patro (rob.patro@cs.stonybrook.edu)
* Organization: Stony Brook University
*
* =====================================================================================
*/
#include<set>
#include<map>
#include<utility>
#include<unordered_set>
#include<iostream>
#include<cassert>
#include <fstream>
#include <time.h>
#include <sys/time.h>
#include "debruijn_graph.h"
#include "hashutil.h"
static void print_time_elapsed(std::string desc,
struct timeval* start,
struct timeval* end);
#define BITMASK(nbits) ((nbits) == 64 \
? 0xffffffffffffffff \
: (1ULL << (nbits)) - 1ULL)
using namespace std;
namespace dna {
/////////////// bases /////////////////
base operator-(base b) {
return (base)((~((uint64_t)b)) & 0x3ULL);
}
const base bases[4] = {C, A, T, G};
const map<char, base> base_from_char = { {'A', A}, {'C', C},
{'G', G}, {'T', T},
{'N', A}};
const map<base, char> base_to_char = { {A, 'A'}, {C, 'C'},
{G, 'G'}, {T, 'T'}};
///////////// kmers /////////////////////
kmer::kmer(void) : len(0), val(0) {}
kmer::kmer(base b) : len(1), val((uint64_t)b) {}
kmer::kmer(int l, uint64_t v) : len(l), val(v & BITMASK(2*l)) {
assert(l <= 32);
}
static uint64_t string_to_kmer_val(string s) {
uint64_t val = 0;
for (auto c : s)
val = (val << 2) | ((uint64_t)(base_from_char.at(c)));
return val;
}
kmer::kmer(string s) : len(s.size()), val(string_to_kmer_val(s)) {
assert(s.size() <= 32);
}
// Convert to string
kmer::operator string() const {
string s;
for (auto i = 1; i < len+1; i++)
s = s + base_to_char.at((base)((val >> (2*(len - i))) & BITMASK(2)));
return s;
}
bool operator<(kmer a, kmer b) {
return a.len != b.len ? a.len < b.len : a.val < b.val;
}
bool operator==(kmer a, kmer b) {
return a.len == b.len && a.val == b.val;
}
bool operator!=(kmer a, kmer b) {
return !operator==(a, b);
}
// Return the reverse complement of k
kmer operator-(kmer k) {
uint64_t val = k.val;
val =
(val >> 32) |
(val << 32);
val =
((val >> 16) & 0x0000ffff0000ffff) |
((val << 16) & 0xffff0000ffff0000);
val =
((val >> 8) & 0x00ff00ff00ff00ff) |
((val << 8) & 0xff00ff00ff00ff00);
val =
((val >> 4) & 0x0f0f0f0f0f0f0f0f) |
((val << 4) & 0xf0f0f0f0f0f0f0f0);
val =
((val >> 2) & 0x3333333333333333) |
((val << 2) & 0xcccccccccccccccc);
val = ~val;
val >>= 64-2*k.len;
return kmer(k.len, val);
}
// backwards from standard definition to match kmer.h definition
kmer canonicalize(kmer k) {
return -k < k ? k : -k;
}
// Return the kmer of length |a| that results from shifting b into a
// from the right
kmer operator<<(kmer a, kmer b) {
uint64_t val = ((a.val << (2*b.len)) | b.val) & BITMASK(2*a.len);
return kmer(a.len, val);
}
// Return the kmer of length |b| that results from shifting b into a
// from the left
kmer operator>>(kmer a, kmer b) {
uint64_t val
= ((b.val >> (2*a.len)) | (a.val << (2*(b.len - a.len))))
& BITMASK(2*b.len);
return kmer(b.len, val);
}
// Append two kmers
kmer operator+(kmer a, kmer b) {
int len = a.len + b.len;
assert(len <= 32);
uint64_t val = (a.val << (2*b.len)) | b.val;
return kmer(len, val);
}
kmer prefix(kmer k, int len) { return kmer(len, k.val >> (2*(k.len - len))); }
kmer suffix(kmer k, int len) { return kmer(len, k.val & BITMASK(2*len)) ; }
bool period_divides(kmer k, uint64_t periodicity) {
static const uint64_t multipliers[33] =
{
0,
0x5555555555555555, // 1
0x1111111111111111, // 2
0x1041041041041041, // 3
0x0101010101010101, // 4
0x1004010040100401, // 5
0x1001001001001001, // 6
0x0100040010004001, // 7
0x0001000100010001, // 8
0x0040001000040001, // 9
0x1000010000100001, // 10
0x0000100000400001, // 11
0x0001000001000001, // 12
0x0010000004000001, // 13
0x0100000010000001, // 14
0x1000000040000001, // 15
0x0000000100000001, // 16
0x0000000400000001, // 17
0x0000001000000001, // 18
0x0000004000000001, // 19
0x0000010000000001, // 20
0x0000040000000001, // 21
0x0000100000000001, // 22
0x0000400000000001, // 23
0x0001000000000001, // 24
0x0004000000000001, // 25
0x0010000000000001, // 26
0x0040000000000001, // 27
0x0100000000000001, // 28
0x0400000000000001, // 29
0x1000000000000001, // 30
0x4000000000000001, // 31
0x0000000000000001, // 32
};
uint64_t piece = k.val & BITMASK(2*periodicity);
piece = piece * multipliers[periodicity];
piece = piece & BITMASK(2*k.len);
return piece == k.val;
}
uint64_t period(kmer k) {
for (int i = 1; i <= k.len; i++) {
if (period_divides(k, i))
return i;
}
abort();
}
canonical_kmer::canonical_kmer(void) : kmer() {}
canonical_kmer::canonical_kmer(base b) : kmer(canonicalize(kmer(b))) {}
canonical_kmer::canonical_kmer(int l, uint64_t v)
: kmer(canonicalize(kmer(l, v))) {}
canonical_kmer::canonical_kmer(std::string s) : kmer(canonicalize(kmer(s))) {}
canonical_kmer::canonical_kmer(kmer k) : kmer(canonicalize(k)) {}
}
namespace std {
size_t hash<dna::kmer>::operator()(const dna::kmer & x) const {
return hash<int>()(x.val);
}
size_t
hash<dna::canonical_kmer>::operator()(const dna::canonical_kmer & x) const {
return hash<int>()(x.val);
}
}
/////////////// Interface for CQFs of kmers ///////////////
namespace dna {
template<class kmer_type>
kmer_counter_cqf<kmer_type>::kmer_counter_cqf(uint64_t l, QF cqf)
: counts(cqf), len(l) {}
template<class kmer_type>
static uint64_t kmer_hash(kmer_type k, uint32_t seed, __uint128_t range) {
return
kmercounting::HashUtil::MurmurHash64A(&k.val,
sizeof(k.val),
seed)
% range;
}
template<class kmer_type>
uint64_t kmer_counter_cqf<kmer_type>::get_hash(kmer_type k) const {
return kmer_hash(k, counts.metadata->seed, counts.metadata->range);
}
template<class kmer_type>
void kmer_counter_cqf<kmer_type>::increment(kmer_type k, uint64_t amount) {
qf_insert(&counts, get_hash(k), 0, amount, 0, 0);
}
template<class kmer_type>
size_t kmer_counter_cqf<kmer_type>::count(kmer_type k) const {
return qf_count_key_value(&counts, get_hash(k), 0);
}
template<class kmer_type>
kmer_counter_lossless_cqf<kmer_type>::kmer_counter_lossless_cqf(uint64_t l,
QF cqf)
: kmer_counter_cqf<kmer_type>(l, cqf) {}
template<class kmer_type>
uint64_t kmer_invertible_hash(kmer_type k, uint32_t seed) {
return kmercounting::HashUtil::hash_64(k.val, BITMASK(2*k.len));
}
template<class kmer_type>
void kmer_counter_lossless_cqf<kmer_type>::increment(kmer_type k,
uint64_t amount) {
uint64_t h = kmer_invertible_hash<kmer_type>(k, this->counts.metadata->seed);
qf_insert(&this->counts, h, 0, amount, 0, 0);
}
template<class kmer_type>
size_t kmer_counter_lossless_cqf<kmer_type>::count(kmer_type k) const {
uint64_t h = kmer_invertible_hash<kmer_type>(k, this->counts.metadata->seed);
return qf_count_key_value(&this->counts, h, 0);
}
template<class kmer_type>
uint64_t kmer_counter_lossless_cqf<kmer_type>::size(void) const {
return this->counts.metadata->ndistinct_elts;
}
template<class kmer_type>
kmer_counter_lossless_cqf<kmer_type>::iterator::iterator(uint64_t l, QFi it)
: len(l), iter(it) {};
template<class kmer_type>
kmer_type
kmer_counter_lossless_cqf<kmer_type>::iterator::operator*(void) const {
uint64_t key = 0, value = 0, count = 0;
qfi_get(&iter, &key, &value, &count);
return kmer_type(len, kmercounting::HashUtil::hash_64i(key, BITMASK(2*len)));
}
template<class kmer_type>
void kmer_counter_lossless_cqf<kmer_type>::iterator::operator++(void) {
qfi_next(&iter);
}
template<class kmer_type>
bool
operator!=(const typename kmer_counter_lossless_cqf<kmer_type>::iterator &a,
const typename kmer_counter_lossless_cqf<kmer_type>::iterator &b) {
return !qfi_end(&a.iter) || !qfi_end(&b.iter);
}
template<class kmer_type>
typename kmer_counter_lossless_cqf<kmer_type>::iterator
kmer_counter_lossless_cqf<kmer_type>::begin(void) const {
QFi qfi;
qf_iterator(&this->counts, &qfi, 0);
return iterator(this->len, qfi);
}
template<class kmer_type>
typename kmer_counter_lossless_cqf<kmer_type>::iterator
kmer_counter_lossless_cqf<kmer_type>::end(void) const {
QFi qfi;
qf_iterator(&this->counts, &qfi, 0xffffffffffffffff);
return iterator(this->len, qfi);
}
/////////////// CQF-based de Bruijn graphs ///////////////
debruijn_graph::debruijn_graph(int l,
kmer_counter_cqf<canonical_kmer> occ,
kmer_counter_lossless_cqf<canonical_kmer> begs,
kmer_counter_lossless_cqf<canonical_kmer> enz,
map<edge, uint64_t> cor
)
: len(l), occurences(occ), begins(begs), ends(enz), corrections(cor) {}
uint64_t debruijn_graph::abundance(edge e) const {
uint64_t o = get_occurences(e);
if (o)
return o - get_corrections(e);
return 0;
}
void debruijn_graph::insert_read(string s) {
// TODO: handle locking
if (s.size() < len)
return;
kmer k(s.substr(0, len));
node n = prefix(k, len-1);
record_end(n, k);
record_occurence(k);
for (auto it = s.begin() + len; it != s.end(); ++it) {
node nextn = suffix(k, len-1);
if (can_have_duplex_edges(nextn))
record_end(nextn, k);
k = k << base_from_char.at(*it);
record_occurence(k);
if (can_have_duplex_edges(nextn))
record_end(nextn, k);
}
n = suffix(k, len-1);
record_end(n, k);
}
const uint64_t debruijn_graph::infinite_depth = ((uint64_t)-1);
template<template<class, class> class Q>
typename debruijn_graph::node_iterator<Q>::work_item
debruijn_graph::node_iterator<Q>::front(const queue<work_item> &w) const {
return w.front();
}
template<template<class, class> class Q>
typename debruijn_graph::node_iterator<Q>::work_item
debruijn_graph::node_iterator<Q>::front(const stack<work_item> &w) const {
return w.top();
}
template<template<class, class> class Q>
debruijn_graph::node_iterator<Q>::node_iterator(const debruijn_graph &g,
const std::unordered_set<node> starts,
uint64_t depth)
: dbg(g) {
for (const auto &n : starts) {
work_item w;
w.curr = n;
w.depth = depth;
work.push(w);
visited.insert(n);
}
}
template<template<class, class> class Q>
debruijn_graph::node debruijn_graph::node_iterator<Q>::operator*(void) const {
return front(work).curr;
}
template<template<class, class> class Q>
void debruijn_graph::node_iterator<Q>::operator++(void) {
work_item w;
w = front(work);
work.pop();
uint64_t nsiblings = 0;
if (w.depth)
for (const auto &neighbor : dbg.neighbors(w.curr))
if (neighbor != w.curr && neighbor != w.prev) {
if (visited.count(neighbor))
nsiblings++;
else {
work_item neww = { neighbor,
w.curr,
w.depth == infinite_depth
? infinite_depth
: w.depth - 1 };
visited.insert(neighbor);
work.push(neww);
}
}
if (w.depth == infinite_depth && nsiblings == 0)
visited.erase(w.curr);
}
template<template<class, class> class Q> bool
debruijn_graph::node_iterator<Q>::operator!=(const node_iterator<Q> &other) const {
return !work.empty() || !other.work.empty();
}
template<template<class, class> class Q>
debruijn_graph::nodes_container<Q>::nodes_container(const debruijn_graph &g,
const std::unordered_set<node> strts,
uint64_t dpth)
: dbg(g), starts(strts), depth(dpth) {}
template<template<class, class> class Q>
debruijn_graph::node_iterator<Q>
debruijn_graph::nodes_container<Q>::begin(void) const {
return node_iterator<Q>(dbg, starts, depth);
}
template<template<class, class> class Q>
debruijn_graph::node_iterator<Q>
debruijn_graph::nodes_container<Q>::end(void) const {
return node_iterator<Q>(dbg, unordered_set<node>(), depth);
}
debruijn_graph::nodes_container<stack>
debruijn_graph::reachable_nodes(void) const {
return nodes_container<stack>(*this, get_all_ends(),
infinite_depth);
}
debruijn_graph::nodes_container<stack>
debruijn_graph::connected_component(node n) const {
return nodes_container<stack>(*this,
unordered_set<node>({n}),
infinite_depth);
}
debruijn_graph::nodes_container<queue>
debruijn_graph::nearby_nodes(node n, uint64_t depth) const {
return nodes_container<queue>(*this, unordered_set<node>({n}), depth);
}
template<template<class, class> class Q>
debruijn_graph::edge_iterator<Q>::edge_iterator(const debruijn_graph &g,
const unordered_set<node> strts,
uint64_t dpth)
: dbg(g),
anode(g, strts, dpth),
nodeend(dbg, unordered_set<node>(), infinite_depth),
anode_edges(),
aedge(anode_edges.end())
{
while (anode != nodeend) {
anode_edges = dbg.edges(*anode);
aedge = anode_edges.begin();
while (aedge != anode_edges.end() &&
!is_loop(*aedge) &&
is_right_node(*aedge, *anode)) {
++aedge;
}
if (aedge != anode_edges.end())
return;
++anode;
}
}
template<template<class, class> class Q>
debruijn_graph::edge debruijn_graph::edge_iterator<Q>::operator*(void) const {
return *aedge;
}
template<template<class, class> class Q>
void debruijn_graph::edge_iterator<Q>::operator++(void) {
++aedge;
while (aedge != anode_edges.end() &&
!is_loop(*aedge) &&
is_right_node(*aedge, *anode))
++aedge;
while(aedge == anode_edges.end()) {
++anode;
if (anode != nodeend) {
anode_edges = dbg.edges(*anode);
aedge = anode_edges.begin();
while (aedge != anode_edges.end() &&
!is_loop(*aedge) &&
is_right_node(*aedge, *anode))
++aedge;
} else {
anode_edges = set<node>();
aedge = anode_edges.end();
break;
}
}
}
template<template<class, class> class Q>
bool
debruijn_graph::edge_iterator<Q>::operator!=(const debruijn_graph::edge_iterator<Q> &other) const {
return aedge != anode_edges.end() || other.aedge != other.anode_edges.end();
}
template<template<class, class> class Q>
debruijn_graph::edges_container<Q>::edges_container(const debruijn_graph &g,
const std::unordered_set<node> strts,
uint64_t dpth)
:dbg(g), starts(strts), depth(dpth) {
}
template<template<class, class> class Q>
debruijn_graph::edge_iterator<Q>
debruijn_graph::edges_container<Q>::begin(void) const {
return edge_iterator<Q>(dbg, starts, depth);
}
template<template<class, class> class Q>
debruijn_graph::edge_iterator<Q>
debruijn_graph::edges_container<Q>::end(void) const {
return edge_iterator<Q>(dbg, unordered_set<node>(), depth);
}
debruijn_graph::edges_container<stack>
debruijn_graph::reachable_edges(void) const {
return edges_container<stack>(*this, get_all_ends(), infinite_depth);
}
debruijn_graph::edges_container<stack>
debruijn_graph::connected_component_edges(node n) const {
return edges_container<stack>(*this,
unordered_set<node>({n}),
infinite_depth);
}
debruijn_graph::edges_container<queue>
debruijn_graph::nearby_edges(node n, uint64_t depth) const {
return edges_container<queue>(*this, unordered_set<node>({n}), depth);
}
set<debruijn_graph::edge> debruijn_graph::left_edges(node n) const {
set<edge> result;
for (const auto b : bases) {
edge e = b + n;
if (abundance(e) && !is_duplex_edge(n, e))
result.insert(e);
}
return result;
}
set<debruijn_graph::edge> debruijn_graph::right_edges(node n) const {
set<edge> result;
for (const auto b : bases) {
edge e = n + b;
if (abundance(e) && !is_duplex_edge(n, e))
result.insert(e);
}
return result;
}
set<debruijn_graph::edge> debruijn_graph::duplex_edges(node n) const {
set<edge> result;
for (const auto b : bases) {
edge e1 = b + n;
if (abundance(e1) && is_duplex_edge(n, e1))
result.insert(e1);
edge e2 = n + b;
if (abundance(e2) && is_duplex_edge(n, e2))
result.insert(e2);
}
return result;
}
set<debruijn_graph::edge> debruijn_graph::edges(node n) const {
set<edge> result;
for (const auto b : bases) {
if (abundance(n + b))
result.insert(n + b);
if (abundance(b + n))
result.insert(b + n);
}
return result;
}
set<debruijn_graph::node> debruijn_graph::left_neighbors(node n) const {
set<node> result;
for (const auto b : bases)
if (abundance(b + n) && !is_duplex_edge(n, b + n))
result.insert(b >> n);
return result;
}
set<debruijn_graph::node> debruijn_graph::right_neighbors(node n) const {
set<node> result;
for (const auto b : bases)
if (abundance(n + b) && !is_duplex_edge(n, n + b))
result.insert(n << b);
return result;
}
set<debruijn_graph::node> debruijn_graph::duplex_neighbors(node n) const {
set<node> result;
for (const auto b : bases) {
if (abundance(b + n) && is_duplex_edge(n, b + n))
result.insert(b >> n);
if (abundance(n + b) && is_duplex_edge(n, n + b))
result.insert(n << b);
}
return result;
}
set<debruijn_graph::node> debruijn_graph::neighbors(node n) const {
set<node> result;
for (const auto b : bases) {
if (abundance(b + n))
result.insert(b >> n);
if (abundance(n + b))
result.insert(n << b);
}
return result;
}
uint64_t debruijn_graph::left_degree(node n) const {
return left_edges(n).size();
return left_edges(n).size();
}
uint64_t debruijn_graph::right_degree(node n) const {
return right_edges(n).size();
}
uint64_t debruijn_graph::duplex_degree(node n) const {
return duplex_edges(n).size();
}
uint64_t debruijn_graph::degree(node n) const {
return edges(n).size();
}
uint64_t debruijn_graph::left_abundance(node n) const {
uint64_t sum = 0;
for (const auto &e : left_edges(n))
sum += abundance(e);
return sum;
}
uint64_t debruijn_graph::right_abundance(node n) const {
uint64_t sum = 0;
for (const auto &e : right_edges(n))
sum += abundance(e);
return sum;
}
uint64_t debruijn_graph::duplex_abundance(node n) const {
uint64_t sum = 0;
for (const auto &e : duplex_edges(n))
sum += abundance(e);
return sum;
}
bool debruijn_graph::is_leaf(node n) const {
return degree(n) < 2;
}
bool debruijn_graph::is_leaf_edge(edge e) const {
return is_leaf(left_node(e)) || is_leaf(right_node(e));
}
template<template<class, class> class Q>
void debruijn_graph::print_graph(string name,
nodes_container<Q> nodes,
edges_container<Q> edges,
ostream &os) const {
os << "digraph " << name << " {" << endl;
for (const auto & n : nodes) {
os << " "
<< (string)n
<< "[ label=\""
<< (string)n
<< "\\n("
<< get_left_ends(n) << ", "
<< get_right_ends(n)
<< ")\", style=filled, fillcolor="
<< ((get_left_ends(n) + get_right_ends(n)) ? "grey" : "white")
<< ", shape="
<< (can_have_duplex_edges(n) ? "diamond" : "ellipse")
<<" ];" << endl;
}
for (const auto & e : edges) {
node ln = left_node(e);
node rn = right_node(e);
os << " "
<< (string)ln << " -> "
<< (string)rn << "[ label=\"("
<< (string)e << ", "
<< get_occurences(e)
<< ")\", tailport="
<< (is_left_edge(ln, e) ? "w" : is_right_edge(ln, e) ? "e" : "s")
<< ", headport="
<< (is_left_edge(rn, e) ? "w" : is_right_edge(rn, e) ? "e" : "s")
<<" ];" << endl;
}
os << "}" << endl;
}
void debruijn_graph::print_reachable_graph(std::string name,
std::ostream &os) const {
print_graph(name, reachable_nodes(), reachable_edges(), os);
}
void debruijn_graph::print_nearby_graph(std::string name,
std::ostream &os,
node n,
uint64_t dist) const {
print_graph(name, nearby_nodes(n, dist), nearby_edges(n, dist), os);
}
void debruijn_graph::record_occurence(edge k) {
occurences.increment(k, 1);
}
uint64_t debruijn_graph::get_occurences(edge e) const {
return occurences.count(e);
}
void debruijn_graph::record_left_end(node n) {
begins.increment(n, 1);
}
uint64_t debruijn_graph::get_left_ends(node n) const {
return begins.count(n);
}
//set<debruijn_graph::node> debruijn_graph::get_all_left_ends(void) const {
//set<node> result;
//for (const auto & it = begins.begin(); it != begins.end(); ++it)
//result.insert(*it);
////for (const auto & p : left_ends)
////result.insert(p.first);
//return result;
//}
//set<debruijn_graph::node> debruijn_graph::get_all_right_ends(void) const {
//set<node> result;
//for (const auto & it = ends.begin(); it != ends.end(); ++it)
//result.insert(*it);
////for (const auto & p : right_ends)
////result.insert(p.first);
//return result;
//}
unordered_set<debruijn_graph::node> debruijn_graph::get_all_ends(void) const {
unordered_set<node> result;
for (auto it = begins.begin(); it != begins.end(); ++it)
result.insert(*it);
for (auto it = ends.begin(); it != ends.end(); ++it)
result.insert(*it);
//for (const auto & p : left_ends)
//result.insert(p.first);
//for (const auto & p : right_ends)
//result.insert(p.first);
return result;
}
void debruijn_graph::record_right_end(node n) {
ends.increment(n, 1);
//if (right_ends.count(n))
//right_ends[n]++;
//else
//right_ends[n] = 1;
}
uint64_t debruijn_graph::get_right_ends(node n) const {
return ends.count(n);
//return right_ends.count(n) ? right_ends.at(n) : 0;
}
void debruijn_graph::record_end(node n, edge e) {
if (is_left_edge(n, e)) {
record_left_end(n);
//cout << "Begin node: " << string(n) << endl;
//cout << "Begin edge: " << string(e) << endl;
} else if (is_right_edge(n, e)) {
record_right_end(n);
//cout << "End node: " << string(n) << endl;
//cout << "End edge: " << string(e) << endl;
}
}
int64_t debruijn_graph::left_invariant_value(node n) const
{
uint64_t sum = 0;
for (const auto &e : left_edges(n))
sum += (is_loop(e) ? 2 : 1) * abundance(e);
return sum - get_left_ends(n);
}
int64_t debruijn_graph::right_invariant_value(node n) const
{
uint64_t sum = 0;
for (const auto &e : right_edges(n))
sum += (is_loop(e) ? 2 : 1) * abundance(e);
return sum - get_right_ends(n);
}
int64_t debruijn_graph::invariant_discrepancy(node n) const {
return right_invariant_value(n) - left_invariant_value(n);
}
int64_t debruijn_graph::invariant_implied_abundance_correction(node n,
edge e) const {
if (is_left_edge(n, e))
return invariant_discrepancy(n);
else if (is_right_edge(n, e))
return -invariant_discrepancy(n);
else // duplex edge
return 0;
}
bool debruijn_graph::abundance_is_correct_whp(edge e) const {
// FIXME: need to check for small cycles, and adjust 3
for (const auto & n : nearby_nodes(left_node(e), 3))
if (invariant_discrepancy(n) != 0)
return false;
return period(e) > 6;
}
debruijn_graph::unresolved_edge_set::unresolved_edge_set(void)
: unordered_set<edge>() {}
bool debruijn_graph::abundance_is_definitely_correct(edge e,
unresolved_edges_map &mbi) const {
if (abundance(e) == 0)
return true;
uint64_t hash = occurences.get_hash(e);
return mbi.count(hash) == 0 || mbi[hash].count(e) == 0;
}
bool
debruijn_graph::left_side_is_definitely_correct(node n,
unresolved_edges_map &mbi) const {
for (const auto & e : left_edges(n)) {
if (!abundance_is_definitely_correct(e, mbi))
return false;
}
return true;
}
bool
debruijn_graph::right_side_is_definitely_correct(node n,
unresolved_edges_map &mbi) const {
for (const auto & e : right_edges(n)) {
if (!abundance_is_definitely_correct(e, mbi))
return false;
}
return true;
}
debruijn_graph::edge
debruijn_graph::find_single_error_edge(node n,
unresolved_edges_map &mbi) const {
vector<edge> error_edges;
for (const auto & e : edges(n))
if (!abundance_is_definitely_correct(e, mbi))
error_edges.push_back(e);
if (error_edges.size() == 1)
return error_edges.front();
return edge();
}
void debruijn_graph::add_correction(edge e, int64_t correction) {
assert(correction < 0);
corrections[e] -= correction;
}
uint64_t debruijn_graph::get_corrections(edge e) const {
return corrections.count(e) ? corrections.at(e) : 0;
}
void debruijn_graph::mark_edge_as_correct(edge e,
unresolved_edges_map &mbi,
unordered_set<edge> &work_queue,
bool can_use_hashing_rules) {
uint64_t hash = occurences.get_hash(e);
// If the edge is currently in mbi
if (mbi.count(hash) > 0 && mbi[hash].count(e) > 0) {
// Remove it, update the amount of unresolved count, and add its
// nodes to the work-queue
mbi[hash].erase(e);
for (const auto & n : nodes(e))
work_queue.insert(n);
if (can_use_hashing_rules) {
mbi[hash].unresolved_count -= abundance(e);
// If any edge currently wants more count than is available,
// then we know it wants too much
for (const auto & o : mbi[hash]) {
if (abundance(o) > mbi[hash].unresolved_count) {
add_correction(o, mbi[hash].unresolved_count - abundance(o));
for (const auto & n : nodes(o))
work_queue.insert(n);
}
}
// Figure out how much count the remaining edges currently want
uint64_t current_total_edge_count = 0;
for (const auto & o : mbi[hash])
current_total_edge_count += abundance(o);
// If the amount of total wanted count is the same as the amount
// of available count, then we know all the remaining edges are
// correct.
if (mbi[hash].unresolved_count == current_total_edge_count) {
for (const auto & o : mbi[hash])
for (const auto & n : nodes(o))
work_queue.insert(n);
mbi.erase(hash);
}
}
}
}
void debruijn_graph::do_work(node n,
unresolved_edges_map &mbi,
unordered_set<node> &work_queue,
bool can_use_hashing_rules) {
// If there's no invariant discrepancy, and we know that all the
// edges on one side are correct, then we can conclude that all
// the edges on the other side are also correct.
if (!invariant_discrepancy(n)) {
if (left_side_is_definitely_correct(n, mbi))
for (const auto & re : right_edges(n))
mark_edge_as_correct(re, mbi, work_queue, can_use_hashing_rules);
else if (right_side_is_definitely_correct(n, mbi))
for (const auto & le : left_edges(n))
mark_edge_as_correct(le, mbi, work_queue, can_use_hashing_rules);
// If we know that there are no "through paths" on the left
// side, and we know there are no paths ending on the right
// side, then there must not be _any_ paths on the right side.
} else if (left_invariant_value(n) == 0 && get_right_ends(n) == 0) {
for (const auto & re : right_edges(n)) {
add_correction(re, -abundance(re));
mark_edge_as_correct(re, mbi, work_queue, can_use_hashing_rules);
}
// And vice versa
} else if (right_invariant_value(n) == 0 && get_left_ends(n) == 0) {
for (const auto & le : left_edges(n)) {
add_correction(le, -abundance(le));
mark_edge_as_correct(le, mbi, work_queue, can_use_hashing_rules);
}
// If we know all but one edge is correct, then solve for the
// abundance of the remaining edge, fix it, and mark it correct.
} else {
edge wrong_edge = find_single_error_edge(n, mbi);
if (wrong_edge != edge()) {
int64_t correction = invariant_implied_abundance_correction(n,
wrong_edge);
if (correction != 0) {
if (is_loop(wrong_edge)) {
assert(((-correction) % 2) == 0);
correction /= 2;
}
add_correction(wrong_edge, correction);
mark_edge_as_correct(wrong_edge, mbi, work_queue, can_use_hashing_rules);
}
}
}
}
template<template<class, class> class Q>
void debruijn_graph::update_corrections(nodes_container<Q> &nc,
bool is_entire_graph,
bool profile) {
unordered_set<node> work_queue;
unresolved_edges_map mbi;
struct timeval start1, end1;
gettimeofday(&start1, NULL);
// Find all the edges that are near an invariant discrepancy or
// might be in a small cycle. Mark them as "might be incorrect"
for (const auto & n : nc)
if (invariant_discrepancy(n) || period(n) <= 6)
for (const auto & e : nearby_edges(n, 3)) {
uint64_t hash = occurences.get_hash(e);
mbi[hash].insert(e);
}
// The hash-collision-based error correction rules are only valid
// when we are working on the entire graph
if (is_entire_graph) {
// Ensure that all edges that hash-collide with an edge that
// "might be incorrect" are also marked as "might be incorrect".
for (const auto & n : nc)
for (const auto & e : edges(n)) {
uint64_t h = occurences.get_hash(e);
if (mbi.count(h) > 0)
mbi[h].insert(e);
}
// Now remove all the hash sets that have a single item -- the
// edges in such singleton sets must be correct.
unordered_set<uint64_t> to_be_deleted;