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(){
29 abort = true; calledHelp = true;
30 vector<string> tempOutNames;
31 outputTypes["sharedrarefaction"] = tempOutNames;
32 outputTypes["sharedr_nseqs"] = tempOutNames;
35 m->errorOut(e, "RareFactSharedCommand", "RareFactSharedCommand");
39 //**********************************************************************************************************************
40 vector<string> RareFactSharedCommand::getRequiredParameters(){
42 vector<string> myArray;
46 m->errorOut(e, "RareFactSharedCommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> RareFactSharedCommand::getRequiredFiles(){
53 string Array[] = {"shared"};
54 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
58 m->errorOut(e, "RareFactSharedCommand", "getRequiredFiles");
62 //**********************************************************************************************************************
64 RareFactSharedCommand::RareFactSharedCommand(string option) {
66 globaldata = GlobalData::getInstance();
68 abort = false; calledHelp = false;
74 //allow user to run help
75 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; calledHelp = true; }
78 //valid paramters for this command
79 string Array[] = {"iters","freq","label","calc","groups", "jumble","outputdir","inputdir"};
80 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
82 OptionParser parser(option);
83 map<string,string> parameters = parser.getParameters();
85 ValidParameters validParameter;
87 //check to make sure all parameters are valid for command
88 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
89 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
92 //initialize outputTypes
93 vector<string> tempOutNames;
94 outputTypes["sharedrarefaction"] = tempOutNames;
95 outputTypes["sharedr_nseqs"] = tempOutNames;
97 //make sure the user has already run the read.otu command
98 if (globaldata->getSharedFile() == "") {
99 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; }
100 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; }
103 //if the user changes the output directory command factory will send this info to us in the output parameter
104 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
106 outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
110 //check for optional parameter and set defaults
111 // ...at some point should added some additional type checking...
112 label = validParameter.validFile(parameters, "label", false);
113 if (label == "not found") { label = ""; }
115 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
116 else { allLines = 1; }
119 //if the user has not specified any labels use the ones from read.otu
121 allLines = globaldata->allLines;
122 labels = globaldata->labels;
125 calc = validParameter.validFile(parameters, "calc", false);
126 if (calc == "not found") { calc = "sharedobserved"; }
128 if (calc == "default") { calc = "sharedobserved"; }
130 m->splitAtDash(calc, Estimators);
132 groups = validParameter.validFile(parameters, "groups", false);
133 if (groups == "not found") { groups = ""; }
135 m->splitAtDash(groups, Groups);
137 globaldata->Groups = Groups;
140 temp = validParameter.validFile(parameters, "freq", false); if (temp == "not found") { temp = "100"; }
143 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "1000"; }
144 convert(temp, nIters);
146 temp = validParameter.validFile(parameters, "jumble", false); if (temp == "not found") { temp = "T"; }
147 if (m->isTrue(temp)) { jumble = true; }
148 else { jumble = false; }
149 globaldata->jumble = jumble;
151 if (abort == false) {
153 string fileNameRoot = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName));
154 // format = globaldata->getFormat();
157 validCalculator = new ValidCalculators();
159 for (int i=0; i<Estimators.size(); i++) {
160 if (validCalculator->isValidCalculator("sharedrarefaction", Estimators[i]) == true) {
161 if (Estimators[i] == "sharedobserved") {
162 rDisplays.push_back(new RareDisplay(new SharedSobs(), new SharedThreeColumnFile(fileNameRoot+"shared.rarefaction", "")));
163 outputNames.push_back(fileNameRoot+"shared.rarefaction"); outputTypes["sharedrarefaction"].push_back(fileNameRoot+"shared.rarefaction");
164 }else if (Estimators[i] == "sharednseqs") {
165 rDisplays.push_back(new RareDisplay(new SharedNSeqs(), new SharedThreeColumnFile(fileNameRoot+"shared.r_nseqs", "")));
166 outputNames.push_back(fileNameRoot+"shared.r_nseqs"); outputTypes["sharedr_nseqs"].push_back(fileNameRoot+"shared.r_nseqs");
175 catch(exception& e) {
176 m->errorOut(e, "RareFactSharedCommand", "RareFactSharedCommand");
181 //**********************************************************************************************************************
183 void RareFactSharedCommand::help(){
185 m->mothurOut("The rarefaction.shared command can only be executed after a successful read.otu command.\n");
186 m->mothurOut("The rarefaction.shared command parameters are label, iters, groups, jumble and calc. No parameters are required.\n");
187 m->mothurOut("The rarefaction command should be in the following format: \n");
188 m->mothurOut("rarefaction.shared(label=yourLabel, iters=yourIters, calc=yourEstimators, jumble=yourJumble, groups=yourGroups).\n");
189 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");
190 m->mothurOut("Example rarefaction.shared(label=unique-0.01-0.03, iters=10000, groups=B-C, jumble=T, calc=sharedobserved).\n");
191 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");
192 m->mothurOut("The default value for groups is all the groups in your groupfile, and jumble is true.\n");
193 validCalculator->printCalc("sharedrarefaction", cout);
194 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
195 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");
196 m->mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
198 catch(exception& e) {
199 m->errorOut(e, "RareFactSharedCommand", "help");
204 //**********************************************************************************************************************
206 RareFactSharedCommand::~RareFactSharedCommand(){
207 if (abort == false) {
208 delete input; globaldata->ginput = NULL;
210 delete validCalculator;
214 //**********************************************************************************************************************
216 int RareFactSharedCommand::execute(){
219 if (abort == true) { if (calledHelp) { return 0; } return 2; }
221 //if the users entered no valid calculators don't execute command
222 if (rDisplays.size() == 0) { return 0; }
224 read = new ReadOTUFile(globaldata->inputFileName);
225 read->read(&*globaldata);
227 input = globaldata->ginput;
228 lookup = input->getSharedRAbundVectors();
229 string lastLabel = lookup[0]->getLabel();
231 if (m->control_pressed) {
232 globaldata->Groups.clear();
233 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
234 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
235 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
240 if (lookup.size() < 2) {
241 m->mothurOut("I cannot run the command without at least 2 valid groups.");
242 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
246 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
247 set<string> processedLabels;
248 set<string> userLabels = labels;
250 //as long as you are not at the end of the file or done wih the lines you want
251 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
252 if (m->control_pressed) {
253 globaldata->Groups.clear();
254 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
255 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
256 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
260 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
261 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
262 rCurve = new Rarefact(lookup, rDisplays);
263 rCurve->getSharedCurve(freq, nIters);
266 processedLabels.insert(lookup[0]->getLabel());
267 userLabels.erase(lookup[0]->getLabel());
270 if ((m->anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
271 string saveLabel = lookup[0]->getLabel();
273 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
274 lookup = input->getSharedRAbundVectors(lastLabel);
276 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
277 rCurve = new Rarefact(lookup, rDisplays);
278 rCurve->getSharedCurve(freq, nIters);
281 processedLabels.insert(lookup[0]->getLabel());
282 userLabels.erase(lookup[0]->getLabel());
284 //restore real lastlabel to save below
285 lookup[0]->setLabel(saveLabel);
289 lastLabel = lookup[0]->getLabel();
291 //get next line to process
292 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
293 lookup = input->getSharedRAbundVectors();
296 if (m->control_pressed) {
297 globaldata->Groups.clear();
298 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
299 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
303 //output error messages about any remaining user labels
304 set<string>::iterator it;
305 bool needToRun = false;
306 for (it = userLabels.begin(); it != userLabels.end(); it++) {
307 m->mothurOut("Your file does not include the label " + *it);
308 if (processedLabels.count(lastLabel) != 1) {
309 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
312 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
316 if (m->control_pressed) {
317 globaldata->Groups.clear();
318 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
319 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
323 //run last label if you need to
324 if (needToRun == true) {
325 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
326 lookup = input->getSharedRAbundVectors(lastLabel);
328 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
329 rCurve = new Rarefact(lookup, rDisplays);
330 rCurve->getSharedCurve(freq, nIters);
332 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
335 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
337 //reset groups parameter
338 globaldata->Groups.clear();
340 if (m->control_pressed) {
341 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
345 m->mothurOutEndLine();
346 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
347 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
348 m->mothurOutEndLine();
352 catch(exception& e) {
353 m->errorOut(e, "RareFactSharedCommand", "execute");
359 //**********************************************************************************************************************