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=0;i<t->getNumNodes();i++){
52 if (m->control_pressed) { return data; }
54 copyIpcount = t->tree[i].pcount;
55 for (it = copyIpcount.begin(); it != copyIpcount.end();) {
56 if (m->inUsersGroups(it->first, groups) != true) {
57 copyIpcount.erase(it++);
61 //if i's children are from the same group then i's pcount size will be 1
62 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
63 if (copyIpcount.size() == 0) { }
64 else if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { UniqueBL += abs(t->tree[i].getBranchLength()); }
66 //add i's BL to total if it is from the groups the user wants
67 if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
68 totalBL += abs(t->tree[i].getBranchLength());
73 UW = (UniqueBL / totalBL);
74 //cout << globaldata->Groups[a] << globaldata->Groups[l] << '\t' << UniqueBL << '\t' << totalBL << endl;
76 if (isnan(UW) || isinf(UW)) { UW = 0; }
87 //get score for all users groups
88 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
89 if (tmap->namesOfGroups[i] != "xxx") {
90 groups.push_back(tmap->namesOfGroups[i]);
94 for (int i = 0; i < globaldata->Groups.size(); i++) {
95 groups.push_back(globaldata->Groups[i]);
99 UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
100 totalBL = 0.00; //all branch lengths
101 UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
104 for(int i=0;i<t->getNumNodes();i++){
106 if (m->control_pressed) { return data; }
108 copyIpcount = t->tree[i].pcount;
109 for (it = copyIpcount.begin(); it != copyIpcount.end();) {
110 if (m->inUsersGroups(it->first, groups) != true) {
111 copyIpcount.erase(it++);
115 //if i's children are from the same group then i's pcount size will be 1
116 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
117 if (copyIpcount.size() == 0) { }
118 else if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { UniqueBL += abs(t->tree[i].getBranchLength()); }
120 //add i's BL to total if it is from the groups the user wants
121 if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
122 totalBL += abs(t->tree[i].getBranchLength());
127 UW = (UniqueBL / totalBL);
129 if (isnan(UW) || isinf(UW)) { UW = 0; }
137 catch(exception& e) {
138 m->errorOut(e, "Unweighted", "getValues");
143 /**************************************************************************************************/
145 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB) {
147 globaldata = GlobalData::getInstance();
149 vector<string> groups;
150 double UniqueBL; //a branch length is unique if it's chidren are from the same group
151 double totalBL; //all branch lengths
152 double UW; //Unweighted Value = UniqueBL / totalBL;
153 map<string, int>::iterator it; //iterator to traverse pgroups
154 map<string, int> copyIpcount;
157 //if the users enters no groups then give them the score of all groups
158 int numGroups = globaldata->Groups.size();
160 //calculate number of comparsions
162 for (int r=0; r<numGroups; r++) {
163 for (int l = r+1; l < numGroups; l++) {
168 //numComp+1 for AB, AC, BC, ABC
169 data.resize(numComp+1,0);
172 for (int a=0; a<numGroups; a++) {
173 for (int l = a+1; l < numGroups; l++) {
174 UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
175 totalBL = 0.00; //all branch lengths
176 UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
179 //copy random tree passed in
180 copyTree->getCopy(t);
182 //groups in this combo
183 groups.push_back(globaldata->Groups[a]); groups.push_back(globaldata->Groups[l]);
185 //swap labels in the groups you want to compare
186 copyTree->assembleRandomUnifracTree(groups[0], groups[1]);
188 if (m->control_pressed) { delete copyTree; return data; }
190 //copyTree->createNewickFile("random"+groupA+toString(count));
192 for(int i=0;i<copyTree->getNumNodes();i++){
194 if (m->control_pressed) { delete copyTree; return data; }
196 /**********************************************************************/
197 //This section adds in all lengths that are non leaf
198 copyIpcount = copyTree->tree[i].pcount;
199 for (it = copyIpcount.begin(); it != copyIpcount.end();) {
200 if (m->inUsersGroups(it->first, groups) != true) {
201 copyIpcount.erase(it++);
205 //if i's children are from the same group then i's pcount size will be 1
206 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
207 if (copyIpcount.size() == 0) { }
208 else if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { UniqueBL += abs(copyTree->tree[i].getBranchLength()); }
210 //add i's BL to total if it is from the groups the user wants
211 if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
212 totalBL += abs(copyTree->tree[i].getBranchLength());
217 UW = (UniqueBL / totalBL);
219 if (isnan(UW) || isinf(UW)) { UW = 0; }
229 if (numGroups == 0) {
230 //get score for all users groups
231 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
232 if (tmap->namesOfGroups[i] != "xxx") {
233 groups.push_back(tmap->namesOfGroups[i]);
237 for (int i = 0; i < globaldata->Groups.size(); i++) {
238 groups.push_back(globaldata->Groups[i]);
242 UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
243 totalBL = 0.00; //all branch lengths
244 UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
247 //copy random tree passed in
248 copyTree->getCopy(t);
250 //swap labels in all the groups you want to compare
251 copyTree->assembleRandomUnifracTree(groups);
253 if (m->control_pressed) { delete copyTree; return data; }
255 for(int i=0;i<copyTree->getNumNodes();i++){
256 if (m->control_pressed) { delete copyTree; return data; }
258 copyIpcount = copyTree->tree[i].pcount;
259 for (it = copyIpcount.begin(); it != copyIpcount.end();) {
260 if (m->inUsersGroups(it->first, groups) != true) {
261 copyIpcount.erase(it++);
265 //if i's children are from the same group then i's pcount size will be 1
266 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
267 if (copyIpcount.size() == 0) { }
268 else if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { abs(UniqueBL += copyTree->tree[i].getBranchLength()); }
270 //add i's BL to total if it is from the groups the user wants
271 if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
272 totalBL += abs(copyTree->tree[i].getBranchLength());
277 UW = (UniqueBL / totalBL);
279 if (isnan(UW) || isinf(UW)) { UW = 0; }
289 catch(exception& e) {
290 m->errorOut(e, "Unweighted", "getValues");