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