]> git.donarmstrong.com Git - mothur.git/blob - unweighted.cpp
created mothurOut class to handle logfiles
[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=0;i<t->getNumNodes();i++){
52         
53                                         copyIpcount = t->tree[i].pcount;
54                                         for (it = copyIpcount.begin(); it != copyIpcount.end();) {
55                                                 if (inUsersGroups(it->first, groups) != true) { 
56                                                         copyIpcount.erase(it++);        
57                                                 }else { it++;  }
58                                         }
59                         
60                                         //if i's children are from the same group then i's pcount size will be 1 
61                                         //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
62                                         if (copyIpcount.size() == 0) { }
63                                         else if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) {  UniqueBL += abs(t->tree[i].getBranchLength());   }
64                         
65                                         //add i's BL to total if it is from the groups the user wants
66                                         if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {  
67                                                 totalBL += abs(t->tree[i].getBranchLength()); 
68                                         }
69                         
70                                 }
71                 
72                                 UW = (UniqueBL / totalBL);  
73                 //cout << globaldata->Groups[a] << globaldata->Groups[l] << '\t' << UniqueBL << '\t' << totalBL << endl;
74         
75                                 if (isnan(UW) || isinf(UW)) { UW = 0; }
76         
77                                 data[count] = UW;
78                                 count++;
79                                 groups.clear();
80                         }
81                 }
82                 
83                 
84                 if (numComp != 1) {
85                         if (numGroups == 0) {
86                                 //get score for all users groups
87                                 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
88                                         if (tmap->namesOfGroups[i] != "xxx") {
89                                                 groups.push_back(tmap->namesOfGroups[i]);
90                                         }
91                                 }
92                         }else {
93                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
94                                         groups.push_back(globaldata->Groups[i]);
95                                 }
96                         }
97                 
98                         UniqueBL=0.0000;  //a branch length is unique if it's chidren are from the same group
99                         totalBL = 0.00; //all branch lengths
100                         UW = 0.00;              //Unweighted Value = UniqueBL / totalBL;
101                         copyIpcount.clear();
102                                 
103                         for(int i=0;i<t->getNumNodes();i++){
104                                 
105                                 copyIpcount = t->tree[i].pcount;
106                                 for (it = copyIpcount.begin(); it != copyIpcount.end();) {
107                                         if (inUsersGroups(it->first, groups) != true) { 
108                                                 copyIpcount.erase(it++);        
109                                         }else {  it++;  }
110                                 }
111                         
112                                 //if i's children are from the same group then i's pcount size will be 1 
113                                 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
114                                 if (copyIpcount.size() == 0) { }
115                                 else if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) {  UniqueBL += abs(t->tree[i].getBranchLength());   }
116                         
117                                 //add i's BL to total if it is from the groups the user wants
118                                 if ((t->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {  
119                                         totalBL += abs(t->tree[i].getBranchLength()); 
120                                 }
121                         
122                         }
123                 
124                         UW = (UniqueBL / totalBL);  
125         
126                         if (isnan(UW) || isinf(UW)) { UW = 0; }
127         
128                         data[count] = UW;
129                 }
130
131                 return data;
132         
133         }
134         catch(exception& e) {
135                 m->errorOut(e, "Unweighted", "getValues");
136                 exit(1);
137         }
138 }
139
140 /**************************************************************************************************/
141
142 EstOutput Unweighted::getValues(Tree* t, string groupA, string groupB) { 
143  try {
144         globaldata = GlobalData::getInstance();
145                 
146                 vector<string> groups;
147                 double UniqueBL;  //a branch length is unique if it's chidren are from the same group
148                 double totalBL; //all branch lengths
149                 double UW;              //Unweighted Value = UniqueBL / totalBL;
150                 map<string, int>::iterator it;  //iterator to traverse pgroups
151                 map<string, int> copyIpcount;
152                 copyTree = new Tree;
153
154                 //if the users enters no groups then give them the score of all groups
155                 int numGroups = globaldata->Groups.size();
156
157                 //calculate number of comparsions
158                 int numComp = 0;
159                 for (int r=0; r<numGroups; r++) { 
160                         for (int l = r+1; l < numGroups; l++) {
161                                 numComp++;
162                         }
163                 }
164
165                 //numComp+1 for AB, AC, BC, ABC
166                 data.resize(numComp+1,0);
167                 
168                 int count = 0;
169                 for (int a=0; a<numGroups; a++) { 
170                         for (int l = a+1; l < numGroups; l++) {
171                                 UniqueBL=0.0000;  //a branch length is unique if it's chidren are from the same group
172                                 totalBL = 0.00; //all branch lengths
173                                 UW = 0.00;              //Unweighted Value = UniqueBL / totalBL;
174                                 copyIpcount.clear();
175                                 
176                                 //copy random tree passed in
177                                 copyTree->getCopy(t);
178                                                                 
179                                 //groups in this combo
180                                 groups.push_back(globaldata->Groups[a]); groups.push_back(globaldata->Groups[l]);
181                                 
182                                 //swap labels in the groups you want to compare
183                                 copyTree->assembleRandomUnifracTree(groups[0], groups[1]);
184                                 
185                                 //copyTree->createNewickFile("random"+groupA+toString(count));
186                                 
187                                 for(int i=0;i<copyTree->getNumNodes();i++){
188                                                 
189                                         /**********************************************************************/
190                                         //This section adds in all lengths that are non leaf
191                                         copyIpcount = copyTree->tree[i].pcount;
192                                         for (it = copyIpcount.begin(); it != copyIpcount.end();) {
193                                                 if (inUsersGroups(it->first, groups) != true) { 
194                                                         copyIpcount.erase(it++);        
195                                                 }else { it++;  }
196                                         }
197                         
198                                         //if i's children are from the same group then i's pcount size will be 1 
199                                         //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
200                                         if (copyIpcount.size() == 0) { }
201                                         else if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) {  UniqueBL += abs(copyTree->tree[i].getBranchLength());     }
202                         
203                                         //add i's BL to total if it is from the groups the user wants
204                                         if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {  
205                                                 totalBL += abs(copyTree->tree[i].getBranchLength()); 
206                                         }
207                         
208                                 }
209                 
210                                 UW = (UniqueBL / totalBL);  
211         
212                                 if (isnan(UW) || isinf(UW)) { UW = 0; }
213         
214                                 data[count] = UW;
215                                 count++;
216                                 groups.clear();
217                         }
218                 }
219                 
220                 
221                 if (numComp != 1) {
222                         if (numGroups == 0) {
223                                 //get score for all users groups
224                                 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
225                                         if (tmap->namesOfGroups[i] != "xxx") {
226                                                 groups.push_back(tmap->namesOfGroups[i]);
227                                         }
228                                 }
229                         }else {
230                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
231                                         groups.push_back(globaldata->Groups[i]);
232                                 }
233                         }
234                 
235                         UniqueBL=0.0000;  //a branch length is unique if it's chidren are from the same group
236                         totalBL = 0.00; //all branch lengths
237                         UW = 0.00;              //Unweighted Value = UniqueBL / totalBL;
238                         copyIpcount.clear();
239                 
240                         //copy random tree passed in
241                         copyTree->getCopy(t);
242                                 
243                         //swap labels in all the groups you want to compare
244                         copyTree->assembleRandomUnifracTree(groups);
245
246                         for(int i=0;i<copyTree->getNumNodes();i++){
247                         
248                                 copyIpcount = copyTree->tree[i].pcount;
249                                 for (it = copyIpcount.begin(); it != copyIpcount.end();) {
250                                                 if (inUsersGroups(it->first, groups) != true) { 
251                                                         copyIpcount.erase(it++);        
252                                                 }else { it++;  }
253                                 }
254                         
255                                 //if i's children are from the same group then i's pcount size will be 1 
256                                 //if copyIpcount.size() = 0 they are from a branch that is entirely from a group the user doesn't want
257                                 if (copyIpcount.size() == 0) { }
258                                 else if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() == 1)) {  abs(UniqueBL += copyTree->tree[i].getBranchLength());     }
259                         
260                                 //add i's BL to total if it is from the groups the user wants
261                                 if ((copyTree->tree[i].getBranchLength() != -1) && (copyIpcount.size() != 0)) {  
262                                         totalBL += abs(copyTree->tree[i].getBranchLength()); 
263                                 }
264                         
265                         }
266                 
267                         UW = (UniqueBL / totalBL);  
268         
269                         if (isnan(UW) || isinf(UW)) { UW = 0; }
270         
271                         data[count] = UW;
272                 }
273                 
274                 delete copyTree;
275                 
276                 return data;
277         
278         }
279         catch(exception& e) {
280                 m->errorOut(e, "Unweighted", "getValues");
281                 exit(1);
282         }
283 }
284
285
286
287