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