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 // cout << "exiting " << endl;
86 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
90 cout << "An unknown error has occurred in the SharedChao1 class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
95 /***********************************************************************/
96 //builds trees structure with n leaf nodes initialized to 0.
97 void SharedChao1::initialTree(int n) {
99 // (2^n) / 2. Divide by 2 because each leaf node contains 2 values. One for + and one for 1 or 2.
100 numLeaves = pow(2, (float)n) / 2;
101 numNodes = 2*numLeaves - 1;
105 f1leaves.resize(numNodes);
106 f2leaves.resize(numNodes);
108 //initialize leaf values
109 for (int i = 0; i < numLeaves; i++) {
110 f1leaves[i] = new IntNode;
111 f1leaves[i]->lvalue = 0;
112 f1leaves[i]->rvalue = 0;
113 f1leaves[i]->left = NULL;
114 f1leaves[i]->right = NULL;
116 f2leaves[i] = new IntNode;
117 f2leaves[i]->lvalue = 0;
118 f2leaves[i]->rvalue = 0;
119 f2leaves[i]->left = NULL;
120 f2leaves[i]->right = NULL;
123 //set pointers to children
124 for (int j = numLeaves; j < numNodes; j++) {
125 f1leaves[j] = new IntNode;
126 f1leaves[j]->left = f1leaves[countleft];
127 f1leaves[j]->right = f1leaves[countright];
129 f2leaves[j] = new IntNode;
130 f2leaves[j]->left = f2leaves[countleft];
131 f2leaves[j]->right =f2leaves[countright];
133 countleft = countleft + 2;
134 countright = countright + 2;
138 f1root = f1leaves[numNodes-1];
141 f2root = f2leaves[numNodes-1];
146 catch(exception& e) {
147 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function initialTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
151 cout << "An unknown error has occurred in the SharedChao1 class function initialTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
156 /***********************************************************************/
157 //take vector containing the abundance info. for a bin and updates trees.
158 void SharedChao1::updateTree(vector<int> bin) {
160 updateBranchf1(f1root, bin, 0);
161 updateBranchf2(f2root, bin, 0);
163 catch(exception& e) {
164 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function updateTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
168 cout << "An unknown error has occurred in the SharedChao1 class function updateTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
173 /***********************************************************************/
174 void SharedChao1::updateBranchf1(IntNode* node, vector<int> bin, int index) {
176 //if you have more than one group
177 if (index == (bin.size()-1)) {
178 if (bin[index] == 1) { node->lvalue++; node->rvalue++; }
179 else { node->rvalue++; }
181 if (bin[index] == 1) {
182 //follow path as if you are 1
183 updateBranchf1(node->left, bin, index+1);
185 //follow path as if you are +
186 updateBranchf1(node->right, bin, index+1);
189 catch(exception& e) {
190 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function updateBranchf1. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
194 cout << "An unknown error has occurred in the SharedChao1 class function updateBranchf1. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
199 /***********************************************************************/
200 void SharedChao1::updateBranchf2(IntNode* node, vector<int> bin, int index) {
202 //if you have more than one group
203 if (index == (bin.size()-1)) {
204 if (bin[index] == 2) { node->lvalue++; node->rvalue++; }
205 else { node->rvalue++; }
207 if (bin[index] == 2) {
208 //follow path as if you are 1
209 updateBranchf2(node->left, bin, index+1);
211 //follow path as if you are +
212 updateBranchf2(node->right, bin, index+1);
215 catch(exception& e) {
216 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function updateBranchf2. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
220 cout << "An unknown error has occurred in the SharedChao1 class function updateBranchf2. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
225 /***********************************************************************/
226 void SharedChao1::setCoef(IntNode* node, int coef) {
228 if (node->left != NULL) {
229 setCoef(node->left, coef+1);
230 setCoef(node->right, coef);
232 node->lcoef = coef+1;
236 catch(exception& e) {
237 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function getCoef. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
241 cout << "An unknown error has occurred in the SharedChao1 class function getCoef. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
246 /***********************************************************************/
247 //for debugging purposes
248 void SharedChao1::printTree() {
250 cout << "F1 leaves" << endl;
253 cout << "F2 leaves" << endl;
258 /*****************************************************************/
259 void SharedChao1::printBranch(IntNode* node) {
262 // you are not a leaf
263 if (node->left != NULL) {
264 printBranch(node->left);
265 printBranch(node->right);
266 }else { //you are a leaf
267 cout << node->lvalue << endl;
268 cout << node->rvalue << endl;
272 catch(exception& e) {
273 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
277 cout << "An unknown error has occurred in the Tree class function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
282 /*****************************************************************/