]> git.donarmstrong.com Git - mothur.git/blob - summarysharedcommand.cpp
*** empty log message ***
[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                 if (format == "sharedfile") {
133                         read = new ReadOTUFile(globaldata->inputFileName);      
134                         read->read(&*globaldata); 
135                         
136                         input = globaldata->ginput;
137                         order = input->getSharedOrderVector();
138                 }else {
139                         //you are using a list and a groupfile
140                         read = new ReadOTUFile(globaldata->inputFileName);      
141                         read->read(&*globaldata); 
142                 
143                         input = globaldata->ginput;
144                         SharedList = globaldata->gSharedList;
145                         order = SharedList->getSharedOrderVector();
146                 }
147                 
148                 //set users groups
149                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups, "summary");
150                 
151                 //output estimator names as column headers
152                 outputFileHandle << "label" <<'\t' << "comparison" << '\t'; 
153                 for(int i=0;i<sumCalculators.size();i++){
154                         outputFileHandle << '\t' << sumCalculators[i]->getName();
155                 }
156                 outputFileHandle << endl;
157                 
158                 //create file and put column headers for multiple groups file
159                 if (mult = true) {
160                         outAllFileName = ((getRootName(globaldata->inputFileName)) + "sharedmultiple.summary");
161                         openOutputFile(outAllFileName, outAll);
162                         
163                         outAll << "label" <<'\t' << "comparison" << '\t'; 
164                         for(int i=0;i<sumCalculators.size();i++){
165                                 if (sumCalculators[i]->getMultiple() == true) { 
166                                         outAll << '\t' << sumCalculators[i]->getName();
167                                 }
168                         }
169                         outAll << endl;
170                 }
171                 
172                 while(order != NULL){
173                 
174                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){                       
175         
176                                 cout << order->getLabel() << '\t' << count << endl;
177                                 util->getSharedVectors(globaldata->Groups, lookup, order);  //fills group vectors from order vector.  //fills group vectors from order vector.
178                                 
179                                 //loop through calculators and add to file all for all calcs that can do mutiple groups
180                                 if (mult = true) {
181                                         //output label
182                                         outAll << order->getLabel() << '\t';
183                                         
184                                         //output groups names
185                                         string outNames = "";
186                                         for (int j = 0; j < lookup.size(); j++) {
187                                                 outNames += lookup[j]->getGroup() +  "-";
188                                         }
189                                         outNames = outNames.substr(0, outNames.length()-1); //rip off extra '-';
190                                         outAll << outNames << '\t';
191                                         
192                                         for(int i=0;i<sumCalculators.size();i++){
193                                                 if (sumCalculators[i]->getMultiple() == true) { 
194                                                         sumCalculators[i]->getValues(lookup);
195                                                         outAll << '\t';
196                                                         sumCalculators[i]->print(outAll);
197                                                 }
198                                         }
199                                         outAll << endl;
200                                 }
201
202                                 int n = 1; 
203                                 vector<SharedRAbundVector*> subset;
204                                 for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
205                                         for (int l = n; l < lookup.size(); l++) {
206                                                 
207                                                 outputFileHandle << order->getLabel() << '\t';
208                                                 
209                                                 subset.clear(); //clear out old pair of sharedrabunds
210                                                 //add new pair of sharedrabunds
211                                                 subset.push_back(lookup[k]); subset.push_back(lookup[l]); 
212                                                 
213                                                 //sort groups to be alphanumeric
214                                                 if (lookup[k]->getGroup() > lookup[l]->getGroup()) {
215                                                         outputFileHandle << (lookup[l]->getGroup() +'\t' + lookup[k]->getGroup()) << '\t'; //print out groups
216                                                 }else{
217                                                         outputFileHandle << (lookup[k]->getGroup() +'\t' + lookup[l]->getGroup()) << '\t'; //print out groups
218                                                 }
219                                                 
220                                                 for(int i=0;i<sumCalculators.size();i++) {
221                                                         sumCalculators[i]->getValues(subset); //saves the calculator outputs
222                                                         outputFileHandle << '\t';
223                                                         sumCalculators[i]->print(outputFileHandle);
224                                                 }
225                                                 outputFileHandle << endl;
226                                         }
227                                         n++;
228                                 }
229                         }
230                 
231                         //get next line to process
232                         if (format == "sharedfile") {
233                                 order = input->getSharedOrderVector();
234                         }else {
235                                 //you are using a list and a groupfile
236                                 SharedList = input->getSharedListVector(); //get new list vector to process
237                                 if (SharedList != NULL) {
238                                         order = SharedList->getSharedOrderVector(); //gets new order vector with group info.
239                                 }else {
240                                         break;
241                                 }
242                         }
243                         count++;
244                 }
245                 
246                 //reset groups parameter
247                 globaldata->Groups.clear();  globaldata->setGroups("");
248
249                 return 0;
250         }
251         catch(exception& e) {
252                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
253                 exit(1);
254         }
255         catch(...) {
256                 cout << "An unknown error has occurred in the SummarySharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
257                 exit(1);
258         }               
259 }
260
261 /***********************************************************/