]> git.donarmstrong.com Git - mothur.git/blob - unweighted.cpp
bugs fixes while testing for 1.5 release
[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();) {
61                                                 if (inUsersGroups(it->first, groups) != true) { 
62                                                         copyIpcount.erase(it++);        
63                                                 }else { it++;  }
64                                         }
65                         
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());   }
70                         
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()); 
74                                         }
75                         
76                                         /**********************************************************************/
77                                         //This section adds in all lengths that are leaf
78                         
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()); 
85                                                 }
86                                         }
87                         
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()); 
93                                                 }
94                                         }
95                         
96                                         /**********************************************************************/
97                                 }
98                 
99                                 UW = (UniqueBL / totalBL);  
100         
101                                 if (isnan(UW) || isinf(UW)) { UW = 0; }
102         
103                                 data[count] = UW;
104                                 count++;
105                                 groups.clear();
106                         }
107                 }
108                 
109                 
110                 if (numComp != 1) {
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]);
116                                         }
117                                 }
118                         }else {
119                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
120                                         groups.push_back(globaldata->Groups[i]);
121                                 }
122                         }
123                 
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;
127                         copyIpcount.clear();
128                                 
129                         for(int i=t->getNumLeaves();i<t->getNumNodes();i++){
130                 
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
133                         
134                                 /**********************************************************************/
135                                 //This section adds in all lengths that are non leaf
136                         
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++);        
141                                         }else {  it++;  }
142                                 }
143                         
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());   }
148                         
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()); 
152                                 }
153                         
154                                 /**********************************************************************/
155                                 //This section adds in all lengths that are leaf
156                         
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()); 
163                                         }
164                                 }
165                         
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()); 
171                                         }
172                                 }
173                         
174                                 /**********************************************************************/
175                         }
176                 
177                         UW = (UniqueBL / totalBL);  
178         
179                         if (isnan(UW) || isinf(UW)) { UW = 0; }
180         
181                         data[count] = UW;
182                 }
183
184                 return data;
185         
186         }
187         catch(exception& e) {
188                 errorOut(e, "Unweighted", "getValues");
189                 exit(1);
190         }
191 }
192
193 /**************************************************************************************************/
194
195 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB) { 
196  try {
197         globaldata = GlobalData::getInstance();
198                 
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;
205                 copyTree = new Tree;
206
207                 //if the users enters no groups then give them the score of all groups
208                 int numGroups = globaldata->Groups.size();
209
210                 //calculate number of comparsions
211                 int numComp = 0;
212                 for (int r=0; r<numGroups; r++) { 
213                         for (int l = r+1; l < numGroups; l++) {
214                                 numComp++;
215                         }
216                 }
217
218                 //numComp+1 for AB, AC, BC, ABC
219                 data.resize(numComp+1,0);
220                 
221                 int count = 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;
227                                 copyIpcount.clear();
228                                 
229                                 //copy random tree passed in
230                                 copyTree->getCopy(t);
231                                                                 
232                                 //groups in this combo
233                                 groups.push_back(globaldata->Groups[a]); groups.push_back(globaldata->Groups[l]);
234                                 
235                                 //swap labels in the groups you want to compare
236                                 copyTree->assembleRandomUnifracTree(groups[0], groups[1]);
237                                 
238                                 //copyTree->createNewickFile("random"+groupA+toString(count));
239                 
240                                 for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
241                 
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
244                         
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++);        
251                                                 }else { it++;  }
252                                         }
253                         
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());     }
258                         
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()); 
262                                         }
263                         
264                                         /**********************************************************************/
265                                         //This section adds in all lengths that are leaf
266                         
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()); 
273                                                 }
274                                         }
275                         
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()); 
281                                                 }
282                                         }
283                         
284                                         /**********************************************************************/
285                                 }
286                 
287                                 UW = (UniqueBL / totalBL);  
288         
289                                 if (isnan(UW) || isinf(UW)) { UW = 0; }
290         
291                                 data[count] = UW;
292                                 count++;
293                                 groups.clear();
294                         }
295                 }
296                 
297                 
298                 if (numComp != 1) {
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]);
304                                         }
305                                 }
306                         }else {
307                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
308                                         groups.push_back(globaldata->Groups[i]);
309                                 }
310                         }
311                 
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;
315                         copyIpcount.clear();
316                 
317                         //copy random tree passed in
318                         copyTree->getCopy(t);
319                                 
320                         //swap labels in all the groups you want to compare
321                         copyTree->assembleRandomUnifracTree(groups);
322
323                         for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
324                 
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
327                         
328                                 /**********************************************************************/
329                                 //This section adds in all lengths that are non leaf
330                         
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++);        
335                                                 }else { it++;  }
336                                 }
337                         
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());     }
342                         
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()); 
346                                 }
347                         
348                                 /**********************************************************************/
349                                 //This section adds in all lengths that are leaf
350                         
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()); 
357                                         }
358                                 }
359                         
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()); 
365                                         }
366                                 }
367                         
368                                 /**********************************************************************/
369                         }
370                 
371                         UW = (UniqueBL / totalBL);  
372         
373                         if (isnan(UW) || isinf(UW)) { UW = 0; }
374         
375                         data[count] = UW;
376                 }
377                 
378                 delete copyTree;
379                 
380                 return data;
381         
382         }
383         catch(exception& e) {
384                 errorOut(e, "Unweighted", "getValues");
385                 exit(1);
386         }
387 }
388
389
390
391