]> git.donarmstrong.com Git - mothur.git/blob - summarysharedcommand.cpp
deleted read.shared command and added its functionality to read.otu.
[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                 int i;
35                 for (i=0; i<globaldata->Estimators.size(); i++) {
36                         if (validCalculator->isValidCalculator("sharedsummary", globaldata->Estimators[i]) == true) { 
37                                 if (globaldata->Estimators[i] == "sharedsobs") { 
38                                         sumCalculators.push_back(new SharedSobsCS());
39                                 }else if (globaldata->Estimators[i] == "sharedchao") { 
40                                         sumCalculators.push_back(new SharedChao1());
41                                 }else if (globaldata->Estimators[i] == "sharedace") { 
42                                         sumCalculators.push_back(new SharedAce());
43                                 }else if (globaldata->Estimators[i] == "sharedjabund") {        
44                                         sumCalculators.push_back(new SharedJAbund());
45                                 }else if (globaldata->Estimators[i] == "sharedsorensonabund") { 
46                                         sumCalculators.push_back(new SharedSorAbund());
47                                 }else if (globaldata->Estimators[i] == "sharedjclass") { 
48                                         sumCalculators.push_back(new SharedJclass());
49                                 }else if (globaldata->Estimators[i] == "sharedsorclass") { 
50                                         sumCalculators.push_back(new SharedSorClass());
51                                 }else if (globaldata->Estimators[i] == "sharedjest") { 
52                                         sumCalculators.push_back(new SharedJest());
53                                 }else if (globaldata->Estimators[i] == "sharedsorest") { 
54                                         sumCalculators.push_back(new SharedSorEst());
55                                 }else if (globaldata->Estimators[i] == "sharedthetayc") { 
56                                         sumCalculators.push_back(new SharedThetaYC());
57                                 }else if (globaldata->Estimators[i] == "sharedthetan") { 
58                                         sumCalculators.push_back(new SharedThetaN());
59                                 }
60                         }
61                 }
62                 
63                 //reset calc for next command
64                 globaldata->setCalc("");
65
66         }
67         catch(exception& e) {
68                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
69                 exit(1);
70         }
71         catch(...) {
72                 cout << "An unknown error has occurred in the SummarySharedCommand class function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
73                 exit(1);
74         }       
75 }
76 //**********************************************************************************************************************
77
78 SummarySharedCommand::~SummarySharedCommand(){
79         delete input;
80         delete read;
81 }
82
83 //**********************************************************************************************************************
84
85 int SummarySharedCommand::execute(){
86         try {
87                 int count = 1;  
88                 
89                 //if the users entered no valid calculators don't execute command
90                 if (sumCalculators.size() == 0) { return 0; }
91
92                 if (format == "sharedfile") {
93                         read = new ReadPhilFile(globaldata->inputFileName);     
94                         read->read(&*globaldata); 
95                         
96                         input = globaldata->ginput;
97                         order = input->getSharedOrderVector();
98                 }else {
99                         //you are using a list and a groupfile
100                         read = new ReadPhilFile(globaldata->inputFileName);     
101                         read->read(&*globaldata); 
102                 
103                         input = globaldata->ginput;
104                         SharedList = globaldata->gSharedList;
105                         order = SharedList->getSharedOrderVector();
106                 }
107                 
108                 //output estimator names as column headers
109                 outputFileHandle << "label" <<'\t' << "comparison" << '\t'; 
110                 for(int i=0;i<sumCalculators.size();i++){
111                         outputFileHandle << '\t' << sumCalculators[i]->getName();
112                 }
113                 outputFileHandle << endl;
114                 
115                 //set users groups
116                 setGroups();
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 /***********************************************************/