]> git.donarmstrong.com Git - mothur.git/blob - summarysharedcommand.cpp
This is mothur v 1.2.0 - the April ~24, 2009 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
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                 
45                 int i;
46                 for (i=0; i<globaldata->Estimators.size(); i++) {
47                         if (validCalculator->isValidCalculator("sharedsummary", globaldata->Estimators[i]) == true) { 
48                                 if (globaldata->Estimators[i] == "sharedsobs") { 
49                                         sumCalculators.push_back(new SharedSobsCS());
50                                 }else if (globaldata->Estimators[i] == "sharedchao") { 
51                                         sumCalculators.push_back(new SharedChao1());
52                                 }else if (globaldata->Estimators[i] == "sharedace") { 
53                                         sumCalculators.push_back(new SharedAce());
54                                 }else if (globaldata->Estimators[i] == "jabund") {      
55                                         sumCalculators.push_back(new JAbund());
56                                 }else if (globaldata->Estimators[i] == "sorabund") { 
57                                         sumCalculators.push_back(new SorAbund());
58                                 }else if (globaldata->Estimators[i] == "jclass") { 
59                                         sumCalculators.push_back(new Jclass());
60                                 }else if (globaldata->Estimators[i] == "sorclass") { 
61                                         sumCalculators.push_back(new SorClass());
62                                 }else if (globaldata->Estimators[i] == "jest") { 
63                                         sumCalculators.push_back(new Jest());
64                                 }else if (globaldata->Estimators[i] == "sorest") { 
65                                         sumCalculators.push_back(new SorEst());
66                                 }else if (globaldata->Estimators[i] == "thetayc") { 
67                                         sumCalculators.push_back(new ThetaYC());
68                                 }else if (globaldata->Estimators[i] == "thetan") { 
69                                         sumCalculators.push_back(new ThetaN());
70                                 }else if (globaldata->Estimators[i] == "kstest") { 
71                                         sumCalculators.push_back(new KSTest());
72                                 }else if (globaldata->Estimators[i] == "sharednseqs") { 
73                                         sumCalculators.push_back(new SharedNSeqs());
74                                 }else if (globaldata->Estimators[i] == "ochiai") { 
75                                         sumCalculators.push_back(new Ochiai());
76                                 }else if (globaldata->Estimators[i] == "anderberg") { 
77                                         sumCalculators.push_back(new Anderberg());
78                                 }else if (globaldata->Estimators[i] == "kulczynski") { 
79                                         sumCalculators.push_back(new Kulczynski());
80                                 }else if (globaldata->Estimators[i] == "kulczynskicody") { 
81                                         sumCalculators.push_back(new KulczynskiCody());
82                                 }else if (globaldata->Estimators[i] == "lennon") { 
83                                         sumCalculators.push_back(new Lennon());
84                                 }else if (globaldata->Estimators[i] == "morisitahorn") { 
85                                         sumCalculators.push_back(new MorHorn());
86                                 }else if (globaldata->Estimators[i] == "braycurtis") { 
87                                         sumCalculators.push_back(new BrayCurtis());
88                                 }else if (globaldata->Estimators[i] == "whittaker") { 
89                                         sumCalculators.push_back(new Whittaker());
90                                 }
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
124                 if (format == "sharedfile") {
125                         read = new ReadOTUFile(globaldata->inputFileName);      
126                         read->read(&*globaldata); 
127                         
128                         input = globaldata->ginput;
129                         order = input->getSharedOrderVector();
130                 }else {
131                         //you are using a list and a groupfile
132                         read = new ReadOTUFile(globaldata->inputFileName);      
133                         read->read(&*globaldata); 
134                 
135                         input = globaldata->ginput;
136                         SharedList = globaldata->gSharedList;
137                         order = SharedList->getSharedOrderVector();
138                 }
139                 
140                 //set users groups
141                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups, "summary");
142                 
143                 //output estimator names as column headers
144                 outputFileHandle << "label" <<'\t' << "comparison" << '\t'; 
145                 for(int i=0;i<sumCalculators.size();i++){
146                         outputFileHandle << '\t' << sumCalculators[i]->getName();
147                 }
148                 outputFileHandle << endl;
149                 
150                 while(order != NULL){
151                 
152                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){                       
153         
154                                 cout << order->getLabel() << '\t' << count << endl;
155                                 util->getSharedVectors(globaldata->Groups, lookup, order);  //fills group vectors from order vector.  //fills group vectors from order vector.
156                                 
157                                 //randomize group order
158                                 if (globaldata->getJumble() == "1") { random_shuffle(lookup.begin(), lookup.end()); }
159
160                                 int n = 1; 
161                                 for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
162                                         for (int l = n; l < lookup.size(); l++) {
163                                                 
164                                                 outputFileHandle << order->getLabel() << '\t';
165                                                 
166                                                 //sort groups to be alphanumeric
167                                                 if (lookup[k]->getGroup() > lookup[l]->getGroup()) {
168                                                         outputFileHandle << (lookup[l]->getGroup() +'\t' + lookup[k]->getGroup()) << '\t'; //print out groups
169                                                 }else{
170                                                         outputFileHandle << (lookup[k]->getGroup() +'\t' + lookup[l]->getGroup()) << '\t'; //print out groups
171                                                 }
172                                                 
173                                                 for(int i=0;i<sumCalculators.size();i++){
174                                                         sumCalculators[i]->getValues(lookup[k], lookup[l]); //saves the calculator outputs
175                                                         outputFileHandle << '\t';
176                                                         sumCalculators[i]->print(outputFileHandle);
177                                                 }
178                                                 outputFileHandle << endl;
179                                         }
180                                         n++;
181                                 }
182                         }
183                 
184                         //get next line to process
185                         if (format == "sharedfile") {
186                                 order = input->getSharedOrderVector();
187                         }else {
188                                 //you are using a list and a groupfile
189                                 SharedList = input->getSharedListVector(); //get new list vector to process
190                                 if (SharedList != NULL) {
191                                         order = SharedList->getSharedOrderVector(); //gets new order vector with group info.
192                                 }else {
193                                         break;
194                                 }
195                         }
196                         count++;
197                 }
198                 
199                 //reset groups parameter
200                 globaldata->Groups.clear();  globaldata->setGroups("");
201
202                 return 0;
203         }
204         catch(exception& e) {
205                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
206                 exit(1);
207         }
208         catch(...) {
209                 cout << "An unknown error has occurred in the SummarySharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
210                 exit(1);
211         }               
212 }
213
214 /***********************************************************/