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