]> git.donarmstrong.com Git - mothur.git/blob - summarysharedcommand.cpp
fixed summary.shared bug and set jumble default to 1.
[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 "sharedsobs.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 SummarySharedCommand::SummarySharedCommand(){
26         try {
27                 globaldata = GlobalData::getInstance();
28                 
29                 int i;
30                 for (i=0; i<globaldata->sharedSummaryEstimators.size(); i++) {
31                         if (globaldata->sharedSummaryEstimators[i] == "sharedSobs") { 
32                                 sumCalculators.push_back(new SharedSobs());
33                         }else if (globaldata->sharedSummaryEstimators[i] == "sharedChao") { 
34                                 sumCalculators.push_back(new SharedChao1());
35                         }else if (globaldata->sharedSummaryEstimators[i] == "sharedAce") { 
36                                 sumCalculators.push_back(new SharedAce());
37                         }else if (globaldata->sharedSummaryEstimators[i] == "sharedJabund") {   
38                                 sumCalculators.push_back(new SharedJAbund());
39                         }else if (globaldata->sharedSummaryEstimators[i] == "sharedSorensonAbund") { 
40                                 sumCalculators.push_back(new SharedSorAbund());
41                         }else if (globaldata->sharedSummaryEstimators[i] == "sharedJclass") { 
42                                 sumCalculators.push_back(new SharedJclass());
43                         }else if (globaldata->sharedSummaryEstimators[i] == "sharedSorClass") { 
44                                 sumCalculators.push_back(new SharedSorClass());
45                         }else if (globaldata->sharedSummaryEstimators[i] == "sharedJest") { 
46                                 sumCalculators.push_back(new SharedJest());
47                         }else if (globaldata->sharedSummaryEstimators[i] == "sharedSorEst") { 
48                                 sumCalculators.push_back(new SharedSorEst());
49                         }else if (globaldata->sharedSummaryEstimators[i] == "SharedThetaYC") { 
50                                 sumCalculators.push_back(new SharedThetaYC());
51                         }else if (globaldata->sharedSummaryEstimators[i] == "SharedThetaN") { 
52                                 sumCalculators.push_back(new SharedThetaN());
53                         }
54                 }
55         }
56         catch(exception& e) {
57                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
58                 exit(1);
59         }
60         catch(...) {
61                 cout << "An unknown error has occurred in the SummarySharedCommand class function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
62                 exit(1);
63         }       
64 }
65 //**********************************************************************************************************************
66
67 SummarySharedCommand::~SummarySharedCommand(){
68         delete input;
69         delete read;
70 }
71
72 //**********************************************************************************************************************
73
74 int SummarySharedCommand::execute(){
75         try {
76                 outputFileName = ((getRootName(globaldata->inputFileName)) + "shared.summary");
77                 openOutputFile(outputFileName, outputFileHandle);
78         
79                 read = new ReadPhilFile(globaldata->inputFileName);     
80                 read->read(&*globaldata); 
81                 
82                 outputFileHandle << "label" <<'\t' << "comparison" << '\t'; 
83                 for(int i=0;i<sumCalculators.size();i++){
84                         outputFileHandle << '\t' << sumCalculators[i]->getName();
85                 }
86                 outputFileHandle << endl;
87                 
88                 SharedList = globaldata->gSharedList;
89                 input = globaldata->ginput;
90                 order = SharedList->getSharedOrderVector();
91                 getGroupComb();
92                 
93                 int count = 1;
94                 while(order != NULL){
95                 
96                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){                       
97         
98                                 cout << order->getLabel() << '\t' << count << endl;
99                                 getSharedVectors();  //fills group vectors from order vector.
100                                 
101                                 //randomize group order
102                                 if (globaldata->getJumble() == "1") { random_shuffle(lookup.begin(), lookup.end()); }
103
104                                 int n = 1; 
105                                 for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
106                                         for (int l = n; l < lookup.size(); l++) {
107                                                 outputFileHandle << order->getLabel() << '\t' << groupComb[n-1] << '\t' << '\t'; //print out label and group
108                                                 for(int i=0;i<sumCalculators.size();i++){
109                                                         sumCalculators[i]->getValues(lookup[k], lookup[l]); //saves the calculator outputs
110                                                         outputFileHandle << '\t';
111                                                         sumCalculators[i]->print(outputFileHandle);
112                                                 }
113                                                 outputFileHandle << endl;
114                                         }
115                                         n++;
116                                 }
117                         }
118                 
119                         SharedList = input->getSharedListVector(); //get new list vector to process
120                         if (SharedList != NULL) {
121                                 order = SharedList->getSharedOrderVector(); //gets new order vector with group info.
122                                 count++;
123                         }else {
124                                 break;
125                         }
126                 }
127         
128                 return 0;
129         }
130         catch(exception& e) {
131                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
132                 exit(1);
133         }
134         catch(...) {
135                 cout << "An unknown error has occurred in the SummarySharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
136                 exit(1);
137         }               
138 }
139
140 //**********************************************************************************************************************
141
142 void SummarySharedCommand::getSharedVectors(){
143 try {
144                 lookup.clear();
145                 //create and initialize vector of sharedvectors, one for each group
146                 for (int i = 0; i < globaldata->gGroupmap->getNumGroups(); i++) { 
147                         SharedRAbundVector* temp = new SharedRAbundVector(order->getNumBins());
148                         temp->setLabel(order->getLabel());
149                         temp->setGroup(globaldata->gGroupmap->namesOfGroups[i]);
150                         lookup.push_back(temp);
151                 }
152                 
153                 int numSeqs = order->size();
154                 //sample all the members
155                 for(int i=0;i<numSeqs;i++){
156                         //get first sample
157                         individual chosen = order->get(i);
158                         int abundance; 
159                                         
160                         //set info for sharedvector in chosens group
161                         for (int j = 0; j < lookup.size(); j++) { 
162                                 if (chosen.group == lookup[j]->getGroup()) {
163                                          abundance = lookup[j]->getAbundance(chosen.bin);
164                                          lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
165                                          break;
166                                 }
167                         }
168                         
169                 }
170         }
171         catch(exception& e) {
172                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
173                 exit(1);
174         }
175         catch(...) {
176                 cout << "An unknown error has occurred in the SummarySharedCommand class function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
177                 exit(1);
178         }
179
180 }
181
182 /**************************************************************************************/
183 void SummarySharedCommand::getGroupComb() {
184         try {
185                 string group;
186                 
187                 int n = 1;
188                 for (int i = 0; i < (globaldata->gGroupmap->getNumGroups() - 1); i++) {
189                         for (int l = n; l < globaldata->gGroupmap->getNumGroups(); l++) {
190                                 group = globaldata->gGroupmap->namesOfGroups[i] + globaldata->gGroupmap->namesOfGroups[l];
191                                 groupComb.push_back(group);     
192                         }
193                         n++;
194                 }
195         }
196         catch(exception& e) {
197                 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function getGroupComb. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
198                 exit(1);
199         }
200         catch(...) {
201                 cout << "An unknown error has occurred in the SummarySharedCommand class function getGroupComb. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
202                 exit(1);
203         }
204
205 }
206