]> git.donarmstrong.com Git - mothur.git/blobdiff - phylotree.cpp
finished get.relabund command
[mothur.git] / phylotree.cpp
index bfac59c5a694db4ec644ffedace6a02f920f2067..d6c740ae791b494ea64951a03405b38535593152 100644 (file)
@@ -32,6 +32,8 @@ PhyloTree::PhyloTree(ifstream& in, string filename){
        try {
                m = MothurOut::getInstance();
                calcTotals = false;
+               numNodes = 0;
+               numSeqs = 0;
                
                #ifdef USE_MPI
                        MPI_File inMPI;
@@ -195,14 +197,22 @@ PhyloTree::PhyloTree(string tfile){
 
 /**************************************************************************************************/
 
-string PhyloTree::getNextTaxon(string& heirarchy){
+string PhyloTree::getNextTaxon(string& heirarchy, string seqname){
        try {
                string currentLevel = "";
                if(heirarchy != ""){
                        int pos = heirarchy.find_first_of(';');
-                       currentLevel=heirarchy.substr(0,pos);
-                       if (pos != (heirarchy.length()-1)) {  heirarchy=heirarchy.substr(pos+1);  }
-                       else { heirarchy = ""; }
+                       
+                       if (pos == -1) { //you can't find another ;
+                               currentLevel = heirarchy;
+                               heirarchy = "";
+                               m->mothurOut(seqname + " is missing a ;, please check for other errors."); m->mothurOutEndLine();
+                       }else{
+                               currentLevel=heirarchy.substr(0,pos);
+                               if (pos != (heirarchy.length()-1)) {  heirarchy=heirarchy.substr(pos+1);  }
+                               else { heirarchy = ""; }
+                       }
+                       
                }
                return currentLevel;
        }
@@ -216,6 +226,7 @@ string PhyloTree::getNextTaxon(string& heirarchy){
 
 int PhyloTree::addSeqToTree(string seqName, string seqTaxonomy){
        try {
+                       
                numSeqs++;
                
                map<string, int>::iterator childPointer;
@@ -225,16 +236,18 @@ int PhyloTree::addSeqToTree(string seqName, string seqTaxonomy){
                
                tree[0].accessions.push_back(seqName);
                string taxon;// = getNextTaxon(seqTaxonomy);
-               
+       
                while(seqTaxonomy != ""){
                        
                        level++;
-                       
+               
                        if (m->control_pressed) { return 0; }
                        
                        //somehow the parent is getting one too many accnos
                        //use print to reassign the taxa id
-                       taxon = getNextTaxon(seqTaxonomy);
+                       taxon = getNextTaxon(seqTaxonomy, seqName);
+                       
+                       if (taxon == "") {  m->mothurOut(seqName + " has an error in the taxonomy.  This may be due to a ;;"); m->mothurOutEndLine(); if (currentNode != 0) {  uniqueTaxonomies[currentNode] = currentNode; } break;  }
                        
                        childPointer = tree[currentNode].children.find(taxon);
                        
@@ -249,21 +262,13 @@ int PhyloTree::addSeqToTree(string seqName, string seqTaxonomy){
                                tree[currentNode].children[taxon] = numNodes-1;
                                tree[numNodes-1].parent = currentNode;
                                
-                               //                      int numChildren = tree[currentNode].children.size();
-                               //                      string heirarchyID = tree[currentNode].heirarchyID;
-                               //                      tree[currentNode].accessions.push_back(seqName);
-                               
                                currentNode = tree[currentNode].children[taxon];
                                tree[currentNode].accessions.push_back(seqName);
                                name2Taxonomy[seqName] = currentNode;
-                               //                      tree[currentNode].level = level;
-                               //                      tree[currentNode].childNumber = numChildren;
-                               //                      tree[currentNode].heirarchyID = heirarchyID + '.' + toString(tree[currentNode].childNumber);
                        }
-               
+       
                        if (seqTaxonomy == "") {   uniqueTaxonomies[currentNode] = currentNode; }
                }
-
        }
        catch(exception& e) {
                m->errorOut(e, "PhyloTree", "addSeqToTree");
@@ -520,7 +525,6 @@ void PhyloTree::printTreeNodes(string treefilename) {
                        for (it2=uniqueTaxonomies.begin(); it2!=uniqueTaxonomies.end(); it2++) {  outTree << it2->first << '\t' << tree[it2->first].accessions.size() << endl;  }
                        outTree << endl;
                        
-                       
                        outTree.close();
                
                #ifdef USE_MPI
@@ -535,6 +539,93 @@ void PhyloTree::printTreeNodes(string treefilename) {
        }
 }
 /**************************************************************************************************/
+TaxNode PhyloTree::get(int i ){
+       try {
+               if (i < tree.size()) {  return tree[i];  }
+               else {  cout << i << '\t' << tree.size() << endl ; m->mothurOut("Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1); }
+       }
+       catch(exception& e) {
+               m->errorOut(e, "PhyloTree", "get");
+               exit(1);
+       }
+}
+/**************************************************************************************************/
+TaxNode PhyloTree::get(string seqName){
+       try {
+               map<string, int>::iterator itFind = name2Taxonomy.find(seqName);
+       
+               if (itFind != name2Taxonomy.end()) {  return tree[name2Taxonomy[seqName]];  }
+               else { m->mothurOut("Cannot find " + seqName + ". Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1);}
+       }
+       catch(exception& e) {
+               m->errorOut(e, "PhyloTree", "get");
+               exit(1);
+       }
+}
+/**************************************************************************************************/
+string PhyloTree::getName(int i ){
+       try {
+               if (i < tree.size()) {  return tree[i].name;     }
+               else { m->mothurOut("Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1); }
+       }
+       catch(exception& e) {
+               m->errorOut(e, "PhyloTree", "get");
+               exit(1);
+       }
+}
+/**************************************************************************************************/
+int PhyloTree::getIndex(string seqName){
+       try {
+               map<string, int>::iterator itFind = name2Taxonomy.find(seqName);
+       
+               if (itFind != name2Taxonomy.end()) {  return name2Taxonomy[seqName];  }
+               else { m->mothurOut("Cannot find " + seqName + ". Mismatch with taxonomy and template files. Cannot continue."); m->mothurOutEndLine(); exit(1);}
+       }
+       catch(exception& e) {
+               m->errorOut(e, "PhyloTree", "get");
+               exit(1);
+       }
+}
+/**************************************************************************************************/
+bool PhyloTree::ErrorCheck(vector<string> templateFileNames){
+       try {
+       
+               bool okay = true;
+               
+               map<string, int>::iterator itFind;
+               map<string, int> taxonomyFileNames = name2Taxonomy;
+               
+               for (int i = 0; i < templateFileNames.size(); i++) {
+                       itFind = taxonomyFileNames.find(templateFileNames[i]);
+                       
+                       if (itFind != taxonomyFileNames.end()) { //found it so erase it
+                               taxonomyFileNames.erase(itFind);
+                       }else {
+                               m->mothurOut(templateFileNames[i] + " is in your template file and is not in your taxonomy file. Please correct."); m->mothurOutEndLine();
+                               okay = false;
+                       }
+                       
+                       templateFileNames.erase(templateFileNames.begin()+i);
+                       i--;
+               }
+               
+               if (taxonomyFileNames.size() > 0) { //there are names in tax file that are not in template
+                       okay = false;
+                       
+                       for (itFind = taxonomyFileNames.begin(); itFind != taxonomyFileNames.end(); itFind++) {
+                               m->mothurOut(itFind->first + " is in your taxonomy file and is not in your template file. Please correct."); m->mothurOutEndLine();
+                       }
+               }
+               
+               return okay;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "PhyloTree", "ErrorCheck");
+               exit(1);
+       }
+}
+/**************************************************************************************************/
+