]> git.donarmstrong.com Git - mothur.git/blob - unweighted.cpp
fixed bug in read.tree
[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                                 //groups in this combo
232                                 groups.push_back(globaldata->Groups[a]); groups.push_back(globaldata->Groups[l]);
233                                 
234                                 //swap labels in the groups you want to compare
235                                 copyTree->assembleRandomUnifracTree(groups[0], groups[1]);
236                                 
237                                 //copyTree->createNewickFile("random"+groupA+toString(count));
238                 
239                                 for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
240                 
241                                         int lc = copyTree->tree[i].getLChild();  //lc = vector index of left child
242                                         int rc = copyTree->tree[i].getRChild();  //rc = vector index of right child
243                         
244                                         /**********************************************************************/
245                                         //This section adds in all lengths that are non leaf
246                         
247                                         copyIpcount = copyTree->tree[i].pcount;
248                                         for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
249                                                 if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first);   }
250                                         }
251                         
252                                         //if i's children are from the same group then i's pcount size will be 1 
253                                         //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
254                                         if (copyIpcount.size() == 0) { }
255                                         else if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) {  UniqueBL += copyTree->tree[i].getBranchLength();  }
256                         
257                                         //add i's BL to total if it is from the groups the user wants
258                                         if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {  
259                                                 totalBL += copyTree->tree[i].getBranchLength(); 
260                                         }
261                         
262                                         /**********************************************************************/
263                                         //This section adds in all lengths that are leaf
264                         
265                                         //if i's chidren are leaves
266                                         if (copyTree->tree[rc].getRChild() == -1) {
267                                                 //if rc is a valid group and rc has a BL
268                                                 if ((inUsersGroups(copyTree->tree[rc].getGroup(), groups) == true) && (copyTree->tree[rc].getBranchLength() != -1)) {
269                                                         UniqueBL += copyTree->tree[rc].getBranchLength();
270                                                         totalBL += copyTree->tree[rc].getBranchLength(); 
271                                                 }
272                                         }
273                         
274                                         if (copyTree->tree[lc].getLChild() == -1) {
275                                                 //if lc is a valid group and lc has a BL
276                                                 if ((inUsersGroups(copyTree->tree[lc].getGroup(), groups) == true) && (copyTree->tree[lc].getBranchLength() != -1)) {
277                                                         UniqueBL += copyTree->tree[lc].getBranchLength();
278                                                         totalBL += copyTree->tree[lc].getBranchLength(); 
279                                                 }
280                                         }
281                         
282                                         /**********************************************************************/
283                                 }
284                 
285                                 UW = (UniqueBL / totalBL);  
286         
287                                 if (isnan(UW) || isinf(UW)) { UW = 0; }
288         
289                                 data[count] = UW;
290                                 count++;
291                                 groups.clear();
292                         }
293                 }
294                 
295                 
296                 if (numComp != 1) {
297                         if (numGroups == 0) {
298                                 //get score for all users groups
299                                 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
300                                         groups.push_back(tmap->namesOfGroups[i]);
301                                 }
302                         }else {
303                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
304                                         groups.push_back(globaldata->Groups[i]);
305                                 }
306                         }
307                 
308                         UniqueBL=0.0000;  //a branch length is unique if it's chidren are from the same group
309                         totalBL = 0.00; //all branch lengths
310                         UW = 0.00;              //Unweighted Value = UniqueBL / totalBL;
311                         copyIpcount.clear();
312                 
313                         //copy random tree passed in
314                         copyTree->getCopy(t);
315                                 
316                         //swap labels in all the groups you want to compare
317                         copyTree->assembleRandomUnifracTree(groups);
318
319                         for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
320                 
321                                 int lc = copyTree->tree[i].getLChild();  //lc = vector index of left child
322                                 int rc = copyTree->tree[i].getRChild();  //rc = vector index of right child
323                         
324                                 /**********************************************************************/
325                                 //This section adds in all lengths that are non leaf
326                         
327                                 copyIpcount = copyTree->tree[i].pcount;
328                                 for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
329                                         if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first);   }
330                                 }
331                         
332                                 //if i's children are from the same group then i's pcount size will be 1 
333                                 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
334                                 if (copyIpcount.size() == 0) { }
335                                 else if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) {  UniqueBL += copyTree->tree[i].getBranchLength();  }
336                         
337                                 //add i's BL to total if it is from the groups the user wants
338                                 if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {  
339                                         totalBL += copyTree->tree[i].getBranchLength(); 
340                                 }
341                         
342                                 /**********************************************************************/
343                                 //This section adds in all lengths that are leaf
344                         
345                                 //if i's chidren are leaves
346                                 if (copyTree->tree[rc].getRChild() == -1) {
347                                         //if rc is a valid group and rc has a BL
348                                         if ((inUsersGroups(copyTree->tree[rc].getGroup(), groups) == true) && (copyTree->tree[rc].getBranchLength() != -1)) {
349                                                 UniqueBL += copyTree->tree[rc].getBranchLength();
350                                                 totalBL += copyTree->tree[rc].getBranchLength(); 
351                                         }
352                                 }
353                         
354                                 if (copyTree->tree[lc].getLChild() == -1) {
355                                         //if lc is a valid group and lc has a BL
356                                         if ((inUsersGroups(copyTree->tree[lc].getGroup(), groups) == true) && (copyTree->tree[lc].getBranchLength() != -1)) {
357                                                 UniqueBL += copyTree->tree[lc].getBranchLength();
358                                                 totalBL += copyTree->tree[lc].getBranchLength(); 
359                                         }
360                                 }
361                         
362                                 /**********************************************************************/
363                         }
364                 
365                         UW = (UniqueBL / totalBL);  
366         
367                         if (isnan(UW) || isinf(UW)) { UW = 0; }
368         
369                         data[count] = UW;
370                 }
371                 
372                 delete copyTree;
373                 
374                 return data;
375         
376         }
377         catch(exception& e) {
378                 cout << "Standard Error: " << e.what() << " has occurred in the Unweighted class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
379                 exit(1);
380         }
381         catch(...) {
382                 cout << "An unknown error has occurred in the Unweighted class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
383                 exit(1);
384         }
385 }
386
387
388
389