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