]> git.donarmstrong.com Git - mothur.git/blob - suffixtree.cpp
fixed some bugs
[mothur.git] / suffixtree.cpp
1 /*
2  *  suffixtree.cpp
3  *  
4  *
5  *  Created by Pat Schloss on 12/15/08.
6  *  Copyright 2008 Patrick D. Schloss. All rights reserved.
7  *
8  *      This is my half-assed attempt to implement a suffix tree.  This is a cobbled together algorithm using materials that
9  *      I found at http://marknelson.us/1996/08/01/suffix-trees/ and:
10  *
11  *              Ukkonen E. (1995). On-line construction of suffix trees. Algorithmica 14 (3): 249--260
12  *              Gusfield, Dan (1999). Algorithms on Strings, Trees and Sequences: Computer Science and Computational Biology. 
13  *                      USA: Cambridge University Press
14  *
15  *      The Ukkonen paper is the seminal paper describing the on-line method of constructing a suffix tree.
16  *
17  *      I have chosen to store the nodes of the tree as a vector of pointers to SuffixNode objects.  The root is stored at
18  *      nodeVector[0].  Each tree also stores the sequence name and the string that corresponds to the actual sequence. 
19  *      Finally, this class provides a way of counting the number of suffixes that are needed in one tree to generate a new
20  *      sequence (countSuffixes).  This method is used to determine similarity between sequences and was inspired by the
21  *      article and Perl source code provided at http://www.ddj.com/web-development/184416093.
22  *
23  */
24
25 #include "sequence.hpp"
26 #include "suffixnodes.hpp"
27 #include "suffixtree.hpp"
28
29
30 //********************************************************************************************************************
31
32 inline bool compareParents(SuffixNode* left, SuffixNode* right){//      this is necessary to print the tree and to sort the
33         return (left->getParentNode() < right->getParentNode());        //      nodes in order of their parent
34 }
35
36 //********************************************************************************************************************
37
38 SuffixTree::SuffixTree(){}
39
40 //********************************************************************************************************************
41
42 SuffixTree::~SuffixTree(){
43         for(int i=0;i<nodeVector.size();i++){   delete nodeVector[i];   }       
44 }
45
46 //********************************************************************************************************************
47
48 void SuffixTree::loadSequence(Sequence* seq){
49         nodeCounter = 0;                                                        //      initially there are 0 nodes in the tree
50         activeStartPosition = 0;
51         activeEndPosition = -1;                                         
52         seqName = seq->getName();
53         sequence = seq->convert2ints();
54         sequence += '5';                                                        //      this essentially concatenates a '$' to the end of the sequence to
55         int seqLength = sequence.length();                      //      make it a cononical suffix tree
56         
57         nodeVector.push_back(new SuffixBranch(-1, 0, -1));      //      enter the root of the suffix tree
58         
59         activeNode = root = 0;
60         string hold;
61         for(int i=0;i<seqLength;i++){
62                 addPrefix(i);                                                   //      step through the sequence adding each prefix
63         }
64 }
65
66 //********************************************************************************************************************
67
68 string SuffixTree::getSeqName() {
69         return seqName;         
70 }
71
72 //********************************************************************************************************************
73
74 void SuffixTree::print(){
75         vector<SuffixNode*> hold = nodeVector;
76         sort(hold.begin(), hold.end(), compareParents);
77         cout << "Address\t\tParent\tNode\tSuffix\tStartC\tEndC\tSuffix" << endl;
78         for(int i=1;i<=nodeCounter;i++){
79                 hold[i]->print(sequence, i);
80         }
81 }
82
83 //********************************************************************************************************************
84
85 int SuffixTree::countSuffixes(string compareSequence, int& minValue){   //      here we count the number of suffix parts 
86                                                                                                                         //      we need to rewrite a user supplied sequence.  if the 
87         int numSuffixes = 0;                                                                    //      count exceeds the supplied minValue, bail out.  The
88         int seqLength = compareSequence.length();                               //      time complexity should be O(L)
89         int position = 0;
90         
91         int presentNode = 0;
92         
93         while(position < seqLength){            //      while the position in the query sequence isn't at the end...
94                 
95                 if(numSuffixes > minValue)      {       return 1000000;         }       //      bail if the count gets too high
96                 
97                 int newNode = nodeVector[presentNode]->getChild(compareSequence[position]);     //      see if the current node has a
98                                                                                                                                 //      child that matches the next character in the query
99                 if(newNode == -1){                                                                              
100                         if(presentNode == 0){   position++;             }                       //      if not, go back to the root and increase the count
101                         numSuffixes++;                                                                          //      by one.
102                         presentNode = 0;
103                 }
104                 else{                                                                                                   //      if there is, move to that node and see how far down
105                         presentNode = newNode;                                                          //      it we can get
106                         
107                         for(int i=nodeVector[newNode]->getStartCharPos(); i<=nodeVector[newNode]->getEndCharPos(); i++){
108                                 if(compareSequence[position] == sequence[i]){
109                                         position++;                                                                     //      as long as the query and branch agree, keep going
110                                 }
111                                 else{
112                                         numSuffixes++;                                                          //      if there is a mismatch, increase the number of 
113                                         presentNode = 0;                                                        //      suffixes and go back to the root
114                                         break;
115                                 }
116                         }
117                 }
118                 //      if we get all the way through the node we'll go to the top of the while loop and find the child node
119                 //      that corresponds to what we are interested in           
120         }
121         numSuffixes--;                                                                                          //      the method puts an extra count on numSuffixes
122         
123         if(numSuffixes < minValue)      {       minValue = numSuffixes; }       //      if the count is less than the previous minValue,
124         return numSuffixes;                                                                                     //      change the value and return the number of suffixes
125         
126 }
127
128 //********************************************************************************************************************
129
130 void SuffixTree::canonize(){    //      if you have to ask how this works, you don't really want to know and this really
131                                                                 //      isn't the place to ask.
132         if ( isExplicit() == 0 ) {      //      if the node has no children...
133                 
134                 int tempNodeIndex = nodeVector[activeNode]->getChild(sequence[activeStartPosition]);
135                 SuffixNode* tempNode = nodeVector[tempNodeIndex];
136                 
137                 int span = tempNode->getEndCharPos() - tempNode->getStartCharPos();
138                 
139                 while ( span <= ( activeEndPosition - activeStartPosition ) ) {
140                         
141             activeStartPosition = activeStartPosition + span + 1;
142                         
143                         activeNode = tempNodeIndex;
144                         
145             if ( activeStartPosition <= activeEndPosition ) {
146                                 tempNodeIndex = nodeVector[tempNodeIndex]->getChild(sequence[activeStartPosition]);
147                                 tempNode = nodeVector[tempNodeIndex];
148                                 span = tempNode->getEndCharPos() - tempNode->getStartCharPos();
149             }
150                         
151         }
152     }
153 }
154
155 //********************************************************************************************************************
156
157 int SuffixTree::split(int nodeIndex, int position){     //      leaves stay leaves, etc, to split a leaf we make a new interior 
158                                                                                                         //      node and reconnect everything
159         SuffixNode* node = nodeVector[nodeIndex];                                       //      get the node that needs to be split
160         SuffixNode* parentNode = nodeVector[node->getParentNode()];     //      get it's parent node
161         
162         parentNode->eraseChild(sequence[node->getStartCharPos()]);      //      erase the present node from the registry of its parent
163         
164         nodeCounter++;
165         SuffixNode* newNode = new SuffixBranch(node->getParentNode(), node->getStartCharPos(), node->getStartCharPos() + activeEndPosition - activeStartPosition);      //      create a new node that will link the parent with the old child
166         parentNode->setChildren(sequence[newNode->getStartCharPos()], nodeCounter);//   give the parent the new child
167         nodeVector.push_back(newNode);
168         
169         node->setParentNode(nodeCounter);       //      give the original node the new node as its parent
170         newNode->setChildren(sequence[node->getStartCharPos() + activeEndPosition - activeStartPosition + 1], nodeIndex);
171         //      put the original node in the registry of the new node's children
172         newNode->setSuffixNode(activeNode);//link the new node with the old active node
173         
174         //      recalculate the startCharPosition of the outermost node
175         node->setStartCharPos(node->getStartCharPos() + activeEndPosition - activeStartPosition + 1 );
176         
177         return node->getParentNode();
178 }
179
180 //********************************************************************************************************************
181
182 void SuffixTree::makeSuffixLink(int& previous, int present){
183         
184 //      here we link the nodes that are suffixes of one another to rapidly speed through the tree
185         if ( previous > 0 ) {   nodeVector[previous]->setSuffixNode(present);   }
186         else                            {       /*      do nothing                                                              */      }
187         
188     previous = present;
189 }
190
191 //********************************************************************************************************************
192
193 void SuffixTree::addPrefix(int prefixPosition){
194         
195         int lastParentNode = -1;        //      we need to place a new prefix in the suffix tree
196         int parentNode = 0;
197         
198         while(1){
199                 
200                 parentNode = activeNode;
201                 
202                 if(isExplicit() == 1){  //      if the node is explicit (has kids), try to follow it down the branch if its there...
203                         if(nodeVector[activeNode]->getChild(sequence[prefixPosition]) != -1){   //      break out and get next prefix...
204                                 break;                                                                                          
205                         }
206                         else{                           //      ...otherwise continue, we'll need to make a new node later on...
207                         }
208                 }
209                 else{                                   //      if it's not explicit (no kids), read through and see if all of the chars agree...
210                         int tempNode = nodeVector[activeNode]->getChild(sequence[activeStartPosition]);
211                         int span = activeEndPosition - activeStartPosition;
212                         
213                         if(sequence[nodeVector[tempNode]->getStartCharPos() + span + 1] == sequence[prefixPosition] ){
214                                 break;                  //      if the existing suffix agrees with the new one, grab a new prefix...
215                         }
216                         else{
217                                 parentNode = split(tempNode, prefixPosition);   //      ... otherwise we need to split the node
218                         }
219                         
220                 }
221                 
222                 nodeCounter++;  //      we need to generate a new node here if the kid didn't exist, or we split a node
223                 SuffixNode* newSuffixLeaf = new SuffixLeaf(parentNode, prefixPosition, sequence.length()-1);
224                 nodeVector[parentNode]->setChildren(sequence[prefixPosition], nodeCounter);
225                 nodeVector.push_back(newSuffixLeaf);
226                 
227                 makeSuffixLink( lastParentNode, parentNode );           //      make a suffix link for the parent node
228                 
229                 if(nodeVector[activeNode]->getParentNode() == -1){      //      move along the start position for the tree
230             activeStartPosition++;
231         } 
232                 else {
233             activeNode = nodeVector[activeNode]->getSuffixNode();
234                 }
235                 canonize();                                                                                     //      frankly, i'm not entirely clear on what canonize does.
236         }
237         
238         makeSuffixLink( lastParentNode, parentNode );
239         activeEndPosition++;                                                                    //      move along the end position for the tree
240         
241         canonize();                                                                                             //      frankly, i'm not entirely clear on what canonize does.
242         
243 }
244
245 //********************************************************************************************************************
246