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