X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=suffixtree.cpp;h=fd18109513bea9f59590e16a1c705128f61ca43e;hb=2bb9267aa4b4ecdf8488b06605cc9f3f36fa4332;hp=0dbe2c0e41e6bd68df024f411c0883bdd306fe8b;hpb=02909d6cae9963ba00dc746969a370fa8ca934fc;p=mothur.git diff --git a/suffixtree.cpp b/suffixtree.cpp index 0dbe2c0..fd18109 100644 --- a/suffixtree.cpp +++ b/suffixtree.cpp @@ -33,9 +33,28 @@ inline bool compareParents(SuffixNode* left, SuffixNode* right){// this is neces return (left->getParentNode() < right->getParentNode()); // nodes in order of their parent } +//******************************************************************************************************************** + +SuffixTree::SuffixTree(const SuffixTree& st) : root(st.root), activeEndPosition(st.activeEndPosition), activeStartPosition(st.activeStartPosition), activeNode(st.activeNode), + nodeCounter(st.nodeCounter), seqName(st.seqName), sequence(st.sequence) { + try { + m = MothurOut::getInstance(); + + for (int i = 0; i < st.nodeVector.size(); i++) { + SuffixNode* temp = new SuffixBranch(*((SuffixBranch*)st.nodeVector[i])); + nodeVector.push_back(temp); + } + + + }catch(exception& e) { + m->errorOut(e, "SuffixTree", "SuffixTree"); + exit(1); + } +} + //******************************************************************************************************************** -SuffixTree::SuffixTree(){} +SuffixTree::SuffixTree(){ m = MothurOut::getInstance(); } //******************************************************************************************************************** @@ -75,7 +94,7 @@ string SuffixTree::getSeqName() { void SuffixTree::print(){ vector hold = nodeVector; sort(hold.begin(), hold.end(), compareParents); - cout << "Address\t\tParent\tNode\tSuffix\tStartC\tEndC\tSuffix" << endl; + m->mothurOut("Address\t\tParent\tNode\tSuffix\tStartC\tEndC\tSuffix"); m->mothurOutEndLine(); for(int i=1;i<=nodeCounter;i++){ hold[i]->print(sequence, i); } @@ -125,7 +144,46 @@ int SuffixTree::countSuffixes(string compareSequence, int& minValue){ // here we return numSuffixes; // change the value and return the number of suffixes } +//******************************************************************************************************************** +int SuffixTree::countSuffixes(string compareSequence){ // here we count the number of suffix parts + // we need to rewrite a user supplied sequence. if the + int numSuffixes = 0; // count exceeds the supplied minValue, bail out. The + int seqLength = compareSequence.length(); // time complexity should be O(L) + int position = 0; + + int presentNode = 0; + + while(position < seqLength){ // while the position in the query sequence isn't at the end... + + int newNode = nodeVector[presentNode]->getChild(compareSequence[position]); // see if the current node has a + // child that matches the next character in the query + if(newNode == -1){ + if(presentNode == 0){ position++; } // if not, go back to the root and increase the count + numSuffixes++; // by one. + presentNode = 0; + } + else{ // if there is, move to that node and see how far down + presentNode = newNode; // it we can get + + for(int i=nodeVector[newNode]->getStartCharPos(); i<=nodeVector[newNode]->getEndCharPos(); i++){ + if(compareSequence[position] == sequence[i]){ + position++; // as long as the query and branch agree, keep going + } + else{ + numSuffixes++; // if there is a mismatch, increase the number of + presentNode = 0; // suffixes and go back to the root + break; + } + } + } + // if we get all the way through the node we'll go to the top of the while loop and find the child node + // that corresponds to what we are interested in + } + numSuffixes--; // the method puts an extra count on numSuffixes + + return numSuffixes; // change the value and return the number of suffixes +} //******************************************************************************************************************** void SuffixTree::canonize(){ // if you have to ask how this works, you don't really want to know and this really