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();) {
61 if (inUsersGroups(it->first, groups) != true) {
62 copyIpcount.erase(it++);
66 //if i's children are from the same group then i's pcount size will be 1
67 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
68 if (copyIpcount.size() == 0) { }
69 else if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { UniqueBL += abs(t->tree[i].getBranchLength()); }
71 //add i's BL to total if it is from the groups the user wants
72 if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
73 totalBL += abs(t->tree[i].getBranchLength());
76 /**********************************************************************/
77 //This section adds in all lengths that are leaf
79 //if i's chidren are leaves
80 if (t->tree[rc].getRChild() == -1) {
81 //if rc is a valid group and rc has a BL
82 if ((inUsersGroups(t->tree[rc].getGroup(), groups) == true) && (t->tree[rc].getBranchLength() != -1)) {
83 UniqueBL += abs(t->tree[rc].getBranchLength());
84 totalBL += abs(t->tree[rc].getBranchLength());
88 if (t->tree[lc].getLChild() == -1) {
89 //if lc is a valid group and lc has a BL
90 if ((inUsersGroups(t->tree[lc].getGroup(), groups) == true) && (t->tree[lc].getBranchLength() != -1)) {
91 UniqueBL += abs(t->tree[lc].getBranchLength());
92 totalBL += abs(t->tree[lc].getBranchLength());
96 /**********************************************************************/
99 UW = (UniqueBL / totalBL);
101 if (isnan(UW) || isinf(UW)) { UW = 0; }
111 if (numGroups == 0) {
112 //get score for all users groups
113 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
114 if (tmap->namesOfGroups[i] != "xxx") {
115 groups.push_back(tmap->namesOfGroups[i]);
119 for (int i = 0; i < globaldata->Groups.size(); i++) {
120 groups.push_back(globaldata->Groups[i]);
124 UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
125 totalBL = 0.00; //all branch lengths
126 UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
129 for(int i=t->getNumLeaves();i<t->getNumNodes();i++){
131 int lc = t->tree[i].getLChild(); //lc = vector index of left child
132 int rc = t->tree[i].getRChild(); //rc = vector index of right child
134 /**********************************************************************/
135 //This section adds in all lengths that are non leaf
137 copyIpcount = t->tree[i].pcount;
138 for (it = copyIpcount.begin(); it != copyIpcount.end();) {
139 if (inUsersGroups(it->first, groups) != true) {
140 copyIpcount.erase(it++);
144 //if i's children are from the same group then i's pcount size will be 1
145 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
146 if (copyIpcount.size() == 0) { }
147 else if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { UniqueBL += abs(t->tree[i].getBranchLength()); }
149 //add i's BL to total if it is from the groups the user wants
150 if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
151 totalBL += abs(t->tree[i].getBranchLength());
154 /**********************************************************************/
155 //This section adds in all lengths that are leaf
157 //if i's chidren are leaves
158 if (t->tree[rc].getRChild() == -1) {
159 //if rc is a valid group and rc has a BL
160 if ((inUsersGroups(t->tree[rc].getGroup(), groups) == true) && (t->tree[rc].getBranchLength() != -1)) {
161 UniqueBL += abs(t->tree[rc].getBranchLength());
162 totalBL += abs(t->tree[rc].getBranchLength());
166 if (t->tree[lc].getLChild() == -1) {
167 //if lc is a valid group and lc has a BL
168 if ((inUsersGroups(t->tree[lc].getGroup(), groups) == true) && (t->tree[lc].getBranchLength() != -1)) {
169 UniqueBL += abs(t->tree[lc].getBranchLength());
170 totalBL += abs(t->tree[lc].getBranchLength());
174 /**********************************************************************/
177 UW = (UniqueBL / totalBL);
179 if (isnan(UW) || isinf(UW)) { UW = 0; }
187 catch(exception& e) {
188 errorOut(e, "Unweighted", "getValues");
193 /**************************************************************************************************/
195 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB) {
197 globaldata = GlobalData::getInstance();
199 vector<string> groups;
200 double UniqueBL; //a branch length is unique if it's chidren are from the same group
201 double totalBL; //all branch lengths
202 double UW; //Unweighted Value = UniqueBL / totalBL;
203 map<string, int>::iterator it; //iterator to traverse pgroups
204 map<string, int> copyIpcount;
207 //if the users enters no groups then give them the score of all groups
208 int numGroups = globaldata->Groups.size();
210 //calculate number of comparsions
212 for (int r=0; r<numGroups; r++) {
213 for (int l = r+1; l < numGroups; l++) {
218 //numComp+1 for AB, AC, BC, ABC
219 data.resize(numComp+1,0);
222 for (int a=0; a<numGroups; a++) {
223 for (int l = a+1; l < numGroups; l++) {
224 UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
225 totalBL = 0.00; //all branch lengths
226 UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
229 //copy random tree passed in
230 copyTree->getCopy(t);
232 //groups in this combo
233 groups.push_back(globaldata->Groups[a]); groups.push_back(globaldata->Groups[l]);
235 //swap labels in the groups you want to compare
236 copyTree->assembleRandomUnifracTree(groups[0], groups[1]);
238 //copyTree->createNewickFile("random"+groupA+toString(count));
240 for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
242 int lc = copyTree->tree[i].getLChild(); //lc = vector index of left child
243 int rc = copyTree->tree[i].getRChild(); //rc = vector index of right child
245 /**********************************************************************/
246 //This section adds in all lengths that are non leaf
247 copyIpcount = copyTree->tree[i].pcount;
248 for (it = copyIpcount.begin(); it != copyIpcount.end();) {
249 if (inUsersGroups(it->first, groups) != true) {
250 copyIpcount.erase(it++);
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();) {
333 if (inUsersGroups(it->first, groups) != true) {
334 copyIpcount.erase(it++);
338 //if i's children are from the same group then i's pcount size will be 1
339 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
340 if (copyIpcount.size() == 0) { }
341 else if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { abs(UniqueBL += copyTree->tree[i].getBranchLength()); }
343 //add i's BL to total if it is from the groups the user wants
344 if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
345 totalBL += abs(copyTree->tree[i].getBranchLength());
348 /**********************************************************************/
349 //This section adds in all lengths that are leaf
351 //if i's chidren are leaves
352 if (copyTree->tree[rc].getRChild() == -1) {
353 //if rc is a valid group and rc has a BL
354 if ((inUsersGroups(copyTree->tree[rc].getGroup(), groups) == true) && (copyTree->tree[rc].getBranchLength() != -1)) {
355 UniqueBL += abs(copyTree->tree[rc].getBranchLength());
356 totalBL += abs(copyTree->tree[rc].getBranchLength());
360 if (copyTree->tree[lc].getLChild() == -1) {
361 //if lc is a valid group and lc has a BL
362 if ((inUsersGroups(copyTree->tree[lc].getGroup(), groups) == true) && (copyTree->tree[lc].getBranchLength() != -1)) {
363 UniqueBL += abs(copyTree->tree[lc].getBranchLength());
364 totalBL += abs(copyTree->tree[lc].getBranchLength());
368 /**********************************************************************/
371 UW = (UniqueBL / totalBL);
373 if (isnan(UW) || isinf(UW)) { UW = 0; }
383 catch(exception& e) {
384 errorOut(e, "Unweighted", "getValues");