]> git.donarmstrong.com Git - mothur.git/blob - collectcommand.cpp
fixed some bugs found while testing 1.8
[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() == "")) { mothurOut("You must read a list, sabund, rabund or shared file before you can use the collect.single command."); 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                 errorOut(e, "CollectCommand", "CollectCommand");
102                 exit(1);
103         }                       
104 }
105 //**********************************************************************************************************************
106
107 void CollectCommand::help(){
108         try {
109                 mothurOut("The collect.single command can only be executed after a successful read.otu command. WITH ONE EXECEPTION. \n");
110                 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                 mothurOut("The collect.single command parameters are label, freq, calc and abund.  No parameters are required. \n");
112                 mothurOut("The collect.single command should be in the following format: \n");
113                 mothurOut("collect.single(label=yourLabel, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n");
114                 mothurOut("Example collect(label=unique-.01-.03, iters=10000, freq=10, calc=sobs-chao-ace-jack).\n");
115                 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                 mothurOut("The label parameter is used to analyze specific labels in your input.\n");
118                 mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
119         }
120         catch(exception& e) {
121                 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                 if ((globaldata->getFormat() != "sharedfile")) { inputFileNames.push_back(globaldata->inputFileName);  }
138                 else {  inputFileNames = parseSharedFile(globaldata->getSharedFile());  globaldata->setFormat("rabund");  }
139         
140                 for (int p = 0; p < inputFileNames.size(); p++) {
141                 
142                         if (outputDir == "") { outputDir += hasPath(inputFileNames[p]); }
143                         string fileNameRoot = outputDir + getRootName(getSimpleName(inputFileNames[p]));
144                         globaldata->inputFileName = inputFileNames[p];
145                 
146                         if (inputFileNames.size() > 1) {
147                                 mothurOutEndLine(); mothurOut("Processing group " + groups[p]); mothurOutEndLine(); mothurOutEndLine();
148                         }
149                 
150                         validCalculator = new ValidCalculators();
151                         
152                         for (int i=0; i<Estimators.size(); i++) {
153                                 if (validCalculator->isValidCalculator("single", Estimators[i]) == true) { 
154                                         if (Estimators[i] == "sobs") { 
155                                                 cDisplays.push_back(new CollectDisplay(new Sobs(), new OneColumnFile(fileNameRoot+"sobs")));
156                                         }else if (Estimators[i] == "chao") { 
157                                                 cDisplays.push_back(new CollectDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"chao")));
158                                         }else if (Estimators[i] == "nseqs") { 
159                                                 cDisplays.push_back(new CollectDisplay(new NSeqs(), new OneColumnFile(fileNameRoot+"nseqs")));
160                                         }else if (Estimators[i] == "coverage") { 
161                                                 cDisplays.push_back(new CollectDisplay(new Coverage(), new OneColumnFile(fileNameRoot+"coverage")));
162                                         }else if (Estimators[i] == "ace") { 
163                                                 cDisplays.push_back(new CollectDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"ace")));
164                                         }else if (Estimators[i] == "jack") { 
165                                                 cDisplays.push_back(new CollectDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"jack")));
166                                         }else if (Estimators[i] == "shannon") { 
167                                                 cDisplays.push_back(new CollectDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"shannon")));
168                                         }else if (Estimators[i] == "npshannon") { 
169                                                 cDisplays.push_back(new CollectDisplay(new NPShannon(), new OneColumnFile(fileNameRoot+"np_shannon")));
170                                         }else if (Estimators[i] == "simpson") { 
171                                                 cDisplays.push_back(new CollectDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"simpson")));
172                                         }else if (Estimators[i] == "bootstrap") { 
173                                                 cDisplays.push_back(new CollectDisplay(new Bootstrap(), new OneColumnFile(fileNameRoot+"bootstrap")));
174                                         }else if (Estimators[i] == "geometric") { 
175                                                 cDisplays.push_back(new CollectDisplay(new Geom(), new OneColumnFile(fileNameRoot+"geometric")));
176                                         }else if (Estimators[i] == "qstat") { 
177                                                 cDisplays.push_back(new CollectDisplay(new QStat(), new OneColumnFile(fileNameRoot+"qstat")));
178                                         }else if (Estimators[i] == "logseries") { 
179                                                 cDisplays.push_back(new CollectDisplay(new LogSD(), new OneColumnFile(fileNameRoot+"logseries")));
180                                         }else if (Estimators[i] == "bergerparker") { 
181                                                 cDisplays.push_back(new CollectDisplay(new BergerParker(), new OneColumnFile(fileNameRoot+"bergerparker")));
182                                         }else if (Estimators[i] == "bstick") { 
183                                                 cDisplays.push_back(new CollectDisplay(new BStick(), new ThreeColumnFile(fileNameRoot+"bstick")));
184                                         }else if (Estimators[i] == "goodscoverage") { 
185                                                 cDisplays.push_back(new CollectDisplay(new GoodsCoverage(), new OneColumnFile(fileNameRoot+"goodscoverage")));
186                                         }else if (Estimators[i] == "efron") {
187                                                 cDisplays.push_back(new CollectDisplay(new Efron(size), new OneColumnFile(fileNameRoot+"efron")));
188                                         }else if (Estimators[i] == "boneh") {
189                                                 cDisplays.push_back(new CollectDisplay(new Boneh(size), new OneColumnFile(fileNameRoot+"boneh")));
190                                         }else if (Estimators[i] == "solow") {
191                                                 cDisplays.push_back(new CollectDisplay(new Solow(size), new OneColumnFile(fileNameRoot+"solow")));
192                                         }else if (Estimators[i] == "shen") {
193                                                 cDisplays.push_back(new CollectDisplay(new Shen(size, abund), new OneColumnFile(fileNameRoot+"shen")));
194                                         }
195                                 }
196                         }
197                 
198                         //if the users entered no valid calculators don't execute command
199                         if (cDisplays.size() == 0) { return 0; }
200                         
201                         read = new ReadOTUFile(inputFileNames[p]);      
202                         read->read(&*globaldata); 
203                 
204                         order = globaldata->gorder;
205                         string lastLabel = order->getLabel();
206                         input = globaldata->ginput;
207                         
208                         //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
209                         set<string> processedLabels;
210                         set<string> userLabels = labels;
211
212                         while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
213                                 
214                                 if(allLines == 1 || labels.count(order->getLabel()) == 1){
215                                         
216                                         cCurve = new Collect(order, cDisplays);
217                                         cCurve->getCurve(freq);
218                                         delete cCurve;
219                                         
220                                         mothurOut(order->getLabel()); mothurOutEndLine();
221                                         processedLabels.insert(order->getLabel());
222                                         userLabels.erase(order->getLabel());
223                                         
224                                         
225                                 }
226                                 //you have a label the user want that is smaller than this label and the last label has not already been processed 
227                                 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
228                                         string saveLabel = order->getLabel();
229                                         
230                                         delete order;
231                                         order = (input->getOrderVector(lastLabel));
232                                         
233                                         cCurve = new Collect(order, cDisplays);
234                                         cCurve->getCurve(freq);
235                                         delete cCurve;
236                                         
237                                         mothurOut(order->getLabel()); mothurOutEndLine();
238                                         processedLabels.insert(order->getLabel());
239                                         userLabels.erase(order->getLabel());
240                                         
241                                         //restore real lastlabel to save below
242                                         order->setLabel(saveLabel);
243                                 }
244                                 
245                                 lastLabel = order->getLabel();  
246                                 
247                                 delete order;           
248                                 order = (input->getOrderVector());
249                         }
250                         
251                         //output error messages about any remaining user labels
252                         set<string>::iterator it;
253                         bool needToRun = false;
254                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
255                                 mothurOut("Your file does not include the label " + *it); 
256                                 if (processedLabels.count(lastLabel) != 1) {
257                                         mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
258                                         needToRun = true;
259                                 }else {
260                                         mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
261                                 }
262                         }
263                         
264                         //run last label if you need to
265                         if (needToRun == true)  {
266                                 if (order != NULL) {    delete order;   }
267                                 order = (input->getOrderVector(lastLabel));
268                                 
269                                 mothurOut(order->getLabel()); mothurOutEndLine();
270                                 
271                                 cCurve = new Collect(order, cDisplays);
272                                 cCurve->getCurve(freq);
273                                 delete cCurve;
274                                 delete order;
275                         }
276                         
277                         for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
278                         cDisplays.clear();
279                         delete input;  globaldata->ginput = NULL;
280                         delete read;
281                         globaldata->gorder = NULL;
282                         delete validCalculator;
283                 }
284                 
285                 
286                 
287                 return 0;
288         }
289         catch(exception& e) {
290                 errorOut(e, "CollectCommand", "execute");
291                 exit(1);
292         }
293 }
294
295 //**********************************************************************************************************************
296 vector<string> CollectCommand::parseSharedFile(string filename) {
297         try {
298                 vector<string> filenames;
299                 
300                 map<string, ofstream*> filehandles;
301                 map<string, ofstream*>::iterator it3;
302                 
303                                 
304                 //read first line
305                 read = new ReadOTUFile(filename);       
306                 read->read(&*globaldata); 
307                         
308                 input = globaldata->ginput;
309                 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
310                 
311                 string sharedFileRoot = getRootName(filename);
312                 
313                 //clears file before we start to write to it below
314                 for (int i=0; i<lookup.size(); i++) {
315                         remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
316                         filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
317                 }
318                 
319                 ofstream* temp;
320                 for (int i=0; i<lookup.size(); i++) {
321                         temp = new ofstream;
322                         filehandles[lookup[i]->getGroup()] = temp;
323                         groups.push_back(lookup[i]->getGroup());
324                 }
325
326                 while(lookup[0] != NULL) {
327                 
328                         for (int i = 0; i < lookup.size(); i++) {
329                                 RAbundVector rav = lookup[i]->getRAbundVector();
330                                 openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
331                                 rav.print(*(filehandles[lookup[i]->getGroup()]));
332                                 (*(filehandles[lookup[i]->getGroup()])).close();
333                         }
334                 
335                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
336                         lookup = input->getSharedRAbundVectors();
337                 }
338                 
339                 //free memory
340                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
341                         delete it3->second;
342                 }
343                 delete read;
344                 delete input;
345                 globaldata->ginput = NULL;
346
347                 return filenames;
348         }
349         catch(exception& e) {
350                 errorOut(e, "CollectCommand", "parseSharedFile");
351                 exit(1);
352         }
353 }
354 //**********************************************************************************************************************
355