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 //**********************************************************************************************************************
16 RareFactSharedCommand::RareFactSharedCommand(string option){
18 globaldata = GlobalData::getInstance();
26 //allow user to run help
27 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
30 //valid paramters for this command
31 string Array[] = {"iters","label","calc","groups", "jumble","outputdir","inputdir"};
32 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
34 OptionParser parser(option);
35 map<string,string> parameters = parser.getParameters();
37 ValidParameters validParameter;
39 //check to make sure all parameters are valid for command
40 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
41 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
44 //make sure the user has already run the read.otu command
45 if (globaldata->getSharedFile() == "") {
46 if (globaldata->getListFile() == "") { mothurOut("You must read a list and a group, or a shared before you can use the collect.shared command."); mothurOutEndLine(); abort = true; }
47 else if (globaldata->getGroupFile() == "") { mothurOut("You must read a list and a group, or a shared before you can use the collect.shared command."); mothurOutEndLine(); abort = true; }
50 //if the user changes the output directory command factory will send this info to us in the output parameter
51 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
53 outputDir += hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it
57 //check for optional parameter and set defaults
58 // ...at some point should added some additional type checking...
59 label = validParameter.validFile(parameters, "label", false);
60 if (label == "not found") { label = ""; }
62 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
63 else { allLines = 1; }
66 //if the user has not specified any labels use the ones from read.otu
68 allLines = globaldata->allLines;
69 labels = globaldata->labels;
72 calc = validParameter.validFile(parameters, "calc", false);
73 if (calc == "not found") { calc = "sharedobserved"; }
75 if (calc == "default") { calc = "sharedobserved"; }
77 splitAtDash(calc, Estimators);
79 groups = validParameter.validFile(parameters, "groups", false);
80 if (groups == "not found") { groups = ""; }
82 splitAtDash(groups, Groups);
84 globaldata->Groups = Groups;
87 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "1000"; }
88 convert(temp, nIters);
90 temp = validParameter.validFile(parameters, "jumble", false); if (temp == "not found") { temp = "T"; }
91 if (isTrue(temp)) { jumble = true; }
92 else { jumble = false; }
93 globaldata->jumble = jumble;
97 string fileNameRoot = outputDir + getRootName(getSimpleName(globaldata->inputFileName));
98 // format = globaldata->getFormat();
101 validCalculator = new ValidCalculators();
103 for (int i=0; i<Estimators.size(); i++) {
104 if (validCalculator->isValidCalculator("sharedrarefaction", Estimators[i]) == true) {
105 if (Estimators[i] == "sharedobserved") {
106 rDisplays.push_back(new RareDisplay(new SharedSobs(), new SharedThreeColumnFile(fileNameRoot+"shared.rarefaction", "")));
107 }else if (Estimators[i] == "sharednseqs") {
108 rDisplays.push_back(new RareDisplay(new SharedNSeqs(), new SharedThreeColumnFile(fileNameRoot+"shared.r_nseqs", "")));
117 catch(exception& e) {
118 errorOut(e, "RareFactSharedCommand", "RareFactSharedCommand");
123 //**********************************************************************************************************************
125 void RareFactSharedCommand::help(){
127 mothurOut("The rarefaction.shared command can only be executed after a successful read.otu command.\n");
128 mothurOut("The rarefaction.shared command parameters are label, iters, groups, jumble and calc. No parameters are required.\n");
129 mothurOut("The rarefaction command should be in the following format: \n");
130 mothurOut("rarefaction.shared(label=yourLabel, iters=yourIters, calc=yourEstimators, jumble=yourJumble, groups=yourGroups).\n");
131 mothurOut("Example rarefaction.shared(label=unique-0.01-0.03, iters=10000, groups=B-C, jumble=T, calc=sharedobserved).\n");
132 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");
133 mothurOut("The default value for groups is all the groups in your groupfile, and jumble is true.\n");
134 validCalculator->printCalc("sharedrarefaction", cout);
135 mothurOut("The label parameter is used to analyze specific labels in your input.\n");
136 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");
137 mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
139 catch(exception& e) {
140 errorOut(e, "RareFactSharedCommand", "help");
145 //**********************************************************************************************************************
147 RareFactSharedCommand::~RareFactSharedCommand(){
148 if (abort == false) {
149 delete input; globaldata->ginput = NULL;
151 delete validCalculator;
155 //**********************************************************************************************************************
157 int RareFactSharedCommand::execute(){
160 if (abort == true) { return 0; }
162 //if the users entered no valid calculators don't execute command
163 if (rDisplays.size() == 0) { return 0; }
165 read = new ReadOTUFile(globaldata->inputFileName);
166 read->read(&*globaldata);
168 input = globaldata->ginput;
169 lookup = input->getSharedRAbundVectors();
170 string lastLabel = lookup[0]->getLabel();
172 if (lookup.size() < 2) {
173 mothurOut("I cannot run the command without at least 2 valid groups.");
174 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
178 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
179 set<string> processedLabels;
180 set<string> userLabels = labels;
182 //as long as you are not at the end of the file or done wih the lines you want
183 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
185 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
187 rCurve = new Rarefact(lookup, rDisplays);
188 rCurve->getSharedCurve(freq, nIters);
191 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
192 processedLabels.insert(lookup[0]->getLabel());
193 userLabels.erase(lookup[0]->getLabel());
196 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
197 string saveLabel = lookup[0]->getLabel();
199 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
200 lookup = input->getSharedRAbundVectors(lastLabel);
202 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
203 rCurve = new Rarefact(lookup, rDisplays);
204 rCurve->getSharedCurve(freq, nIters);
207 processedLabels.insert(lookup[0]->getLabel());
208 userLabels.erase(lookup[0]->getLabel());
210 //restore real lastlabel to save below
211 lookup[0]->setLabel(saveLabel);
215 lastLabel = lookup[0]->getLabel();
217 //get next line to process
218 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
219 lookup = input->getSharedRAbundVectors();
222 //output error messages about any remaining user labels
223 set<string>::iterator it;
224 bool needToRun = false;
225 for (it = userLabels.begin(); it != userLabels.end(); it++) {
226 mothurOut("Your file does not include the label " + *it);
227 if (processedLabels.count(lastLabel) != 1) {
228 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
231 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
235 //run last label if you need to
236 if (needToRun == true) {
237 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
238 lookup = input->getSharedRAbundVectors(lastLabel);
240 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
241 rCurve = new Rarefact(lookup, rDisplays);
242 rCurve->getSharedCurve(freq, nIters);
244 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
247 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
249 //reset groups parameter
250 globaldata->Groups.clear();
254 catch(exception& e) {
255 errorOut(e, "RareFactSharedCommand", "execute");
261 //**********************************************************************************************************************