]> git.donarmstrong.com Git - mothur.git/blob - unifracunweightedcommand.cpp
merged pat's trim seqs edits with sarah's major overhaul of global data; also added...
[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(string option) {
14         try {
15                 globaldata = GlobalData::getInstance();
16                 abort = false;
17                 Groups.clear();
18                 
19                 //allow user to run help
20                 if(option == "help") { help(); abort = true; }
21                 
22                 else {
23                         //valid paramters for this command
24                         string Array[] =  {"groups","iters"};
25                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
26                         
27                         OptionParser parser(option);
28                         map<string,string> parameters = parser.getParameters();
29                         
30                         ValidParameters validParameter;
31                 
32                         //check to make sure all parameters are valid for command
33                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
34                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
35                         }
36                         
37                         if (globaldata->gTree.size() == 0) {//no trees were read
38                                 cout << "You must execute the read.tree command, before you may execute the unifrac.unweighted command." << endl; abort = true;  }
39                                                                                 
40                         //check for optional parameter and set defaults
41                         // ...at some point should added some additional type checking...
42                         groups = validParameter.validFile(parameters, "groups", false);                 
43                         if (groups == "not found") { groups = ""; }
44                         else { 
45                                 splitAtDash(groups, Groups);
46                                 globaldata->Groups = Groups;
47                         }
48                                 
49                         itersString = validParameter.validFile(parameters, "iters", false);                     if (itersString == "not found") { itersString = "1000"; }
50                         convert(itersString, iters); 
51                         
52                         
53                         if (abort == false) {
54                                 T = globaldata->gTree;
55                                 tmap = globaldata->gTreemap;
56                                 sumFile = globaldata->getTreeFile() + ".uwsummary";
57                                 openOutputFile(sumFile, outSum);
58                                 
59                                 util = new SharedUtil();
60                                 util->setGroups(globaldata->Groups, tmap->namesOfGroups, allGroups, numGroups, "unweighted");   //sets the groups the user wants to analyze
61                                 util->getCombos(groupComb, globaldata->Groups, numComp);
62                                 
63                                 if (numGroups == 1) { numComp++; groupComb.push_back(allGroups); }
64                                 
65                                 unweighted = new Unweighted(tmap);
66                                 
67                         }
68                         
69                 }
70                 
71         }
72         catch(exception& e) {
73                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
74                 exit(1);
75         }
76         catch(...) {
77                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function UnifracUnweightedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
78                 exit(1);
79         }
80 }
81
82 //**********************************************************************************************************************
83
84 void UnifracUnweightedCommand::help(){
85         try {
86                 cout << "The unifrac.unweighted command can only be executed after a successful read.tree command." << "\n";
87                 cout << "The unifrac.unweighted command parameters are groups and iters.  No parameters are required." << "\n";
88                 cout << "The groups parameter allows you to specify which of the groups in your groupfile you would like analyzed.  You must enter at least 1 valid group." << "\n";
89                 cout << "The group names are separated by dashes.  The iters parameter allows you to specify how many random trees you would like compared to your tree." << "\n";
90                 cout << "The unifrac.unweighted command should be in the following format: unifrac.unweighted(groups=yourGroups, iters=yourIters)." << "\n";
91                 cout << "Example unifrac.unweighted(groups=A-B-C, iters=500)." << "\n";
92                 cout << "The default value for groups is all the groups in your groupfile, and iters is 1000." << "\n";
93                 cout << "The unifrac.unweighted command output two files: .unweighted and .uwsummary their descriptions are in the manual." << "\n";
94                 cout << "Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups)." << "\n" << "\n";
95         }
96         catch(exception& e) {
97                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
98                 exit(1);
99         }
100         catch(...) {
101                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
102                 exit(1);
103         }       
104 }
105
106
107 /***********************************************************/
108 int UnifracUnweightedCommand::execute() {
109         try {
110                 
111                 if (abort == true) { return 0; }
112                 
113                 userData.resize(numComp,0);  //data[0] = unweightedscore 
114                 randomData.resize(numComp,0); //data[0] = unweightedscore
115                 //create new tree with same num nodes and leaves as users
116                 
117                 outSum << "Tree#" << '\t' << "Groups" << '\t'  <<  "UWScore" <<'\t' << "UWSig" <<  endl;
118                 cout << "Tree#" << '\t' << "Groups" << '\t'  <<  "UWScore" << '\t' << "UWSig" <<  endl;
119                 
120                 //get pscores for users trees
121                 for (int i = 0; i < T.size(); i++) {
122                         counter = 0;
123                         
124                         output = new ColumnFile(globaldata->getTreeFile()  + toString(i+1) + ".unweighted", itersString);
125                         
126                         //get unweighted for users tree
127                         rscoreFreq.resize(numComp);  
128                         rCumul.resize(numComp);  
129                         utreeScores.resize(numComp);  
130                         UWScoreSig.resize(numComp); 
131
132                         userData = unweighted->getValues(T[i]);  //userData[0] = unweightedscore
133                         
134                         //output scores for each combination
135                         for(int k = 0; k < numComp; k++) {
136                                 //saves users score
137                                 utreeScores[k].push_back(userData[k]);
138
139                         }
140                         
141                         //get unweighted scores for random trees
142                         for (int j = 0; j < iters; j++) {
143                                 //we need a different getValues because when we swap the labels we only want to swap those in each parwise comparison
144                                 randomData = unweighted->getValues(T[i], "", "");
145                                 
146                                 for(int k = 0; k < numComp; k++) {      
147                                         //add trees unweighted score to map of scores
148                                         map<float,float>::iterator it = rscoreFreq[k].find(randomData[k]);
149                                         if (it != rscoreFreq[k].end()) {//already have that score
150                                                 rscoreFreq[k][randomData[k]]++;
151                                         }else{//first time we have seen this score
152                                                 rscoreFreq[k][randomData[k]] = 1;
153                                         }
154                                 
155                                         //add randoms score to validscores
156                                         validScores[randomData[k]] = randomData[k];
157                                 }
158                                 
159                         }
160                 
161                         for(int a = 0; a < numComp; a++) {
162                                 float rcumul = 1.0000;
163                                 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
164                                 for (map<float,float>::iterator it = validScores.begin(); it != validScores.end(); it++) { 
165                                         //make rscoreFreq map and rCumul
166                                         map<float,float>::iterator it2 = rscoreFreq[a].find(it->first);
167                                         rCumul[a][it->first] = rcumul;
168                                         //get percentage of random trees with that info
169                                         if (it2 != rscoreFreq[a].end()) {  rscoreFreq[a][it->first] /= iters; rcumul-= it2->second;  }
170                                         else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
171                                 }
172                                 UWScoreSig[a].push_back(rCumul[a][userData[a]]);
173                         }
174                 
175                 
176                 
177                         printUnweightedFile();
178                         printUWSummaryFile(i);
179                         
180                         delete output;
181                         rscoreFreq.clear(); 
182                         rCumul.clear();  
183                         validScores.clear(); 
184                         utreeScores.clear();  
185                         UWScoreSig.clear(); 
186                 }
187                 
188                 //reset groups parameter
189                 globaldata->Groups.clear(); 
190                 outSum.close();
191                 
192                 return 0;
193                 
194         }
195         catch(exception& e) {
196                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
197                 exit(1);
198         }
199         catch(...) {
200                 cout << "An unknown error has occurred in the UnifracUnweightedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
201                 exit(1);
202         }
203 }
204 /***********************************************************/
205 void UnifracUnweightedCommand::printUnweightedFile() {
206         try {
207                 vector<double> data;
208                 vector<string> tags;
209                 tags.push_back("Score"); tags.push_back("RandFreq"); tags.push_back("RandCumul");
210                 
211                 for(int a = 0; a < numComp; a++) {
212                         output->initFile(groupComb[a], tags);
213                         //print each line
214                         for (map<float,float>::iterator it = validScores.begin(); it != validScores.end(); it++) { 
215                                 data.push_back(it->first);  data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]); 
216                                 output->output(data);
217                                 data.clear();
218                         } 
219                         output->resetFile();
220                 }
221         }
222         catch(exception& e) {
223                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUnweightedFile. 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 UnifracUnweightedCommand class function printUnweightedFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
228                 exit(1);
229         }
230 }
231
232 /***********************************************************/
233 void UnifracUnweightedCommand::printUWSummaryFile(int i) {
234         try {
235                                 
236                 //format output
237                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
238                         
239                 //print each line
240
241                 for(int a = 0; a < numComp; a++) {
242                         outSum << i+1 << '\t';
243                         cout << i+1 << '\t';
244                         
245                         if (UWScoreSig[a][0] > (1/(float)iters)) {
246                                 outSum << setprecision(6) << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(itersString.length()) << UWScoreSig[a][0] << endl;
247                                 cout << setprecision(6)  << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(itersString.length()) << UWScoreSig[a][0] << endl; 
248                         }else {
249                                 outSum << setprecision(6) << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(itersString.length()) << "<" << (1/float(iters)) << endl;
250                                 cout << setprecision(6)  << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(itersString.length()) << "<" << (1/float(iters)) << endl; 
251                         }
252                 }
253                 
254         }
255         catch(exception& e) {
256                 cout << "Standard Error: " << e.what() << " has occurred in the UnifracUnweightedCommand class Function printUWSummaryFile. 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 printUWSummaryFile. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
261                 exit(1);
262         }
263 }
264
265 /***********************************************************/
266
267