-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathvg.cpp
8038 lines (7054 loc) · 306 KB
/
vg.cpp
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
#include "vg.hpp"
#include "stream.hpp"
#include "gssw_aligner.hpp"
// We need to use ultrabubbles for dot output
#include "genotypekit.hpp"
#include "algorithms/topological_sort.hpp"
#include <raptor2/raptor2.h>
#include <stPinchGraphs.h>
#include <stack>
//#define debug
namespace vg {
using namespace std;
// construct from a stream of protobufs
VG::VG(istream& in, bool showp, bool warn_on_duplicates) {
from_istream(in, showp, warn_on_duplicates);
}
void VG::from_istream(istream& in, bool showp, bool warn_on_duplicates) {
// set up uninitialized values
init();
show_progress = showp;
// Work out how long the input file is, if applicable
size_t file_size = 0;
if (!in.good()) {
throw runtime_error("Cannot read VG graph from bad stream");
}
// Save our position
auto here = in.tellg();
// Go to the end
in.seekg(0, in.end);
// Get its position
auto there = in.tellg();
// Go back to where we were
in.seekg(here);
if (in.good()) {
// We can seek in this stream. So how far until the end?
file_size = there - here;
} else {
// It's entirely possible that none of that worked. So clear the error flags and leave the size at 0.
in.clear();
}
// Don't give an actual 0 to the progress code or it will NaN
create_progress("loading graph", file_size == 0 ? 1 : file_size);
// the graph is read in chunks, which are attached to this graph
function<void(Graph&)> lambda = [this, &in, &file_size, &warn_on_duplicates](Graph& g) {
if(in.good() && file_size != 0) {
// We want to update the progress bar from the file position.
// Note that tellg isn't thread-safe, but we're single-threaded.
update_progress(in.tellg());
if (!in.good()) {
// tellg upset something
in.clear();
}
}
// We usually expect these to not overlap in nodes or edges, so complain unless we've been told not to.
extend(g, warn_on_duplicates);
};
stream::for_each(in, lambda);
update_progress(file_size);
// Collate all the path mappings we got from all the different chunks. A
// mapping from any chunk might fall anywhere in a path (because paths may
// loop around cycles), so we need to sort on ranks.
paths.sort_by_mapping_rank();
paths.rebuild_mapping_aux();
// store paths in graph
paths.to_graph(graph);
destroy_progress();
}
// construct from an arbitrary source of Graph protobuf messages
VG::VG(function<bool(Graph&)>& get_next_graph, bool showp, bool warn_on_duplicates) {
// set up uninitialized values
init();
show_progress = showp;
// We can't show loading progress since we don't know the total number of
// subgraphs.
// Try to load the first graph
Graph subgraph;
bool got_subgraph = get_next_graph(subgraph);
while(got_subgraph) {
// If there is a valid subgraph, add it to ourselves.
// We usually expect these to not overlap in nodes or edges, so complain unless we've been told not to.
extend(subgraph, warn_on_duplicates);
// Try and load the next subgraph, if it exists.
got_subgraph = get_next_graph(subgraph);
}
// store paths in graph
paths.to_graph(graph);
}
// Construct from one giant graph
VG::VG(const Graph& from, bool showp, bool warn_on_duplicates) {
// set up uninitialized values
init();
show_progress = showp;
// Ingest the graph data
extend(from, warn_on_duplicates);
// Store paths in graph
paths.to_graph(graph);
}
handle_t VG::get_handle(const id_t& node_id, bool is_reverse) const {
// Handle is ID in low bits and orientation in high bit
// Where in the g vector do we need to be
size_t handle = node_id;
// And set the high bit if it's reverse
if (is_reverse) handle |= HIGH_BIT;
return as_handle(handle);
}
id_t VG::get_id(const handle_t& handle) const {
return as_integer(handle) & LOW_BITS;
}
bool VG::get_is_reverse(const handle_t& handle) const {
return as_integer(handle) & HIGH_BIT;
}
handle_t VG::flip(const handle_t& handle) const {
return as_handle(as_integer(handle) ^ HIGH_BIT);
}
size_t VG::get_length(const handle_t& handle) const {
// Don't get the real sequence because it might need a reverse complement calculation
auto found = node_by_id.find(get_id(handle));
if (found != node_by_id.end()) {
// We found a node. Grab its sequence length
return (*found).second->sequence().size();
} else {
throw runtime_error("No node " + to_string(get_id(handle)) + " in graph");
}
}
string VG::get_sequence(const handle_t& handle) const {
auto found = node_by_id.find(get_id(handle));
if (found != node_by_id.end()) {
// We found a node. Grab its sequence
auto sequence = (*found).second->sequence();
if (as_integer(handle) & HIGH_BIT) {
// Needs to be reverse-complemented
return reverse_complement(sequence);
} else {
return sequence;
}
} else {
throw runtime_error("No node " + to_string(get_id(handle)) + " in graph");
}
}
bool VG::follow_edges(const handle_t& handle, bool go_left, const function<bool(const handle_t&)>& iteratee) const {
// Are we reverse?
bool is_reverse = get_is_reverse(handle);
// Which edges will we look at?
auto& edge_set = (go_left != is_reverse) ? edges_on_start : edges_on_end;
// Look up edges of this node specifically
auto found = edge_set.find(get_id(handle));
if (found != edge_set.end()) {
// There are (or may be) edges
for (auto& id_and_flip : found->second) {
// For each edge destination and the flag that says if we flip orientation or not
bool new_reverse = (is_reverse != id_and_flip.second);
if (!iteratee(get_handle(id_and_flip.first, new_reverse))) {
// Iteratee said to stop
return false;
}
}
}
return true;
}
void VG::for_each_handle(const function<bool(const handle_t&)>& iteratee, bool parallel) const {
if (parallel) {
#pragma omp parallel for schedule(dynamic,1)
for (id_t i = 0; i < graph.node_size(); ++i) {
// For each node in the backing graph
// Get its ID and make a handle to it forward
// And pass it to the iteratee
if (!iteratee(get_handle(graph.node(i).id(), false))) {
// Iteratee stopped, but we can't do anything if we want to run this in parallel
//return;
}
}
} else { // same but serial
for (id_t i = 0; i < graph.node_size(); ++i) {
if (!iteratee(get_handle(graph.node(i).id(), false))) {
return;
}
}
}
}
size_t VG::node_size() const {
return graph.node_size();
}
handle_t VG::create_handle(const string& sequence) {
Node* node = create_node(sequence);
return get_handle(node->id(), false);
}
handle_t VG::create_handle(const string& sequence, const id_t& id) {
Node* node = create_node(sequence, id);
return get_handle(id, false);
}
void VG::destroy_handle(const handle_t& handle) {
destroy_node(get_id(handle));
// TODO: does destroy_node update paths?
}
void VG::create_edge(const handle_t& left, const handle_t& right) {
create_edge(get_node(get_id(left)), get_node(get_id(right)),
get_is_reverse(left), get_is_reverse(right));
}
void VG::destroy_edge(const handle_t& left, const handle_t& right) {
// Convert to NodeSides and find the edge between them
Edge* found = get_edge(NodeSide(get_id(left), !get_is_reverse(left)),
NodeSide(get_id(right), get_is_reverse(right)));
if (found != nullptr) {
// If there is one, destroy it.
destroy_edge(found);
// TODO: does destroy_edge update paths?
}
}
void VG::clear() {
graph.mutable_node()->Clear();
graph.mutable_edge()->Clear();
clear_indexes();
}
void VG::swap_handles(const handle_t& a, const handle_t& b) {
swap_nodes(get_node(get_id(a)), get_node(get_id(b)));
}
handle_t VG::apply_orientation(const handle_t& handle) {
if (!get_is_reverse(handle)) {
// Nothing to do!
return handle;
}
// Otherwise we need to reverse it
// Grab a handle to the reverse version that exists now.
handle_t rev_handle = flip(handle);
// Find all the edges (including self loops)
// We represent self loops with the (soon to be invalidated) forward and
// reverse handles to the node we're flipping.
vector<handle_t> left_nodes;
vector<handle_t> right_nodes;
follow_edges(handle, false, [&](const handle_t& other) -> void {
right_nodes.push_back(other);
return;
});
follow_edges(handle, true, [&](const handle_t& other) -> void {
left_nodes.push_back(other);
return;
});
// Remove them
for (auto& left : left_nodes) {
destroy_edge(left, handle);
}
for (auto& right : right_nodes) {
destroy_edge(handle, right);
}
// Copy the sequence from the reverse view of the node to become its locally
// forward sequence.
string new_sequence = get_sequence(handle);
// Save the ID to reuse
id_t id = get_id(handle);
// Remove the old node (without destroying the paths???)
destroy_handle(handle);
// Create a new node, re-using the ID
Node* new_node = create_node(new_sequence, id);
handle_t new_handle = get_handle(id, false);
// Connect up the new node
for (handle_t left : left_nodes) {
if (left == handle) {
// Actually go to the reverse of the new handle
left = flip(new_handle);
} else if (left == rev_handle) {
// Actually go to the new handle forward
left = new_handle;
}
create_edge(left, new_handle);
}
for (handle_t right : right_nodes) {
if (right == handle) {
// Actually go to the reverse of the new handle
right = flip(new_handle);
} else if (right == rev_handle) {
// Actually go to the new handle forward
right = new_handle;
}
create_edge(new_handle, right);
}
// TODO: Fix up the paths
return new_handle;
}
vector<handle_t> VG::divide_handle(const handle_t& handle, const vector<size_t>& offsets) {
Node* node = get_node(get_id(handle));
bool reverse = get_is_reverse(handle);
// We need to convert vector types
vector<int> int_offsets;
if (reverse) {
// We need to fill in the vector of offsets from the end of the node.
auto node_size = get_length(handle);
for (auto it = offsets.rbegin(); it != offsets.rend(); ++it) {
// Flip the order around, and also measure from the other end
int_offsets.push_back(node_size - *it);
}
} else {
// Just blit over the offsets
int_offsets = vector<int>(offsets.begin(), offsets.end());
}
// Populate this parts vector by doing the division
vector<Node*> parts;
divide_node( node, int_offsets, parts);
vector<handle_t> to_return;
for (Node* n : parts) {
// Copy the nodes into handles in their final orientation
to_return.push_back(get_handle(n->id(), reverse));
}
if (reverse) {
// And make sure they are in the right order
std::reverse(to_return.begin(), to_return.end());
}
return to_return;
}
void VG::clear_paths(void) {
paths.clear();
graph.clear_path(); // paths.clear() should do this too
sync_paths();
}
// synchronize the VG index and its backing store
void VG::sync_paths(void) {
// ensure we can navigate paths correctly
// by building paths.
paths.rebuild_mapping_aux();
}
void VG::serialize_to_ostream(ostream& out, id_t chunk_size) {
serialize_to_ostream_as_part(out, chunk_size);
stream::finish(out);
}
void VG::serialize_to_ostream_as_part(ostream& out, id_t chunk_size) {
// This makes sure mapping ranks are updated to reflect their actual
// positions along their paths.
sync_paths();
create_progress("saving graph", graph.node_size());
// Have a function to grab the chunk for the given range of nodes
function<Graph(size_t, size_t)> lambda = [this](size_t element_start, size_t element_length) -> Graph {
VG g;
map<string, map<size_t, mapping_t*> > sorted_paths;
for (size_t j = element_start;
j < element_start + element_length && j < graph.node_size();
++j) {
Node* node = graph.mutable_node(j);
// Grab the node and only the edges where it has the lower ID.
// This prevents duplication of edges in the serialized output.
nonoverlapping_node_context_without_paths(node, g);
auto& mappings = paths.get_node_mapping(node);
//cerr << "getting node mappings for " << node->id() << endl;
for (auto m : mappings) {
auto& name = paths.get_path_name(m.first);
auto& mappings = m.second;
for (auto& mapping : mappings) {
//cerr << "mapping " << name << pb2json(*mapping) << endl;
sorted_paths[name][mapping->rank] = mapping;
}
}
}
// now get the paths for this chunk so that they are ordered correctly
for (auto& p : sorted_paths) {
auto& name = p.first;
auto& path = p.second;
// now sorted in ascending order by rank
// May not be contiguous because other chunks may contain nodes between the nodes in this one
for (auto& m : path) {
g.paths.append_mapping(name, m.second->to_mapping());
}
}
if (element_start == 0) {
// The first chunk will always include all the 0-length paths.
// TODO: if there are too many, this chunk may grow too large!
paths.for_each_name([&](const string& name) {
// For every path
if (paths.get_path(name).empty()) {
// If its mapping list has no mappings, make it in the chunk
g.paths.create_path(name);
}
});
}
// record our circular paths
g.paths.circular = this->paths.circular;
// TODO: but this is broken as our paths have been reordered as
// the nodes they cross are stored in graph.nodes
g.paths.to_graph(g.graph);
update_progress(element_start);
return g.graph;
};
// Write all the dynamically sized chunks, starting with our selected chunk
// size as a guess.
stream::write(out, graph.node_size(), chunk_size, lambda);
destroy_progress();
}
void VG::serialize_to_file(const string& file_name, id_t chunk_size) {
ofstream f(file_name);
serialize_to_ostream(f);
f.close();
}
VG::~VG(void) {
//destroy_alignable_graph();
}
VG::VG(void) {
init();
}
void VG::init(void) {
current_id = 1;
show_progress = false;
}
VG::VG(set<Node*>& nodes, set<Edge*>& edges) {
init();
add_nodes(nodes);
add_edges(edges);
algorithms::sort(this);
}
id_t VG::get_node_at_nucleotide(string pathname, int nuc){
Path p = paths.path(pathname);
int nt_start = 0;
int nt_end = 0;
for (int i = 0; i < p.mapping_size(); i++){
Mapping m = p.mapping(i);
Position pos = m.position();
id_t n_id = pos.node_id();
Node* node = get_node(n_id);
nt_end += node->sequence().length();
if (nuc < nt_end && nuc >= nt_start){
return n_id;
}
nt_start += node->sequence().length();
if (nt_start > nuc && nt_end > nuc){
throw std::out_of_range("Nucleotide position not found in path.");
}
}
throw std::out_of_range("Nucleotide position not found in path.");
}
void VG::add_nodes(const set<Node*>& nodes) {
for (auto node : nodes) {
add_node(*node);
}
}
void VG::add_edges(const set<Edge*>& edges) {
for (auto edge : edges) {
add_edge(*edge);
}
}
void VG::add_edges(const vector<Edge*>& edges) {
for (auto edge : edges) {
add_edge(*edge);
}
}
void VG::add_nodes(const vector<Node>& nodes) {
for (auto& node : nodes) {
add_node(node);
}
}
void VG::add_edges(const vector<Edge>& edges) {
for (auto& edge : edges) {
add_edge(edge);
}
}
void VG::add_node(const Node& node) {
if (!has_node(node)) {
Node* new_node = graph.add_node(); // add it to the graph
*new_node = node; // overwrite it with the value of the given node
node_by_id[new_node->id()] = new_node; // and insert into our id lookup table
node_index[new_node] = graph.node_size()-1;
}
}
void VG::add_edge(const Edge& edge) {
if (!has_edge(edge)) {
Edge* new_edge = graph.add_edge(); // add it to the graph
*new_edge = edge;
set_edge(new_edge);
edge_index[new_edge] = graph.edge_size()-1;
}
}
void VG::circularize(id_t head, id_t tail) {
Edge* e = create_edge(tail, head);
add_edge(*e);
}
void VG::circularize(vector<string> pathnames){
for(auto p : pathnames){
Path curr_path = paths.path(p);
Position start_pos = path_start(curr_path);
Position end_pos = path_end(curr_path);
id_t head = start_pos.node_id();
id_t tail = end_pos.node_id();
if (start_pos.offset() != 0){
//VG::divide_node(Node* node, int pos, Node*& left, Node*& right)
Node* left; Node* right;
Node* head_node = get_node(head);
divide_node(head_node, start_pos.offset(), left, right);
head = left->id();
paths.compact_ranks();
}
if (start_pos.offset() != 0){
Node* left; Node* right;
Node* tail_node = get_node(tail);
divide_node(tail_node, end_pos.offset(), left, right);
tail = right->id();
paths.compact_ranks();
}
Edge* e = create_edge(tail, head, false, false);
add_edge(*e);
// record a flag in the path object to indicate that it is circular
paths.make_circular(p);
}
}
size_t VG::node_count(void) const {
return graph.node_size();
}
size_t VG::edge_count(void) const {
return graph.edge_size();
}
vector<pair<id_t, bool>>& VG::edges_start(Node* node) {
if(node == nullptr) {
return empty_edge_ends;
}
return edges_start(node->id());
}
vector<pair<id_t, bool>>& VG::edges_start(id_t id) {
if(edges_on_start.count(id) == 0) {
return empty_edge_ends;
}
return edges_on_start[id];
}
vector<pair<id_t, bool>>& VG::edges_end(Node* node) {
if(node == nullptr) {
return empty_edge_ends;
}
return edges_end(node->id());
}
vector<pair<id_t, bool>>& VG::edges_end(id_t id) {
if(edges_on_end.count(id) == 0) {
return empty_edge_ends;
}
return edges_on_end[id];
}
int VG::start_degree(Node* node) {
return edges_start(node).size();
}
int VG::end_degree(Node* node) {
return edges_end(node).size();
}
int VG::left_degree(NodeTraversal node) {
// If we're backward, the end is on the left. Otherwise, the start is.
return node.backward ? end_degree(node.node) : start_degree(node.node);
}
int VG::right_degree(NodeTraversal node) {
// If we're backward, the start is on the right. Otherwise, the end is.
return node.backward ? start_degree(node.node) : end_degree(node.node);
}
void VG::edges_of_node(Node* node, vector<Edge*>& edges) {
for(pair<id_t, bool>& off_start : edges_start(node)) {
// Go through the edges on this node's start
Edge* edge = edge_by_sides[NodeSide::pair_from_start_edge(node->id(), off_start)];
if (!edge) {
cerr << "error:[VG::edges_of_node] nonexistent start edge " << off_start.first << " start <-> "
<< node->id() << (off_start.second ? " start" : " end") << endl;
exit(1);
}
edges.push_back(edge);
}
for(pair<id_t, bool>& off_end : edges_end(node)) {
// And on its end
Edge* edge = edge_by_sides[NodeSide::pair_from_end_edge(node->id(), off_end)];
if (!edge) {
cerr << "error:[VG::edges_of_node] nonexistent end edge " << off_end.first << " end <-> "
<< node->id() << (off_end.second ? " end" : " start") << endl;
exit(1);
}
if(edge->from() == edge->to() && edge->from_start() == edge->to_end()) {
// This edge touches both our start and our end, so we already
// handled it on our start. Don't produce it twice.
continue;
}
edges.push_back(edge);
}
}
vector<Edge*> VG::edges_from(Node* node) {
vector<Edge*> from;
for (auto e : edges_of(node)) {
if (e->from() == node->id()) {
from.push_back(e);
}
}
return from;
}
vector<Edge*> VG::edges_to(Node* node) {
vector<Edge*> to;
for (auto e : edges_of(node)) {
if (e->to() == node->id()) {
to.push_back(e);
}
}
return to;
}
vector<Edge*> VG::edges_of(Node* node) {
vector<Edge*> edges;
edges_of_node(node, edges);
return edges;
}
void VG::edges_of_nodes(set<Node*>& nodes, set<Edge*>& edges) {
for (set<Node*>::iterator n = nodes.begin(); n != nodes.end(); ++n) {
vector<Edge*> ev;
edges_of_node(*n, ev);
for (vector<Edge*>::iterator e = ev.begin(); e != ev.end(); ++e) {
edges.insert(*e);
}
}
}
set<pair<NodeSide, bool>> VG::sides_context(id_t node_id) {
// return the side we're going to and if we go from the start or end to get there
set<pair<NodeSide, bool>> all;
for (auto& s : sides_to(NodeSide(node_id, false))) {
all.insert(make_pair(s, false));
}
for (auto& s : sides_to(NodeSide(node_id, true))) {
all.insert(make_pair(s, true));
}
for (auto& s : sides_from(NodeSide(node_id, false))) {
all.insert(make_pair(s, false));
}
for (auto& s : sides_from(NodeSide(node_id, true))) {
all.insert(make_pair(s, true));
}
return all;
}
bool VG::same_context(id_t n1, id_t n2) {
auto c1 = sides_context(n1);
auto c2 = sides_context(n2);
bool same = true;
for (auto& s : c1) {
if (!c2.count(s)) { same = false; break; }
}
return same;
}
bool VG::is_ancestor_prev(id_t node_id, id_t candidate_id) {
set<id_t> seen;
return is_ancestor_prev(node_id, candidate_id, seen);
}
bool VG::is_ancestor_prev(id_t node_id, id_t candidate_id, set<id_t>& seen, size_t steps) {
if (node_id == candidate_id) return true;
if (!steps) return false;
for (auto& side : sides_to(NodeSide(node_id, false))) {
if (seen.count(side.node)) continue;
seen.insert(side.node);
if (is_ancestor_prev(side.node, candidate_id, seen, steps-1)) return true;
}
return false;
}
bool VG::is_ancestor_next(id_t node_id, id_t candidate_id) {
set<id_t> seen;
return is_ancestor_next(node_id, candidate_id, seen);
}
bool VG::is_ancestor_next(id_t node_id, id_t candidate_id, set<id_t>& seen, size_t steps) {
if (node_id == candidate_id) return true;
if (!steps) return false;
for (auto& side : sides_from(NodeSide(node_id, true))) {
if (seen.count(side.node)) continue;
seen.insert(side.node);
if (is_ancestor_next(side.node, candidate_id, seen, steps-1)) return true;
}
return false;
}
id_t VG::common_ancestor_prev(id_t id1, id_t id2, size_t steps) {
// arbitrarily step back from node 1 asking if we are prev-ancestral to node 2
auto scan = [this](id_t id1, id_t id2, size_t steps) -> id_t {
set<id_t> to_visit;
to_visit.insert(id1);
for (size_t i = 0; i < steps; ++i) {
// collect nodes to visit
set<id_t> to_visit_next;
for (auto& id : to_visit) {
if (is_ancestor_prev(id2, id)) return id;
for (auto& side : sides_to(NodeSide(id, false))) {
to_visit_next.insert(side.node);
}
}
to_visit = to_visit_next;
if (to_visit.empty()) return -1; // we hit the end of the graph
}
return 0;
};
id_t id3 = scan(id1, id2, steps);
if (id3) {
return id3;
} else {
return scan(id2, id1, steps);
}
}
id_t VG::common_ancestor_next(id_t id1, id_t id2, size_t steps) {
// arbitrarily step forward from node 1 asking if we are next-ancestral to node 2
auto scan = [this](id_t id1, id_t id2, size_t steps) -> id_t {
set<id_t> to_visit;
to_visit.insert(id1);
for (size_t i = 0; i < steps; ++i) {
// collect nodes to visit
set<id_t> to_visit_next;
for (auto& id : to_visit) {
if (is_ancestor_next(id2, id)) return id;
for (auto& side : sides_from(NodeSide(id, true))) {
to_visit_next.insert(side.node);
}
}
to_visit = to_visit_next;
if (to_visit.empty()) return -1; // we hit the end of the graph
}
return 0;
};
id_t id3 = scan(id1, id2, steps);
if (id3) {
return id3;
} else {
return scan(id2, id1, steps);
}
}
set<NodeSide> VG::sides_of(NodeSide side) {
set<NodeSide> v1 = sides_to(side);
set<NodeSide> v2 = sides_from(side);
for (auto s : v2) v1.insert(s);
return v1;
}
set<NodeSide> VG::sides_to(NodeSide side) {
set<NodeSide> other_sides;
vector<Edge*> edges;
edges_of_node(get_node(side.node), edges);
for (auto* edge : edges) {
if (edge->to() == side.node && edge->to_end() == side.is_end) {
other_sides.insert(NodeSide(edge->from(), !edge->from_start()));
}
}
return other_sides;
}
set<NodeSide> VG::sides_from(NodeSide side) {
set<NodeSide> other_sides;
vector<Edge*> edges;
edges_of_node(get_node(side.node), edges);
for (auto* edge : edges) {
if (edge->from() == side.node && edge->from_start() != side.is_end) {
other_sides.insert(NodeSide(edge->to(), edge->to_end()));
}
}
return other_sides;
}
set<NodeSide> VG::sides_from(id_t id) {
set<NodeSide> sides;
for (auto side : sides_from(NodeSide(id, true))) {
sides.insert(side);
}
for (auto side : sides_from(NodeSide(id, false))) {
sides.insert(side);
}
return sides;
}
set<NodeSide> VG::sides_to(id_t id) {
set<NodeSide> sides;
for (auto side : sides_to(NodeSide(id, true))) {
sides.insert(side);
}
for (auto side : sides_to(NodeSide(id, false))) {
sides.insert(side);
}
return sides;
}
set<NodeTraversal> VG::siblings_to(const NodeTraversal& trav) {
// find the sides to
auto to_sides = sides_to(NodeSide(trav.node->id(), trav.backward));
// and then find the traversals from them
set<NodeTraversal> travs_from_to_sides;
for (auto& s1 : to_sides) {
// and the from-children of these
for (auto& s2 : sides_from(s1)) {
auto sib = NodeTraversal(get_node(s2.node), s2.is_end);
// which are not this node
if (sib != trav) {
travs_from_to_sides.insert(sib);
}
}
}
return travs_from_to_sides;
}
set<NodeTraversal> VG::siblings_from(const NodeTraversal& trav) {
// find the sides from
auto from_sides = sides_from(NodeSide(trav.node->id(), !trav.backward));
// and then find the traversals from them
set<NodeTraversal> travs_to_from_sides;
for (auto& s1 : from_sides) {
// and the to-children of these
for (auto& s2 : sides_to(s1)) {
auto sib = NodeTraversal(get_node(s2.node), !s2.is_end);
// which are not this node
if (sib != trav) {
travs_to_from_sides.insert(sib);
}
}
}
return travs_to_from_sides;
}
set<Node*> VG::siblings_of(Node* node) {
set<Node*> sibs;
for (auto& s : siblings_to(NodeTraversal(node, false))) {
sibs.insert(s.node);
}
for (auto& s : siblings_to(NodeTraversal(node, true))) {
sibs.insert(s.node);
}
for (auto& s : siblings_from(NodeTraversal(node, false))) {
sibs.insert(s.node);
}
for (auto& s : siblings_from(NodeTraversal(node, true))) {
sibs.insert(s.node);
}
return sibs;
}
set<NodeTraversal> VG::full_siblings_to(const NodeTraversal& trav) {
// get the siblings of
auto sibs_to = siblings_to(trav);
// and filter them for nodes with the same inbound sides
auto to_sides = sides_to(NodeSide(trav.node->id(), trav.backward));
set<NodeTraversal> full_sibs_to;
for (auto& sib : sibs_to) {
auto sib_to_sides = sides_to(NodeSide(sib.node->id(), sib.backward));
if (sib_to_sides == to_sides) {
full_sibs_to.insert(sib);
}
}
return full_sibs_to;
}
set<NodeTraversal> VG::full_siblings_from(const NodeTraversal& trav) {
// get the siblings of
auto sibs_from = siblings_from(trav);
// and filter them for nodes with the same outbound sides
auto from_sides = sides_from(NodeSide(trav.node->id(), !trav.backward));
set<NodeTraversal> full_sibs_from;
for (auto& sib : sibs_from) {
auto sib_from_sides = sides_from(NodeSide(sib.node->id(), !sib.backward));
if (sib_from_sides == from_sides) {
full_sibs_from.insert(sib);
}
}
return full_sibs_from;
}
// returns sets of sibling nodes that are only in one set of sibling nodes
set<set<NodeTraversal>> VG::transitive_sibling_sets(const set<set<NodeTraversal>>& sibs) {
set<set<NodeTraversal>> trans_sibs;
map<Node*, int> membership;
// determine the number of sibling sets that each node is in
for (auto& s : sibs) {
for (auto& t : s) {
if (membership.find(t.node) == membership.end()) {
membership[t.node] = 1;
} else {
++membership[t.node];
}
}
}
// now exclude components which are intransitive
// by only keeping those sib sets whose members are in only one set
for (auto& s : sibs) {
// all members must only appear in this set
bool is_transitive = true;
for (auto& t : s) {
if (membership[t.node] > 1) {