-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathust.cpp
1639 lines (1339 loc) · 54.9 KB
/
ust.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
// UST
// --- VERSION 1.0 ----
// Author: amatur, Last Edited: Nov 1
#include <cmath>
#include<cstring>
#include <fstream>
#include <iostream>
#include <vector>
#include <assert.h>
#include <stdint.h>
#include <unordered_set>
#include <map>
#include <set>
#include <sstream>
#include <algorithm>
#include <cstdlib>
#include <list>
#include <stack>
#include <unordered_map>
#include <utility>
#include <queue>
#include <deque>
#include <unistd.h>
#include <tuple>
using namespace std;
bool DEBUGMODE = false;
int K = 0;
string UNITIG_FILE = "examples/k11.unitigs.fa";
string OUTPUT_FILENAME;
enum DEBUGFLAG_T { NONE = 0, UKDEBUG = 6, VERIFYINPUT = 1, INDEGREEPRINT = 2, DFSDEBUGG = 3, PARTICULAR = 4, NODENUMBER_DBG = 5, OLDNEWMAP = 9, PRINTER = 10, SINKSOURCE = 12};
enum ALGOMODE_T { BASIC = 0, INDEGREE_DFS = 1, INDEGREE_DFS_1 = 2, OUTDEGREE_DFS = 3, OUTDEGREE_DFS_1 = 4, INDEGREE_DFS_INVERTED = 5, PLUS_INDEGREE_DFS = 6, RANDOM_DFS = 7, NODEASSIGN = 8, SOURCEFIRST = 9, TWOWAYEXT = 10, PROFILE_ONLY = 11, EPPRIOR=12, GRAPHPRINT = 13, TIGHTUB = 14, BRACKETCOMP = 15};
bool FLG_NEWUB = true;
bool FLG_ABUNDANCE = false;
DEBUGFLAG_T DBGFLAG = NONE; //NODENUMBER_DBG
ALGOMODE_T ALGOMODE = TWOWAYEXT;
string mapmode[] = {"basic", "indegree_dfs", "indegree_dfs_initial_sort_only", "outdegree_dfs", "outdegree_dfs_initial_sort_only", "inverted_indegree_dfs", "plus_indegree_dfs", "random_dfs", "node_assign", "source_first", "twoway", "profile_only", "endpoint_priority", "graph_print", "tight_ub", "tip"
};
string modefilename[] = {"Fwd", "indegree_dfs", "indegree_dfs_initial_sort_only", "outdegree_dfs", "outdegree_dfs_initial_sort_only", "inverted_indegree_dfs", "plus_indegree_dfs", "random_dfs", "node_assign", "source_first", "", "profile_only", "endpoint_priority", "graph_print", "tight_ub", "Tip"
};
typedef tuple<int,int,int, int> fourtuple; // uid, walkid, pos, isTip
bool sort_by_walkId (const fourtuple &lhs, const fourtuple &rhs){
return get<1>(lhs) < get<1>(rhs);
}
bool sort_by_pos (const fourtuple &lhs, const fourtuple &rhs){
return get<2>(lhs) < get<2>(rhs);
}
bool sort_by_tipstatus (const fourtuple &lhs, const fourtuple &rhs){
return get<3>(lhs) < get<3>(rhs);
}
typedef struct {
int serial = -1;
int startPosWithKOverlap;
int endPosWithKOVerlap;
bool isWalkEnd = false;
int pos_in_walk = -100;
int finalWalkId = -1; // renders some walkId as invalid
int isTip = 0;
} new_node_info_t;
typedef struct {
int serial;
int ln;
} unitig_struct_t;
typedef struct {
//1 means +, 0 means -
bool left;
bool right;
int toNode;
} edge_t;
typedef struct {
edge_t edge;
int fromNode;
} edge_both_t;
typedef struct {
edge_t edge;
int kmerStartIndex;
int kmerEndIndex;
} newEdge_t;
int C_ustitch = 0;
int C_twoway_ustitch = 0;
int C_tip_ustitch = 0;
int V_ustitch = 0;
int V_twoway_ustitch = 0;
int V_tip_ustitch = 0;
int isolated_node_count = 0;
int sink_count = 0;
int source_count = 0;
int sharedparent_count = 0;
int sharparentCntRefined = 0;
int onecount = 0;
struct node_sorter {
int node;
int sortkey;
//bool operator() (struct node_sorter i, struct node_sorter j) { return (i.sortkey<j.sortkey);}
};
bool sort_by_key (struct node_sorter i, struct node_sorter j) { return (i.sortkey<j.sortkey); }
bool sort_by_key_inverted (struct node_sorter i, struct node_sorter j) { return (i.sortkey>j.sortkey); }
int* global_indegree;
int* global_outdegree;
int* global_plusindegree;
int* global_plusoutdegree;
int* global_issinksource;
//int* global_priority;
map<pair <int, int>, int> inOutCombo;
vector<vector<edge_t> > adjList;
vector<vector<edge_t> > reverseAdjList;
vector<unitig_struct_t> unitigs;
map<int, string> newSequences;
map<int, string> newNewSequences; //int is the unitig id (old id)
set<int> newNewMarker;
vector<list<int> > newToOld;
vector<int> walkFirstNode; //given a walk id, what's the first node of that walk
unordered_map<int, vector<edge_t> > sinkSrcEdges; //int is the unitig id (old id)
string getFileName(const string& s) {
char sep = '/';
size_t i = s.rfind(sep, s.length());
if (i != string::npos) {
return(s.substr(i+1, s.length() - i));
}
return("");
}
inline string plus_strings(const string& a, const string& b, size_t kmersize) {
if (a == "") return b;
if (b == "") return a;
string ret = a + b.substr(kmersize - 1, b.length() - (kmersize - 1));
return ret;
}
string delSpaces(string &str) {
str.erase(std::remove(str.begin(), str.end(), ' '), str.end());
return str;
}
bool charToBool(char c) {
if (c == '+') {
return true;
} else {
if (c != '-') cout << "Erroneus character in BCALM2 output." << endl;
return false;
}
}
string reverseComplement(string base) {
size_t len = base.length();
char* out = new char[len + 1];
out[len] = '\0';
for (int i = 0; i < len; i++) {
if (base[i] == 'A') out[len - i - 1] = 'T';
else if (base[i] == 'C') out[len - i - 1] = 'G';
else if (base[i] == 'G') out[len - i - 1] = 'C';
else if (base[i] == 'T') out[len - i - 1] = 'A';
}
string outString(out);
free(out);
return outString;
}
double readTimer() {
return clock() / (double) CLOCKS_PER_SEC;
}
inline string currentDateTime() {
// Get current date/time, format is YYYY-MM-DD HH:mm:ss
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof (buf), "%Y-%m-%d %X\n", &tstruct);
return buf;
}
int countOutArcs(int node) {
return (adjList.at(node)).size();
}
inline char boolToCharSign(bool sign) {
return (sign == true) ? '+' : '-';
}
// @@ --- ALL PRINTING CODE --- //
void printBCALMGraph(vector<vector<edge_t> > adjList) {
for (int i = 0; i < adjList.size(); i++) {
cout << i << "# ";
for (edge_t edge : adjList.at(i)) {
cout << boolToCharSign(edge.left) << ":" << edge.toNode << ":" << boolToCharSign(edge.right) << ", ";
}
cout << endl;
}
}
class GroupMerger {
public:
map<int, bool> fwdVisited;
map<int, bool> bwdVisited;
map<int, int> bwdWalkId;
map<int, int> fwdWalkId;
GroupMerger() {
}
void connectGroups(int from, int to){
fwdVisited[from] = false;
bwdVisited[to] = false;
fwdWalkId[from] = to;
bwdWalkId[to] = from;
}
~GroupMerger() {
}
};
class DisjointSet {
unordered_map<int, int> parent;
public:
DisjointSet() {
}
void make_set(int id) {
this->parent[id] = -1;
}
void Union(int xId, int yId) {
int xset = find_set(xId);
int yset = find_set(yId);
if(xset != yset)
{
parent[xset] = yset;
}
}
int find_set(int id) {
if (parent[id] == -1)
return id;
return find_set(parent[id]);
}
~DisjointSet(){
}
};
class Graph {
public:
size_t V = adjList.size();
int countNewNode = 0;
int time = 0;
char* color;
int* p_dfs;
bool* nodeSign;
new_node_info_t* oldToNew;
bool* saturated;
struct node_sorter * sortStruct;
bool* countedForLowerBound;
DisjointSet disSet;
GroupMerger gmerge;
Graph() {
color = new char[V];
p_dfs = new int[V];
nodeSign = new bool[V];
oldToNew = new new_node_info_t[V];
saturated = new bool[V];
sortStruct = new struct node_sorter[V];
global_indegree = new int[V];
global_outdegree = new int[V];
global_plusindegree = new int[V];
global_plusoutdegree = new int[V];
global_issinksource = new int[V];
//global_priority = new int[V];
countedForLowerBound = new bool[V];
for (int i = 0; i < V; i++) {
if(ALGOMODE == TWOWAYEXT || ALGOMODE == BRACKETCOMP ){
disSet.make_set(i);
}
oldToNew[i].serial = -1;
saturated[i] = false;
sortStruct[i].sortkey = 0;
sortStruct[i].node = i;
global_indegree[i] = 0;
global_outdegree[i] = 0;
global_plusindegree[i] = 0;
global_plusoutdegree[i] = 0;
global_issinksource[i] = 0;
//global_priority[i] = 0;
countedForLowerBound[i] = false;
}
}
inline bool sRight(edge_t plusminusedge){
return !(plusminusedge.right == true);
}
inline bool sLeft(edge_t plusminusedge){
return (plusminusedge.left == true);
}
void indegreePopulate(){
int xc = 0;
for(vector<edge_t> elist: adjList){
for(edge_t e: elist){
global_indegree[e.toNode] += 1;
sortStruct[e.toNode].sortkey = sortStruct[e.toNode].sortkey + 1;
if(e.right == true){
global_plusindegree[e.toNode] += 1;
}
if(e.left == true){
global_plusoutdegree[xc] += 1;
}
}
global_outdegree[xc] = elist.size();
xc++;
}
for(int i = 0; i<5; i++){
for(int j = 0; j<5; j++){
inOutCombo[make_pair(i,j)] = 0;
}
}
for(int i = 0; i<V; i++){
pair<int, int> a;
a = make_pair(global_plusindegree[i], global_plusoutdegree[i]);
inOutCombo[a] = (inOutCombo.count(a) ? inOutCombo[a] + 1 : 1 );
if(DBGFLAG == SINKSOURCE){
cout<<i<<"is ";
}
if(global_plusoutdegree[i] == 0 && global_plusindegree[i] != 0){
sink_count++;
global_issinksource[i] = 1;
//global_priority[i] = 5;
countedForLowerBound[i] = true;
if(DBGFLAG == SINKSOURCE){
cout<<"sink, ";
}
}
if(global_plusindegree[i] == 0 && global_plusoutdegree[i] != 0){
source_count++;
global_issinksource[i] = 1;
//global_priority[i] = 5;
countedForLowerBound[i] = true;
if(DBGFLAG == SINKSOURCE){
cout<<"source, ";
}
}
if(global_indegree[i] == 0){
global_issinksource[i] = 1;
isolated_node_count++;
if(DBGFLAG == SINKSOURCE){
cout<<"isolated, ";
}
}
if(global_indegree[i] == 1){
onecount++;
}
if(DBGFLAG == SINKSOURCE){
cout<<endl;
}
}
xc = 0; // current vertex while traversing the adjacency list
for(vector<edge_t> elist: adjList){
int neighborCount = 0;
int spNeighborCount[2];
spNeighborCount[0]=0;
spNeighborCount[1]=0;
stack<int> countedNodes;
set<pair<int, bool> > countedSides;
//if(true){
if(FLG_NEWUB == true){
//ENDPOINT SIDE UPPER BOUND - improved
for(edge_t e_xy: elist){ //central node: all neighbors of x
int y = e_xy.toNode;
vector<edge_t> adjY = adjList[y];
bool eligibleSp = true;
//pair<int, bool> pairr;
for(edge_t e_from_y : adjY){ // check if this neighbor is speacial
//pairr =make_pair(e_from_y.toNode, sRight(e_xy) );
if(e_from_y.toNode!=xc){
if(sRight(e_xy) == sLeft(e_from_y)){
eligibleSp = false;
break;
}
}
}
if(eligibleSp){
spNeighborCount[sLeft(e_xy)]++;
}
}
if(spNeighborCount[0]>1){
sharedparent_count += spNeighborCount[0] - 1 ;
}
if(spNeighborCount[1]>1){
sharedparent_count += spNeighborCount[1] - 1 ;
}
}
if(FLG_NEWUB == false){
//ENDPOINT SIDE UPPER BOUND
for(edge_t e_xy: elist){
int y = e_xy.toNode;
vector<edge_t> adjY = adjList[y];
bool eligible = true;
pair<int, bool> pairr;
for(edge_t e_from_y : adjY){
pairr =make_pair(e_from_y.toNode, sRight(e_xy) );
if(e_from_y.toNode!=xc){
if(sRight(e_xy) == sLeft(e_from_y)){
eligible = false;
break;
}
}
}
if(eligible){
neighborCount++;
}
}
if(global_issinksource[xc] == 1){
if(neighborCount>1){
sharedparent_count += neighborCount - 1 ;
}
}else{
if(neighborCount>2){
sharedparent_count += neighborCount - 2 ;
}
}
}
//sharedparent_count_wrong =sharedparent_count;
//if(true){
if(1==0){
// OLDER UPPER BOUND CALC
int neighborCount = 0;
for(edge_t e_xy: elist){
int y = e_xy.toNode;
if(!countedForLowerBound[y]){
vector<edge_t> adjY = adjList[y];
bool eligible = true;
for(edge_t e_from_y : adjY){
if(e_from_y.toNode!=xc){
if(sRight(e_xy) == sLeft(e_from_y) ){
eligible = false;
break;
}
}
}
if(eligible){
countedForLowerBound[y] = true;
//global_priority[y] = 4;
neighborCount++;
countedNodes.push(y);
}
}
}
if(global_issinksource[xc] == 1){
if(neighborCount>1){
sharedparent_count += neighborCount - 1 ;
}else{
while(!countedNodes.empty()){
countedForLowerBound[countedNodes.top()] = false;
countedNodes.pop();
}
}
}else{
if(neighborCount>2){
sharedparent_count += neighborCount - 2 ;
}else{
while(!countedNodes.empty()){
countedForLowerBound[countedNodes.top()] = false;
countedNodes.pop();
}
}
}
}
xc++;
}
//check if not ALGOMODE == INDEGREE_DFS_INVERTED
delete [] global_indegree;
delete [] global_outdegree;
delete [] global_plusindegree;
delete [] global_plusoutdegree;
}
void DFS_visit(int u) {
if(ALGOMODE == BRACKETCOMP){
if(global_issinksource[u]==1){
vector<edge_t> adju = adjList.at(u);
vector<edge_t> myvector;
for (edge_t e : adju) {
myvector.push_back(e);
}
sinkSrcEdges[u] = myvector;
return;
}
}
stack<edge_t> s;
edge_t uEdge;
uEdge.toNode = u;
s.push(uEdge);
while (!s.empty()) {
edge_t xEdge = s.top();
int x = xEdge.toNode;
s.pop();
if (color[x] == 'w') {
//Original DFS code
time = time + 1;
color[x] = 'g';
s.push(xEdge);
vector<edge_t> adjx = adjList.at(x);
if(ALGOMODE == RANDOM_DFS){
random_shuffle ( adjx.begin(), adjx.end() );
}
// if(ALGOMODE == EPPRIOR){
// sort( adjx.begin( ), adjx.end( ), [ ]( const edge_t& lhs, const edge_t& rhs )
// {
// return global_priority[lhs.toNode] < global_priority[rhs.toNode] ;
// });
// }
if(ALGOMODE == INDEGREE_DFS){
sort( adjx.begin( ), adjx.end( ), [ ]( const edge_t& lhs, const edge_t& rhs )
{
return global_indegree[lhs.toNode] < global_indegree[rhs.toNode];
});
}
if(ALGOMODE == PLUS_INDEGREE_DFS){
sort( adjx.begin( ), adjx.end( ), [ ]( const edge_t& lhs, const edge_t& rhs )
{
return global_indegree[lhs.toNode] - global_plusindegree[lhs.toNode] > global_indegree[lhs.toNode] - global_plusindegree[rhs.toNode];
});
}
if(ALGOMODE == INDEGREE_DFS_INVERTED){
sort( adjx.begin( ), adjx.end( ), [ ]( const edge_t& lhs, const edge_t& rhs )
{
return global_indegree[lhs.toNode] > global_indegree[rhs.toNode];
});
}
if (ALGOMODE == OUTDEGREE_DFS){
if(p_dfs[x] == -1){
sort( adjx.begin( ), adjx.end( ), [ ]( const edge_t& lhs, const edge_t& rhs )
{
return global_outdegree[lhs.toNode] < global_outdegree[rhs.toNode];
});
}else if (nodeSign[x] == false){
sort( adjx.begin( ), adjx.end( ), [ ]( const edge_t& lhs, const edge_t& rhs )
{
return global_outdegree[lhs.toNode] - global_plusoutdegree[lhs.toNode] < global_outdegree[rhs.toNode] - global_plusoutdegree[rhs.toNode];
});
}else if (nodeSign[x] == true){
sort( adjx.begin( ), adjx.end( ), [ ]( const edge_t& lhs, const edge_t& rhs )
{
return global_plusoutdegree[lhs.toNode] < global_plusoutdegree[rhs.toNode];
});
}
}
// Now our branching code ::
// For a white x
// Consider 2 case:
// Case 1. p[x] = -1, it can happen in two way, x is the first one ever in this connected component, or no one wanted to take x
// either way, if p[x] = -1, i can be representative of a new node in new graph
// Case 2. p[x] != -1, so x won't be the representative/head of a newHome. x just gets added to its parent's newHome.
int u = unitigs.at(x).ln; //unitig length
if (p_dfs[x] == -1) {
list<int> xxx;
xxx.push_back(x);
newToOld.push_back(xxx);
oldToNew[x].serial = countNewNode++; // countNewNode starts at 0, then keeps increasing
oldToNew[x].finalWalkId = oldToNew[x].serial;
//added while doing bracket comp
walkFirstNode.push_back(x);
oldToNew[x].pos_in_walk = 1;
oldToNew[x].startPosWithKOverlap = 1;
if (u < K) {
oldToNew[x].endPosWithKOVerlap = 1; // do we actually see this? yes
if(DBGFLAG == UKDEBUG){
cout<< "node: "<< x<<"u< k ***** u = "<<u<<endl;
}
} else {
oldToNew[x].endPosWithKOVerlap = u - K + 1;
}
} else {
newToOld[oldToNew[p_dfs[x]].serial].push_back(x);
oldToNew[x].serial = oldToNew[p_dfs[x]].serial;
oldToNew[x].finalWalkId = oldToNew[x].serial;
if(ALGOMODE==TWOWAYEXT || ALGOMODE==BRACKETCOMP ){
disSet.Union(x, p_dfs[x]);
}
oldToNew[x].startPosWithKOverlap = oldToNew[p_dfs[x]].endPosWithKOVerlap + 1;
oldToNew[x].pos_in_walk = oldToNew[p_dfs[x]].pos_in_walk + 1;
if (u < K) {
oldToNew[x].endPosWithKOVerlap = oldToNew[x].startPosWithKOverlap + 1; // do we actually see this? yes
if(DBGFLAG == UKDEBUG){
cout<< "node: "<< x<<"u< k ***** u = "<<u<<endl;
}
} else {
oldToNew[x].endPosWithKOVerlap = u - K + (oldToNew[x].startPosWithKOverlap); //check correctness
}
}
// x->y is the edge, x is the parent we are extending
for (edge_t yEdge : adjx) { //edge_t yEdge = adjx.at(i);
int y = yEdge.toNode;
if(ALGOMODE == BRACKETCOMP){
if(global_issinksource[y] == true){
continue;
}
}
//Normal DFS
if (color[y] == 'w') {
s.push(yEdge);
}
//handle self-loop, self-loop will always be an extra edge
// redundant, because we remove self-loop anyway
if (y == x) {
edge_both_t e;
e.edge = yEdge;
e.fromNode = x;
} else if (saturated[x]) {
// Since x is saturated, we only add resolveLater edges
// no need to check for consistency
if (y != p_dfs[x]) {
edge_both_t e;
e.edge = yEdge;
e.fromNode = x;
}
} else {
// If x has space to take a child, meaning x is not saturated
// hunting for potential child
if (color[y] == 'w' && p_dfs[y] == -1) {
// y has white color & got no parent => means it's homeless, so let's see if we can take it as a child of x
//But just see if it is eligible to be a child, i.e. is it consistent (sign check)?
//2 case, Does x's child have grandparent?
// If No:
if (p_dfs[x] == -1 && ALGOMODE != NODEASSIGN) {
// case 1: child has no grandparent
// so extend path without checking any sign
nodeSign[x] = yEdge.left;
nodeSign[y] = yEdge.right;
p_dfs[y] = x;
saturated[x] = true; //found a child
} else if (nodeSign[x] == yEdge.left) {
// case 2: child (=y) has grandparent, i.e. x's parent exists
nodeSign[y] = yEdge.right;
p_dfs[y] = x;
saturated[x] = true; //found a child
} else {
// do we reach this case?
edge_both_t e;
e.edge = yEdge;
e.fromNode = x;
}
} else {
//merger
if(ALGOMODE == TWOWAYEXT || ALGOMODE == BRACKETCOMP){
// y is not white
bool consistentEdge = (nodeSign[y] == yEdge.right && (p_dfs[x]==-1 || (p_dfs[x]!=-1&& nodeSign[x] == yEdge.left)) );
if(p_dfs[y]==-1 && consistentEdge && oldToNew[x].serial != oldToNew[y].serial){
//cout<<"x: "<<x<<":" <<disSet.find_set(x)<<" ";
//cout<<"y: "<<y<<":" <<disSet.find_set(y) <<endl;
//not in same group already, prevent cycle
if(disSet.find_set(x)!=disSet.find_set(y)){
nodeSign[x] = yEdge.left;
nodeSign[y] = yEdge.right;
p_dfs[y] = x;
saturated[x] = true; //found a child
// oldToNew[y].serial
disSet.Union(x, y);
gmerge.connectGroups(oldToNew[x].serial,oldToNew[y].serial );
}
}
}
if (y != p_dfs[x]) {
edge_both_t e;
e.edge = yEdge;
e.fromNode = x;
}
}
}
}
} else if (color[x] == 'g') {
time = time + 1;
color[x] = 'b';
}
}
}
void DFS() {
if(ALGOMODE == NODEASSIGN){
for (int i=0; i<V; i++) {
nodeSign[i] = true;
if(global_plusindegree[i]< global_indegree[i] - global_plusindegree[i]){
nodeSign[i] = true;
}
}
}
if(ALGOMODE == SOURCEFIRST){
for (int i = 0; i < V; i++) {
sortStruct[i].node = i;
sortStruct[i].sortkey = global_issinksource[i];
}
vector<struct node_sorter> myvector (sortStruct, sortStruct+V);
sort (myvector.begin(), myvector.end(), sort_by_key_inverted);
copy(myvector.begin(), myvector.end(), sortStruct);
}
// if(ALGOMODE == EPPRIOR){
// for (int i = 0; i < V; i++) {
// sortStruct[i].node = i;
// sortStruct[i].sortkey = global_priority[i];
// }
// vector<struct node_sorter> myvector (sortStruct, sortStruct+V);
// sort (myvector.begin(), myvector.end(), sort_by_key_inverted);
// copy(myvector.begin(), myvector.end(), sortStruct);
// }
if(ALGOMODE == INDEGREE_DFS_INVERTED){
for (int i = 0; i < V; i++) {
sortStruct[i].node = i;
sortStruct[i].sortkey = global_indegree[i];
}
vector<struct node_sorter> myvector (sortStruct, sortStruct+V);
sort (myvector.begin(), myvector.end(), sort_by_key_inverted);
//random_shuffle ( myvector.begin(), myvector.end() );
copy(myvector.begin(), myvector.end(), sortStruct);
}
if (ALGOMODE == INDEGREE_DFS || ALGOMODE == INDEGREE_DFS_1 ){
for (int i = 0; i < V; i++) {
sortStruct[i].node = i;
sortStruct[i].sortkey = global_indegree[i];
}
vector<struct node_sorter> myvector (sortStruct, sortStruct+V);
sort (myvector.begin(), myvector.end(), sort_by_key);
copy(myvector.begin(), myvector.end(), sortStruct);
if(DBGFLAG == INDEGREEPRINT){
cout<<"print in degrees"<<endl;
for(int i = 0; i<V; i++){
cout<<sortStruct[i].node<<"->"<<sortStruct[i].sortkey<<endl;
}
}
}
if(ALGOMODE == RANDOM_DFS){
for (int i = 0; i < V; i++) {
sortStruct[i].node = i;
sortStruct[i].sortkey = global_indegree[i];
}
vector<struct node_sorter> myvector (sortStruct, sortStruct+V);
sort (myvector.begin(), myvector.end(), sort_by_key);
random_shuffle ( myvector.begin(), myvector.end() );
copy(myvector.begin(), myvector.end(), sortStruct);
}
if (ALGOMODE == OUTDEGREE_DFS || ALGOMODE == OUTDEGREE_DFS_1){
for (int i = 0; i < V; i++) {
sortStruct[i].node = i;
sortStruct[i].sortkey = global_outdegree[i];
}
vector<struct node_sorter> myvector (sortStruct, sortStruct+V);
sort (myvector.begin(), myvector.end(), sort_by_key);
copy(myvector.begin(), myvector.end(), sortStruct);
}
double time_a = readTimer();
for (int i = 0; i < V; i++) {
color[i] = 'w';
p_dfs[i] = -1;
}
cout<<"Basic V loop time: "<<readTimer() - time_a<<" sec"<<endl;
time_a = readTimer();
for (int j = 0; j < V; j++) {
int i;
if(ALGOMODE == OUTDEGREE_DFS || ALGOMODE == OUTDEGREE_DFS_1 || ALGOMODE == INDEGREE_DFS || ALGOMODE == INDEGREE_DFS_1 || ALGOMODE == SOURCEFIRST){
i = sortStruct[j].node;
}else{
i = j;
}
if (color[i] == 'w') {
if(DBGFLAG == DFSDEBUGG ){
cout<<"visit start of node: "<<i<<endl;
}
DFS_visit(i);
}
}
cout<<"DFS time: "<<readTimer() - time_a<<" sec"<<endl;
/***MERGE START***/
bool* merged = new bool[countNewNode];
for (int i = 0; i<countNewNode; i++) {
merged[i] = false;
}
if(ALGOMODE == TWOWAYEXT || ALGOMODE == BRACKETCOMP){
ofstream uidSequenceFile;
uidSequenceFile.open("uidSeq"+modefilename[ALGOMODE]+".txt");
for ( const auto& p: gmerge.fwdWalkId)
{
if(gmerge.fwdVisited[p.first] == false){
int fromnode =p.first;
int tonode = p.second;
deque<int> lst;
lst.push_back(fromnode);
lst.push_back(tonode);
gmerge.fwdVisited[fromnode] = true;
gmerge.bwdVisited[tonode] = true;
if(gmerge.fwdVisited.count(tonode)>0){
while(gmerge.fwdVisited[tonode] == false){
gmerge.fwdVisited[tonode] = true;
tonode = gmerge.fwdWalkId[tonode];
gmerge.bwdVisited[tonode] = true;
lst.push_back(tonode);
if(gmerge.fwdVisited.count(tonode)==0)
break;
}
}
if(gmerge.bwdVisited.count(fromnode)>0){
while(gmerge.bwdVisited[fromnode] == false){
gmerge.bwdVisited[fromnode] = true;
fromnode = gmerge.bwdWalkId[fromnode];
gmerge.fwdVisited[fromnode] = true;
lst.push_front(fromnode);
if(gmerge.bwdVisited.count(fromnode)==0)
break;
}
}
int headOfThisWalk = walkFirstNode[lst.at(0)]; //CHECK AGAIN
assert(!lst.empty());
int commonWalkId = lst.at(0);
int posOffset = 1;
int lastWalk = -1;
for(auto i: lst){
// i is new walk id before merging
merged[i] = true;
walkFirstNode[i] = headOfThisWalk;
// travesing the walk list of walk ID i
for(int uid: newToOld[i]){
oldToNew[uid].serial = commonWalkId;
oldToNew[uid].finalWalkId = commonWalkId;
oldToNew[uid].pos_in_walk = posOffset++;
}
}
oldToNew[newToOld[lst.back()].back()].isWalkEnd = true;
V_twoway_ustitch ++;
}
}
for (int newNodeNum = 0; newNodeNum<countNewNode; newNodeNum++){
if(merged[newNodeNum] == false){
oldToNew[newToOld[newNodeNum].back()].isWalkEnd = true;
V_twoway_ustitch++;
}
}
}
//sorter of all walks and printing them
vector<fourtuple> sorter;
for(int uid = 0 ; uid< V; uid++){
new_node_info_t nd = oldToNew[uid];
sorter.push_back(make_tuple(uid, nd.finalWalkId, nd.pos_in_walk, nd.isTip));
}
//stable_sort(sorter.begin(),sorter.end(),sort_by_tipstatus);
stable_sort(sorter.begin(),sorter.end(),sort_by_pos);
stable_sort(sorter.begin(),sorter.end(),sort_by_walkId);
ofstream uidSequence;
string uidSeqFilename = "uidSeq.usttemp"; //"uidSeq"+ mapmode[ALGOMODE] +".txt"
uidSequence.open(uidSeqFilename);
int finalUnitigSerial = 0;
for(fourtuple n : sorter){