-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvg.cpp
6173 lines (5403 loc) · 238 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"
namespace vg {
using namespace std;
// construct from a stream of protobufs
VG::VG(istream& in, bool showp) {
// set up uninitialized values
init();
show_progress = showp;
// and if we should show progress
function<void(uint64_t)> handle_count = [this](uint64_t count) {
create_progress("loading graph", count);
};
// the graph is read in chunks, which are attached to this graph
uint64_t i = 0;
function<void(Graph&)> lambda = [this, &i](Graph& g) {
update_progress(++i);
// We expect these to not overlap in nodes or edges, so complain if they do.
extend(g, true);
};
stream::for_each(in, lambda, handle_count);
// 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) {
// 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 expect these to not overlap in nodes or edges, so complain if they do.
extend(subgraph, true);
// Try and load the next subgraph, if it exists.
got_subgraph = get_next_graph(subgraph);
}
// store paths in graph
paths.to_graph(graph);
}
void VG::serialize_to_ostream(ostream& out, int64_t chunk_size) {
// ensure we can navigate paths correctly
// by building paths.mapping_path_order
paths.rebuild_mapping_aux();
// save the number of the messages to be serialized into the output file
int64_t count = graph.node_size() / chunk_size + 1;
create_progress("saving graph", count);
// partition the graph into a number of chunks (required by format)
// constructing subgraphs and writing them to the stream
function<Graph(uint64_t)> lambda =
[this, chunk_size](uint64_t i) -> Graph {
VG g;
map<string, map<size_t, Mapping*> > sorted_paths;
for (int64_t j = i * chunk_size;
j < (i+1)*chunk_size && 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);
//set<pair<string, Mapping*> >& Paths::get_node_mapping(int64_t id);
auto& mappings = paths.get_node_mapping(node);
//cerr << "getting node mappings for " << node->id() << endl;
for (auto m : mappings) {
auto& name = m.first;
auto mapping = m.second;
//cerr << "mapping " << name << pb2json(*mapping) << endl;
sorted_paths[name][paths.mapping_path_order[mapping]] = 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
// we could also assert that we have a contiguous path here
for (auto& m : path) {
g.paths.append_mapping(name, *m.second);
}
}
// 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(i);
return g.graph;
};
stream::write(out, count, lambda);
destroy_progress();
}
void VG::serialize_to_file(const string& file_name, int64_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) {
gssw_aligner = NULL;
current_id = 1;
show_progress = false;
progress_message = "progress";
progress = NULL;
}
VG::VG(set<Node*>& nodes, set<Edge*>& edges) {
init();
add_nodes(nodes);
add_edges(edges);
sort();
}
// check for conflict (duplicate nodes and edges) occurs within add_* functions
void VG::add_nodes(set<Node*>& nodes) {
for (auto node : nodes) {
add_node(*node);
}
}
void VG::add_edges(set<Edge*>& edges) {
for (auto edge : edges) {
add_edge(*edge);
}
}
void VG::add_nodes(vector<Node>& nodes) {
for (auto& node : nodes) {
add_node(node);
}
}
void VG::add_edges(vector<Edge>& edges) {
for (auto& edge : edges) {
add_edge(edge);
}
}
void VG::add_node(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(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;
}
}
int64_t VG::node_count(void) {
return graph.node_size();
}
int64_t VG::edge_count(void) {
return graph.edge_size();
}
vector<pair<int64_t, bool>>& VG::edges_start(Node* node) {
if(node == nullptr) {
return empty_edge_ends;
}
return edges_start(node->id());
}
vector<pair<int64_t, bool>>& VG::edges_start(int64_t id) {
if(edges_on_start.count(id) == 0) {
return empty_edge_ends;
}
return edges_on_start[id];
}
vector<pair<int64_t, bool>>& VG::edges_end(Node* node) {
if(node == nullptr) {
return empty_edge_ends;
}
return edges_end(node->id());
}
vector<pair<int64_t, bool>>& VG::edges_end(int64_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<int64_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<int64_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 out start and our end, so we already
// handled it on our start. Don't produce it twice.
continue;
}
edges.push_back(edge);
}
}
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);
}
}
}
bool VG::is_ancestor_prev(int64_t node_id, int64_t candidate_id, size_t steps) {
if (node_id == candidate_id) return true;
if (!steps) return false;
for (auto& side : sides_to(NodeSide(node_id, false))) {
if (is_ancestor_prev(side.node, candidate_id, steps-1)) return true;
}
return false;
}
bool VG::is_ancestor_next(int64_t node_id, int64_t candidate_id, size_t steps) {
if (node_id == candidate_id) return true;
if (!steps) return false;
for (auto& side : sides_from(NodeSide(node_id, true))) {
if (is_ancestor_next(side.node, candidate_id, steps-1)) return true;
}
return false;
}
int64_t VG::common_ancestor_prev(int64_t id1, int64_t id2, size_t steps) {
// arbitrarily step back from node 1 asking if we are prev-ancestral to node 2
auto scan = [this](int64_t id1, int64_t id2, size_t steps) -> int64_t {
set<int64_t> to_visit;
to_visit.insert(id1);
for (size_t i = 0; i < steps; ++i) {
// collect nodes to visit
set<int64_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;
};
int64_t id3 = scan(id1, id2, steps);
if (id3) {
return id3;
} else {
return scan(id2, id1, steps);
}
}
int64_t VG::common_ancestor_next(int64_t id1, int64_t id2, size_t steps) {
// arbitrarily step forward from node 1 asking if we are next-ancestral to node 2
auto scan = [this](int64_t id1, int64_t id2, size_t steps) -> int64_t {
set<int64_t> to_visit;
to_visit.insert(id1);
for (size_t i = 0; i < steps; ++i) {
// collect nodes to visit
set<int64_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;
};
int64_t id3 = scan(id1, id2, steps);
if (id3) {
return id3;
} else {
return scan(id2, id1, steps);
}
}
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<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<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 inbound 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;
}
void VG::simplify_siblings(void) {
// make a list of all the sets of full siblings
set<set<NodeTraversal>> to_sibs;
set<set<NodeTraversal>> from_sibs;
for_each_node([this, &to_sibs, &from_sibs](Node* n) {
auto trav = NodeTraversal(n, false);
auto tsibs = full_siblings_to(trav);
tsibs.insert(trav);
if (tsibs.size() > 1) {
to_sibs.insert(tsibs);
}
auto fsibs = full_siblings_from(trav);
fsibs.insert(trav);
if (fsibs.size() > 1) {
from_sibs.insert(fsibs);
}
});
// for each sibling group, try to simplify it
// first do the perfect to-sibs
simplify_to_siblings(to_sibs);
// then the from direction
simplify_from_siblings(from_sibs);
// and remove any null nodes that result
remove_null_nodes_forwarding_edges();
}
void VG::simplify_to_siblings(const set<set<NodeTraversal>>& to_sibs) {
for (auto& sibs : to_sibs) {
// determine the amount of sharing at the start
// the to-sibs have the same parent(s) feeding into them
// so we can safely make a single node out of the shared sequence
// and link this to them and their parent to remove node level redundancy
vector<string*> seqs;
size_t min_seq_size = sibs.begin()->node->sequence().size();
for (auto& sib : sibs) {
auto seqp = sib.node->mutable_sequence();
seqs.push_back(seqp);
if (seqp->size() < min_seq_size) {
min_seq_size = seqp->size();
}
}
size_t i = 0;
size_t j = 0;
bool similar = true;
for ( ; similar && i < min_seq_size; ++i) {
//cerr << i << endl;
char c = seqs.front()->at(i);
for (auto s : seqs) {
//cerr << "checking " << c << " vs " << s->at(i) << endl;
if (c != s->at(i)) {
similar = false;
break;
}
}
if (!similar) break;
++j;
}
size_t shared_start = j;
//cerr << "sharing is " << shared_start << " for to-sibs of "
// << sibs.begin()->node->id() << endl;
if (shared_start == 0) continue;
bool self_ancestors = false;
bool common_ancestor = true;
for (auto& sib1 : sibs) {
for (auto& sib2 : sibs) {
if (sib1 != sib2) {
if (is_ancestor_next(sib1.node->id(), sib2.node->id())) {
self_ancestors = true;
}
if (!common_ancestor_next(sib1.node->id(), sib2.node->id())) {
common_ancestor = false;
}
}
}
}
if (self_ancestors || !common_ancestor) continue;
// make a new node with the shared sequence
string seq = seqs.front()->substr(0,shared_start);
auto new_node = create_node(seq);
// chop it off of the old nodes
for (auto& sib : sibs) {
*sib.node->mutable_sequence() = sib.node->sequence().substr(shared_start);
}
// connect the new node to the common parents
// by definition we are only working with nodes that have exactly the same set of parents
// so we just use the first node in the set to drive the reconnection
auto new_left_side = NodeSide(new_node->id(), false);
auto new_right_side = NodeSide(new_node->id(), true);
for (auto side : sides_to(NodeSide(sibs.begin()->node->id(), sibs.begin()->backward))) {
create_edge(side, new_left_side);
}
// disconnect the old nodes from their common parents
for (auto& sib : sibs) {
auto old_side = NodeSide(sib.node->id(), sib.backward);
for (auto side : sides_to(old_side)) {
destroy_edge(side, old_side);
}
// connect the new node to the old nodes
create_edge(new_right_side, old_side);
}
}
}
void VG::simplify_from_siblings(const set<set<NodeTraversal>>& from_sibs) {
for (auto& sibs : from_sibs) {
// determine the amount of sharing at the end
// the from-sibs have the same downstream nodes ("parents")
// so we can safely make a single node out of the shared sequence at the end
// and link this to them and their parent to remove node level redundancy
vector<string*> seqs;
size_t min_seq_size = sibs.begin()->node->sequence().size();
for (auto& sib : sibs) {
auto seqp = sib.node->mutable_sequence();
seqs.push_back(seqp);
if (seqp->size() < min_seq_size) {
min_seq_size = seqp->size();
}
}
size_t i = 0;
size_t j = 0;
bool similar = true;
for ( ; similar && i < min_seq_size; ++i) {
char c = seqs.front()->at(seqs.front()->size()-(i+1));
for (auto s : seqs) {
if (c != s->at(s->size()-(i+1))) {
similar = false;
break;
}
}
if (!similar) break;
++j;
}
size_t shared_end = j;
//cerr << "sharing is " << shared_end << " for from-sibs of "
// << sibs.begin()->node->id() << endl;
if (shared_end == 0) continue;
// we will only use this normalization method if:
// 1) none of the nodes is an ancestor of any of the others
// 2) we can identify common ancestors within a given number of steps in the graph
// thus verifying that our determination of ancestorship (or not) is approximately sound
// ... yes this can also fail, but at worst we add cycles into the graph
// check if any of the nodes is an ancester of the others
bool self_ancestors = false;
bool common_ancestor = true;
for (auto& sib1 : sibs) {
for (auto& sib2 : sibs) {
if (sib1 != sib2) {
if (is_ancestor_prev(sib1.node->id(), sib2.node->id())) {
self_ancestors = true;
}
if (!common_ancestor_prev(sib1.node->id(), sib2.node->id())) {
common_ancestor = false;
}
}
}
}
if (self_ancestors || !common_ancestor) continue;
// make a new node with the shared sequence
string seq = seqs.front()->substr(seqs.front()->size()-shared_end);
auto new_node = create_node(seq);
// chop it off of the old nodes
for (auto& sib : sibs) {
*sib.node->mutable_sequence()
= sib.node->sequence().substr(0, sib.node->sequence().size()-shared_end);
}
// connect the new node to the common downstream nodes
// by definition we are only working with nodes that have exactly the same set of "children"
// so we just use the first node in the set to drive the reconnection
auto new_left_side = NodeSide(new_node->id(), false);
auto new_right_side = NodeSide(new_node->id(), true);
for (auto side : sides_from(NodeSide(sibs.begin()->node->id(), !sibs.begin()->backward))) {
create_edge(new_right_side, side);
}
// disconnect the old nodes from their common "children"
for (auto& sib : sibs) {
auto old_side = NodeSide(sib.node->id(), !sib.backward);
for (auto side : sides_from(old_side)) {
destroy_edge(old_side, side);
}
// connect the new node to the old nodes
create_edge(old_side, new_left_side);
}
}
}
bool VG::adjacent(const Position& pos1, const Position& pos2) {
// two positions are on the same node
if (pos1.node_id() == pos2.node_id()) {
if (pos1.offset() == pos1.offset()+1) {
// and have adjacent offsets
return true;
} else {
// if not, they aren't adjacent
return false;
}
} else {
// is the first at the end of its node
// and the second at the start of its node
// determine if the two nodes are connected
auto* node1 = get_node(pos1.node_id());
auto* node2 = get_node(pos2.node_id());
if (pos1.offset() == node1->sequence().size()-1
&& pos2.offset() == 0) {
// these are adjacent iff we have an edge
return has_edge(NodeSide(pos1.node_id(), true),
NodeSide(pos2.node_id(), false));
} else {
// the offsets aren't at the end and start
// so these positions can't be adjacent
return false;
}
}
}
void VG::unchop(void) {
for (auto& comp : simple_components()) {
merge_nodes(comp);
}
}
void VG::normalize(void) {
// combine diced/chopped nodes (subpaths with no branching)
unchop();
// merge redundancy across multiple nodes into single nodes
simplify_siblings();
// there may now be some cut nodes that can be simplified
unchop();
}
void VG::remove_non_path(void) {
set<Edge*> path_edges;
function<void(Path&)> lambda = [this, &path_edges](Path& path) {
for (int i = 0; i < path.mapping_size(); ++i) {
const Mapping& m1 = path.mapping(i);
if (i < path.mapping_size()-1) {
const Mapping& m2 = path.mapping(i+1);
// Find the Edge connecting the mappings in the order they occur in the path.
Edge* edge = get_edge(NodeTraversal(get_node(m1.position().node_id()),
m1.is_reverse()),
NodeTraversal(get_node(m2.position().node_id()),
m2.is_reverse()));
path_edges.insert(edge);
}
}
};
paths.for_each(lambda);
// now determine which edges aren't used
set<Edge*> non_path_edges;
for_each_edge([this, &path_edges, &non_path_edges](Edge* e) {
if (!path_edges.count(e)) {
non_path_edges.insert(e);
}
});
// and destroy them
for (auto* e : non_path_edges) {
destroy_edge(e);
}
set<int64_t> non_path_nodes;
for_each_node([this, &non_path_nodes](Node* n) {
if (!paths.has_node_mapping(n->id())) {
non_path_nodes.insert(n->id());
}
});
for (auto id : non_path_nodes) {
destroy_node(id);
}
// re-compact ids if we have made changes to the graph
if (!non_path_nodes.empty()) {
sort();
compact_ids();
}
}
// the set of components that could be merged into single nodes without
// changing the path space of the graph
set<list<Node*>> VG::simple_components(void) {
// go around and establish groupings
set<list<Node*>> components;
for_each_node([this, &components](Node* n) {
// go left and right through each as far as we have only single edges connecting us
// to nodes that have only single edges coming in or out
// and these edges are "normal" in that they go from the tail to the head
list<Node*> c;
// go left
{
Node* l = n;
auto sides = sides_to(NodeSide(l->id(), false));
while (sides.size() == 1
&& end_degree(get_node(sides.begin()->node)) == 1
&& sides.begin()->is_end) {
l = get_node(sides.begin()->node);
sides = sides_to(NodeSide(l->id(), false));
c.push_front(l);
}
}
// add the node (in the middle)
c.push_back(n);
// go right
{
Node* r = n;
auto sides = sides_from(NodeSide(r->id(), true));
while (sides.size() == 1
&& start_degree(get_node(sides.begin()->node)) == 1
&& !sides.begin()->is_end) {
r = get_node(sides.begin()->node);
sides = sides_from(NodeSide(r->id(), true));
c.push_back(r);
}
}
if (c.size() > 0) {
components.insert(c);
}
});
/*
for (auto& c : components) {
for (auto x : c) {
cerr << x->id() << " ";
}
cerr << endl;
}
*/
return components;
}
void VG::merge_nodes(const list<Node*>& nodes) {
// determine the common paths that will apply to the new node
// TODO XXX (paths)
// make a new node that concatenates the labels in the order they occur in the graph
string seq;
for (auto n : nodes) {
seq += n->sequence();
}
auto node = create_node(seq);
// connect this node to the left and right connections of the set
auto old_start = NodeSide(nodes.front()->id(), false);
auto new_start = NodeSide(node->id(), false);
for (auto side : sides_to(old_start)) {
create_edge(side, new_start);
}
auto old_end = NodeSide(nodes.back()->id(), true);
auto new_end = NodeSide(node->id(), true);
for (auto side : sides_from(old_end)) {
create_edge(new_end, side);
}
// remove the old nodes
for (auto n : nodes) {
destroy_node(n);
}
}
int64_t VG::total_length_of_nodes(void) {
int64_t length = 0;
for (int64_t i = 0; i < graph.node_size(); ++i) {
Node* n = graph.mutable_node(i);
length += n->sequence().size();
}
return length;
}
void VG::build_indexes(void) {
for (int64_t i = 0; i < graph.node_size(); ++i) {
Node* n = graph.mutable_node(i);
node_index[n] = i;
node_by_id[n->id()] = n;
}
for (int64_t i = 0; i < graph.edge_size(); ++i) {
Edge* e = graph.mutable_edge(i);
edge_index[e] = i;
set_edge(e);
}
}
void VG::clear_indexes(void) {
node_index.clear();
node_by_id.clear();
edge_by_sides.clear();
edge_index.clear();
edges_on_start.clear();
edges_on_end.clear();
}
void VG::clear_indexes_no_resize(void) {
#ifdef USE_DENSE_HASH
node_index.clear_no_resize();
node_by_id.clear_no_resize();
edge_by_sides.clear_no_resize();
edge_index.clear_no_resize();
edges_on_start.clear_no_resize();
edges_on_end.clear_no_resize();
#else
clear_indexes();
#endif
}
void VG::resize_indexes(void) {
node_index.resize(graph.node_size());
node_by_id.resize(graph.node_size());
edge_by_sides.resize(graph.edge_size());
edge_index.resize(graph.edge_size());
edges_on_start.resize(graph.edge_size());
edges_on_end.resize(graph.edge_size());
}
void VG::rebuild_indexes(void) {
//clear_indexes();
//resize_indexes();
clear_indexes_no_resize();
build_indexes();
paths.rebuild_node_mapping();
}
bool VG::empty(void) {
return graph.node_size() == 0 && graph.edge_size() == 0;
}
bool VG::has_node(Node* node) {
return node && has_node(node->id());
}
bool VG::has_node(Node& node) {
return has_node(node.id());
}
bool VG::has_node(int64_t id) {
return node_by_id.find(id) != node_by_id.end();
}
bool VG::has_edge(Edge* edge) {
return edge && has_edge(*edge);
}
bool VG::has_edge(Edge& edge) {
return edge_by_sides.find(NodeSide::pair_from_edge(edge)) != edge_by_sides.end();
}
bool VG::has_edge(const NodeSide& side1, const NodeSide& side2) {
return edge_by_sides.find(minmax(side1, side2)) != edge_by_sides.end();
}
bool VG::has_edge(const pair<NodeSide, NodeSide>& sides) {
return has_edge(sides.first, sides.second);
}
// remove duplicated nodes and edges that would occur if we merged the graphs
void VG::remove_duplicated_in(VG& g) {
vector<Node*> nodes_to_destroy;
for (int64_t i = 0; i < graph.node_size(); ++i) {
Node* n = graph.mutable_node(i);
if (g.has_node(n)) {
nodes_to_destroy.push_back(n);
}
}
vector<Edge*> edges_to_destroy;
for (int64_t i = 0; i < graph.edge_size(); ++i) {
Edge* e = graph.mutable_edge(i);
if (g.has_edge(e)) {
edges_to_destroy.push_back(e);
}
}
for (vector<Node*>::iterator n = nodes_to_destroy.begin();
n != nodes_to_destroy.end(); ++n) {
g.destroy_node(g.get_node((*n)->id()));
}
for (vector<Edge*>::iterator e = edges_to_destroy.begin();
e != edges_to_destroy.end(); ++e) {
// Find and destroy the edge that does the same thing in g.
g.destroy_edge(g.get_edge(NodeSide::pair_from_edge(*e)));
}
}
void VG::merge_union(VG& g) {
// remove duplicates, then merge
remove_duplicated_in(g);
if (g.graph.node_size() > 0) {
merge(g.graph);
}
}
void VG::merge(VG& g) {
merge(g.graph);
}
// this merges without any validity checks
// this could be rather expensive if the graphs to merge are largely overlapping
void VG::merge(Graph& g) {
graph.mutable_node()->MergeFrom(g.node());
graph.mutable_edge()->MergeFrom(g.edge());
rebuild_indexes();
}
// iterates over nodes and edges, adding them in when they don't already exist
void VG::extend(VG& g, bool warn_on_duplicates) {
for (int64_t i = 0; i < g.graph.node_size(); ++i) {
Node* n = g.graph.mutable_node(i);
if(n->id() == 0) {
cerr << "[vg] warning: node ID 0 is not allowed. Skipping." << endl;
} else if (!has_node(n)) {
add_node(*n);
} else if(warn_on_duplicates) {
cerr << "[vg] warning: node ID " << n->id() << " appears multiple times. Skipping." << endl;
}
}
for (int64_t i = 0; i < g.graph.edge_size(); ++i) {
Edge* e = g.graph.mutable_edge(i);
if (!has_edge(e)) {
add_edge(*e);
} else if(warn_on_duplicates) {
cerr << "[vg] warning: edge " << e->from() << (e->from_start() ? " start" : " end") << " <-> "
<< e->to() << (e->to_end() ? " end" : " start") << " appears multiple times. Skipping." << endl;
}
}
paths.append(g.paths);
}
// TODO: unify with above. The only difference is what's done with the paths.
void VG::extend(Graph& graph, bool warn_on_duplicates) {
for (int64_t i = 0; i < graph.node_size(); ++i) {
Node* n = graph.mutable_node(i);
if(n->id() == 0) {
cerr << "[vg] warning: node ID 0 is not allowed. Skipping." << endl;
} else if (!has_node(n)) {
add_node(*n);
} else if(warn_on_duplicates) {
cerr << "[vg] warning: node ID " << n->id() << " appears multiple times. Skipping." << endl;
}
}
for (int64_t i = 0; i < graph.edge_size(); ++i) {
Edge* e = graph.mutable_edge(i);
if (!has_edge(e)) {
add_edge(*e);
} else if(warn_on_duplicates) {
cerr << "[vg] warning: edge " << e->from() << (e->from_start() ? " start" : " end") << " <-> "
<< e->to() << (e->to_end() ? " end" : " start") << " appears multiple times. Skipping." << endl;
}
}
paths.append(graph);
}
// extend this graph by g, connecting the tails of this graph to the heads of the other
// the ids of the second graph are modified for compact representation
void VG::append(VG& g) {
// compact and increment the ids of g out of range of this graph
//g.compact_ids();
// assume we've already compacted the other, or that id compaction doesn't matter
// just get out of the way
g.increment_node_ids(max_node_id());
// get the heads of the other graph, now that we've compacted the ids