]> git.donarmstrong.com Git - mothur.git/blob - summarysharedcommand.cpp
fixed multiple bugs for 1.4 release
[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(){
39         try {
40                 globaldata = GlobalData::getInstance();
41                 outputFileName = ((getRootName(globaldata->inputFileName)) + "shared.summary");
42                 openOutputFile(outputFileName, outputFileHandle);
43                 format = globaldata->getFormat();
44                 validCalculator = new ValidCalculators();
45                 mult = false;
46                 
47                 int i;
48                 for (i=0; i<globaldata->Estimators.size(); i++) {
49                         if (validCalculator->isValidCalculator("sharedsummary", globaldata->Estimators[i]) == true) { 
50                                 if (globaldata->Estimators[i] == "sharedsobs") { 
51                                         sumCalculators.push_back(new SharedSobsCS());
52                                 }else if (globaldata->Estimators[i] == "sharedchao") { 
53                                         sumCalculators.push_back(new SharedChao1());
54                                 }else if (globaldata->Estimators[i] == "sharedace") { 
55                                         sumCalculators.push_back(new SharedAce());
56                                 }else if (globaldata->Estimators[i] == "jabund") {      
57                                         sumCalculators.push_back(new JAbund());
58                                 }else if (globaldata->Estimators[i] == "sorabund") { 
59                                         sumCalculators.push_back(new SorAbund());
60                                 }else if (globaldata->Estimators[i] == "jclass") { 
61                                         sumCalculators.push_back(new Jclass());
62                                 }else if (globaldata->Estimators[i] == "sorclass") { 
63                                         sumCalculators.push_back(new SorClass());
64                                 }else if (globaldata->Estimators[i] == "jest") { 
65                                         sumCalculators.push_back(new Jest());
66                                 }else if (globaldata->Estimators[i] == "sorest") { 
67                                         sumCalculators.push_back(new SorEst());
68                                 }else if (globaldata->Estimators[i] == "thetayc") { 
69                                         sumCalculators.push_back(new ThetaYC());
70                                 }else if (globaldata->Estimators[i] == "thetan") { 
71                                         sumCalculators.push_back(new ThetaN());
72                                 }else if (globaldata->Estimators[i] == "kstest") { 
73                                         sumCalculators.push_back(new KSTest());
74                                 }else if (globaldata->Estimators[i] == "sharednseqs") { 
75                                         sumCalculators.push_back(new SharedNSeqs());
76                                 }else if (globaldata->Estimators[i] == "ochiai") { 
77                                         sumCalculators.push_back(new Ochiai());
78                                 }else if (globaldata->Estimators[i] == "anderberg") { 
79                                         sumCalculators.push_back(new Anderberg());
80                                 }else if (globaldata->Estimators[i] == "kulczynski") { 
81                                         sumCalculators.push_back(new Kulczynski());
82                                 }else if (globaldata->Estimators[i] == "kulczynskicody") { 
83                                         sumCalculators.push_back(new KulczynskiCody());
84                                 }else if (globaldata->Estimators[i] == "lennon") { 
85                                         sumCalculators.push_back(new Lennon());
86                                 }else if (globaldata->Estimators[i] == "morisitahorn") { 
87                                         sumCalculators.push_back(new MorHorn());
88                                 }else if (globaldata->Estimators[i] == "braycurtis") { 
89                                         sumCalculators.push_back(new BrayCurtis());
90                                 }else if (globaldata->Estimators[i] == "whittaker") { 
91                                         sumCalculators.push_back(new Whittaker());
92                                 }
93                         }
94                 }
95                 //reset calc for next command
96                 globaldata->setCalc("");
97
98         }
99         catch(exception& e) {
100                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
101                 exit(1);
102         }
103         catch(...) {
104                 cout << "An unknown error has occurred in the SummarySharedCommand class function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
105                 exit(1);
106         }       
107 }
108 //**********************************************************************************************************************
109
110 SummarySharedCommand::~SummarySharedCommand(){
111         delete input;
112         delete read;
113 }
114
115 //**********************************************************************************************************************
116
117 int SummarySharedCommand::execute(){
118         try {
119                 int count = 1;  
120         
121                 //if the users entered no valid calculators don't execute command
122                 if (sumCalculators.size() == 0) { return 0; }
123                 //check if any calcs can do multiples
124                 else{
125                         for (int i = 0; i < sumCalculators.size(); i++) {
126                                 if (sumCalculators[i]->getMultiple() == true) { mult = true; }
127                         }
128                 }
129                 
130                 //read first line
131                 read = new ReadOTUFile(globaldata->inputFileName);      
132                 read->read(&*globaldata); 
133                         
134                 input = globaldata->ginput;
135                 lookup = input->getSharedRAbundVectors();
136                 
137                 //output estimator names as column headers
138                 outputFileHandle << "label" <<'\t' << "comparison" << '\t'; 
139                 for(int i=0;i<sumCalculators.size();i++){
140                         outputFileHandle << '\t' << sumCalculators[i]->getName();
141                 }
142                 outputFileHandle << endl;
143                 
144                 //create file and put column headers for multiple groups file
145                 if (mult == true) {
146                         outAllFileName = ((getRootName(globaldata->inputFileName)) + "sharedmultiple.summary");
147                         openOutputFile(outAllFileName, outAll);
148                         
149                         outAll << "label" <<'\t' << "comparison" << '\t'; 
150                         for(int i=0;i<sumCalculators.size();i++){
151                                 if (sumCalculators[i]->getMultiple() == true) { 
152                                         outAll << '\t' << sumCalculators[i]->getName();
153                                 }
154                         }
155                         outAll << endl;
156                 }
157                 
158                 if (lookup.size() < 2) { 
159                         cout << "I cannot run the command without at least 2 valid groups."; 
160                         for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
161                         
162                         //close files and clean up
163                         outputFileHandle.close();  remove(outputFileName.c_str());
164                         if (mult == true) {  outAll.close();  remove(outAllFileName.c_str());  }
165                         return 0;
166                 }
167                                         
168                 
169                 while(lookup[0] != NULL){
170                 
171                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(lookup[0]->getLabel()) == 1){                   
172         
173                                 cout << lookup[0]->getLabel() << '\t' << count << endl;
174                                 
175                                 //loop through calculators and add to file all for all calcs that can do mutiple groups
176                                 if (mult == true) {
177                                         //output label
178                                         outAll << lookup[0]->getLabel() << '\t';
179                                         
180                                         //output groups names
181                                         string outNames = "";
182                                         for (int j = 0; j < lookup.size(); j++) {
183                                                 outNames += lookup[j]->getGroup() +  "-";
184                                         }
185                                         outNames = outNames.substr(0, outNames.length()-1); //rip off extra '-';
186                                         outAll << outNames << '\t';
187                                         
188                                         for(int i=0;i<sumCalculators.size();i++){
189                                                 if (sumCalculators[i]->getMultiple() == true) { 
190                                                         sumCalculators[i]->getValues(lookup);
191                                                         outAll << '\t';
192                                                         sumCalculators[i]->print(outAll);
193                                                 }
194                                         }
195                                         outAll << endl;
196                                 }
197         
198                                 int n = 1; 
199                                 vector<SharedRAbundVector*> subset;
200                                 for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
201                                         for (int l = n; l < lookup.size(); l++) {
202                                                 
203                                                 outputFileHandle << lookup[0]->getLabel() << '\t';
204                                                 
205                                                 subset.clear(); //clear out old pair of sharedrabunds
206                                                 //add new pair of sharedrabunds
207                                                 subset.push_back(lookup[k]); subset.push_back(lookup[l]); 
208                                                 
209                                                 //sort groups to be alphanumeric
210                                                 if (lookup[k]->getGroup() > lookup[l]->getGroup()) {
211                                                         outputFileHandle << (lookup[l]->getGroup() +'\t' + lookup[k]->getGroup()) << '\t'; //print out groups
212                                                 }else{
213                                                         outputFileHandle << (lookup[k]->getGroup() +'\t' + lookup[l]->getGroup()) << '\t'; //print out groups
214                                                 }
215                                                 
216                                                 for(int i=0;i<sumCalculators.size();i++) {
217
218                                                         sumCalculators[i]->getValues(subset); //saves the calculator outputs
219                                                         outputFileHandle << '\t';
220                                                         sumCalculators[i]->print(outputFileHandle);
221                                                 }
222                                                 outputFileHandle << endl;
223                                         }
224                                         n++;
225                                 }
226                         }
227                 
228                         //prevent memory leak
229                         for (int i = 0; i < lookup.size(); i++) {       delete lookup[i];       }
230                                 
231                         //get next line to process
232                         lookup = input->getSharedRAbundVectors();
233                         count++;
234                 }
235                 
236                 //reset groups parameter
237                 globaldata->Groups.clear();  globaldata->setGroups("");
238                 
239                 //close files
240                 outputFileHandle.close();
241                 if (mult == true) {  outAll.close();  }
242
243                 return 0;
244         }
245         catch(exception& e) {
246                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
247                 exit(1);
248         }
249         catch(...) {
250                 cout << "An unknown error has occurred in the SummarySharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
251                 exit(1);
252         }               
253 }
254
255 /***********************************************************/