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