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