]> git.donarmstrong.com Git - mothur.git/blob - unifracunweightedcommand.cpp
fcff253fc4812bafea60669dcb07c301dfa8de62
[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                 if (numGroups == 1) { numComp++; groupComb.push_back(allGroups); }
28                                 
29                 convert(globaldata->getIters(), iters);  //how many random trees to generate
30                 unweighted = new Unweighted(tmap);
31
32         }
33         catch(exception& e) {
34                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
35                 exit(1);
36         }
37         catch(...) {
38                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
39                 exit(1);
40         }
41 }
42 /***********************************************************/
43 int UnifracUnweightedCommand::execute() {
44         try {
45
46                 userData.resize(numComp,0);  //data[0] = unweightedscore 
47                 randomData.resize(numComp,0); //data[0] = unweightedscore
48                 //create new tree with same num nodes and leaves as users
49                 
50                 outSum << "Tree#" << '\t' << "Groups" << '\t'  <<  "UWScore" <<'\t' << "UWSig" <<  endl;
51                 cout << "Tree#" << '\t' << "Groups" << '\t'  <<  "UWScore" << '\t' << "UWSig" <<  endl;
52                 
53                 //get pscores for users trees
54                 for (int i = 0; i < T.size(); i++) {
55                         counter = 0;
56                         
57                         output = new ColumnFile(globaldata->getTreeFile()  + toString(i+1) + ".unweighted");
58                         
59                         //get unweighted for users tree
60                         rscoreFreq.resize(numComp);  
61                         rCumul.resize(numComp);  
62                         utreeScores.resize(numComp);  
63                         UWScoreSig.resize(numComp); 
64
65                         userData = unweighted->getValues(T[i]);  //userData[0] = unweightedscore
66                         
67                         //output scores for each combination
68                         for(int k = 0; k < numComp; k++) {
69                                 //saves users score
70                                 utreeScores[k].push_back(userData[k]);
71
72                         }
73                         
74                         //get unweighted scores for random trees
75                         for (int j = 0; j < iters; j++) {
76                                 //we need a different getValues because when we swap the labels we only want to swap those in each parwise comparison
77                                 randomData = unweighted->getValues(T[i], "", "");
78                                 
79                                 for(int k = 0; k < numComp; k++) {      
80                                         //add trees unweighted score to map of scores
81                                         it2 = rscoreFreq[k].find(randomData[k]);
82                                         if (it2 != rscoreFreq[k].end()) {//already have that score
83                                                 rscoreFreq[k][randomData[k]]++;
84                                         }else{//first time we have seen this score
85                                                 rscoreFreq[k][randomData[k]] = 1;
86                                         }
87                                 
88                                         //add randoms score to validscores
89                                         validScores[randomData[k]] = randomData[k];
90                                 }
91                                 
92                         }
93                 
94                         for(int a = 0; a < numComp; a++) {
95                                 float rcumul = 1.0000;
96                                 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
97                                 for (it = validScores.begin(); it != validScores.end(); it++) { 
98                                         //make rscoreFreq map and rCumul
99                                         it2 = rscoreFreq[a].find(it->first);
100                                         rCumul[a][it->first] = rcumul;
101                                         //get percentage of random trees with that info
102                                         if (it2 != rscoreFreq[a].end()) {  rscoreFreq[a][it->first] /= iters; rcumul-= it2->second;  }
103                                         else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
104                                 }
105                                 UWScoreSig[a].push_back(rCumul[a][userData[a]]);
106                         }
107                 
108                 
109                 
110                         printUnweightedFile();
111                         printUWSummaryFile(i);
112                         
113                         delete output;
114                         rscoreFreq.clear(); 
115                         rCumul.clear();  
116                         validScores.clear(); 
117                         utreeScores.clear();  
118                         UWScoreSig.clear(); 
119                 }
120                 
121                 //reset groups parameter
122                 globaldata->Groups.clear(); 
123                 outSum.close();
124                 
125                 return 0;
126                 
127         }
128         catch(exception& e) {
129                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
130                 exit(1);
131         }
132         catch(...) {
133                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
134                 exit(1);
135         }
136 }
137 /***********************************************************/
138 void UnifracUnweightedCommand::printUnweightedFile() {
139         try {
140                 vector<double> data;
141                 vector<string> tags;
142                 tags.push_back("Score"); tags.push_back("RandFreq"); tags.push_back("RandCumul");
143                 
144                 for(int a = 0; a < numComp; a++) {
145                         output->initFile(groupComb[a], tags);
146                         //print each line
147                         for (it = validScores.begin(); it != validScores.end(); it++) { 
148                                 data.push_back(it->first);  data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]); 
149                                 output->output(data);
150                                 data.clear();
151                         } 
152                         output->resetFile();
153                 }
154         }
155         catch(exception& e) {
156                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
157                 exit(1);
158         }
159         catch(...) {
160                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
161                 exit(1);
162         }
163 }
164
165 /***********************************************************/
166 void UnifracUnweightedCommand::printUWSummaryFile(int i) {
167         try {
168                                 
169                 //format output
170                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
171                         
172                 //print each line
173
174                 for(int a = 0; a < numComp; a++) {
175                         outSum << i+1 << '\t';
176                         cout << i+1 << '\t';
177                         
178                         if (UWScoreSig[a][0] > (1/(float)iters)) {
179                                 outSum << setprecision(6) << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << UWScoreSig[a][0] << endl;
180                                 cout << setprecision(6)  << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << UWScoreSig[a][0] << endl; 
181                         }else {
182                                 outSum << setprecision(6) << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
183                                 cout << setprecision(6)  << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl; 
184                         }
185                 }
186                 
187         }
188         catch(exception& e) {
189                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
190                 exit(1);
191         }
192         catch(...) {
193                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
194                 exit(1);
195         }
196 }
197
198 /***********************************************************/
199
200