]> git.donarmstrong.com Git - mothur.git/blob - rarefactsharedcommand.cpp
Revert to previous commit
[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                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
85                 
86                 else {
87                         vector<string> myArray = setParameters();
88                         
89                         OptionParser parser(option);
90                         map<string,string> parameters = parser.getParameters();
91                         map<string,string>::iterator it;
92                         
93                         ValidParameters validParameter;
94                         
95                         //check to make sure all parameters are valid for command
96                         for (it = parameters.begin(); it != parameters.end(); it++) { 
97                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
98                         }
99                         
100                         //initialize outputTypes
101                         vector<string> tempOutNames;
102                         outputTypes["sharedrarefaction"] = tempOutNames;
103                         outputTypes["sharedr_nseqs"] = tempOutNames;
104                         
105                         //if the user changes the input directory command factory will send this info to us in the output parameter 
106                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
107                         if (inputDir == "not found"){   inputDir = "";          }
108                         else {
109                                 string path;
110                                 it = parameters.find("shared");
111                                 //user has given a template file
112                                 if(it != parameters.end()){ 
113                                         path = m->hasPath(it->second);
114                                         //if the user has not given a path then, add inputdir. else leave path alone.
115                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
116                                 }
117                         }
118                         
119                         //get shared file
120                         sharedfile = validParameter.validFile(parameters, "shared", true);
121                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }        
122                         else if (sharedfile == "not found") { 
123                                 //if there is a current shared file, use it
124                                 sharedfile = m->getSharedFile(); 
125                                 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
126                                 else {  m->mothurOut("You have no current sharedfile and the shared parameter is required."); m->mothurOutEndLine(); abort = true; }
127                         }else { m->setSharedFile(sharedfile); }
128                         
129                         
130                         //if the user changes the output directory command factory will send this info to us in the output parameter 
131                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(sharedfile);             }
132                         
133                         
134                         //check for optional parameter and set defaults
135                         // ...at some point should added some additional type checking...
136                         label = validParameter.validFile(parameters, "label", false);                   
137                         if (label == "not found") { label = ""; }
138                         else { 
139                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
140                                 else { allLines = 1;  }
141                         }
142                         
143                                 
144                         calc = validParameter.validFile(parameters, "calc", false);                     
145                         if (calc == "not found") { calc = "sharedobserved";  }
146                         else { 
147                                  if (calc == "default")  {  calc = "sharedobserved";  }
148                         }
149                         m->splitAtDash(calc, Estimators);
150                         if (m->inUsersGroups("citation", Estimators)) { 
151                                 ValidCalculators validCalc; validCalc.printCitations(Estimators); 
152                                 //remove citation from list of calcs
153                                 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") {  Estimators.erase(Estimators.begin()+i); break; } }
154                         }
155                         
156                         groups = validParameter.validFile(parameters, "groups", false);                 
157                         if (groups == "not found") { groups = ""; }
158                         else { 
159                                 m->splitAtDash(groups, Groups);
160                         }
161                         m->setGroups(Groups);
162                         
163                         string temp;
164                         temp = validParameter.validFile(parameters, "freq", false);                     if (temp == "not found") { temp = "100"; }
165                         m->mothurConvert(temp, freq); 
166                         
167                         temp = validParameter.validFile(parameters, "iters", false);                    if (temp == "not found") { temp = "1000"; }
168                         m->mothurConvert(temp, nIters); 
169                         
170                         temp = validParameter.validFile(parameters, "jumble", false);                   if (temp == "not found") { temp = "T"; }
171                         if (m->isTrue(temp)) { jumble = true; }
172                         else { jumble = false; }
173                         m->jumble = jumble;
174                                 
175                 }
176
177         }
178         catch(exception& e) {
179                 m->errorOut(e, "RareFactSharedCommand", "RareFactSharedCommand");
180                 exit(1);
181         }
182 }
183 //**********************************************************************************************************************
184
185 int RareFactSharedCommand::execute(){
186         try {
187         
188                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
189         
190                 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(sharedfile));
191                 
192                 ValidCalculators validCalculator;
193                         
194                 for (int i=0; i<Estimators.size(); i++) {
195                         if (validCalculator.isValidCalculator("sharedrarefaction", Estimators[i]) == true) { 
196                                 if (Estimators[i] == "sharedobserved") { 
197                                         rDisplays.push_back(new RareDisplay(new SharedSobs(), new SharedThreeColumnFile(fileNameRoot+"shared.rarefaction", "")));
198                                         outputNames.push_back(fileNameRoot+"shared.rarefaction"); outputTypes["sharedrarefaction"].push_back(fileNameRoot+"shared.rarefaction");
199                                 }else if (Estimators[i] == "sharednseqs") { 
200                                         rDisplays.push_back(new RareDisplay(new SharedNSeqs(), new SharedThreeColumnFile(fileNameRoot+"shared.r_nseqs", "")));
201                                         outputNames.push_back(fileNameRoot+"shared.r_nseqs"); outputTypes["sharedr_nseqs"].push_back(fileNameRoot+"shared.r_nseqs");
202                                 }
203                         }
204                 }
205                 
206                 //if the users entered no valid calculators don't execute command
207                 if (rDisplays.size() == 0) { return 0; }
208                         
209                 input = new InputData(sharedfile, "sharedfile");
210                 lookup = input->getSharedRAbundVectors();
211                 string lastLabel = lookup[0]->getLabel();
212                 
213                 if (m->control_pressed) { 
214                         m->clearGroups(); 
215                         delete input;
216                         for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }
217                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);        }
218                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
219                         return 0;
220                 }
221
222
223                 if (lookup.size() < 2) { 
224                         m->mothurOut("I cannot run the command without at least 2 valid groups."); 
225                         for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
226                         return 0;
227                 }
228                 
229                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
230                 set<string> processedLabels;
231                 set<string> userLabels = labels;
232         
233                 //as long as you are not at the end of the file or done wih the lines you want
234                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
235                         if (m->control_pressed) { 
236                                 m->clearGroups(); 
237                                 delete input;
238                                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }
239                                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);        }
240                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
241                                 return 0;
242                         }
243                         
244                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
245                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
246                                 rCurve = new Rarefact(lookup, rDisplays);
247                                 rCurve->getSharedCurve(freq, nIters);
248                                 delete rCurve;
249                         
250                                 processedLabels.insert(lookup[0]->getLabel());
251                                 userLabels.erase(lookup[0]->getLabel());
252                         }
253                         
254                         if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
255                                         string saveLabel = lookup[0]->getLabel();
256                         
257                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
258                                         lookup = input->getSharedRAbundVectors(lastLabel);
259
260                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
261                                         rCurve = new Rarefact(lookup, rDisplays);
262                                         rCurve->getSharedCurve(freq, nIters);
263                                         delete rCurve;
264
265                                         processedLabels.insert(lookup[0]->getLabel());
266                                         userLabels.erase(lookup[0]->getLabel());
267                                         
268                                         //restore real lastlabel to save below
269                                         lookup[0]->setLabel(saveLabel);
270                         }
271                                 
272                         
273                         lastLabel = lookup[0]->getLabel();
274                         
275                         //get next line to process
276                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
277                         lookup = input->getSharedRAbundVectors();
278                 }
279                 
280                 if (m->control_pressed) { 
281                         m->clearGroups(); 
282                         delete input;
283                         for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }
284                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);        }
285                         return 0;
286                 }
287                 
288                 //output error messages about any remaining user labels
289                 set<string>::iterator it;
290                 bool needToRun = false;
291                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
292                         m->mothurOut("Your file does not include the label " + *it); 
293                         if (processedLabels.count(lastLabel) != 1) {
294                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
295                                 needToRun = true;
296                         }else {
297                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
298                         }
299                 }
300                 
301                 if (m->control_pressed) { 
302                         m->clearGroups(); 
303                         delete input; 
304                         for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }
305                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);        }
306                         return 0;
307                 }
308                 
309                 //run last label if you need to
310                 if (needToRun == true)  {
311                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {     delete lookup[i]; }  } 
312                         lookup = input->getSharedRAbundVectors(lastLabel);
313
314                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
315                         rCurve = new Rarefact(lookup, rDisplays);
316                         rCurve->getSharedCurve(freq, nIters);
317                         delete rCurve;
318                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
319                 }
320                 
321                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }       
322                 m->clearGroups(); 
323                 delete input;
324                 
325                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]); } return 0; }
326                 
327                 m->mothurOutEndLine();
328                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
329                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
330                 m->mothurOutEndLine();
331
332                 return 0;
333         }
334         catch(exception& e) {
335                 m->errorOut(e, "RareFactSharedCommand", "execute");
336                 exit(1);
337         }
338 }
339
340
341 //**********************************************************************************************************************