]> git.donarmstrong.com Git - mothur.git/blob - unifracunweightedcommand.cpp
b5e592622868b93738d66e0b6a4639e1797c69f0
[mothur.git] / unifracunweightedcommand.cpp
1 /*
2  *  unifracunweightedcommand.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 "unifracunweightedcommand.h"
11
12 /***********************************************************/
13 UnifracUnweightedCommand::UnifracUnweightedCommand() {
14         try {
15                 globaldata = GlobalData::getInstance();
16                 
17                 T = globaldata->gTree;
18                 tmap = globaldata->gTreemap;
19                 sumFile = globaldata->getTreeFile() + ".uwsummary";
20                 openOutputFile(sumFile, outSum);
21
22                 setGroups(); //sets users groups to analyze
23                 convert(globaldata->getIters(), iters);  //how many random trees to generate
24                 unweighted = new Unweighted(tmap);
25
26         }
27         catch(exception& e) {
28                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
29                 exit(1);
30         }
31         catch(...) {
32                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
33                 exit(1);
34         }
35 }
36 /***********************************************************/
37 int UnifracUnweightedCommand::execute() {
38         try {
39         
40                 userData.resize(numComp,0);  //data[0] = unweightedscore 
41                 randomData.resize(numComp,0); //data[0] = unweightedscore
42                 //create new tree with same num nodes and leaves as users
43                 
44                 outSum << "Tree#" << '\t' << "Groups" << '\t'  <<  "UWScore" <<'\t' << "UWSig" <<  endl;
45                 cout << "Tree#" << '\t' << "Groups" << '\t'  <<  "UWScore" << '\t' << "UWSig" <<  endl;
46                 
47                 //get pscores for users trees
48                 for (int i = 0; i < T.size(); i++) {
49                         counter = 0;
50                         unweightedFile = globaldata->getTreeFile()  + toString(i+1) + ".unweighted";
51                         unweightedFileout = globaldata->getTreeFile() + "temp." + toString(i+1) + ".unweighted";
52                         
53                         outSum << i+1 << '\t';
54                         cout << i+1 << '\t';
55                         
56                         //get unweighted for users tree
57                         rscoreFreq.resize(numComp);  
58                         rCumul.resize(numComp);  
59                         utreeScores.resize(numComp);  
60                         UWScoreSig.resize(numComp); 
61
62                         userData = unweighted->getValues(T[i]);  //userData[0] = unweightedscore
63                         
64                         //output scores for each combination
65                         for(int k = 0; k < numComp; k++) {
66                                 //saves users score
67                                 utreeScores[k].push_back(userData[k]);
68                         }
69                         
70                         //get unweighted scores for random trees
71                         for (int j = 0; j < iters; j++) {
72                                 //we need a different getValues because when we swap the labels we only want to swap those in each parwise comparison
73                                 randomData = unweighted->getValues(T[i], "", "");
74                                 
75                                 for(int k = 0; k < numComp; k++) {      
76                                         //add trees unweighted score to map of scores
77                                         it2 = rscoreFreq[k].find(randomData[k]);
78                                         if (it2 != rscoreFreq[k].end()) {//already have that score
79                                                 rscoreFreq[k][randomData[k]]++;
80                                         }else{//first time we have seen this score
81                                                 rscoreFreq[k][randomData[k]] = 1;
82                                         }
83                                 
84                                         //add randoms score to validscores
85                                         validScores[randomData[k]] = randomData[k];
86                                 }
87                         }
88                 
89                 for(int a = 0; a < numComp; a++) {
90                         float rcumul = 1.0000;
91                         //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
92                         for (it = validScores.begin(); it != validScores.end(); it++) { 
93                                 //make rscoreFreq map and rCumul
94                                 it2 = rscoreFreq[a].find(it->first);
95                                 rCumul[a][it->first] = rcumul;
96                                 //get percentage of random trees with that info
97                                 if (it2 != rscoreFreq[a].end()) {  rscoreFreq[a][it->first] /= iters; rcumul-= it2->second;  }
98                                 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
99                         }
100                         UWScoreSig[a].push_back(rCumul[a][userData[a]]);
101                 }
102                 
103                 printUnweightedFile();
104                 printUWSummaryFile();
105                 
106                 rscoreFreq.clear(); 
107                 rCumul.clear();  
108                 validScores.clear(); 
109                 utreeScores.clear();  
110                 UWScoreSig.clear(); 
111         }
112                 //reset groups parameter
113                 globaldata->Groups.clear(); globaldata->setGroups("");
114                 outSum.close();
115                 
116                 return 0;
117                 
118         }
119         catch(exception& e) {
120                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
121                 exit(1);
122         }
123         catch(...) {
124                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
125                 exit(1);
126         }
127 }
128 /***********************************************************/
129 void UnifracUnweightedCommand::printUnweightedFile() {
130         try {
131                 vector<double> data;
132                 
133                 for(int a = 0; a < numComp; a++) {
134                         initFile(groupComb[a]);
135                         //print each line
136                         for (it = validScores.begin(); it != validScores.end(); it++) { 
137                                 data.push_back(it->first);  data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]); 
138                                 output(data);
139                                 data.clear();
140                         } 
141                         resetFile();
142                 }
143                 
144                 out.close();
145                 inFile.close();
146                 remove(unweightedFileout.c_str());
147                 
148         }
149         catch(exception& e) {
150                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
151                 exit(1);
152         }
153         catch(...) {
154                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
155                 exit(1);
156         }
157 }
158
159 /***********************************************************/
160 void UnifracUnweightedCommand::printUWSummaryFile() {
161         try {
162                                 
163                 //format output
164                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
165                         
166                 //print each line
167
168                 for(int a = 0; a < numComp; a++) {
169                         if (UWScoreSig[a][0] > (1/(float)iters)) {
170                                 outSum << setprecision(6) << groupComb[a] << '\t' << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << UWScoreSig[a][0] << endl;
171                                 cout << setprecision(6)  << groupComb[a] << '\t' << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << UWScoreSig[a][0] << endl; 
172                         }else {
173                                 outSum << setprecision(6) << groupComb[a] << '\t' << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
174                                 cout << setprecision(6)  << groupComb[a] << '\t' << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl; 
175                         }
176                 }
177                 
178         }
179         catch(exception& e) {
180                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
181                 exit(1);
182         }
183         catch(...) {
184                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
185                 exit(1);
186         }
187 }
188 /***********************************************************/
189
190 void UnifracUnweightedCommand::setGroups() {
191         try {
192                 string allGroups = "";
193                 numGroups = 0;
194                 //if the user has not entered specific groups to analyze then do them all
195                 if (globaldata->Groups.size() != 0) {
196                         if (globaldata->Groups[0] != "all") {
197                                 //check that groups are valid
198                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
199                                         if (tmap->isValidGroup(globaldata->Groups[i]) != true) {
200                                                 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
201                                                 // erase the invalid group from globaldata->Groups
202                                                 globaldata->Groups.erase(globaldata->Groups.begin()+i);
203                                         }
204                                 }
205                         
206                                 //if the user only entered invalid groups
207                                 if (globaldata->Groups.size() == 0) { 
208                                         cout << "When using the groups parameter you must have at least 1 valid group. I will run the command using all the groups in your groupfile." << endl; 
209                                         for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
210                                                 globaldata->Groups.push_back(tmap->namesOfGroups[i]);
211                                                 numGroups++;
212                                                 allGroups += tmap->namesOfGroups[i] + "-";
213                                         }
214                                         allGroups = allGroups.substr(0, allGroups.length()-1);
215                                 }else {
216                                         for (int i = 0; i < globaldata->Groups.size(); i++) {
217                                                 allGroups += globaldata->Groups[i] + "-";
218                                                 numGroups++;
219                                         }
220                                         allGroups = allGroups.substr(0, allGroups.length()-1);
221                                 }
222                         }else{//user has enter "all" and wants the default groups
223                                 globaldata->Groups.clear();
224                                 for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
225                                         globaldata->Groups.push_back(tmap->namesOfGroups[i]);
226                                         numGroups++;
227                                         allGroups += tmap->namesOfGroups[i] + "-";
228                                 }
229                                 allGroups = allGroups.substr(0, allGroups.length()-1);
230                                 globaldata->setGroups("");
231                         }
232                 }else {
233                         for (int i = 0; i < tmap->namesOfGroups.size(); i++) {
234                                 allGroups += tmap->namesOfGroups[i] + "-";
235                         }
236                         allGroups = allGroups.substr(0, allGroups.length()-1);
237                         numGroups = 1;
238                 }
239                 
240                 //calculate number of comparsions
241                 numComp = 0;
242                 for (int r=0; r<numGroups; r++) { 
243                         for (int l = r+1; l < numGroups; l++) {
244                                 groupComb.push_back(globaldata->Groups[r]+ "-" + globaldata->Groups[l]);
245                                 numComp++;
246                         }
247                 }
248                 
249                 //ABC
250                 if (numComp != 1) {
251                         groupComb.push_back(allGroups);
252                         numComp++;
253                 }
254         }
255         catch(exception& e) {
256                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
257                 exit(1);
258         }
259         catch(...) {
260                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
261                 exit(1);
262         }               
263
264 }
265 /*****************************************************************/
266
267 void UnifracUnweightedCommand::initFile(string label){
268         try {
269                 if(counter != 0){
270                         openOutputFile(unweightedFileout, out);
271                         openInputFile(unweightedFile, inFile);
272
273                         string inputBuffer;
274                         getline(inFile, inputBuffer);
275                 
276                         out     <<  inputBuffer << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;           
277                 }else{
278                         openOutputFile(unweightedFileout, out);
279                         out     << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
280                 }
281
282                 out.setf(ios::fixed, ios::floatfield);
283                 out.setf(ios::showpoint);
284         }
285         catch(exception& e) {
286                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
287                 exit(1);
288         }
289         catch(...) {
290                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
291                 exit(1);
292         }
293 }
294
295 /***********************************************************************/
296
297 void UnifracUnweightedCommand::output(vector<double> data){
298         try {
299                 if(counter != 0){               
300                         string inputBuffer;
301                         getline(inFile, inputBuffer);
302 //                      out     <<  inputBuffer << setprecision(6) << '\t' << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
303                 
304                         out << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(globaldata->getIters().length())  << '\t' << data[1] << '\t' << data[2] << endl;
305                 }
306                 else{
307                         out << setprecision(6) << data[0] << setprecision(globaldata->getIters().length())  << '\t' << data[1] << '\t' << data[2] << endl;
308                 }
309
310         }
311         catch(exception& e) {
312                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
313                 exit(1);
314         }
315         catch(...) {
316                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
317                 exit(1);
318         }
319 };
320
321 /***********************************************************************/
322
323 void UnifracUnweightedCommand::resetFile(){
324         try {
325                 if(counter != 0){
326                         out.close();
327                         inFile.close();
328                 }
329                 else{
330                         out.close();
331                 }
332                 counter = 1;
333                 
334                 remove(unweightedFile.c_str());
335                 rename(unweightedFileout.c_str(), unweightedFile.c_str());
336         }
337         catch(exception& e) {
338                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
339                 exit(1);
340         }
341         catch(...) {
342                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
343                 exit(1);
344         }       
345 }
346