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