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