]> git.donarmstrong.com Git - mothur.git/blob - unifracunweightedcommand.cpp
added bootstrap.shared command and fixed some bugs with heatmap
[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                         
62                         output = new ColumnFile(globaldata->getTreeFile()  + 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                         delete output;
122                         rscoreFreq.clear(); 
123                         rCumul.clear();  
124                         validScores.clear(); 
125                         utreeScores.clear();  
126                         UWScoreSig.clear(); 
127                 }
128                 
129                 //reset groups parameter
130                 globaldata->Groups.clear(); 
131                 outSum.close();
132                 
133                 return 0;
134                 
135         }
136         catch(exception& e) {
137                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
138                 exit(1);
139         }
140         catch(...) {
141                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
142                 exit(1);
143         }
144 }
145 /***********************************************************/
146 void UnifracUnweightedCommand::printUnweightedFile() {
147         try {
148                 vector<double> data;
149                 vector<string> tags;
150                 tags.push_back("Score"); tags.push_back("RandFreq"); tags.push_back("RandCumul");
151                 
152                 for(int a = 0; a < numComp; a++) {
153                         output->initFile(groupComb[a], tags);
154                         //print each line
155                         for (it = validScores.begin(); it != validScores.end(); it++) { 
156                                 data.push_back(it->first);  data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]); 
157                                 output->output(data);
158                                 data.clear();
159                         } 
160                         output->resetFile();
161                 }
162         }
163         catch(exception& e) {
164                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
165                 exit(1);
166         }
167         catch(...) {
168                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
169                 exit(1);
170         }
171 }
172
173 /***********************************************************/
174 void UnifracUnweightedCommand::printUWSummaryFile() {
175         try {
176                                 
177                 //format output
178                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
179                         
180                 //print each line
181
182                 for(int a = 0; a < numComp; a++) {
183                         if (UWScoreSig[a][0] > (1/(float)iters)) {
184                                 outSum << setprecision(6) << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << UWScoreSig[a][0] << endl;
185                                 cout << setprecision(6)  << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << UWScoreSig[a][0] << endl; 
186                         }else {
187                                 outSum << setprecision(6) << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl;
188                                 cout << setprecision(6)  << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(globaldata->getIters().length()) << "<" << (1/float(iters)) << endl; 
189                         }
190                 }
191                 
192         }
193         catch(exception& e) {
194                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
195                 exit(1);
196         }
197         catch(...) {
198                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
199                 exit(1);
200         }
201 }
202
203 /***********************************************************/
204
205