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) {
46 // for (int h = 0; h < temp.size(); h++) { cout << temp[h] << " "; } cout << endl;
47 //find f1 and f2values
53 //cout << "Entering " << endl;
54 //calculate chao1, (numleaves-1) because numleaves contains the ++ values.
56 for(int i=0;i<numLeaves;i++){
57 if (f2leaves[i]->lvalue == 0) { bias = true;}// break;}
61 for (int i = 0; i < numLeaves; i++) {
63 leftvalue = (float)(f1leaves[i]->lvalue * (f1leaves[i]->lvalue - 1)) / (float)((pow(2, (float)f2leaves[i]->lcoef)) * (f2leaves[i]->lvalue + 1));
64 if (i != (numLeaves-1)) {
65 rightvalue = (float)(f1leaves[i]->rvalue * (f1leaves[i]->rvalue - 1)) / (float)((pow(2, (float)f2leaves[i]->rcoef)) * (f2leaves[i]->rvalue + 1));
67 rightvalue = (float)(f1leaves[i]->rvalue);
69 Chao += leftvalue + rightvalue;
74 for (int i = 0; i < numLeaves; i++) {
76 leftvalue = (float)(f1leaves[i]->lvalue * f1leaves[i]->lvalue) / (float)((pow(2, (float)f2leaves[i]->lcoef)) * f2leaves[i]->lvalue);
77 if (i != (numLeaves-1)) {
78 rightvalue = (float)(f1leaves[i]->rvalue * f1leaves[i]->rvalue) / (float)((pow(2, (float)f2leaves[i]->rcoef)) * f2leaves[i]->rvalue);
80 rightvalue = (float)(f1leaves[i]->rvalue);
82 Chao += leftvalue + rightvalue;
86 for (int i = 0; i < numNodes; i++) {
91 // cout << "exiting " << endl;
96 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
100 cout << "An unknown error has occurred in the SharedChao1 class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
105 /***********************************************************************/
106 //builds trees structure with n leaf nodes initialized to 0.
107 void SharedChao1::initialTree(int n) {
109 // (2^n) / 2. Divide by 2 because each leaf node contains 2 values. One for + and one for 1 or 2.
110 numLeaves = pow(2, (float)n) / 2;
111 numNodes = 2*numLeaves - 1;
115 f1leaves.resize(numNodes);
116 f2leaves.resize(numNodes);
118 //initialize leaf values
119 for (int i = 0; i < numLeaves; i++) {
120 f1leaves[i] = new IntNode;
121 f1leaves[i]->lvalue = 0;
122 f1leaves[i]->rvalue = 0;
123 f1leaves[i]->left = NULL;
124 f1leaves[i]->right = NULL;
126 f2leaves[i] = new IntNode;
127 f2leaves[i]->lvalue = 0;
128 f2leaves[i]->rvalue = 0;
129 f2leaves[i]->left = NULL;
130 f2leaves[i]->right = NULL;
133 //set pointers to children
134 for (int j = numLeaves; j < numNodes; j++) {
135 f1leaves[j] = new IntNode;
136 f1leaves[j]->left = f1leaves[countleft];
137 f1leaves[j]->right = f1leaves[countright];
139 f2leaves[j] = new IntNode;
140 f2leaves[j]->left = f2leaves[countleft];
141 f2leaves[j]->right =f2leaves[countright];
143 countleft = countleft + 2;
144 countright = countright + 2;
148 f1root = f1leaves[numNodes-1];
151 f2root = f2leaves[numNodes-1];
156 catch(exception& e) {
157 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function initialTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
161 cout << "An unknown error has occurred in the SharedChao1 class function initialTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
166 /***********************************************************************/
167 //take vector containing the abundance info. for a bin and updates trees.
168 void SharedChao1::updateTree(vector<int> bin) {
170 updateBranchf1(f1root, bin, 0);
171 updateBranchf2(f2root, bin, 0);
173 catch(exception& e) {
174 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function updateTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
178 cout << "An unknown error has occurred in the SharedChao1 class function updateTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
183 /***********************************************************************/
184 void SharedChao1::updateBranchf1(IntNode* node, vector<int> bin, int index) {
186 //if you have more than one group
187 if (index == (bin.size()-1)) {
188 if (bin[index] == 1) { node->lvalue++; node->rvalue++; }
189 else { node->rvalue++; }
191 if (bin[index] == 1) {
192 //follow path as if you are 1
193 updateBranchf1(node->left, bin, index+1);
195 //follow path as if you are +
196 updateBranchf1(node->right, bin, index+1);
199 catch(exception& e) {
200 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function updateBranchf1. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
204 cout << "An unknown error has occurred in the SharedChao1 class function updateBranchf1. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
209 /***********************************************************************/
210 void SharedChao1::updateBranchf2(IntNode* node, vector<int> bin, int index) {
212 //if you have more than one group
213 if (index == (bin.size()-1)) {
214 if (bin[index] == 2) { node->lvalue++; node->rvalue++; }
215 else { node->rvalue++; }
217 if (bin[index] == 2) {
218 //follow path as if you are 1
219 updateBranchf2(node->left, bin, index+1);
221 //follow path as if you are +
222 updateBranchf2(node->right, bin, index+1);
225 catch(exception& e) {
226 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function updateBranchf2. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
230 cout << "An unknown error has occurred in the SharedChao1 class function updateBranchf2. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
235 /***********************************************************************/
236 void SharedChao1::setCoef(IntNode* node, int coef) {
238 if (node->left != NULL) {
239 setCoef(node->left, coef+1);
240 setCoef(node->right, coef);
242 node->lcoef = coef+1;
246 catch(exception& e) {
247 cout << "Standard Error: " << e.what() << " has occurred in the SharedChao1 class Function getCoef. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
251 cout << "An unknown error has occurred in the SharedChao1 class function getCoef. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
256 /***********************************************************************/
257 //for debugging purposes
258 void SharedChao1::printTree() {
260 cout << "F1 leaves" << endl;
263 cout << "F2 leaves" << endl;
268 /*****************************************************************/
269 void SharedChao1::printBranch(IntNode* node) {
272 // you are not a leaf
273 if (node->left != NULL) {
274 printBranch(node->left);
275 printBranch(node->right);
276 }else { //you are a leaf
277 cout << node->lvalue << endl;
278 cout << node->rvalue << endl;
282 catch(exception& e) {
283 cout << "Standard Error: " << e.what() << " has occurred in the Tree class Function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
287 cout << "An unknown error has occurred in the Tree class function printBranch. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
292 /*****************************************************************/