]> git.donarmstrong.com Git - mothur.git/blob - rarefactsharedcommand.cpp
added pipeline commands which involved change to command factory and command class...
[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::getValidParameters(){     
16         try {
17                 string Array[] =  {"iters","freq","label","calc","groups", "jumble","outputdir","inputdir"};
18                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
19                 return myArray;
20         }
21         catch(exception& e) {
22                 m->errorOut(e, "RareFactSharedCommand", "getValidParameters");
23                 exit(1);
24         }
25 }
26 //**********************************************************************************************************************
27 RareFactSharedCommand::RareFactSharedCommand(){ 
28         try {
29                 //initialize outputTypes
30                 vector<string> tempOutNames;
31                 outputTypes["sharedrarefaction"] = tempOutNames;
32                 outputTypes["sharedr_nseqs"] = tempOutNames;
33         }
34         catch(exception& e) {
35                 m->errorOut(e, "RareFactSharedCommand", "RareFactSharedCommand");
36                 exit(1);
37         }
38 }
39 //**********************************************************************************************************************
40 vector<string> RareFactSharedCommand::getRequiredParameters(){  
41         try {
42                 vector<string> myArray;
43                 return myArray;
44         }
45         catch(exception& e) {
46                 m->errorOut(e, "RareFactSharedCommand", "getRequiredParameters");
47                 exit(1);
48         }
49 }
50 //**********************************************************************************************************************
51 vector<string> RareFactSharedCommand::getRequiredFiles(){       
52         try {
53                 string Array[] =  {"shared"};
54                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
55                 return myArray;
56         }
57         catch(exception& e) {
58                 m->errorOut(e, "RareFactSharedCommand", "getRequiredFiles");
59                 exit(1);
60         }
61 }
62 //**********************************************************************************************************************
63
64 RareFactSharedCommand::RareFactSharedCommand(string option)  {
65         try {
66                 globaldata = GlobalData::getInstance();
67                 
68                 abort = false;
69                 allLines = 1;
70                 labels.clear();
71                 Estimators.clear();
72                 Groups.clear();
73                                 
74                 //allow user to run help
75                 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
76                 
77                 else {
78                         //valid paramters for this command
79                         string Array[] =  {"iters","freq","label","calc","groups", "jumble","outputdir","inputdir"};
80                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
81                         
82                         OptionParser parser(option);
83                         map<string,string> parameters = parser.getParameters();
84                         
85                         ValidParameters validParameter;
86                         
87                         //check to make sure all parameters are valid for command
88                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
89                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
90                         }
91                         
92                         //initialize outputTypes
93                         vector<string> tempOutNames;
94                         outputTypes["sharedrarefaction"] = tempOutNames;
95                         outputTypes["sharedr_nseqs"] = tempOutNames;
96                         
97                         //make sure the user has already run the read.otu command
98                         if (globaldata->getSharedFile() == "") {
99                                 if (globaldata->getListFile() == "") { m->mothurOut("You must read a list and a group, or a shared before you can use the collect.shared command."); m->mothurOutEndLine(); abort = true; }
100                                 else if (globaldata->getGroupFile() == "") { m->mothurOut("You must read a list and a group, or a shared before you can use the collect.shared command."); m->mothurOutEndLine(); abort = true; }
101                         }
102                         
103                         //if the user changes the output directory command factory will send this info to us in the output parameter 
104                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
105                                 outputDir = ""; 
106                                 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it       
107                         }
108
109                         
110                         //check for optional parameter and set defaults
111                         // ...at some point should added some additional type checking...
112                         label = validParameter.validFile(parameters, "label", false);                   
113                         if (label == "not found") { label = ""; }
114                         else { 
115                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
116                                 else { allLines = 1;  }
117                         }
118                         
119                         //if the user has not specified any labels use the ones from read.otu
120                         if(label == "") {  
121                                 allLines = globaldata->allLines; 
122                                 labels = globaldata->labels; 
123                         }
124                                 
125                         calc = validParameter.validFile(parameters, "calc", false);                     
126                         if (calc == "not found") { calc = "sharedobserved";  }
127                         else { 
128                                  if (calc == "default")  {  calc = "sharedobserved";  }
129                         }
130                         m->splitAtDash(calc, Estimators);
131                         
132                         groups = validParameter.validFile(parameters, "groups", false);                 
133                         if (groups == "not found") { groups = ""; }
134                         else { 
135                                 m->splitAtDash(groups, Groups);
136                         }
137                         globaldata->Groups = Groups;
138                         
139                         string temp;
140                         temp = validParameter.validFile(parameters, "freq", false);                     if (temp == "not found") { temp = "100"; }
141                         convert(temp, freq); 
142                         
143                         temp = validParameter.validFile(parameters, "iters", false);                    if (temp == "not found") { temp = "1000"; }
144                         convert(temp, nIters); 
145                         
146                         temp = validParameter.validFile(parameters, "jumble", false);                   if (temp == "not found") { temp = "T"; }
147                         if (m->isTrue(temp)) { jumble = true; }
148                         else { jumble = false; }
149                         globaldata->jumble = jumble;
150                         
151                         if (abort == false) {
152                         
153                                 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName));
154 //                              format = globaldata->getFormat();
155
156                                 
157                                 validCalculator = new ValidCalculators();
158                                 
159                                 for (int i=0; i<Estimators.size(); i++) {
160                                         if (validCalculator->isValidCalculator("sharedrarefaction", Estimators[i]) == true) { 
161                                                 if (Estimators[i] == "sharedobserved") { 
162                                                         rDisplays.push_back(new RareDisplay(new SharedSobs(), new SharedThreeColumnFile(fileNameRoot+"shared.rarefaction", "")));
163                                                         outputNames.push_back(fileNameRoot+"shared.rarefaction"); outputTypes["sharedrarefaction"].push_back(fileNameRoot+"shared.rarefaction");
164                                                 }else if (Estimators[i] == "sharednseqs") { 
165                                                         rDisplays.push_back(new RareDisplay(new SharedNSeqs(), new SharedThreeColumnFile(fileNameRoot+"shared.r_nseqs", "")));
166                                                         outputNames.push_back(fileNameRoot+"shared.r_nseqs"); outputTypes["sharedr_nseqs"].push_back(fileNameRoot+"shared.r_nseqs");
167                                                 }
168                                         }
169                                 }
170                         }
171                                 
172                 }
173
174         }
175         catch(exception& e) {
176                 m->errorOut(e, "RareFactSharedCommand", "RareFactSharedCommand");
177                 exit(1);
178         }
179 }
180
181 //**********************************************************************************************************************
182
183 void RareFactSharedCommand::help(){
184         try {
185                 m->mothurOut("The rarefaction.shared command can only be executed after a successful read.otu command.\n");
186                 m->mothurOut("The rarefaction.shared command parameters are label, iters, groups, jumble and calc.  No parameters are required.\n");
187                 m->mothurOut("The rarefaction command should be in the following format: \n");
188                 m->mothurOut("rarefaction.shared(label=yourLabel, iters=yourIters, calc=yourEstimators, jumble=yourJumble, groups=yourGroups).\n");
189                 m->mothurOut("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");
190                 m->mothurOut("Example rarefaction.shared(label=unique-0.01-0.03,  iters=10000, groups=B-C, jumble=T, calc=sharedobserved).\n");
191                 m->mothurOut("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");
192                 m->mothurOut("The default value for groups is all the groups in your groupfile, and jumble is true.\n");
193                 validCalculator->printCalc("sharedrarefaction", cout);
194                 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
195                 m->mothurOut("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");
196                 m->mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
197         }
198         catch(exception& e) {
199                 m->errorOut(e, "RareFactSharedCommand", "help");
200                 exit(1);
201         }
202 }
203
204 //**********************************************************************************************************************
205
206 RareFactSharedCommand::~RareFactSharedCommand(){
207         if (abort == false) {
208                 delete input;   globaldata->ginput = NULL;
209                 delete read;
210                 delete validCalculator;
211         }
212 }
213
214 //**********************************************************************************************************************
215
216 int RareFactSharedCommand::execute(){
217         try {
218         
219                 if (abort == true) { return 0; }
220                 
221                 //if the users entered no valid calculators don't execute command
222                 if (rDisplays.size() == 0) { return 0; }
223
224                 read = new ReadOTUFile(globaldata->inputFileName);      
225                 read->read(&*globaldata); 
226                         
227                 input = globaldata->ginput;
228                 lookup = input->getSharedRAbundVectors();
229                 string lastLabel = lookup[0]->getLabel();
230                 
231                 if (m->control_pressed) { 
232                         globaldata->Groups.clear(); 
233                         for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }
234                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
235                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
236                         return 0;
237                 }
238
239
240                 if (lookup.size() < 2) { 
241                         m->mothurOut("I cannot run the command without at least 2 valid groups."); 
242                         for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
243                         return 0;
244                 }
245                 
246                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
247                 set<string> processedLabels;
248                 set<string> userLabels = labels;
249         
250                 //as long as you are not at the end of the file or done wih the lines you want
251                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
252                         if (m->control_pressed) { 
253                                 globaldata->Groups.clear(); 
254                                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }
255                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
256                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
257                                 return 0;
258                         }
259                         
260                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
261                                 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
262                                 rCurve = new Rarefact(lookup, rDisplays);
263                                 rCurve->getSharedCurve(freq, nIters);
264                                 delete rCurve;
265                         
266                                 processedLabels.insert(lookup[0]->getLabel());
267                                 userLabels.erase(lookup[0]->getLabel());
268                         }
269                         
270                         if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
271                                         string saveLabel = lookup[0]->getLabel();
272                         
273                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
274                                         lookup = input->getSharedRAbundVectors(lastLabel);
275
276                                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
277                                         rCurve = new Rarefact(lookup, rDisplays);
278                                         rCurve->getSharedCurve(freq, nIters);
279                                         delete rCurve;
280
281                                         processedLabels.insert(lookup[0]->getLabel());
282                                         userLabels.erase(lookup[0]->getLabel());
283                                         
284                                         //restore real lastlabel to save below
285                                         lookup[0]->setLabel(saveLabel);
286                         }
287                                 
288                         
289                         lastLabel = lookup[0]->getLabel();
290                         
291                         //get next line to process
292                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
293                         lookup = input->getSharedRAbundVectors();
294                 }
295                 
296                 if (m->control_pressed) { 
297                                 globaldata->Groups.clear(); 
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                 //output error messages about any remaining user labels
304                 set<string>::iterator it;
305                 bool needToRun = false;
306                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
307                         m->mothurOut("Your file does not include the label " + *it); 
308                         if (processedLabels.count(lastLabel) != 1) {
309                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
310                                 needToRun = true;
311                         }else {
312                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
313                         }
314                 }
315                 
316                 if (m->control_pressed) { 
317                                 globaldata->Groups.clear(); 
318                                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }
319                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
320                                 return 0;
321                 }
322                 
323                 //run last label if you need to
324                 if (needToRun == true)  {
325                         for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {     delete lookup[i]; }  } 
326                         lookup = input->getSharedRAbundVectors(lastLabel);
327
328                         m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
329                         rCurve = new Rarefact(lookup, rDisplays);
330                         rCurve->getSharedCurve(freq, nIters);
331                         delete rCurve;
332                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
333                 }
334                 
335                 for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }       
336                 
337                 //reset groups parameter
338                 globaldata->Groups.clear();  
339                 
340                 if (m->control_pressed) { 
341                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
342                                 return 0;
343                 }
344                 
345                 m->mothurOutEndLine();
346                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
347                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
348                 m->mothurOutEndLine();
349
350                 return 0;
351         }
352         catch(exception& e) {
353                 m->errorOut(e, "RareFactSharedCommand", "execute");
354                 exit(1);
355         }
356 }
357
358
359 //**********************************************************************************************************************