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