-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequenceGraph.cpp
1464 lines (1367 loc) · 44.7 KB
/
sequenceGraph.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
/**
Copyright (C) 2013 INRA-URGI
This file is part of TEDNA, a short reads transposable elements assembler
TEDNA is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
GNU Affero General Public License for more details.
See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program.
**/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "globals.hpp"
#include "sequenceGraph.hpp"
#include <thread>
#include <stack>
#include <map>
SequenceNode::SequenceNode (unsigned int id, KmerNb count): _id(id), _count(count), _set(true), _marked(false), _direct(true) {}
SequenceNode::SequenceNode (unsigned int id, KmerNb count, const Sequence &sequence): _id(id), _count(count), _sequence(sequence), _set(true), _marked(false), _direct(true) {}
void SequenceNode::addNeighbor(unsigned int id, short position, short direction) {
if (find(_neighbors[position][direction].begin(), _neighbors[position][direction].end(), id) == _neighbors[position][direction].end()) {
_neighbors[position][direction].push_back(id);
}
}
void SequenceNode::setId(unsigned int id) {
_id = id;
}
unsigned int SequenceNode::getId() const {
return _id;
}
unsigned int SequenceNode::getSize() const {
return _sequence.getSize();
}
const Sequence &SequenceNode::getSequence() const {
return _sequence;
}
void SequenceNode::setSequence(const Sequence sequence) {
_sequence = sequence;
}
KmerNb SequenceNode::getCount() const {
return _count;
}
void SequenceNode::unset() {
_set = false;
}
bool SequenceNode::isSet() const {
return _set;
}
void SequenceNode::mark() {
_marked = true;
}
bool SequenceNode::isMarked() const {
return _marked;
}
bool SequenceNode::isDirect() const {
return _direct;
}
bool SequenceNode::isLeaf() const {
return (((_neighbors[Globals::BEFORE][Globals::DIRECT].empty()) && (_neighbors[Globals::BEFORE][Globals::REVERSE].empty())) || ((_neighbors[Globals::AFTER][Globals::DIRECT].empty()) && (_neighbors[Globals::AFTER][Globals::REVERSE].empty())));
}
short SequenceNode::getOnlyDirection() const {
if ((! _neighbors[Globals::BEFORE][Globals::DIRECT].empty()) || (! _neighbors[Globals::BEFORE][Globals::REVERSE].empty())) {
return Globals::BEFORE;
}
else {
return Globals::AFTER;
}
}
int SequenceNode::getNbNeighbors(short position, short direction) const {
return _neighbors[position][direction].size();
}
const unsigned int SequenceNode::getNeighbor(short position, short direction, int i) const {
return _neighbors[position][direction][i];
}
bool SequenceNode::merge(short position, short direction, const SequenceNode &n) {
for (short d = 0; d < Globals::DIRECTIONS; d++) {
//_neighbors[position][d] = n._neighbors[(direction == Globals::DIRECT)? position: 1-position][(direction == Globals::DIRECT)? d: 1-d];
const vector <unsigned int> &neighbors = n._neighbors[(direction == Globals::DIRECT)? position: 1-position][(direction == Globals::DIRECT)? d: 1-d];
_neighbors[position][d].assign(neighbors.begin(), neighbors.end());
}
_count = (getCount() * getSize() + n.getCount() * n.getSize()) / (getSize() + n.getSize());
string thisString = (_direct)? _sequence.getWord(Globals::DIRECT): _sequence.getWord(Globals::REVERSE);
//string thatString = (_direct == (direction == Globals::DIRECT))? n._sequence.getWord(Globals::DIRECT): n._sequence.getWord(Globals::REVERSE);
string thatString = ((n._direct) == (direction == Globals::DIRECT))? n._sequence.getWord(Globals::DIRECT): n._sequence.getWord(Globals::REVERSE);
string firstString, secondString;
if (position == Globals::AFTER) {
firstString = thisString;
secondString = thatString;
}
else {
firstString = thatString;
secondString = thisString;
}
secondString = secondString.substr(Globals::KMER-1);
string sequence = firstString + secondString;
_sequence = Sequence(sequence);
//cout << "\t\tChecking reverse: " << _sequence.getFirstWord() << " vs " << sequence << endl;
_direct = (_sequence.getFirstWord() == sequence);
/*
if (_sequence.getFirstWord() != sequence) {
_direct = ! _direct;
return false;
}
*/
return true;
}
void SequenceNode::updateLinks(unsigned int oldId, unsigned int newId, bool direct) {
for (short p = 0; p < Globals::POSITIONS; p++) {
for (short d = 0; d < Globals::DIRECTIONS; d++) {
for (unsigned int n = 0; n < _neighbors[p][d].size(); n++) {
if (_neighbors[p][d][n] == oldId) {
if (direct) {
_neighbors[p][d][n] = newId;
}
else {
_neighbors[p][1-d].push_back(newId);
}
}
}
}
}
}
void SequenceNode::removeLinks(short position, short direction) {
if (direction == Globals::DIRECTIONS) {
_neighbors[position][Globals::DIRECT].clear();
_neighbors[position][Globals::REVERSE].clear();
}
else {
_neighbors[position][direction].clear();
}
}
bool operator==(const SequenceNode& n1, const SequenceNode& n2) {
return n1.getId() == n2.getId();
}
ostream& operator<<(ostream& output, const SequenceNode& n) {
if (! n._neighbors[Globals::BEFORE][Globals::DIRECT].empty()) {
output << "(+) ";
for (const int neighbor: n._neighbors[Globals::BEFORE][Globals::DIRECT]) {
output << neighbor << " ";
}
}
if (! n._neighbors[Globals::BEFORE][Globals::REVERSE].empty()) {
output << "(-) ";
for (const int neighbor: n._neighbors[Globals::BEFORE][Globals::REVERSE]) {
output << neighbor << " ";
}
}
output << "<-- (" << n._id << ", " << n._sequence << ", " << n._count << ")";
if (! n._direct) {
output << " (-)";
}
output << " --> ";
if (! n._neighbors[Globals::AFTER][Globals::DIRECT].empty()) {
output << "(+) ";
for (const int neighbor: n._neighbors[Globals::AFTER][Globals::DIRECT]) {
output << neighbor << " ";
}
}
if (! n._neighbors[Globals::AFTER][Globals::REVERSE].empty()) {
output << "(-) ";
for (const int neighbor: n._neighbors[Globals::AFTER][Globals::REVERSE]) {
output << neighbor << " ";
}
}
return output;
}
SequencePath::SequencePath (): _cycle(false), _count(-1) {}
SequencePath::SequencePath (const SequencePath &p): _nodes(p._nodes), _nodeIds(p._nodeIds), _cycle(p._cycle), _count(p._count) {}
SequencePath::SequencePath (unsigned int id): _cycle(false), _count(-1) {
addNode(id);
}
SequencePath::SequencePath(unsigned int id1, short position, short direction, unsigned int id2): _cycle(false), _count(-1) {
addNode(id1);
addNode(position, direction, id2);
}
void SequencePath::addNode(unsigned int id) {
addNode(-1, -1, id);
}
void SequencePath::addNode(short position, short direction, unsigned int id) {
_nodes.push_back(make_tuple(position, direction, id));
_nodeIds.insert(id);
}
void SequencePath::removeLastNode() {
int id = SequencePath::getLastNode();
_nodes.pop_back();
_nodeIds.erase(id);
}
unsigned int SequencePath::getSize() const {
return _nodes.size();
}
unsigned int SequencePath::getNode(int i) const {
return get<2>(_nodes[i]);
}
int SequencePath::getNodePosition(int i) const {
return get<0>(_nodes[i]);
}
int SequencePath::getNodeDirection(int i) const {
return get<1>(_nodes[i]);
}
unsigned int SequencePath::getLastNode() const {
return get<2>(_nodes[_nodes.size()-1]);
}
bool SequencePath::empty() const {
return _nodes.empty();
}
void SequencePath::clear() {
_nodes.clear();
_nodeIds.clear();
}
void SequencePath::reverse() {
int size = _nodes.size();
vector < tuple <short, short, unsigned int> > nodes(size);
for (int i = 0; i < size; i++) {
nodes[i] = make_tuple(-1, -1, get<2>(_nodes[size-1-i]));
}
for (int i = 1; i < size; i++) {
get<0>(nodes[i]) = (get<1>(_nodes[size-i]) == Globals::DIRECT)? 1-get<0>(_nodes[size-i]): get<0>(_nodes[size-i]);
get<1>(nodes[i]) = get<1>(_nodes[size-i]);
}
_nodes = nodes;
}
void SequencePath::setCycle() {
_cycle = true;
}
bool SequencePath::isCycle() const {
return _cycle;
}
void SequencePath::setCount(const KmerNb count) {
_count = count;
}
KmerNb SequencePath::getCount() const {
return _count;
}
bool SequencePath::contains(int id) const {
return (_nodeIds.find(id) != _nodeIds.end());
}
bool SequencePath::contains(const SequencePath &p) const {
for (int node: p._nodeIds) {
if (! contains(node)) {
return false;
}
}
return true;
}
bool SequencePath::crosses(const SequencePath &p) const {
for (int node: p._nodeIds) {
if (contains(node)) {
return true;
}
}
return false;
}
bool SequencePath::crossesInterior(const SequencePath &p) const {
for (unsigned int i = 1; i < p._nodeIds.size()-1; i++) {
if (contains(p.getNode(i))) {
return true;
}
}
for (unsigned int i = 1; i < _nodeIds.size()-1; i++) {
if (p.contains(getNode(i))) {
return true;
}
}
return false;
}
void SequencePath::trimTo(const unsigned int destination) {
vector < tuple <short, short, unsigned int> > nodes;
short direct = true;
unsigned int pos;
_nodeIds.clear();
for (pos = 0; getNode(pos) != destination; pos++) {
if (getNodeDirection(pos) != Globals::DIRECT) {
direct = ! direct;
}
}
nodes.push_back(make_tuple(0, 0, destination));
_nodeIds.insert(destination);
for (pos++; pos < _nodes.size(); pos++) {
_nodeIds.insert(getNode(pos));
nodes.push_back(_nodes[pos]);
}
_nodes = nodes;
}
bool SequencePath::canMerge(const SequencePath &p) const {
if (getLastNode() == p.getNode(0)) {
//cout << "case A" << endl;
unsigned int last1 = getSize()-1;
short position1 = (getNodeDirection(last1) == Globals::DIRECT)? 1-getNodePosition(last1): getNodePosition(last1);
//short direction1 = getNodeDirection(last1);
//return (((position1 == Globals::AFTER) == (direction1 == Globals::DIRECT)) == (p.getNodePosition(1)));
return (position1 != p.getNodePosition(1));
}
else if (p.getLastNode() == getNode(0)) {
//cout << "case B" << endl;
unsigned int last2 = p.getSize()-1;
short position2 = (p.getNodeDirection(last2) == Globals::DIRECT)? 1-p.getNodePosition(last2): p.getNodePosition(last2);
//short direction2 = p.getNodeDirection(last2);
//return (((position2 == Globals::AFTER) == (direction2 == Globals::DIRECT)) == (getNodePosition(1)));
return (position2 != getNodePosition(1));
}
else if (getNode(0) == p.getNode(0)) {
//cout << "case C" << endl;
return (getNodePosition(1) != p.getNodePosition(1));
}
else if (getLastNode() == p.getLastNode()) {
//cout << "case D" << endl;
unsigned int last1 = getSize()-1;
unsigned int last2 = p.getSize()-1;
short position1 = (getNodeDirection(last1) == Globals::DIRECT)? 1-getNodePosition(last1): getNodePosition(last1);
//short direction1 = getNodeDirection(last1);
short position2 = (p.getNodeDirection(last2) == Globals::DIRECT)? 1-p.getNodePosition(last2): p.getNodePosition(last2);
//short direction2 = p.getNodeDirection(last2);
return (position1 != position2);
//return (((position1 == Globals::AFTER) == (direction1 == Globals::DIRECT)) != ((position2 == Globals::AFTER) == (direction2 == Globals::DIRECT)));
}
return false;
}
bool SequencePath::formCycle(const SequencePath &p) const {
if ((getNode(0) != p.getNode(0)) || (getLastNode() != p.getLastNode())) {
return false;
}
if (getNodePosition(1) == p.getNodePosition(1)) {
return false;
}
unsigned int last1 = getSize()-1;
unsigned int last2 = p.getSize()-1;
short position1 = (getNodeDirection(last1) == Globals::DIRECT)? 1-getNodePosition(last1): getNodePosition(last1);
short position2 = (p.getNodeDirection(last2) == Globals::DIRECT)? 1-p.getNodePosition(last2): p.getNodePosition(last2);
return (position1 != position2);
}
vector < SequencePath > SequencePath::addNode(const SequenceNode &linkNode, const SequenceNode &newNode) const {
vector < SequencePath > paths;
unsigned int linkNodeId = linkNode.getId(), newNodeId = newNode.getId();
if (getSize() == 1) {
for (short position = 0; position < Globals::POSITIONS; position++) {
for (short direction = 0; direction < Globals::DIRECTIONS; direction++) {
for (int i = 0; i < linkNode.getNbNeighbors(position, direction); i++) {
if (linkNode.getNeighbor(position, direction, i) == newNodeId) {
SequencePath newPath(linkNodeId);
newPath.addNode(position, direction, newNodeId);
paths.push_back(newPath);
}
}
}
}
}
else {
if (linkNodeId == getNode(0)) {
short p = getNodePosition(1);
for (short position = 0; position < Globals::POSITIONS; position++) {
short direction = Globals::DIRECT;
if (p != position) {
direction = Globals::REVERSE;
}
for (int i = 0; i < newNode.getNbNeighbors(position, direction); i++) {
if (newNode.getNeighbor(position, direction, i) == linkNode) {
SequencePath newPath(newNodeId);
newPath.addNode(position, direction, linkNodeId);
for (unsigned int j = 1; j < getSize(); j++) {
newPath.addNode(getNodePosition(j), getNodeDirection(j), getNode(j));
}
paths.push_back(newPath);
}
}
}
}
else if (linkNodeId == getLastNode()) {
unsigned int last = getSize() - 1;
short p = getNodePosition(last);
short d = getNodeDirection(last);
short position = (d == Globals::DIRECT)? p: 1-p;
for (short direction = 0; direction < Globals::DIRECTIONS; direction++) {
for (int i = 0; i < linkNode.getNbNeighbors(position, direction); i++) {
if (linkNode.getNeighbor(position, direction, i) == newNodeId) {
SequencePath newPath(*this);
newPath.addNode(position, direction, newNodeId);
paths.push_back(newPath);
}
}
}
}
else {
cerr << "Problem in 'AddNode'" << endl;
}
}
return paths;
}
SequencePath SequencePath::merge(const SequenceNode &linkNode, const SequencePath &path) const {
unsigned int linkNodeId = linkNode.getId();
SequencePath p1 = *this, p2 = path, pOut;
if ((p1.getNode(0) == linkNodeId) && (p2.getLastNode() == linkNodeId)) {
swap(p1, p2);
}
else {
if (p1.getNode(0) == linkNodeId) {
p1.reverse();
}
if (p2.getLastNode() == linkNodeId) {
p2.reverse();
}
}
for (unsigned int nodeId: path._nodeIds) {
if ((nodeId != linkNodeId) && (_nodeIds.find(nodeId) != _nodeIds.end())) {
return pOut;
}
}
if ((p1.getLastNode() == linkNodeId) && (p2.getNode(0) == linkNodeId)) {
unsigned int last1 = p1.getSize()-1;
short d = p1.getNodeDirection(last1), p = p1.getNodePosition(last1);
short position1 = (d == Globals::DIRECT)? p: 1-p;
if (position1 == p2.getNodePosition(1)) {
for (unsigned int i = 0; i < p1.getSize(); i++) {
pOut.addNode(p1.getNodePosition(i), p1.getNodeDirection(i), p1.getNode(i));
}
for (unsigned int i = 1; i < p2.getSize(); i++) {
pOut.addNode(p2.getNodePosition(i), p2.getNodeDirection(i), p2.getNode(i));
}
}
}
else {
cerr << "Problem while merging paths" << endl;
}
return pOut;
}
SequencePath operator+(const SequencePath& p1, const SequencePath& p2) {
SequencePath pOut, pIn1, pIn2;
if (p1.getLastNode() == p2.getNode(0)) {
//cout << " case 1" << endl;
pIn1 = p1;
pIn2 = p2;
}
else if (p2.getLastNode() == p1.getNode(0)) {
//cout << " case 2" << endl;
pIn1 = p2;
pIn2 = p1;
}
else if (p1.getNode(0) == p2.getNode(0)) {
//cout << " case 3. pIn1: " << pIn1;
pIn1 = p1;
pIn2 = p2;
pIn1.reverse();
//cout << " then " << pIn1 << endl;
}
else if (p1.getLastNode() == p2.getLastNode()) {
//cout << " case 4" << endl;
pIn1 = p1;
pIn2 = p2;
pIn2.reverse();
}
//cout << "+ing " << p1 << " and " << p2 << ", pIn1 is " << pIn1 << " and pIn2 is " << pIn2 << endl;
for (unsigned int i = 0; i < pIn1.getSize(); i++) {
pOut.addNode(pIn1.getNodePosition(i), pIn1.getNodeDirection(i), pIn1.getNode(i));
}
for (unsigned int i = 1; i < pIn2.getSize(); i++) {
pOut.addNode(pIn2.getNodePosition(i), pIn2.getNodeDirection(i), pIn2.getNode(i));
}
/*
pOut._nodes.insert(pOut._nodes.end(), p1._nodes.begin(), p1._nodes.end());
pOut._nodes.insert(pOut._nodes.end(), p2._nodes.begin(), p2._nodes.end());
for (int i = 0; i < pIn1.getSize(); i++) {
pOut._nodes.push_back(pIn1._nodes[i]);
}
for (int i = 1; i < pIn2.getSize(); i++) {
pOut._nodes.push_back(pIn2._nodes[i]);
}
for (int i: p1._nodeIds) {
pOut._nodeIds.insert(i);
}
for (int i: p2._nodeIds) {
pOut._nodeIds.insert(i);
}
*/
if (pOut.getNode(0) > pOut.getLastNode()) {
pOut.reverse();
//cout << "\t\tReversing" << endl;
}
//cout << "\t\tInput paths: " << p1 << " and " << p2 << endl;
return pOut;
}
bool operator==(const SequencePath& p1, const SequencePath& p2) {
if (p1.isCycle() != p2.isCycle()) {
return false;
}
if (p1._nodes.size() != p2._nodes.size()) {
return false;
}
return equal(p1._nodeIds.begin(), p1._nodeIds.end(), p2._nodeIds.begin());
}
ostream& operator<<(ostream& output, const SequencePath& p) {
for (const tuple < short, short, unsigned int > &element: p._nodes) {
int id;
short position, direction;
tie(position, direction, id) = element;
if (position == Globals::AFTER) {
output << " -->";
}
else if (position == Globals::BEFORE) {
output << " <--";
}
if (direction == Globals::DIRECT) {
output << " (+)";
}
if (direction == Globals::REVERSE) {
output << " (-)";
}
output << " " << id;
}
if (p.getCount() != static_cast<KmerNb>(-1)) {
output << " -- " << p.getCount();
}
if (p.isCycle()) {
output << " -- cycle";
}
return output;
}
SequenceGraph::SequenceGraph (): _maxPaths(0) {}
SequenceGraph::SequenceGraph (int size): _maxPaths(0), _nodes(size) {}
void SequenceGraph::setMaxPaths(const unsigned int maxPaths) {
_maxPaths = maxPaths;
}
unsigned int SequenceGraph::getSize() const {
return _nodes.size();
}
bool SequenceGraph::isSmall () const {
int size = Globals::KMER-1;
for (const SequenceNode &node: _nodes) {
//cout << "Size of node " << node << " is " << node.getSize() << endl;
if (node.isSet()) {
//cout << "\tis set" << endl;
//size += node.getSize();
size++;
}
}
//cout << "Total size is " << size << ", threshold is " << Globals::MIN_NB_NODES << endl;
return (size < Globals::MIN_NB_NODES);
}
bool SequenceGraph::isBig () const {
return (_nodes.size() > Globals::MAX_NB_NODES);
}
void SequenceGraph::addNode(unsigned int id, KmerNb count) {
if (id < _nodes.size()) {
_nodes[id] = SequenceNode(id, count);
}
else if (id == _nodes.size()) {
_nodes.push_back(SequenceNode(id, count));
}
else {
cerr << "Error while inserting the node " << id << endl;
}
}
void SequenceGraph::addNode(unsigned int id, KmerNb count, const Sequence &sequence) {
if (id < _nodes.size()) {
//cout << "adding node " << id << "/" << _nodes.size() << " mode 1" << endl;
_nodes[id] = SequenceNode(id, count, sequence);
}
else if (id == _nodes.size()) {
//cout << "adding node " << id << "/" << _nodes.size() << " mode 2" << endl;
_nodes.push_back(SequenceNode(id, count, sequence));
}
else {
cerr << "Error while inserting the node " << id << endl;
}
}
void SequenceGraph::addLink (unsigned int n1, short pos1, short dir1, unsigned int n2) {
_nodes[n1].addNeighbor(n2, pos1, dir1);
short dir2 = dir1;
short pos2 = (dir1 == Globals::DIRECT)? 1-pos1: pos1;
_nodes[n2].addNeighbor(n1, pos2, dir2);
}
SequenceNode &SequenceGraph::getNode (const unsigned int i) {
return _nodes[i];
}
bool SequenceGraph::findAllPathes() {
//findLeaves();
return findPathes();
//findCycles();
}
bool SequenceGraph::findPathes() {
//cout << "Starting path finding with graph\n" << *this << endl;
map < unsigned int, vector < unsigned int > > pathEnds;
for (unsigned int nodeId = 0; nodeId < getSize(); nodeId++) {
SequenceNode &node = _nodes[nodeId];
if (node.isSet()) {
//cout << "\tNow node " << node << endl;
unsigned int start = _pathes.size();
for (short position = 0; position < Globals::POSITIONS; position++) {
for (short direction = 0; direction < Globals::DIRECTIONS; direction++) {
for (int i = 0; i < node.getNbNeighbors(position, direction); i++) {
unsigned int neighborId = node.getNeighbor(position, direction, i);
SequenceNode &neighbor = _nodes[neighborId];
if (neighbor.isSet()) {
//cout << "\t\tNow neighbor " << neighbor << endl;
unsigned int peEnd = pathEnds[neighborId].size();
for (unsigned int pathId = 0; pathId < peEnd; pathId++) {
SequencePath &path = _pathes[pathEnds[neighborId][pathId]];
//cout << "\t\t\tNow path " << path << endl;
for (SequencePath &newPath: path.addNode(neighbor, node)) {
if (checkPath(newPath)) {
//cout << "\t\t\t\tNow new path " << newPath << endl;
if (! newPath.isCycle()) {
pathEnds[newPath.getNode(0)].push_back(_pathes.size());
pathEnds[newPath.getLastNode()].push_back(_pathes.size());
}
_pathes.push_back(newPath);
if ((_maxPaths > 0) && (_pathes.size() > _maxPaths)) {
_pathes.clear();
findGreedyPathes();
return false;
}
}
}
}
}
}
}
}
unsigned int end = _pathes.size();
for (unsigned int pathId = start; pathId < end; pathId++) {
SequencePath &path = _pathes[pathId];
if (! path.isCycle()) {
//cout << "\t\tCycling path " << path << endl;
unsigned int last = path.getSize() - 1;
short pStart = path.getNodePosition(1);
short pEnd = path.getNodePosition(last);
short dEnd = path.getNodeDirection(last);
short p = (dEnd == Globals::DIRECT)? pEnd: 1-pEnd;
short d = Globals::DIRECT;
bool found = false;
SequenceNode &lastNode = _nodes[path.getNode(last)];
unsigned int firstNodeId = path.getNode(0);
if (p != pStart) {
d = Globals::REVERSE;
}
for (int i = 0; (i < lastNode.getNbNeighbors(p, d)) && (! found); i++) {
if (lastNode.getNeighbor(p, d, i) == firstNodeId) {
found = true;
}
}
if (found) {
SequencePath newPath(path);
newPath.addNode(p, d, firstNodeId);
newPath.setCycle();
if (checkPath(newPath)) {
//cout << "\t\t\tGot new path " << newPath << endl;
_pathes.push_back(newPath);
if ((_maxPaths > 0) && (_pathes.size() > _maxPaths)) {
_pathes.clear();
findGreedyPathes();
return false;
}
}
}
}
}
for (unsigned int pathId1 = start; pathId1 < end; pathId1++) {
if (! _pathes[pathId1].isCycle()) {
//cout << "\t\tPath 1 " << _pathes[pathId1] << endl;
for (unsigned int pathId2 = pathId1+1; pathId2 < end; pathId2++) {
if (! _pathes[pathId2].isCycle()) {
//cout << "\t\t\tPath 2 " << _pathes[pathId2] << endl;
SequencePath newPath = _pathes[pathId1].merge(node, _pathes[pathId2]);
if ((! newPath.empty()) && (checkPath(newPath))) {
//cout << "\t\t\t\tNew path " << newPath << endl;
if (! newPath.isCycle()) {
pathEnds[newPath.getNode(0)].push_back(_pathes.size());
pathEnds[newPath.getLastNode()].push_back(_pathes.size());
}
_pathes.push_back(newPath);
if ((_maxPaths > 0) && (_pathes.size() > _maxPaths)) {
_pathes.clear();
findGreedyPathes();
return false;
}
}
}
}
}
}
pathEnds[nodeId].push_back(_pathes.size());
_pathes.push_back(SequencePath(nodeId));
if ((_maxPaths > 0) && (_pathes.size() > _maxPaths)) {
_pathes.clear();
findGreedyPathes();
return false;
}
}
}
/*
cout << "Over with" << endl;
for (SequencePath &path: _pathes) {
cout << "\t" << path << endl;
}
*/
//cout << "Found " << _pathes.size() << " pathes." << endl;
return true;
}
unsigned int SequenceGraph::findMostSeenNode() const {
KmerNb highestCount = 0;
unsigned int highestId = -1;
for (unsigned int nodeId = 0; nodeId < getSize(); nodeId++) {
if ((_nodes[nodeId].isSet()) && (_nodes[nodeId].getCount() > highestCount)) {
highestCount = _nodes[nodeId].getCount();
highestId = nodeId;
}
}
return highestId;
}
pair < unsigned int, bool > SequenceGraph::findMostSeenNeighbor(const unsigned int nodeId, const unsigned short position) const {
const SequenceNode& node = _nodes[nodeId];
KmerNb highestCount = 0, count;
unsigned int highestId = -1, id;
short highestDir = 0;
for (short d = 0; d < Globals::DIRECTIONS; d++) {
for (int i = 0; i < node.getNbNeighbors(position, d); i++) {
id = node.getNeighbor(position, d, i);
count = _nodes[id].getCount();
if ((_nodes[id].isSet()) && (count > highestCount)) {
highestCount = count;
highestId = id;
highestDir = d;
}
}
}
return make_pair(highestId, highestDir);
}
void SequenceGraph::findGreedyPathes() {
//cout << "Starting path finding with graph\n" << *this << endl;
static const unsigned int over = static_cast<unsigned int>(-1);
unsigned int highestId = findMostSeenNode(), currentId, nextId;
short highestDir, p, d;
while (highestId != over) {
SequenceNode &highestNode = _nodes[highestId];
string sequence = highestNode.getSequence().getWord(Globals::DIRECT);
KmerNb count = highestNode.getCount();
unsigned int cpt = 1;
highestNode.unset();
for (short position = 0; position < Globals::POSITIONS; position++) {
currentId = highestId;
p = position;
d = Globals::DIRECT;
tie(nextId, highestDir) = findMostSeenNeighbor(currentId, p);
while (nextId != over) {
currentId = nextId;
if (highestDir == Globals::REVERSE) {
p = 1-p;
d = 1-d;
}
SequenceNode ¤tNode = _nodes[currentId];
string currentSequence = currentNode.getSequence().getWord(d);
count += currentNode.getCount();
if (position == Globals::AFTER) {
sequence += currentSequence.substr(Globals::KMER-1);
}
else {
sequence = currentSequence.substr(0, currentSequence.size() - Globals::KMER + 1) + sequence;
}
cpt++;
currentNode.unset();
tie(nextId, highestDir) = findMostSeenNeighbor(currentId, p);
}
}
highestId = findMostSeenNode();
_repeats.push_back(CountedRepeat(sequence, count / cpt));
}
/*
cout << "Over with" << endl;
for (CountedRepeat &repeat: _repeats) {
cout << "\t" << repeat << endl;
}
*/
}
/*
void SequenceGraph::findPathes() {
//cout << "Starting path finding" << endl;
unsigned int nbPaths = _pathes.size()+1;
while (nbPaths != _pathes.size()) {
nbPaths = _pathes.size();
findLeaves();
vector < unsigned int > leaves;
vector <thread> threads(Globals::NB_THREADS);
mutex m1, m2;
for (unsigned int i = 0; i < _leaves.size(); i++) {
SequenceNode &node = _nodes[_leaves[i]];
if (node.isSet()) {
//cout << "Found leaf " << node << endl;
leaves.push_back(node.getId());
}
}
for (int threadId = 0; threadId < Globals::NB_THREADS; threadId++) {
threads[threadId] = thread([this, &leaves, &m1, &m2]() {
while (true) {
unsigned int id;
{
lock_guard<mutex> lock(m1);
if (! leaves.empty()) {
id = leaves.back();
leaves.pop_back();
}
else {
return;
}
}
findPathes(id, m2);
}
});
}
for (int threadId = 0; threadId < Globals::NB_THREADS; threadId++) {
threads[threadId].join();
}
removeMarkedNodes();
}
}
*/
/*
void SequenceGraph::findCycles() {
mutex m;
for (const SequenceNode &node: _nodes) {
if ((node.isSet()) && (! node.isMarked())) {
//cout << "Starting cycle at " << node << endl;
findPathes(node.getId(), m);
}
}
}
*/
bool SequenceGraph::isLeaf(const SequenceNode &n) const {
if (n.isLeaf()) {
return true;
}
for (short position = 0; position < Globals::POSITIONS; position++) {
bool empty = true;
for (short direction = 0; (direction < Globals::DIRECTIONS) && (empty); direction++) {
for (int i = 0; (i < n.getNbNeighbors(position, direction)) && (empty); i++) {
if (_nodes[n.getNeighbor(position, direction, i)].isSet()) {
empty = false;
}
}
}
if (empty) {
return true;
}
}
return false;
}
void SequenceGraph::countNeighbors(const SequenceNode &n, KmerNb *count) const {
for (short position = 0; position < Globals::POSITIONS; position++) {
for (short direction = 0; direction < Globals::DIRECTIONS; direction++) {
for (int i = 0; i < n.getNbNeighbors(position, direction); i++) {
if (_nodes[n.getNeighbor(position, direction, i)].isSet()) {
count[position]++;
}
}
}
}
}
int SequenceGraph::isVee(const SequenceNode &n) const {
KmerNb count[Globals::POSITIONS] = {0, 0};
countNeighbors(n, count);
if ((count[Globals::BEFORE] > 1) && (count[Globals::AFTER] > 1)) {
return 1+Globals::POSITIONS;
}
if (count[Globals::BEFORE] > 1) {
return 1+Globals::BEFORE;
}
if (count[Globals::AFTER] > 1) {
return 1+Globals::AFTER;
}
return 0;
}
int SequenceGraph::isFork(const SequenceNode &n) const {
if (n.isLeaf()) {
return false;
}
KmerNb count[Globals::POSITIONS] = {0, 0};
countNeighbors(n, count);
if ((count[Globals::BEFORE] == 1) && (count[Globals::AFTER] > 1)) {
return 1 + Globals::AFTER;
}
if ((count[Globals::BEFORE] > 1) && (count[Globals::AFTER] == 1)) {
return 1 + Globals::BEFORE;
}
return 0;
}
bool SequenceGraph::isAlone(const SequenceNode &n) const {
for (short position = 0; position < Globals::POSITIONS; position++) {
for (short direction = 0; direction < Globals::DIRECTIONS; direction++) {
for (int i = 0; i < n.getNbNeighbors(position, direction); i++) {
if (_nodes[n.getNeighbor(position, direction, i)].isSet()) {
return false;
}
}
}
}
return true;
}
short SequenceGraph::getOnlyDirection(const SequenceNode &n) const {
//cout << "\t\t\t\tGetting directions of " << n << ":";
for (short direction = 0; direction < Globals::DIRECTIONS; direction++) {
//cout << " direction: " << direction;
for (int i = 0; i < n.getNbNeighbors(Globals::BEFORE, direction); i++) {
//cout << " i: " << i << _nodes[n.getNeighbor(Globals::BEFORE, direction, i)];
if (_nodes[n.getNeighbor(Globals::BEFORE, direction, i)].isSet()) {
//cout << endl;
return Globals::BEFORE;
}
}
}
//cout << endl;
return Globals::AFTER;
}
unsigned int SequenceGraph::getCumulatedSize(const SequencePath &path) const {
unsigned int sum = Globals::KMER-1;
for (unsigned int i = 0; i < path.getSize(); i++) {
sum += _nodes[path.getNode(i)].getSize() - (Globals::KMER-1);