]> git.donarmstrong.com Git - mothur.git/blob - collectsharedcommand.cpp
finished work on classify.seqs bayesian method and various bug fixes
[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                 labels.clear();
45                 Estimators.clear();
46                 Groups.clear();
47                 
48                 //allow user to run help
49                 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
50                 
51                 else {
52                         //valid paramters for this command
53                         string Array[] =  {"freq","label","calc","groups","all"};
54                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
55                         
56                         OptionParser parser(option);
57                         map<string,string> parameters=parser.getParameters();
58                         
59                         ValidParameters validParameter;
60                 
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;  }
64                         }
65                         
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; }
70                         }
71
72                         
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 = ""; }
77                         else { 
78                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
79                                 else { allLines = 1;  }
80                         }
81                         
82                         //if the user has not specified any labels use the ones from read.otu
83                         if(label == "") {  
84                                 allLines = globaldata->allLines; 
85                                 labels = globaldata->labels; 
86                         }
87                                 
88                         calc = validParameter.validFile(parameters, "calc", false);                     
89                         if (calc == "not found") { calc = "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan";  }
90                         else { 
91                                  if (calc == "default")  {  calc = "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan";  }
92                         }
93                         splitAtDash(calc, Estimators);
94                         
95                         groups = validParameter.validFile(parameters, "groups", false);                 
96                         if (groups == "not found") { groups = ""; }
97                         else { 
98                                 splitAtDash(groups, Groups);
99                         }
100                         globaldata->Groups = Groups;
101                         
102                         string temp;
103                         temp = validParameter.validFile(parameters, "freq", false);                     if (temp == "not found") { temp = "100"; }
104                         convert(temp, freq); 
105                         
106                         temp = validParameter.validFile(parameters, "all", false);                              if (temp == "not found") { temp = "true"; }
107                         all = isTrue(temp);
108                                                 
109                         if (abort == false) {
110                         
111                                 string fileNameRoot = getRootName(globaldata->inputFileName);
112                                 format = globaldata->getFormat();
113                                 int i;
114                                 
115                                 validCalculator = new ValidCalculators();
116                                 util = new SharedUtil();
117                                 
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")));
162                                                 }
163                                         }
164                                 }       
165                         }
166                 }
167
168         }
169         catch(exception& e) {
170                 errorOut(e, "CollectSharedCommand", "CollectSharedCommand");
171                 exit(1);
172         }
173 }
174 //**********************************************************************************************************************
175
176 void CollectSharedCommand::help(){
177         try {
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");
191                 
192         }
193         catch(exception& e) {
194                 errorOut(e, "CollectSharedCommand", "help");
195                 exit(1);
196         }
197 }
198
199 //**********************************************************************************************************************
200
201 CollectSharedCommand::~CollectSharedCommand(){
202         if (abort == false) {
203                 delete input; globaldata->ginput = NULL;
204                 delete read;
205                 delete util;
206                 delete validCalculator;
207                 globaldata->gorder = NULL;
208         }
209 }
210
211 //**********************************************************************************************************************
212
213 int CollectSharedCommand::execute(){
214         try {
215                 
216                 if (abort == true) {    return 0;       }
217                 
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);      }       
221                 
222                 read = new ReadOTUFile(globaldata->inputFileName);      
223                 read->read(&*globaldata); 
224                         
225                 input = globaldata->ginput;
226                 order = input->getSharedOrderVector();
227                 string lastLabel = order->getLabel();
228                 
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;
232                                 
233                 //set users groups
234                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups, "collect");
235                 util->updateGroupIndex(globaldata->Groups, globaldata->gGroupmap->groupIndex);
236
237                 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
238
239                         if(allLines == 1 || labels.count(order->getLabel()) == 1){
240                                 
241                                 //create collectors curve
242                                 cCurve = new Collect(order, cDisplays);
243                                 cCurve->getSharedCurve(freq);
244                                 delete cCurve;
245                         
246                                 mothurOut(order->getLabel()); mothurOutEndLine();
247                                 processedLabels.insert(order->getLabel());
248                                 userLabels.erase(order->getLabel());
249                         }
250                         
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();
254                                 
255                                 delete order;
256                                 order = input->getSharedOrderVector(lastLabel);
257                                 
258                                 //create collectors curve
259                                 cCurve = new Collect(order, cDisplays);
260                                 cCurve->getSharedCurve(freq);
261                                 delete cCurve;
262                         
263                                 mothurOut(order->getLabel()); mothurOutEndLine();
264                                 processedLabels.insert(order->getLabel());
265                                 userLabels.erase(order->getLabel());
266                                 
267                                 //restore real lastlabel to save below
268                                 order->setLabel(saveLabel);
269                         }
270                         
271                         
272                         lastLabel = order->getLabel();                  
273                         
274                         //get next line to process
275                         delete order;
276                         order = input->getSharedOrderVector();
277                 }
278                 
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();
286                                 needToRun = true;
287                         }else {
288                                 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
289                         }
290                 }
291                 
292                 //run last label if you need to
293                 if (needToRun == true)  {
294                         if (order != NULL) {  delete order;  }
295                         order = input->getSharedOrderVector(lastLabel);
296
297                         cCurve = new Collect(order, cDisplays);
298                         cCurve->getSharedCurve(freq);
299                         delete cCurve;
300                         
301                         mothurOut(order->getLabel()); mothurOutEndLine();
302                         delete order;
303                 }
304                 
305                 for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }       
306                 
307                 //reset groups parameter
308                 globaldata->Groups.clear(); 
309                 
310                 return 0;
311         }
312         catch(exception& e) {
313                 errorOut(e, "CollectSharedCommand", "execute");
314                 exit(1);
315         }
316 }
317
318 /***********************************************************/