]> git.donarmstrong.com Git - mothur.git/blob - rarefactsharedcommand.cpp
added getCommandInfoCommand for gui
[mothur.git] / rarefactsharedcommand.cpp
1 /*
2  *  rarefactsharedcommand.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/6/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "rarefactsharedcommand.h"
11 #include "sharedsobs.h"
12 #include "sharednseqs.h"
13
14 //**********************************************************************************************************************
15 vector<string> RareFactSharedCommand::setParameters(){  
16         try {
17                 CommandParameter pshared("shared", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pshared);
18                 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
19                 CommandParameter pfreq("freq", "Number", "", "100", "", "", "",false,false); parameters.push_back(pfreq);
20                 CommandParameter piters("iters", "Number", "", "1000", "", "", "",false,false); parameters.push_back(piters);
21                 CommandParameter pcalc("calc", "Multiple", "sharednseqs-sharedobserved", "sharedobserved", "", "", "",true,false); parameters.push_back(pcalc);
22                 CommandParameter pjumble("jumble", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pjumble);
23                 CommandParameter pgroups("groups", "String", "", "", "", "", "",false,false); parameters.push_back(pgroups);
24                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
25                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
26                 
27                 vector<string> myArray;
28                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
29                 return myArray;
30         }
31         catch(exception& e) {
32                 m->errorOut(e, "RareFactSharedCommand", "setParameters");
33                 exit(1);
34         }
35 }
36 //**********************************************************************************************************************
37 string RareFactSharedCommand::getHelpString(){  
38         try {
39                 string helpString = "";
40                 ValidCalculators validCalculator;
41                 helpString += "The collect.shared command parameters are shared, label, freq, calc and groups.  shared is required if there is no current sharedfile. \n";
42                 helpString += "The rarefaction.shared command parameters are shared, label, iters, groups, jumble and calc.  shared is required if there is no current sharedfile. \n";
43                 helpString += "The rarefaction command should be in the following format: \n";
44                 helpString += "rarefaction.shared(label=yourLabel, iters=yourIters, calc=yourEstimators, jumble=yourJumble, groups=yourGroups).\n";
45                 helpString += "The freq parameter is used indicate when to output your data, by default it is set to 100. But you can set it to a percentage of the number of sequence. For example freq=0.10, means 10%. \n";
46                 helpString += "Example rarefaction.shared(label=unique-0.01-0.03,  iters=10000, groups=B-C, jumble=T, calc=sharedobserved).\n";
47                 helpString += "The default values for iters is 1000, freq is 100, and calc is sharedobserved which calculates the shared rarefaction curve for the observed richness.\n";
48                 helpString += "The default value for groups is all the groups in your groupfile, and jumble is true.\n";
49                 helpString += validCalculator.printCalc("sharedrarefaction");
50                 helpString += "The label parameter is used to analyze specific labels in your input.\n";
51                 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like analyzed.  You must enter at least 2 valid groups.\n";
52                 helpString += "Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n";
53                 return helpString;
54         }
55         catch(exception& e) {
56                 m->errorOut(e, "RareFactSharedCommand", "getHelpString");
57                 exit(1);
58         }
59 }
60
61 //**********************************************************************************************************************
62 RareFactSharedCommand::RareFactSharedCommand(){ 
63         try {
64                 abort = true; calledHelp = true; 
65                 setParameters();
66                 vector<string> tempOutNames;
67                 outputTypes["sharedrarefaction"] = tempOutNames;
68                 outputTypes["sharedr_nseqs"] = tempOutNames;
69         }
70         catch(exception& e) {
71                 m->errorOut(e, "RareFactSharedCommand", "RareFactSharedCommand");
72                 exit(1);
73         }
74 }
75 //**********************************************************************************************************************
76
77 RareFactSharedCommand::RareFactSharedCommand(string option)  {
78         try {
79                 abort = false; calledHelp = false;   
80                 allLines = 1;
81                                 
82                 //allow user to run help
83                 if(option == "help") {  help(); abort = true; calledHelp = true; }
84                 
85                 else {
86                         vector<string> myArray = setParameters();
87                         
88                         OptionParser parser(option);
89                         map<string,string> parameters = parser.getParameters();
90                         map<string,string>::iterator it;
91                         
92                         ValidParameters validParameter;
93                         
94                         //check to make sure all parameters are valid for command
95                         for (it = parameters.begin(); it != parameters.end(); it++) { 
96                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
97                         }
98                         
99                         //initialize outputTypes
100                         vector<string> tempOutNames;
101                         outputTypes["sharedrarefaction"] = tempOutNames;
102                         outputTypes["sharedr_nseqs"] = tempOutNames;
103                         
104                         //if the user changes the input directory command factory will send this info to us in the output parameter 
105                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
106                         if (inputDir == "not found"){   inputDir = "";          }
107                         else {
108                                 string path;
109                                 it = parameters.find("shared");
110                                 //user has given a template file
111                                 if(it != parameters.end()){ 
112                                         path = m->hasPath(it->second);
113                                         //if the user has not given a path then, add inputdir. else leave path alone.
114                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
115                                 }
116                         }
117                         
118                         //get shared file
119                         sharedfile = validParameter.validFile(parameters, "shared", true);
120                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }        
121                         else if (sharedfile == "not found") { 
122                                 //if there is a current shared file, use it
123                                 sharedfile = m->getSharedFile(); 
124                                 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
125                                 else {  m->mothurOut("You have no current sharedfile and the shared parameter is required."); m->mothurOutEndLine(); abort = true; }
126                         }
127                         
128                         
129                         //if the user changes the output directory command factory will send this info to us in the output parameter 
130                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(sharedfile);             }
131                         
132                         
133                         //check for optional parameter and set defaults
134                         // ...at some point should added some additional type checking...
135                         label = validParameter.validFile(parameters, "label", false);                   
136                         if (label == "not found") { label = ""; }
137                         else { 
138                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
139                                 else { allLines = 1;  }
140                         }
141                         
142                                 
143                         calc = validParameter.validFile(parameters, "calc", false);                     
144                         if (calc == "not found") { calc = "sharedobserved";  }
145                         else { 
146                                  if (calc == "default")  {  calc = "sharedobserved";  }
147                         }
148                         m->splitAtDash(calc, Estimators);
149                         
150                         groups = validParameter.validFile(parameters, "groups", false);                 
151                         if (groups == "not found") { groups = ""; }
152                         else { 
153                                 m->splitAtDash(groups, Groups);
154                         }
155                         m->Groups = Groups;
156                         
157                         string temp;
158                         temp = validParameter.validFile(parameters, "freq", false);                     if (temp == "not found") { temp = "100"; }
159                         convert(temp, freq); 
160                         
161                         temp = validParameter.validFile(parameters, "iters", false);                    if (temp == "not found") { temp = "1000"; }
162                         convert(temp, nIters); 
163                         
164                         temp = validParameter.validFile(parameters, "jumble", false);                   if (temp == "not found") { temp = "T"; }
165                         if (m->isTrue(temp)) { jumble = true; }
166                         else { jumble = false; }
167                         m->jumble = jumble;
168                                 
169                 }
170
171         }
172         catch(exception& e) {
173                 m->errorOut(e, "RareFactSharedCommand", "RareFactSharedCommand");
174                 exit(1);
175         }
176 }
177 //**********************************************************************************************************************
178
179 int RareFactSharedCommand::execute(){
180         try {
181         
182                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
183         
184                 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(sharedfile));
185                 
186                 ValidCalculators validCalculator;
187                         
188                 for (int i=0; i<Estimators.size(); i++) {
189                         if (validCalculator.isValidCalculator("sharedrarefaction", Estimators[i]) == true) { 
190                                 if (Estimators[i] == "sharedobserved") { 
191                                         rDisplays.push_back(new RareDisplay(new SharedSobs(), new SharedThreeColumnFile(fileNameRoot+"shared.rarefaction", "")));
192                                         outputNames.push_back(fileNameRoot+"shared.rarefaction"); outputTypes["sharedrarefaction"].push_back(fileNameRoot+"shared.rarefaction");
193                                 }else if (Estimators[i] == "sharednseqs") { 
194                                         rDisplays.push_back(new RareDisplay(new SharedNSeqs(), new SharedThreeColumnFile(fileNameRoot+"shared.r_nseqs", "")));
195                                         outputNames.push_back(fileNameRoot+"shared.r_nseqs"); outputTypes["sharedr_nseqs"].push_back(fileNameRoot+"shared.r_nseqs");
196                                 }
197                         }
198                 }
199                 
200                 //if the users entered no valid calculators don't execute command
201                 if (rDisplays.size() == 0) { return 0; }
202                         
203                 input = new InputData(sharedfile, "sharedfile");
204                 lookup = input->getSharedRAbundVectors();
205                 string lastLabel = lookup[0]->getLabel();
206                 
207                 if (m->control_pressed) { 
208                         m->Groups.clear(); 
209                         delete input;
210                         for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }
211                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
212                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
213                         return 0;
214                 }
215
216
217                 if (lookup.size() < 2) { 
218                         m->mothurOut("I cannot run the command without at least 2 valid groups."); 
219                         for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
220                         return 0;
221                 }
222                 
223                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
224                 set<string> processedLabels;
225                 set<string> userLabels = labels;
226         
227                 //as long as you are not at the end of the file or done wih the lines you want
228                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
229                         if (m->control_pressed) { 
230                                 m->Groups.clear(); 
231                                 delete input;
232                                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }
233                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
234                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
235                                 return 0;
236                         }
237                         
238                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
239                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
240                                 rCurve = new Rarefact(lookup, rDisplays);
241                                 rCurve->getSharedCurve(freq, nIters);
242                                 delete rCurve;
243                         
244                                 processedLabels.insert(lookup[0]->getLabel());
245                                 userLabels.erase(lookup[0]->getLabel());
246                         }
247                         
248                         if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
249                                         string saveLabel = lookup[0]->getLabel();
250                         
251                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
252                                         lookup = input->getSharedRAbundVectors(lastLabel);
253
254                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
255                                         rCurve = new Rarefact(lookup, rDisplays);
256                                         rCurve->getSharedCurve(freq, nIters);
257                                         delete rCurve;
258
259                                         processedLabels.insert(lookup[0]->getLabel());
260                                         userLabels.erase(lookup[0]->getLabel());
261                                         
262                                         //restore real lastlabel to save below
263                                         lookup[0]->setLabel(saveLabel);
264                         }
265                                 
266                         
267                         lastLabel = lookup[0]->getLabel();
268                         
269                         //get next line to process
270                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
271                         lookup = input->getSharedRAbundVectors();
272                 }
273                 
274                 if (m->control_pressed) { 
275                         m->Groups.clear(); 
276                         delete input;
277                         for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }
278                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
279                         return 0;
280                 }
281                 
282                 //output error messages about any remaining user labels
283                 set<string>::iterator it;
284                 bool needToRun = false;
285                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
286                         m->mothurOut("Your file does not include the label " + *it); 
287                         if (processedLabels.count(lastLabel) != 1) {
288                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
289                                 needToRun = true;
290                         }else {
291                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
292                         }
293                 }
294                 
295                 if (m->control_pressed) { 
296                         m->Groups.clear(); 
297                         delete input; 
298                         for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }
299                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
300                         return 0;
301                 }
302                 
303                 //run last label if you need to
304                 if (needToRun == true)  {
305                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {     delete lookup[i]; }  } 
306                         lookup = input->getSharedRAbundVectors(lastLabel);
307
308                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
309                         rCurve = new Rarefact(lookup, rDisplays);
310                         rCurve->getSharedCurve(freq, nIters);
311                         delete rCurve;
312                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
313                 }
314                 
315                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }       
316                 m->Groups.clear(); 
317                 delete input;
318                 
319                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str()); } return 0; }
320                 
321                 m->mothurOutEndLine();
322                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
323                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
324                 m->mothurOutEndLine();
325
326                 return 0;
327         }
328         catch(exception& e) {
329                 m->errorOut(e, "RareFactSharedCommand", "execute");
330                 exit(1);
331         }
332 }
333
334
335 //**********************************************************************************************************************