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","freq","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() == "") { 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; }
47 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; }
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, "freq", false); if (temp == "not found") { temp = "100"; }
90 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "1000"; }
91 convert(temp, nIters);
93 temp = validParameter.validFile(parameters, "jumble", false); if (temp == "not found") { temp = "T"; }
94 if (isTrue(temp)) { jumble = true; }
95 else { jumble = false; }
96 globaldata->jumble = jumble;
100 string fileNameRoot = outputDir + getRootName(getSimpleName(globaldata->inputFileName));
101 // format = globaldata->getFormat();
104 validCalculator = new ValidCalculators();
106 for (int i=0; i<Estimators.size(); i++) {
107 if (validCalculator->isValidCalculator("sharedrarefaction", Estimators[i]) == true) {
108 if (Estimators[i] == "sharedobserved") {
109 rDisplays.push_back(new RareDisplay(new SharedSobs(), new SharedThreeColumnFile(fileNameRoot+"shared.rarefaction", "")));
110 outputNames.push_back(fileNameRoot+"shared.rarefaction");
111 }else if (Estimators[i] == "sharednseqs") {
112 rDisplays.push_back(new RareDisplay(new SharedNSeqs(), new SharedThreeColumnFile(fileNameRoot+"shared.r_nseqs", "")));
113 outputNames.push_back(fileNameRoot+"shared.r_nseqs");
122 catch(exception& e) {
123 m->errorOut(e, "RareFactSharedCommand", "RareFactSharedCommand");
128 //**********************************************************************************************************************
130 void RareFactSharedCommand::help(){
132 m->mothurOut("The rarefaction.shared command can only be executed after a successful read.otu command.\n");
133 m->mothurOut("The rarefaction.shared command parameters are label, iters, groups, jumble and calc. No parameters are required.\n");
134 m->mothurOut("The rarefaction command should be in the following format: \n");
135 m->mothurOut("rarefaction.shared(label=yourLabel, iters=yourIters, calc=yourEstimators, jumble=yourJumble, groups=yourGroups).\n");
136 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");
137 m->mothurOut("Example rarefaction.shared(label=unique-0.01-0.03, iters=10000, groups=B-C, jumble=T, calc=sharedobserved).\n");
138 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");
139 m->mothurOut("The default value for groups is all the groups in your groupfile, and jumble is true.\n");
140 validCalculator->printCalc("sharedrarefaction", cout);
141 m->mothurOut("The label parameter is used to analyze specific labels in your input.\n");
142 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");
143 m->mothurOut("Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq).\n\n");
145 catch(exception& e) {
146 m->errorOut(e, "RareFactSharedCommand", "help");
151 //**********************************************************************************************************************
153 RareFactSharedCommand::~RareFactSharedCommand(){
154 if (abort == false) {
155 delete input; globaldata->ginput = NULL;
157 delete validCalculator;
161 //**********************************************************************************************************************
163 int RareFactSharedCommand::execute(){
166 if (abort == true) { return 0; }
168 //if the users entered no valid calculators don't execute command
169 if (rDisplays.size() == 0) { return 0; }
171 read = new ReadOTUFile(globaldata->inputFileName);
172 read->read(&*globaldata);
174 input = globaldata->ginput;
175 lookup = input->getSharedRAbundVectors();
176 string lastLabel = lookup[0]->getLabel();
178 if (m->control_pressed) {
179 globaldata->Groups.clear();
180 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
181 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
182 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
187 if (lookup.size() < 2) {
188 m->mothurOut("I cannot run the command without at least 2 valid groups.");
189 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
193 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
194 set<string> processedLabels;
195 set<string> userLabels = labels;
197 //as long as you are not at the end of the file or done wih the lines you want
198 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
199 if (m->control_pressed) {
200 globaldata->Groups.clear();
201 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
202 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
203 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
207 if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){
208 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
209 rCurve = new Rarefact(lookup, rDisplays);
210 rCurve->getSharedCurve(freq, nIters);
213 processedLabels.insert(lookup[0]->getLabel());
214 userLabels.erase(lookup[0]->getLabel());
217 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
218 string saveLabel = lookup[0]->getLabel();
220 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
221 lookup = input->getSharedRAbundVectors(lastLabel);
223 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
224 rCurve = new Rarefact(lookup, rDisplays);
225 rCurve->getSharedCurve(freq, nIters);
228 processedLabels.insert(lookup[0]->getLabel());
229 userLabels.erase(lookup[0]->getLabel());
231 //restore real lastlabel to save below
232 lookup[0]->setLabel(saveLabel);
236 lastLabel = lookup[0]->getLabel();
238 //get next line to process
239 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
240 lookup = input->getSharedRAbundVectors();
243 if (m->control_pressed) {
244 globaldata->Groups.clear();
245 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
246 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
250 //output error messages about any remaining user labels
251 set<string>::iterator it;
252 bool needToRun = false;
253 for (it = userLabels.begin(); it != userLabels.end(); it++) {
254 m->mothurOut("Your file does not include the label " + *it);
255 if (processedLabels.count(lastLabel) != 1) {
256 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
259 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
263 if (m->control_pressed) {
264 globaldata->Groups.clear();
265 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
266 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
270 //run last label if you need to
271 if (needToRun == true) {
272 for (int i = 0; i < lookup.size(); i++) { if (lookup[i] != NULL) { delete lookup[i]; } }
273 lookup = input->getSharedRAbundVectors(lastLabel);
275 m->mothurOut(lookup[0]->getLabel()); m->mothurOutEndLine();
276 rCurve = new Rarefact(lookup, rDisplays);
277 rCurve->getSharedCurve(freq, nIters);
279 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
282 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
284 //reset groups parameter
285 globaldata->Groups.clear();
287 if (m->control_pressed) {
288 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
292 m->mothurOutEndLine();
293 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
294 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
295 m->mothurOutEndLine();
299 catch(exception& e) {
300 m->errorOut(e, "RareFactSharedCommand", "execute");
306 //**********************************************************************************************************************