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 if (tmap->namesOfGroups[i] != "xxx") {
113 groups.push_back(tmap->namesOfGroups[i]);
117 for (int i = 0; i < globaldata->Groups.size(); i++) {
118 groups.push_back(globaldata->Groups[i]);
122 UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
123 totalBL = 0.00; //all branch lengths
124 UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
127 for(int i=t->getNumLeaves();i<t->getNumNodes();i++){
129 int lc = t->tree[i].getLChild(); //lc = vector index of left child
130 int rc = t->tree[i].getRChild(); //rc = vector index of right child
132 /**********************************************************************/
133 //This section adds in all lengths that are non leaf
135 copyIpcount = t->tree[i].pcount;
136 for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
137 if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first); }
140 //if i's children are from the same group then i's pcount size will be 1
141 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
142 if (copyIpcount.size() == 0) { }
143 else if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { UniqueBL += abs(t->tree[i].getBranchLength()); }
145 //add i's BL to total if it is from the groups the user wants
146 if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
147 totalBL += abs(t->tree[i].getBranchLength());
150 /**********************************************************************/
151 //This section adds in all lengths that are leaf
153 //if i's chidren are leaves
154 if (t->tree[rc].getRChild() == -1) {
155 //if rc is a valid group and rc has a BL
156 if ((inUsersGroups(t->tree[rc].getGroup(), groups) == true) && (t->tree[rc].getBranchLength() != -1)) {
157 UniqueBL += abs(t->tree[rc].getBranchLength());
158 totalBL += abs(t->tree[rc].getBranchLength());
162 if (t->tree[lc].getLChild() == -1) {
163 //if lc is a valid group and lc has a BL
164 if ((inUsersGroups(t->tree[lc].getGroup(), groups) == true) && (t->tree[lc].getBranchLength() != -1)) {
165 UniqueBL += abs(t->tree[lc].getBranchLength());
166 totalBL += abs(t->tree[lc].getBranchLength());
170 /**********************************************************************/
173 UW = (UniqueBL / totalBL);
175 if (isnan(UW) || isinf(UW)) { UW = 0; }
183 catch(exception& e) {
184 cout << "Standard Error: " << e.what() << " has occurred in the Unweighted class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
188 cout << "An unknown error has occurred in the Unweighted class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
194 /**************************************************************************************************/
196 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB) {
198 globaldata = GlobalData::getInstance();
200 vector<string> groups;
201 double UniqueBL; //a branch length is unique if it's chidren are from the same group
202 double totalBL; //all branch lengths
203 double UW; //Unweighted Value = UniqueBL / totalBL;
204 map<string, int>::iterator it; //iterator to traverse pgroups
205 map<string, int> copyIpcount;
208 //if the users enters no groups then give them the score of all groups
209 int numGroups = globaldata->Groups.size();
211 //calculate number of comparsions
213 for (int r=0; r<numGroups; r++) {
214 for (int l = r+1; l < numGroups; l++) {
219 //numComp+1 for AB, AC, BC, ABC
220 data.resize(numComp+1,0);
223 for (int a=0; a<numGroups; a++) {
224 for (int l = a+1; l < numGroups; l++) {
225 UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
226 totalBL = 0.00; //all branch lengths
227 UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
230 //copy random tree passed in
231 copyTree->getCopy(t);
233 //groups in this combo
234 groups.push_back(globaldata->Groups[a]); groups.push_back(globaldata->Groups[l]);
236 //swap labels in the groups you want to compare
237 copyTree->assembleRandomUnifracTree(groups[0], groups[1]);
239 //copyTree->createNewickFile("random"+groupA+toString(count));
241 for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
243 int lc = copyTree->tree[i].getLChild(); //lc = vector index of left child
244 int rc = copyTree->tree[i].getRChild(); //rc = vector index of right child
246 /**********************************************************************/
247 //This section adds in all lengths that are non leaf
249 copyIpcount = copyTree->tree[i].pcount;
250 for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
251 if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first); }
254 //if i's children are from the same group then i's pcount size will be 1
255 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
256 if (copyIpcount.size() == 0) { }
257 else if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { UniqueBL += abs(copyTree->tree[i].getBranchLength()); }
259 //add i's BL to total if it is from the groups the user wants
260 if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
261 totalBL += abs(copyTree->tree[i].getBranchLength());
264 /**********************************************************************/
265 //This section adds in all lengths that are leaf
267 //if i's chidren are leaves
268 if (copyTree->tree[rc].getRChild() == -1) {
269 //if rc is a valid group and rc has a BL
270 if ((inUsersGroups(copyTree->tree[rc].getGroup(), groups) == true) && (copyTree->tree[rc].getBranchLength() != -1)) {
271 UniqueBL += abs(copyTree->tree[rc].getBranchLength());
272 totalBL += abs(copyTree->tree[rc].getBranchLength());
276 if (copyTree->tree[lc].getLChild() == -1) {
277 //if lc is a valid group and lc has a BL
278 if ((inUsersGroups(copyTree->tree[lc].getGroup(), groups) == true) && (copyTree->tree[lc].getBranchLength() != -1)) {
279 UniqueBL += abs(copyTree->tree[lc].getBranchLength());
280 totalBL += abs(copyTree->tree[lc].getBranchLength());
284 /**********************************************************************/
287 UW = (UniqueBL / totalBL);
289 if (isnan(UW) || isinf(UW)) { UW = 0; }
299 if (numGroups == 0) {
300 //get score for all users groups
301 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
302 if (tmap->namesOfGroups[i] != "xxx") {
303 groups.push_back(tmap->namesOfGroups[i]);
307 for (int i = 0; i < globaldata->Groups.size(); i++) {
308 groups.push_back(globaldata->Groups[i]);
312 UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
313 totalBL = 0.00; //all branch lengths
314 UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
317 //copy random tree passed in
318 copyTree->getCopy(t);
320 //swap labels in all the groups you want to compare
321 copyTree->assembleRandomUnifracTree(groups);
323 for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
325 int lc = copyTree->tree[i].getLChild(); //lc = vector index of left child
326 int rc = copyTree->tree[i].getRChild(); //rc = vector index of right child
328 /**********************************************************************/
329 //This section adds in all lengths that are non leaf
331 copyIpcount = copyTree->tree[i].pcount;
332 for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
333 if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first); }
336 //if i's children are from the same group then i's pcount size will be 1
337 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
338 if (copyIpcount.size() == 0) { }
339 else if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { abs(UniqueBL += copyTree->tree[i].getBranchLength()); }
341 //add i's BL to total if it is from the groups the user wants
342 if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
343 totalBL += abs(copyTree->tree[i].getBranchLength());
346 /**********************************************************************/
347 //This section adds in all lengths that are leaf
349 //if i's chidren are leaves
350 if (copyTree->tree[rc].getRChild() == -1) {
351 //if rc is a valid group and rc has a BL
352 if ((inUsersGroups(copyTree->tree[rc].getGroup(), groups) == true) && (copyTree->tree[rc].getBranchLength() != -1)) {
353 UniqueBL += abs(copyTree->tree[rc].getBranchLength());
354 totalBL += abs(copyTree->tree[rc].getBranchLength());
358 if (copyTree->tree[lc].getLChild() == -1) {
359 //if lc is a valid group and lc has a BL
360 if ((inUsersGroups(copyTree->tree[lc].getGroup(), groups) == true) && (copyTree->tree[lc].getBranchLength() != -1)) {
361 UniqueBL += abs(copyTree->tree[lc].getBranchLength());
362 totalBL += abs(copyTree->tree[lc].getBranchLength());
366 /**********************************************************************/
369 UW = (UniqueBL / totalBL);
371 if (isnan(UW) || isinf(UW)) { UW = 0; }
381 catch(exception& e) {
382 cout << "Standard Error: " << e.what() << " has occurred in the Unweighted class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
386 cout << "An unknown error has occurred in the Unweighted class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";