]> git.donarmstrong.com Git - mothur.git/blob - summarysharedcommand.cpp
added sharedochiai and sharedanderberg calculators
[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 "sharedochiai.h"
24 #include "sharedanderberg.h"
25
26
27 //**********************************************************************************************************************
28
29 SummarySharedCommand::SummarySharedCommand(){
30         try {
31                 globaldata = GlobalData::getInstance();
32                 outputFileName = ((getRootName(globaldata->inputFileName)) + "shared.summary");
33                 openOutputFile(outputFileName, outputFileHandle);
34                 format = globaldata->getFormat();
35                 validCalculator = new ValidCalculators();
36                 
37                 int i;
38                 for (i=0; i<globaldata->Estimators.size(); i++) {
39                         if (validCalculator->isValidCalculator("sharedsummary", globaldata->Estimators[i]) == true) { 
40                                 if (globaldata->Estimators[i] == "sharedsobs") { 
41                                         sumCalculators.push_back(new SharedSobsCS());
42                                 }else if (globaldata->Estimators[i] == "sharedchao") { 
43                                         sumCalculators.push_back(new SharedChao1());
44                                 }else if (globaldata->Estimators[i] == "sharedace") { 
45                                         sumCalculators.push_back(new SharedAce());
46                                 }else if (globaldata->Estimators[i] == "sharedjabund") {        
47                                         sumCalculators.push_back(new SharedJAbund());
48                                 }else if (globaldata->Estimators[i] == "sharedsorensonabund") { 
49                                         sumCalculators.push_back(new SharedSorAbund());
50                                 }else if (globaldata->Estimators[i] == "sharedjclass") { 
51                                         sumCalculators.push_back(new SharedJclass());
52                                 }else if (globaldata->Estimators[i] == "sharedsorclass") { 
53                                         sumCalculators.push_back(new SharedSorClass());
54                                 }else if (globaldata->Estimators[i] == "sharedjest") { 
55                                         sumCalculators.push_back(new SharedJest());
56                                 }else if (globaldata->Estimators[i] == "sharedsorest") { 
57                                         sumCalculators.push_back(new SharedSorEst());
58                                 }else if (globaldata->Estimators[i] == "sharedthetayc") { 
59                                         sumCalculators.push_back(new SharedThetaYC());
60                                 }else if (globaldata->Estimators[i] == "sharedthetan") { 
61                                         sumCalculators.push_back(new SharedThetaN());
62                                 }else if (globaldata->Estimators[i] == "sharednseqs") { 
63                                         sumCalculators.push_back(new SharedNSeqs());
64                                 }else if (globaldata->Estimators[i] == "sharedochiai") { 
65                                         sumCalculators.push_back(new SharedOchiai());
66                                 }else if (globaldata->Estimators[i] == "sharedanderberg") { 
67                                         sumCalculators.push_back(new SharedAnderberg());
68                                 }
69
70
71                         }
72                 }
73                 
74                 //reset calc for next command
75                 globaldata->setCalc("");
76
77         }
78         catch(exception& e) {
79                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
80                 exit(1);
81         }
82         catch(...) {
83                 cout << "An unknown error has occurred in the SummarySharedCommand class function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
84                 exit(1);
85         }       
86 }
87 //**********************************************************************************************************************
88
89 SummarySharedCommand::~SummarySharedCommand(){
90         delete input;
91         delete read;
92 }
93
94 //**********************************************************************************************************************
95
96 int SummarySharedCommand::execute(){
97         try {
98                 int count = 1;  
99                 
100                 //if the users entered no valid calculators don't execute command
101                 if (sumCalculators.size() == 0) { return 0; }
102
103                 if (format == "sharedfile") {
104                         read = new ReadPhilFile(globaldata->inputFileName);     
105                         read->read(&*globaldata); 
106                         
107                         input = globaldata->ginput;
108                         order = input->getSharedOrderVector();
109                 }else {
110                         //you are using a list and a groupfile
111                         read = new ReadPhilFile(globaldata->inputFileName);     
112                         read->read(&*globaldata); 
113                 
114                         input = globaldata->ginput;
115                         SharedList = globaldata->gSharedList;
116                         order = SharedList->getSharedOrderVector();
117                 }
118                 
119                 //set users groups
120                 setGroups();
121                 
122                 //output estimator names as column headers
123                 outputFileHandle << "label" <<'\t' << "comparison" << '\t'; 
124                 for(int i=0;i<sumCalculators.size();i++){
125                         outputFileHandle << '\t' << sumCalculators[i]->getName();
126                 }
127                 outputFileHandle << endl;
128                 
129                 while(order != NULL){
130                 
131                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){                       
132         
133                                 cout << order->getLabel() << '\t' << count << endl;
134                                 getSharedVectors();  //fills group vectors from order vector.
135                                 
136                                 //randomize group order
137                                 if (globaldata->getJumble() == "1") { random_shuffle(lookup.begin(), lookup.end()); }
138
139                                 int n = 1; 
140                                 for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
141                                         for (int l = n; l < lookup.size(); l++) {
142                                                 outputFileHandle << order->getLabel() << '\t';
143                                                 
144                                                 //sort groups to be alphanumeric
145                                                 if (lookup[k]->getGroup() > lookup[l]->getGroup()) {
146                                                         outputFileHandle << (lookup[l]->getGroup() +'\t' + lookup[k]->getGroup()) << '\t'; //print out groups
147                                                 }else{
148                                                         outputFileHandle << (lookup[k]->getGroup() +'\t' + lookup[l]->getGroup()) << '\t'; //print out groups
149                                                 }
150                                                 
151                                                 for(int i=0;i<sumCalculators.size();i++){
152                                                         sumCalculators[i]->getValues(lookup[k], lookup[l]); //saves the calculator outputs
153                                                         outputFileHandle << '\t';
154                                                         sumCalculators[i]->print(outputFileHandle);
155                                                 }
156                                                 outputFileHandle << endl;
157                                         }
158                                         n++;
159                                 }
160                         }
161                 
162                         //get next line to process
163                         if (format == "sharedfile") {
164                                 order = input->getSharedOrderVector();
165                         }else {
166                                 //you are using a list and a groupfile
167                                 SharedList = input->getSharedListVector(); //get new list vector to process
168                                 if (SharedList != NULL) {
169                                         order = SharedList->getSharedOrderVector(); //gets new order vector with group info.
170                                 }else {
171                                         break;
172                                 }
173                         }
174                         count++;
175                 }
176                 
177                 //reset groups parameter
178                 globaldata->Groups.clear();  globaldata->setGroups("");
179
180                 return 0;
181         }
182         catch(exception& e) {
183                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
184                 exit(1);
185         }
186         catch(...) {
187                 cout << "An unknown error has occurred in the SummarySharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
188                 exit(1);
189         }               
190 }
191
192 //**********************************************************************************************************************
193
194 void SummarySharedCommand::getSharedVectors(){
195 try {
196                 lookup.clear();
197                 //create and initialize vector of sharedvectors, one for each group
198                 for (int i = 0; i < globaldata->Groups.size(); i++) { 
199                         SharedRAbundVector* temp = new SharedRAbundVector(order->getNumBins());
200                         temp->setLabel(order->getLabel());
201                         temp->setGroup(globaldata->Groups[i]);
202                         lookup.push_back(temp);
203                 }
204                 
205                 int numSeqs = order->size();
206                 //sample all the members
207                 for(int i=0;i<numSeqs;i++){
208                         //get first sample
209                         individual chosen = order->get(i);
210                         int abundance; 
211                                         
212                         //set info for sharedvector in chosens group
213                         for (int j = 0; j < lookup.size(); j++) { 
214                                 if (chosen.group == lookup[j]->getGroup()) {
215                                          abundance = lookup[j]->getAbundance(chosen.bin);
216                                          lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
217                                          break;
218                                 }
219                         }
220                 }
221         }
222         catch(exception& e) {
223                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
224                 exit(1);
225         }
226         catch(...) {
227                 cout << "An unknown error has occurred in the SummarySharedCommand class function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
228                 exit(1);
229         }
230
231 }
232
233 //**********************************************************************************************************************
234 void SummarySharedCommand::setGroups() {
235         try {
236                 //if the user has not entered specific groups to analyze then do them all
237                 if (globaldata->Groups.size() != 0) {
238                         if (globaldata->Groups[0] != "all") {
239                                 //check that groups are valid
240                                 for (int i = 0; i < globaldata->Groups.size(); i++) {
241                                         if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
242                                                 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
243                                                 // erase the invalid group from globaldata->Groups
244                                                 globaldata->Groups.erase(globaldata->Groups.begin()+i);
245                                         }
246                                 }
247                         
248                                 //if the user only entered invalid groups
249                                 if ((globaldata->Groups.size() == 0) || (globaldata->Groups.size() == 1)) { 
250                                         cout << "When using the groups parameter you must have at least 2 valid groups. I will run the command using all the groups in your groupfile." << endl; 
251                                         for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
252                                                 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
253                                         }
254                                 }
255                         }else{//user has enter "all" and wants the default groups
256                                 globaldata->Groups.clear();
257                                 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
258                                         globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
259                                 }
260                                 globaldata->setGroups("");
261                         }
262                 }else {
263                         for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
264                                 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
265                         }
266                 }
267                 
268         }
269         catch(exception& e) {
270                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
271                 exit(1);
272         }
273         catch(...) {
274                 cout << "An unknown error has occurred in the SummarySharedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
275                 exit(1);
276         }               
277
278 }
279 /***********************************************************/