]> git.donarmstrong.com Git - mothur.git/blob - collectcommand.cpp
adding mothurout.h and .cpp to repo
[mothur.git] / collectcommand.cpp
1 /*
2  *  collectcommand.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/2/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
10 #include "collectcommand.h"
11 #include "ace.h"
12 #include "sobs.h"
13 #include "nseqs.h"
14 #include "chao1.h"
15 #include "bootstrap.h"
16 #include "simpson.h"
17 #include "npshannon.h"
18 #include "shannon.h"
19 #include "jackknife.h"
20 #include "geom.h"
21 #include "qstat.h"
22 #include "logsd.h"
23 #include "bergerparker.h"
24 #include "bstick.h"
25 #include "goodscoverage.h"
26 #include "efron.h"
27 #include "boneh.h"
28 #include "solow.h"
29 #include "shen.h"
30 #include "coverage.h"
31
32
33 //**********************************************************************************************************************
34 CollectCommand::CollectCommand(string option)  {
35         try {
36                 globaldata = GlobalData::getInstance();
37                 abort = false;
38                 allLines = 1;
39                 labels.clear();
40                 Estimators.clear();
41                 
42                 //allow user to run help
43                 if(option == "help") { validCalculator = new ValidCalculators(); help(); delete validCalculator; abort = true; }
44                 
45                 else {
46                         //valid paramters for this command
47                         string Array[] =  {"freq","label","calc","abund","size","outputdir","inputdir"};
48                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
49                         
50                         OptionParser parser(option);
51                         map<string,string> parameters = parser.getParameters();
52                         
53                         ValidParameters validParameter;
54                 
55                         //check to make sure all parameters are valid for command
56                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
57                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
58                         }
59                         
60                         //if the user changes the output directory command factory will send this info to us in the output parameter 
61                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
62                         
63                         //make sure the user has already run the read.otu command
64                         if ((globaldata->getSharedFile() == "") && (globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "")) { m->mothurOut("You must read a list, sabund, rabund or shared file before you can use the collect.single command."); m->mothurOutEndLine(); abort = true; }
65                         
66                         //check for optional parameter and set defaults
67                         // ...at some point should added some additional type checking...
68                         label = validParameter.validFile(parameters, "label", false);                   
69                         if (label == "not found") { label = ""; }
70                         else { 
71                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
72                                 else { allLines = 1;  }
73                         }
74                         
75                         //if the user has not specified any labels use the ones from read.otu
76                         if (label == "") {  
77                                 allLines = globaldata->allLines; 
78                                 labels = globaldata->labels; 
79                         }
80                                 
81                         calc = validParameter.validFile(parameters, "calc", false);                     
82                         if (calc == "not found") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson";  }
83                         else { 
84                                  if (calc == "default")  {  calc = "sobs-chao-ace-jack-shannon-npshannon-simpson";  }
85                         }
86                         splitAtDash(calc, Estimators);
87
88                         string temp;
89                         temp = validParameter.validFile(parameters, "freq", false);                     if (temp == "not found") { temp = "100"; }
90                         convert(temp, freq); 
91                         
92                         temp = validParameter.validFile(parameters, "abund", false);            if (temp == "not found") { temp = "10"; }
93                         convert(temp, abund); 
94                         
95                         temp = validParameter.validFile(parameters, "size", false);                     if (temp == "not found") { temp = "0"; }
96                         convert(temp, size); 
97                 }
98                 
99         }
100         catch(exception& e) {
101                 m->errorOut(e, "CollectCommand", "CollectCommand");
102                 exit(1);
103         }                       
104 }
105 //**********************************************************************************************************************
106
107 void CollectCommand::help(){
108         try {
109                 m->mothurOut("The collect.single command can only be executed after a successful read.otu command. WITH ONE EXECEPTION. \n");
110                 m->mothurOut("The collect.single command can be executed after a successful cluster command.  It will use the .list file from the output of the cluster.\n");
111                 m->mothurOut("The collect.single command parameters are label, freq, calc and abund.  No parameters are required. \n");
112                 m->mothurOut("The collect.single command should be in the following format: \n");
113                 m->mothurOut("collect.single(label=yourLabel, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n");
114                 m->mothurOut("Example collect(label=unique-.01-.03, iters=10000, freq=10, calc=sobs-chao-ace-jack).\n");
115                 m->mothurOut("The default values for freq is 100, and calc are sobs-chao-ace-jack-shannon-npshannon-simpson.\n");
116                 validCalculator->printCalc("single", cout);
117                 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
118                 m->mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
119         }
120         catch(exception& e) {
121                 m->errorOut(e, "CollectCommand", "help");
122                 exit(1);
123         }
124 }
125
126 //**********************************************************************************************************************
127
128 CollectCommand::~CollectCommand(){}
129
130 //**********************************************************************************************************************
131
132 int CollectCommand::execute(){
133         try {
134                 
135                 if (abort == true) { return 0; }
136                 
137                 vector<string> outputNames;
138                 
139                 if ((globaldata->getFormat() != "sharedfile")) { inputFileNames.push_back(globaldata->inputFileName);  }
140                 else {  inputFileNames = parseSharedFile(globaldata->getSharedFile());  globaldata->setFormat("rabund");  }
141         
142                 for (int p = 0; p < inputFileNames.size(); p++) {
143                         
144                         if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str());         }  globaldata->Groups.clear(); return 0; }
145                         
146                         if (outputDir == "") { outputDir += hasPath(inputFileNames[p]); }
147                         string fileNameRoot = outputDir + getRootName(getSimpleName(inputFileNames[p]));
148                         globaldata->inputFileName = inputFileNames[p];
149                 
150                         if (inputFileNames.size() > 1) {
151                                 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
152                         }
153                 
154                         validCalculator = new ValidCalculators();
155                         
156                         for (int i=0; i<Estimators.size(); i++) {
157                                 if (validCalculator->isValidCalculator("single", Estimators[i]) == true) { 
158                                         if (Estimators[i] == "sobs") { 
159                                                 cDisplays.push_back(new CollectDisplay(new Sobs(), new OneColumnFile(fileNameRoot+"sobs")));
160                                                 outputNames.push_back(fileNameRoot+"sobs");
161                                         }else if (Estimators[i] == "chao") { 
162                                                 cDisplays.push_back(new CollectDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"chao")));
163                                                 outputNames.push_back(fileNameRoot+"chao");
164                                         }else if (Estimators[i] == "nseqs") { 
165                                                 cDisplays.push_back(new CollectDisplay(new NSeqs(), new OneColumnFile(fileNameRoot+"nseqs")));
166                                                 outputNames.push_back(fileNameRoot+"nseqs");
167                                         }else if (Estimators[i] == "coverage") { 
168                                                 cDisplays.push_back(new CollectDisplay(new Coverage(), new OneColumnFile(fileNameRoot+"coverage")));
169                                                 outputNames.push_back(fileNameRoot+"coverage");
170                                         }else if (Estimators[i] == "ace") { 
171                                                 cDisplays.push_back(new CollectDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"ace")));
172                                                 outputNames.push_back(fileNameRoot+"ace");
173                                         }else if (Estimators[i] == "jack") { 
174                                                 cDisplays.push_back(new CollectDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"jack")));
175                                                 outputNames.push_back(fileNameRoot+"jack");
176                                         }else if (Estimators[i] == "shannon") { 
177                                                 cDisplays.push_back(new CollectDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"shannon")));
178                                                 outputNames.push_back(fileNameRoot+"shannon");
179                                         }else if (Estimators[i] == "npshannon") { 
180                                                 cDisplays.push_back(new CollectDisplay(new NPShannon(), new OneColumnFile(fileNameRoot+"np_shannon")));
181                                                 outputNames.push_back(fileNameRoot+"np_shannon");
182                                         }else if (Estimators[i] == "simpson") { 
183                                                 cDisplays.push_back(new CollectDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"simpson")));
184                                                 outputNames.push_back(fileNameRoot+"simpson");
185                                         }else if (Estimators[i] == "bootstrap") { 
186                                                 cDisplays.push_back(new CollectDisplay(new Bootstrap(), new OneColumnFile(fileNameRoot+"bootstrap")));
187                                                 outputNames.push_back(fileNameRoot+"bootstrap");
188                                         }else if (Estimators[i] == "geometric") { 
189                                                 cDisplays.push_back(new CollectDisplay(new Geom(), new OneColumnFile(fileNameRoot+"geometric")));
190                                                 outputNames.push_back(fileNameRoot+"geometric");
191                                         }else if (Estimators[i] == "qstat") { 
192                                                 cDisplays.push_back(new CollectDisplay(new QStat(), new OneColumnFile(fileNameRoot+"qstat")));
193                                                 outputNames.push_back(fileNameRoot+"qstat");
194                                         }else if (Estimators[i] == "logseries") { 
195                                                 cDisplays.push_back(new CollectDisplay(new LogSD(), new OneColumnFile(fileNameRoot+"logseries")));
196                                                 outputNames.push_back(fileNameRoot+"logseries");
197                                         }else if (Estimators[i] == "bergerparker") { 
198                                                 cDisplays.push_back(new CollectDisplay(new BergerParker(), new OneColumnFile(fileNameRoot+"bergerparker")));
199                                                 outputNames.push_back(fileNameRoot+"bergerparker");
200                                         }else if (Estimators[i] == "bstick") { 
201                                                 cDisplays.push_back(new CollectDisplay(new BStick(), new ThreeColumnFile(fileNameRoot+"bstick")));
202                                                 outputNames.push_back(fileNameRoot+"bstick");
203                                         }else if (Estimators[i] == "goodscoverage") { 
204                                                 cDisplays.push_back(new CollectDisplay(new GoodsCoverage(), new OneColumnFile(fileNameRoot+"goodscoverage")));
205                                                 outputNames.push_back(fileNameRoot+"goodscoverage");
206                                         }else if (Estimators[i] == "efron") {
207                                                 cDisplays.push_back(new CollectDisplay(new Efron(size), new OneColumnFile(fileNameRoot+"efron")));
208                                                 outputNames.push_back(fileNameRoot+"efron");
209                                         }else if (Estimators[i] == "boneh") {
210                                                 cDisplays.push_back(new CollectDisplay(new Boneh(size), new OneColumnFile(fileNameRoot+"boneh")));
211                                                 outputNames.push_back(fileNameRoot+"boneh");
212                                         }else if (Estimators[i] == "solow") {
213                                                 cDisplays.push_back(new CollectDisplay(new Solow(size), new OneColumnFile(fileNameRoot+"solow")));
214                                                 outputNames.push_back(fileNameRoot+"solow");
215                                         }else if (Estimators[i] == "shen") {
216                                                 cDisplays.push_back(new CollectDisplay(new Shen(size, abund), new OneColumnFile(fileNameRoot+"shen")));
217                                                 outputNames.push_back(fileNameRoot+"shen");
218                                         }
219                                 }
220                         }
221                 
222                         //if the users entered no valid calculators don't execute command
223                         if (cDisplays.size() == 0) { return 0; }
224                         
225                         read = new ReadOTUFile(inputFileNames[p]);      
226                         read->read(&*globaldata); 
227                 
228                         order = globaldata->gorder;
229                         string lastLabel = order->getLabel();
230                         input = globaldata->ginput;
231                         
232                         //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
233                         set<string> processedLabels;
234                         set<string> userLabels = labels;
235                         
236                         if (m->control_pressed) {  
237                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }  
238                                 for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
239                                 delete input;  globaldata->ginput = NULL;
240                                 delete read;
241                                 delete order; globaldata->gorder = NULL;
242                                 delete validCalculator;
243                                 globaldata->Groups.clear();
244                                 return 0;
245                         }
246
247
248                         while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
249                                 
250                                 if(allLines == 1 || labels.count(order->getLabel()) == 1){
251                                         
252                                         cCurve = new Collect(order, cDisplays);
253                                         int error = cCurve->getCurve(freq);
254                                         delete cCurve;
255                                         
256                                         if (error == 1) {
257                                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }  
258                                                 for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
259                                                 delete input;  globaldata->ginput = NULL;
260                                                 delete read;
261                                                 delete order; globaldata->gorder = NULL;
262                                                 delete validCalculator;
263                                                 globaldata->Groups.clear();
264                                                 return 0;
265                                         }
266                                         
267                                         m->mothurOut(order->getLabel()); m->mothurOutEndLine();
268                                         processedLabels.insert(order->getLabel());
269                                         userLabels.erase(order->getLabel());
270                                         
271                                         
272                                 }
273                                 //you have a label the user want that is smaller than this label and the last label has not already been processed 
274                                 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
275                                         string saveLabel = order->getLabel();
276                                         
277                                         delete order;
278                                         order = (input->getOrderVector(lastLabel));
279                                         
280                                         cCurve = new Collect(order, cDisplays);
281                                         int error = cCurve->getCurve(freq);
282                                         delete cCurve;
283                                         
284                                         if (error == 1) {
285                                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }  
286                                                 for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
287                                                 delete input;  globaldata->ginput = NULL;
288                                                 delete read;
289                                                 delete order; globaldata->gorder = NULL;
290                                                 delete validCalculator;
291                                                 globaldata->Groups.clear();
292                                                 return 0;
293                                         }
294                                         
295                                         m->mothurOut(order->getLabel()); m->mothurOutEndLine();
296                                         processedLabels.insert(order->getLabel());
297                                         userLabels.erase(order->getLabel());
298                                         
299                                         //restore real lastlabel to save below
300                                         order->setLabel(saveLabel);
301                                 }
302                                 
303                                 lastLabel = order->getLabel();  
304                                 
305                                 delete order;           
306                                 order = (input->getOrderVector());
307                         }
308                         
309                         //output error messages about any remaining user labels
310                         set<string>::iterator it;
311                         bool needToRun = false;
312                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
313                                 m->mothurOut("Your file does not include the label " + *it); 
314                                 if (processedLabels.count(lastLabel) != 1) {
315                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
316                                         needToRun = true;
317                                 }else {
318                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
319                                 }
320                         }
321                         
322                         //run last label if you need to
323                         if (needToRun == true)  {
324                                 if (order != NULL) {    delete order;   }
325                                 order = (input->getOrderVector(lastLabel));
326                                 
327                                 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
328                                 
329                                 cCurve = new Collect(order, cDisplays);
330                                 int error = cCurve->getCurve(freq);
331                                 delete cCurve;
332                                 
333                                 if (error == 1) {
334                                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }  
335                                         for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
336                                         delete input;  globaldata->ginput = NULL;
337                                         delete read;
338                                         delete order; globaldata->gorder = NULL;
339                                         delete validCalculator;
340                                         globaldata->Groups.clear();
341                                         return 0;
342                                 }
343                                 delete order;
344                         }
345                         
346                         for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
347                         cDisplays.clear();
348                         delete input;  globaldata->ginput = NULL;
349                         delete read;
350                         globaldata->gorder = NULL;
351                         delete validCalculator;
352                 }
353                 
354                 m->mothurOutEndLine();
355                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
356                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
357                 m->mothurOutEndLine();
358
359                 
360                 return 0;
361         }
362         catch(exception& e) {
363                 m->errorOut(e, "CollectCommand", "execute");
364                 exit(1);
365         }
366 }
367
368 //**********************************************************************************************************************
369 vector<string> CollectCommand::parseSharedFile(string filename) {
370         try {
371                 vector<string> filenames;
372                 
373                 map<string, ofstream*> filehandles;
374                 map<string, ofstream*>::iterator it3;
375                 
376                                 
377                 //read first line
378                 read = new ReadOTUFile(filename);       
379                 read->read(&*globaldata); 
380                         
381                 input = globaldata->ginput;
382                 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
383                 
384                 string sharedFileRoot = getRootName(filename);
385                 
386                 //clears file before we start to write to it below
387                 for (int i=0; i<lookup.size(); i++) {
388                         remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
389                         filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
390                 }
391                 
392                 ofstream* temp;
393                 for (int i=0; i<lookup.size(); i++) {
394                         temp = new ofstream;
395                         filehandles[lookup[i]->getGroup()] = temp;
396                         groups.push_back(lookup[i]->getGroup());
397                 }
398
399                 while(lookup[0] != NULL) {
400                 
401                         for (int i = 0; i < lookup.size(); i++) {
402                                 RAbundVector rav = lookup[i]->getRAbundVector();
403                                 openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
404                                 rav.print(*(filehandles[lookup[i]->getGroup()]));
405                                 (*(filehandles[lookup[i]->getGroup()])).close();
406                         }
407                 
408                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
409                         lookup = input->getSharedRAbundVectors();
410                 }
411                 
412                 //free memory
413                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
414                         delete it3->second;
415                 }
416                 delete read;
417                 delete input;
418                 globaldata->ginput = NULL;
419
420                 return filenames;
421         }
422         catch(exception& e) {
423                 m->errorOut(e, "CollectCommand", "parseSharedFile");
424                 exit(1);
425         }
426 }
427 //**********************************************************************************************************************
428