5 * Created by Sarah Westcott on 1/8/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "sharedchao1.h"
12 /***********************************************************************/
13 EstOutput SharedChao1::getValues(vector<SharedRAbundVector*> shared){
17 int numGroups = shared.size();
18 float Chao = 0.0; float leftvalue, rightvalue;
20 // IntNode is defined in mothur.h
21 // The tree used here is a binary tree used to represent the f1+++, f+1++, f++1+, f+++1, f11++, f1+1+...
22 // combinations required to solve the chao estimator equation for any number of groups. Conceptually, think
23 // of each node as having a 1 and a + value, or for f2 values a 2 and a + value, and 2 pointers to intnodes, and 2 coeffient values.
24 // The coeffient value is how many times you chose branch 1 to get to that fvalue.
25 // If you choose left you are selecting the 1 or 2 value and right means the + value. For instance, to find
26 // the number of bins that have f1+1+ you would start at the root, go left, right, left, and select the rightvalue.
27 // the coeffient is 2. Note: we only set the coeffient in f2 values.
29 //create and initialize trees to 0.
30 initialTree(numGroups);
32 //loop through vectors calculating the f11, f1A, f2A, f1B, f2B, S12 values
33 for (int i = 0; i < shared[0]->size(); i++) {
34 //get bin values and calc shared
35 bool sharedByAll = true;
37 for (int j = 0; j < numGroups; j++) {
38 temp.push_back(shared[j]->getAbundance(i));
39 if (temp[j] == 0) { sharedByAll = false; }
43 if (sharedByAll == true) {
45 // for (int h = 0; h < temp.size(); h++) { cout << temp[h] << " "; } cout << endl;
46 //find f1 and f2values
52 //cout << "Entering " << endl;
53 //calculate chao1, (numleaves-1) because numleaves contains the ++ values.
55 for(int i=0;i<numLeaves;i++){
56 if (f2leaves[i]->lvalue == 0) { bias = true;}// break;}
60 for (int i = 0; i < numLeaves; i++) {
62 leftvalue = (float)(f1leaves[i]->lvalue * (f1leaves[i]->lvalue - 1)) / (float)((pow(2, (float)f2leaves[i]->lcoef)) * (f2leaves[i]->lvalue + 1));
63 if (i != (numLeaves-1)) {
64 rightvalue = (float)(f1leaves[i]->rvalue * (f1leaves[i]->rvalue - 1)) / (float)((pow(2, (float)f2leaves[i]->rcoef)) * (f2leaves[i]->rvalue + 1));
66 rightvalue = (float)(f1leaves[i]->rvalue);
68 Chao += leftvalue + rightvalue;
73 for (int i = 0; i < numLeaves; i++) {
75 leftvalue = (float)(f1leaves[i]->lvalue * f1leaves[i]->lvalue) / (float)((pow(2, (float)f2leaves[i]->lcoef)) * f2leaves[i]->lvalue);
76 if (i != (numLeaves-1)) {
77 rightvalue = (float)(f1leaves[i]->rvalue * f1leaves[i]->rvalue) / (float)((pow(2, (float)f2leaves[i]->rcoef)) * f2leaves[i]->rvalue);
79 rightvalue = (float)(f1leaves[i]->rvalue);
81 Chao += leftvalue + rightvalue;
85 for (int i = 0; i < numNodes; i++) {
90 // cout << "exiting " << endl;
95 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
99 cout << "An unknown error has occurred in the SharedChao1 class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
104 /***********************************************************************/
105 //builds trees structure with n leaf nodes initialized to 0.
106 void SharedChao1::initialTree(int n) {
108 // (2^n) / 2. Divide by 2 because each leaf node contains 2 values. One for + and one for 1 or 2.
109 numLeaves = pow(2, (float)n) / 2;
110 numNodes = 2*numLeaves - 1;
114 f1leaves.resize(numNodes);
115 f2leaves.resize(numNodes);
117 //initialize leaf values
118 for (int i = 0; i < numLeaves; i++) {
119 f1leaves[i] = new IntNode;
120 f1leaves[i]->lvalue = 0;
121 f1leaves[i]->rvalue = 0;
122 f1leaves[i]->left = NULL;
123 f1leaves[i]->right = NULL;
125 f2leaves[i] = new IntNode;
126 f2leaves[i]->lvalue = 0;
127 f2leaves[i]->rvalue = 0;
128 f2leaves[i]->left = NULL;
129 f2leaves[i]->right = NULL;
132 //set pointers to children
133 for (int j = numLeaves; j < numNodes; j++) {
134 f1leaves[j] = new IntNode;
135 f1leaves[j]->left = f1leaves[countleft];
136 f1leaves[j]->right = f1leaves[countright];
138 f2leaves[j] = new IntNode;
139 f2leaves[j]->left = f2leaves[countleft];
140 f2leaves[j]->right =f2leaves[countright];
142 countleft = countleft + 2;
143 countright = countright + 2;
147 f1root = f1leaves[numNodes-1];
150 f2root = f2leaves[numNodes-1];
155 catch(exception& e) {
156 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function initialTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
160 cout << "An unknown error has occurred in the SharedChao1 class function initialTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
165 /***********************************************************************/
166 //take vector containing the abundance info. for a bin and updates trees.
167 void SharedChao1::updateTree(vector<int> bin) {
169 updateBranchf1(f1root, bin, 0);
170 updateBranchf2(f2root, bin, 0);
172 catch(exception& e) {
173 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function updateTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
177 cout << "An unknown error has occurred in the SharedChao1 class function updateTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
182 /***********************************************************************/
183 void SharedChao1::updateBranchf1(IntNode* node, vector<int> bin, int index) {
185 //if you have more than one group
186 if (index == (bin.size()-1)) {
187 if (bin[index] == 1) { node->lvalue++; node->rvalue++; }
188 else { node->rvalue++; }
190 if (bin[index] == 1) {
191 //follow path as if you are 1
192 updateBranchf1(node->left, bin, index+1);
194 //follow path as if you are +
195 updateBranchf1(node->right, bin, index+1);
198 catch(exception& e) {
199 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function updateBranchf1. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
203 cout << "An unknown error has occurred in the SharedChao1 class function updateBranchf1. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
208 /***********************************************************************/
209 void SharedChao1::updateBranchf2(IntNode* node, vector<int> bin, int index) {
211 //if you have more than one group
212 if (index == (bin.size()-1)) {
213 if (bin[index] == 2) { node->lvalue++; node->rvalue++; }
214 else { node->rvalue++; }
216 if (bin[index] == 2) {
217 //follow path as if you are 1
218 updateBranchf2(node->left, bin, index+1);
220 //follow path as if you are +
221 updateBranchf2(node->right, bin, index+1);
224 catch(exception& e) {
225 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function updateBranchf2. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
229 cout << "An unknown error has occurred in the SharedChao1 class function updateBranchf2. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
234 /***********************************************************************/
235 void SharedChao1::setCoef(IntNode* node, int coef) {
237 if (node->left != NULL) {
238 setCoef(node->left, coef+1);
239 setCoef(node->right, coef);
241 node->lcoef = coef+1;
245 catch(exception& e) {
246 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function getCoef. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
250 cout << "An unknown error has occurred in the SharedChao1 class function getCoef. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
255 /***********************************************************************/
256 //for debugging purposes
257 void SharedChao1::printTree() {
259 cout << "F1 leaves" << endl;
262 cout << "F2 leaves" << endl;
267 /*****************************************************************/
268 void SharedChao1::printBranch(IntNode* node) {
271 // you are not a leaf
272 if (node->left != NULL) {
273 printBranch(node->left);
274 printBranch(node->right);
275 }else { //you are a leaf
276 cout << node->lvalue << endl;
277 cout << node->rvalue << endl;
281 catch(exception& e) {
282 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
286 cout << "An unknown error has occurred in the Tree class function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
291 /*****************************************************************/