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++){
53 copyIpcount = t->tree[i].pcount;
54 for (it = copyIpcount.begin(); it != copyIpcount.end();) {
55 if (inUsersGroups(it->first, groups) != true) {
56 copyIpcount.erase(it++);
60 //if i's children are from the same group then i's pcount size will be 1
61 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
62 if (copyIpcount.size() == 0) { }
63 else if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { UniqueBL += abs(t->tree[i].getBranchLength()); }
65 //add i's BL to total if it is from the groups the user wants
66 if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
67 totalBL += abs(t->tree[i].getBranchLength());
72 UW = (UniqueBL / totalBL);
73 //cout << globaldata->Groups[a] << globaldata->Groups[l] << '\t' << UniqueBL << '\t' << totalBL << endl;
75 if (isnan(UW) || isinf(UW)) { UW = 0; }
86 //get score for all users groups
87 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
88 if (tmap->namesOfGroups[i] != "xxx") {
89 groups.push_back(tmap->namesOfGroups[i]);
93 for (int i = 0; i < globaldata->Groups.size(); i++) {
94 groups.push_back(globaldata->Groups[i]);
98 UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
99 totalBL = 0.00; //all branch lengths
100 UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
103 for(int i=0;i<t->getNumNodes();i++){
105 copyIpcount = t->tree[i].pcount;
106 for (it = copyIpcount.begin(); it != copyIpcount.end();) {
107 if (inUsersGroups(it->first, groups) != true) {
108 copyIpcount.erase(it++);
112 //if i's children are from the same group then i's pcount size will be 1
113 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
114 if (copyIpcount.size() == 0) { }
115 else if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { UniqueBL += abs(t->tree[i].getBranchLength()); }
117 //add i's BL to total if it is from the groups the user wants
118 if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
119 totalBL += abs(t->tree[i].getBranchLength());
124 UW = (UniqueBL / totalBL);
126 if (isnan(UW) || isinf(UW)) { UW = 0; }
134 catch(exception& e) {
135 m->errorOut(e, "Unweighted", "getValues");
140 /**************************************************************************************************/
142 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB) {
144 globaldata = GlobalData::getInstance();
146 vector<string> groups;
147 double UniqueBL; //a branch length is unique if it's chidren are from the same group
148 double totalBL; //all branch lengths
149 double UW; //Unweighted Value = UniqueBL / totalBL;
150 map<string, int>::iterator it; //iterator to traverse pgroups
151 map<string, int> copyIpcount;
154 //if the users enters no groups then give them the score of all groups
155 int numGroups = globaldata->Groups.size();
157 //calculate number of comparsions
159 for (int r=0; r<numGroups; r++) {
160 for (int l = r+1; l < numGroups; l++) {
165 //numComp+1 for AB, AC, BC, ABC
166 data.resize(numComp+1,0);
169 for (int a=0; a<numGroups; a++) {
170 for (int l = a+1; l < numGroups; l++) {
171 UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
172 totalBL = 0.00; //all branch lengths
173 UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
176 //copy random tree passed in
177 copyTree->getCopy(t);
179 //groups in this combo
180 groups.push_back(globaldata->Groups[a]); groups.push_back(globaldata->Groups[l]);
182 //swap labels in the groups you want to compare
183 copyTree->assembleRandomUnifracTree(groups[0], groups[1]);
185 //copyTree->createNewickFile("random"+groupA+toString(count));
187 for(int i=0;i<copyTree->getNumNodes();i++){
189 /**********************************************************************/
190 //This section adds in all lengths that are non leaf
191 copyIpcount = copyTree->tree[i].pcount;
192 for (it = copyIpcount.begin(); it != copyIpcount.end();) {
193 if (inUsersGroups(it->first, groups) != true) {
194 copyIpcount.erase(it++);
198 //if i's children are from the same group then i's pcount size will be 1
199 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
200 if (copyIpcount.size() == 0) { }
201 else if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { UniqueBL += abs(copyTree->tree[i].getBranchLength()); }
203 //add i's BL to total if it is from the groups the user wants
204 if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
205 totalBL += abs(copyTree->tree[i].getBranchLength());
210 UW = (UniqueBL / totalBL);
212 if (isnan(UW) || isinf(UW)) { UW = 0; }
222 if (numGroups == 0) {
223 //get score for all users groups
224 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
225 if (tmap->namesOfGroups[i] != "xxx") {
226 groups.push_back(tmap->namesOfGroups[i]);
230 for (int i = 0; i < globaldata->Groups.size(); i++) {
231 groups.push_back(globaldata->Groups[i]);
235 UniqueBL=0.0000; //a branch length is unique if it's chidren are from the same group
236 totalBL = 0.00; //all branch lengths
237 UW = 0.00; //Unweighted Value = UniqueBL / totalBL;
240 //copy random tree passed in
241 copyTree->getCopy(t);
243 //swap labels in all the groups you want to compare
244 copyTree->assembleRandomUnifracTree(groups);
246 for(int i=0;i<copyTree->getNumNodes();i++){
248 copyIpcount = copyTree->tree[i].pcount;
249 for (it = copyIpcount.begin(); it != copyIpcount.end();) {
250 if (inUsersGroups(it->first, groups) != true) {
251 copyIpcount.erase(it++);
255 //if i's children are from the same group then i's pcount size will be 1
256 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
257 if (copyIpcount.size() == 0) { }
258 else if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) { abs(UniqueBL += copyTree->tree[i].getBranchLength()); }
260 //add i's BL to total if it is from the groups the user wants
261 if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {
262 totalBL += abs(copyTree->tree[i].getBranchLength());
267 UW = (UniqueBL / totalBL);
269 if (isnan(UW) || isinf(UW)) { UW = 0; }
279 catch(exception& e) {
280 m->errorOut(e, "Unweighted", "getValues");