2 * rarefactsharedcommand.cpp
5 * Created by Sarah Westcott on 1/6/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "rarefactsharedcommand.h"
11 #include "sharedsobs.h"
12 #include "sharednseqs.h"
14 //**********************************************************************************************************************
15 vector<string> RareFactSharedCommand::getValidParameters(){
17 string Array[] = {"iters","freq","label","calc","groups", "jumble","outputdir","inputdir"};
18 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
22 m->errorOut(e, "RareFactSharedCommand", "getValidParameters");
26 //**********************************************************************************************************************
27 RareFactSharedCommand::RareFactSharedCommand(){
30 //initialize outputTypes
31 vector<string> tempOutNames;
32 outputTypes["sharedrarefaction"] = tempOutNames;
33 outputTypes["sharedr_nseqs"] = tempOutNames;
36 m->errorOut(e, "RareFactSharedCommand", "RareFactSharedCommand");
40 //**********************************************************************************************************************
41 vector<string> RareFactSharedCommand::getRequiredParameters(){
43 vector<string> myArray;
47 m->errorOut(e, "RareFactSharedCommand", "getRequiredParameters");
51 //**********************************************************************************************************************
52 vector<string> RareFactSharedCommand::getRequiredFiles(){
54 string Array[] = {"shared"};
55 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
59 m->errorOut(e, "RareFactSharedCommand", "getRequiredFiles");
63 //**********************************************************************************************************************
65 RareFactSharedCommand::RareFactSharedCommand(string option) {
67 globaldata = GlobalData::getInstance();
75 //allow user to run help
76 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
79 //valid paramters for this command
80 string Array[] = {"iters","freq","label","calc","groups", "jumble","outputdir","inputdir"};
81 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
83 OptionParser parser(option);
84 map<string,string> parameters = parser.getParameters();
86 ValidParameters validParameter;
88 //check to make sure all parameters are valid for command
89 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
90 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
93 //initialize outputTypes
94 vector<string> tempOutNames;
95 outputTypes["sharedrarefaction"] = tempOutNames;
96 outputTypes["sharedr_nseqs"] = tempOutNames;
98 //make sure the user has already run the read.otu command
99 if (globaldata->getSharedFile() == "") {
100 if (globaldata->getListFile() == "") { m->mothurOut("You must read a list and a group, or a shared before you can use the collect.shared command."); m->mothurOutEndLine(); abort = true; }
101 else if (globaldata->getGroupFile() == "") { m->mothurOut("You must read a list and a group, or a shared before you can use the collect.shared command."); m->mothurOutEndLine(); abort = true; }
104 //if the user changes the output directory command factory will send this info to us in the output parameter
105 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
107 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
111 //check for optional parameter and set defaults
112 // ...at some point should added some additional type checking...
113 label = validParameter.validFile(parameters, "label", false);
114 if (label == "not found") { label = ""; }
116 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
117 else { allLines = 1; }
120 //if the user has not specified any labels use the ones from read.otu
122 allLines = globaldata->allLines;
123 labels = globaldata->labels;
126 calc = validParameter.validFile(parameters, "calc", false);
127 if (calc == "not found") { calc = "sharedobserved"; }
129 if (calc == "default") { calc = "sharedobserved"; }
131 m->splitAtDash(calc, Estimators);
133 groups = validParameter.validFile(parameters, "groups", false);
134 if (groups == "not found") { groups = ""; }
136 m->splitAtDash(groups, Groups);
138 globaldata->Groups = Groups;
141 temp = validParameter.validFile(parameters, "freq", false); if (temp == "not found") { temp = "100"; }
144 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "1000"; }
145 convert(temp, nIters);
147 temp = validParameter.validFile(parameters, "jumble", false); if (temp == "not found") { temp = "T"; }
148 if (m->isTrue(temp)) { jumble = true; }
149 else { jumble = false; }
150 globaldata->jumble = jumble;
152 if (abort == false) {
154 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName));
155 // format = globaldata->getFormat();
158 validCalculator = new ValidCalculators();
160 for (int i=0; i<Estimators.size(); i++) {
161 if (validCalculator->isValidCalculator("sharedrarefaction", Estimators[i]) == true) {
162 if (Estimators[i] == "sharedobserved") {
163 rDisplays.push_back(new RareDisplay(new SharedSobs(), new SharedThreeColumnFile(fileNameRoot+"shared.rarefaction", "")));
164 outputNames.push_back(fileNameRoot+"shared.rarefaction"); outputTypes["sharedrarefaction"].push_back(fileNameRoot+"shared.rarefaction");
165 }else if (Estimators[i] == "sharednseqs") {
166 rDisplays.push_back(new RareDisplay(new SharedNSeqs(), new SharedThreeColumnFile(fileNameRoot+"shared.r_nseqs", "")));
167 outputNames.push_back(fileNameRoot+"shared.r_nseqs"); outputTypes["sharedr_nseqs"].push_back(fileNameRoot+"shared.r_nseqs");
176 catch(exception& e) {
177 m->errorOut(e, "RareFactSharedCommand", "RareFactSharedCommand");
182 //**********************************************************************************************************************
184 void RareFactSharedCommand::help(){
186 m->mothurOut("The rarefaction.shared command can only be executed after a successful read.otu command.\n");
187 m->mothurOut("The rarefaction.shared command parameters are label, iters, groups, jumble and calc. No parameters are required.\n");
188 m->mothurOut("The rarefaction command should be in the following format: \n");
189 m->mothurOut("rarefaction.shared(label=yourLabel, iters=yourIters, calc=yourEstimators, jumble=yourJumble, groups=yourGroups).\n");
190 m->mothurOut("The freq parameter is used indicate when to output your data, by default it is set to 100. But you can set it to a percentage of the number of sequence. For example freq=0.10, means 10%. \n");
191 m->mothurOut("Example rarefaction.shared(label=unique-0.01-0.03, iters=10000, groups=B-C, jumble=T, calc=sharedobserved).\n");
192 m->mothurOut("The default values for iters is 1000, freq is 100, and calc is sharedobserved which calculates the shared rarefaction curve for the observed richness.\n");
193 m->mothurOut("The default value for groups is all the groups in your groupfile, and jumble is true.\n");
194 validCalculator->printCalc("sharedrarefaction", cout);
195 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
196 m->mothurOut("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");
197 m->mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
199 catch(exception& e) {
200 m->errorOut(e, "RareFactSharedCommand", "help");
205 //**********************************************************************************************************************
207 RareFactSharedCommand::~RareFactSharedCommand(){
208 if (abort == false) {
209 delete input; globaldata->ginput = NULL;
211 delete validCalculator;
215 //**********************************************************************************************************************
217 int RareFactSharedCommand::execute(){
220 if (abort == true) { return 0; }
222 //if the users entered no valid calculators don't execute command
223 if (rDisplays.size() == 0) { return 0; }
225 read = new ReadOTUFile(globaldata->inputFileName);
226 read->read(&*globaldata);
228 input = globaldata->ginput;
229 lookup = input->getSharedRAbundVectors();
230 string lastLabel = lookup[0]->getLabel();
232 if (m->control_pressed) {
233 globaldata->Groups.clear();
234 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
235 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
236 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
241 if (lookup.size() < 2) {
242 m->mothurOut("I cannot run the command without at least 2 valid groups.");
243 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
247 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
248 set<string> processedLabels;
249 set<string> userLabels = labels;
251 //as long as you are not at the end of the file or done wih the lines you want
252 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
253 if (m->control_pressed) {
254 globaldata->Groups.clear();
255 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
256 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
257 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
261 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
262 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
263 rCurve = new Rarefact(lookup, rDisplays);
264 rCurve->getSharedCurve(freq, nIters);
267 processedLabels.insert(lookup[0]->getLabel());
268 userLabels.erase(lookup[0]->getLabel());
271 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
272 string saveLabel = lookup[0]->getLabel();
274 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
275 lookup = input->getSharedRAbundVectors(lastLabel);
277 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
278 rCurve = new Rarefact(lookup, rDisplays);
279 rCurve->getSharedCurve(freq, nIters);
282 processedLabels.insert(lookup[0]->getLabel());
283 userLabels.erase(lookup[0]->getLabel());
285 //restore real lastlabel to save below
286 lookup[0]->setLabel(saveLabel);
290 lastLabel = lookup[0]->getLabel();
292 //get next line to process
293 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
294 lookup = input->getSharedRAbundVectors();
297 if (m->control_pressed) {
298 globaldata->Groups.clear();
299 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
300 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
304 //output error messages about any remaining user labels
305 set<string>::iterator it;
306 bool needToRun = false;
307 for (it = userLabels.begin(); it != userLabels.end(); it++) {
308 m->mothurOut("Your file does not include the label " + *it);
309 if (processedLabels.count(lastLabel) != 1) {
310 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
313 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
317 if (m->control_pressed) {
318 globaldata->Groups.clear();
319 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
320 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
324 //run last label if you need to
325 if (needToRun == true) {
326 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
327 lookup = input->getSharedRAbundVectors(lastLabel);
329 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
330 rCurve = new Rarefact(lookup, rDisplays);
331 rCurve->getSharedCurve(freq, nIters);
333 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
336 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
338 //reset groups parameter
339 globaldata->Groups.clear();
341 if (m->control_pressed) {
342 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
346 m->mothurOutEndLine();
347 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
348 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
349 m->mothurOutEndLine();
353 catch(exception& e) {
354 m->errorOut(e, "RareFactSharedCommand", "execute");
360 //**********************************************************************************************************************