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 errorOut(e, "Unweighted", "getValues");
189 /**************************************************************************************************/
191 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB) {
193 globaldata = GlobalData::getInstance();
195 vector<string> groups;
196 double UniqueBL; //a branch length is unique if it's chidren are from the same group
197 double totalBL; //all branch lengths
198 double UW; //Unweighted Value = UniqueBL / totalBL;
199 map<string, int>::iterator it; //iterator to traverse pgroups
200 map<string, int> copyIpcount;
203 //if the users enters no groups then give them the score of all groups
204 int numGroups = globaldata->Groups.size();
206 //calculate number of comparsions
208 for (int r=0; r<numGroups; r++) {
209 for (int l = r+1; l < numGroups; l++) {
214 //numComp+1 for AB, AC, BC, ABC
215 data.resize(numComp+1,0);
218 for (int a=0; a<numGroups; a++) {
219 for (int l = a+1; l < numGroups; l++) {
220 UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
221 totalBL = 0.00; //all branch lengths
222 UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
225 //copy random tree passed in
226 copyTree->getCopy(t);
228 //groups in this combo
229 groups.push_back(globaldata->Groups[a]); groups.push_back(globaldata->Groups[l]);
231 //swap labels in the groups you want to compare
232 copyTree->assembleRandomUnifracTree(groups[0], groups[1]);
234 //copyTree->createNewickFile("random"+groupA+toString(count));
236 for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
238 int lc = copyTree->tree[i].getLChild(); //lc = vector index of left child
239 int rc = copyTree->tree[i].getRChild(); //rc = vector index of right child
241 /**********************************************************************/
242 //This section adds in all lengths that are non leaf
244 copyIpcount = copyTree->tree[i].pcount;
245 for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
246 if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first); }
249 //if i's children are from the same group then i's pcount size will be 1
250 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
251 if (copyIpcount.size() == 0) { }
252 else if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { UniqueBL += abs(copyTree->tree[i].getBranchLength()); }
254 //add i's BL to total if it is from the groups the user wants
255 if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
256 totalBL += abs(copyTree->tree[i].getBranchLength());
259 /**********************************************************************/
260 //This section adds in all lengths that are leaf
262 //if i's chidren are leaves
263 if (copyTree->tree[rc].getRChild() == -1) {
264 //if rc is a valid group and rc has a BL
265 if ((inUsersGroups(copyTree->tree[rc].getGroup(), groups) == true) && (copyTree->tree[rc].getBranchLength() != -1)) {
266 UniqueBL += abs(copyTree->tree[rc].getBranchLength());
267 totalBL += abs(copyTree->tree[rc].getBranchLength());
271 if (copyTree->tree[lc].getLChild() == -1) {
272 //if lc is a valid group and lc has a BL
273 if ((inUsersGroups(copyTree->tree[lc].getGroup(), groups) == true) && (copyTree->tree[lc].getBranchLength() != -1)) {
274 UniqueBL += abs(copyTree->tree[lc].getBranchLength());
275 totalBL += abs(copyTree->tree[lc].getBranchLength());
279 /**********************************************************************/
282 UW = (UniqueBL / totalBL);
284 if (isnan(UW) || isinf(UW)) { UW = 0; }
294 if (numGroups == 0) {
295 //get score for all users groups
296 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
297 if (tmap->namesOfGroups[i] != "xxx") {
298 groups.push_back(tmap->namesOfGroups[i]);
302 for (int i = 0; i < globaldata->Groups.size(); i++) {
303 groups.push_back(globaldata->Groups[i]);
307 UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
308 totalBL = 0.00; //all branch lengths
309 UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
312 //copy random tree passed in
313 copyTree->getCopy(t);
315 //swap labels in all the groups you want to compare
316 copyTree->assembleRandomUnifracTree(groups);
318 for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
320 int lc = copyTree->tree[i].getLChild(); //lc = vector index of left child
321 int rc = copyTree->tree[i].getRChild(); //rc = vector index of right child
323 /**********************************************************************/
324 //This section adds in all lengths that are non leaf
326 copyIpcount = copyTree->tree[i].pcount;
327 for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
328 if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first); }
331 //if i's children are from the same group then i's pcount size will be 1
332 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
333 if (copyIpcount.size() == 0) { }
334 else if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { abs(UniqueBL += copyTree->tree[i].getBranchLength()); }
336 //add i's BL to total if it is from the groups the user wants
337 if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
338 totalBL += abs(copyTree->tree[i].getBranchLength());
341 /**********************************************************************/
342 //This section adds in all lengths that are leaf
344 //if i's chidren are leaves
345 if (copyTree->tree[rc].getRChild() == -1) {
346 //if rc is a valid group and rc has a BL
347 if ((inUsersGroups(copyTree->tree[rc].getGroup(), groups) == true) && (copyTree->tree[rc].getBranchLength() != -1)) {
348 UniqueBL += abs(copyTree->tree[rc].getBranchLength());
349 totalBL += abs(copyTree->tree[rc].getBranchLength());
353 if (copyTree->tree[lc].getLChild() == -1) {
354 //if lc is a valid group and lc has a BL
355 if ((inUsersGroups(copyTree->tree[lc].getGroup(), groups) == true) && (copyTree->tree[lc].getBranchLength() != -1)) {
356 UniqueBL += abs(copyTree->tree[lc].getBranchLength());
357 totalBL += abs(copyTree->tree[lc].getBranchLength());
361 /**********************************************************************/
364 UW = (UniqueBL / totalBL);
366 if (isnan(UW) || isinf(UW)) { UW = 0; }
376 catch(exception& e) {
377 errorOut(e, "Unweighted", "getValues");