]> git.donarmstrong.com Git - mothur.git/blob - collectsharedcommand.cpp
fixes while testing 1.33.0
[mothur.git] / collectsharedcommand.cpp
1 /*
2  *  collectsharedcommand.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 "collectsharedcommand.h"
11 #include "sharedsobscollectsummary.h"
12 #include "sharedchao1.h"
13 #include "sharedace.h"
14 #include "sharedjabund.h"
15 #include "sharedsorabund.h"
16 #include "sharedjclass.h"
17 #include "sharedsorclass.h"
18 #include "sharedjest.h"
19 #include "sharedsorest.h"
20 #include "sharedthetayc.h"
21 #include "sharedthetan.h"
22 #include "sharedkstest.h"
23 #include "whittaker.h"
24 #include "sharednseqs.h"
25 #include "sharedochiai.h"
26 #include "sharedanderbergs.h"
27 #include "sharedkulczynski.h"
28 #include "sharedkulczynskicody.h"
29 #include "sharedlennon.h"
30 #include "sharedmorisitahorn.h"
31 #include "sharedbraycurtis.h"
32 #include "sharedjackknife.h"
33 #include "whittaker.h"
34 #include "odum.h"
35 #include "canberra.h"
36 #include "structeuclidean.h"
37 #include "structchord.h"
38 #include "hellinger.h"
39 #include "manhattan.h"
40 #include "structpearson.h"
41 #include "soergel.h"
42 #include "spearman.h"
43 #include "structkulczynski.h"
44 #include "structchi2.h"
45 #include "speciesprofile.h"
46 #include "hamming.h"
47 #include "gower.h"
48 #include "memchi2.h"
49 #include "memchord.h"
50 #include "memeuclidean.h"
51 #include "mempearson.h"
52 #include "sharedjsd.h"
53 #include "sharedrjsd.h"
54
55
56 //**********************************************************************************************************************
57 vector<string> CollectSharedCommand::setParameters(){   
58         try {
59                 CommandParameter pshared("shared", "InputTypes", "", "", "none", "none", "none","",false,true,true); parameters.push_back(pshared);
60                 CommandParameter plabel("label", "String", "", "", "", "", "","",false,false); parameters.push_back(plabel);
61                 CommandParameter pfreq("freq", "Number", "", "100", "", "", "","",false,false); parameters.push_back(pfreq);
62                 CommandParameter pcalc("calc", "Multiple", "sharedchao-sharedsobs-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan-kstest-whittaker-sharednseqs-ochiai-anderberg-kulczynski-kulczynskicody-lennon-morisitahorn-braycurtis-odum-canberra-structeuclidean-structchord-hellinger-manhattan-structpearson-soergel-spearman-structkulczynski-speciesprofile-structchi2-hamming-gower-memchi2-memchord-memeuclidean-mempearson-jsd-rjsd", "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan", "", "", "","",true,false,true); parameters.push_back(pcalc);
63                 CommandParameter pall("all", "Boolean", "", "F", "", "", "","",false,false); parameters.push_back(pall);
64                 CommandParameter pgroups("groups", "String", "", "", "", "", "","",false,false); parameters.push_back(pgroups);
65                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
66                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
67                 
68                 vector<string> myArray;
69                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
70                 return myArray;
71         }
72         catch(exception& e) {
73                 m->errorOut(e, "CollectSharedCommand", "setParameters");
74                 exit(1);
75         }
76 }
77 //**********************************************************************************************************************
78 string CollectSharedCommand::getHelpString(){   
79         try {
80                 string helpString = "";
81                 ValidCalculators validCalculator;
82                 helpString += "The collect.shared command parameters are shared, label, freq, calc and groups.  shared is required if there is no current sharedfile. \n";
83                 helpString += "The collect.shared command should be in the following format: \n";
84                 helpString += "collect.shared(label=yourLabel, freq=yourFreq, calc=yourEstimators, groups=yourGroups).\n";
85                 helpString += "Example collect.shared(label=unique-.01-.03, freq=10, groups=B-C, calc=sharedchao-sharedace-jabund-sorensonabund-jclass-sorclass-jest-sorest-thetayc-thetan).\n";
86                 helpString += "The default values for freq is 100 and calc are sharedsobs-sharedchao-sharedace-jabund-sorensonabund-jclass-sorclass-jest-sorest-thetayc-thetan.\n";
87                 helpString += "The default value for groups is all the groups in your groupfile.\n";
88                 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";
89                 helpString += validCalculator.printCalc("shared");
90                 helpString += "The label parameter is used to analyze specific labels in your input.\n";
91                 helpString += "The all parameter is used to specify if you want the estimate of all your groups together.  This estimate can only be made for sharedsobs and sharedchao calculators. The default is false.\n";
92                 helpString += "If you use sharedchao and run into memory issues, set all to false. \n";
93                 helpString += "The groups parameter allows you to specify which of the groups in your groupfile you would like analyzed.  You must enter at least 2 valid groups.\n";
94                 helpString += "Note: No spaces between parameter labels (i.e. shared), '=' and parameters (i.e.yourSharedfile).\n";
95                 return helpString;
96         }
97         catch(exception& e) {
98                 m->errorOut(e, "CollectSharedCommand", "getHelpString");
99                 exit(1);
100         }
101 }
102 //**********************************************************************************************************************
103 string CollectSharedCommand::getOutputPattern(string type) {
104     try {
105         string pattern = "";
106         
107         if (type == "sharedchao")               {  pattern =  "[filename],shared.chao";     }
108         else if (type == "sharedsobs")          {  pattern =  "[filename],shared.sobs";     }
109         else if (type == "sharedace")           {  pattern =  "[filename],shared.ace";      }
110         else if (type == "jabund")              {  pattern =  "[filename],jabund";          }
111         else if (type == "sorabund")            {  pattern =  "[filename],sorabund";        }
112         else if (type == "jclass")              {  pattern =  "[filename],jclass";          }
113         else if (type == "sorclass")            {  pattern =  "[filename],sorclass";        }
114         else if (type == "jest")                {  pattern =  "[filename],jest";            }
115         else if (type == "sorest")              {  pattern =  "[filename],sorest";          }
116         else if (type == "thetayc")             {  pattern =  "[filename],thetayc";         }
117         else if (type == "thetan")              {  pattern =  "[filename],thetan";          }
118         else if (type == "kstest")              {  pattern =  "[filename],kstest";          }
119         else if (type == "whittaker")           {  pattern =  "[filename],whittaker";       }
120         else if (type == "sharednseqs")         {  pattern =  "[filename],shared.nseqs";    }
121         else if (type == "ochiai")              {  pattern =  "[filename],ochiai";          }
122         else if (type == "anderberg")           {  pattern =  "[filename],anderberg";       }
123         else if (type == "kulczynski")          {  pattern =  "[filename],kulczynski";      }
124         else if (type == "kulczynskicody")      {  pattern =  "[filename],kulczynskicody";  }
125         else if (type == "lennon")              {  pattern =  "[filename],lennon";          }
126         else if (type == "morisitahorn")        {  pattern =  "[filename],morisitahorn";    }
127         else if (type == "braycurtis")          {  pattern =  "[filename],braycurtis";      }
128         else if (type == "odum")                {  pattern =  "[filename],odum";            }
129         else if (type == "canberra")            {  pattern =  "[filename],canberra";        }
130         else if (type == "structeuclidean")     {  pattern =  "[filename],structeuclidean"; }
131         else if (type == "structchord")         {  pattern =  "[filename],structchord";     }
132         else if (type == "hellinger")           {  pattern =  "[filename],hellinger";       }
133         else if (type == "manhattan")           {  pattern =  "[filename],manhattan";       }
134         else if (type == "structpearson")       {  pattern =  "[filename],structpearson";   }
135         else if (type == "soergel")             {  pattern =  "[filename],soergel";         }
136         else if (type == "spearman")            {  pattern =  "[filename],spearman";        }
137         else if (type == "structkulczynski")    {  pattern =  "[filename],structkulczynski";}
138         else if (type == "structchi2")          {  pattern =  "[filename],structchi2";      }
139         else if (type == "speciesprofile")      {  pattern =  "[filename],speciesprofile";  }
140         else if (type == "hamming")             {  pattern =  "[filename],hamming";         }
141         else if (type == "gower")               {  pattern =  "[filename],gower";           }
142         else if (type == "memchi2")             {  pattern =  "[filename],memchi2";         }
143         else if (type == "memchord")            {  pattern =  "[filename],memchord";        }
144         else if (type == "memeuclidean")        {  pattern =  "[filename],memeuclidean";    }
145         else if (type == "mempearson")          {  pattern =  "[filename],mempearson";      }
146         else if (type == "jsd")                 {  pattern =  "[filename],jsd";             }
147         else if (type == "rjsd")                 {  pattern =  "[filename],rjsd";             }
148         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
149         
150         return pattern;
151     }
152     catch(exception& e) {
153         m->errorOut(e, "CollectSharedCommand", "getOutputPattern");
154         exit(1);
155     }
156 }
157
158 //**********************************************************************************************************************
159 CollectSharedCommand::CollectSharedCommand(){   
160         try {
161                 abort = true; calledHelp = true; 
162                 setParameters();
163                 vector<string> tempOutNames;
164                 outputTypes["sharedchao"] = tempOutNames;
165                 outputTypes["sharedsobs"] = tempOutNames;
166                 outputTypes["sharedace"] = tempOutNames;
167                 outputTypes["jabund"] = tempOutNames;
168                 outputTypes["sorabund"] = tempOutNames;
169                 outputTypes["jclass"] = tempOutNames;
170                 outputTypes["sorclass"] = tempOutNames;
171                 outputTypes["jest"] = tempOutNames;
172                 outputTypes["sorest"] = tempOutNames;
173                 outputTypes["thetayc"] = tempOutNames;
174                 outputTypes["thetan"] = tempOutNames;
175                 outputTypes["kstest"] = tempOutNames;
176                 outputTypes["whittaker"] = tempOutNames;
177                 outputTypes["sharednseqs"] = tempOutNames;
178                 outputTypes["ochiai"] = tempOutNames;
179                 outputTypes["anderberg"] = tempOutNames;
180                 outputTypes["kulczynski"] = tempOutNames;
181                 outputTypes["kulczynskicody"] = tempOutNames;
182                 outputTypes["lennon"] = tempOutNames;
183                 outputTypes["morisitahorn"] = tempOutNames;
184                 outputTypes["braycurtis"] = tempOutNames;
185                 outputTypes["odum"] = tempOutNames;
186                 outputTypes["canberra"] = tempOutNames;
187                 outputTypes["structeuclidean"] = tempOutNames;
188                 outputTypes["structchord"] = tempOutNames;
189                 outputTypes["hellinger"] = tempOutNames;
190                 outputTypes["manhattan"] = tempOutNames;
191                 outputTypes["structpearson"] = tempOutNames;
192                 outputTypes["soergel"] = tempOutNames;
193                 outputTypes["spearman"] = tempOutNames;
194                 outputTypes["structkulczynski"] = tempOutNames;
195                 outputTypes["structchi2"] = tempOutNames;
196                 outputTypes["speciesprofile"] = tempOutNames;
197                 outputTypes["hamming"] = tempOutNames;
198                 outputTypes["gower"] = tempOutNames;
199                 outputTypes["memchi2"] = tempOutNames;
200                 outputTypes["memchord"] = tempOutNames;
201                 outputTypes["memeuclidean"] = tempOutNames;
202                 outputTypes["mempearson"] = tempOutNames;
203         outputTypes["jsd"] = tempOutNames;
204         outputTypes["rjsd"] = tempOutNames;
205                 
206         }
207         catch(exception& e) {
208                 m->errorOut(e, "CollectSharedCommand", "CollectSharedCommand");
209                 exit(1);
210         }
211 }
212 //**********************************************************************************************************************
213 CollectSharedCommand::CollectSharedCommand(string option)  {
214         try {
215                 abort = false; calledHelp = false;   
216                 allLines = 1;
217                 
218                 //allow user to run help
219                 if(option == "help") { help(); abort = true; calledHelp = true; }
220                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
221                 
222                 else {
223                         vector<string> myArray = setParameters();
224                         
225                         OptionParser parser(option);
226                         map<string,string> parameters=parser.getParameters();
227                         map<string,string>::iterator it;
228                         
229                         ValidParameters validParameter;
230                 
231                         //check to make sure all parameters are valid for command
232                         for (it = parameters.begin(); it != parameters.end(); it++) { 
233                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
234                         }
235         
236                         //initialize outputTypes
237                         vector<string> tempOutNames;
238                         outputTypes["sharedchao"] = tempOutNames;
239                         outputTypes["sharedsobs"] = tempOutNames;
240                         outputTypes["sharedace"] = tempOutNames;
241                         outputTypes["jabund"] = tempOutNames;
242                         outputTypes["sorabund"] = tempOutNames;
243                         outputTypes["jclass"] = tempOutNames;
244                         outputTypes["sorclass"] = tempOutNames;
245                         outputTypes["jest"] = tempOutNames;
246                         outputTypes["sorest"] = tempOutNames;
247                         outputTypes["thetayc"] = tempOutNames;
248                         outputTypes["thetan"] = tempOutNames;
249                         outputTypes["kstest"] = tempOutNames;
250                         outputTypes["whittaker"] = tempOutNames;
251                         outputTypes["sharednseqs"] = tempOutNames;
252                         outputTypes["ochiai"] = tempOutNames;
253                         outputTypes["anderberg"] = tempOutNames;
254                         outputTypes["kulczynski"] = tempOutNames;
255                         outputTypes["kulczynskicody"] = tempOutNames;
256                         outputTypes["lennon"] = tempOutNames;
257                         outputTypes["morisitahorn"] = tempOutNames;
258                         outputTypes["braycurtis"] = tempOutNames;
259                         outputTypes["odum"] = tempOutNames;
260                         outputTypes["canberra"] = tempOutNames;
261                         outputTypes["structeuclidean"] = tempOutNames;
262                         outputTypes["structchord"] = tempOutNames;
263                         outputTypes["hellinger"] = tempOutNames;
264                         outputTypes["manhattan"] = tempOutNames;
265                         outputTypes["structpearson"] = tempOutNames;
266                         outputTypes["soergel"] = tempOutNames;
267                         outputTypes["spearman"] = tempOutNames;
268                         outputTypes["structkulczynski"] = tempOutNames;
269                         outputTypes["speciesprofile"] = tempOutNames;
270                         outputTypes["structchi2"] = tempOutNames;
271                         outputTypes["hamming"] = tempOutNames;
272                         outputTypes["gower"] = tempOutNames;
273                         outputTypes["memchi2"] = tempOutNames;
274                         outputTypes["memchord"] = tempOutNames;
275                         outputTypes["memeuclidean"] = tempOutNames;
276                         outputTypes["mempearson"] = tempOutNames;
277             outputTypes["jsd"] = tempOutNames;
278             outputTypes["rjsd"] = tempOutNames;
279                         
280                         
281                         //if the user changes the input directory command factory will send this info to us in the output parameter 
282                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
283                         if (inputDir == "not found"){   inputDir = "";          }
284                         else {
285                                 string path;
286                                 it = parameters.find("shared");
287                                 //user has given a template file
288                                 if(it != parameters.end()){ 
289                                         path = m->hasPath(it->second);
290                                         //if the user has not given a path then, add inputdir. else leave path alone.
291                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
292                                 }
293                         }
294                         
295                         //get shared file
296                         sharedfile = validParameter.validFile(parameters, "shared", true);
297                         if (sharedfile == "not open") { sharedfile = ""; abort = true; }        
298                         else if (sharedfile == "not found") { 
299                                 //if there is a current shared file, use it
300                                 sharedfile = m->getSharedFile(); 
301                                 if (sharedfile != "") { m->mothurOut("Using " + sharedfile + " as input file for the shared parameter."); m->mothurOutEndLine(); }
302                                 else {  m->mothurOut("You have no current sharedfile and the shared parameter is required."); m->mothurOutEndLine(); abort = true; }
303                         }else { m->setSharedFile(sharedfile); }
304                         
305                         
306                         //if the user changes the output directory command factory will send this info to us in the output parameter 
307                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(sharedfile);             }
308                         
309                         //check for optional parameter and set defaults
310                         // ...at some point should added some additional type checking..
311                         label = validParameter.validFile(parameters, "label", false);                   
312                         if (label == "not found") { label = ""; }
313                         else { 
314                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
315                                 else { allLines = 1;  }
316                         }
317                         
318                         calc = validParameter.validFile(parameters, "calc", false);                     
319                         if (calc == "not found") { calc = "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan";  }
320                         else { 
321                                  if (calc == "default")  {  calc = "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan";  }
322                         }
323                         m->splitAtDash(calc, Estimators);
324                         if (m->inUsersGroups("citation", Estimators)) { 
325                                 ValidCalculators validCalc; validCalc.printCitations(Estimators); 
326                                 //remove citation from list of calcs
327                                 for (int i = 0; i < Estimators.size(); i++) { if (Estimators[i] == "citation") {  Estimators.erase(Estimators.begin()+i); break; } }
328                         }
329                         
330                         groups = validParameter.validFile(parameters, "groups", false);                 
331                         if (groups == "not found") { groups = ""; }
332                         else { 
333                                 m->splitAtDash(groups, Groups);
334                         }
335                         m->setGroups(Groups);
336                         
337                         string temp;
338                         temp = validParameter.validFile(parameters, "freq", false);                     if (temp == "not found") { temp = "100"; }
339                         m->mothurConvert(temp, freq); 
340                         
341                         temp = validParameter.validFile(parameters, "all", false);                              if (temp == "not found") { temp = "false"; }
342                         all = m->isTrue(temp);
343                                                 
344                         if (abort == false) {
345                                 
346                                 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(sharedfile));
347                                 map<string, string> variables; 
348                 variables["[filename]"] = fileNameRoot;
349                 
350                                 ValidCalculators validCalculator;
351                                 
352                                 for (int i=0; i<Estimators.size(); i++) {
353                                         if (validCalculator.isValidCalculator("shared", Estimators[i]) == true) { 
354                                                 if (Estimators[i] == "sharedchao") { 
355                                                         cDisplays.push_back(new CollectDisplay(new SharedChao1(), new SharedOneColumnFile(getOutputFileName("sharedchao", variables))));
356                                                         outputNames.push_back(getOutputFileName("sharedchao", variables)); outputTypes["sharedchao"].push_back(getOutputFileName("sharedchao", variables));
357                                                 }else if (Estimators[i] == "sharedsobs") { 
358                                                         cDisplays.push_back(new CollectDisplay(new SharedSobsCS(), new SharedOneColumnFile(getOutputFileName("sharedsobs", variables))));
359                                                         outputNames.push_back(getOutputFileName("sharedsobs", variables)); outputTypes["sharedsobs"].push_back(getOutputFileName("sharedsobs", variables));
360                                                 }else if (Estimators[i] == "sharedace") { 
361                                                         cDisplays.push_back(new CollectDisplay(new SharedAce(), new SharedOneColumnFile(getOutputFileName("sharedace", variables))));
362                                                         outputNames.push_back(getOutputFileName("sharedace", variables)); outputTypes["sharedace"].push_back(getOutputFileName("sharedace", variables));
363                                                 }else if (Estimators[i] == "jabund") {  
364                                                         cDisplays.push_back(new CollectDisplay(new JAbund(), new SharedOneColumnFile(getOutputFileName("jabund", variables))));
365                                                         outputNames.push_back(getOutputFileName("jabund", variables)); outputTypes["jabund"].push_back(getOutputFileName("jabund", variables));
366                                                 }else if (Estimators[i] == "sorabund") { 
367                                                         cDisplays.push_back(new CollectDisplay(new SorAbund(), new SharedOneColumnFile(getOutputFileName("sorabund", variables))));
368                                                         outputNames.push_back(getOutputFileName("sorabund", variables)); outputTypes["sorabund"].push_back(getOutputFileName("sorabund", variables));
369                                                 }else if (Estimators[i] == "jclass") { 
370                                                         cDisplays.push_back(new CollectDisplay(new Jclass(), new SharedOneColumnFile(getOutputFileName("jclass", variables))));
371                                                         outputNames.push_back(getOutputFileName("jclass", variables)); outputTypes["jclass"].push_back(getOutputFileName("jclass", variables));
372                                                 }else if (Estimators[i] == "sorclass") { 
373                                                         cDisplays.push_back(new CollectDisplay(new SorClass(), new SharedOneColumnFile(getOutputFileName("sorclass", variables))));
374                                                         outputNames.push_back(getOutputFileName("sorclass", variables)); outputTypes["sorclass"].push_back(getOutputFileName("sorclass", variables));
375                                                 }else if (Estimators[i] == "jest") { 
376                                                         cDisplays.push_back(new CollectDisplay(new Jest(), new SharedOneColumnFile(getOutputFileName("jest", variables))));
377                                                         outputNames.push_back(getOutputFileName("jest", variables)); outputTypes["jest"].push_back(getOutputFileName("jest", variables));
378                                                 }else if (Estimators[i] == "sorest") { 
379                                                         cDisplays.push_back(new CollectDisplay(new SorEst(), new SharedOneColumnFile(getOutputFileName("sorest", variables))));
380                                                         outputNames.push_back(getOutputFileName("sorest", variables)); outputTypes["sorest"].push_back(getOutputFileName("sorest", variables));
381                                                 }else if (Estimators[i] == "thetayc") { 
382                                                         cDisplays.push_back(new CollectDisplay(new ThetaYC(), new SharedOneColumnFile(getOutputFileName("thetayc", variables))));
383                                                         outputNames.push_back(getOutputFileName("thetayc", variables)); outputTypes["thetayc"].push_back(getOutputFileName("thetayc", variables));
384                                                 }else if (Estimators[i] == "thetan") { 
385                                                         cDisplays.push_back(new CollectDisplay(new ThetaN(), new SharedOneColumnFile(getOutputFileName("thetan", variables))));
386                                                         outputNames.push_back(getOutputFileName("thetan", variables)); outputTypes["thetan"].push_back(getOutputFileName("thetan", variables));
387                                                 }else if (Estimators[i] == "kstest") { 
388                                                         cDisplays.push_back(new CollectDisplay(new KSTest(), new SharedOneColumnFile(getOutputFileName("kstest", variables))));
389                                                         outputNames.push_back(getOutputFileName("kstest", variables)); outputTypes["kstest"].push_back(getOutputFileName("kstest", variables));
390                                                 }else if (Estimators[i] == "whittaker") { 
391                                                         cDisplays.push_back(new CollectDisplay(new Whittaker(), new SharedOneColumnFile(getOutputFileName("whittaker", variables))));
392                                                         outputNames.push_back(getOutputFileName("whittaker", variables)); outputTypes["whittaker"].push_back(getOutputFileName("whittaker", variables));
393                                                 }else if (Estimators[i] == "sharednseqs") { 
394                                                         cDisplays.push_back(new CollectDisplay(new SharedNSeqs(), new SharedOneColumnFile(getOutputFileName("sharednseqs", variables))));
395                                                         outputNames.push_back(getOutputFileName("sharednseqs", variables)); outputTypes["shared.nseqs"].push_back(getOutputFileName("sharednseqs", variables));
396                                                 }else if (Estimators[i] == "ochiai") { 
397                                                         cDisplays.push_back(new CollectDisplay(new Ochiai(), new SharedOneColumnFile(getOutputFileName("ochiai", variables))));
398                                                         outputNames.push_back(getOutputFileName("ochiai", variables)); outputTypes["ochiai"].push_back(getOutputFileName("ochiai", variables));
399                                                 }else if (Estimators[i] == "anderberg") { 
400                                                         cDisplays.push_back(new CollectDisplay(new Anderberg(), new SharedOneColumnFile(getOutputFileName("anderberg", variables))));
401                                                         outputNames.push_back(getOutputFileName("anderberg", variables)); outputTypes["anderberg"].push_back(getOutputFileName("anderberg", variables));
402                                                 }else if (Estimators[i] == "kulczynski") { 
403                                                         cDisplays.push_back(new CollectDisplay(new Kulczynski(), new SharedOneColumnFile(getOutputFileName("kulczynski", variables))));
404                                                         outputNames.push_back(getOutputFileName("kulczynski", variables)); outputTypes["kulczynski"].push_back(getOutputFileName("kulczynski", variables));
405                                                 }else if (Estimators[i] == "kulczynskicody") { 
406                                                         cDisplays.push_back(new CollectDisplay(new KulczynskiCody(), new SharedOneColumnFile(getOutputFileName("kulczynskicody", variables))));
407                                                         outputNames.push_back(getOutputFileName("kulczynskicody", variables)); outputTypes["kulczynskicody"].push_back(getOutputFileName("kulczynskicody", variables));
408                                                 }else if (Estimators[i] == "lennon") { 
409                                                         cDisplays.push_back(new CollectDisplay(new Lennon(), new SharedOneColumnFile(getOutputFileName("lennon", variables))));
410                                                         outputNames.push_back(getOutputFileName("lennon", variables)); outputTypes["lennon"].push_back(getOutputFileName("lennon", variables));
411                                                 }else if (Estimators[i] == "morisitahorn") { 
412                                                         cDisplays.push_back(new CollectDisplay(new MorHorn(), new SharedOneColumnFile(getOutputFileName("morisitahorn", variables))));
413                                                         outputNames.push_back(getOutputFileName("morisitahorn", variables)); outputTypes["morisitahorn"].push_back(getOutputFileName("morisitahorn", variables));
414                                                 }else if (Estimators[i] == "braycurtis") { 
415                                                         cDisplays.push_back(new CollectDisplay(new BrayCurtis(), new SharedOneColumnFile(getOutputFileName("braycurtis", variables))));
416                                                         outputNames.push_back(getOutputFileName("braycurtis", variables)); outputTypes["braycurtis"].push_back(getOutputFileName("braycurtis", variables));
417                                                 }else if (Estimators[i] == "odum") { 
418                                                         cDisplays.push_back(new CollectDisplay(new Odum(), new SharedOneColumnFile(getOutputFileName("odum", variables))));
419                                                         outputNames.push_back(getOutputFileName("odum", variables)); outputTypes["odum"].push_back(getOutputFileName("odum", variables));
420                                                 }else if (Estimators[i] == "canberra") { 
421                                                         cDisplays.push_back(new CollectDisplay(new Canberra(), new SharedOneColumnFile(getOutputFileName("canberra", variables))));
422                                                         outputNames.push_back(getOutputFileName("canberra", variables)); outputTypes["canberra"].push_back(getOutputFileName("canberra", variables));
423                                                 }else if (Estimators[i] == "structeuclidean") { 
424                                                         cDisplays.push_back(new CollectDisplay(new StructEuclidean(), new SharedOneColumnFile(getOutputFileName("structeuclidean", variables))));
425                                                         outputNames.push_back(getOutputFileName("structeuclidean", variables)); outputTypes["structeuclidean"].push_back(getOutputFileName("structeuclidean", variables));
426                                                 }else if (Estimators[i] == "structchord") { 
427                                                         cDisplays.push_back(new CollectDisplay(new StructChord(), new SharedOneColumnFile(getOutputFileName("structchord", variables))));
428                                                         outputNames.push_back(getOutputFileName("structchord", variables)); outputTypes["structchord"].push_back(getOutputFileName("structchord", variables));
429                                                 }else if (Estimators[i] == "hellinger") { 
430                                                         cDisplays.push_back(new CollectDisplay(new Hellinger(), new SharedOneColumnFile(getOutputFileName("hellinger", variables))));
431                                                         outputNames.push_back(getOutputFileName("hellinger", variables)); outputTypes["hellinger"].push_back(getOutputFileName("hellinger", variables));
432                                                 }else if (Estimators[i] == "manhattan") { 
433                                                         cDisplays.push_back(new CollectDisplay(new Manhattan(), new SharedOneColumnFile(getOutputFileName("manhattan", variables))));
434                                                         outputNames.push_back(getOutputFileName("manhattan", variables)); outputTypes["manhattan"].push_back(getOutputFileName("manhattan", variables));
435                                                 }else if (Estimators[i] == "structpearson") { 
436                                                         cDisplays.push_back(new CollectDisplay(new StructPearson(), new SharedOneColumnFile(getOutputFileName("structpearson", variables))));
437                                                         outputNames.push_back(getOutputFileName("structpearson", variables)); outputTypes["structpearson"].push_back(getOutputFileName("structpearson", variables));
438                                                 }else if (Estimators[i] == "soergel") { 
439                                                         cDisplays.push_back(new CollectDisplay(new Soergel(), new SharedOneColumnFile(getOutputFileName("soergel", variables))));
440                                                         outputNames.push_back(getOutputFileName("soergel", variables)); outputTypes["soergel"].push_back(getOutputFileName("soergel", variables));
441                                                 }else if (Estimators[i] == "spearman") { 
442                                                         cDisplays.push_back(new CollectDisplay(new Spearman(), new SharedOneColumnFile(getOutputFileName("spearman", variables))));
443                                                         outputNames.push_back(getOutputFileName("spearman", variables)); outputTypes["spearman"].push_back(getOutputFileName("spearman", variables));
444                                                 }else if (Estimators[i] == "structkulczynski") { 
445                                                         cDisplays.push_back(new CollectDisplay(new StructKulczynski(), new SharedOneColumnFile(getOutputFileName("structkulczynski", variables))));
446                                                         outputNames.push_back(getOutputFileName("structkulczynski", variables)); outputTypes["structkulczynski"].push_back(getOutputFileName("structkulczynski", variables));
447                                                 }else if (Estimators[i] == "speciesprofile") { 
448                                                         cDisplays.push_back(new CollectDisplay(new SpeciesProfile(), new SharedOneColumnFile(getOutputFileName("speciesprofile", variables))));
449                                                         outputNames.push_back(getOutputFileName("speciesprofile", variables)); outputTypes["speciesprofile"].push_back(getOutputFileName("speciesprofile", variables));
450                                                 }else if (Estimators[i] == "hamming") { 
451                                                         cDisplays.push_back(new CollectDisplay(new Hamming(), new SharedOneColumnFile(getOutputFileName("hamming", variables))));
452                                                         outputNames.push_back(getOutputFileName("hamming", variables)); outputTypes["hamming"].push_back(getOutputFileName("hamming", variables));
453                                                 }else if (Estimators[i] == "structchi2") { 
454                                                         cDisplays.push_back(new CollectDisplay(new StructChi2(), new SharedOneColumnFile(getOutputFileName("structchi2", variables))));
455                                                         outputNames.push_back(getOutputFileName("structchi2", variables)); outputTypes["structchi2"].push_back(getOutputFileName("structchi2", variables));
456                                                 }else if (Estimators[i] == "gower") { 
457                                                         cDisplays.push_back(new CollectDisplay(new Gower(), new SharedOneColumnFile(getOutputFileName("gower", variables))));
458                                                         outputNames.push_back(getOutputFileName("gower", variables)); outputTypes["gower"].push_back(getOutputFileName("gower", variables));
459                                                 }else if (Estimators[i] == "memchi2") { 
460                                                         cDisplays.push_back(new CollectDisplay(new MemChi2(), new SharedOneColumnFile(getOutputFileName("memchi2", variables))));
461                                                         outputNames.push_back(getOutputFileName("memchi2", variables)); outputTypes["memchi2"].push_back(getOutputFileName("memchi2", variables));
462                                                 }else if (Estimators[i] == "memchord") { 
463                                                         cDisplays.push_back(new CollectDisplay(new MemChord(), new SharedOneColumnFile(getOutputFileName("memchord", variables))));
464                                                         outputNames.push_back(getOutputFileName("memchord", variables)); outputTypes["memchord"].push_back(getOutputFileName("memchord", variables));
465                                                 }else if (Estimators[i] == "memeuclidean") { 
466                                                         cDisplays.push_back(new CollectDisplay(new MemEuclidean(), new SharedOneColumnFile(getOutputFileName("memeuclidean", variables))));
467                                                         outputNames.push_back(getOutputFileName("memeuclidean", variables)); outputTypes["memeuclidean"].push_back(getOutputFileName("memeuclidean", variables));
468                                                 }else if (Estimators[i] == "mempearson") { 
469                                                         cDisplays.push_back(new CollectDisplay(new MemPearson(), new SharedOneColumnFile(getOutputFileName("mempearson", variables))));
470                                                         outputNames.push_back(getOutputFileName("mempearson", variables)); outputTypes["mempearson"].push_back(getOutputFileName("mempearson", variables));
471                                                 }else if (Estimators[i] == "jsd") {
472                                                         cDisplays.push_back(new CollectDisplay(new JSD(), new SharedOneColumnFile(getOutputFileName("jsd", variables))));
473                                                         outputNames.push_back(getOutputFileName("jsd", variables)); outputTypes["jsd"].push_back(getOutputFileName("jsd", variables));
474                                                 }else if (Estimators[i] == "rjsd") {
475                                                         cDisplays.push_back(new CollectDisplay(new RJSD(), new SharedOneColumnFile(getOutputFileName("rjsd", variables))));
476                                                         outputNames.push_back(getOutputFileName("rjsd", variables)); outputTypes["rjsd"].push_back(getOutputFileName("rjsd", variables));
477                                                 }
478
479                                                 
480                                         }
481                                 }       
482                         }
483                 }
484
485         }
486         catch(exception& e) {
487                 m->errorOut(e, "CollectSharedCommand", "CollectSharedCommand");
488                 exit(1);
489         }
490 }
491 //**********************************************************************************************************************
492 CollectSharedCommand::~CollectSharedCommand(){}
493 //**********************************************************************************************************************
494
495 int CollectSharedCommand::execute(){
496         try {
497                 
498                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
499                 
500                 //if the users entered no valid calculators don't execute command
501                 if (cDisplays.size() == 0) { return 0; }
502                 for(int i=0;i<cDisplays.size();i++){    cDisplays[i]->setAll(all);      }       
503         
504                 input = new InputData(sharedfile, "sharedfile");
505                 order = input->getSharedOrderVector();
506                 string lastLabel = order->getLabel();
507                 
508                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
509                 set<string> processedLabels;
510                 set<string> userLabels = labels;
511                         
512                 //set users groups
513                 SharedUtil* util = new SharedUtil();
514                 Groups = m->getGroups();
515                 vector<string> allGroups = m->getAllGroups();
516                 util->setGroups(Groups, allGroups, "collect");
517                 m->setGroups(Groups);
518                 m->setAllGroups(allGroups);
519                 delete util;
520
521                 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
522                         if (m->control_pressed) { 
523                                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);        }  outputTypes.clear();
524                                         for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
525                                         delete order; delete input;
526                                         m->clearGroups();
527                                         return 0;
528                         }
529
530                         if(allLines == 1 || labels.count(order->getLabel()) == 1){
531                         
532                                 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
533                                 //create collectors curve
534                                 cCurve = new Collect(order, cDisplays);
535                                 cCurve->getSharedCurve(freq);
536                                 delete cCurve;
537                         
538                                 processedLabels.insert(order->getLabel());
539                                 userLabels.erase(order->getLabel());
540                         }
541                         
542                         //you have a label the user want that is smaller than this label and the last label has not already been processed
543                         if ((m->anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
544                                 string saveLabel = order->getLabel();
545                                 
546                                 delete order;
547                                 order = input->getSharedOrderVector(lastLabel);
548                                 
549                                 m->mothurOut(order->getLabel()); m->mothurOutEndLine();
550                                 //create collectors curve
551                                 cCurve = new Collect(order, cDisplays);
552                                 cCurve->getSharedCurve(freq);
553                                 delete cCurve;
554                                 
555                                 processedLabels.insert(order->getLabel());
556                                 userLabels.erase(order->getLabel());
557                                 
558                                 //restore real lastlabel to save below
559                                 order->setLabel(saveLabel);
560                         }
561                         
562                         
563                         lastLabel = order->getLabel();                  
564                         
565                         //get next line to process
566                         delete order;
567                         order = input->getSharedOrderVector();
568                 }
569                 
570                 if (m->control_pressed) { 
571                                         for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);        }   outputTypes.clear();
572                                         for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
573                                         m->clearGroups();
574                                         delete input;
575                                         return 0;
576                 }
577                 
578                 //output error messages about any remaining user labels
579                 set<string>::iterator it;
580                 bool needToRun = false;
581                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
582                         m->mothurOut("Your file does not include the label " + *it); 
583                         if (processedLabels.count(lastLabel) != 1) {
584                                 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
585                                 needToRun = true;
586                         }else {
587                                 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
588                         }
589                 }
590                 
591                 //run last label if you need to
592                 if (needToRun == true)  {
593                         if (order != NULL) {  delete order;  }
594                         order = input->getSharedOrderVector(lastLabel);
595                         
596                         m->mothurOut(order->getLabel()); m->mothurOutEndLine();
597                         cCurve = new Collect(order, cDisplays);
598                         cCurve->getSharedCurve(freq);
599                         delete cCurve;
600                         
601                         if (m->control_pressed) { 
602                                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurRemove(outputNames[i]);        }  outputTypes.clear();
603                                 for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }
604                                 delete order; 
605                                 delete input;
606                                 m->clearGroups();
607                                 return 0;
608                         }
609
610                         delete order;
611                 }
612                 
613                 for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }       
614                 
615                 //reset groups parameter
616                 m->clearGroups(); 
617                 delete input;
618                 
619                 m->mothurOutEndLine();
620                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
621                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
622                 m->mothurOutEndLine();
623
624                 
625                 return 0;
626         }
627         catch(exception& e) {
628                 m->errorOut(e, "CollectSharedCommand", "execute");
629                 exit(1);
630         }
631 }
632
633 /***********************************************************/