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