]> git.donarmstrong.com Git - mothur.git/blob - unweighted.cpp
added logfile feature
[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 += abs(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 += abs(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 += abs(t->tree[rc].getBranchLength());
82                                                         totalBL += abs(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 += abs(t->tree[lc].getBranchLength());
90                                                         totalBL += abs(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                                         if (tmap->namesOfGroups[i] != "xxx") {
113                                                 groups.push_back(tmap->namesOfGroups[i]);
114                                         }
115                                 }
116                         }else {
117                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
118                                         groups.push_back(globaldata->Groups[i]);
119                                 }
120                         }
121                 
122                         UniqueBL=0.0000;  //a branch length is unique if it's chidren are from the same group
123                         totalBL = 0.00; //all branch lengths
124                         UW = 0.00;              //Unweighted Value = UniqueBL / totalBL;
125                         copyIpcount.clear();
126                                 
127                         for(int i=t->getNumLeaves();i<t->getNumNodes();i++){
128                 
129                                 int lc = t->tree[i].getLChild();  //lc = vector index of left child
130                                 int rc = t->tree[i].getRChild();  //rc = vector index of right child
131                         
132                                 /**********************************************************************/
133                                 //This section adds in all lengths that are non leaf
134                         
135                                 copyIpcount = t->tree[i].pcount;
136                                 for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
137                                         if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first);   }
138                                 }
139                         
140                                 //if i's children are from the same group then i's pcount size will be 1 
141                                 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
142                                 if (copyIpcount.size() == 0) { }
143                                 else if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) {  UniqueBL += abs(t->tree[i].getBranchLength());   }
144                         
145                                 //add i's BL to total if it is from the groups the user wants
146                                 if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {  
147                                         totalBL += abs(t->tree[i].getBranchLength()); 
148                                 }
149                         
150                                 /**********************************************************************/
151                                 //This section adds in all lengths that are leaf
152                         
153                                 //if i's chidren are leaves
154                                 if (t->tree[rc].getRChild() == -1) {
155                                         //if rc is a valid group and rc has a BL
156                                         if ((inUsersGroups(t->tree[rc].getGroup(), groups) == true) && (t->tree[rc].getBranchLength() != -1)) {
157                                                 UniqueBL += abs(t->tree[rc].getBranchLength());
158                                                 totalBL += abs(t->tree[rc].getBranchLength()); 
159                                         }
160                                 }
161                         
162                                 if (t->tree[lc].getLChild() == -1) {
163                                         //if lc is a valid group and lc has a BL
164                                         if ((inUsersGroups(t->tree[lc].getGroup(), groups) == true) && (t->tree[lc].getBranchLength() != -1)) {
165                                                 UniqueBL += abs(t->tree[lc].getBranchLength());
166                                                 totalBL += abs(t->tree[lc].getBranchLength()); 
167                                         }
168                                 }
169                         
170                                 /**********************************************************************/
171                         }
172                 
173                         UW = (UniqueBL / totalBL);  
174         
175                         if (isnan(UW) || isinf(UW)) { UW = 0; }
176         
177                         data[count] = UW;
178                 }
179
180                 return data;
181         
182         }
183         catch(exception& e) {
184                 errorOut(e, "Unweighted", "getValues");
185                 exit(1);
186         }
187 }
188
189 /**************************************************************************************************/
190
191 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB) { 
192  try {
193         globaldata = GlobalData::getInstance();
194                 
195                 vector<string> groups;
196                 double UniqueBL;  //a branch length is unique if it's chidren are from the same group
197                 double totalBL; //all branch lengths
198                 double UW;              //Unweighted Value = UniqueBL / totalBL;
199                 map<string, int>::iterator it;  //iterator to traverse pgroups
200                 map<string, int> copyIpcount;
201                 copyTree = new Tree;
202
203                 //if the users enters no groups then give them the score of all groups
204                 int numGroups = globaldata->Groups.size();
205
206                 //calculate number of comparsions
207                 int numComp = 0;
208                 for (int r=0; r<numGroups; r++) { 
209                         for (int l = r+1; l < numGroups; l++) {
210                                 numComp++;
211                         }
212                 }
213
214                 //numComp+1 for AB, AC, BC, ABC
215                 data.resize(numComp+1,0);
216                 
217                 int count = 0;
218                 for (int a=0; a<numGroups; a++) { 
219                         for (int l = a+1; l < numGroups; l++) {
220                                 UniqueBL=0.0000;  //a branch length is unique if it's chidren are from the same group
221                                 totalBL = 0.00; //all branch lengths
222                                 UW = 0.00;              //Unweighted Value = UniqueBL / totalBL;
223                                 copyIpcount.clear();
224                                 
225                                 //copy random tree passed in
226                                 copyTree->getCopy(t);
227                                                                 
228                                 //groups in this combo
229                                 groups.push_back(globaldata->Groups[a]); groups.push_back(globaldata->Groups[l]);
230                                 
231                                 //swap labels in the groups you want to compare
232                                 copyTree->assembleRandomUnifracTree(groups[0], groups[1]);
233                                 
234                                 //copyTree->createNewickFile("random"+groupA+toString(count));
235                 
236                                 for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
237                 
238                                         int lc = copyTree->tree[i].getLChild();  //lc = vector index of left child
239                                         int rc = copyTree->tree[i].getRChild();  //rc = vector index of right child
240                         
241                                         /**********************************************************************/
242                                         //This section adds in all lengths that are non leaf
243                         
244                                         copyIpcount = copyTree->tree[i].pcount;
245                                         for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
246                                                 if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first);   }
247                                         }
248                         
249                                         //if i's children are from the same group then i's pcount size will be 1 
250                                         //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
251                                         if (copyIpcount.size() == 0) { }
252                                         else if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) {  UniqueBL += abs(copyTree->tree[i].getBranchLength());     }
253                         
254                                         //add i's BL to total if it is from the groups the user wants
255                                         if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {  
256                                                 totalBL += abs(copyTree->tree[i].getBranchLength()); 
257                                         }
258                         
259                                         /**********************************************************************/
260                                         //This section adds in all lengths that are leaf
261                         
262                                         //if i's chidren are leaves
263                                         if (copyTree->tree[rc].getRChild() == -1) {
264                                                 //if rc is a valid group and rc has a BL
265                                                 if ((inUsersGroups(copyTree->tree[rc].getGroup(), groups) == true) && (copyTree->tree[rc].getBranchLength() != -1)) {
266                                                         UniqueBL += abs(copyTree->tree[rc].getBranchLength());
267                                                         totalBL += abs(copyTree->tree[rc].getBranchLength()); 
268                                                 }
269                                         }
270                         
271                                         if (copyTree->tree[lc].getLChild() == -1) {
272                                                 //if lc is a valid group and lc has a BL
273                                                 if ((inUsersGroups(copyTree->tree[lc].getGroup(), groups) == true) && (copyTree->tree[lc].getBranchLength() != -1)) {
274                                                         UniqueBL += abs(copyTree->tree[lc].getBranchLength());
275                                                         totalBL += abs(copyTree->tree[lc].getBranchLength()); 
276                                                 }
277                                         }
278                         
279                                         /**********************************************************************/
280                                 }
281                 
282                                 UW = (UniqueBL / totalBL);  
283         
284                                 if (isnan(UW) || isinf(UW)) { UW = 0; }
285         
286                                 data[count] = UW;
287                                 count++;
288                                 groups.clear();
289                         }
290                 }
291                 
292                 
293                 if (numComp != 1) {
294                         if (numGroups == 0) {
295                                 //get score for all users groups
296                                 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
297                                         if (tmap->namesOfGroups[i] != "xxx") {
298                                                 groups.push_back(tmap->namesOfGroups[i]);
299                                         }
300                                 }
301                         }else {
302                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
303                                         groups.push_back(globaldata->Groups[i]);
304                                 }
305                         }
306                 
307                         UniqueBL=0.0000;  //a branch length is unique if it's chidren are from the same group
308                         totalBL = 0.00; //all branch lengths
309                         UW = 0.00;              //Unweighted Value = UniqueBL / totalBL;
310                         copyIpcount.clear();
311                 
312                         //copy random tree passed in
313                         copyTree->getCopy(t);
314                                 
315                         //swap labels in all the groups you want to compare
316                         copyTree->assembleRandomUnifracTree(groups);
317
318                         for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
319                 
320                                 int lc = copyTree->tree[i].getLChild();  //lc = vector index of left child
321                                 int rc = copyTree->tree[i].getRChild();  //rc = vector index of right child
322                         
323                                 /**********************************************************************/
324                                 //This section adds in all lengths that are non leaf
325                         
326                                 copyIpcount = copyTree->tree[i].pcount;
327                                 for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
328                                         if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first);   }
329                                 }
330                         
331                                 //if i's children are from the same group then i's pcount size will be 1 
332                                 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
333                                 if (copyIpcount.size() == 0) { }
334                                 else if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) {  abs(UniqueBL += copyTree->tree[i].getBranchLength());     }
335                         
336                                 //add i's BL to total if it is from the groups the user wants
337                                 if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {  
338                                         totalBL += abs(copyTree->tree[i].getBranchLength()); 
339                                 }
340                         
341                                 /**********************************************************************/
342                                 //This section adds in all lengths that are leaf
343                         
344                                 //if i's chidren are leaves
345                                 if (copyTree->tree[rc].getRChild() == -1) {
346                                         //if rc is a valid group and rc has a BL
347                                         if ((inUsersGroups(copyTree->tree[rc].getGroup(), groups) == true) && (copyTree->tree[rc].getBranchLength() != -1)) {
348                                                 UniqueBL += abs(copyTree->tree[rc].getBranchLength());
349                                                 totalBL += abs(copyTree->tree[rc].getBranchLength()); 
350                                         }
351                                 }
352                         
353                                 if (copyTree->tree[lc].getLChild() == -1) {
354                                         //if lc is a valid group and lc has a BL
355                                         if ((inUsersGroups(copyTree->tree[lc].getGroup(), groups) == true) && (copyTree->tree[lc].getBranchLength() != -1)) {
356                                                 UniqueBL += abs(copyTree->tree[lc].getBranchLength());
357                                                 totalBL += abs(copyTree->tree[lc].getBranchLength()); 
358                                         }
359                                 }
360                         
361                                 /**********************************************************************/
362                         }
363                 
364                         UW = (UniqueBL / totalBL);  
365         
366                         if (isnan(UW) || isinf(UW)) { UW = 0; }
367         
368                         data[count] = UW;
369                 }
370                 
371                 delete copyTree;
372                 
373                 return data;
374         
375         }
376         catch(exception& e) {
377                 errorOut(e, "Unweighted", "getValues");
378                 exit(1);
379         }
380 }
381
382
383
384