]> git.donarmstrong.com Git - mothur.git/blob - collectsharedcommand.cpp
bugs fixes while testing for 1.5 release
[mothur.git] / collectsharedcommand.cpp
1 /*
2  *  collectsharedcommand.cpp
3  *  Dotur
4  *
5  *  Created by Sarah Westcott on 1/2/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
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"
34
35
36
37 //**********************************************************************************************************************
38
39 CollectSharedCommand::CollectSharedCommand(string option){
40         try {
41                 globaldata = GlobalData::getInstance();
42                 abort = false;
43                 allLines = 1;
44                 lines.clear();
45                 labels.clear();
46                 Estimators.clear();
47                 Groups.clear();
48                 
49                 //allow user to run help
50                 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
51                 
52                 else {
53                         //valid paramters for this command
54                         string Array[] =  {"freq","line","label","calc","groups"};
55                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
56                         
57                         OptionParser parser(option);
58                         map<string,string> parameters=parser.getParameters();
59                         
60                         ValidParameters validParameter;
61                 
62                         //check to make sure all parameters are valid for command
63                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
64                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
65                         }
66                         
67                         //make sure the user has already run the read.otu command
68                         if (globaldata->getSharedFile() == "") {
69                                 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; }
70                                 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; }
71                         }
72
73                         
74                         //check for optional parameter and set defaults
75                         // ...at some point should added some additional type checking...
76                         line = validParameter.validFile(parameters, "line", false);                             
77                         if (line == "not found") { line = "";  }
78                         else { 
79                                 if(line != "all") {  splitAtDash(line, lines);  allLines = 0;  }
80                                 else { allLines = 1;  }
81                         }
82                         
83                         label = validParameter.validFile(parameters, "label", false);                   
84                         if (label == "not found") { label = ""; }
85                         else { 
86                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
87                                 else { allLines = 1;  }
88                         }
89                         
90                         //make sure user did not use both the line and label parameters
91                         if ((line != "") && (label != "")) { mothurOut("You cannot use both the line and label parameters at the same time. "); mothurOutEndLine(); abort = true; }
92                         //if the user has not specified any line or labels use the ones from read.otu
93                         else if((line == "") && (label == "")) {  
94                                 allLines = globaldata->allLines; 
95                                 labels = globaldata->labels; 
96                                 lines = globaldata->lines;
97                         }
98                                 
99                         calc = validParameter.validFile(parameters, "calc", false);                     
100                         if (calc == "not found") { calc = "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan";  }
101                         else { 
102                                  if (calc == "default")  {  calc = "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan";  }
103                         }
104                         splitAtDash(calc, Estimators);
105                         
106                         groups = validParameter.validFile(parameters, "groups", false);                 
107                         if (groups == "not found") { groups = ""; }
108                         else { 
109                                 splitAtDash(groups, Groups);
110                         }
111                         globaldata->Groups = Groups;
112                         
113                         string temp;
114                         temp = validParameter.validFile(parameters, "freq", false);                     if (temp == "not found") { temp = "100"; }
115                         convert(temp, freq); 
116                                                 
117                         if (abort == false) {
118                         
119                                 string fileNameRoot = getRootName(globaldata->inputFileName);
120                                 format = globaldata->getFormat();
121                                 int i;
122                                 
123                                 validCalculator = new ValidCalculators();
124                                 util = new SharedUtil();
125                                 
126                                 for (i=0; i<Estimators.size(); i++) {
127                                         if (validCalculator->isValidCalculator("shared", Estimators[i]) == true) { 
128                                                 if (Estimators[i] == "sharedchao") { 
129                                                         cDisplays.push_back(new CollectDisplay(new SharedChao1(), new SharedOneColumnFile(fileNameRoot+"shared.chao")));
130                                                 }else if (Estimators[i] == "sharedsobs") { 
131                                                         cDisplays.push_back(new CollectDisplay(new SharedSobsCS(), new SharedOneColumnFile(fileNameRoot+"shared.sobs")));
132                                                 }else if (Estimators[i] == "sharedace") { 
133                                                         cDisplays.push_back(new CollectDisplay(new SharedAce(), new SharedOneColumnFile(fileNameRoot+"shared.ace")));
134                                                 }else if (Estimators[i] == "jabund") {  
135                                                         cDisplays.push_back(new CollectDisplay(new JAbund(), new SharedOneColumnFile(fileNameRoot+"jabund")));
136                                                 }else if (Estimators[i] == "sorabund") { 
137                                                         cDisplays.push_back(new CollectDisplay(new SorAbund(), new SharedOneColumnFile(fileNameRoot+"sorabund")));
138                                                 }else if (Estimators[i] == "jclass") { 
139                                                         cDisplays.push_back(new CollectDisplay(new Jclass(), new SharedOneColumnFile(fileNameRoot+"jclass")));
140                                                 }else if (Estimators[i] == "sorclass") { 
141                                                         cDisplays.push_back(new CollectDisplay(new SorClass(), new SharedOneColumnFile(fileNameRoot+"sorclass")));
142                                                 }else if (Estimators[i] == "jest") { 
143                                                         cDisplays.push_back(new CollectDisplay(new Jest(), new SharedOneColumnFile(fileNameRoot+"jest")));
144                                                 }else if (Estimators[i] == "sorest") { 
145                                                         cDisplays.push_back(new CollectDisplay(new SorEst(), new SharedOneColumnFile(fileNameRoot+"sorest")));
146                                                 }else if (Estimators[i] == "thetayc") { 
147                                                         cDisplays.push_back(new CollectDisplay(new ThetaYC(), new SharedOneColumnFile(fileNameRoot+"thetayc")));
148                                                 }else if (Estimators[i] == "thetan") { 
149                                                         cDisplays.push_back(new CollectDisplay(new ThetaN(), new SharedOneColumnFile(fileNameRoot+"thetan")));
150                                                 }else if (Estimators[i] == "kstest") { 
151                                                         cDisplays.push_back(new CollectDisplay(new KSTest(), new SharedOneColumnFile(fileNameRoot+"kstest")));
152                                                 }else if (Estimators[i] == "whittaker") { 
153                                                         cDisplays.push_back(new CollectDisplay(new Whittaker(), new SharedOneColumnFile(fileNameRoot+"whittaker")));
154                                                 }else if (Estimators[i] == "sharednseqs") { 
155                                                         cDisplays.push_back(new CollectDisplay(new SharedNSeqs(), new SharedOneColumnFile(fileNameRoot+"shared.nseqs")));
156                                                 }else if (Estimators[i] == "ochiai") { 
157                                                         cDisplays.push_back(new CollectDisplay(new Ochiai(), new SharedOneColumnFile(fileNameRoot+"ochiai")));
158                                                 }else if (Estimators[i] == "anderberg") { 
159                                                         cDisplays.push_back(new CollectDisplay(new Anderberg(), new SharedOneColumnFile(fileNameRoot+"anderberg")));
160                                                 }else if (Estimators[i] == "skulczynski") { 
161                                                         cDisplays.push_back(new CollectDisplay(new Kulczynski(), new SharedOneColumnFile(fileNameRoot+"kulczynski")));
162                                                 }else if (Estimators[i] == "kulczynskicody") { 
163                                                         cDisplays.push_back(new CollectDisplay(new KulczynskiCody(), new SharedOneColumnFile(fileNameRoot+"kulczynskicody")));
164                                                 }else if (Estimators[i] == "lennon") { 
165                                                         cDisplays.push_back(new CollectDisplay(new Lennon(), new SharedOneColumnFile(fileNameRoot+"lennon")));
166                                                 }else if (Estimators[i] == "morisitahorn") { 
167                                                         cDisplays.push_back(new CollectDisplay(new MorHorn(), new SharedOneColumnFile(fileNameRoot+"morisitahorn")));
168                                                 }else if (Estimators[i] == "braycurtis") { 
169                                                         cDisplays.push_back(new CollectDisplay(new BrayCurtis(), new SharedOneColumnFile(fileNameRoot+"braycurtis")));
170                                                 }
171                                         }
172                                 }       
173                         }
174                 }
175
176         }
177         catch(exception& e) {
178                 errorOut(e, "CollectSharedCommand", "CollectSharedCommand");
179                 exit(1);
180         }
181 }
182 //**********************************************************************************************************************
183
184 void CollectSharedCommand::help(){
185         try {
186                 mothurOut("The collect.shared command can only be executed after a successful read.otu command.\n");
187                 mothurOut("The collect.shared command parameters are label, line, freq, calc and groups.  No parameters are required, but you may not use \n");
188                 mothurOut("both the line and label parameters at the same time. The collect.shared command should be in the following format: \n");
189                 mothurOut("collect.shared(label=yourLabel, line=yourLines, freq=yourFreq, calc=yourEstimators, groups=yourGroups).\n");
190                 mothurOut("Example collect.shared(label=unique-.01-.03, line=0-5-10, freq=10, groups=B-C, calc=sharedchao-sharedace-jabund-sorensonabund-jclass-sorclass-jest-sorest-thetayc-thetan).\n");
191                 mothurOut("The default values for freq is 100 and calc are sharedsobs-sharedchao-sharedace-jabund-sorensonabund-jclass-sorclass-jest-sorest-thetayc-thetan.\n");
192                 mothurOut("The default value for groups is all the groups in your groupfile.\n");
193                 validCalculator->printCalc("shared", cout);
194                 mothurOut("The label and line parameters are used to analyze specific lines in your input.\n");
195                 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                 mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListfile).\n\n");
197                 
198         }
199         catch(exception& e) {
200                 errorOut(e, "CollectSharedCommand", "help");
201                 exit(1);
202         }
203 }
204
205 //**********************************************************************************************************************
206
207 CollectSharedCommand::~CollectSharedCommand(){
208         if (abort == false) {
209                 delete input; globaldata->ginput = NULL;
210                 delete read;
211                 delete util;
212                 delete validCalculator;
213                 globaldata->gorder = NULL;
214         }
215 }
216
217 //**********************************************************************************************************************
218
219 int CollectSharedCommand::execute(){
220         try {
221                 
222                 if (abort == true) {    return 0;       }
223                 
224                 int count = 1;
225                 
226                 //if the users entered no valid calculators don't execute command
227                 if (cDisplays.size() == 0) { return 0; }
228                 
229                 read = new ReadOTUFile(globaldata->inputFileName);      
230                 read->read(&*globaldata); 
231                         
232                 input = globaldata->ginput;
233                 order = input->getSharedOrderVector();
234                 string lastLabel = order->getLabel();
235                 
236                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
237                 set<string> processedLabels;
238                 set<string> userLabels = labels;
239                 set<int> userLines = lines;
240                                 
241                 //set users groups
242                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups, "collect");
243                 util->updateGroupIndex(globaldata->Groups, globaldata->gGroupmap->groupIndex);
244
245                 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
246
247                         if(allLines == 1 || lines.count(count) == 1 || labels.count(order->getLabel()) == 1){
248                                 
249                                 //create collectors curve
250                                 cCurve = new Collect(order, cDisplays);
251                                 cCurve->getSharedCurve(freq);
252                                 delete cCurve;
253                         
254                                 mothurOut(order->getLabel() + "\t" + toString(count)); mothurOutEndLine();
255                                 processedLabels.insert(order->getLabel());
256                                 userLabels.erase(order->getLabel());
257                                 userLines.erase(count);
258                         }
259                         
260                         //you have a label the user want that is smaller than this line and the last line has not already been processed
261                         if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
262                                 delete order;
263                                 order = input->getSharedOrderVector(lastLabel);
264                                 
265                                 //create collectors curve
266                                 cCurve = new Collect(order, cDisplays);
267                                 cCurve->getSharedCurve(freq);
268                                 delete cCurve;
269                         
270                                 mothurOut(order->getLabel() + "\t" + toString(count)); mothurOutEndLine();
271                                 processedLabels.insert(order->getLabel());
272                                 userLabels.erase(order->getLabel());
273                         }
274                         
275                         
276                         lastLabel = order->getLabel();                  
277                         
278                         //get next line to process
279                         delete order;
280                         order = input->getSharedOrderVector();
281                         count++;
282                 }
283                 
284                 //output error messages about any remaining user labels
285                 set<string>::iterator it;
286                 bool needToRun = false;
287                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
288                         mothurOut("Your file does not include the label " + *it); 
289                         if (processedLabels.count(lastLabel) != 1) {
290                                 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
291                                 needToRun = true;
292                         }else {
293                                 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
294                         }
295                 }
296                 
297                 //run last line if you need to
298                 if (needToRun == true)  {
299                         delete order;
300                         order = input->getSharedOrderVector(lastLabel);
301
302                         cCurve = new Collect(order, cDisplays);
303                         cCurve->getCurve(freq);
304                         delete cCurve;
305                         
306                         mothurOut(order->getLabel() + "\t" + toString(count)); mothurOutEndLine();
307                         delete order;
308                 }
309                 
310                 for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }       
311                 
312                 //reset groups parameter
313                 globaldata->Groups.clear(); 
314                 
315                 return 0;
316         }
317         catch(exception& e) {
318                 errorOut(e, "CollectSharedCommand", "execute");
319                 exit(1);
320         }
321 }
322
323 /***********************************************************/