]> git.donarmstrong.com Git - mothur.git/blob - collectcommand.cpp
8c1565b901d5b885ad86b91d31cb7cc041138535
[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("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");
114                 m->mothurOut("collect.single(label=yourLabel, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n");
115                 m->mothurOut("Example collect(label=unique-.01-.03, iters=10000, freq=10, calc=sobs-chao-ace-jack).\n");
116                 m->mothurOut("The default values for freq is 100, and calc are sobs-chao-ace-jack-shannon-npshannon-simpson.\n");
117                 validCalculator->printCalc("single", cout);
118                 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
119                 m->mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
120         }
121         catch(exception& e) {
122                 m->errorOut(e, "CollectCommand", "help");
123                 exit(1);
124         }
125 }
126
127 //**********************************************************************************************************************
128
129 CollectCommand::~CollectCommand(){}
130
131 //**********************************************************************************************************************
132
133 int CollectCommand::execute(){
134         try {
135                 
136                 if (abort == true) { return 0; }
137                 
138                 vector<string> outputNames;
139                 
140                 string hadShared = "";
141                 if ((globaldata->getFormat() != "sharedfile")) { inputFileNames.push_back(globaldata->inputFileName);  }
142                 else { hadShared = globaldata->getSharedFile(); inputFileNames = parseSharedFile(globaldata->getSharedFile());  globaldata->setFormat("rabund");  }
143         
144                 for (int p = 0; p < inputFileNames.size(); p++) {
145                         
146                         if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str());         }  globaldata->Groups.clear(); if (hadShared != "") {  globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile");  } return 0; }
147                         
148                         if (outputDir == "") { outputDir += hasPath(inputFileNames[p]); }
149                         string fileNameRoot = outputDir + getRootName(getSimpleName(inputFileNames[p]));
150                         globaldata->inputFileName = inputFileNames[p];
151                 
152                         if (inputFileNames.size() > 1) {
153                                 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
154                         }
155                 
156                         validCalculator = new ValidCalculators();
157                         
158                         for (int i=0; i<Estimators.size(); i++) {
159                                 if (validCalculator->isValidCalculator("single", Estimators[i]) == true) { 
160                                         if (Estimators[i] == "sobs") { 
161                                                 cDisplays.push_back(new CollectDisplay(new Sobs(), new OneColumnFile(fileNameRoot+"sobs")));
162                                                 outputNames.push_back(fileNameRoot+"sobs");
163                                         }else if (Estimators[i] == "chao") { 
164                                                 cDisplays.push_back(new CollectDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"chao")));
165                                                 outputNames.push_back(fileNameRoot+"chao");
166                                         }else if (Estimators[i] == "nseqs") { 
167                                                 cDisplays.push_back(new CollectDisplay(new NSeqs(), new OneColumnFile(fileNameRoot+"nseqs")));
168                                                 outputNames.push_back(fileNameRoot+"nseqs");
169                                         }else if (Estimators[i] == "coverage") { 
170                                                 cDisplays.push_back(new CollectDisplay(new Coverage(), new OneColumnFile(fileNameRoot+"coverage")));
171                                                 outputNames.push_back(fileNameRoot+"coverage");
172                                         }else if (Estimators[i] == "ace") { 
173                                                 cDisplays.push_back(new CollectDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"ace")));
174                                                 outputNames.push_back(fileNameRoot+"ace");
175                                         }else if (Estimators[i] == "jack") { 
176                                                 cDisplays.push_back(new CollectDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"jack")));
177                                                 outputNames.push_back(fileNameRoot+"jack");
178                                         }else if (Estimators[i] == "shannon") { 
179                                                 cDisplays.push_back(new CollectDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"shannon")));
180                                                 outputNames.push_back(fileNameRoot+"shannon");
181                                         }else if (Estimators[i] == "npshannon") { 
182                                                 cDisplays.push_back(new CollectDisplay(new NPShannon(), new OneColumnFile(fileNameRoot+"np_shannon")));
183                                                 outputNames.push_back(fileNameRoot+"np_shannon");
184                                         }else if (Estimators[i] == "simpson") { 
185                                                 cDisplays.push_back(new CollectDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"simpson")));
186                                                 outputNames.push_back(fileNameRoot+"simpson");
187                                         }else if (Estimators[i] == "bootstrap") { 
188                                                 cDisplays.push_back(new CollectDisplay(new Bootstrap(), new OneColumnFile(fileNameRoot+"bootstrap")));
189                                                 outputNames.push_back(fileNameRoot+"bootstrap");
190                                         }else if (Estimators[i] == "geometric") { 
191                                                 cDisplays.push_back(new CollectDisplay(new Geom(), new OneColumnFile(fileNameRoot+"geometric")));
192                                                 outputNames.push_back(fileNameRoot+"geometric");
193                                         }else if (Estimators[i] == "qstat") { 
194                                                 cDisplays.push_back(new CollectDisplay(new QStat(), new OneColumnFile(fileNameRoot+"qstat")));
195                                                 outputNames.push_back(fileNameRoot+"qstat");
196                                         }else if (Estimators[i] == "logseries") { 
197                                                 cDisplays.push_back(new CollectDisplay(new LogSD(), new OneColumnFile(fileNameRoot+"logseries")));
198                                                 outputNames.push_back(fileNameRoot+"logseries");
199                                         }else if (Estimators[i] == "bergerparker") { 
200                                                 cDisplays.push_back(new CollectDisplay(new BergerParker(), new OneColumnFile(fileNameRoot+"bergerparker")));
201                                                 outputNames.push_back(fileNameRoot+"bergerparker");
202                                         }else if (Estimators[i] == "bstick") { 
203                                                 cDisplays.push_back(new CollectDisplay(new BStick(), new ThreeColumnFile(fileNameRoot+"bstick")));
204                                                 outputNames.push_back(fileNameRoot+"bstick");
205                                         }else if (Estimators[i] == "goodscoverage") { 
206                                                 cDisplays.push_back(new CollectDisplay(new GoodsCoverage(), new OneColumnFile(fileNameRoot+"goodscoverage")));
207                                                 outputNames.push_back(fileNameRoot+"goodscoverage");
208                                         }else if (Estimators[i] == "efron") {
209                                                 cDisplays.push_back(new CollectDisplay(new Efron(size), new OneColumnFile(fileNameRoot+"efron")));
210                                                 outputNames.push_back(fileNameRoot+"efron");
211                                         }else if (Estimators[i] == "boneh") {
212                                                 cDisplays.push_back(new CollectDisplay(new Boneh(size), new OneColumnFile(fileNameRoot+"boneh")));
213                                                 outputNames.push_back(fileNameRoot+"boneh");
214                                         }else if (Estimators[i] == "solow") {
215                                                 cDisplays.push_back(new CollectDisplay(new Solow(size), new OneColumnFile(fileNameRoot+"solow")));
216                                                 outputNames.push_back(fileNameRoot+"solow");
217                                         }else if (Estimators[i] == "shen") {
218                                                 cDisplays.push_back(new CollectDisplay(new Shen(size, abund), new OneColumnFile(fileNameRoot+"shen")));
219                                                 outputNames.push_back(fileNameRoot+"shen");
220                                         }
221                                 }
222                         }
223                 
224                         //if the users entered no valid calculators don't execute command
225                         if (cDisplays.size() == 0) { if (hadShared != "") {  globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile");  } return 0; }
226                         
227                         read = new ReadOTUFile(inputFileNames[p]);      
228                         read->read(&*globaldata); 
229                 
230                         order = globaldata->gorder;
231                         string lastLabel = order->getLabel();
232                         input = globaldata->ginput;
233                         
234                         //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
235                         set<string> processedLabels;
236                         set<string> userLabels = labels;
237                         
238                         if (m->control_pressed) {  
239                                 for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
240                                 for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
241                                 delete input;  globaldata->ginput = NULL;
242                                 delete read;
243                                 delete order; globaldata->gorder = NULL;
244                                 delete validCalculator;
245                                 globaldata->Groups.clear();
246                                 if (hadShared != "") {  globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile");  }
247                                 return 0;
248                         }
249
250
251                         while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
252                         
253                                 if (m->control_pressed) { 
254                                         for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
255                                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
256                                         delete input;  globaldata->ginput = NULL;
257                                         delete read;
258                                         delete order; globaldata->gorder = NULL;
259                                         delete validCalculator;
260                                         globaldata->Groups.clear();
261                                         if (hadShared != "") {  globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile");  }
262                                         return 0;
263                                 }
264
265                                 
266                                 if(allLines == 1 || labels.count(order->getLabel()) == 1){
267                                 
268                                         m->mothurOut(order->getLabel()); m->mothurOutEndLine();
269                                         cCurve = new Collect(order, cDisplays);
270                                         cCurve->getCurve(freq);
271                                         delete cCurve;
272                                         
273                                         processedLabels.insert(order->getLabel());
274                                         userLabels.erase(order->getLabel());
275                                         
276                                         
277                                 }
278                                 //you have a label the user want that is smaller than this label and the last label has not already been processed 
279                                 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
280                                         string saveLabel = order->getLabel();
281                                         
282                                         delete order;
283                                         order = (input->getOrderVector(lastLabel));
284                                         
285                                         m->mothurOut(order->getLabel()); m->mothurOutEndLine();
286                                         cCurve = new Collect(order, cDisplays);
287                                         cCurve->getCurve(freq);
288                                         delete cCurve;
289                                         
290                                         
291                                         processedLabels.insert(order->getLabel());
292                                         userLabels.erase(order->getLabel());
293                                         
294                                         //restore real lastlabel to save below
295                                         order->setLabel(saveLabel);
296                                 }
297                                 
298                                 lastLabel = order->getLabel();  
299                                 
300                                 delete order;           
301                                 order = (input->getOrderVector());
302                         }
303                         
304                         
305                         if (m->control_pressed) { 
306                                         for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
307                                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
308                                         delete input;  globaldata->ginput = NULL;
309                                         delete read;
310                                         delete validCalculator;
311                                         globaldata->Groups.clear();
312                                         if (hadShared != "") {  globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile");  }
313                                         return 0;
314                         }
315                                 
316                         //output error messages about any remaining user labels
317                         set<string>::iterator it;
318                         bool needToRun = false;
319                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
320                                 m->mothurOut("Your file does not include the label " + *it); 
321                                 if (processedLabels.count(lastLabel) != 1) {
322                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
323                                         needToRun = true;
324                                 }else {
325                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
326                                 }
327                         }
328                         
329                         //run last label if you need to
330                         if (needToRun == true)  {
331                                 if (order != NULL) {    delete order;   }
332                                 order = (input->getOrderVector(lastLabel));
333                                 
334                                 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
335                                 
336                                 cCurve = new Collect(order, cDisplays);
337                                 cCurve->getCurve(freq);
338                                 delete cCurve;
339                                 
340                                 if (m->control_pressed) { 
341                                         for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
342                                         for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }
343                                         delete input;  globaldata->ginput = NULL;
344                                         delete read;
345                                         delete order; globaldata->gorder = NULL;
346                                         delete validCalculator;
347                                         globaldata->Groups.clear();
348                                         if (hadShared != "") {  globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile");  }
349                                         return 0;
350                                 }
351                                 delete order;
352                         }
353                         
354                         for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
355                         cDisplays.clear();
356                         delete input;  globaldata->ginput = NULL;
357                         delete read;
358                         globaldata->gorder = NULL;
359                         delete validCalculator;
360                 }
361                 
362                 //return to shared mode if you changed above
363                 if (hadShared != "") {  globaldata->setSharedFile(hadShared); globaldata->setFormat("sharedfile");  }
364
365                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str());         } return 0; }
366                                 
367                 m->mothurOutEndLine();
368                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
369                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
370                 m->mothurOutEndLine();
371
372                 
373                 return 0;
374         }
375         catch(exception& e) {
376                 m->errorOut(e, "CollectCommand", "execute");
377                 exit(1);
378         }
379 }
380
381 //**********************************************************************************************************************
382 vector<string> CollectCommand::parseSharedFile(string filename) {
383         try {
384                 vector<string> filenames;
385                 
386                 map<string, ofstream*> filehandles;
387                 map<string, ofstream*>::iterator it3;
388                 
389                                 
390                 //read first line
391                 read = new ReadOTUFile(filename);       
392                 read->read(&*globaldata); 
393                         
394                 input = globaldata->ginput;
395                 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
396                 
397                 string sharedFileRoot = getRootName(filename);
398                 
399                 //clears file before we start to write to it below
400                 for (int i=0; i<lookup.size(); i++) {
401                         remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
402                         filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
403                 }
404                 
405                 ofstream* temp;
406                 for (int i=0; i<lookup.size(); i++) {
407                         temp = new ofstream;
408                         filehandles[lookup[i]->getGroup()] = temp;
409                         groups.push_back(lookup[i]->getGroup());
410                 }
411
412                 while(lookup[0] != NULL) {
413                 
414                         for (int i = 0; i < lookup.size(); i++) {
415                                 RAbundVector rav = lookup[i]->getRAbundVector();
416                                 openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
417                                 rav.print(*(filehandles[lookup[i]->getGroup()]));
418                                 (*(filehandles[lookup[i]->getGroup()])).close();
419                         }
420                 
421                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
422                         lookup = input->getSharedRAbundVectors();
423                 }
424                 
425                 //free memory
426                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
427                         delete it3->second;
428                 }
429                 delete read;
430                 delete input;
431                 globaldata->ginput = NULL;
432
433                 return filenames;
434         }
435         catch(exception& e) {
436                 m->errorOut(e, "CollectCommand", "parseSharedFile");
437                 exit(1);
438         }
439 }
440 //**********************************************************************************************************************
441