]> git.donarmstrong.com Git - mothur.git/blobdiff - sharedchao1.cpp
changed random forest output filename
[mothur.git] / sharedchao1.cpp
index c3388fd621d8ec6e33aac32891b54831a524417d..8d47ad2fca1d307bd150a985224b1099e492626a 100644 (file)
@@ -12,8 +12,7 @@
 /***********************************************************************/
 EstOutput SharedChao1::getValues(vector<SharedRAbundVector*> shared){
        try {
-               data.resize(1,0);
-               
+               data.resize(1,0);               
                vector<int> temp; 
                int numGroups = shared.size();
                float Chao = 0.0; float leftvalue, rightvalue;
@@ -28,10 +27,9 @@ EstOutput SharedChao1::getValues(vector<SharedRAbundVector*> shared){
                // the coeffient is 2.  Note: we only set the coeffient in f2 values.
                
                //create and initialize trees to 0.
-               initialTree(numGroups); 
+               initialTree(numGroups);
                
-               //loop through vectors calculating the f11, f1A, f2A, f1B, f2B, S12 values
-               for (int i = 0; i < shared[0]->size(); i++) {
+               for (int i = 0; i < shared[0]->getNumBins(); i++) {
                        //get bin values and calc shared 
                        bool sharedByAll = true;
                        temp.clear();
@@ -42,40 +40,44 @@ EstOutput SharedChao1::getValues(vector<SharedRAbundVector*> shared){
                        
                        //they are shared
                        if (sharedByAll == true) { 
-                                
-                               // cout << "temp = ";
-                               // for (int h = 0; h < temp.size(); h++) { cout << temp[h] << " "; } cout << endl;
                                //find f1 and f2values
                                updateTree(temp);
                        }
                }
 
-                       
-               //cout << "Entering " << endl;
+               
                //calculate chao1, (numleaves-1) because numleaves contains the ++ values.
-               for (int i = 0; i < numLeaves; i++) {
-                       //divide by zero error
-                       if (f2leaves[i]->lvalue == 0) { f2leaves[i]->lvalue++; }
-                       if (f2leaves[i]->rvalue == 0) { f2leaves[i]->rvalue++; }
-                       
-                       //cout << "f2 leaves ";
-                       //cout << f2leaves[i]->lvalue << f2leaves[i]->rvalue << endl;
-                       
-               //      cout << "f2 leaf coef ";
-                       //cout << f2leaves[i]->lcoef << '\t' << f2leaves[i]->rcoef << endl;
-                       
-               //      cout << "f1 leaves ";
-               //      cout << f1leaves[i]->lvalue << f1leaves[i]->rvalue << endl;
-                       
+               bool bias = false;
+               for(int i=0;i<numLeaves;i++){
+                       if (f2leaves[i]->lvalue == 0 || f2leaves[i]->rvalue == 0) { bias = true;}// break;}
+               }
+
+               if(bias){
+                       for (int i = 0; i < numLeaves; i++) {
+                               
+                               leftvalue = (float)(f1leaves[i]->lvalue * (f1leaves[i]->lvalue - 1)) / (float)((pow(2, (float)f2leaves[i]->lcoef)) * (f2leaves[i]->lvalue + 1));
+                               if (i != (numLeaves-1)) {
+                                       rightvalue = (float)(f1leaves[i]->rvalue * (f1leaves[i]->rvalue - 1)) / (float)((pow(2, (float)f2leaves[i]->rcoef)) * (f2leaves[i]->rvalue + 1));
+                               }else{
+                                       //add in sobs
+                                       rightvalue = (float)(f1leaves[i]->rvalue);
+                               }
+                               Chao += leftvalue + rightvalue;
+                       }
+               }
+               else{
                        
-                       leftvalue = (float)(f1leaves[i]->lvalue * f1leaves[i]->lvalue) / (float)((pow(2, (float)f2leaves[i]->lcoef)) * f2leaves[i]->lvalue);
-                       if (i != (numLeaves-1)) {
-                               rightvalue = (float)(f1leaves[i]->rvalue * f1leaves[i]->rvalue) / (float)((pow(2, (float)f2leaves[i]->rcoef)) * f2leaves[i]->rvalue);
-                       }else{
-                               rightvalue = (float)(f1leaves[i]->rvalue);
+                       for (int i = 0; i < numLeaves; i++) {
+                               
+                               leftvalue = (float)(f1leaves[i]->lvalue * f1leaves[i]->lvalue) / (float)((pow(2, (float)f2leaves[i]->lcoef)) * f2leaves[i]->lvalue);
+                               if (i != (numLeaves-1)) {
+                                       rightvalue = (float)(f1leaves[i]->rvalue * f1leaves[i]->rvalue) / (float)((pow(2, (float)f2leaves[i]->rcoef)) * f2leaves[i]->rvalue);
+                               }else{
+                                       //add in sobs
+                                       rightvalue = (float)(f1leaves[i]->rvalue);
+                               }
+                               Chao += leftvalue + rightvalue;
                        }
-                       //cout << "left = " << leftvalue << " right = " << rightvalue << endl;
-                       Chao += leftvalue + rightvalue;
                }
                
                for (int i = 0; i < numNodes; i++) {
@@ -83,18 +85,14 @@ EstOutput SharedChao1::getValues(vector<SharedRAbundVector*> shared){
                        delete f2leaves[i];
                }
                
-       //      cout << "exiting " << endl;
+               
                data[0] = Chao;
                return data;
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               m->errorOut(e, "SharedChao1", "getValues");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the SharedChao1 class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
 }
 
 /***********************************************************************/
@@ -109,39 +107,30 @@ void SharedChao1::initialTree(int n) {
                
                f1leaves.resize(numNodes);
                f2leaves.resize(numNodes);
-               
+       
                //initialize leaf values
                for (int i = 0; i < numLeaves; i++) {
-                       f1leaves[i] = new IntNode;
-                       f1leaves[i]->lvalue = 0;
-                       f1leaves[i]->rvalue = 0;
-                       f1leaves[i]->left = NULL;
-                       f1leaves[i]->right = NULL;
-                       
-                       f2leaves[i] = new IntNode;
-                       f2leaves[i]->lvalue = 0;
-                       f2leaves[i]->rvalue = 0;
-                       f2leaves[i]->left = NULL;
-                       f2leaves[i]->right = NULL;
+                       f1leaves[i] = new IntNode(0, 0, NULL, NULL);
+                       f2leaves[i] = new IntNode(0, 0, NULL, NULL);
                }
                
                //set pointers to children
                for (int j = numLeaves; j < numNodes; j++) {
-                       f1leaves[j] = new IntNode;
+                       f1leaves[j] = new IntNode();
                        f1leaves[j]->left = f1leaves[countleft];
                        f1leaves[j]->right = f1leaves[countright];
                                                
-                       f2leaves[j] = new IntNode;
+                       f2leaves[j] = new IntNode();
                        f2leaves[j]->left = f2leaves[countleft];
                        f2leaves[j]->right =f2leaves[countright];
-                                               
+                       
                        countleft = countleft + 2;
                        countright = countright + 2;
                }
                
                //point to root
                f1root = f1leaves[numNodes-1];
-               
+       
                //point to root
                f2root = f2leaves[numNodes-1];
                
@@ -149,13 +138,10 @@ void SharedChao1::initialTree(int n) {
                setCoef(f2root, 0);
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function initialTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               if ((toString(e.what()) == "vector::_M_fill_insert") || (toString(e.what()) == "St9bad_alloc")) { m->mothurOut("You are using " + toString(n) + " groups which creates 2^" + toString(n+1) + " nodes. Try reducing the number of groups you selected. "); m->mothurOutEndLine(); exit(1); }
+               m->errorOut(e, "SharedChao1", "initialTree");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the SharedChao1 class function initialTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
 }
 
 /***********************************************************************/
@@ -166,13 +152,9 @@ void SharedChao1::updateTree(vector<int> bin) {
                updateBranchf2(f2root, bin, 0); 
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function updateTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               m->errorOut(e, "SharedChao1", "updateTree");
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the SharedChao1 class function updateTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
 }
 
 /***********************************************************************/
@@ -192,13 +174,9 @@ void SharedChao1::updateBranchf1(IntNode* node, vector<int> bin, int index) {
                }
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function updateBranchf1. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               m->errorOut(e, "SharedChao1", "updateBranchf1");                
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the SharedChao1 class function updateBranchf1. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
 }
 
 /***********************************************************************/
@@ -218,13 +196,9 @@ void SharedChao1::updateBranchf2(IntNode* node, vector<int> bin, int index) {
                }
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function updateBranchf2. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               m->errorOut(e, "SharedChao1", "updateBranchf2");        
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the SharedChao1 class function updateBranchf2. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
 }
 
 /***********************************************************************/
@@ -239,23 +213,19 @@ void SharedChao1::setCoef(IntNode* node, int coef) {
                }
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function getCoef. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               m->errorOut(e, "SharedChao1", "setCoef");       
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the SharedChao1 class function getCoef. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }       
 }
 
 /***********************************************************************/
 //for debugging purposes
 void SharedChao1::printTree() {
        
-       cout << "F1 leaves" << endl;
+       m->mothurOut("F1 leaves"); m->mothurOutEndLine();
        printBranch(f1root);
        
-       cout << "F2 leaves" << endl;
+       m->mothurOut("F2 leaves"); m->mothurOutEndLine();
        printBranch(f2root);
 
 
@@ -269,19 +239,15 @@ void SharedChao1::printBranch(IntNode* node) {
                        printBranch(node->left);
                        printBranch(node->right);
                }else { //you are a leaf
-                       cout << node->lvalue << endl;
-                       cout << node->rvalue << endl;
+                       m->mothurOut(toString(node->lvalue)); m->mothurOutEndLine();
+                       m->mothurOut(toString(node->rvalue)); m->mothurOutEndLine();
                }
                
        }
        catch(exception& e) {
-               cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
+               m->errorOut(e, "SharedChao1", "printBranch");   
                exit(1);
        }
-       catch(...) {
-               cout << "An unknown error has occurred in the Tree class function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
-               exit(1);
-       }               
 }
 
 /*****************************************************************/