5 * Created by Sarah Westcott on 2/9/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "unweighted.h"
12 /**************************************************************************************************/
14 EstOutput Unweighted::getValues(Tree* t) {
16 globaldata = GlobalData::getInstance();
18 vector<string> groups;
19 double UniqueBL; //a branch length is unique if it's chidren are from the same group
20 double totalBL; //all branch lengths
21 double UW; //Unweighted Value = UniqueBL / totalBL;
22 map<string, int>::iterator it; //iterator to traverse pgroups
23 map<string, int> copyIpcount;
26 //if the users enters no groups then give them the score of all groups
27 int numGroups = globaldata->Groups.size();
29 //calculate number of comparsions
31 for (int r=0; r<numGroups; r++) {
32 for (int l = r+1; l < numGroups; l++) {
37 //numComp+1 for AB, AC, BC, ABC
38 data.resize(numComp+1,0);
41 for (int a=0; a<numGroups; a++) {
42 for (int l = a+1; l < numGroups; l++) {
43 UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
44 totalBL = 0.00; //all branch lengths
45 UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
48 //groups in this combo
49 groups.push_back(globaldata->Groups[a]); groups.push_back(globaldata->Groups[l]);
51 for(int i=t->getNumLeaves();i<t->getNumNodes();i++){
53 int lc = t->tree[i].getLChild(); //lc = vector index of left child
54 int rc = t->tree[i].getRChild(); //rc = vector index of right child
56 /**********************************************************************/
57 //This section adds in all lengths that are non leaf
59 copyIpcount = t->tree[i].pcount;
60 for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
61 if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first); }
64 //if i's children are from the same group then i's pcount size will be 1
65 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
66 if (copyIpcount.size() == 0) { }
67 else if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { UniqueBL += t->tree[i].getBranchLength(); }
69 //add i's BL to total if it is from the groups the user wants
70 if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
71 totalBL += t->tree[i].getBranchLength();
74 /**********************************************************************/
75 //This section adds in all lengths that are leaf
77 //if i's chidren are leaves
78 if (t->tree[rc].getRChild() == -1) {
79 //if rc is a valid group and rc has a BL
80 if ((inUsersGroups(t->tree[rc].getGroup(), globaldata->Groups) == true) && (t->tree[rc].getBranchLength() != -1)) {
81 UniqueBL += t->tree[rc].getBranchLength();
82 totalBL += t->tree[rc].getBranchLength();
86 if (t->tree[lc].getLChild() == -1) {
87 //if lc is a valid group and lc has a BL
88 if ((inUsersGroups(t->tree[lc].getGroup(), globaldata->Groups) == true) && (t->tree[lc].getBranchLength() != -1)) {
89 UniqueBL += t->tree[lc].getBranchLength();
90 totalBL += t->tree[lc].getBranchLength();
94 /**********************************************************************/
97 UW = (UniqueBL / totalBL);
99 if (isnan(UW) || isinf(UW)) { UW = 0; }
109 if (numGroups == 0) {
110 //get score for all users groups
111 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
112 groups.push_back(tmap->namesOfGroups[i]);
115 for (int i = 0; i < globaldata->Groups.size(); i++) {
116 groups.push_back(globaldata->Groups[i]);
120 UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
121 totalBL = 0.00; //all branch lengths
122 UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
125 for(int i=t->getNumLeaves();i<t->getNumNodes();i++){
127 int lc = t->tree[i].getLChild(); //lc = vector index of left child
128 int rc = t->tree[i].getRChild(); //rc = vector index of right child
130 /**********************************************************************/
131 //This section adds in all lengths that are non leaf
133 copyIpcount = t->tree[i].pcount;
134 for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
135 if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first); }
138 //if i's children are from the same group then i's pcount size will be 1
139 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
140 if (copyIpcount.size() == 0) { }
141 else if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { UniqueBL += t->tree[i].getBranchLength(); }
143 //add i's BL to total if it is from the groups the user wants
144 if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
145 totalBL += t->tree[i].getBranchLength();
148 /**********************************************************************/
149 //This section adds in all lengths that are leaf
151 //if i's chidren are leaves
152 if (t->tree[rc].getRChild() == -1) {
153 //if rc is a valid group and rc has a BL
154 if ((inUsersGroups(t->tree[rc].getGroup(), globaldata->Groups) == true) && (t->tree[rc].getBranchLength() != -1)) {
155 UniqueBL += t->tree[rc].getBranchLength();
156 totalBL += t->tree[rc].getBranchLength();
160 if (t->tree[lc].getLChild() == -1) {
161 //if lc is a valid group and lc has a BL
162 if ((inUsersGroups(t->tree[lc].getGroup(), globaldata->Groups) == true) && (t->tree[lc].getBranchLength() != -1)) {
163 UniqueBL += t->tree[lc].getBranchLength();
164 totalBL += t->tree[lc].getBranchLength();
168 /**********************************************************************/
171 UW = (UniqueBL / totalBL);
173 if (isnan(UW) || isinf(UW)) { UW = 0; }
181 catch(exception& e) {
182 cout << "Standard Error: " << e.what() << " has occurred in the Unweighted class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
186 cout << "An unknown error has occurred in the Unweighted class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
192 /**************************************************************************************************/
194 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB) {
196 globaldata = GlobalData::getInstance();
198 vector<string> groups;
199 double UniqueBL; //a branch length is unique if it's chidren are from the same group
200 double totalBL; //all branch lengths
201 double UW; //Unweighted Value = UniqueBL / totalBL;
202 map<string, int>::iterator it; //iterator to traverse pgroups
203 map<string, int> copyIpcount;
206 //if the users enters no groups then give them the score of all groups
207 int numGroups = globaldata->Groups.size();
209 //calculate number of comparsions
211 for (int r=0; r<numGroups; r++) {
212 for (int l = r+1; l < numGroups; l++) {
217 //numComp+1 for AB, AC, BC, ABC
218 data.resize(numComp+1,0);
221 for (int a=0; a<numGroups; a++) {
222 for (int l = a+1; l < numGroups; l++) {
223 UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
224 totalBL = 0.00; //all branch lengths
225 UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
228 //copy random tree passed in
229 copyTree->getCopy(t);
231 //swap labels in the groups you want to compare
232 copyTree->assembleRandomUnifracTree(globaldata->Groups[a], globaldata->Groups[l]);
234 //groups in this combo
235 groups.push_back(globaldata->Groups[a]); groups.push_back(globaldata->Groups[l]);
237 for(int i=t->getNumLeaves();i<t->getNumNodes();i++){
239 int lc = t->tree[i].getLChild(); //lc = vector index of left child
240 int rc = t->tree[i].getRChild(); //rc = vector index of right child
242 /**********************************************************************/
243 //This section adds in all lengths that are non leaf
245 copyIpcount = t->tree[i].pcount;
246 for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
247 if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first); }
250 //if i's children are from the same group then i's pcount size will be 1
251 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
252 if (copyIpcount.size() == 0) { }
253 else if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { UniqueBL += t->tree[i].getBranchLength(); }
255 //add i's BL to total if it is from the groups the user wants
256 if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
257 totalBL += t->tree[i].getBranchLength();
260 /**********************************************************************/
261 //This section adds in all lengths that are leaf
263 //if i's chidren are leaves
264 if (t->tree[rc].getRChild() == -1) {
265 //if rc is a valid group and rc has a BL
266 if ((inUsersGroups(t->tree[rc].getGroup(), globaldata->Groups) == true) && (t->tree[rc].getBranchLength() != -1)) {
267 UniqueBL += t->tree[rc].getBranchLength();
268 totalBL += t->tree[rc].getBranchLength();
272 if (t->tree[lc].getLChild() == -1) {
273 //if lc is a valid group and lc has a BL
274 if ((inUsersGroups(t->tree[lc].getGroup(), globaldata->Groups) == true) && (t->tree[lc].getBranchLength() != -1)) {
275 UniqueBL += t->tree[lc].getBranchLength();
276 totalBL += t->tree[lc].getBranchLength();
280 /**********************************************************************/
283 UW = (UniqueBL / totalBL);
285 if (isnan(UW) || isinf(UW)) { UW = 0; }
295 if (numGroups == 0) {
296 //get score for all users groups
297 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
298 groups.push_back(tmap->namesOfGroups[i]);
301 for (int i = 0; i < globaldata->Groups.size(); i++) {
302 groups.push_back(globaldata->Groups[i]);
306 UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
307 totalBL = 0.00; //all branch lengths
308 UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
311 //copy random tree passed in
312 copyTree->getCopy(t);
314 //swap labels in all the groups you want to compare
315 copyTree->assembleRandomUnifracTree();
317 for(int i=t->getNumLeaves();i<t->getNumNodes();i++){
319 int lc = t->tree[i].getLChild(); //lc = vector index of left child
320 int rc = t->tree[i].getRChild(); //rc = vector index of right child
322 /**********************************************************************/
323 //This section adds in all lengths that are non leaf
325 copyIpcount = t->tree[i].pcount;
326 for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
327 if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first); }
330 //if i's children are from the same group then i's pcount size will be 1
331 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
332 if (copyIpcount.size() == 0) { }
333 else if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { UniqueBL += t->tree[i].getBranchLength(); }
335 //add i's BL to total if it is from the groups the user wants
336 if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
337 totalBL += t->tree[i].getBranchLength();
340 /**********************************************************************/
341 //This section adds in all lengths that are leaf
343 //if i's chidren are leaves
344 if (t->tree[rc].getRChild() == -1) {
345 //if rc is a valid group and rc has a BL
346 if ((inUsersGroups(t->tree[rc].getGroup(), globaldata->Groups) == true) && (t->tree[rc].getBranchLength() != -1)) {
347 UniqueBL += t->tree[rc].getBranchLength();
348 totalBL += t->tree[rc].getBranchLength();
352 if (t->tree[lc].getLChild() == -1) {
353 //if lc is a valid group and lc has a BL
354 if ((inUsersGroups(t->tree[lc].getGroup(), globaldata->Groups) == true) && (t->tree[lc].getBranchLength() != -1)) {
355 UniqueBL += t->tree[lc].getBranchLength();
356 totalBL += t->tree[lc].getBranchLength();
360 /**********************************************************************/
363 UW = (UniqueBL / totalBL);
365 if (isnan(UW) || isinf(UW)) { UW = 0; }
373 catch(exception& e) {
374 cout << "Standard Error: " << e.what() << " has occurred in the Unweighted class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
378 cout << "An unknown error has occurred in the Unweighted class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";