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 += abs(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 += abs(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(), groups) == true) && (t->tree[rc].getBranchLength() != -1)) {
81 UniqueBL += abs(t->tree[rc].getBranchLength());
82 totalBL += abs(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(), groups) == true) && (t->tree[lc].getBranchLength() != -1)) {
89 UniqueBL += abs(t->tree[lc].getBranchLength());
90 totalBL += abs(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 += abs(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 += abs(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(), groups) == true) && (t->tree[rc].getBranchLength() != -1)) {
155 UniqueBL += abs(t->tree[rc].getBranchLength());
156 totalBL += abs(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(), groups) == true) && (t->tree[lc].getBranchLength() != -1)) {
163 UniqueBL += abs(t->tree[lc].getBranchLength());
164 totalBL += abs(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 //groups in this combo
232 groups.push_back(globaldata->Groups[a]); groups.push_back(globaldata->Groups[l]);
234 //swap labels in the groups you want to compare
235 copyTree->assembleRandomUnifracTree(groups[0], groups[1]);
237 //copyTree->createNewickFile("random"+groupA+toString(count));
239 for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
241 int lc = copyTree->tree[i].getLChild(); //lc = vector index of left child
242 int rc = copyTree->tree[i].getRChild(); //rc = vector index of right child
244 /**********************************************************************/
245 //This section adds in all lengths that are non leaf
247 copyIpcount = copyTree->tree[i].pcount;
248 for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
249 if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first); }
252 //if i's children are from the same group then i's pcount size will be 1
253 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
254 if (copyIpcount.size() == 0) { }
255 else if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { UniqueBL += abs(copyTree->tree[i].getBranchLength()); }
257 //add i's BL to total if it is from the groups the user wants
258 if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
259 totalBL += abs(copyTree->tree[i].getBranchLength());
262 /**********************************************************************/
263 //This section adds in all lengths that are leaf
265 //if i's chidren are leaves
266 if (copyTree->tree[rc].getRChild() == -1) {
267 //if rc is a valid group and rc has a BL
268 if ((inUsersGroups(copyTree->tree[rc].getGroup(), groups) == true) && (copyTree->tree[rc].getBranchLength() != -1)) {
269 UniqueBL += abs(copyTree->tree[rc].getBranchLength());
270 totalBL += abs(copyTree->tree[rc].getBranchLength());
274 if (copyTree->tree[lc].getLChild() == -1) {
275 //if lc is a valid group and lc has a BL
276 if ((inUsersGroups(copyTree->tree[lc].getGroup(), groups) == true) && (copyTree->tree[lc].getBranchLength() != -1)) {
277 UniqueBL += abs(copyTree->tree[lc].getBranchLength());
278 totalBL += abs(copyTree->tree[lc].getBranchLength());
282 /**********************************************************************/
285 UW = (UniqueBL / totalBL);
287 if (isnan(UW) || isinf(UW)) { UW = 0; }
297 if (numGroups == 0) {
298 //get score for all users groups
299 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
300 groups.push_back(tmap->namesOfGroups[i]);
303 for (int i = 0; i < globaldata->Groups.size(); i++) {
304 groups.push_back(globaldata->Groups[i]);
308 UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
309 totalBL = 0.00; //all branch lengths
310 UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
313 //copy random tree passed in
314 copyTree->getCopy(t);
316 //swap labels in all the groups you want to compare
317 copyTree->assembleRandomUnifracTree(groups);
319 for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
321 int lc = copyTree->tree[i].getLChild(); //lc = vector index of left child
322 int rc = copyTree->tree[i].getRChild(); //rc = vector index of right child
324 /**********************************************************************/
325 //This section adds in all lengths that are non leaf
327 copyIpcount = copyTree->tree[i].pcount;
328 for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
329 if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first); }
332 //if i's children are from the same group then i's pcount size will be 1
333 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
334 if (copyIpcount.size() == 0) { }
335 else if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { abs(UniqueBL += copyTree->tree[i].getBranchLength()); }
337 //add i's BL to total if it is from the groups the user wants
338 if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
339 totalBL += abs(copyTree->tree[i].getBranchLength());
342 /**********************************************************************/
343 //This section adds in all lengths that are leaf
345 //if i's chidren are leaves
346 if (copyTree->tree[rc].getRChild() == -1) {
347 //if rc is a valid group and rc has a BL
348 if ((inUsersGroups(copyTree->tree[rc].getGroup(), groups) == true) && (copyTree->tree[rc].getBranchLength() != -1)) {
349 UniqueBL += abs(copyTree->tree[rc].getBranchLength());
350 totalBL += abs(copyTree->tree[rc].getBranchLength());
354 if (copyTree->tree[lc].getLChild() == -1) {
355 //if lc is a valid group and lc has a BL
356 if ((inUsersGroups(copyTree->tree[lc].getGroup(), groups) == true) && (copyTree->tree[lc].getBranchLength() != -1)) {
357 UniqueBL += abs(copyTree->tree[lc].getBranchLength());
358 totalBL += abs(copyTree->tree[lc].getBranchLength());
362 /**********************************************************************/
365 UW = (UniqueBL / totalBL);
367 if (isnan(UW) || isinf(UW)) { UW = 0; }
377 catch(exception& e) {
378 cout << "Standard Error: " << e.what() << " has occurred in the Unweighted class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
382 cout << "An unknown error has occurred in the Unweighted class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";