]> git.donarmstrong.com Git - mothur.git/blob - unifracunweightedcommand.cpp
9d170631592cc9faf9c54fbd5bde5c710efe05f6
[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                 util = new SharedUtil();
23                 util->setGroups(globaldata->Groups, tmap->namesOfGroups, allGroups, numGroups, "unweighted");   //sets the groups the user wants to analyze
24                 util->getCombos(groupComb, globaldata->Groups, numComp);
25                 globaldata->setGroups("");
26                 
27                 //ABC
28                 if (numComp != 1) {
29                         groupComb.push_back(allGroups);
30                         numComp++;
31                 }
32
33                 
34                 convert(globaldata->getIters(), iters);  //how many random trees to generate
35                 unweighted = new Unweighted(tmap);
36
37         }
38         catch(exception& e) {
39                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
40                 exit(1);
41         }
42         catch(...) {
43                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
44                 exit(1);
45         }
46 }
47 /***********************************************************/
48 int UnifracUnweightedCommand::execute() {
49         try {
50
51                 userData.resize(numComp,0);  //data[0] = unweightedscore 
52                 randomData.resize(numComp,0); //data[0] = unweightedscore
53                 //create new tree with same num nodes and leaves as users
54                 
55                 outSum << "Tree#" << '\t' << "Groups" << '\t'  <<  "UWScore" <<'\t' << "UWSig" <<  endl;
56                 cout << "Tree#" << '\t' << "Groups" << '\t'  <<  "UWScore" << '\t' << "UWSig" <<  endl;
57                 
58                 //get pscores for users trees
59                 for (int i = 0; i < T.size(); i++) {
60                         counter = 0;
61                         unweightedFile = globaldata->getTreeFile()  + toString(i+1) + ".unweighted";
62                         unweightedFileout = globaldata->getTreeFile() + "temp." + toString(i+1) + ".unweighted";
63                         
64                         outSum << i+1 << '\t';
65                         cout << i+1 << '\t';
66                         
67                         //get unweighted for users tree
68                         rscoreFreq.resize(numComp);  
69                         rCumul.resize(numComp);  
70                         utreeScores.resize(numComp);  
71                         UWScoreSig.resize(numComp); 
72
73                         userData = unweighted->getValues(T[i]);  //userData[0] = unweightedscore
74                         
75                         //output scores for each combination
76                         for(int k = 0; k < numComp; k++) {
77                                 //saves users score
78                                 utreeScores[k].push_back(userData[k]);
79
80                         }
81                         
82                         //get unweighted scores for random trees
83                         for (int j = 0; j < iters; j++) {
84                                 //we need a different getValues because when we swap the labels we only want to swap those in each parwise comparison
85                                 randomData = unweighted->getValues(T[i], "", "");
86                                 
87                                 for(int k = 0; k < numComp; k++) {      
88                                         //add trees unweighted score to map of scores
89                                         it2 = rscoreFreq[k].find(randomData[k]);
90                                         if (it2 != rscoreFreq[k].end()) {//already have that score
91                                                 rscoreFreq[k][randomData[k]]++;
92                                         }else{//first time we have seen this score
93                                                 rscoreFreq[k][randomData[k]] = 1;
94                                         }
95                                 
96                                         //add randoms score to validscores
97                                         validScores[randomData[k]] = randomData[k];
98                                 }
99                                 
100                         }
101                 
102                 for(int a = 0; a < numComp; a++) {
103                         float rcumul = 1.0000;
104                         //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
105                         for (it = validScores.begin(); it != validScores.end(); it++) { 
106                                 //make rscoreFreq map and rCumul
107                                 it2 = rscoreFreq[a].find(it->first);
108                                 rCumul[a][it->first] = rcumul;
109                                 //get percentage of random trees with that info
110                                 if (it2 != rscoreFreq[a].end()) {  rscoreFreq[a][it->first] /= iters; rcumul-= it2->second;  }
111                                 else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
112                         }
113                         UWScoreSig[a].push_back(rCumul[a][userData[a]]);
114                 }
115                 
116                 
117                 
118                 printUnweightedFile();
119                 printUWSummaryFile();
120                 
121                 rscoreFreq.clear(); 
122                 rCumul.clear();  
123                 validScores.clear(); 
124                 utreeScores.clear();  
125                 UWScoreSig.clear(); 
126         }
127                 //reset groups parameter
128                 globaldata->Groups.clear(); 
129                 outSum.close();
130                 
131                 return 0;
132                 
133         }
134         catch(exception& e) {
135                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
136                 exit(1);
137         }
138         catch(...) {
139                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
140                 exit(1);
141         }
142 }
143 /***********************************************************/
144 void UnifracUnweightedCommand::printUnweightedFile() {
145         try {
146                 vector<double> data;
147                 
148                 for(int a = 0; a < numComp; a++) {
149                         initFile(groupComb[a]);
150                         //print each line
151                         for (it = validScores.begin(); it != validScores.end(); it++) { 
152                                 data.push_back(it->first);  data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]); 
153                                 output(data);
154                                 data.clear();
155                         } 
156                         resetFile();
157                 }
158                 
159                 out.close();
160                 inFile.close();
161                 remove(unweightedFileout.c_str());
162                 
163         }
164         catch(exception& e) {
165                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
166                 exit(1);
167         }
168         catch(...) {
169                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
170                 exit(1);
171         }
172 }
173
174 /***********************************************************/
175 void UnifracUnweightedCommand::printUWSummaryFile() {
176         try {
177                                 
178                 //format output
179                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
180                         
181                 //print each line
182
183                 for(int a = 0; a < numComp; a++) {
184                         if (UWScoreSig[a][0] > (1/(float)iters)) {
185                                 outSum << setprecision(6) << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << UWScoreSig[a][0] << endl;
186                                 cout << setprecision(6)  << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << UWScoreSig[a][0] << endl; 
187                         }else {
188                                 outSum << setprecision(6) << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
189                                 cout << setprecision(6)  << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl; 
190                         }
191                 }
192                 
193         }
194         catch(exception& e) {
195                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
196                 exit(1);
197         }
198         catch(...) {
199                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
200                 exit(1);
201         }
202 }
203 /*****************************************************************/
204
205 void UnifracUnweightedCommand::initFile(string label){
206         try {
207                 if(counter != 0){
208                         openOutputFile(unweightedFileout, out);
209                         openInputFile(unweightedFile, inFile);
210
211                         string inputBuffer;
212                         getline(inFile, inputBuffer);
213                 
214                         out     <<  inputBuffer << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;           
215                 }else{
216                         openOutputFile(unweightedFileout, out);
217                         out     << label + "Score" << '\t' << label + "RandFreq" << '\t' << label + "RandCumul" << endl;
218                 }
219
220                 out.setf(ios::fixed, ios::floatfield);
221                 out.setf(ios::showpoint);
222         }
223         catch(exception& e) {
224                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
225                 exit(1);
226         }
227         catch(...) {
228                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function initFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
229                 exit(1);
230         }
231 }
232
233 /***********************************************************************/
234
235 void UnifracUnweightedCommand::output(vector<double> data){
236         try {
237                 if(counter != 0){               
238                         string inputBuffer;
239                         getline(inFile, inputBuffer);
240 //                      out     <<  inputBuffer << setprecision(6) << '\t' << data[0] << setprecision(globaldata->getIters().length()) << '\t' << data[1] << '\t' << data[2] << endl;
241                 
242                         out << inputBuffer << '\t' << setprecision(6) << data[0] << setprecision(globaldata->getIters().length())  << '\t' << data[1] << '\t' << data[2] << endl;
243                 }
244                 else{
245                         out << setprecision(6) << data[0] << setprecision(globaldata->getIters().length())  << '\t' << data[1] << '\t' << data[2] << endl;
246                 }
247
248         }
249         catch(exception& e) {
250                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function output. 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 UnifracUnweightedCommand class function output. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
255                 exit(1);
256         }
257 };
258
259 /***********************************************************************/
260
261 void UnifracUnweightedCommand::resetFile(){
262         try {
263                 if(counter != 0){
264                         out.close();
265                         inFile.close();
266                 }
267                 else{
268                         out.close();
269                 }
270                 counter = 1;
271                 
272                 remove(unweightedFile.c_str());
273                 rename(unweightedFileout.c_str(), unweightedFile.c_str());
274         }
275         catch(exception& e) {
276                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
277                 exit(1);
278         }
279         catch(...) {
280                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function resetFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
281                 exit(1);
282         }       
283 }
284