]> git.donarmstrong.com Git - mothur.git/blob - collectcommand.cpp
changed random forest output filename
[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 vector<string> CollectCommand::setParameters(){ 
40         try {
41                 CommandParameter plist("list", "InputTypes", "", "", "LRSS", "LRSS", "none","",false,false,true); parameters.push_back(plist);
42                 CommandParameter prabund("rabund", "InputTypes", "", "", "LRSS", "LRSS", "none","",false,false,true); parameters.push_back(prabund);
43                 CommandParameter psabund("sabund", "InputTypes", "", "", "LRSS", "LRSS", "none","",false,false,true); parameters.push_back(psabund);
44                 CommandParameter pshared("shared", "InputTypes", "", "", "LRSS", "LRSS", "none","",false,false,true); parameters.push_back(pshared);
45                 CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
46                 CommandParameter pfreq("freq", "Number", "", "100", "", "", "","",false,false); parameters.push_back(pfreq);
47                 CommandParameter pcalc("calc", "Multiple", "sobs-chao-nseqs-coverage-ace-jack-shannon-shannoneven-npshannon-heip-smithwilson-simpson-simpsoneven-invsimpson-bootstrap-geometric-qstat-logseries-bergerparker-bstick-goodscoverage-efron-boneh-solow-shen", "sobs-chao-ace-jack-shannon-npshannon-simpson", "", "", "","",true,false,true); parameters.push_back(pcalc);
48                 CommandParameter pabund("abund", "Number", "", "10", "", "", "","",false,false); parameters.push_back(pabund);
49                 CommandParameter psize("size", "Number", "", "0", "", "", "","",false,false); parameters.push_back(psize);
50                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
51                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
52                 
53                 vector<string> myArray;
54                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
55                 return myArray;
56         }
57         catch(exception& e) {
58                 m->errorOut(e, "CollectCommand", "setParameters");
59                 exit(1);
60         }
61 }
62 //**********************************************************************************************************************
63 string CollectCommand::getHelpString(){ 
64         try {
65                 string helpString = "";
66                 ValidCalculators validCalculator;
67                 helpString += "The collect.single command parameters are list, sabund, rabund, shared, label, freq, calc and abund.  list, sabund, rabund or shared is required unless you have a valid current file. \n";
68                 helpString += "The collect.single command should be in the following format: \n";
69                 helpString += "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";
70                 helpString += "collect.single(label=yourLabel, freq=yourFreq, calc=yourEstimators).\n";
71                 helpString += "Example collect(label=unique-.01-.03, freq=10, calc=sobs-chao-ace-jack).\n";
72                 helpString += "The default values for freq is 100, and calc are sobs-chao-ace-jack-shannon-npshannon-simpson.\n";
73                 helpString += validCalculator.printCalc("single");
74                 helpString += "The label parameter is used to analyze specific labels in your input.\n";
75                 helpString += "Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n";
76                 return helpString;
77         }
78         catch(exception& e) {
79                 m->errorOut(e, "CollectCommand", "getHelpString");
80                 exit(1);
81         }
82 }
83 //**********************************************************************************************************************
84 string CollectCommand::getOutputPattern(string type) {
85     try {
86         string pattern = "";
87         
88         if (type == "sobs")             {  pattern =  "[filename],sobs";            }
89         else if (type == "chao")        {  pattern =  "[filename],chao";            }
90         else if (type == "nseqs")       {  pattern =  "[filename],nseqs";           }
91         else if (type == "coverage")    {  pattern =  "[filename],coverage";        }
92         else if (type == "ace")         {  pattern =  "[filename],ace";             }
93         else if (type == "jack")        {  pattern =  "[filename],jack";            }
94         else if (type == "shannon")     {  pattern =  "[filename],shannon";         }
95         else if (type == "shannoneven") {  pattern =  "[filename],shannoneven";     }
96         else if (type == "npshannon")   {  pattern =  "[filename],npshannon";       }
97         else if (type == "heip")        {  pattern =  "[filename],heip";            }
98         else if (type == "smithwilson") {  pattern =  "[filename],smithwilson";     }
99         else if (type == "simpson")     {  pattern =  "[filename],simpson";         }
100         else if (type == "simpsoneven") {  pattern =  "[filename],simpsoneven";     }
101         else if (type == "invsimpson")  {  pattern =  "[filename],invsimpson";      }
102         else if (type == "bootstrap")   {  pattern =  "[filename],bootstrap";       }
103         else if (type == "geometric")   {  pattern =  "[filename],geometric";       }
104         else if (type == "qstat")       {  pattern =  "[filename],qstat";           }
105         else if (type == "logseries")   {  pattern =  "[filename],logseries";       }
106         else if (type == "bergerparker") {  pattern =  "[filename],bergerparker";   }
107         else if (type == "bstick")      {  pattern =  "[filename],bstick";          }
108         else if (type == "goodscoverage") {  pattern =  "[filename],goodscoverage"; }
109         else if (type == "efron")       {  pattern =  "[filename],efron";           }
110         else if (type == "boneh")       {  pattern =  "[filename],boneh";           }
111         else if (type == "solow")       {  pattern =  "[filename],solow";           }
112         else if (type == "shen")        {  pattern =  "[filename],shen";            }
113         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
114         
115         return pattern;
116     }
117     catch(exception& e) {
118         m->errorOut(e, "CollectCommand", "getOutputPattern");
119         exit(1);
120     }
121 }
122 //**********************************************************************************************************************
123 CollectCommand::CollectCommand(){       
124         try {
125                 abort = true; calledHelp = true; 
126                 setParameters();
127                 vector<string> tempOutNames;
128                 outputTypes["sobs"] = tempOutNames;
129                 outputTypes["chao"] = tempOutNames;
130                 outputTypes["nseqs"] = tempOutNames;
131                 outputTypes["coverage"] = tempOutNames;
132                 outputTypes["ace"] = tempOutNames;
133                 outputTypes["jack"] = tempOutNames;
134                 outputTypes["shannon"] = tempOutNames;
135                 outputTypes["shannoneven"] = tempOutNames;
136                 outputTypes["npshannon"] = tempOutNames;
137                 outputTypes["heip"] = tempOutNames;
138                 outputTypes["smithwilson"] = tempOutNames;
139                 outputTypes["simpson"] = tempOutNames;
140                 outputTypes["simpsoneven"] = tempOutNames;
141                 outputTypes["invsimpson"] = tempOutNames;
142                 outputTypes["bootstrap"] = tempOutNames;
143                 outputTypes["geometric"] = tempOutNames;
144                 outputTypes["qstat"] = tempOutNames;
145                 outputTypes["logseries"] = tempOutNames;
146                 outputTypes["bergerparker"] = tempOutNames;
147                 outputTypes["bstick"] = tempOutNames;
148                 outputTypes["goodscoverage"] = tempOutNames;
149                 outputTypes["efron"] = tempOutNames;
150                 outputTypes["boneh"] = tempOutNames;
151                 outputTypes["solow"] = tempOutNames;
152                 outputTypes["shen"] = tempOutNames;
153         }
154         catch(exception& e) {
155                 m->errorOut(e, "CollectCommand", "CollectCommand");
156                 exit(1);
157         }
158 }
159 //**********************************************************************************************************************
160 CollectCommand::CollectCommand(string option)  {
161         try {
162                 abort = false; calledHelp = false;   
163                 allLines = 1;
164                 
165                 //allow user to run help
166                 if(option == "help") { help(); calledHelp = true; abort = true; }
167                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
168                 
169                 else {
170                         vector<string> myArray = setParameters();
171                         
172                         OptionParser parser(option);
173                         map<string,string> parameters = parser.getParameters();
174                         map<string,string>::iterator it;
175                         
176                         ValidParameters validParameter;
177                 
178                         //check to make sure all parameters are valid for command
179                         for (it = parameters.begin(); it != parameters.end(); it++) { 
180                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
181                         }
182
183                         //initialize outputTypes
184                         vector<string> tempOutNames;
185                         outputTypes["sobs"] = tempOutNames;
186                         outputTypes["chao"] = tempOutNames;
187                         outputTypes["nseqs"] = tempOutNames;
188                         outputTypes["coverage"] = tempOutNames;
189                         outputTypes["ace"] = tempOutNames;
190                         outputTypes["jack"] = tempOutNames;
191                         outputTypes["shannon"] = tempOutNames;
192                         outputTypes["shannoneven"] = tempOutNames;
193                         outputTypes["npshannon"] = tempOutNames;
194                         outputTypes["heip"] = tempOutNames;
195                         outputTypes["smithwilson"] = tempOutNames;
196                         outputTypes["simpson"] = tempOutNames;
197                         outputTypes["simpsoneven"] = tempOutNames;
198                         outputTypes["invsimpson"] = tempOutNames;
199                         outputTypes["bootstrap"] = tempOutNames;
200                         outputTypes["geometric"] = tempOutNames;
201                         outputTypes["qstat"] = tempOutNames;
202                         outputTypes["logseries"] = tempOutNames;
203                         outputTypes["bergerparker"] = tempOutNames;
204                         outputTypes["bstick"] = tempOutNames;
205                         outputTypes["goodscoverage"] = tempOutNames;
206                         outputTypes["efron"] = tempOutNames;
207                         outputTypes["boneh"] = tempOutNames;
208                         outputTypes["solow"] = tempOutNames;
209                         outputTypes["shen"] = tempOutNames;
210                         
211                         //if the user changes the input directory command factory will send this info to us in the output parameter 
212                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
213                         if (inputDir == "not found"){   inputDir = "";          }
214                         else {
215                                 string path;
216                                 it = parameters.find("shared");
217                                 //user has given a template file
218                                 if(it != parameters.end()){ 
219                                         path = m->hasPath(it->second);
220                                         //if the user has not given a path then, add inputdir. else leave path alone.
221                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
222                                 }
223                                 
224                                 it = parameters.find("rabund");
225                                 //user has given a template file
226                                 if(it != parameters.end()){ 
227                                         path = m->hasPath(it->second);
228                                         //if the user has not given a path then, add inputdir. else leave path alone.
229                                         if (path == "") {       parameters["rabund"] = inputDir + it->second;           }
230                                 }
231                                 
232                                 it = parameters.find("sabund");
233                                 //user has given a template file
234                                 if(it != parameters.end()){ 
235                                         path = m->hasPath(it->second);
236                                         //if the user has not given a path then, add inputdir. else leave path alone.
237                                         if (path == "") {       parameters["sabund"] = inputDir + it->second;           }
238                                 }
239                                 
240                                 it = parameters.find("list");
241                                 //user has given a template file
242                                 if(it != parameters.end()){ 
243                                         path = m->hasPath(it->second);
244                                         //if the user has not given a path then, add inputdir. else leave path alone.
245                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
246                                 }
247                         }
248                         
249                         //check for required parameters
250                         listfile = validParameter.validFile(parameters, "list", true);
251                         if (listfile == "not open") { listfile = ""; abort = true; }
252                         else if (listfile == "not found") { listfile = ""; }
253                         else {  format = "list"; inputfile = listfile; m->setListFile(listfile); }
254                         
255                         sabundfile = validParameter.validFile(parameters, "sabund", true);
256                         if (sabundfile == "not open") { sabundfile = ""; abort = true; }        
257                         else if (sabundfile == "not found") { sabundfile = ""; }
258                         else {  format = "sabund"; inputfile = sabundfile; m->setSabundFile(sabundfile); }
259                         
260                         rabundfile = validParameter.validFile(parameters, "rabund", true);
261                         if (rabundfile == "not open") { rabundfile = ""; abort = true; }        
262                         else if (rabundfile == "not found") { rabundfile = ""; }
263                         else {  format = "rabund"; inputfile = rabundfile; m->setRabundFile(rabundfile); }
264                         
265                         sharedfile = validParameter.validFile(parameters, "shared", true);
266                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }        
267                         else if (sharedfile == "not found") { sharedfile = ""; }
268                         else {  format = "sharedfile"; inputfile = sharedfile; m->setSharedFile(sharedfile); }
269                         
270                         
271                         //if the user changes the output directory command factory will send this info to us in the output parameter 
272                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = "";         }
273                         
274                         if ((sharedfile == "") && (listfile == "") && (rabundfile == "") && (sabundfile == "")) { 
275                                 //is there are current file available for any of these?
276                                 //give priority to shared, then list, then rabund, then sabund
277                                 //if there is a current shared file, use it
278                                 sharedfile = m->getSharedFile(); 
279                                 if (sharedfile != "") { inputfile = sharedfile; format = "sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
280                                 else { 
281                                         listfile = m->getListFile(); 
282                                         if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
283                                         else { 
284                                                 rabundfile = m->getRabundFile(); 
285                                                 if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
286                                                 else { 
287                                                         sabundfile = m->getSabundFile(); 
288                                                         if (sabundfile != "") { inputfile = sabundfile; format = "sabund"; m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
289                                                         else { 
290                                                                 m->mothurOut("No valid current files. You must provide a list, sabund, rabund or shared file before you can use the collect.single command."); m->mothurOutEndLine(); 
291                                                                 abort = true;
292                                                         }
293                                                 }
294                                         }
295                                 }
296                         }
297                         
298                         //check for optional parameter and set defaults
299                         // ...at some point should added some additional type checking...
300                         label = validParameter.validFile(parameters, "label", false);                   
301                         if (label == "not found") { label = ""; }
302                         else { 
303                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
304                                 else { allLines = 1;  }
305                         }
306                         
307                         //NOTE: if you add new calc options, don't forget to add them to the parameter initialize in setParameters or the gui won't be able to use them
308                         calc = validParameter.validFile(parameters, "calc", false);                     
309                         if (calc == "not found") { calc = "sobs-chao-ace-jack-shannon-npshannon-simpson";  }
310                         else { 
311                                  if (calc == "default")  {  calc = "sobs-chao-ace-jack-shannon-npshannon-simpson";  }
312                         }
313                         m->splitAtDash(calc, Estimators);
314                         if (m->inUsersGroups("citation", Estimators)) { 
315                                 ValidCalculators validCalc; validCalc.printCitations(Estimators); 
316                                 //remove citation from list of calcs
317                                 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") {  Estimators.erase(Estimators.begin()+i); break; } }
318                         }
319
320                         string temp;
321                         temp = validParameter.validFile(parameters, "freq", false);                     if (temp == "not found") { temp = "100"; }
322                         m->mothurConvert(temp, freq); 
323                         
324                         temp = validParameter.validFile(parameters, "abund", false);            if (temp == "not found") { temp = "10"; }
325                         m->mothurConvert(temp, abund); 
326                         
327                         temp = validParameter.validFile(parameters, "size", false);                     if (temp == "not found") { temp = "0"; }
328                         m->mothurConvert(temp, size); 
329                 }
330                 
331         }
332         catch(exception& e) {
333                 m->errorOut(e, "CollectCommand", "CollectCommand");
334                 exit(1);
335         }                       
336 }
337 //**********************************************************************************************************************
338
339 int CollectCommand::execute(){
340         try {
341                 
342                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
343         
344                 if ((format != "sharedfile")) { inputFileNames.push_back(inputfile);  }
345                 else {  inputFileNames = parseSharedFile(sharedfile);  format = "rabund"; }
346         
347                 for (int p = 0; p < inputFileNames.size(); p++) {
348                         
349                         if (m->control_pressed) {  outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);        }  m->clearGroups();  return 0; }
350                         
351                         if (outputDir == "") { outputDir += m->hasPath(inputFileNames[p]); }
352                         string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p]));
353             map<string, string> variables; 
354             variables["[filename]"] = fileNameRoot;
355                         //globaldata->inputFileName = inputFileNames[p];
356                 
357                         if (inputFileNames.size() > 1) {
358                                 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
359                         }
360                 
361                         ValidCalculators validCalculator;
362                         
363                         for (int i=0; i<Estimators.size(); i++) {
364                                 if (validCalculator.isValidCalculator("single", Estimators[i]) == true) { 
365                                         if (Estimators[i] == "sobs") { 
366                                                 cDisplays.push_back(new CollectDisplay(new Sobs(), new OneColumnFile(getOutputFileName("sobs", variables))));
367                                                 outputNames.push_back(getOutputFileName("sobs", variables)); outputTypes["sobs"].push_back(getOutputFileName("sobs", variables));
368                                         }else if (Estimators[i] == "chao") { 
369                                                 cDisplays.push_back(new CollectDisplay(new Chao1(), new ThreeColumnFile(getOutputFileName("chao", variables))));
370                                                 outputNames.push_back(getOutputFileName("chao", variables)); outputTypes["chao"].push_back(getOutputFileName("chao", variables));
371                                         }else if (Estimators[i] == "nseqs") { 
372                                                 cDisplays.push_back(new CollectDisplay(new NSeqs(), new OneColumnFile(getOutputFileName("nseqs", variables))));
373                                                 outputNames.push_back(getOutputFileName("nseqs", variables)); outputTypes["nseqs"].push_back(getOutputFileName("nseqs", variables));
374                                         }else if (Estimators[i] == "coverage") { 
375                                                 cDisplays.push_back(new CollectDisplay(new Coverage(), new OneColumnFile(getOutputFileName("coverage", variables))));
376                                                 outputNames.push_back(getOutputFileName("coverage", variables)); outputTypes["coverage"].push_back(getOutputFileName("coverage", variables));
377                                         }else if (Estimators[i] == "ace") { 
378                                                 cDisplays.push_back(new CollectDisplay(new Ace(abund), new ThreeColumnFile(getOutputFileName("ace", variables))));
379                                                 outputNames.push_back(getOutputFileName("ace", variables)); outputTypes["ace"].push_back(getOutputFileName("ace", variables));
380                                         }else if (Estimators[i] == "jack") { 
381                                                 cDisplays.push_back(new CollectDisplay(new Jackknife(), new ThreeColumnFile(getOutputFileName("jack", variables))));
382                                                 outputNames.push_back(getOutputFileName("jack", variables)); outputTypes["jack"].push_back(getOutputFileName("jack", variables));
383                                         }else if (Estimators[i] == "shannon") { 
384                                                 cDisplays.push_back(new CollectDisplay(new Shannon(), new ThreeColumnFile(getOutputFileName("shannon", variables))));
385                                                 outputNames.push_back(getOutputFileName("shannon", variables)); outputTypes["shannon"].push_back(getOutputFileName("shannon", variables));
386                                         }else if (Estimators[i] == "shannoneven") { 
387                                                 cDisplays.push_back(new CollectDisplay(new ShannonEven(), new OneColumnFile(getOutputFileName("shannoneven", variables))));
388                                                 outputNames.push_back(getOutputFileName("shannoneven", variables)); outputTypes["shannoneven"].push_back(getOutputFileName("shannoneven", variables));
389                                         }else if (Estimators[i] == "npshannon") { 
390                                                 cDisplays.push_back(new CollectDisplay(new NPShannon(), new OneColumnFile(getOutputFileName("npshannon", variables))));
391                                                 outputNames.push_back(getOutputFileName("npshannon", variables)); outputTypes["npshannon"].push_back(getOutputFileName("npshannon", variables));
392                                         }else if (Estimators[i] == "heip") { 
393                                                 cDisplays.push_back(new CollectDisplay(new Heip(), new OneColumnFile(getOutputFileName("heip", variables))));
394                                                 outputNames.push_back(getOutputFileName("heip", variables)); outputTypes["heip"].push_back(getOutputFileName("heip", variables));
395                                         }else if (Estimators[i] == "smithwilson") { 
396                                                 cDisplays.push_back(new CollectDisplay(new SmithWilson(), new OneColumnFile(getOutputFileName("smithwilson", variables))));
397                                                 outputNames.push_back(getOutputFileName("smithwilson", variables)); outputTypes["smithwilson"].push_back(getOutputFileName("smithwilson", variables));
398                                         }else if (Estimators[i] == "simpson") { 
399                                                 cDisplays.push_back(new CollectDisplay(new Simpson(), new ThreeColumnFile(getOutputFileName("simpson", variables))));
400                                                 outputNames.push_back(getOutputFileName("simpson", variables)); outputTypes["simpson"].push_back(getOutputFileName("simpson", variables));
401                                         }else if (Estimators[i] == "simpsoneven") { 
402                                                 cDisplays.push_back(new CollectDisplay(new SimpsonEven(), new OneColumnFile(getOutputFileName("simpsoneven", variables))));
403                                                 outputNames.push_back(getOutputFileName("simpsoneven", variables)); outputTypes["simpsoneven"].push_back(getOutputFileName("simpsoneven", variables));
404                                         }else if (Estimators[i] == "invsimpson") { 
405                                                 cDisplays.push_back(new CollectDisplay(new InvSimpson(), new ThreeColumnFile(getOutputFileName("invsimpson", variables))));
406                                                 outputNames.push_back(getOutputFileName("invsimpson", variables)); outputTypes["invsimpson"].push_back(getOutputFileName("invsimpson", variables));
407                                         }else if (Estimators[i] == "bootstrap") { 
408                                                 cDisplays.push_back(new CollectDisplay(new Bootstrap(), new OneColumnFile(getOutputFileName("bootstrap", variables))));
409                                                 outputNames.push_back(getOutputFileName("bootstrap", variables)); outputTypes["bootstrap"].push_back(getOutputFileName("bootstrap", variables));
410                                         }else if (Estimators[i] == "geometric") { 
411                                                 cDisplays.push_back(new CollectDisplay(new Geom(), new OneColumnFile(getOutputFileName("geometric", variables))));
412                                                 outputNames.push_back(getOutputFileName("geometric", variables)); outputTypes["geometric"].push_back(getOutputFileName("geometric", variables));
413                                         }else if (Estimators[i] == "qstat") { 
414                                                 cDisplays.push_back(new CollectDisplay(new QStat(), new OneColumnFile(getOutputFileName("qstat", variables))));
415                                                 outputNames.push_back(getOutputFileName("qstat", variables)); outputTypes["qstat"].push_back(getOutputFileName("qstat", variables));
416                                         }else if (Estimators[i] == "logseries") { 
417                                                 cDisplays.push_back(new CollectDisplay(new LogSD(), new OneColumnFile(getOutputFileName("logseries", variables))));
418                                                 outputNames.push_back(getOutputFileName("logseries", variables)); outputTypes["logseries"].push_back(getOutputFileName("logseries", variables));
419                                         }else if (Estimators[i] == "bergerparker") { 
420                                                 cDisplays.push_back(new CollectDisplay(new BergerParker(), new OneColumnFile(getOutputFileName("bergerparker", variables))));
421                                                 outputNames.push_back(getOutputFileName("bergerparker", variables)); outputTypes["bergerparker"].push_back(getOutputFileName("bergerparker", variables));
422                                         }else if (Estimators[i] == "bstick") { 
423                                                 cDisplays.push_back(new CollectDisplay(new BStick(), new ThreeColumnFile(getOutputFileName("bstick", variables))));
424                                                 outputNames.push_back(getOutputFileName("bstick", variables)); outputTypes["bstick"].push_back(getOutputFileName("bstick", variables));
425                                         }else if (Estimators[i] == "goodscoverage") { 
426                                                 cDisplays.push_back(new CollectDisplay(new GoodsCoverage(), new OneColumnFile(getOutputFileName("goodscoverage", variables))));
427                                                 outputNames.push_back(getOutputFileName("goodscoverage", variables)); outputTypes["goodscoverage"].push_back(getOutputFileName("goodscoverage", variables));
428                                         }else if (Estimators[i] == "efron") {
429                                                 cDisplays.push_back(new CollectDisplay(new Efron(size), new OneColumnFile(getOutputFileName("efron", variables))));
430                                                 outputNames.push_back(getOutputFileName("efron", variables)); outputTypes["efron"].push_back(getOutputFileName("efron", variables));
431                                         }else if (Estimators[i] == "boneh") {
432                                                 cDisplays.push_back(new CollectDisplay(new Boneh(size), new OneColumnFile(getOutputFileName("boneh", variables))));
433                                                 outputNames.push_back(getOutputFileName("boneh", variables)); outputTypes["boneh"].push_back(getOutputFileName("boneh", variables));
434                                         }else if (Estimators[i] == "solow") {
435                                                 cDisplays.push_back(new CollectDisplay(new Solow(size), new OneColumnFile(getOutputFileName("solow", variables))));
436                                                 outputNames.push_back(getOutputFileName("solow", variables)); outputTypes["solow"].push_back(getOutputFileName("solow", variables));
437                                         }else if (Estimators[i] == "shen") {
438                                                 cDisplays.push_back(new CollectDisplay(new Shen(size, abund), new OneColumnFile(getOutputFileName("shen", variables))));
439                                                 outputNames.push_back(getOutputFileName("shen", variables)); outputTypes["shen"].push_back(getOutputFileName("shen", variables));
440                                         }
441                                 }
442                         }
443                 
444                         //if the users entered no valid calculators don't execute command
445                         if (cDisplays.size() == 0) { return 0; }
446                         
447                         input = new InputData(inputFileNames[p], format);
448                         order = input->getOrderVector();
449                         string lastLabel = order->getLabel();
450                         
451                         //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
452                         set<string> processedLabels;
453                         set<string> userLabels = labels;
454                         
455                         if (m->control_pressed) {  
456                                 for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
457                                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);        } outputTypes.clear(); 
458                                 delete input;  
459                                 delete order; 
460                                 m->clearGroups();
461                                 return 0;
462                         }
463
464
465                         while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
466                         
467                                 if (m->control_pressed) { 
468                                         for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
469                                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);        } outputTypes.clear(); 
470                                         delete input;  
471                                         delete order; 
472                                         m->clearGroups();
473                                         return 0;
474                                 }
475
476                                 
477                                 if(allLines == 1 || labels.count(order->getLabel()) == 1){
478                                 
479                                         m->mothurOut(order->getLabel()); m->mothurOutEndLine();
480                                         cCurve = new Collect(order, cDisplays);
481                                         cCurve->getCurve(freq);
482                                         delete cCurve;
483                                         
484                                         processedLabels.insert(order->getLabel());
485                                         userLabels.erase(order->getLabel());
486                                         
487                                         
488                                 }
489                                 //you have a label the user want that is smaller than this label and the last label has not already been processed 
490                                 if ((m->anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
491                                         string saveLabel = order->getLabel();
492                                         
493                                         delete order;
494                                         order = (input->getOrderVector(lastLabel));
495                                         
496                                         m->mothurOut(order->getLabel()); m->mothurOutEndLine();
497                                         cCurve = new Collect(order, cDisplays);
498                                         cCurve->getCurve(freq);
499                                         delete cCurve;
500                                         
501                                         
502                                         processedLabels.insert(order->getLabel());
503                                         userLabels.erase(order->getLabel());
504                                         
505                                         //restore real lastlabel to save below
506                                         order->setLabel(saveLabel);
507                                 }
508                                 
509                                 lastLabel = order->getLabel();  
510                                 
511                                 delete order;           
512                                 order = (input->getOrderVector());
513                         }
514                         
515                         
516                         if (m->control_pressed) { 
517                                         for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
518                                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);        } outputTypes.clear(); 
519                                         delete input;  
520                                         m->clearGroups();
521                                         return 0;
522                         }
523                                 
524                         //output error messages about any remaining user labels
525                         set<string>::iterator it;
526                         bool needToRun = false;
527                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
528                                 m->mothurOut("Your file does not include the label " + *it); 
529                                 if (processedLabels.count(lastLabel) != 1) {
530                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
531                                         needToRun = true;
532                                 }else {
533                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
534                                 }
535                         }
536                         
537                         //run last label if you need to
538                         if (needToRun == true)  {
539                                 if (order != NULL) {    delete order;   }
540                                 order = (input->getOrderVector(lastLabel));
541                                 
542                                 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
543                                 
544                                 cCurve = new Collect(order, cDisplays);
545                                 cCurve->getCurve(freq);
546                                 delete cCurve;
547                                 
548                                 if (m->control_pressed) { 
549                                         for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
550                                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);        } outputTypes.clear(); 
551                                         delete input;  
552                                         delete order;
553                                         m->clearGroups();
554                                         return 0;
555                                 }
556                                 delete order;
557                         }
558                         
559                         for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
560                         cDisplays.clear();
561                         delete input;  
562                 }
563                 
564                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]);        } return 0; }
565                                 
566                 m->mothurOutEndLine();
567                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
568                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
569                 m->mothurOutEndLine();
570
571                 
572                 return 0;
573         }
574         catch(exception& e) {
575                 m->errorOut(e, "CollectCommand", "execute");
576                 exit(1);
577         }
578 }
579
580 //**********************************************************************************************************************
581 vector<string> CollectCommand::parseSharedFile(string filename) {
582         try {
583                 vector<string> filenames;
584                 
585                 map<string, ofstream*> filehandles;
586                 map<string, ofstream*>::iterator it3;
587                                         
588                 input = new InputData(filename, "sharedfile");
589                 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
590                 
591                 string sharedFileRoot = m->getRootName(filename);
592                 
593                 //clears file before we start to write to it below
594                 for (int i=0; i<lookup.size(); i++) {
595                         m->mothurRemove((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
596                         filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
597                 }
598                 
599                 ofstream* temp;
600                 for (int i=0; i<lookup.size(); i++) {
601                         temp = new ofstream;
602                         filehandles[lookup[i]->getGroup()] = temp;
603                         groups.push_back(lookup[i]->getGroup());
604                 }
605
606                 while(lookup[0] != NULL) {
607                 
608                         for (int i = 0; i < lookup.size(); i++) {
609                                 RAbundVector rav = lookup[i]->getRAbundVector();
610                                 m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
611                                 rav.print(*(filehandles[lookup[i]->getGroup()]));
612                                 (*(filehandles[lookup[i]->getGroup()])).close();
613                         }
614                 
615                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
616                         lookup = input->getSharedRAbundVectors();
617                 }
618                 
619                 //free memory
620                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
621                         delete it3->second;
622                 }
623                 
624                 delete input;
625
626                 return filenames;
627         }
628         catch(exception& e) {
629                 m->errorOut(e, "CollectCommand", "parseSharedFile");
630                 exit(1);
631         }
632 }
633 //**********************************************************************************************************************
634