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