2 * collectsharedcommand.cpp
5 * Created by Sarah Westcott on 1/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "collectsharedcommand.h"
11 #include "sharedsobscollectsummary.h"
12 #include "sharedchao1.h"
13 #include "sharedace.h"
14 #include "sharedjabund.h"
15 #include "sharedsorabund.h"
16 #include "sharedjclass.h"
17 #include "sharedsorclass.h"
18 #include "sharedjest.h"
19 #include "sharedsorest.h"
20 #include "sharedthetayc.h"
21 #include "sharedthetan.h"
22 #include "sharedkstest.h"
23 #include "whittaker.h"
24 #include "sharednseqs.h"
25 #include "sharedochiai.h"
26 #include "sharedanderbergs.h"
27 #include "sharedkulczynski.h"
28 #include "sharedkulczynskicody.h"
29 #include "sharedlennon.h"
30 #include "sharedmorisitahorn.h"
31 #include "sharedbraycurtis.h"
32 #include "sharedjackknife.h"
33 #include "whittaker.h"
37 //**********************************************************************************************************************
39 CollectSharedCommand::CollectSharedCommand(string option){
41 globaldata = GlobalData::getInstance();
48 //allow user to run help
49 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
52 //valid paramters for this command
53 string Array[] = {"freq","label","calc","groups","all"};
54 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
56 OptionParser parser(option);
57 map<string,string> parameters=parser.getParameters();
59 ValidParameters validParameter;
61 //check to make sure all parameters are valid for command
62 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
63 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
66 //make sure the user has already run the read.otu command
67 if (globaldata->getSharedFile() == "") {
68 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; }
69 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; }
73 //check for optional parameter and set defaults
74 // ...at some point should added some additional type checking..
75 label = validParameter.validFile(parameters, "label", false);
76 if (label == "not found") { label = ""; }
78 if(label != "all") { splitAtDash(label, labels); allLines = 0; }
79 else { allLines = 1; }
82 //if the user has not specified any labels use the ones from read.otu
84 allLines = globaldata->allLines;
85 labels = globaldata->labels;
88 calc = validParameter.validFile(parameters, "calc", false);
89 if (calc == "not found") { calc = "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan"; }
91 if (calc == "default") { calc = "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan"; }
93 splitAtDash(calc, Estimators);
95 groups = validParameter.validFile(parameters, "groups", false);
96 if (groups == "not found") { groups = ""; }
98 splitAtDash(groups, Groups);
100 globaldata->Groups = Groups;
103 temp = validParameter.validFile(parameters, "freq", false); if (temp == "not found") { temp = "100"; }
106 temp = validParameter.validFile(parameters, "all", false); if (temp == "not found") { temp = "true"; }
109 if (abort == false) {
111 string fileNameRoot = getRootName(globaldata->inputFileName);
112 format = globaldata->getFormat();
115 validCalculator = new ValidCalculators();
116 util = new SharedUtil();
118 for (i=0; i<Estimators.size(); i++) {
119 if (validCalculator->isValidCalculator("shared", Estimators[i]) == true) {
120 if (Estimators[i] == "sharedchao") {
121 cDisplays.push_back(new CollectDisplay(new SharedChao1(), new SharedOneColumnFile(fileNameRoot+"shared.chao")));
122 }else if (Estimators[i] == "sharedsobs") {
123 cDisplays.push_back(new CollectDisplay(new SharedSobsCS(), new SharedOneColumnFile(fileNameRoot+"shared.sobs")));
124 }else if (Estimators[i] == "sharedace") {
125 cDisplays.push_back(new CollectDisplay(new SharedAce(), new SharedOneColumnFile(fileNameRoot+"shared.ace")));
126 }else if (Estimators[i] == "jabund") {
127 cDisplays.push_back(new CollectDisplay(new JAbund(), new SharedOneColumnFile(fileNameRoot+"jabund")));
128 }else if (Estimators[i] == "sorabund") {
129 cDisplays.push_back(new CollectDisplay(new SorAbund(), new SharedOneColumnFile(fileNameRoot+"sorabund")));
130 }else if (Estimators[i] == "jclass") {
131 cDisplays.push_back(new CollectDisplay(new Jclass(), new SharedOneColumnFile(fileNameRoot+"jclass")));
132 }else if (Estimators[i] == "sorclass") {
133 cDisplays.push_back(new CollectDisplay(new SorClass(), new SharedOneColumnFile(fileNameRoot+"sorclass")));
134 }else if (Estimators[i] == "jest") {
135 cDisplays.push_back(new CollectDisplay(new Jest(), new SharedOneColumnFile(fileNameRoot+"jest")));
136 }else if (Estimators[i] == "sorest") {
137 cDisplays.push_back(new CollectDisplay(new SorEst(), new SharedOneColumnFile(fileNameRoot+"sorest")));
138 }else if (Estimators[i] == "thetayc") {
139 cDisplays.push_back(new CollectDisplay(new ThetaYC(), new SharedOneColumnFile(fileNameRoot+"thetayc")));
140 }else if (Estimators[i] == "thetan") {
141 cDisplays.push_back(new CollectDisplay(new ThetaN(), new SharedOneColumnFile(fileNameRoot+"thetan")));
142 }else if (Estimators[i] == "kstest") {
143 cDisplays.push_back(new CollectDisplay(new KSTest(), new SharedOneColumnFile(fileNameRoot+"kstest")));
144 }else if (Estimators[i] == "whittaker") {
145 cDisplays.push_back(new CollectDisplay(new Whittaker(), new SharedOneColumnFile(fileNameRoot+"whittaker")));
146 }else if (Estimators[i] == "sharednseqs") {
147 cDisplays.push_back(new CollectDisplay(new SharedNSeqs(), new SharedOneColumnFile(fileNameRoot+"shared.nseqs")));
148 }else if (Estimators[i] == "ochiai") {
149 cDisplays.push_back(new CollectDisplay(new Ochiai(), new SharedOneColumnFile(fileNameRoot+"ochiai")));
150 }else if (Estimators[i] == "anderberg") {
151 cDisplays.push_back(new CollectDisplay(new Anderberg(), new SharedOneColumnFile(fileNameRoot+"anderberg")));
152 }else if (Estimators[i] == "skulczynski") {
153 cDisplays.push_back(new CollectDisplay(new Kulczynski(), new SharedOneColumnFile(fileNameRoot+"kulczynski")));
154 }else if (Estimators[i] == "kulczynskicody") {
155 cDisplays.push_back(new CollectDisplay(new KulczynskiCody(), new SharedOneColumnFile(fileNameRoot+"kulczynskicody")));
156 }else if (Estimators[i] == "lennon") {
157 cDisplays.push_back(new CollectDisplay(new Lennon(), new SharedOneColumnFile(fileNameRoot+"lennon")));
158 }else if (Estimators[i] == "morisitahorn") {
159 cDisplays.push_back(new CollectDisplay(new MorHorn(), new SharedOneColumnFile(fileNameRoot+"morisitahorn")));
160 }else if (Estimators[i] == "braycurtis") {
161 cDisplays.push_back(new CollectDisplay(new BrayCurtis(), new SharedOneColumnFile(fileNameRoot+"braycurtis")));
169 catch(exception& e) {
170 errorOut(e, "CollectSharedCommand", "CollectSharedCommand");
174 //**********************************************************************************************************************
176 void CollectSharedCommand::help(){
178 mothurOut("The collect.shared command can only be executed after a successful read.otu command.\n");
179 mothurOut("The collect.shared command parameters are label, freq, calc and groups. No parameters are required \n");
180 mothurOut("The collect.shared command should be in the following format: \n");
181 mothurOut("collect.shared(label=yourLabel, freq=yourFreq, calc=yourEstimators, groups=yourGroups).\n");
182 mothurOut("Example collect.shared(label=unique-.01-.03, freq=10, groups=B-C, calc=sharedchao-sharedace-jabund-sorensonabund-jclass-sorclass-jest-sorest-thetayc-thetan).\n");
183 mothurOut("The default values for freq is 100 and calc are sharedsobs-sharedchao-sharedace-jabund-sorensonabund-jclass-sorclass-jest-sorest-thetayc-thetan.\n");
184 mothurOut("The default value for groups is all the groups in your groupfile.\n");
185 validCalculator->printCalc("shared", cout);
186 mothurOut("The label parameter is used to analyze specific labels in your input.\n");
187 mothurOut("The all parameter is used to specify if you want the estimate of all your groups together. This estimate can only be made for sharedsobs and sharedchao calculators. The default is true.\n");
188 mothurOut("If you use sharedchao and run into memory issues, set all to false. \n");
189 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");
190 mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListfile).\n\n");
193 catch(exception& e) {
194 errorOut(e, "CollectSharedCommand", "help");
199 //**********************************************************************************************************************
201 CollectSharedCommand::~CollectSharedCommand(){
202 if (abort == false) {
203 delete input; globaldata->ginput = NULL;
206 delete validCalculator;
207 globaldata->gorder = NULL;
211 //**********************************************************************************************************************
213 int CollectSharedCommand::execute(){
216 if (abort == true) { return 0; }
218 //if the users entered no valid calculators don't execute command
219 if (cDisplays.size() == 0) { return 0; }
220 for(int i=0;i<cDisplays.size();i++){ cDisplays[i]->setAll(all); }
222 read = new ReadOTUFile(globaldata->inputFileName);
223 read->read(&*globaldata);
225 input = globaldata->ginput;
226 order = input->getSharedOrderVector();
227 string lastLabel = order->getLabel();
229 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
230 set<string> processedLabels;
231 set<string> userLabels = labels;
234 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups, "collect");
235 util->updateGroupIndex(globaldata->Groups, globaldata->gGroupmap->groupIndex);
237 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
239 if(allLines == 1 || labels.count(order->getLabel()) == 1){
241 //create collectors curve
242 cCurve = new Collect(order, cDisplays);
243 cCurve->getSharedCurve(freq);
246 mothurOut(order->getLabel()); mothurOutEndLine();
247 processedLabels.insert(order->getLabel());
248 userLabels.erase(order->getLabel());
251 //you have a label the user want that is smaller than this label and the last label has not already been processed
252 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
253 string saveLabel = order->getLabel();
256 order = input->getSharedOrderVector(lastLabel);
258 //create collectors curve
259 cCurve = new Collect(order, cDisplays);
260 cCurve->getSharedCurve(freq);
263 mothurOut(order->getLabel()); mothurOutEndLine();
264 processedLabels.insert(order->getLabel());
265 userLabels.erase(order->getLabel());
267 //restore real lastlabel to save below
268 order->setLabel(saveLabel);
272 lastLabel = order->getLabel();
274 //get next line to process
276 order = input->getSharedOrderVector();
279 //output error messages about any remaining user labels
280 set<string>::iterator it;
281 bool needToRun = false;
282 for (it = userLabels.begin(); it != userLabels.end(); it++) {
283 mothurOut("Your file does not include the label " + *it);
284 if (processedLabels.count(lastLabel) != 1) {
285 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
288 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
292 //run last label if you need to
293 if (needToRun == true) {
294 if (order != NULL) { delete order; }
295 order = input->getSharedOrderVector(lastLabel);
297 cCurve = new Collect(order, cDisplays);
298 cCurve->getSharedCurve(freq);
301 mothurOut(order->getLabel()); mothurOutEndLine();
305 for(int i=0;i<cDisplays.size();i++){ delete cDisplays[i]; }
307 //reset groups parameter
308 globaldata->Groups.clear();
312 catch(exception& e) {
313 errorOut(e, "CollectSharedCommand", "execute");
318 /***********************************************************/