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){
18 int numGroups = shared.size();
19 float Chao = 0.0; float leftvalue, rightvalue;
21 // IntNode is defined in mothur.h
22 // The tree used here is a binary tree used to represent the f1+++, f+1++, f++1+, f+++1, f11++, f1+1+...
23 // combinations required to solve the chao estimator equation for any number of groups. Conceptually, think
24 // 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.
25 // The coeffient value is how many times you chose branch 1 to get to that fvalue.
26 // If you choose left you are selecting the 1 or 2 value and right means the + value. For instance, to find
27 // the number of bins that have f1+1+ you would start at the root, go left, right, left, and select the rightvalue.
28 // the coeffient is 2. Note: we only set the coeffient in f2 values.
30 //create and initialize trees to 0.
31 initialTree(numGroups);
33 //loop through vectors calculating the f11, f1A, f2A, f1B, f2B, S12 values
34 for (int i = 0; i < shared[0]->size(); i++) {
35 //get bin values and calc shared
36 bool sharedByAll = true;
38 for (int j = 0; j < numGroups; j++) {
39 temp.push_back(shared[j]->getAbundance(i));
40 if (temp[j] == 0) { sharedByAll = false; }
44 if (sharedByAll == true) {
47 // for (int h = 0; h < temp.size(); h++) { cout << temp[h] << " "; } cout << endl;
48 //find f1 and f2values
54 //cout << "Entering " << endl;
55 //calculate chao1, (numleaves-1) because numleaves contains the ++ values.
56 for (int i = 0; i < numLeaves; i++) {
57 //divide by zero error
58 if (f2leaves[i]->lvalue == 0) { f2leaves[i]->lvalue++; }
59 if (f2leaves[i]->rvalue == 0) { f2leaves[i]->rvalue++; }
61 //cout << "f2 leaves ";
62 //cout << f2leaves[i]->lvalue << f2leaves[i]->rvalue << endl;
64 // cout << "f2 leaf coef ";
65 //cout << f2leaves[i]->lcoef << '\t' << f2leaves[i]->rcoef << endl;
67 // cout << "f1 leaves ";
68 // cout << f1leaves[i]->lvalue << f1leaves[i]->rvalue << endl;
71 leftvalue = (float)(f1leaves[i]->lvalue * f1leaves[i]->lvalue) / (float)((pow(2, (float)f2leaves[i]->lcoef)) * f2leaves[i]->lvalue);
72 if (i != (numLeaves-1)) {
73 rightvalue = (float)(f1leaves[i]->rvalue * f1leaves[i]->rvalue) / (float)((pow(2, (float)f2leaves[i]->rcoef)) * f2leaves[i]->rvalue);
75 rightvalue = (float)(f1leaves[i]->rvalue);
77 //cout << "left = " << leftvalue << " right = " << rightvalue << endl;
78 Chao += leftvalue + rightvalue;
81 for (int i = 0; i < numNodes; i++) {
86 // cout << "exiting " << endl;
91 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
95 cout << "An unknown error has occurred in the SharedChao1 class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
100 /***********************************************************************/
101 //builds trees structure with n leaf nodes initialized to 0.
102 void SharedChao1::initialTree(int n) {
104 // (2^n) / 2. Divide by 2 because each leaf node contains 2 values. One for + and one for 1 or 2.
105 numLeaves = pow(2, (float)n) / 2;
106 numNodes = 2*numLeaves - 1;
110 f1leaves.resize(numNodes);
111 f2leaves.resize(numNodes);
113 //initialize leaf values
114 for (int i = 0; i < numLeaves; i++) {
115 f1leaves[i] = new IntNode;
116 f1leaves[i]->lvalue = 0;
117 f1leaves[i]->rvalue = 0;
118 f1leaves[i]->left = NULL;
119 f1leaves[i]->right = NULL;
121 f2leaves[i] = new IntNode;
122 f2leaves[i]->lvalue = 0;
123 f2leaves[i]->rvalue = 0;
124 f2leaves[i]->left = NULL;
125 f2leaves[i]->right = NULL;
128 //set pointers to children
129 for (int j = numLeaves; j < numNodes; j++) {
130 f1leaves[j] = new IntNode;
131 f1leaves[j]->left = f1leaves[countleft];
132 f1leaves[j]->right = f1leaves[countright];
134 f2leaves[j] = new IntNode;
135 f2leaves[j]->left = f2leaves[countleft];
136 f2leaves[j]->right =f2leaves[countright];
138 countleft = countleft + 2;
139 countright = countright + 2;
143 f1root = f1leaves[numNodes-1];
146 f2root = f2leaves[numNodes-1];
151 catch(exception& e) {
152 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function initialTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
156 cout << "An unknown error has occurred in the SharedChao1 class function initialTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
161 /***********************************************************************/
162 //take vector containing the abundance info. for a bin and updates trees.
163 void SharedChao1::updateTree(vector<int> bin) {
165 updateBranchf1(f1root, bin, 0);
166 updateBranchf2(f2root, bin, 0);
168 catch(exception& e) {
169 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function updateTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
173 cout << "An unknown error has occurred in the SharedChao1 class function updateTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
178 /***********************************************************************/
179 void SharedChao1::updateBranchf1(IntNode* node, vector<int> bin, int index) {
181 //if you have more than one group
182 if (index == (bin.size()-1)) {
183 if (bin[index] == 1) { node->lvalue++; node->rvalue++; }
184 else { node->rvalue++; }
186 if (bin[index] == 1) {
187 //follow path as if you are 1
188 updateBranchf1(node->left, bin, index+1);
190 //follow path as if you are +
191 updateBranchf1(node->right, bin, index+1);
194 catch(exception& e) {
195 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function updateBranchf1. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
199 cout << "An unknown error has occurred in the SharedChao1 class function updateBranchf1. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
204 /***********************************************************************/
205 void SharedChao1::updateBranchf2(IntNode* node, vector<int> bin, int index) {
207 //if you have more than one group
208 if (index == (bin.size()-1)) {
209 if (bin[index] == 2) { node->lvalue++; node->rvalue++; }
210 else { node->rvalue++; }
212 if (bin[index] == 2) {
213 //follow path as if you are 1
214 updateBranchf2(node->left, bin, index+1);
216 //follow path as if you are +
217 updateBranchf2(node->right, bin, index+1);
220 catch(exception& e) {
221 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function updateBranchf2. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
225 cout << "An unknown error has occurred in the SharedChao1 class function updateBranchf2. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
230 /***********************************************************************/
231 void SharedChao1::setCoef(IntNode* node, int coef) {
233 if (node->left != NULL) {
234 setCoef(node->left, coef+1);
235 setCoef(node->right, coef);
237 node->lcoef = coef+1;
241 catch(exception& e) {
242 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function getCoef. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
246 cout << "An unknown error has occurred in the SharedChao1 class function getCoef. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
251 /***********************************************************************/
252 //for debugging purposes
253 void SharedChao1::printTree() {
255 cout << "F1 leaves" << endl;
258 cout << "F2 leaves" << endl;
263 /*****************************************************************/
264 void SharedChao1::printBranch(IntNode* node) {
267 // you are not a leaf
268 if (node->left != NULL) {
269 printBranch(node->left);
270 printBranch(node->right);
271 }else { //you are a leaf
272 cout << node->lvalue << endl;
273 cout << node->rvalue << endl;
277 catch(exception& e) {
278 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
282 cout << "An unknown error has occurred in the Tree class function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
287 /*****************************************************************/