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