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