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