-
Notifications
You must be signed in to change notification settings - Fork 0
/
findCNS.cpp
3608 lines (2999 loc) · 113 KB
/
findCNS.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 <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <math.h>
#include <sys/time.h>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <string.h>
#include <algorithm>
#include <stdint.h>
#include <list>
#include <stack>
#include <limits.h>
#include <vector>
#define NINF INT_MIN
using namespace std;
// Default length, can override at runtime
int Kmer_Len = 30;
string CDG_Filename = "cdg.dot"; //for output of compressed de Bruijn graph in dot format
int DEBUG = 0;
int VERBOSE = 0;
int VERIFY = 0;
typedef uint32_t treeint;
typedef uint64_t treeintLarge;
treeintLarge skippedbases = 0;
treeintLarge skippedextensions = 0;
treeint numKmerLens = 0;
treeint numNodesWithTable = 0;
treeint numEntriesInAuxTables = 0;
// Settings for linear time alg
bool FORCEROOT = false;
bool DOJUMP = true;
bool DOINTERNALSKIP = true;
bool DOPHASETRICK = true;
bool MEM = false;
const int basecount = 7;
int b2i(char base)
{
switch (base)
{
case '$' : return 0;
case 'A' : return 1;
case 'C' : return 2;
case 'G' : return 3;
case 'N' : return 4;
case 'T' : return 5;
case '#' : return 6;
default:
cerr << "Unknown base: " << base << endl;
return b2i('N');
};
}
class MerVertex_t;
class SuffixNode
{
public:
static treeintLarge s_nodecount;
SuffixNode(
treeint s,
treeint e,
SuffixNode * p,
SuffixNode * x)
: m_start(s),
m_end(e),
m_parent(p)//,
{
s_nodecount++;
m_isCopy = false;
m_suffixTable = new SuffixNode*[1]; //for first suffix link, replaced with a larger table later
m_suffixTable[0] = x;
m_strdepth = 0;
for (int i = 0; i < basecount; i++)
{
m_children[i] = NULL;
}
}
//copy constructor
SuffixNode(const SuffixNode & node)
{
m_start = node.m_start;
m_end = node.m_end;
m_SA_start = node.m_SA_start;
m_SA_end = node.m_SA_end;
m_parent = node.m_parent;
for (int i = 0; i < basecount; i++)
{
m_children[i] = NULL;
}
m_isCopy = true;
m_suffixTable = node.m_suffixTable;
m_LMAproximityTable= node.m_LMAproximityTable;
m_LMAprox_nodeTable = node.m_LMAprox_nodeTable;
m_MEM = node.m_MEM;
m_LMA = node.m_LMA;
m_strdepth = node.m_strdepth;
}
void createSuffixLinkTable(treeint numSuffixLinks, bool otherTables = true)
{
if(numSuffixLinks==0)
numSuffixLinks = 1; //min number of entries in table
m_suffixTable = new SuffixNode*[numSuffixLinks];
if(otherTables)
{
numNodesWithTable++;
m_LMAproximityTable = new treeint[numSuffixLinks];
m_LMAprox_nodeTable = new SuffixNode*[numSuffixLinks];
}
else
{
m_LMAproximityTable = NULL;
m_LMAprox_nodeTable = NULL;
}
}
void deleteSuffixLinkTable()
{
SuffixNode * suffixLink = m_suffixTable[0];
if(m_suffixTable)
{
delete [] m_suffixTable;
}
if(m_LMAproximityTable)
{
delete [] m_LMAproximityTable;
m_LMAproximityTable = NULL;
}
if(m_LMAprox_nodeTable)
{
delete [] m_LMAprox_nodeTable;
m_LMAprox_nodeTable = NULL;
}
createSuffixLinkTable(1, false);
m_suffixTable[0] = suffixLink;
}
~SuffixNode()
{
if(!m_isCopy)
{
if(m_suffixTable)
delete [] m_suffixTable;
if(m_LMAproximityTable)
delete [] m_LMAproximityTable;
if(m_LMAprox_nodeTable)
delete [] m_LMAprox_nodeTable;
}
for (int i = 0; i < basecount; i++)
{
if (m_children[i]) { delete m_children[i]; }
}
}
string str(const string & s)
{
return s.substr(m_start, m_end-m_start+1);
}
treeint len(int i=-1)
{
if (i != -1)
{
if (i < m_end)
{
return i - m_start + 1;
}
}
return m_end - m_start + 1;
}
ostream & printLabel(ostream & os, const string & str)
{
string seq = str.substr(m_start, m_end-m_start+1);
if (m_start == m_end && m_start == 0)
{
os << "\"ROOT\"";
}
else
{
os << "\"" << seq;
if(m_MEM)
{
os << " MEM ";
}
os << ":"
<< " [" << m_start
<< "," << m_end << "]"
<< "}\"";
}
return os;
}
ostream & printNodeLabel(ostream & os)
{
os << m_start << m_end << m_strdepth ;
return os;
}
ostream & printEdgeLabel(ostream & os, const string & str)
{
string seq = str.substr(m_start, m_end-m_start+1);
os << "\"" << seq << (MEM?(m_MEM ? " MEM " :""):"")
<< " SA [" << m_SA_start
<< ", " << m_SA_end <<"]"
<< " [" << m_start
<< "," << m_end << "]\"";
return os;
}
//returns lowest marked ancestor (if this node is marked, it is the lowest)
//if no marked ancestor, NULL
SuffixNode* LMA()
{
return m_LMA;
}
//return num suffix links in table based on strdepth
inline int nodeNumSuffixLinks()
{
if(m_strdepth == 0)
return 0; //for root
return (int)(ceil(log(m_strdepth) / log(2)));
}
treeint m_start;
treeint m_end;
treeint m_strdepth;
bool m_isCopy; //set to true for copy constructor. then suffix link table will not be deleted when the function returns
bool m_MEM;
bool m_prevChar[basecount];
typedef SuffixNode * SuffixNodePtr;
SuffixNode * m_parent;
SuffixNode * m_children [basecount];
SuffixNodePtr * m_suffixTable;
treeint * m_LMAproximityTable; //[i] stores smallest m_LMAproximity encountered in m_suffixTable[i]
SuffixNodePtr * m_LMAprox_nodeTable; //stores LMAnode corresponding to m_LMAproximityTable entries
SuffixNode * m_LMA;
//interval in suffix array that corresponds to this node
treeint m_SA_start;
treeint m_SA_end;
};
treeintLarge SuffixNode::s_nodecount(1);
ostream & operator<< (ostream & os, SuffixNode * n)
{
return n->printNodeLabel(os);
}
class SuffixTree
{
public:
SuffixTree(const string & s)
: m_nodecount(0), m_string(s), m_maxMEMstrdepth(0)
{
m_root = new SuffixNode(0,0,NULL,NULL);
m_root->m_suffixTable[0] = m_root;
m_suffixArray = new treeint[s.length()];
}
treeint * m_suffixArray;
SuffixNode * m_root;
treeintLarge m_nodecount;
string m_string;
treeint m_maxMEMstrdepth; //max strdepth at any MEM node; only nodes whose strdepth \leq maxMEMstrdepth have suffix link tables and skipped MEM tables, each one is the size of their strdepth
//std::forward_list<SuffixNodeMark> nodesWithSuffixSkips;
SuffixNode ** nodesWithSuffixSkips;
treeint nextNodeWithSuffixSkips; //next position in array to fill
treeint numNodesWithSuffixSkips;
void resetMaxMemStrdepth()
{
m_maxMEMstrdepth = 0;
}
void dumpNode(SuffixNode * node)
{
int children = 0;
for (int i = 0; i < basecount; i++)
{
SuffixNode * child = node->m_children[i];
if (child)
{
children++;
cout << " " << node << "->" << child;
cout << " [minlen=" << child->len() << ", label=";
child->printEdgeLabel(cout, m_string) << "]" << endl;
dumpNode(child);
}
}
if (node->m_suffixTable[0])
{
cout << " " << node << " -> " << node->m_suffixTable[0]
<< " [style=dotted, constraint=false]" << endl;
}
if (children == 0)
{
cout << " " << node << " [shape=box, label=";
node->printLabel(cout, m_string) << "]" << endl;
}
else
{
cout << " " << node << " [label=";
node->printLabel(cout, m_string) << "]" << endl;
}
}
void dumpTree()
{
cerr << "Dumping tree" << endl;
cout << "digraph G {" << endl;
cout << " size=\"7.5,10\"" << endl;
cout << " center=true" << endl;
cout << " label=\"Suffix tree of \'" << m_string << "\' len:"
<< m_string.length()-1 << " nc:"
<< m_nodecount << "\"" << endl;
dumpNode(m_root);
cout << "}" << endl;
}
void dumpNodeText(ostream & out, SuffixNode * n, treeint depth)
{
for (int b = 0; b < basecount; b++)
{
if (n->m_children[b])
{
for (treeint i = 0; i < depth; i++)
{
out << " ";
}
out << " ";
out << n->m_children[b]->str(m_string) << endl;
dumpNodeText(out, n->m_children[b], depth+1);
}
}
}
void dumpTreeText(ostream & out)
{
out << "Suffix Tree len=" << m_string.length()-1 << endl;
out << "String: \"" << m_string << "\"" << endl;
out << "+" << endl;
dumpNodeText(out, m_root, 0);
}
void dumpTreeSorted(ostream & out, SuffixNode * node, const string & pathstring)
{
int c = 0;
string mystring = node->str(m_string);
string ps(pathstring);
ps.append(mystring);
for (int i = 0; i < basecount; i++)
{
if (node->m_children[i])
{
c++;
dumpTreeSorted(out, node->m_children[i], ps);
}
}
if (c == 0)
{
out << ps << endl;
}
}
SuffixNode * createNode(treeint s, treeint e, SuffixNode * p, SuffixNode * x)
{
SuffixNode * retval = new SuffixNode(s, e, p, x);
m_nodecount++;
return retval;
}
//error to call this function with k<1.
bool fillKhopSuffixLinkedList(SuffixNode * node, treeint k)
{
bool hasSuffixSkip = false;
//calculate num suffix links at node: log base 2 of strdepth at node
int nodeSuffixLinks = node->nodeNumSuffixLinks();
int children = 0;
for (int b = 0; b < basecount; b++)
if(node->m_children[b])
children++;
//SM added last clause 8/6/14
if(children == 0 || node->m_strdepth > m_maxMEMstrdepth || node->m_strdepth < Kmer_Len) //no tables to fill for this node
return hasSuffixSkip;
if(nodeSuffixLinks > k)
{
hasSuffixSkip = true;
if(node->m_suffixTable[k-1])
{
//calculate suffix link that is 2^k hops away from node
SuffixNode * kNeighbor = node->m_suffixTable[k-1]; //really the (k-1)Neighbor
int kNeighborSuffixLinks = kNeighbor->nodeNumSuffixLinks();
if(kNeighborSuffixLinks > k-1)
{
node->m_suffixTable[k] = kNeighbor->m_suffixTable[k-1];
if(kNeighbor->m_LMAproximityTable[k-1] < node->m_LMAproximityTable[k-1])
{
node->m_LMAproximityTable[k] = kNeighbor->m_LMAproximityTable[k-1];
node->m_LMAprox_nodeTable[k] = kNeighbor->m_LMAprox_nodeTable[k-1];
}
else
{
node->m_LMAproximityTable[k] = node->m_LMAproximityTable[k-1];
node->m_LMAprox_nodeTable[k] = node->m_LMAprox_nodeTable[k-1];
}
}
else
{
node->m_suffixTable[k] = NULL;
node->m_LMAproximityTable[k] = node->m_LMAproximityTable[0];
node->m_LMAprox_nodeTable[k] = node->m_LMAprox_nodeTable[0];
//return; //chidren won't be any deeper as far as strdepth //SM added 6/12/14
}
}
else //only applies to root
{
node->m_suffixTable[k] = NULL;
}
}
return hasSuffixSkip;
}
//error to call this function with k<1.
void fillKhopSuffixNode(SuffixNode * node, treeint k, bool fillLinkedList)
{
int nodeSuffixLinks = node->nodeNumSuffixLinks();
int children = 0;
for (int b = 0; b < basecount; b++)
if(node->m_children[b])
children++;
if(children == 0 || node->m_strdepth > m_maxMEMstrdepth ) //no tables to fill for leaf
return;
if(nodeSuffixLinks > k)
{
if(fillLinkedList)
{
//nodesWithSuffixSkips.push_front(node); //SM added 6/16/14
nodesWithSuffixSkips[nextNodeWithSuffixSkips++] = node;
}
numNodesWithSuffixSkips++;
if(node->m_suffixTable[k-1])
{
//calculate suffix link that is 2^k hops away from node
SuffixNode * kNeighbor = node->m_suffixTable[k-1]; //really the (k-1)Neighbor
int kNeighborSuffixLinks = kNeighbor->nodeNumSuffixLinks();
if(kNeighborSuffixLinks > k-1)
{
node->m_suffixTable[k] = kNeighbor->m_suffixTable[k-1];
if(kNeighbor->m_LMAproximityTable[k-1] < node->m_LMAproximityTable[k-1])
{
node->m_LMAproximityTable[k] = kNeighbor->m_LMAproximityTable[k-1];
node->m_LMAprox_nodeTable[k] = kNeighbor->m_LMAprox_nodeTable[k-1];
}
else
{
node->m_LMAproximityTable[k] = node->m_LMAproximityTable[k-1];
node->m_LMAprox_nodeTable[k] = node->m_LMAprox_nodeTable[k-1];
}
}
else
{
node->m_suffixTable[k] = NULL;
node->m_LMAproximityTable[k] = node->m_LMAproximityTable[0];
node->m_LMAprox_nodeTable[k] = node->m_LMAprox_nodeTable[0];
}
}
else //only applies to root
{
node->m_suffixTable[k] = NULL;
}
}
for (int b = 0; b < basecount; b++)
{
if(node->m_children[b])
{
fillKhopSuffixNode(node->m_children[b], k, fillLinkedList);
}
}
}
//recursively perform DFS to calc k-hop suffix links for each node
void fillKhopSuffix(treeint k, bool fillLinkedList)
{
if(fillLinkedList) //dynamically allocate array
{
nodesWithSuffixSkips = new SuffixNode*[m_nodecount];
nextNodeWithSuffixSkips = 0; //array position to fill next
}
fillKhopSuffixNode(m_root, k, fillLinkedList);
}
//this function uses linked list instead of multiple recursive DFSs over suffix tree
//this function populates the table m_SuffixLinkTable and associated skipped LMA tables
//entry i is the suffix link that is 2^i hops away
//traverse suffix tree floor(log n) = m_numSuffixLinks times to populate the table
void fillSuffixTableNonRecursively()
{
//timeval starttime;
//timeval endtime;
bool hasSuffixSkip;
treeint nextSNodeSetup; //where up to in array
treeint nextSNodeReplace; //where to copy to in array
treeint totalSNodesWithSuffixSkips; //where working array ends
//forward_list<SuffixNodeMark>::iterator it;
treeint seconds;
treeint microseconds;
double elapsed;
//begin with 1 since m_suffixLInkTable[0] is calcuated as part of Ukkonen's construction algorithm
unsigned int numSuffixLinks = (int)( ceil(log(m_maxMEMstrdepth) / log(2))); //floor(log(m_maxMEMstrdepth - Kmer_Len) / log(2));
cerr<<" numSuffixLinks="<<numSuffixLinks<<endl;
//fill first suffix skip entries by traversing entire tree and filling nodesWithSuffixSkips linked list with only nodes that have suffix skips
int k = 1;
numNodesWithSuffixSkips = 0;
//gettimeofday(&starttime, NULL);
fillKhopSuffix(k, true);
totalSNodesWithSuffixSkips = nextNodeWithSuffixSkips;
cerr<<" total nodes with suffix skips ="<<nextNodeWithSuffixSkips<<endl;
cerr<<" numNodesWithSuffixSkips = "<<numNodesWithSuffixSkips<<endl;
cerr<<" now using linked lists"<<endl;
for(k = 2; k < numSuffixLinks; k++)
{
numNodesWithSuffixSkips = 0;
//gettimeofday(&starttime, NULL);
nextSNodeReplace = 0;
for( nextSNodeSetup = 0; nextSNodeSetup<totalSNodesWithSuffixSkips; nextSNodeSetup++)
{
hasSuffixSkip = fillKhopSuffixLinkedList(nodesWithSuffixSkips[nextSNodeSetup], k);
if(hasSuffixSkip)
{
nodesWithSuffixSkips[nextSNodeReplace++] = nodesWithSuffixSkips[nextSNodeSetup];
numNodesWithSuffixSkips++;
}
//else
//nothing to do: loop advances nextSNodeSetup
}
totalSNodesWithSuffixSkips = numNodesWithSuffixSkips;
cerr<<" numNodesWithSuffixSkips = "<<numNodesWithSuffixSkips<<endl;
}
delete [] nodesWithSuffixSkips;
}
void preprocessLMAnode(SuffixNode * node, SuffixNode * markedNode)
{
if(node->m_MEM && node->m_strdepth >= Kmer_Len)
{
markedNode = node;
}
node->m_LMA = markedNode;
for (int b = 0; b < basecount; b++)
{
if(node->m_children[b])
{
preprocessLMAnode(node->m_children[b], markedNode);
}
}
}
//mark internal nodes that represent MEMs of length >=minMEM by setting m_MEM = true
void preprocessLMA( )
{
//recursively perform DFS on suffix tree and set LMA to point to lowest ancestor that is marked as a MEM
preprocessLMAnode(m_root, NULL);
}
//recursively unmark all MEM nodes
//can stop when reach a node that is long enough to be a MEM but isn't
void unmarkMEMnode(SuffixNode * node, int minMEM)
{
for (int b = 0; b < basecount; b++)
{
if(node->m_children[b])
unmarkMEMnode(node->m_children[b], minMEM);
}
node->m_MEM = false;
node->m_LMA = NULL;
node->deleteSuffixLinkTable();
}
void unmarkMEMnodes(int minMEM)
{
unmarkMEMnode(m_root, minMEM); //root cannot be marked node
}
//recursively mark node if it's a MEM, fill suffix array along the way with same DFS
void markMEMnode(SuffixNode * node, treeint minMEM, treeint * nextSAentry, bool setupSA)
{
int children = 0;
for (int b = 0; b < basecount; b++)
{
if(node->m_children[b])
{
children++;
}
}
if(setupSA)
{
//when visit a node the first time: set string depth in m_strdepth to reflect the length of the labels on the path from the root to this node's end
if(node == m_root) //set string depth = length of labels on path from root to this node's end
node->m_strdepth = 0;
else
node->m_strdepth = node->m_parent->m_strdepth + node->len();
//fill next suffix array element for this leaf
if(children == 0)
{
m_suffixArray[*nextSAentry] = node->m_start - node->m_parent->m_strdepth;
node->m_SA_start = *nextSAentry;
node->m_SA_end = *nextSAentry;
(*nextSAentry)++;
}
}
//perform DFS recursively
bool firstChild = true;
for (int b = 0; b < basecount; b++)
{
if(node->m_children[b])
{
markMEMnode(node->m_children[b], minMEM, nextSAentry, setupSA);
for(int c = 0; c < basecount; c++)
{
if(node->m_children[b]->m_prevChar[c])
node->m_prevChar[c] = true;
}
if(setupSA)
{
if(firstChild) //first child
{
node->m_SA_start = node->m_children[b]->m_SA_start;
node->m_SA_end = node->m_children[b]->m_SA_end;
firstChild = false;
}
else
{
if(node->m_children[b]->m_SA_start < node->m_SA_start)
node->m_SA_start = node->m_children[b]->m_SA_start;
if(node->m_children[b]->m_SA_end > node->m_SA_end)
node->m_SA_end = node->m_children[b]->m_SA_end;
}
}
}
}
//when visit node the second time: set m_prevChar to true for its children's values (its value if leaf). if more than one array element is set to true, set m_MEM to true if m_strdepth >= minMEM
if(node == m_root)
{
node->m_MEM = false;
}
else if(children == 0) //leaf, cannot be MEM
{
node->m_MEM = false;
treeint prevCharPos = node->m_end - node->m_strdepth;
//if(prevCharPos >= 0) //ignore position 1, treat as $ before string so will be considered maximal (can't extend beyond beginning of string)
{
char prevChar;
if(prevCharPos == 0)
prevChar = '$';
else
prevChar = m_string[prevCharPos];
int prevCharNum = b2i(prevChar);
node->m_prevChar[prevCharNum] = true;
}
}
else //internal node
{
if(node->m_strdepth >= minMEM) //see if this node is left maximal
{
int numPrevChars = 0;
for(int b = 0; b < basecount; b++)
{
if(node->m_prevChar[b])
numPrevChars++;
}
if(numPrevChars>=2) //can short-circuit OR if a child is a MEM
{
node->m_MEM = true;
numKmerLens++;
if(node->m_strdepth > m_maxMEMstrdepth)
m_maxMEMstrdepth = node->m_strdepth;
//when visit MEM node the second time, also propagate up the start positions from the children, adjusted to account for the length of this node
}
else
node->m_MEM = false;
}
}
}
//mark internal nodes that represent MEMs of length >=minMEM by setting m_MEM = true
//fill suffix array with same DFS in suffix tree
void markMEMnodes(int minMEM, bool setupSA)
{
cerr<<" marking MEM nodes in suffix tree of at least "<<minMEM<<" bp"<<endl;
treeint nextSAentry = 0;
//recursively perform DFS on suffix tree and mark internal nodes that are left maximal as MEMs
markMEMnode(m_root, minMEM, &nextSAentry, setupSA);
}
void createAuxTablesAtNode(SuffixNode * node)
{
SuffixNode * suffixLink = node->m_suffixTable[0];
if(node->m_suffixTable != NULL)
delete [] node->m_suffixTable;
int children = 0;
for (int b = 0; b < basecount; b++)
{
if(node->m_children[b])
{
children++;
}
}
if(node == m_root || children == 0 || node->m_strdepth > m_maxMEMstrdepth) //leaves don't need tables for suffix links and skipped LMA
{
node->createSuffixLinkTable(1, false);
}
else
{
//don't go through all suffix links until root. stop when strdepth of node is < Kmer_Len
int numSuffixLinks = node->nodeNumSuffixLinks();
if(numSuffixLinks==0)
numSuffixLinks = 1; //min table size
node->createSuffixLinkTable(numSuffixLinks, true);
}
node->m_suffixTable[0] = suffixLink;
//from here comes from fillFirstLMAproxEntry_Node
if(node != m_root && children > 0 && node->m_strdepth <= m_maxMEMstrdepth)
{
int minProx, thisProx, sProx;
SuffixNode * snode = node->m_suffixTable[0];
if(node->m_LMA)
thisProx = node->m_strdepth - node->m_LMA->m_strdepth; //this node's info is first entry
else
thisProx = node->m_strdepth;
if(snode)
{
if(snode->m_LMA)
{
sProx = snode->m_strdepth - snode->m_LMA->m_strdepth; //this node's info is first entry
}
else
{
sProx = snode->m_strdepth;
}
if(sProx < thisProx)
minProx = sProx;
else
minProx = thisProx;
}
else
{
minProx = thisProx;
}
node->m_LMAproximityTable[0] = minProx;
if(minProx == thisProx)
{
node->m_LMAprox_nodeTable[0] = node->LMA();
}
else
{
node->m_LMAprox_nodeTable[0] = snode->LMA();
}
} //to here
for (int b = 0; b < basecount; b++)
{
if(node->m_children[b])
createAuxTablesAtNode(node->m_children[b]);
}
}
void createAuxTables()
{
numNodesWithTable = 0; //reset variable so can use in limited output as tables are created
cerr<<" creating aux tables at nodes"<<endl;
//recursively perform DFS on suffix tree and create suffix link and skipped LMA tables when appropriate
createAuxTablesAtNode(m_root);
}
void buildUkkonen()
{
treeint len = m_string.length() - 1; // length of the string, not of the buffer
char base = m_string[1];
if (DEBUG)
{
cerr << "Building Ukkonen Tree for ";
cerr << "string of len: " << len << endl;
}
// Construct T1
SuffixNode * node = createNode(1, len, m_root, NULL);
m_root->m_children[b2i(base)] = node;
SuffixNode * firstleaf = node;
SuffixNode * lastleaf = node;
if (DEBUG)
{
cerr << "Phase 1 Child: ";
node->printLabel(cerr, m_string) << endl;
}
treeint startj = 2;
// phase i+1
for (int i = 2; i <= len; i++)
{
DEBUG = 0;
// Start at the last leaf created which will allow easy
// access to the node for startj
node = lastleaf;
treeint nodewalk = 0;
// Keep track of last internal nodes created in split so we can add suffix links
SuffixNode * splitnode = NULL;
if (!DOPHASETRICK)
{
startj = 2;
node = firstleaf;
}
if (DEBUG)
{
char next = m_string[i];
cerr << endl;
cerr << i << ".0 " << "Phase " << i << " adding " << next << " starting with " << startj << endl;
string beta = m_string.substr(1, i);
cerr << i << ".1" << " Extension 1: [implicit]" << endl;
}
for (treeint j = startj; j <= i; j++)
{
// Goal: Ensure S[j .. i] (beta) is in the suffix tree
// Precondition: S[j-1 .. i] (alpha) is in the suffix tree "near" node
// All Internal nodes have a suffix link
// Idea: 1) Remember where alpha is in the tree relative to node
// 2) Walk up the tree w bases until we get to a node with a suffix link.
// 3) Follow suffix link which shifts the path from S[j-1..i] to S[j..i]
// 4) Walk down tree in new location ensuring S[i-w .. i] is in tree
// Notes: 1) All internal nodes have a suffix link by next extension
// 2) Any time we walk up to root, have to check S[j..i]
// 3) Suffix [1..i] is always present so start extension j with 2
treeint betapos = i; // The first position in string we need to check in tree
if (DEBUG)
{