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