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