]> git.donarmstrong.com Git - mothur.git/blob - collectsharedcommand.cpp
added hcluster command and fixed some bugs, namely one with smart distancing.
[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"};
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                         if (abort == false) {
107                         
108                                 string fileNameRoot = getRootName(globaldata->inputFileName);
109                                 format = globaldata->getFormat();
110                                 int i;
111                                 
112                                 validCalculator = new ValidCalculators();
113                                 util = new SharedUtil();
114                                 
115                                 for (i=0; i<Estimators.size(); i++) {
116                                         if (validCalculator->isValidCalculator("shared", Estimators[i]) == true) { 
117                                                 if (Estimators[i] == "sharedchao") { 
118                                                         cDisplays.push_back(new CollectDisplay(new SharedChao1(), new SharedOneColumnFile(fileNameRoot+"shared.chao")));
119                                                 }else if (Estimators[i] == "sharedsobs") { 
120                                                         cDisplays.push_back(new CollectDisplay(new SharedSobsCS(), new SharedOneColumnFile(fileNameRoot+"shared.sobs")));
121                                                 }else if (Estimators[i] == "sharedace") { 
122                                                         cDisplays.push_back(new CollectDisplay(new SharedAce(), new SharedOneColumnFile(fileNameRoot+"shared.ace")));
123                                                 }else if (Estimators[i] == "jabund") {  
124                                                         cDisplays.push_back(new CollectDisplay(new JAbund(), new SharedOneColumnFile(fileNameRoot+"jabund")));
125                                                 }else if (Estimators[i] == "sorabund") { 
126                                                         cDisplays.push_back(new CollectDisplay(new SorAbund(), new SharedOneColumnFile(fileNameRoot+"sorabund")));
127                                                 }else if (Estimators[i] == "jclass") { 
128                                                         cDisplays.push_back(new CollectDisplay(new Jclass(), new SharedOneColumnFile(fileNameRoot+"jclass")));
129                                                 }else if (Estimators[i] == "sorclass") { 
130                                                         cDisplays.push_back(new CollectDisplay(new SorClass(), new SharedOneColumnFile(fileNameRoot+"sorclass")));
131                                                 }else if (Estimators[i] == "jest") { 
132                                                         cDisplays.push_back(new CollectDisplay(new Jest(), new SharedOneColumnFile(fileNameRoot+"jest")));
133                                                 }else if (Estimators[i] == "sorest") { 
134                                                         cDisplays.push_back(new CollectDisplay(new SorEst(), new SharedOneColumnFile(fileNameRoot+"sorest")));
135                                                 }else if (Estimators[i] == "thetayc") { 
136                                                         cDisplays.push_back(new CollectDisplay(new ThetaYC(), new SharedOneColumnFile(fileNameRoot+"thetayc")));
137                                                 }else if (Estimators[i] == "thetan") { 
138                                                         cDisplays.push_back(new CollectDisplay(new ThetaN(), new SharedOneColumnFile(fileNameRoot+"thetan")));
139                                                 }else if (Estimators[i] == "kstest") { 
140                                                         cDisplays.push_back(new CollectDisplay(new KSTest(), new SharedOneColumnFile(fileNameRoot+"kstest")));
141                                                 }else if (Estimators[i] == "whittaker") { 
142                                                         cDisplays.push_back(new CollectDisplay(new Whittaker(), new SharedOneColumnFile(fileNameRoot+"whittaker")));
143                                                 }else if (Estimators[i] == "sharednseqs") { 
144                                                         cDisplays.push_back(new CollectDisplay(new SharedNSeqs(), new SharedOneColumnFile(fileNameRoot+"shared.nseqs")));
145                                                 }else if (Estimators[i] == "ochiai") { 
146                                                         cDisplays.push_back(new CollectDisplay(new Ochiai(), new SharedOneColumnFile(fileNameRoot+"ochiai")));
147                                                 }else if (Estimators[i] == "anderberg") { 
148                                                         cDisplays.push_back(new CollectDisplay(new Anderberg(), new SharedOneColumnFile(fileNameRoot+"anderberg")));
149                                                 }else if (Estimators[i] == "skulczynski") { 
150                                                         cDisplays.push_back(new CollectDisplay(new Kulczynski(), new SharedOneColumnFile(fileNameRoot+"kulczynski")));
151                                                 }else if (Estimators[i] == "kulczynskicody") { 
152                                                         cDisplays.push_back(new CollectDisplay(new KulczynskiCody(), new SharedOneColumnFile(fileNameRoot+"kulczynskicody")));
153                                                 }else if (Estimators[i] == "lennon") { 
154                                                         cDisplays.push_back(new CollectDisplay(new Lennon(), new SharedOneColumnFile(fileNameRoot+"lennon")));
155                                                 }else if (Estimators[i] == "morisitahorn") { 
156                                                         cDisplays.push_back(new CollectDisplay(new MorHorn(), new SharedOneColumnFile(fileNameRoot+"morisitahorn")));
157                                                 }else if (Estimators[i] == "braycurtis") { 
158                                                         cDisplays.push_back(new CollectDisplay(new BrayCurtis(), new SharedOneColumnFile(fileNameRoot+"braycurtis")));
159                                                 }
160                                         }
161                                 }       
162                         }
163                 }
164
165         }
166         catch(exception& e) {
167                 errorOut(e, "CollectSharedCommand", "CollectSharedCommand");
168                 exit(1);
169         }
170 }
171 //**********************************************************************************************************************
172
173 void CollectSharedCommand::help(){
174         try {
175                 mothurOut("The collect.shared command can only be executed after a successful read.otu command.\n");
176                 mothurOut("The collect.shared command parameters are label, freq, calc and groups.  No parameters are required \n");
177                 mothurOut("The collect.shared command should be in the following format: \n");
178                 mothurOut("collect.shared(label=yourLabel, freq=yourFreq, calc=yourEstimators, groups=yourGroups).\n");
179                 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");
180                 mothurOut("The default values for freq is 100 and calc are sharedsobs-sharedchao-sharedace-jabund-sorensonabund-jclass-sorclass-jest-sorest-thetayc-thetan.\n");
181                 mothurOut("The default value for groups is all the groups in your groupfile.\n");
182                 validCalculator->printCalc("shared", cout);
183                 mothurOut("The label parameter is used to analyze specific labels in your input.\n");
184                 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");
185                 mothurOut("Note: No spaces between parameter labels (i.e. list), '=' and parameters (i.e.yourListfile).\n\n");
186                 
187         }
188         catch(exception& e) {
189                 errorOut(e, "CollectSharedCommand", "help");
190                 exit(1);
191         }
192 }
193
194 //**********************************************************************************************************************
195
196 CollectSharedCommand::~CollectSharedCommand(){
197         if (abort == false) {
198                 delete input; globaldata->ginput = NULL;
199                 delete read;
200                 delete util;
201                 delete validCalculator;
202                 globaldata->gorder = NULL;
203         }
204 }
205
206 //**********************************************************************************************************************
207
208 int CollectSharedCommand::execute(){
209         try {
210                 
211                 if (abort == true) {    return 0;       }
212                 
213                 //if the users entered no valid calculators don't execute command
214                 if (cDisplays.size() == 0) { return 0; }
215                 
216                 read = new ReadOTUFile(globaldata->inputFileName);      
217                 read->read(&*globaldata); 
218                         
219                 input = globaldata->ginput;
220                 order = input->getSharedOrderVector();
221                 string lastLabel = order->getLabel();
222                 
223                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
224                 set<string> processedLabels;
225                 set<string> userLabels = labels;
226                                 
227                 //set users groups
228                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups, "collect");
229                 util->updateGroupIndex(globaldata->Groups, globaldata->gGroupmap->groupIndex);
230
231                 while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
232
233                         if(allLines == 1 || labels.count(order->getLabel()) == 1){
234                                 
235                                 //create collectors curve
236                                 cCurve = new Collect(order, cDisplays);
237                                 cCurve->getSharedCurve(freq);
238                                 delete cCurve;
239                         
240                                 mothurOut(order->getLabel()); mothurOutEndLine();
241                                 processedLabels.insert(order->getLabel());
242                                 userLabels.erase(order->getLabel());
243                         }
244                         
245                         //you have a label the user want that is smaller than this label and the last label has not already been processed
246                         if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
247                                 string saveLabel = order->getLabel();
248                                 
249                                 delete order;
250                                 order = input->getSharedOrderVector(lastLabel);
251                                 
252                                 //create collectors curve
253                                 cCurve = new Collect(order, cDisplays);
254                                 cCurve->getSharedCurve(freq);
255                                 delete cCurve;
256                         
257                                 mothurOut(order->getLabel()); mothurOutEndLine();
258                                 processedLabels.insert(order->getLabel());
259                                 userLabels.erase(order->getLabel());
260                                 
261                                 //restore real lastlabel to save below
262                                 order->setLabel(saveLabel);
263                         }
264                         
265                         
266                         lastLabel = order->getLabel();                  
267                         
268                         //get next line to process
269                         delete order;
270                         order = input->getSharedOrderVector();
271                 }
272                 
273                 //output error messages about any remaining user labels
274                 set<string>::iterator it;
275                 bool needToRun = false;
276                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
277                         mothurOut("Your file does not include the label " + *it); 
278                         if (processedLabels.count(lastLabel) != 1) {
279                                 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
280                                 needToRun = true;
281                         }else {
282                                 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
283                         }
284                 }
285                 
286                 //run last label if you need to
287                 if (needToRun == true)  {
288                         if (order != NULL) {  delete order;  }
289                         order = input->getSharedOrderVector(lastLabel);
290
291                         cCurve = new Collect(order, cDisplays);
292                         cCurve->getSharedCurve(freq);
293                         delete cCurve;
294                         
295                         mothurOut(order->getLabel()); mothurOutEndLine();
296                         delete order;
297                 }
298                 
299                 for(int i=0;i<cDisplays.size();i++){    delete cDisplays[i];    }       
300                 
301                 //reset groups parameter
302                 globaldata->Groups.clear(); 
303                 
304                 return 0;
305         }
306         catch(exception& e) {
307                 errorOut(e, "CollectSharedCommand", "execute");
308                 exit(1);
309         }
310 }
311
312 /***********************************************************/