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