]> git.donarmstrong.com Git - mothur.git/blob - summarysharedcommand.cpp
added hcluster command and fixed some bugs, namely one with smart distancing.
[mothur.git] / summarysharedcommand.cpp
1 /*
2  *  summarysharedcommand.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 "summarysharedcommand.h"
11 #include "sharedsobscollectsummary.h"
12 #include "sharedchao1.h"
13 #include "sharedace.h"
14 #include "sharednseqs.h"
15 #include "sharedjabund.h"
16 #include "sharedsorabund.h"
17 #include "sharedjclass.h"
18 #include "sharedsorclass.h"
19 #include "sharedjest.h"
20 #include "sharedsorest.h"
21 #include "sharedthetayc.h"
22 #include "sharedthetan.h"
23 #include "sharedkstest.h"
24 #include "whittaker.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 SummarySharedCommand::SummarySharedCommand(string option){
39         try {
40                 globaldata = GlobalData::getInstance();
41                 abort = false;
42                 allLines = 1;
43                 labels.clear();
44                 Estimators.clear();
45                 
46                 //allow user to run help
47                 if(option == "help") { validCalculator = new ValidCalculators(); help(); abort = true; }
48                 
49                 else {
50                         //valid paramters for this command
51                         string Array[] =  {"label","calc","groups"};
52                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
53                         
54                         OptionParser parser(option);
55                         map<string, string> parameters = parser.getParameters();
56                         
57                         ValidParameters validParameter;
58                 
59                         //check to make sure all parameters are valid for command
60                         for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
61                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
62                         }
63                         
64                         //make sure the user has already run the read.otu command
65                         if (globaldata->getSharedFile() == "") {
66                                  mothurOut("You must read a list and a group, or a shared before you can use the summary.shared command."); mothurOutEndLine(); abort = true; 
67                         }
68                         
69                         //check for optional parameter and set defaults
70                         // ...at some point should added some additional type checking...
71                         label = validParameter.validFile(parameters, "label", false);                   
72                         if (label == "not found") { label = ""; }
73                         else { 
74                                 if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
75                                 else { allLines = 1;  }
76                         }
77                         
78                         //if the user has not specified any labels use the ones from read.otu
79                         if(label == "") {  
80                                 allLines = globaldata->allLines; 
81                                 labels = globaldata->labels; 
82                         }
83                                 
84                         calc = validParameter.validFile(parameters, "calc", false);                     
85                         if (calc == "not found") { calc = "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan";  }
86                         else { 
87                                  if (calc == "default")  {  calc = "sharedsobs-sharedchao-sharedace-jabund-sorabund-jclass-sorclass-jest-sorest-thetayc-thetan";  }
88                         }
89                         splitAtDash(calc, Estimators);
90                         
91                         groups = validParameter.validFile(parameters, "groups", false);                 
92                         if (groups == "not found") { groups = ""; }
93                         else { 
94                                 splitAtDash(groups, Groups);
95                                 globaldata->Groups = Groups;
96                         }
97                         
98                         if (abort == false) {
99                         
100                                 validCalculator = new ValidCalculators();
101                                 int i;
102                                 
103                                 for (i=0; i<Estimators.size(); i++) {
104                                         if (validCalculator->isValidCalculator("sharedsummary", Estimators[i]) == true) { 
105                                                 if (Estimators[i] == "sharedsobs") { 
106                                                         sumCalculators.push_back(new SharedSobsCS());
107                                                 }else if (Estimators[i] == "sharedchao") { 
108                                                         sumCalculators.push_back(new SharedChao1());
109                                                 }else if (Estimators[i] == "sharedace") { 
110                                                         sumCalculators.push_back(new SharedAce());
111                                                 }else if (Estimators[i] == "jabund") {  
112                                                         sumCalculators.push_back(new JAbund());
113                                                 }else if (Estimators[i] == "sorabund") { 
114                                                         sumCalculators.push_back(new SorAbund());
115                                                 }else if (Estimators[i] == "jclass") { 
116                                                         sumCalculators.push_back(new Jclass());
117                                                 }else if (Estimators[i] == "sorclass") { 
118                                                         sumCalculators.push_back(new SorClass());
119                                                 }else if (Estimators[i] == "jest") { 
120                                                         sumCalculators.push_back(new Jest());
121                                                 }else if (Estimators[i] == "sorest") { 
122                                                         sumCalculators.push_back(new SorEst());
123                                                 }else if (Estimators[i] == "thetayc") { 
124                                                         sumCalculators.push_back(new ThetaYC());
125                                                 }else if (Estimators[i] == "thetan") { 
126                                                         sumCalculators.push_back(new ThetaN());
127                                                 }else if (Estimators[i] == "kstest") { 
128                                                         sumCalculators.push_back(new KSTest());
129                                                 }else if (Estimators[i] == "sharednseqs") { 
130                                                         sumCalculators.push_back(new SharedNSeqs());
131                                                 }else if (Estimators[i] == "ochiai") { 
132                                                         sumCalculators.push_back(new Ochiai());
133                                                 }else if (Estimators[i] == "anderberg") { 
134                                                         sumCalculators.push_back(new Anderberg());
135                                                 }else if (Estimators[i] == "kulczynski") { 
136                                                         sumCalculators.push_back(new Kulczynski());
137                                                 }else if (Estimators[i] == "kulczynskicody") { 
138                                                         sumCalculators.push_back(new KulczynskiCody());
139                                                 }else if (Estimators[i] == "lennon") { 
140                                                         sumCalculators.push_back(new Lennon());
141                                                 }else if (Estimators[i] == "morisitahorn") { 
142                                                         sumCalculators.push_back(new MorHorn());
143                                                 }else if (Estimators[i] == "braycurtis") { 
144                                                         sumCalculators.push_back(new BrayCurtis());
145                                                 }else if (Estimators[i] == "whittaker") { 
146                                                         sumCalculators.push_back(new Whittaker());
147                                                 }
148                                         }
149                                 }
150                                 
151                                 outputFileName = ((getRootName(globaldata->inputFileName)) + "shared.summary");
152                                 openOutputFile(outputFileName, outputFileHandle);
153                                 mult = false;
154                         }
155                 }
156         }
157         catch(exception& e) {
158                 errorOut(e, "SummarySharedCommand", "SummarySharedCommand");
159                 exit(1);
160         }
161 }
162
163 //**********************************************************************************************************************
164
165 void SummarySharedCommand::help(){
166         try {
167                 mothurOut("The summary.shared command can only be executed after a successful read.otu command.\n");
168                 mothurOut("The summary.shared command parameters are label and calc.  No parameters are required.\n");
169                 mothurOut("The summary.shared command should be in the following format: \n");
170                 mothurOut("summary.shared(label=yourLabel, calc=yourEstimators, groups=yourGroups).\n");
171                 mothurOut("Example summary.shared(label=unique-.01-.03, groups=B-C, calc=sharedchao-sharedace-jabund-sorensonabund-jclass-sorclass-jest-sorest-thetayc-thetan).\n");
172                 validCalculator->printCalc("sharedsummary", cout);
173                 mothurOut("The default value for calc is sharedsobs-sharedchao-sharedace-jabund-sorensonabund-jclass-sorclass-jest-sorest-thetayc-thetan\n");
174                 mothurOut("The default value for groups is all the groups in your groupfile.\n");
175                 mothurOut("The label parameter is used to analyze specific labels in your input.\n");
176                 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");
177                 mothurOut("Note: No spaces between parameter labels (i.e. label), '=' and parameters (i.e.yourLabel).\n\n");
178         }
179         catch(exception& e) {
180                 errorOut(e, "SummarySharedCommand", "help");
181                 exit(1);
182         }
183 }
184
185 //**********************************************************************************************************************
186
187 SummarySharedCommand::~SummarySharedCommand(){
188         if (abort == false) {
189                 delete read;
190                 delete validCalculator;
191         }
192 }
193
194 //**********************************************************************************************************************
195
196 int SummarySharedCommand::execute(){
197         try {
198         
199                 if (abort == true) { return 0; }
200         
201                 //if the users entered no valid calculators don't execute command
202                 if (sumCalculators.size() == 0) { return 0; }
203                 //check if any calcs can do multiples
204                 else{
205                         for (int i = 0; i < sumCalculators.size(); i++) {
206                                 if (sumCalculators[i]->getMultiple() == true) { mult = true; }
207                         }
208                 }
209                 
210                 //read first line
211                 read = new ReadOTUFile(globaldata->inputFileName);      
212                 read->read(&*globaldata); 
213                         
214                 input = globaldata->ginput;
215                 lookup = input->getSharedRAbundVectors();
216                 string lastLabel = lookup[0]->getLabel();
217                 
218                 //output estimator names as column headers
219                 outputFileHandle << "label" <<'\t' << "comparison" << '\t'; 
220                 for(int i=0;i<sumCalculators.size();i++){
221                         outputFileHandle << '\t' << sumCalculators[i]->getName();
222                 }
223                 outputFileHandle << endl;
224                 
225                 //create file and put column headers for multiple groups file
226                 if (mult == true) {
227                         outAllFileName = ((getRootName(globaldata->inputFileName)) + "sharedmultiple.summary");
228                         openOutputFile(outAllFileName, outAll);
229                         
230                         outAll << "label" <<'\t' << "comparison" << '\t'; 
231                         for(int i=0;i<sumCalculators.size();i++){
232                                 if (sumCalculators[i]->getMultiple() == true) { 
233                                         outAll << '\t' << sumCalculators[i]->getName();
234                                 }
235                         }
236                         outAll << endl;
237                 }
238                 
239                 if (lookup.size() < 2) { 
240                         mothurOut("I cannot run the command without at least 2 valid groups."); 
241                         for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
242                         
243                         //close files and clean up
244                         outputFileHandle.close();  remove(outputFileName.c_str());
245                         if (mult == true) {  outAll.close();  remove(outAllFileName.c_str());  }
246                         return 0;
247                 //if you only have 2 groups you don't need a .sharedmultiple file
248                 }else if ((lookup.size() == 2) && (mult == true)) { 
249                         mult = false;
250                         outAll.close();  
251                         remove(outAllFileName.c_str());
252                 }
253                                         
254                 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
255                 set<string> processedLabels;
256                 set<string> userLabels = labels;
257                         
258                 //as long as you are not at the end of the file or done wih the lines you want
259                 while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
260                 
261                         if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
262                                 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
263                                 process(lookup);
264                                 
265                                 processedLabels.insert(lookup[0]->getLabel());
266                                 userLabels.erase(lookup[0]->getLabel());
267                         }
268                         
269                         if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
270                                         string saveLabel = lookup[0]->getLabel();
271                                         
272                                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
273                                         lookup = input->getSharedRAbundVectors(lastLabel);
274
275                                         mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
276                                         process(lookup);
277                                         
278                                         processedLabels.insert(lookup[0]->getLabel());
279                                         userLabels.erase(lookup[0]->getLabel());
280                                         
281                                         //restore real lastlabel to save below
282                                         lookup[0]->setLabel(saveLabel);
283                         }
284                         
285                         lastLabel = lookup[0]->getLabel();                      
286                                 
287                         //get next line to process
288                         //prevent memory leak
289                         for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
290                         lookup = input->getSharedRAbundVectors();
291                 }
292                 
293                 //output error messages about any remaining user labels
294                 set<string>::iterator it;
295                 bool needToRun = false;
296                 for (it = userLabels.begin(); it != userLabels.end(); it++) {  
297                         mothurOut("Your file does not include the label " + *it); 
298                         if (processedLabels.count(lastLabel) != 1) {
299                                 mothurOut(". I will use " + lastLabel + "."); mothurOutEndLine();
300                                 needToRun = true;
301                         }else {
302                                 mothurOut(". Please refer to " + lastLabel + "."); mothurOutEndLine();
303                         }
304                 }
305                 
306                 //run last label if you need to
307                 if (needToRun == true)  {
308                                 for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {     delete lookup[i];       } } 
309                                 lookup = input->getSharedRAbundVectors(lastLabel);
310
311                                 mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
312                                 process(lookup);
313                                 for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
314                 }
315                 
316
317                 //reset groups parameter
318                 globaldata->Groups.clear();  
319                 
320                 //close files
321                 outputFileHandle.close();
322                 if (mult == true) {  outAll.close();  }
323                 
324                 for(int i=0;i<sumCalculators.size();i++){  delete sumCalculators[i]; }
325                 
326                 delete input;  globaldata->ginput = NULL;
327
328                 return 0;
329         }
330         catch(exception& e) {
331                 errorOut(e, "SummarySharedCommand", "execute");
332                 exit(1);
333         }
334 }
335
336 /***********************************************************/
337 void SummarySharedCommand::process(vector<SharedRAbundVector*> thisLookup) {
338         try {
339                                 //loop through calculators and add to file all for all calcs that can do mutiple groups
340                                 if (mult == true) {
341                                         //output label
342                                         outAll << thisLookup[0]->getLabel() << '\t';
343                                         
344                                         //output groups names
345                                         string outNames = "";
346                                         for (int j = 0; j < thisLookup.size(); j++) {
347                                                 outNames += thisLookup[j]->getGroup() +  "-";
348                                         }
349                                         outNames = outNames.substr(0, outNames.length()-1); //rip off extra '-';
350                                         outAll << outNames << '\t';
351                                         
352                                         for(int i=0;i<sumCalculators.size();i++){
353                                                 if (sumCalculators[i]->getMultiple() == true) { 
354                                                         sumCalculators[i]->getValues(thisLookup);
355                                                         outAll << '\t';
356                                                         sumCalculators[i]->print(outAll);
357                                                 }
358                                         }
359                                         outAll << endl;
360                                 }
361         
362                                 int n = 1; 
363                                 vector<SharedRAbundVector*> subset;
364                                 for (int k = 0; k < (thisLookup.size() - 1); k++) { // pass cdd each set of groups to commpare
365                                         for (int l = n; l < thisLookup.size(); l++) {
366                                                 
367                                                 outputFileHandle << thisLookup[0]->getLabel() << '\t';
368                                                 
369                                                 subset.clear(); //clear out old pair of sharedrabunds
370                                                 //add new pair of sharedrabunds
371                                                 subset.push_back(thisLookup[k]); subset.push_back(thisLookup[l]); 
372                                                 
373                                                 //sort groups to be alphanumeric
374                                                 if (thisLookup[k]->getGroup() > thisLookup[l]->getGroup()) {
375                                                         outputFileHandle << (thisLookup[l]->getGroup() +'\t' + thisLookup[k]->getGroup()) << '\t'; //print out groups
376                                                 }else{
377                                                         outputFileHandle << (thisLookup[k]->getGroup() +'\t' + thisLookup[l]->getGroup()) << '\t'; //print out groups
378                                                 }
379                                                 
380                                                 for(int i=0;i<sumCalculators.size();i++) {
381
382                                                         sumCalculators[i]->getValues(subset); //saves the calculator outputs
383                                                         outputFileHandle << '\t';
384                                                         sumCalculators[i]->print(outputFileHandle);
385                                                 }
386                                                 outputFileHandle << endl;
387                                         }
388                                         n++;
389                                 }
390
391         }
392         catch(exception& e) {
393                 errorOut(e, "SummarySharedCommand", "process");
394                 exit(1);
395         }
396 }
397
398 /***********************************************************/