]> git.donarmstrong.com Git - mothur.git/blob - rarefactcommand.cpp
Merge remote-tracking branch 'mothur/master'
[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 pgroupmode("groupmode", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pgroupmode);
42                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
43                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
44                 
45                 vector<string> myArray;
46                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
47                 return myArray;
48         }
49         catch(exception& e) {
50                 m->errorOut(e, "RareFactCommand", "setParameters");
51                 exit(1);
52         }
53 }
54 //**********************************************************************************************************************
55 string RareFactCommand::getHelpString(){        
56         try {
57                 ValidCalculators validCalculator;
58                 string helpString = "";
59                 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";
60                 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";
61                 helpString += "The processors parameter allows you to specify the number of processors to use. The default is 1.\n";
62                 helpString += "The rarefaction.single command should be in the following format: \n";
63                 helpString += "rarefaction.single(label=yourLabel, iters=yourIters, freq=yourFreq, calc=yourEstimators).\n";
64                 helpString += "Example rarefaction.single(label=unique-.01-.03, iters=10000, freq=10, calc=sobs-rchao-race-rjack-rbootstrap-rshannon-rnpshannon-rsimpson).\n";
65                 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";
66                 validCalculator.printCalc("rarefaction");
67                 helpString += "If you are running rarefaction.single with a shared file and would like your results collated in one file, set groupmode=t. (Default=true).\n";
68                 helpString += "The label parameter is used to analyze specific labels in your input.\n";
69                 helpString += "Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n";
70                 return helpString;
71         }
72         catch(exception& e) {
73                 m->errorOut(e, "RareFactCommand", "getHelpString");
74                 exit(1);
75         }
76 }
77 //**********************************************************************************************************************
78 string RareFactCommand::getOutputFileNameTag(string type, string inputName=""){ 
79         try {
80         string outputFileName = "";
81                 map<string, vector<string> >::iterator it;
82         
83         //is this a type this command creates
84         it = outputTypes.find(type);
85         if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
86         else {
87             if (type == "rarefaction") {  outputFileName =  "rarefaction"; }
88             else if (type == "r_chao") {  outputFileName =  "r_chao"; }
89             else if (type == "r_ace") {  outputFileName =  "r_ace"; }
90             else if (type == "r_jack") {  outputFileName =  "r_jack"; }
91             else if (type == "r_shannon") {  outputFileName =  "r_shannon"; }
92             else if (type == "r_shannoneven") {  outputFileName =  "r_shannoneven"; }
93             else if (type == "r_smithwilson") {  outputFileName =  "r_smithwilson"; }
94             else if (type == "r_npshannon") {  outputFileName =  "r_npshannon"; }
95             else if (type == "r_simpson") {  outputFileName =  "r_simpson"; }
96             else if (type == "r_simpsoneven") {  outputFileName =  "r_simpsoneven"; }
97             else if (type == "r_invsimpson") {  outputFileName =  "r_invsimpson"; }
98             else if (type == "r_bootstrap") {  outputFileName =  "r_bootstrap"; }
99             else if (type == "r_coverage") {  outputFileName =  "r_coverage"; }
100             else if (type == "r_nseqs") {  outputFileName =  "r_nseqs"; }
101             else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
102         }
103         return outputFileName;
104         }
105         catch(exception& e) {
106                 m->errorOut(e, "RareFactCommand", "getOutputFileNameTag");
107                 exit(1);
108         }
109 }
110
111 //**********************************************************************************************************************
112 RareFactCommand::RareFactCommand(){     
113         try {
114                 abort = true; calledHelp = true; 
115                 setParameters();
116                 vector<string> tempOutNames;
117                 outputTypes["rarefaction"] = tempOutNames;
118                 outputTypes["r_chao"] = tempOutNames;
119                 outputTypes["r_ace"] = tempOutNames;
120                 outputTypes["r_jack"] = tempOutNames;
121                 outputTypes["r_shannon"] = tempOutNames;
122                 outputTypes["r_shannoneven"] = tempOutNames;
123                 outputTypes["r_heip"] = tempOutNames;
124                 outputTypes["r_smithwilson"] = tempOutNames;
125                 outputTypes["r_npshannon"] = tempOutNames;
126                 outputTypes["r_simpson"] = tempOutNames;
127                 outputTypes["r_simpsoneven"] = tempOutNames;
128                 outputTypes["r_invsimpson"] = tempOutNames;
129                 outputTypes["r_bootstrap"] = tempOutNames;
130                 outputTypes["r_coverage"] = tempOutNames;
131                 outputTypes["r_nseqs"] = tempOutNames;
132         }
133         catch(exception& e) {
134                 m->errorOut(e, "RareFactCommand", "RareFactCommand");
135                 exit(1);
136         }
137 }
138 //**********************************************************************************************************************
139 RareFactCommand::RareFactCommand(string option)  {
140         try {
141                 abort = false; calledHelp = false;   
142                 allLines = 1;
143                                                 
144                 //allow user to run help
145                 if(option == "help") { help(); abort = true; calledHelp = true; }
146                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
147                 
148                 else {
149                         vector<string> myArray = setParameters();
150                         
151                         OptionParser parser(option);
152                         map<string,string> parameters = parser.getParameters();
153                         map<string,string>::iterator it;
154                         
155                         ValidParameters validParameter;
156                 
157                         //check to make sure all parameters are valid for command
158                         for (it = parameters.begin(); it != parameters.end(); it++) { 
159                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
160                         }
161                         
162                         //initialize outputTypes
163                         vector<string> tempOutNames;
164                         outputTypes["rarefaction"] = tempOutNames;
165                         outputTypes["r_chao"] = tempOutNames;
166                         outputTypes["r_ace"] = tempOutNames;
167                         outputTypes["r_jack"] = tempOutNames;
168                         outputTypes["r_shannon"] = tempOutNames;
169                         outputTypes["r_shannoneven"] = tempOutNames;
170                         outputTypes["r_heip"] = tempOutNames;
171                         outputTypes["r_smithwilson"] = tempOutNames;
172                         outputTypes["r_npshannon"] = tempOutNames;
173                         outputTypes["r_simpson"] = tempOutNames;
174                         outputTypes["r_simpsoneven"] = tempOutNames;
175                         outputTypes["r_invsimpson"] = tempOutNames;
176                         outputTypes["r_bootstrap"] = tempOutNames;
177                         outputTypes["r_coverage"] = tempOutNames;
178                         outputTypes["r_nseqs"] = tempOutNames;
179                         
180                         //if the user changes the input directory command factory will send this info to us in the output parameter 
181                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
182                         if (inputDir == "not found"){   inputDir = "";          }
183                         else {
184                                 string path;
185                                 it = parameters.find("shared");
186                                 //user has given a template file
187                                 if(it != parameters.end()){ 
188                                         path = m->hasPath(it->second);
189                                         //if the user has not given a path then, add inputdir. else leave path alone.
190                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
191                                 }
192                                 
193                                 it = parameters.find("rabund");
194                                 //user has given a template file
195                                 if(it != parameters.end()){ 
196                                         path = m->hasPath(it->second);
197                                         //if the user has not given a path then, add inputdir. else leave path alone.
198                                         if (path == "") {       parameters["rabund"] = inputDir + it->second;           }
199                                 }
200                                 
201                                 it = parameters.find("sabund");
202                                 //user has given a template file
203                                 if(it != parameters.end()){ 
204                                         path = m->hasPath(it->second);
205                                         //if the user has not given a path then, add inputdir. else leave path alone.
206                                         if (path == "") {       parameters["sabund"] = inputDir + it->second;           }
207                                 }
208                                 
209                                 it = parameters.find("list");
210                                 //user has given a template file
211                                 if(it != parameters.end()){ 
212                                         path = m->hasPath(it->second);
213                                         //if the user has not given a path then, add inputdir. else leave path alone.
214                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
215                                 }
216                         }
217                         
218                         //check for required parameters
219                         listfile = validParameter.validFile(parameters, "list", true);
220                         if (listfile == "not open") { listfile = ""; abort = true; }
221                         else if (listfile == "not found") { listfile = ""; }
222                         else {  format = "list"; inputfile = listfile; m->setListFile(listfile); }
223                         
224                         sabundfile = validParameter.validFile(parameters, "sabund", true);
225                         if (sabundfile == "not open") { sabundfile = ""; abort = true; }        
226                         else if (sabundfile == "not found") { sabundfile = ""; }
227                         else {  format = "sabund"; inputfile = sabundfile; m->setSabundFile(sabundfile); }
228                         
229                         rabundfile = validParameter.validFile(parameters, "rabund", true);
230                         if (rabundfile == "not open") { rabundfile = ""; abort = true; }        
231                         else if (rabundfile == "not found") { rabundfile = ""; }
232                         else {  format = "rabund"; inputfile = rabundfile; m->setRabundFile(rabundfile); }
233                         
234                         sharedfile = validParameter.validFile(parameters, "shared", true);
235                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }        
236                         else if (sharedfile == "not found") { sharedfile = ""; }
237                         else {  format = "sharedfile"; inputfile = sharedfile; m->setSharedFile(sharedfile); }
238                                 
239                         if ((sharedfile == "") && (listfile == "") && (rabundfile == "") && (sabundfile == "")) { 
240                                 //is there are current file available for any of these?
241                                 //give priority to shared, then list, then rabund, then sabund
242                                 //if there is a current shared file, use it
243                                 sharedfile = m->getSharedFile(); 
244                                 if (sharedfile != "") { inputfile = sharedfile; format = "sharedfile"; m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
245                                 else { 
246                                         listfile = m->getListFile(); 
247                                         if (listfile != "") { inputfile = listfile; format = "list"; m->mothurOut("Using " + listfile + " as input file for the list parameter."); m->mothurOutEndLine(); }
248                                         else { 
249                                                 rabundfile = m->getRabundFile(); 
250                                                 if (rabundfile != "") { inputfile = rabundfile; format = "rabund"; m->mothurOut("Using " + rabundfile + " as input file for the rabund parameter."); m->mothurOutEndLine(); }
251                                                 else { 
252                                                         sabundfile = m->getSabundFile(); 
253                                                         if (sabundfile != "") { inputfile = sabundfile; format = "sabund"; m->mothurOut("Using " + sabundfile + " as input file for the sabund parameter."); m->mothurOutEndLine(); }
254                                                         else { 
255                                                                 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(); 
256                                                                 abort = true;
257                                                         }
258                                                 }
259                                         }
260                                 }
261                         }
262                         
263                         //if the user changes the output directory command factory will send this info to us in the output parameter 
264                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(inputfile);              }
265
266                         //check for optional parameter and set defaults
267                         // ...at some point should added some additional type checking...
268                         label = validParameter.validFile(parameters, "label", false);                   
269                         if (label == "not found") { label = ""; }
270                         else { 
271                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
272                                 else { allLines = 1;  }
273                         }
274                                 
275                         calc = validParameter.validFile(parameters, "calc", false);                     
276                         if (calc == "not found") { calc = "sobs";  }
277                         else { 
278                                  if (calc == "default")  {  calc = "sobs";  }
279                         }
280                         m->splitAtDash(calc, Estimators);
281                         if (m->inUsersGroups("citation", Estimators)) { 
282                                 ValidCalculators validCalc; validCalc.printCitations(Estimators); 
283                                 //remove citation from list of calcs
284                                 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") {  Estimators.erase(Estimators.begin()+i); break; } }
285                         }
286
287                         string temp;
288                         temp = validParameter.validFile(parameters, "freq", false);                     if (temp == "not found") { temp = "100"; }
289                         m->mothurConvert(temp, freq); 
290                         
291                         temp = validParameter.validFile(parameters, "abund", false);                    if (temp == "not found") { temp = "10"; }
292                         m->mothurConvert(temp, abund); 
293                         
294                         temp = validParameter.validFile(parameters, "iters", false);                    if (temp == "not found") { temp = "1000"; }
295                         m->mothurConvert(temp, nIters); 
296                         
297                         temp = validParameter.validFile(parameters, "processors", false);       if (temp == "not found"){       temp = m->getProcessors();      }
298                         m->setProcessors(temp);
299                         m->mothurConvert(temp, processors);
300                         
301                         temp = validParameter.validFile(parameters, "groupmode", false);                if (temp == "not found") { temp = "T"; }
302                         groupMode = m->isTrue(temp);
303                 }
304                 
305         }
306         catch(exception& e) {
307                 m->errorOut(e, "RareFactCommand", "RareFactCommand");
308                 exit(1);
309         }
310 }
311 //**********************************************************************************************************************
312
313 int RareFactCommand::execute(){
314         try {
315         
316                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
317                 
318         map<string, set<int> > labelToEnds;
319                 if ((format != "sharedfile")) { inputFileNames.push_back(inputfile);  }
320                 else {  inputFileNames = parseSharedFile(sharedfile, labelToEnds);  format = "rabund"; }
321         
322         if (m->control_pressed) { return 0; }
323                 
324                 map<int, string> file2Group; //index in outputNames[i] -> group
325                 for (int p = 0; p < inputFileNames.size(); p++) {
326                         
327                         string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p]));
328                                                 
329                         if (m->control_pressed) {  outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);        }  m->clearGroups();  return 0; }
330                         
331                         if (inputFileNames.size() > 1) {
332                                 m->mothurOutEndLine(); m->mothurOut("Processing group " + groups[p]); m->mothurOutEndLine(); m->mothurOutEndLine();
333                         }
334                         int i;
335                         ValidCalculators validCalculator;
336                         
337                           
338                         for (i=0; i<Estimators.size(); i++) {
339                                 if (validCalculator.isValidCalculator("rarefaction", Estimators[i]) == true) { 
340                                         if (Estimators[i] == "sobs") { 
341                                                 rDisplays.push_back(new RareDisplay(new Sobs(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("rarefaction"))));
342                                                 outputNames.push_back(fileNameRoot+getOutputFileNameTag("rarefaction")); outputTypes["rarefaction"].push_back(fileNameRoot+getOutputFileNameTag("rarefaction"));
343                                         }else if (Estimators[i] == "chao") { 
344                                                 rDisplays.push_back(new RareDisplay(new Chao1(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_chao"))));
345                                                 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_chao")); outputTypes["r_chao"].push_back(fileNameRoot+getOutputFileNameTag("r_chao"));
346                                         }else if (Estimators[i] == "ace") { 
347                                                 if(abund < 5)
348                                                         abund = 10;
349                                                 rDisplays.push_back(new RareDisplay(new Ace(abund), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_ace"))));
350                                                 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_ace")); outputTypes["r_ace"].push_back(fileNameRoot+getOutputFileNameTag("r_ace"));
351                                         }else if (Estimators[i] == "jack") { 
352                                                 rDisplays.push_back(new RareDisplay(new Jackknife(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_jack"))));
353                                                 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_jack")); outputTypes["r_jack"].push_back(fileNameRoot+getOutputFileNameTag("r_jack"));
354                                         }else if (Estimators[i] == "shannon") { 
355                                                 rDisplays.push_back(new RareDisplay(new Shannon(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_shannon"))));
356                                                 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_shannon")); outputTypes["r_shannon"].push_back(fileNameRoot+getOutputFileNameTag("r_shannon"));
357                                         }else if (Estimators[i] == "shannoneven") { 
358                                                 rDisplays.push_back(new RareDisplay(new ShannonEven(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_shannoneven"))));
359                                                 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_shannoneven")); outputTypes["r_shannoneven"].push_back(fileNameRoot+getOutputFileNameTag("r_shannoneven"));
360                                         }else if (Estimators[i] == "heip") { 
361                                                 rDisplays.push_back(new RareDisplay(new Heip(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_heip"))));
362                                                 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_heip")); outputTypes["r_heip"].push_back(fileNameRoot+getOutputFileNameTag("r_heip"));
363                                         }else if (Estimators[i] == "smithwilson") { 
364                                                 rDisplays.push_back(new RareDisplay(new SmithWilson(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_smithwilson"))));
365                                                 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_smithwilson")); outputTypes["r_smithwilson"].push_back(fileNameRoot+getOutputFileNameTag("r_smithwilson"));
366                                         }else if (Estimators[i] == "npshannon") { 
367                                                 rDisplays.push_back(new RareDisplay(new NPShannon(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_npshannon"))));
368                                                 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_npshannon")); outputTypes["r_npshannon"].push_back(fileNameRoot+getOutputFileNameTag("r_npshannon"));
369                                         }else if (Estimators[i] == "simpson") { 
370                                                 rDisplays.push_back(new RareDisplay(new Simpson(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_simpson"))));
371                                                 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_simpson")); outputTypes["r_simpson"].push_back(fileNameRoot+getOutputFileNameTag("r_simpson"));
372                                         }else if (Estimators[i] == "simpsoneven") { 
373                                                 rDisplays.push_back(new RareDisplay(new SimpsonEven(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_simpsoneven"))));
374                                                 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_simpsoneven")); outputTypes["r_simpsoneven"].push_back(fileNameRoot+getOutputFileNameTag("r_simpsoneven"));
375                                         }else if (Estimators[i] == "invsimpson") { 
376                                                 rDisplays.push_back(new RareDisplay(new InvSimpson(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_invsimpson"))));
377                                                 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_invsimpson")); outputTypes["r_invsimpson"].push_back(fileNameRoot+getOutputFileNameTag("r_invsimpson"));
378                                         }else if (Estimators[i] == "bootstrap") { 
379                                                 rDisplays.push_back(new RareDisplay(new Bootstrap(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_bootstrap"))));
380                                                 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_bootstrap")); outputTypes["r_bootstrap"].push_back(fileNameRoot+getOutputFileNameTag("r_bootstrap"));
381                                         }else if (Estimators[i] == "coverage") { 
382                                                 rDisplays.push_back(new RareDisplay(new Coverage(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_coverage"))));
383                                                 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_coverage")); outputTypes["r_coverage"].push_back(fileNameRoot+getOutputFileNameTag("r_coverage"));
384                                         }else if (Estimators[i] == "nseqs") { 
385                                                 rDisplays.push_back(new RareDisplay(new NSeqs(), new ThreeColumnFile(fileNameRoot+getOutputFileNameTag("r_nseqs"))));
386                                                 outputNames.push_back(fileNameRoot+getOutputFileNameTag("r_nseqs")); outputTypes["r_nseqs"].push_back(fileNameRoot+getOutputFileNameTag("r_nseqs"));
387                                         }
388                     if (inputFileNames.size() > 1) { file2Group[outputNames.size()-1] = groups[p]; }
389                                 }
390                         }
391                         
392                         
393                         //if the users entered no valid calculators don't execute command
394                         if (rDisplays.size() == 0) { for(int i=0;i<rDisplays.size();i++){       delete rDisplays[i];    }  return 0; }
395                         
396                         input = new InputData(inputFileNames[p], format);                       
397                         order = input->getOrderVector();
398                         string lastLabel = order->getLabel();
399                         
400                         //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
401                         set<string> processedLabels;
402                         set<string> userLabels = labels;
403                         
404                         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++) { m->mothurRemove(outputNames[i]); } return 0; }
405                         
406                         //as long as you are not at the end of the file or done wih the lines you want
407                         while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
408                                 
409                                 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++) { m->mothurRemove(outputNames[i]); } return 0; }
410
411                                 
412                                 if(allLines == 1 || labels.count(order->getLabel()) == 1){
413                                         
414                                         m->mothurOut(order->getLabel()); m->mothurOutEndLine();
415                     map<string, set<int> >::iterator itEndings = labelToEnds.find(order->getLabel());
416                     set<int> ends;
417                     if (itEndings != labelToEnds.end()) { ends = itEndings->second; }
418                                         rCurve = new Rarefact(order, rDisplays, processors, ends);
419                                         rCurve->getCurve(freq, nIters);
420                                         delete rCurve;
421                                         
422                                         processedLabels.insert(order->getLabel());
423                                         userLabels.erase(order->getLabel());
424                                 }
425                                 
426                                 if ((m->anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
427                                         string saveLabel = order->getLabel();
428                                         
429                                         delete order;
430                                         order = (input->getOrderVector(lastLabel));
431                                         
432                                         m->mothurOut(order->getLabel()); m->mothurOutEndLine();
433                                         map<string, set<int> >::iterator itEndings = labelToEnds.find(order->getLabel());
434                     set<int> ends;
435                     if (itEndings != labelToEnds.end()) { ends = itEndings->second; }
436                                         rCurve = new Rarefact(order, rDisplays, processors, ends);
437
438                                         rCurve->getCurve(freq, nIters);
439                                         delete rCurve;
440                                         
441                                         processedLabels.insert(order->getLabel());
442                                         userLabels.erase(order->getLabel());
443                                         
444                                         //restore real lastlabel to save below
445                                         order->setLabel(saveLabel);
446                                 }
447                                 
448                                 lastLabel = order->getLabel();          
449                                 
450                                 delete order;
451                                 order = (input->getOrderVector());
452                         }
453                         
454                         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++) {       m->mothurRemove(outputNames[i]); } return 0; }
455
456                         //output error messages about any remaining user labels
457                         set<string>::iterator it;
458                         bool needToRun = false;
459                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
460                                 m->mothurOut("Your file does not include the label " + *it);
461                                 if (processedLabels.count(lastLabel) != 1) {
462                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
463                                         needToRun = true;
464                                 }else {
465                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
466                                 }
467                         }
468                         
469                         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++) {       m->mothurRemove(outputNames[i]); } return 0; }
470
471                         //run last label if you need to
472                         if (needToRun == true)  {
473                                 if (order != NULL) {    delete order;   }
474                                 order = (input->getOrderVector(lastLabel));
475                                 
476                                 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
477                                 map<string, set<int> >::iterator itEndings = labelToEnds.find(order->getLabel());
478                 set<int> ends;
479                 if (itEndings != labelToEnds.end()) { ends = itEndings->second; }
480                 rCurve = new Rarefact(order, rDisplays, processors, ends);
481
482                                 rCurve->getCurve(freq, nIters);
483                                 delete rCurve;
484                                 
485                                 delete order;
486                         }
487                         
488                         
489                         for(int i=0;i<rDisplays.size();i++){    delete rDisplays[i];    }       
490                         rDisplays.clear();
491                         delete input;  
492                 }
493                 
494                 
495                 if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       m->mothurRemove(outputNames[i]); } return 0; }
496
497                 //create summary file containing all the groups data for each label - this function just combines the info from the files already created.
498                 if ((sharedfile != "") && (groupMode)) {   outputNames = createGroupFile(outputNames, file2Group);  }
499
500                 if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       m->mothurRemove(outputNames[i]); } return 0; }
501
502                 m->mothurOutEndLine();
503                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
504                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
505                 m->mothurOutEndLine();
506
507                 return 0;
508         }
509         catch(exception& e) {
510                 m->errorOut(e, "RareFactCommand", "execute");
511                 exit(1);
512         }
513 }
514 //**********************************************************************************************************************
515 vector<string> RareFactCommand::createGroupFile(vector<string>& outputNames, map<int, string> file2Group) {
516         try {
517                 
518                 vector<string> newFileNames;
519                 
520                 //find different types of files
521                 map<string, map<string, string> > typesFiles;
522         map<string, vector< vector<string> > > fileLabels; //combofile name to labels. each label is a vector because it may be unique lci hci.
523         vector<string> groupNames;
524                 for (int i = 0; i < outputNames.size(); i++) {
525             
526                         string extension = m->getExtension(outputNames[i]);
527             string combineFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "groups" + extension;
528                         m->mothurRemove(combineFileName); //remove old file
529             
530                         ifstream in;
531                         m->openInputFile(outputNames[i], in);
532                         
533                         string labels = m->getline(in);
534             
535                         istringstream iss (labels,istringstream::in);
536             string newLabel = ""; vector<string> theseLabels;
537             while(!iss.eof()) {  iss >> newLabel; m->gobble(iss); theseLabels.push_back(newLabel); }
538             vector< vector<string> > allLabels;
539             vector<string> thisSet; thisSet.push_back(theseLabels[0]); allLabels.push_back(thisSet); thisSet.clear(); //makes "numSampled" its own grouping
540             for (int j = 1; j < theseLabels.size()-1; j++) {
541                 if (theseLabels[j+1] == "lci") {
542                     thisSet.push_back(theseLabels[j]); 
543                     thisSet.push_back(theseLabels[j+1]); 
544                     thisSet.push_back(theseLabels[j+2]);
545                     j++; j++;
546                 }else{ //no lci or hci for this calc.
547                     thisSet.push_back(theseLabels[j]); 
548                 }
549                 allLabels.push_back(thisSet); 
550                 thisSet.clear();
551             }
552             fileLabels[combineFileName] = allLabels;
553                     
554             map<string, map<string, string> >::iterator itfind = typesFiles.find(extension);
555             if (itfind != typesFiles.end()) {
556                 (itfind->second)[outputNames[i]] = file2Group[i];
557             }else {
558                 map<string, string> temp;  
559                 temp[outputNames[i]] = file2Group[i];
560                 typesFiles[extension] = temp;
561             }
562             if (!(m->inUsersGroups(file2Group[i], groupNames))) {  groupNames.push_back(file2Group[i]); }
563                 }
564                 
565                 //for each type create a combo file
566                 
567                 for (map<string, map<string, string> >::iterator it = typesFiles.begin(); it != typesFiles.end(); it++) {
568                         
569                         ofstream out;
570                         string combineFileName = outputDir + m->getRootName(m->getSimpleName(sharedfile)) + "groups" + it->first;
571                         m->openOutputFileAppend(combineFileName, out);
572                         newFileNames.push_back(combineFileName);
573                         map<string, string> thisTypesFiles = it->second; //it->second maps filename to group
574             set<int> numSampledSet;
575             
576                         //open each type summary file
577                         map<string, map<int, vector< vector<string> > > > files; //maps file name to lines in file
578                         int maxLines = 0;
579                         for (map<string, string>::iterator itFileNameGroup = thisTypesFiles.begin(); itFileNameGroup != thisTypesFiles.end(); itFileNameGroup++) {
580                 
581                 string thisfilename = itFileNameGroup->first;
582                 string group = itFileNameGroup->second;
583                 
584                                 ifstream temp;
585                                 m->openInputFile(thisfilename, temp);
586                                 
587                                 //read through first line - labels
588                                 m->getline(temp);       m->gobble(temp);
589                                 
590                                 map<int, vector< vector<string> > > thisFilesLines;
591                                 while (!temp.eof()){
592                     int numSampled = 0;
593                     temp >> numSampled; m->gobble(temp);
594                 
595                     vector< vector<string> > theseReads;
596                     vector<string> thisSet; thisSet.push_back(toString(numSampled)); theseReads.push_back(thisSet); thisSet.clear();
597                     for (int k = 1; k < fileLabels[combineFileName].size(); k++) { //output thing like 0.03-A lci-A hci-A
598                         vector<string> reads;
599                         string next = "";
600                         for (int l = 0; l < fileLabels[combineFileName][k].size(); l++) { //output modified labels
601                             temp >> next; m->gobble(temp);
602                             reads.push_back(next);
603                         }
604                         theseReads.push_back(reads);
605                     }
606                     thisFilesLines[numSampled] = theseReads;
607                     m->gobble(temp);
608                    
609                     numSampledSet.insert(numSampled);
610                                 }
611                                 
612                                 files[group] = thisFilesLines;
613                                 
614                                 //save longest file for below
615                                 if (maxLines < thisFilesLines.size()) { maxLines = thisFilesLines.size(); }
616                                 
617                                 temp.close();
618                                 m->mothurRemove(thisfilename);
619                         }
620                         
621             //output new labels line
622             out << fileLabels[combineFileName][0][0] << '\t';
623             for (int k = 1; k < fileLabels[combineFileName].size(); k++) { //output thing like 0.03-A lci-A hci-A
624                 for (int n = 0; n < groupNames.size(); n++) { // for each group
625                     for (int l = 0; l < fileLabels[combineFileName][k].size(); l++) { //output modified labels
626                         out << fileLabels[combineFileName][k][l] << '-' << groupNames[n] << '\t';
627                     }
628                 }
629             }
630                         out << endl;
631             
632                         //for each label
633                         for (set<int>::iterator itNumSampled = numSampledSet.begin(); itNumSampled != numSampledSet.end(); itNumSampled++) {
634                                 
635                 out << (*itNumSampled) << '\t';
636                                
637                 if (m->control_pressed) { break; }
638                 
639                 for (int k = 1; k < fileLabels[combineFileName].size(); k++) { //each chunk
640                                     //grab data for each group
641                     for (map<string, map<int, vector< vector<string> > > >::iterator itFileNameGroup = files.begin(); itFileNameGroup != files.end(); itFileNameGroup++) {
642                         
643                         string group = itFileNameGroup->first;
644                        
645                         map<int, vector< vector<string> > >::iterator itLine = files[group].find(*itNumSampled);
646                         if (itLine != files[group].end()) { 
647                             for (int l = 0; l < (itLine->second)[k].size(); l++) { 
648                                 out << (itLine->second)[k][l] << '\t';
649                                
650                             }                             
651                         }else { 
652                             for (int l = 0; l < fileLabels[combineFileName][k].size(); l++) { 
653                                 out << "NA" << '\t';
654                             } 
655                         }
656                     }
657                 }
658                 out << endl;
659                         }       
660                         out.close();
661                 }
662                 
663                 //return combine file name
664                 return newFileNames;
665                 
666         }
667         catch(exception& e) {
668                 m->errorOut(e, "RareFactCommand", "createGroupFile");
669                 exit(1);
670         }
671 }
672 //**********************************************************************************************************************
673 vector<string> RareFactCommand::parseSharedFile(string filename, map<string, set<int> >& label2Ends) {
674         try {
675                 vector<string> filenames;
676                 
677                 map<string, ofstream*> filehandles;
678                 map<string, ofstream*>::iterator it3;
679                 
680                 input = new InputData(filename, "sharedfile");
681                 vector<SharedRAbundVector*> lookup = input->getSharedRAbundVectors();
682                 
683                 string sharedFileRoot = m->getRootName(filename);
684                 
685                 //clears file before we start to write to it below
686                 for (int i=0; i<lookup.size(); i++) {
687                         m->mothurRemove((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
688                         filenames.push_back((sharedFileRoot + lookup[i]->getGroup() + ".rabund"));
689                 }
690                 
691                 ofstream* temp;
692                 for (int i=0; i<lookup.size(); i++) {
693                         temp = new ofstream;
694                         filehandles[lookup[i]->getGroup()] = temp;
695                         groups.push_back(lookup[i]->getGroup());
696                 }
697
698                 while(lookup[0] != NULL) {
699                 
700                         for (int i = 0; i < lookup.size(); i++) {
701                                 RAbundVector rav = lookup[i]->getRAbundVector();
702                                 m->openOutputFileAppend(sharedFileRoot + lookup[i]->getGroup() + ".rabund", *(filehandles[lookup[i]->getGroup()]));
703                                 rav.print(*(filehandles[lookup[i]->getGroup()]));
704                                 (*(filehandles[lookup[i]->getGroup()])).close();
705                 label2Ends[lookup[i]->getLabel()].insert(rav.getNumSeqs());
706                         }
707                 
708                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
709                         lookup = input->getSharedRAbundVectors();
710                 }
711                 
712                 //free memory
713                 for (it3 = filehandles.begin(); it3 != filehandles.end(); it3++) {
714                         delete it3->second;
715                 }
716                 
717                 delete input;
718                 m->clearGroups();
719
720                 return filenames;
721         }
722         catch(exception& e) {
723                 m->errorOut(e, "RareFactCommand", "parseSharedFile");
724                 exit(1);
725         }
726 }
727 //**********************************************************************************************************************
728
729
730