]> git.donarmstrong.com Git - mothur.git/blob - summarysharedcommand.cpp
started shared utilities, updates to venn and heatmap added tree.groups command
[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 "sharedbdiversity.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] == "sharedjabund") {        
55                                         sumCalculators.push_back(new SharedJAbund());
56                                 }else if (globaldata->Estimators[i] == "sharedsorensonabund") { 
57                                         sumCalculators.push_back(new SharedSorAbund());
58                                 }else if (globaldata->Estimators[i] == "sharedjclass") { 
59                                         sumCalculators.push_back(new SharedJclass());
60                                 }else if (globaldata->Estimators[i] == "sharedsorclass") { 
61                                         sumCalculators.push_back(new SharedSorClass());
62                                 }else if (globaldata->Estimators[i] == "sharedjest") { 
63                                         sumCalculators.push_back(new SharedJest());
64                                 }else if (globaldata->Estimators[i] == "sharedsorest") { 
65                                         sumCalculators.push_back(new SharedSorEst());
66                                 }else if (globaldata->Estimators[i] == "sharedthetayc") { 
67                                         sumCalculators.push_back(new SharedThetaYC());
68                                 }else if (globaldata->Estimators[i] == "sharedthetan") { 
69                                         sumCalculators.push_back(new SharedThetaN());
70                                 }else if (globaldata->Estimators[i] == "sharedkstest") { 
71                                         sumCalculators.push_back(new SharedKSTest());
72                                 }else if (globaldata->Estimators[i] == "sharednseqs") { 
73                                         sumCalculators.push_back(new SharedNSeqs());
74                                 }else if (globaldata->Estimators[i] == "sharedochiai") { 
75                                         sumCalculators.push_back(new SharedOchiai());
76                                 }else if (globaldata->Estimators[i] == "sharedanderberg") { 
77                                         sumCalculators.push_back(new SharedAnderberg());
78                                 }else if (globaldata->Estimators[i] == "sharedkulczynski") { 
79                                         sumCalculators.push_back(new SharedKulczynski());
80                                 }else if (globaldata->Estimators[i] == "sharedkulczynskicody") { 
81                                         sumCalculators.push_back(new SharedKulczynskiCody());
82                                 }else if (globaldata->Estimators[i] == "sharedlennon") { 
83                                         sumCalculators.push_back(new SharedLennon());
84                                 }else if (globaldata->Estimators[i] == "sharedmorisitahorn") { 
85                                         sumCalculators.push_back(new SharedMorHorn());
86                                 }else if (globaldata->Estimators[i] == "sharedbraycurtis") { 
87                                         sumCalculators.push_back(new SharedBrayCurtis());
88                                 }
89                                 else if (globaldata->Estimators[i] == "sharedbdiversity") { 
90                                         sumCalculators.push_back(new SharedBDiversity());
91                                 }
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         delete util;
114 }
115
116 //**********************************************************************************************************************
117
118 int SummarySharedCommand::execute(){
119         try {
120                 int count = 1;  
121                 
122                 //if the users entered no valid calculators don't execute command
123                 if (sumCalculators.size() == 0) { return 0; }
124
125                 if (format == "sharedfile") {
126                         read = new ReadPhilFile(globaldata->inputFileName);     
127                         read->read(&*globaldata); 
128                         
129                         input = globaldata->ginput;
130                         order = input->getSharedOrderVector();
131                 }else {
132                         //you are using a list and a groupfile
133                         read = new ReadPhilFile(globaldata->inputFileName);     
134                         read->read(&*globaldata); 
135                 
136                         input = globaldata->ginput;
137                         SharedList = globaldata->gSharedList;
138                         order = SharedList->getSharedOrderVector();
139                 }
140                 
141                 //set users groups
142                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups, "summary");
143                 
144                 //output estimator names as column headers
145                 outputFileHandle << "label" <<'\t' << "comparison" << '\t'; 
146                 for(int i=0;i<sumCalculators.size();i++){
147                         outputFileHandle << '\t' << sumCalculators[i]->getName();
148                 }
149                 outputFileHandle << endl;
150                 
151                 while(order != NULL){
152                 
153                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){                       
154         
155                                 cout << order->getLabel() << '\t' << count << endl;
156                                 util->getSharedVectors(globaldata->Groups, lookup, order);  //fills group vectors from order vector.  //fills group vectors from order vector.
157                                 
158                                 //randomize group order
159                                 if (globaldata->getJumble() == "1") { random_shuffle(lookup.begin(), lookup.end()); }
160
161                                 int n = 1; 
162                                 for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
163                                         for (int l = n; l < lookup.size(); l++) {
164                                                 
165                                                 outputFileHandle << order->getLabel() << '\t';
166                                                 
167                                                 //sort groups to be alphanumeric
168                                                 if (lookup[k]->getGroup() > lookup[l]->getGroup()) {
169                                                         outputFileHandle << (lookup[l]->getGroup() +'\t' + lookup[k]->getGroup()) << '\t'; //print out groups
170                                                 }else{
171                                                         outputFileHandle << (lookup[k]->getGroup() +'\t' + lookup[l]->getGroup()) << '\t'; //print out groups
172                                                 }
173                                                 
174                                                 for(int i=0;i<sumCalculators.size();i++){
175                                                         sumCalculators[i]->getValues(lookup[k], lookup[l]); //saves the calculator outputs
176                                                         outputFileHandle << '\t';
177                                                         sumCalculators[i]->print(outputFileHandle);
178                                                 }
179                                                 outputFileHandle << endl;
180                                         }
181                                         n++;
182                                 }
183                         }
184                 
185                         //get next line to process
186                         if (format == "sharedfile") {
187                                 order = input->getSharedOrderVector();
188                         }else {
189                                 //you are using a list and a groupfile
190                                 SharedList = input->getSharedListVector(); //get new list vector to process
191                                 if (SharedList != NULL) {
192                                         order = SharedList->getSharedOrderVector(); //gets new order vector with group info.
193                                 }else {
194                                         break;
195                                 }
196                         }
197                         count++;
198                 }
199                 
200                 //reset groups parameter
201                 globaldata->Groups.clear();  globaldata->setGroups("");
202
203                 return 0;
204         }
205         catch(exception& e) {
206                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
207                 exit(1);
208         }
209         catch(...) {
210                 cout << "An unknown error has occurred in the SummarySharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
211                 exit(1);
212         }               
213 }
214
215 /***********************************************************/