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();
27 //allow user to run help
28 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
31 //valid paramters for this command
32 string Array[] = {"iters","line","label","calc","groups"};
33 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
35 OptionParser parser(option);
36 map<string,string> parameters = parser.getParameters();
38 ValidParameters validParameter;
40 //check to make sure all parameters are valid for command
41 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
42 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
45 //make sure the user has already run the read.otu command
46 if (globaldata->getSharedFile() == "") {
47 if (globaldata->getListFile() == "") { cout << "You must read a list and a group, or a shared before you can use the collect.shared command." << endl; abort = true; }
48 else if (globaldata->getGroupFile() == "") { cout << "You must read a list and a group, or a shared before you can use the collect.shared command." << endl; abort = true; }
52 //check for optional parameter and set defaults
53 // ...at some point should added some additional type checking...
54 line = validParameter.validFile(parameters, "line", false);
55 if (line == "not found") { line = ""; }
57 if(line != "all") { splitAtDash(line, lines); allLines = 0; }
58 else { allLines = 1; }
61 label = validParameter.validFile(parameters, "label", false);
62 if (label == "not found") { label = ""; }
64 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
65 else { allLines = 1; }
68 //make sure user did not use both the line and label parameters
69 if ((line != "") && (label != "")) { cout << "You cannot use both the line and label parameters at the same time. " << endl; abort = true; }
70 //if the user has not specified any line or labels use the ones from read.otu
71 else if((line == "") && (label == "")) {
72 allLines = globaldata->allLines;
73 labels = globaldata->labels;
74 lines = globaldata->lines;
77 calc = validParameter.validFile(parameters, "calc", false);
78 if (calc == "not found") { calc = "sharedobserved"; }
80 if (calc == "default") { calc = "sharedobserved"; }
82 splitAtDash(calc, Estimators);
84 groups = validParameter.validFile(parameters, "groups", false);
85 if (groups == "not found") { groups = ""; }
87 splitAtDash(groups, Groups);
89 globaldata->Groups = Groups;
92 temp = validParameter.validFile(parameters, "iters", false); if (temp == "not found") { temp = "1000"; }
93 convert(temp, nIters);
97 string fileNameRoot = getRootName(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 cout << "Standard Error: " << e.what() << " has occurred in the RareFactSharedCommand class Function RareFactSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
122 cout << "An unknown error has occurred in the RareFactSharedCommand class function RareFactSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
128 //**********************************************************************************************************************
130 void RareFactSharedCommand::help(){
132 cout << "The rarefaction.shared command can only be executed after a successful read.otu command." << "\n";
133 cout << "The rarefaction.shared command parameters are label, line, iters, groups and calc. No parameters are required, but you may not use " << "\n";
134 cout << "both the line and label parameters at the same time. The rarefaction command should be in the following format: " << "\n";
135 cout << "rarefaction.shared(label=yourLabel, line=yourLines, iters=yourIters, calc=yourEstimators, groups=yourGroups)." << "\n";
136 cout << "Example rarefaction.shared(label=unique-.01-.03, line=0-5-10, iters=10000, groups=B-C, calc=sharedobserved)." << "\n";
137 cout << "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";
138 cout << "The default value for groups is all the groups in your groupfile." << "\n";
139 validCalculator->printCalc("sharedrarefaction", cout);
140 cout << "The label and line parameters are used to analyze specific lines in your input." << "\n";
141 cout << "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";
142 cout << "Note: No spaces between parameter labels (i.e. freq), '=' and parameters (i.e.yourFreq)." << "\n" << "\n";
144 catch(exception& e) {
145 cout << "Standard Error: " << e.what() << " has occurred in the RareFactSharedCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
149 cout << "An unknown error has occurred in the RareFactSharedCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
154 //**********************************************************************************************************************
156 RareFactSharedCommand::~RareFactSharedCommand(){
157 if (abort == false) {
158 delete input; globaldata->ginput = NULL;
160 delete validCalculator;
164 //**********************************************************************************************************************
166 int RareFactSharedCommand::execute(){
169 if (abort == true) { return 0; }
173 //if the users entered no valid calculators don't execute command
174 if (rDisplays.size() == 0) { return 0; }
176 read = new ReadOTUFile(globaldata->inputFileName);
177 read->read(&*globaldata);
179 input = globaldata->ginput;
180 lookup = input->getSharedRAbundVectors();
181 vector<SharedRAbundVector*> lastLookup = lookup;
183 if (lookup.size() < 2) {
184 cout << "I cannot run the command without at least 2 valid groups.";
185 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
189 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
190 set<string> processedLabels;
191 set<string> userLabels = labels;
192 set<int> userLines = lines;
194 //as long as you are not at the end of the file or done wih the lines you want
195 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
197 if(allLines == 1 || lines.count(count) == 1 || labels.count(lookup[0]->getLabel()) == 1){
199 rCurve = new Rarefact(lookup, rDisplays);
200 rCurve->getSharedCurve(freq, nIters);
203 cout << lookup[0]->getLabel() << '\t' << count << endl;
204 processedLabels.insert(lookup[0]->getLabel());
205 userLabels.erase(lookup[0]->getLabel());
206 userLines.erase(count);
209 if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLookup[0]->getLabel()) != 1)) {
210 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
211 rCurve = new Rarefact(lastLookup, rDisplays);
212 rCurve->getSharedCurve(freq, nIters);
215 processedLabels.insert(lastLookup[0]->getLabel());
216 userLabels.erase(lastLookup[0]->getLabel());
219 //prevent memory leak
220 if (count != 1) { for (int i = 0; i < lastLookup.size(); i++) { delete lastLookup[i]; } }
223 //get next line to process
224 lookup = input->getSharedRAbundVectors();
228 //output error messages about any remaining user labels
229 set<string>::iterator it;
230 bool needToRun = false;
231 for (it = userLabels.begin(); it != userLabels.end(); it++) {
232 cout << "Your file does not include the label "<< *it;
233 if (processedLabels.count(lastLookup[0]->getLabel()) != 1) {
234 cout << ". I will use " << lastLookup[0]->getLabel() << "." << endl;
237 cout << ". Please refer to " << lastLookup[0]->getLabel() << "." << endl;
241 //run last line if you need to
242 if (needToRun == true) {
243 cout << lastLookup[0]->getLabel() << '\t' << count << endl;
244 rCurve = new Rarefact(lastLookup, rDisplays);
245 rCurve->getSharedCurve(freq, nIters);
249 for (int i = 0; i < lastLookup.size(); i++) { delete lastLookup[i]; }
251 for(int i=0;i<rDisplays.size();i++){ delete rDisplays[i]; }
253 //reset groups parameter
254 globaldata->Groups.clear();
258 catch(exception& e) {
259 cout << "Standard Error: " << e.what() << " has occurred in the RareFactSharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
263 cout << "An unknown error has occurred in the RareFactSharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
269 //**********************************************************************************************************************