]> git.donarmstrong.com Git - mothur.git/blobdiff - tree.cpp
pat's differences before v.1.8
[mothur.git] / tree.cpp
index ce3de379d05d5eca8c991d2e93e64060377a2304..ad80b2bafe98c048fb44e8e2683aae962da08894 100644 (file)
--- a/tree.cpp
+++ b/tree.cpp
@@ -16,7 +16,7 @@ Tree::Tree() {
                globaldata = GlobalData::getInstance();
                
                if (globaldata->runParse == true) {  parseTreeFile();  globaldata->runParse = false;  }
-               
+//for(int i = 0; i <   globaldata->Treenames.size(); i++) { cout << i << '\t' << globaldata->Treenames[i] << endl;  }  
                numLeaves = globaldata->Treenames.size();
                numNodes = 2*numLeaves - 1;
                
@@ -159,11 +159,10 @@ map<string, int> Tree::mergeGroups(int i) {
                // this is true if right child had a greater parsimony for a certain group
                if(maxPars > 1){
                        //erase all the groups that are only 1 because you found something with 2.
-                       for(it=parsimony.begin();it!=parsimony.end();it++){
+                       for(it=parsimony.begin();it!=parsimony.end();){
                                if(it->second == 1){
-                                       parsimony.erase(it->first);
-//                                     it--;
-                               }
+                                       parsimony.erase(it++);
+                               }else { it++; }
                        }
                        //set one remaining groups to 1
                        //so with our above example p[white] = 2 would be left and it would become p[white] = 1
@@ -193,13 +192,17 @@ map<string, int> Tree::mergeUserGroups(int i, vector<string> g) {
                int rc = tree[i].getRChild();
                
                //loop through nodes groups removing the ones the user doesn't want
-               for (it = tree[lc].pGroups.begin(); it != tree[lc].pGroups.end(); it++) {
-                       if (inUsersGroups(it->first, g) != true) { tree[lc].pGroups.erase(it->first); }
+               for(it=tree[lc].pGroups.begin();it!=tree[lc].pGroups.end();){
+                               if (inUsersGroups(it->first, g) != true) {
+                                       tree[lc].pGroups.erase(it++);
+                               }else { it++; }
                }
-               
+
                //loop through nodes groups removing the ones the user doesn't want
-               for (it = tree[rc].pGroups.begin(); it != tree[rc].pGroups.end(); it++) {
-                       if (inUsersGroups(it->first, g) != true) { tree[rc].pGroups.erase(it->first); }
+               for(it=tree[rc].pGroups.begin();it!=tree[rc].pGroups.end();){
+                               if (inUsersGroups(it->first, g) != true) {
+                                       tree[rc].pGroups.erase(it++);
+                               }else { it++; }
                }
 
                //set parsimony groups to left child
@@ -224,11 +227,12 @@ map<string, int> Tree::mergeUserGroups(int i, vector<string> g) {
                // this is true if right child had a greater parsimony for a certain group
                if(maxPars > 1){
                        //erase all the groups that are only 1 because you found something with 2.
-                       for(it=parsimony.begin();it!=parsimony.end();it++){
+                       for(it=parsimony.begin();it!=parsimony.end();){
                                if(it->second == 1){
-                                       parsimony.erase(it->first);
-                               }
+                                       parsimony.erase(it++);
+                               }else { it++; }
                        }
+
                        for(it=parsimony.begin();it!=parsimony.end();it++){
                                parsimony[it->first] = 1;
                        }
@@ -395,7 +399,7 @@ void Tree::randomTopology() {
                                        escape = 1;
                                }               
                        }
-               
+       
                        tree[i].setChildren(rnd_index1,rnd_index2);
                        tree[i].setParent(-1);
                        tree[rnd_index1].setParent(i);
@@ -529,7 +533,7 @@ void Tree::printTree() {
 }
 
 /*****************************************************************/
-
+//this code is a mess and should be rethought...-slw
 void Tree::parseTreeFile() {
        
        //only takes names from the first tree and assumes that all trees use the same names.
@@ -539,6 +543,7 @@ void Tree::parseTreeFile() {
                openInputFile(filename, filehandle);
                int c, comment;
                comment = 0;
+               int done = 1;
                
                //ifyou are not a nexus file 
                if((c = filehandle.peek()) != '#') {  
@@ -555,7 +560,8 @@ void Tree::parseTreeFile() {
                                        filehandle.get();
                                }
 
-                               readTreeString(filehandle); 
+                               done = readTreeString(filehandle); 
+                               if (done == 0) { break; }
                        }
                //ifyou are a nexus file
                }else if((c = filehandle.peek()) == '#') {
@@ -570,17 +576,20 @@ void Tree::parseTreeFile() {
                                        comment = 0;
                                }
                                filehandle >> holder; 
-       
-                               //ifthere is no translate then you must read tree string otherwise use translate to get names
-                               if(holder == "tree" && comment != 1){   
+
+                               //if there is no translate then you must read tree string otherwise use translate to get names
+                               if((holder == "tree") && (comment != 1)){       
                                        //pass over the "tree rep.6878900 = "
                                        while (((c = filehandle.get()) != '(') && ((c = filehandle.peek()) != EOF)) {;}
 
                                        if(c == EOF) { break; }
                                        filehandle.putback(c);  //put back first ( of tree.
-                                       readTreeString(filehandle);     
+                                       done = readTreeString(filehandle);
+       
                                        break;
                                }
+                       
+                               if (done == 0) { break;  }
                        }
                        
                        //use nexus translation rather than parsing tree to save time
@@ -610,10 +619,10 @@ void Tree::parseTreeFile() {
 /*******************************************************/
 
 /*******************************************************/
-void Tree::readTreeString(ifstream& filehandle)        {
+int Tree::readTreeString(ifstream& filehandle) {
        try {
                int c;
-               string name;// k;
+               string name;  //, k
                
                while((c = filehandle.peek()) != ';') { 
 //k = c;
@@ -624,7 +633,8 @@ void Tree::readTreeString(ifstream& filehandle)     {
                                while((c!=',') && (c != -1) && (c!= ':') && (c!=';')){ c=filehandle.get(); }
                                filehandle.putback(c);
                        }
-                       if(c == ';') { break; }
+                       if(c == ';') { return 0; }
+                       if(c == -1) { return 0; }
                        //if you are a name
                        if((c != '(') && (c != ')') && (c != ',') && (c != ':') && (c != '\n') && (c != '\t') && (c != 32)) { //32 is space
                                name = "";
@@ -648,19 +658,22 @@ void Tree::readTreeString(ifstream& filehandle)   {
                        if(c  == ':') { //read until you reach the end of the branch length
                                while ((c != '(') && (c != ')') && (c != ',') && (c != ';') && (c != '\n') && (c != '\t') && (c != 32)) {
                                        c = filehandle.get();
-                                       //k = c;
+       //k = c;
        //cout << " in branch while " << k << endl;
                                }
                                filehandle.putback(c);
                        }
-                       
+               
                        c = filehandle.get();
-                       if(c == ';') { break; }
+//k = c;
+       //cout << " here after get " << k << endl;
+                       if(c == ';') { return 0; }
                        if(c == ')') { filehandle.putback(c); }
-       //              k = c;
+       //k = c;
 //cout << k << endl;
 
                }
+               return 0;
        }
        catch(exception& e) {
                errorOut(e, "Tree", "readTreeString");