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