]> git.donarmstrong.com Git - mothur.git/blob - unifracweightedcommand.cpp
changed unweighted and weighted score cumulatives to be that score or higher instead...
[mothur.git] / unifracweightedcommand.cpp
1 /*
2  *  unifracweightedcommand.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 "unifracweightedcommand.h"
11
12 /***********************************************************/
13 UnifracWeightedCommand::UnifracWeightedCommand() {
14         try {
15                 globaldata = GlobalData::getInstance();
16                 
17                 T = globaldata->gTree;
18                 tmap = globaldata->gTreemap;
19                 weightedFile = globaldata->getTreeFile() + ".weighted";
20                 openOutputFile(weightedFile, out);
21                 sumFile = globaldata->getTreeFile() + ".wsummary";
22                 openOutputFile(sumFile, outSum);
23                 distFile = globaldata->getTreeFile() + ".wdistrib";
24                 openOutputFile(distFile, outDist);
25                 
26                 //if the user has not entered specific groups to analyze then do them all
27                 if (globaldata->Groups.size() == 0) {
28                         numGroups = tmap->getNumGroups();
29                 }else {
30                         //check that groups are valid
31                         for (int i = 0; i < globaldata->Groups.size(); i++) {
32                                 if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
33                                         cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
34                                         // erase the invalid group from globaldata->Groups
35                                         globaldata->Groups.erase (globaldata->Groups.begin()+i);
36                                 }
37                         }
38                         
39                         //if the user only entered invalid groups
40                         if (globaldata->Groups.size() == 0) { 
41                                 numGroups = tmap->getNumGroups();
42                                 cout << "When using the groups parameter you must have at least 2 valid groups. I will run the command using all the groups in your groupfile." << endl; 
43                         }else if (globaldata->Groups.size() == 1) { 
44                                 cout << "When using the groups parameter you must have at least 2 valid groups. I will run the command using all the groups in your groupfile." << endl;
45                                 numGroups = tmap->getNumGroups();
46                                 globaldata->Groups.clear();
47                         }else { numGroups = globaldata->Groups.size(); }
48                 }
49                 
50                 //calculate number of comparisons i.e. with groups A,B,C = AB, AC, BC = 3;
51                 numComp = 0;
52                 int n = 1;
53                 for (int i=1; i<numGroups; i++) { 
54                         numComp += i; 
55                         for (int l = n; l < numGroups; l++) {
56                                 //set group comparison labels
57                                 if (globaldata->Groups.size() != 0) {
58                                         groupComb.push_back(globaldata->Groups[i-1]+globaldata->Groups[l]);
59                                 }else {
60                                         groupComb.push_back(tmap->namesOfGroups[i-1]+tmap->namesOfGroups[l]);
61                                 }
62                         }
63                         n++;
64                 }
65                         
66                 convert(globaldata->getIters(), iters);  //how many random trees to generate
67                 weighted = new Weighted(tmap);
68
69         }
70         catch(exception& e) {
71                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function UnifracWeightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
72                 exit(1);
73         }
74         catch(...) {
75                 cout << "An unknown error has occurred in the UnifracWeightedCommand class function UnifracWeightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
76                 exit(1);
77         }
78 }
79 /***********************************************************/
80 int UnifracWeightedCommand::execute() {
81         try {
82                 
83                 //get weighted for users tree
84                 userData.resize(numComp,0);  //data[0] = weightedscore AB, data[1] = weightedscore AC...
85                 randomData.resize(numComp,0); //data[0] = weightedscore AB, data[1] = weightedscore AC...
86                 uscoreFreq.resize(numComp);  
87                 validScores.resize(numComp);  
88                 totalrscoreFreq.resize(numComp); 
89                 uCumul.resize(numComp);         
90                 
91                 //format output
92                 outDist.setf(ios::fixed, ios::floatfield); outDist.setf(ios::showpoint);
93                 outDist << "Tree#" << '\t' << "Iter" << '\t' << "Groups"<< '\t' << "WScore" << endl;
94
95                 
96                 //create new tree with same num nodes and leaves as users
97                 randT = new Tree();
98                 
99                 //get pscores for users trees
100                 for (int i = 0; i < T.size(); i++) {
101                         rscoreFreq.resize(numComp);  //data[0] = weightedscore AB, data[1] = weightedscore AC...
102                         rCumul.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC...     
103                                 
104                         cout << "Processing tree " << i+1 << endl;
105                         userData = weighted->getValues(T[i]);  //userData[0] = weightedscore
106                         
107                         //save users score
108                         for (int s=0; s<numComp; s++) {
109                                 //update uscoreFreq
110                                 it = uscoreFreq[s].find(userData[s]);
111                                 if (it == uscoreFreq[s].end()) {//new score
112                                         uscoreFreq[s][userData[s]] = 1;
113                                 }else{ uscoreFreq[s][userData[s]]++; }
114                                 
115                                 //add user score to valid scores
116                                 validScores[s][userData[s]] = userData[s];
117
118                                 //save users tree score for summary file
119                                 utreeScores.push_back(userData[s]);
120                         }
121                         
122                         //copy T[i]'s info.
123                         randT->getCopy(T[i]); 
124                         
125                         //get scores for random trees
126                         for (int j = 0; j < iters; j++) {
127                                 //create a random tree with same topology as T[i], but different labels
128                                 randT->assembleRandomUnifracTree();
129                                 //get pscore of random tree
130                                 randomData = weighted->getValues(randT);
131                                 
132                                 //save ramdoms score
133                                 for (int p=0; p<numComp; p++) {
134                                         //add trees weighted score random score freq
135                                         it2 = rscoreFreq[p].find(randomData[p]);
136                                         if (it2 != rscoreFreq[p].end()) {//already have that score
137                                                 rscoreFreq[p][randomData[p]]++;
138                                         }else{//first time we have seen this score
139                                                 rscoreFreq[p][randomData[p]] = 1;
140                                         }
141                                         
142                                         //add random score to valid scores
143                                         validScores[p][randomData[p]] = randomData[p];
144                                         
145                                         //output info to uwdistrib file
146                                         outDist << i+1 << '\t' << '\t'<< j+1 << '\t' << '\t' << groupComb[p] << '\t'<< randomData[p] << endl;
147                                 }
148                         }
149                         
150                         saveRandomScores(); //save all random scores for weighted file
151                         
152                         //find the signifigance of the score for summary file
153                         for (int t = 0; t < numComp; t++) {
154                                 float rcumul = 1.0000;
155                                 for (it = validScores[t].begin(); it != validScores[t].end(); it++) { 
156                                         //make rscoreFreq map and rCumul
157                                         it2 = rscoreFreq[t].find(it->first);
158                                         rCumul[t][it->first] = rcumul;
159                                         //get percentage of random trees with that info
160                                         if (it2 != rscoreFreq[t].end()) {  rscoreFreq[t][it->first] /= iters; rcumul-= it2->second;  }
161                                         else { rscoreFreq[t][it->first] = 0.0000; } //no random trees with that score
162                                 }
163                         }
164                         
165                         //save the signifigance of the users score for printing later
166                         for (int f = 0; f < numComp; f++) {
167                                 WScoreSig.push_back(rCumul[f][userData[f]]);
168                         }
169                         
170                         
171                         //clear random data
172                         rscoreFreq.clear();
173                         rCumul.clear();
174                 }
175                 
176                 rCumul.resize(numComp);
177                 for (int b = 0; b < numComp; b++) {
178                         float ucumul = 1.0000;
179                         float rcumul = 1.0000;
180                         //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
181                         for (it = validScores[b].begin(); it != validScores[b].end(); it++) { 
182                                 it2 = uscoreFreq[b].find(it->first);
183                                 //make uCumul map
184                                 uCumul[b][it->first] = ucumul;
185                                 //user data has that score 
186                                 if (it2 != uscoreFreq[b].end()) { uscoreFreq[b][it->first] /= T.size(); ucumul-= it2->second;  }
187                                 else { uscoreFreq[b][it->first] = 0.0000; } //no user trees with that score
188                         
189                                 //make rscoreFreq map and rCumul
190                                 it2 = totalrscoreFreq[b].find(it->first);
191                                 rCumul[b][it->first] = rcumul;
192                                 //get percentage of random trees with that info
193                                 if (it2 != totalrscoreFreq[b].end()) {  totalrscoreFreq[b][it->first] /= (iters * T.size()); rcumul-= it2->second;  }
194                                 else { totalrscoreFreq[b][it->first] = 0.0000; } //no random trees with that score
195                                                         }
196                 }
197                 
198                 printWeightedFile();
199                 printWSummaryFile();
200                 
201                 //reset randomTree parameter to 0
202                 globaldata->setRandomTree("0");
203                 //clear out users groups
204                 globaldata->Groups.clear();
205                 
206                 delete randT;
207                 
208                 return 0;
209                 
210         }
211         catch(exception& e) {
212                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
213                 exit(1);
214         }
215         catch(...) {
216                 cout << "An unknown error has occurred in the UnifracWeightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
217                 exit(1);
218         }
219 }
220 /***********************************************************/
221 void UnifracWeightedCommand::printWeightedFile() {
222         try {
223                 //column headers
224                 
225                 out << "Group" << '\t' << "Score" << '\t' << "UserFreq" << '\t' << "UserCumul" << '\t' << "RandFreq" << '\t' << "RandCumul" << endl;
226                                 
227                 //format output
228                 out.setf(ios::fixed, ios::floatfield); out.setf(ios::showpoint);
229                 
230                 //for each group
231                 for (int e = 0; e < numComp; e++) {
232                         //print each line in that group
233                         for (it = validScores[e].begin(); it != validScores[e].end(); it++) { 
234                                 out << setprecision(6) <<  groupComb[e] << '\t' << it->first << '\t' << '\t' << uscoreFreq[e][it->first] << '\t' << uCumul[e][it->first] << '\t' << totalrscoreFreq[e][it->first] << '\t' << rCumul[e][it->first] << endl; 
235                         } 
236                 }
237                 
238                 out.close();
239                 
240         }
241         catch(exception& e) {
242                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
243                 exit(1);
244         }
245         catch(...) {
246                 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
247                 exit(1);
248         }
249 }
250
251
252 /***********************************************************/
253 void UnifracWeightedCommand::printWSummaryFile() {
254         try {
255                 //column headers
256                 outSum << "Tree#" << '\t' << "Groups" << '\t' << '\t' << "WScore" << '\t' << '\t' << "WSig" <<  endl;
257                 
258                 //format output
259                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
260                 
261                 //print each line
262                 int count = 0;
263                 for (int i = 0; i < T.size(); i++) { 
264                         for (int j = 0; j < numComp; j++) {
265                                 outSum << setprecision(6) << i+1 << '\t' << '\t' << groupComb[j] << '\t' << utreeScores[count] << '\t' << WScoreSig[count] << endl; 
266                                 count++;
267                         }
268                 }
269                 outSum.close();
270         }
271         catch(exception& e) {
272                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
273                 exit(1);
274         }
275         catch(...) {
276                 cout << "An unknown error has occurred in the UnifracWeightedCommand class function printWeightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
277                 exit(1);
278         }
279 }
280
281 /***********************************************************/
282 void UnifracWeightedCommand::saveRandomScores() {
283         try {
284                 for (int e = 0; e < numComp; e++) {
285                         //update total map with new random scores
286                         for (it = rscoreFreq[e].begin(); it != rscoreFreq[e].end(); it++) { 
287                                 //does this score already exist in the total map
288                                 it2 = totalrscoreFreq[e].find(it->first);
289                                 //if yes then add them
290                                 if (it2 != totalrscoreFreq[e].end()) { 
291                                         totalrscoreFreq[e][it->first] += rscoreFreq[e][it->first];
292                                 }else{ //its a new score
293                                         totalrscoreFreq[e][it->first] = rscoreFreq[e][it->first];
294                                 }
295                         }
296                 }
297         }
298         catch(exception& e) {
299                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracWeightedCommand class Function saveRandomScores. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
300                 exit(1);
301         }
302         catch(...) {
303                 cout << "An unknown error has occurred in the UnifracWeightedCommand class function saveRandomScores. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
304                 exit(1);
305         }
306 }
307
308 /***********************************************************/