]> git.donarmstrong.com Git - mothur.git/blob - unweighted.cpp
fixed bug in read.tree updates help and validparameters strings, added ability for...
[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                 cout << "Standard Error: " << e.what() << " has occurred in the Unweighted class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
185                 exit(1);
186         }
187         catch(...) {
188                 cout << "An unknown error has occurred in the Unweighted class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
189                 exit(1);
190         }
191
192 }
193
194 /**************************************************************************************************/
195
196 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB) { 
197  try {
198         globaldata = GlobalData::getInstance();
199                 
200                 vector<string> groups;
201                 double UniqueBL;  //a branch length is unique if it's chidren are from the same group
202                 double totalBL; //all branch lengths
203                 double UW;              //Unweighted Value = UniqueBL / totalBL;
204                 map<string, int>::iterator it;  //iterator to traverse pgroups
205                 map<string, int> copyIpcount;
206                 copyTree = new Tree;
207
208                 //if the users enters no groups then give them the score of all groups
209                 int numGroups = globaldata->Groups.size();
210
211                 //calculate number of comparsions
212                 int numComp = 0;
213                 for (int r=0; r<numGroups; r++) { 
214                         for (int l = r+1; l < numGroups; l++) {
215                                 numComp++;
216                         }
217                 }
218
219                 //numComp+1 for AB, AC, BC, ABC
220                 data.resize(numComp+1,0);
221                 
222                 int count = 0;
223                 for (int a=0; a<numGroups; a++) { 
224                         for (int l = a+1; l < numGroups; l++) {
225                                 UniqueBL=0.0000;  //a branch length is unique if it's chidren are from the same group
226                                 totalBL = 0.00; //all branch lengths
227                                 UW = 0.00;              //Unweighted Value = UniqueBL / totalBL;
228                                 copyIpcount.clear();
229                                 
230                                 //copy random tree passed in
231                                 copyTree->getCopy(t);
232                                                                 
233                                 //groups in this combo
234                                 groups.push_back(globaldata->Groups[a]); groups.push_back(globaldata->Groups[l]);
235                                 
236                                 //swap labels in the groups you want to compare
237                                 copyTree->assembleRandomUnifracTree(groups[0], groups[1]);
238                                 
239                                 //copyTree->createNewickFile("random"+groupA+toString(count));
240                 
241                                 for(int i=copyTree->getNumLeaves();i<copyTree->getNumNodes();i++){
242                 
243                                         int lc = copyTree->tree[i].getLChild();  //lc = vector index of left child
244                                         int rc = copyTree->tree[i].getRChild();  //rc = vector index of right child
245                         
246                                         /**********************************************************************/
247                                         //This section adds in all lengths that are non leaf
248                         
249                                         copyIpcount = copyTree->tree[i].pcount;
250                                         for (it = copyIpcount.begin(); it != copyIpcount.end(); it++) {
251                                                 if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first);   }
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(); it++) {
333                                         if (inUsersGroups(it->first, groups) != true) { copyIpcount.erase(it->first);   }
334                                 }
335                         
336                                 //if i's children are from the same group then i's pcount size will be 1 
337                                 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
338                                 if (copyIpcount.size() == 0) { }
339                                 else if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) {  abs(UniqueBL += copyTree->tree[i].getBranchLength());     }
340                         
341                                 //add i's BL to total if it is from the groups the user wants
342                                 if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {  
343                                         totalBL += abs(copyTree->tree[i].getBranchLength()); 
344                                 }
345                         
346                                 /**********************************************************************/
347                                 //This section adds in all lengths that are leaf
348                         
349                                 //if i's chidren are leaves
350                                 if (copyTree->tree[rc].getRChild() == -1) {
351                                         //if rc is a valid group and rc has a BL
352                                         if ((inUsersGroups(copyTree->tree[rc].getGroup(), groups) == true) && (copyTree->tree[rc].getBranchLength() != -1)) {
353                                                 UniqueBL += abs(copyTree->tree[rc].getBranchLength());
354                                                 totalBL += abs(copyTree->tree[rc].getBranchLength()); 
355                                         }
356                                 }
357                         
358                                 if (copyTree->tree[lc].getLChild() == -1) {
359                                         //if lc is a valid group and lc has a BL
360                                         if ((inUsersGroups(copyTree->tree[lc].getGroup(), groups) == true) && (copyTree->tree[lc].getBranchLength() != -1)) {
361                                                 UniqueBL += abs(copyTree->tree[lc].getBranchLength());
362                                                 totalBL += abs(copyTree->tree[lc].getBranchLength()); 
363                                         }
364                                 }
365                         
366                                 /**********************************************************************/
367                         }
368                 
369                         UW = (UniqueBL / totalBL);  
370         
371                         if (isnan(UW) || isinf(UW)) { UW = 0; }
372         
373                         data[count] = UW;
374                 }
375                 
376                 delete copyTree;
377                 
378                 return data;
379         
380         }
381         catch(exception& e) {
382                 cout << "Standard Error: " << e.what() << " has occurred in the Unweighted class Function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
383                 exit(1);
384         }
385         catch(...) {
386                 cout << "An unknown error has occurred in the Unweighted class function getValues. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
387                 exit(1);
388         }
389 }
390
391
392
393