]> git.donarmstrong.com Git - mothur.git/blob - unifracunweightedcommand.cpp
This is 1.5.
[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                                 mothurOut("You must execute the read.tree command, before you may execute the unifrac.unweighted command."); mothurOutEndLine(); 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                 errorOut(e, "UnifracUnweightedCommand", "UnifracUnweightedCommand");
74                 exit(1);
75         }
76 }
77
78 //**********************************************************************************************************************
79
80 void UnifracUnweightedCommand::help(){
81         try {
82                 mothurOut("The unifrac.unweighted command can only be executed after a successful read.tree command.\n");
83                 mothurOut("The unifrac.unweighted command parameters are groups and iters.  No parameters are required.\n");
84                 mothurOut("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");
85                 mothurOut("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");
86                 mothurOut("The unifrac.unweighted command should be in the following format: unifrac.unweighted(groups=yourGroups, iters=yourIters).\n");
87                 mothurOut("Example unifrac.unweighted(groups=A-B-C, iters=500).\n");
88                 mothurOut("The default value for groups is all the groups in your groupfile, and iters is 1000.\n");
89                 mothurOut("The unifrac.unweighted command output two files: .unweighted and .uwsummary their descriptions are in the manual.\n");
90                 mothurOut("Note: No spaces between parameter labels (i.e. groups), '=' and parameters (i.e.yourGroups).\n\n");
91         }
92         catch(exception& e) {
93                 errorOut(e, "UnifracUnweightedCommand", "help");
94                 exit(1);
95         }
96 }
97
98
99 /***********************************************************/
100 int UnifracUnweightedCommand::execute() {
101         try {
102                 
103                 if (abort == true) { return 0; }
104                 
105                 userData.resize(numComp,0);  //data[0] = unweightedscore 
106                 randomData.resize(numComp,0); //data[0] = unweightedscore
107                 //create new tree with same num nodes and leaves as users
108                 
109                 outSum << "Tree#" << '\t' << "Groups" << '\t'  <<  "UWScore" <<'\t' << "UWSig" <<  endl;
110                 mothurOut("Tree#\tGroups\tUWScore\tUWSig"); mothurOutEndLine();
111                 
112                 //get pscores for users trees
113                 for (int i = 0; i < T.size(); i++) {
114                         counter = 0;
115                         
116                         output = new ColumnFile(globaldata->getTreeFile()  + toString(i+1) + ".unweighted", itersString);
117                         
118                         //get unweighted for users tree
119                         rscoreFreq.resize(numComp);  
120                         rCumul.resize(numComp);  
121                         utreeScores.resize(numComp);  
122                         UWScoreSig.resize(numComp); 
123
124                         userData = unweighted->getValues(T[i]);  //userData[0] = unweightedscore
125                         
126                         //output scores for each combination
127                         for(int k = 0; k < numComp; k++) {
128                                 //saves users score
129                                 utreeScores[k].push_back(userData[k]);
130
131                         }
132                         
133                         //get unweighted scores for random trees
134                         for (int j = 0; j < iters; j++) {
135                                 //we need a different getValues because when we swap the labels we only want to swap those in each parwise comparison
136                                 randomData = unweighted->getValues(T[i], "", "");
137                         
138                                 for(int k = 0; k < numComp; k++) {      
139                                         //add trees unweighted score to map of scores
140                                         map<float,float>::iterator it = rscoreFreq[k].find(randomData[k]);
141                                         if (it != rscoreFreq[k].end()) {//already have that score
142                                                 rscoreFreq[k][randomData[k]]++;
143                                         }else{//first time we have seen this score
144                                                 rscoreFreq[k][randomData[k]] = 1;
145                                         }
146                                 
147                                         //add randoms score to validscores
148                                         validScores[randomData[k]] = randomData[k];
149                                 }
150                                 
151                         }
152                 
153                         for(int a = 0; a < numComp; a++) {
154                                 float rcumul = 1.0000;
155                                 //this loop fills the cumulative maps and put 0.0000 in the score freq map to make it easier to print.
156                                 for (map<float,float>::iterator it = validScores.begin(); it != validScores.end(); it++) { 
157                                         //make rscoreFreq map and rCumul
158                                         map<float,float>::iterator it2 = rscoreFreq[a].find(it->first);
159                                         rCumul[a][it->first] = rcumul;
160                                         //get percentage of random trees with that info
161                                         if (it2 != rscoreFreq[a].end()) {  rscoreFreq[a][it->first] /= iters; rcumul-= it2->second;  }
162                                         else { rscoreFreq[a][it->first] = 0.0000; } //no random trees with that score
163                                 }
164                                 UWScoreSig[a].push_back(rCumul[a][userData[a]]);
165                         }
166                 
167                 
168                 
169                         printUnweightedFile();
170                         printUWSummaryFile(i);
171                         
172                         delete output;
173                         rscoreFreq.clear(); 
174                         rCumul.clear();  
175                         validScores.clear(); 
176                         utreeScores.clear();  
177                         UWScoreSig.clear(); 
178                 }
179                 
180                 //reset groups parameter
181                 globaldata->Groups.clear(); 
182                 outSum.close();
183                 
184                 return 0;
185                 
186         }
187         catch(exception& e) {
188                 errorOut(e, "UnifracUnweightedCommand", "execute");
189                 exit(1);
190         }
191 }
192 /***********************************************************/
193 void UnifracUnweightedCommand::printUnweightedFile() {
194         try {
195                 vector<double> data;
196                 vector<string> tags;
197                 tags.push_back("Score"); tags.push_back("RandFreq"); tags.push_back("RandCumul");
198                 
199                 for(int a = 0; a < numComp; a++) {
200                         output->initFile(groupComb[a], tags);
201                         //print each line
202                         for (map<float,float>::iterator it = validScores.begin(); it != validScores.end(); it++) { 
203                                 data.push_back(it->first);  data.push_back(rscoreFreq[a][it->first]); data.push_back(rCumul[a][it->first]); 
204                                 output->output(data);
205                                 data.clear();
206                         } 
207                         output->resetFile();
208                 }
209         }
210         catch(exception& e) {
211                 errorOut(e, "UnifracUnweightedCommand", "printUnweightedFile");
212                 exit(1);
213         }
214 }
215
216 /***********************************************************/
217 void UnifracUnweightedCommand::printUWSummaryFile(int i) {
218         try {
219                                 
220                 //format output
221                 outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint);
222                         
223                 //print each line
224
225                 for(int a = 0; a < numComp; a++) {
226                         outSum << i+1 << '\t';
227                         mothurOut(toString(i+1) + "\t");
228                         
229                         if (UWScoreSig[a][0] > (1/(float)iters)) {
230                                 outSum << setprecision(6) << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(itersString.length()) << UWScoreSig[a][0] << endl;
231                                 cout << setprecision(6)  << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(itersString.length()) << UWScoreSig[a][0] << endl; 
232                                 mothurOutJustToLog(groupComb[a]  + "\t" + toString(utreeScores[a][0])  + "\t" + toString(UWScoreSig[a][0])); mothurOutEndLine(); 
233                         }else {
234                                 outSum << setprecision(6) << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(itersString.length()) << "<" << (1/float(iters)) << endl;
235                                 cout << setprecision(6)  << groupComb[a]  << '\t' << utreeScores[a][0] << '\t' << setprecision(itersString.length()) << "<" << (1/float(iters)) << endl; 
236                                 mothurOutJustToLog(groupComb[a]  + "\t" + toString(utreeScores[a][0])  + "\t<" + toString((1/float(iters)))); mothurOutEndLine();
237                         }
238                 }
239                 
240         }
241         catch(exception& e) {
242                 errorOut(e, "UnifracUnweightedCommand", "printUWSummaryFile");
243                 exit(1);
244         }
245 }
246
247 /***********************************************************/
248
249