]> git.donarmstrong.com Git - mothur.git/blob - rarefactcommand.cpp
added getCommandInfoCommand for gui
[mothur.git] / rarefactcommand.cpp
1 /*
2  *  rarefactcommand.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 "rarefactcommand.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 "heip.h"
19 #include "smithwilson.h"
20 #include "invsimpson.h"
21 #include "npshannon.h"
22 #include "shannoneven.h"
23 #include "shannon.h"
24 #include "jackknife.h"
25 #include "coverage.h"
26
27
28 //**********************************************************************************************************************
29 vector<string> RareFactCommand::setParameters(){        
30         try {
31                 CommandParameter plist("list", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(plist);
32                 CommandParameter prabund("rabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(prabund);
33                 CommandParameter psabund("sabund", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(psabund);
34                 CommandParameter pshared("shared", "InputTypes", "", "", "LRSS", "LRSS", "none",false,false); parameters.push_back(pshared);
35                 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
36                 CommandParameter pfreq("freq", "Number", "", "100", "", "", "",false,false); parameters.push_back(pfreq);
37                 CommandParameter piters("iters", "Number", "", "1000", "", "", "",false,false); parameters.push_back(piters);
38                 CommandParameter pcalc("calc", "Multiple", "sobs-chao-nseqs-coverage-ace-jack-shannon-shannoneven-npshannon-heip-smithwilson-simpson-simpsoneven-invsimpson-bootstrap", "sobs", "", "", "",true,false); parameters.push_back(pcalc);
39                 CommandParameter pabund("abund", "Number", "", "10", "", "", "",false,false); parameters.push_back(pabund);
40                 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
41                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
42                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
43                 
44                 vector<string> myArray;
45                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
46                 return myArray;
47         }
48         catch(exception& e) {
49                 m->errorOut(e, "RareFactCommand", "setParameters");
50                 exit(1);
51         }
52 }
53 //**********************************************************************************************************************
54 string RareFactCommand::getHelpString(){        
55         try {
56                 ValidCalculators validCalculator;
57                 string helpString = "";
58                 helpString += "The rarefaction.single command parameters are list, sabund, rabund, shared, label, iters, freq, calc, processors and abund.  list, sabund, rabund or shared is required unless you have a valid current file. \n";
59                 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";
60                 helpString += "The processors parameter allows you to specify the number of processors to use. The default is 1.\n";
61                 helpString += "The rarefaction.single command should be in the following format: \n";
62                 helpString += "rarefaction.single(label=yourLabel, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n";
63                 helpString += "Example rarefaction.single(label=unique-.01-.03, iters=10000, freq=10, calc=sobs-rchao-race-rjack-rbootstrap-rshannon-rnpshannon-rsimpson).\n";
64                 helpString += "The default values for iters is 1000, freq is 100, and calc is rarefaction which calculates the rarefaction curve for the observed richness.\n";
65                 validCalculator.printCalc("rarefaction");
66                 helpString += "The label parameter is used to analyze specific labels in your input.\n";
67                 helpString += "Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n";
68                 return helpString;
69         }
70         catch(exception& e) {
71                 m->errorOut(e, "RareFactCommand", "getHelpString");
72                 exit(1);
73         }
74 }
75
76 //**********************************************************************************************************************
77 RareFactCommand::RareFactCommand(){     
78         try {
79                 abort = true; calledHelp = true; 
80                 setParameters();
81                 vector<string> tempOutNames;
82                 outputTypes["rarefaction"] = tempOutNames;
83                 outputTypes["r_chao"] = tempOutNames;
84                 outputTypes["r_ace"] = tempOutNames;
85                 outputTypes["r_jack"] = tempOutNames;
86                 outputTypes["r_shannon"] = tempOutNames;
87                 outputTypes["r_shannoneven"] = tempOutNames;
88                 outputTypes["r_heip"] = tempOutNames;
89                 outputTypes["r_smithwilson"] = tempOutNames;
90                 outputTypes["r_npshannon"] = tempOutNames;
91                 outputTypes["r_simpson"] = tempOutNames;
92                 outputTypes["r_simpsoneven"] = tempOutNames;
93                 outputTypes["r_invsimpson"] = tempOutNames;
94                 outputTypes["r_bootstrap"] = tempOutNames;
95                 outputTypes["r_coverage"] = tempOutNames;
96                 outputTypes["r_nseqs"] = tempOutNames;
97         }
98         catch(exception& e) {
99                 m->errorOut(e, "RareFactCommand", "RareFactCommand");
100                 exit(1);
101         }
102 }
103 //**********************************************************************************************************************
104 RareFactCommand::RareFactCommand(string option)  {
105         try {
106                 abort = false; calledHelp = false;   
107                 allLines = 1;
108                                                 
109                 //allow user to run help
110                 if(option == "help") { abort = true; calledHelp = true; }
111                 
112                 else {
113                         vector<string> myArray = setParameters();
114                         
115                         OptionParser parser(option);
116                         map<string,string> parameters = parser.getParameters();
117                         map<string,string>::iterator it;
118                         
119                         ValidParameters validParameter;
120                 
121                         //check to make sure all parameters are valid for command
122                         for (it = parameters.begin(); it != parameters.end(); it++) { 
123                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
124                         }
125                         
126                         //initialize outputTypes
127                         vector<string> tempOutNames;
128                         outputTypes["rarefaction"] = tempOutNames;
129                         outputTypes["r_chao"] = tempOutNames;
130                         outputTypes["r_ace"] = tempOutNames;
131                         outputTypes["r_jack"] = tempOutNames;
132                         outputTypes["r_shannon"] = tempOutNames;
133                         outputTypes["r_shannoneven"] = tempOutNames;
134                         outputTypes["r_heip"] = tempOutNames;
135                         outputTypes["r_smithwilson"] = tempOutNames;
136                         outputTypes["r_npshannon"] = tempOutNames;
137                         outputTypes["r_simpson"] = tempOutNames;
138                         outputTypes["r_simpsoneven"] = tempOutNames;
139                         outputTypes["r_invsimpson"] = tempOutNames;
140                         outputTypes["r_bootstrap"] = tempOutNames;
141                         outputTypes["r_coverage"] = tempOutNames;
142                         outputTypes["r_nseqs"] = tempOutNames;
143                         
144                         //if the user changes the input directory command factory will send this info to us in the output parameter 
145                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
146                         if (inputDir == "not found"){   inputDir = "";          }
147                         else {
148                                 string path;
149                                 it = parameters.find("shared");
150                                 //user has given a template file
151                                 if(it != parameters.end()){ 
152                                         path = m->hasPath(it->second);
153                                         //if the user has not given a path then, add inputdir. else leave path alone.
154                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
155                                 }
156                                 
157                                 it = parameters.find("rabund");
158                                 //user has given a template file
159                                 if(it != parameters.end()){ 
160                                         path = m->hasPath(it->second);
161                                         //if the user has not given a path then, add inputdir. else leave path alone.
162                                         if (path == "") {       parameters["rabund"] = inputDir + it->second;           }
163                                 }
164                                 
165                                 it = parameters.find("sabund");
166                                 //user has given a template file
167                                 if(it != parameters.end()){ 
168                                         path = m->hasPath(it->second);
169                                         //if the user has not given a path then, add inputdir. else leave path alone.
170                                         if (path == "") {       parameters["sabund"] = inputDir + it->second;           }
171                                 }
172                                 
173                                 it = parameters.find("list");
174                                 //user has given a template file
175                                 if(it != parameters.end()){ 
176                                         path = m->hasPath(it->second);
177                                         //if the user has not given a path then, add inputdir. else leave path alone.
178                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
179                                 }
180                         }
181                         
182                         //check for required parameters
183                         listfile = validParameter.validFile(parameters, "list", true);
184                         if (listfile == "not open") { listfile = ""; abort = true; }
185                         else if (listfile == "not found") { listfile = ""; }
186                         else {  format = "list"; inputfile = listfile; }
187                         
188                         sabundfile = validParameter.validFile(parameters, "sabund", true);
189                         if (sabundfile == "not open") { sabundfile = ""; abort = true; }        
190                         else if (sabundfile == "not found") { sabundfile = ""; }
191                         else {  format = "sabund"; inputfile = sabundfile; }
192                         
193                         rabundfile = validParameter.validFile(parameters, "rabund", true);
194                         if (rabundfile == "not open") { rabundfile = ""; abort = true; }        
195                         else if (rabundfile == "not found") { rabundfile = ""; }
196                         else {  format = "rabund"; inputfile = rabundfile; }
197                         
198                         sharedfile = validParameter.validFile(parameters, "shared", true);
199                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }        
200                         else if (sharedfile == "not found") { sharedfile = ""; }
201                         else {  format = "sharedfile"; inputfile = sharedfile; }
202                                 
203                         if ((sharedfile == "") && (listfile == "") && (rabundfile == "") && (sabundfile == "")) { 
204                                 //is there are current file available for any of these?
205                                 //give priority to shared, then list, then rabund, then sabund
206                                 //if there is a current shared file, use it
207                                 sharedfile = m->getSharedFile(); 
208                                 if (sharedfile != "") { inputfile = sharedfile; format = "sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
209                                 else { 
210                                         listfile = m->getListFile(); 
211                                         if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
212                                         else { 
213                                                 rabundfile = m->getRabundFile(); 
214                                                 if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
215                                                 else { 
216                                                         sabundfile = m->getSabundFile(); 
217                                                         if (sabundfile != "") { inputfile = sabundfile; format = "sabund"; m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
218                                                         else { 
219                                                                 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(); 
220                                                                 abort = true;
221                                                         }
222                                                 }
223                                         }
224                                 }
225                         }
226                         
227                         //if the user changes the output directory command factory will send this info to us in the output parameter 
228                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(inputfile);              }
229
230                         //check for optional parameter and set defaults
231                         // ...at some point should added some additional type checking...
232                         label = validParameter.validFile(parameters, "label", false);                   
233                         if (label == "not found") { label = ""; }
234                         else { 
235                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
236                                 else { allLines = 1;  }
237                         }
238                                 
239                         calc = validParameter.validFile(parameters, "calc", false);                     
240                         if (calc == "not found") { calc = "sobs";  }
241                         else { 
242                                  if (calc == "default")  {  calc = "sobs";  }
243                         }
244                         m->splitAtDash(calc, Estimators);
245
246                         string temp;
247                         temp = validParameter.validFile(parameters, "freq", false);                     if (temp == "not found") { temp = "100"; }
248                         convert(temp, freq); 
249                         
250                         temp = validParameter.validFile(parameters, "abund", false);                    if (temp == "not found") { temp = "10"; }
251                         convert(temp, abund); 
252                         
253                         temp = validParameter.validFile(parameters, "iters", false);                    if (temp == "not found") { temp = "1000"; }
254                         convert(temp, nIters); 
255                         
256                         temp = validParameter.validFile(parameters, "processors", false);       if (temp == "not found"){       temp = m->getProcessors();      }
257                         m->setProcessors(temp);
258                         convert(temp, processors);
259                 }
260                 
261         }
262         catch(exception& e) {
263                 m->errorOut(e, "RareFactCommand", "RareFactCommand");
264                 exit(1);
265         }
266 }
267 //**********************************************************************************************************************
268
269 int RareFactCommand::execute(){
270         try {
271         
272                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
273                 
274                 if ((format != "sharedfile")) { inputFileNames.push_back(inputfile);  }
275                 else {  inputFileNames = parseSharedFile(sharedfile);  format = "rabund"; }
276                                 
277                 if (m->control_pressed) { return 0; }
278                 
279                 for (int p = 0; p < inputFileNames.size(); p++) {
280                         
281                         string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p]));
282                                                 
283                         if (m->control_pressed) {  outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str());         }  m->Groups.clear();  return 0; }
284                         
285                         if (inputFileNames.size() > 1) {
286                                 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
287                         }
288                         int i;
289                         ValidCalculators validCalculator;
290                         
291                           
292                         for (i=0; i<Estimators.size(); i++) {
293                                 if (validCalculator.isValidCalculator("rarefaction", Estimators[i]) == true) { 
294                                         if (Estimators[i] == "sobs") { 
295                                                 rDisplays.push_back(new RareDisplay(new Sobs(), new ThreeColumnFile(fileNameRoot+"rarefaction")));
296                                                 outputNames.push_back(fileNameRoot+"rarefaction"); outputTypes["rarefaction"].push_back(fileNameRoot+"rarefaction");
297                                         }else if (Estimators[i] == "chao") { 
298                                                 rDisplays.push_back(new RareDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+"r_chao")));
299                                                 outputNames.push_back(fileNameRoot+"r_chao"); outputTypes["r_chao"].push_back(fileNameRoot+"r_chao");
300                                         }else if (Estimators[i] == "ace") { 
301                                                 if(abund < 5)
302                                                         abund = 10;
303                                                 rDisplays.push_back(new RareDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+"r_ace")));
304                                                 outputNames.push_back(fileNameRoot+"r_ace"); outputTypes["r_ace"].push_back(fileNameRoot+"r_ace");
305                                         }else if (Estimators[i] == "jack") { 
306                                                 rDisplays.push_back(new RareDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+"r_jack")));
307                                                 outputNames.push_back(fileNameRoot+"r_jack"); outputTypes["r_jack"].push_back(fileNameRoot+"r_jack");
308                                         }else if (Estimators[i] == "shannon") { 
309                                                 rDisplays.push_back(new RareDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+"r_shannon")));
310                                                 outputNames.push_back(fileNameRoot+"r_shannon"); outputTypes["r_shannon"].push_back(fileNameRoot+"r_shannon");
311                                         }else if (Estimators[i] == "shannoneven") { 
312                                                 rDisplays.push_back(new RareDisplay(new ShannonEven(), new ThreeColumnFile(fileNameRoot+"r_shannoneven")));
313                                                 outputNames.push_back(fileNameRoot+"r_shannoneven"); outputTypes["r_shannoneven"].push_back(fileNameRoot+"r_shannoneven");
314                                         }else if (Estimators[i] == "heip") { 
315                                                 rDisplays.push_back(new RareDisplay(new Heip(), new ThreeColumnFile(fileNameRoot+"r_heip")));
316                                                 outputNames.push_back(fileNameRoot+"r_heip"); outputTypes["r_heip"].push_back(fileNameRoot+"r_heip");
317                                         }else if (Estimators[i] == "smithwilson") { 
318                                                 rDisplays.push_back(new RareDisplay(new SmithWilson(), new ThreeColumnFile(fileNameRoot+"r_smithwilson")));
319                                                 outputNames.push_back(fileNameRoot+"r_smithwilson"); outputTypes["r_smithwilson"].push_back(fileNameRoot+"r_smithwilson");
320                                         }else if (Estimators[i] == "npshannon") { 
321                                                 rDisplays.push_back(new RareDisplay(new NPShannon(), new ThreeColumnFile(fileNameRoot+"r_npshannon")));
322                                                 outputNames.push_back(fileNameRoot+"r_npshannon"); outputTypes["r_npshannon"].push_back(fileNameRoot+"r_npshannon");
323                                         }else if (Estimators[i] == "simpson") { 
324                                                 rDisplays.push_back(new RareDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+"r_simpson")));
325                                                 outputNames.push_back(fileNameRoot+"r_simpson"); outputTypes["r_simpson"].push_back(fileNameRoot+"r_simpson");
326                                         }else if (Estimators[i] == "simpsoneven") { 
327                                                 rDisplays.push_back(new RareDisplay(new SimpsonEven(), new ThreeColumnFile(fileNameRoot+"r_simpsoneven")));
328                                                 outputNames.push_back(fileNameRoot+"r_simpsoneven"); outputTypes["r_simpsoneven"].push_back(fileNameRoot+"r_simpsoneven");
329                                         }else if (Estimators[i] == "invsimpson") { 
330                                                 rDisplays.push_back(new RareDisplay(new InvSimpson(), new ThreeColumnFile(fileNameRoot+"r_invsimpson")));
331                                                 outputNames.push_back(fileNameRoot+"r_invsimpson"); outputTypes["r_invsimpson"].push_back(fileNameRoot+"r_invsimpson");
332                                         }else if (Estimators[i] == "bootstrap") { 
333                                                 rDisplays.push_back(new RareDisplay(new Bootstrap(), new ThreeColumnFile(fileNameRoot+"r_bootstrap")));
334                                                 outputNames.push_back(fileNameRoot+"r_bootstrap"); outputTypes["r_bootstrap"].push_back(fileNameRoot+"r_bootstrap");
335                                         }else if (Estimators[i] == "coverage") { 
336                                                 rDisplays.push_back(new RareDisplay(new Coverage(), new ThreeColumnFile(fileNameRoot+"r_coverage")));
337                                                 outputNames.push_back(fileNameRoot+"r_coverage"); outputTypes["r_coverage"].push_back(fileNameRoot+"r_coverage");
338                                         }else if (Estimators[i] == "nseqs") { 
339                                                 rDisplays.push_back(new RareDisplay(new NSeqs(), new ThreeColumnFile(fileNameRoot+"r_nseqs")));
340                                                 outputNames.push_back(fileNameRoot+"r_nseqs"); outputTypes["r_nseqs"].push_back(fileNameRoot+"r_nseqs");
341                                         }
342                                 }
343                         }
344                         
345                         
346                         //if the users entered no valid calculators don't execute command
347                         if (rDisplays.size() == 0) { for(int i=0;i<rDisplays.size();i++){       delete rDisplays[i];    }  return 0; }
348                         
349                         input = new InputData(inputFileNames[p], format);                       
350                         order = input->getOrderVector();
351                         string lastLabel = order->getLabel();
352                         
353                         //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
354                         set<string> processedLabels;
355                         set<string> userLabels = labels;
356                         
357                         if (m->control_pressed) { for(int i=0;i<rDisplays.size();i++){  delete rDisplays[i];    }  delete input;  delete order;  for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
358                         
359                         //as long as you are not at the end of the file or done wih the lines you want
360                         while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
361                                 
362                                 if (m->control_pressed) { for(int i=0;i<rDisplays.size();i++){  delete rDisplays[i];    }  delete input;  delete order;  for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
363
364                                 
365                                 if(allLines == 1 || labels.count(order->getLabel()) == 1){
366                                         
367                                         m->mothurOut(order->getLabel()); m->mothurOutEndLine();
368                                         rCurve = new Rarefact(order, rDisplays, processors);
369                                         rCurve->getCurve(freq, nIters);
370                                         delete rCurve;
371                                         
372                                         processedLabels.insert(order->getLabel());
373                                         userLabels.erase(order->getLabel());
374                                 }
375                                 
376                                 if ((m->anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
377                                         string saveLabel = order->getLabel();
378                                         
379                                         delete order;
380                                         order = (input->getOrderVector(lastLabel));
381                                         
382                                         m->mothurOut(order->getLabel()); m->mothurOutEndLine();
383                                         rCurve = new Rarefact(order, rDisplays, processors);
384                                         rCurve->getCurve(freq, nIters);
385                                         delete rCurve;
386                                         
387                                         processedLabels.insert(order->getLabel());
388                                         userLabels.erase(order->getLabel());
389                                         
390                                         //restore real lastlabel to save below
391                                         order->setLabel(saveLabel);
392                                 }
393                                 
394                                 lastLabel = order->getLabel();          
395                                 
396                                 delete order;
397                                 order = (input->getOrderVector());
398                         }
399                         
400                         if (m->control_pressed) { for(int i=0;i<rDisplays.size();i++){  delete rDisplays[i];    }  delete input;   for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str()); } return 0; }
401
402                         //output error messages about any remaining user labels
403                         set<string>::iterator it;
404                         bool needToRun = false;
405                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
406                                 m->mothurOut("Your file does not include the label " + *it);
407                                 if (processedLabels.count(lastLabel) != 1) {
408                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
409                                         needToRun = true;
410                                 }else {
411                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
412                                 }
413                         }
414                         
415                         if (m->control_pressed) { for(int i=0;i<rDisplays.size();i++){  delete rDisplays[i];    }  delete input;   for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str()); } return 0; }
416
417                         //run last label if you need to
418                         if (needToRun == true)  {
419                                 if (order != NULL) {    delete order;   }
420                                 order = (input->getOrderVector(lastLabel));
421                                 
422                                 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
423                                 rCurve = new Rarefact(order, rDisplays, processors);
424                                 rCurve->getCurve(freq, nIters);
425                                 delete rCurve;
426                                 
427                                 delete order;
428                         }
429                         
430                         
431                         for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }       
432                         rDisplays.clear();
433                         delete input;  
434                 }
435                 
436                 
437                 if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str()); } return 0; }
438
439                 m->mothurOutEndLine();
440                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
441                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
442                 m->mothurOutEndLine();
443
444                 return 0;
445         }
446         catch(exception& e) {
447                 m->errorOut(e, "RareFactCommand", "execute");
448                 exit(1);
449         }
450 }
451 //**********************************************************************************************************************
452 vector<string> RareFactCommand::parseSharedFile(string filename) {
453         try {
454                 vector<string> filenames;
455                 
456                 map<string, ofstream*> filehandles;
457                 map<string, ofstream*>::iterator it3;
458                 
459                 input = new InputData(filename, "sharedfile");
460                 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
461                 
462                 string sharedFileRoot = m->getRootName(filename);
463                 
464                 //clears file before we start to write to it below
465                 for (int i=0; i<lookup.size(); i++) {
466                         remove((sharedFileRoot + lookup[i]->getGroup() + ".rabund").c_str());
467                         filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
468                 }
469                 
470                 ofstream* temp;
471                 for (int i=0; i<lookup.size(); i++) {
472                         temp = new ofstream;
473                         filehandles[lookup[i]->getGroup()] = temp;
474                         groups.push_back(lookup[i]->getGroup());
475                 }
476
477                 while(lookup[0] != NULL) {
478                 
479                         for (int i = 0; i < lookup.size(); i++) {
480                                 RAbundVector rav = lookup[i]->getRAbundVector();
481                                 m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
482                                 rav.print(*(filehandles[lookup[i]->getGroup()]));
483                                 (*(filehandles[lookup[i]->getGroup()])).close();
484                         }
485                 
486                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
487                         lookup = input->getSharedRAbundVectors();
488                 }
489                 
490                 //free memory
491                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
492                         delete it3->second;
493                 }
494                 
495                 delete input;
496                 m->Groups.clear();
497
498                 return filenames;
499         }
500         catch(exception& e) {
501                 m->errorOut(e, "RareFactCommand", "parseSharedFile");
502                 exit(1);
503         }
504 }
505 //**********************************************************************************************************************
506
507
508