]> git.donarmstrong.com Git - mothur.git/blob - unweighted.cpp
fixed bug in unweighted
[mothur.git] / unweighted.cpp
1 /*
2  *  unweighted.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 2/9/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "unweighted.h"
11
12 /**************************************************************************************************/
13
14 EstOutput Unweighted::getValues(Tree* t) {
15         try {
16                 globaldata = GlobalData::getInstance();
17                 
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;
24
25         
26                 //if the users enters no groups then give them the score of all groups
27                 int numGroups = globaldata->Groups.size();
28                 
29                 //calculate number of comparsions
30                 int numComp = 0;
31                 for (int r=0; r<numGroups; r++) { 
32                         for (int l = r+1; l < numGroups; l++) {
33                                 numComp++;
34                         }
35                 }
36
37                 //numComp+1 for AB, AC, BC, ABC
38                 data.resize(numComp+1,0);
39                 
40                 int count = 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;
46                                 copyIpcount.clear();
47                                 
48                                 //groups in this combo
49                                 groups.push_back(globaldata->Groups[a]); groups.push_back(globaldata->Groups[l]);
50                 
51                                 for(int i=t->getNumLeaves();i<t->getNumNodes();i++){
52                 
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
55                         
56                                         /**********************************************************************/
57                                         //This section adds in all lengths that are non leaf
58                         
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);   }
62                                         }
63                         
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 += t->tree[i].getBranchLength();        }
68                         
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 += t->tree[i].getBranchLength(); 
72                                         }
73                         
74                                         /**********************************************************************/
75                                         //This section adds in all lengths that are leaf
76                         
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 += t->tree[rc].getBranchLength();
82                                                         totalBL += t->tree[rc].getBranchLength(); 
83                                                 }
84                                         }
85                         
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 += t->tree[lc].getBranchLength();
90                                                         totalBL += t->tree[lc].getBranchLength(); 
91                                                 }
92                                         }
93                         
94                                         /**********************************************************************/
95                                 }
96                 
97                                 UW = (UniqueBL / totalBL);  
98         
99                                 if (isnan(UW) || isinf(UW)) { UW = 0; }
100         
101                                 data[count] = UW;
102                                 count++;
103                                 groups.clear();
104                         }
105                 }
106                 
107                 
108                 if (numComp != 1) {
109                         if (numGroups == 0) {
110                                 //get score for all users groups
111                                 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
112                                         groups.push_back(tmap->namesOfGroups[i]);
113                                 }
114                         }else {
115                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
116                                         groups.push_back(globaldata->Groups[i]);
117                                 }
118                         }
119                 
120                         UniqueBL=0.0000;  //a branch length is unique if it's chidren are from the same group
121                         totalBL = 0.00; //all branch lengths
122                         UW = 0.00;              //Unweighted Value = UniqueBL / totalBL;
123                         copyIpcount.clear();
124                                 
125                         for(int i=t->getNumLeaves();i<t->getNumNodes();i++){
126                 
127                                 int lc = t->tree[i].getLChild();  //lc = vector index of left child
128                                 int rc = t->tree[i].getRChild();  //rc = vector index of right child
129                         
130                                 /**********************************************************************/
131                                 //This section adds in all lengths that are non leaf
132                         
133                                 copyIpcount = t->tree[i].pcount;
134                                 for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
135                                         if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first);   }
136                                 }
137                         
138                                 //if i's children are from the same group then i's pcount size will be 1 
139                                 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
140                                 if (copyIpcount.size() == 0) { }
141                                 else if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) {  UniqueBL += t->tree[i].getBranchLength();        }
142                         
143                                 //add i's BL to total if it is from the groups the user wants
144                                 if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {  
145                                         totalBL += t->tree[i].getBranchLength(); 
146                                 }
147                         
148                                 /**********************************************************************/
149                                 //This section adds in all lengths that are leaf
150                         
151                                 //if i's chidren are leaves
152                                 if (t->tree[rc].getRChild() == -1) {
153                                         //if rc is a valid group and rc has a BL
154                                         if ((inUsersGroups(t->tree[rc].getGroup(), groups) == true) && (t->tree[rc].getBranchLength() != -1)) {
155                                                 UniqueBL += t->tree[rc].getBranchLength();
156                                                 totalBL += t->tree[rc].getBranchLength(); 
157                                         }
158                                 }
159                         
160                                 if (t->tree[lc].getLChild() == -1) {
161                                         //if lc is a valid group and lc has a BL
162                                         if ((inUsersGroups(t->tree[lc].getGroup(), groups) == true) && (t->tree[lc].getBranchLength() != -1)) {
163                                                 UniqueBL += t->tree[lc].getBranchLength();
164                                                 totalBL += t->tree[lc].getBranchLength(); 
165                                         }
166                                 }
167                         
168                                 /**********************************************************************/
169                         }
170                 
171                         UW = (UniqueBL / totalBL);  
172         
173                         if (isnan(UW) || isinf(UW)) { UW = 0; }
174         
175                         data[count] = UW;
176                 }
177
178                 return data;
179         
180         }
181         catch(exception& e) {
182                 cout << "Standard Error: " << e.what() << " has occurred in the Unweighted class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
183                 exit(1);
184         }
185         catch(...) {
186                 cout << "An unknown error has occurred in the Unweighted class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
187                 exit(1);
188         }
189
190 }
191
192 /**************************************************************************************************/
193
194 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB) { 
195  try {
196         globaldata = GlobalData::getInstance();
197                 
198                 vector<string> groups;
199                 double UniqueBL;  //a branch length is unique if it's chidren are from the same group
200                 double totalBL; //all branch lengths
201                 double UW;              //Unweighted Value = UniqueBL / totalBL;
202                 map<string, int>::iterator it;  //iterator to traverse pgroups
203                 map<string, int> copyIpcount;
204                 copyTree = new Tree;
205
206                 //if the users enters no groups then give them the score of all groups
207                 int numGroups = globaldata->Groups.size();
208                 
209                 //calculate number of comparsions
210                 int numComp = 0;
211                 for (int r=0; r<numGroups; r++) { 
212                         for (int l = r+1; l < numGroups; l++) {
213                                 numComp++;
214                         }
215                 }
216
217                 //numComp+1 for AB, AC, BC, ABC
218                 data.resize(numComp+1,0);
219                 
220                 int count = 0;
221                 for (int a=0; a<numGroups; a++) { 
222                         for (int l = a+1; l < numGroups; l++) {
223                                 UniqueBL=0.0000;  //a branch length is unique if it's chidren are from the same group
224                                 totalBL = 0.00; //all branch lengths
225                                 UW = 0.00;              //Unweighted Value = UniqueBL / totalBL;
226                                 copyIpcount.clear();
227                                 
228                                 //copy random tree passed in
229                                 copyTree->getCopy(t);
230                                 
231                                 //swap labels in the groups you want to compare
232                                 copyTree->assembleRandomUnifracTree(globaldata->Groups[a], globaldata->Groups[l]);
233                                 
234                                 //groups in this combo
235                                 groups.push_back(globaldata->Groups[a]); groups.push_back(globaldata->Groups[l]);
236                 
237                                 for(int i=t->getNumLeaves();i<t->getNumNodes();i++){
238                 
239                                         int lc = t->tree[i].getLChild();  //lc = vector index of left child
240                                         int rc = t->tree[i].getRChild();  //rc = vector index of right child
241                         
242                                         /**********************************************************************/
243                                         //This section adds in all lengths that are non leaf
244                         
245                                         copyIpcount = t->tree[i].pcount;
246                                         for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
247                                                 if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first);   }
248                                         }
249                         
250                                         //if i's children are from the same group then i's pcount size will be 1 
251                                         //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
252                                         if (copyIpcount.size() == 0) { }
253                                         else if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) {  UniqueBL += t->tree[i].getBranchLength();        }
254                         
255                                         //add i's BL to total if it is from the groups the user wants
256                                         if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {  
257                                                 totalBL += t->tree[i].getBranchLength(); 
258                                         }
259                         
260                                         /**********************************************************************/
261                                         //This section adds in all lengths that are leaf
262                         
263                                         //if i's chidren are leaves
264                                         if (t->tree[rc].getRChild() == -1) {
265                                                 //if rc is a valid group and rc has a BL
266                                                 if ((inUsersGroups(t->tree[rc].getGroup(), groups) == true) && (t->tree[rc].getBranchLength() != -1)) {
267                                                         UniqueBL += t->tree[rc].getBranchLength();
268                                                         totalBL += t->tree[rc].getBranchLength(); 
269                                                 }
270                                         }
271                         
272                                         if (t->tree[lc].getLChild() == -1) {
273                                                 //if lc is a valid group and lc has a BL
274                                                 if ((inUsersGroups(t->tree[lc].getGroup(), groups) == true) && (t->tree[lc].getBranchLength() != -1)) {
275                                                         UniqueBL += t->tree[lc].getBranchLength();
276                                                         totalBL += t->tree[lc].getBranchLength(); 
277                                                 }
278                                         }
279                         
280                                         /**********************************************************************/
281                                 }
282                 
283                                 UW = (UniqueBL / totalBL);  
284         
285                                 if (isnan(UW) || isinf(UW)) { UW = 0; }
286         
287                                 data[count] = UW;
288                                 count++;
289                                 groups.clear();
290                         }
291                 }
292                 
293                 
294                 if (numComp != 1) {
295                         if (numGroups == 0) {
296                                 //get score for all users groups
297                                 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
298                                         groups.push_back(tmap->namesOfGroups[i]);
299                                 }
300                         }else {
301                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
302                                         groups.push_back(globaldata->Groups[i]);
303                                 }
304                         }
305                 
306                         UniqueBL=0.0000;  //a branch length is unique if it's chidren are from the same group
307                         totalBL = 0.00; //all branch lengths
308                         UW = 0.00;              //Unweighted Value = UniqueBL / totalBL;
309                         copyIpcount.clear();
310                 
311                         //copy random tree passed in
312                         copyTree->getCopy(t);
313                                 
314                         //swap labels in all the groups you want to compare
315                         copyTree->assembleRandomUnifracTree();
316
317                         for(int i=t->getNumLeaves();i<t->getNumNodes();i++){
318                 
319                                 int lc = t->tree[i].getLChild();  //lc = vector index of left child
320                                 int rc = t->tree[i].getRChild();  //rc = vector index of right child
321                         
322                                 /**********************************************************************/
323                                 //This section adds in all lengths that are non leaf
324                         
325                                 copyIpcount = t->tree[i].pcount;
326                                 for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
327                                         if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first);   }
328                                 }
329                         
330                                 //if i's children are from the same group then i's pcount size will be 1 
331                                 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
332                                 if (copyIpcount.size() == 0) { }
333                                 else if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) {  UniqueBL += t->tree[i].getBranchLength();        }
334                         
335                                 //add i's BL to total if it is from the groups the user wants
336                                 if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {  
337                                         totalBL += t->tree[i].getBranchLength(); 
338                                 }
339                         
340                                 /**********************************************************************/
341                                 //This section adds in all lengths that are leaf
342                         
343                                 //if i's chidren are leaves
344                                 if (t->tree[rc].getRChild() == -1) {
345                                         //if rc is a valid group and rc has a BL
346                                         if ((inUsersGroups(t->tree[rc].getGroup(), groups) == true) && (t->tree[rc].getBranchLength() != -1)) {
347                                                 UniqueBL += t->tree[rc].getBranchLength();
348                                                 totalBL += t->tree[rc].getBranchLength(); 
349                                         }
350                                 }
351                         
352                                 if (t->tree[lc].getLChild() == -1) {
353                                         //if lc is a valid group and lc has a BL
354                                         if ((inUsersGroups(t->tree[lc].getGroup(), groups) == true) && (t->tree[lc].getBranchLength() != -1)) {
355                                                 UniqueBL += t->tree[lc].getBranchLength();
356                                                 totalBL += t->tree[lc].getBranchLength(); 
357                                         }
358                                 }
359                         
360                                 /**********************************************************************/
361                         }
362                 
363                         UW = (UniqueBL / totalBL);  
364         
365                         if (isnan(UW) || isinf(UW)) { UW = 0; }
366         
367                         data[count] = UW;
368                 }
369
370                 return data;
371         
372         }
373         catch(exception& e) {
374                 cout << "Standard Error: " << e.what() << " has occurred in the Unweighted class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
375                 exit(1);
376         }
377         catch(...) {
378                 cout << "An unknown error has occurred in the Unweighted class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
379                 exit(1);
380         }
381 }
382
383
384
385