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