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);
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) {
44 //find f1 and f2values
50 //calculate chao1, (numleaves-1) because numleaves contains the ++ values.
52 for(int i=0;i<numLeaves;i++){
53 if (f2leaves[i]->lvalue == 0 || f2leaves[i]->rvalue == 0) { bias = true;}// break;}
57 for (int i = 0; i < numLeaves; i++) {
59 leftvalue = (float)(f1leaves[i]->lvalue * (f1leaves[i]->lvalue - 1)) / (float)((pow(2, (float)f2leaves[i]->lcoef)) * (f2leaves[i]->lvalue + 1));
60 if (i != (numLeaves-1)) {
61 rightvalue = (float)(f1leaves[i]->rvalue * (f1leaves[i]->rvalue - 1)) / (float)((pow(2, (float)f2leaves[i]->rcoef)) * (f2leaves[i]->rvalue + 1));
64 rightvalue = (float)(f1leaves[i]->rvalue);
66 Chao += leftvalue + rightvalue;
71 for (int i = 0; i < numLeaves; i++) {
73 leftvalue = (float)(f1leaves[i]->lvalue * f1leaves[i]->lvalue) / (float)((pow(2, (float)f2leaves[i]->lcoef)) * f2leaves[i]->lvalue);
74 if (i != (numLeaves-1)) {
75 rightvalue = (float)(f1leaves[i]->rvalue * f1leaves[i]->rvalue) / (float)((pow(2, (float)f2leaves[i]->rcoef)) * f2leaves[i]->rvalue);
78 rightvalue = (float)(f1leaves[i]->rvalue);
80 Chao += leftvalue + rightvalue;
84 for (int i = 0; i < numNodes; i++) {
94 errorOut(e, "SharedChao1", "getValues");
99 /***********************************************************************/
100 //builds trees structure with n leaf nodes initialized to 0.
101 void SharedChao1::initialTree(int n) {
103 // (2^n) / 2. Divide by 2 because each leaf node contains 2 values. One for + and one for 1 or 2.
104 numLeaves = pow(2, (float)n) / 2;
105 numNodes = 2*numLeaves - 1;
109 f1leaves.resize(numNodes);
110 f2leaves.resize(numNodes);
112 //initialize leaf values
113 for (int i = 0; i < numLeaves; i++) {
114 f1leaves[i] = new IntNode;
115 f1leaves[i]->lvalue = 0;
116 f1leaves[i]->rvalue = 0;
117 f1leaves[i]->left = NULL;
118 f1leaves[i]->right = NULL;
120 f2leaves[i] = new IntNode;
121 f2leaves[i]->lvalue = 0;
122 f2leaves[i]->rvalue = 0;
123 f2leaves[i]->left = NULL;
124 f2leaves[i]->right = NULL;
127 //set pointers to children
128 for (int j = numLeaves; j < numNodes; j++) {
129 f1leaves[j] = new IntNode;
130 f1leaves[j]->left = f1leaves[countleft];
131 f1leaves[j]->right = f1leaves[countright];
133 f2leaves[j] = new IntNode;
134 f2leaves[j]->left = f2leaves[countleft];
135 f2leaves[j]->right =f2leaves[countright];
137 countleft = countleft + 2;
138 countright = countright + 2;
142 f1root = f1leaves[numNodes-1];
145 f2root = f2leaves[numNodes-1];
150 catch(exception& e) {
151 errorOut(e, "SharedChao1", "initialTree");
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 errorOut(e, "SharedChao1", "updateTree");
169 /***********************************************************************/
170 void SharedChao1::updateBranchf1(IntNode* node, vector<int> bin, int index) {
172 //if you have more than one group
173 if (index == (bin.size()-1)) {
174 if (bin[index] == 1) { node->lvalue++; node->rvalue++; }
175 else { node->rvalue++; }
177 if (bin[index] == 1) {
178 //follow path as if you are 1
179 updateBranchf1(node->left, bin, index+1);
181 //follow path as if you are +
182 updateBranchf1(node->right, bin, index+1);
185 catch(exception& e) {
186 errorOut(e, "SharedChao1", "updateBranchf1");
191 /***********************************************************************/
192 void SharedChao1::updateBranchf2(IntNode* node, vector<int> bin, int index) {
194 //if you have more than one group
195 if (index == (bin.size()-1)) {
196 if (bin[index] == 2) { node->lvalue++; node->rvalue++; }
197 else { node->rvalue++; }
199 if (bin[index] == 2) {
200 //follow path as if you are 1
201 updateBranchf2(node->left, bin, index+1);
203 //follow path as if you are +
204 updateBranchf2(node->right, bin, index+1);
207 catch(exception& e) {
208 errorOut(e, "SharedChao1", "updateBranchf2");
213 /***********************************************************************/
214 void SharedChao1::setCoef(IntNode* node, int coef) {
216 if (node->left != NULL) {
217 setCoef(node->left, coef+1);
218 setCoef(node->right, coef);
220 node->lcoef = coef+1;
224 catch(exception& e) {
225 errorOut(e, "SharedChao1", "setCoef");
230 /***********************************************************************/
231 //for debugging purposes
232 void SharedChao1::printTree() {
234 mothurOut("F1 leaves"); mothurOutEndLine();
237 mothurOut("F2 leaves"); mothurOutEndLine();
242 /*****************************************************************/
243 void SharedChao1::printBranch(IntNode* node) {
246 // you are not a leaf
247 if (node->left != NULL) {
248 printBranch(node->left);
249 printBranch(node->right);
250 }else { //you are a leaf
251 mothurOut(toString(node->lvalue)); mothurOutEndLine();
252 mothurOut(toString(node->rvalue)); mothurOutEndLine();
256 catch(exception& e) {
257 errorOut(e, "SharedChao1", "printBranch");
262 /*****************************************************************/