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