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