2 * summarysharedcommand.cpp
5 * Created by Sarah Westcott on 1/2/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
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"
24 //**********************************************************************************************************************
26 SummarySharedCommand::SummarySharedCommand(){
28 globaldata = GlobalData::getInstance();
29 outputFileName = ((getRootName(globaldata->inputFileName)) + "shared.summary");
30 openOutputFile(outputFileName, outputFileHandle);
31 format = globaldata->getFormat();
32 validCalculator = new ValidCalculators();
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());
63 //reset calc for next command
64 globaldata->setCalc("");
68 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
72 cout << "An unknown error has occurred in the SummarySharedCommand class function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
76 //**********************************************************************************************************************
78 SummarySharedCommand::~SummarySharedCommand(){
83 //**********************************************************************************************************************
85 int SummarySharedCommand::execute(){
89 //if the users entered no valid calculators don't execute command
90 if (sumCalculators.size() == 0) { return 0; }
92 if (format == "sharedfile") {
93 read = new ReadPhilFile(globaldata->inputFileName);
94 read->read(&*globaldata);
96 input = globaldata->ginput;
97 order = input->getSharedOrderVector();
99 //you are using a list and a groupfile
100 read = new ReadPhilFile(globaldata->inputFileName);
101 read->read(&*globaldata);
103 input = globaldata->ginput;
104 SharedList = globaldata->gSharedList;
105 order = SharedList->getSharedOrderVector();
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();
116 outputFileHandle << endl;
118 while(order != NULL){
120 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){
122 cout << order->getLabel() << '\t' << count << endl;
123 getSharedVectors(); //fills group vectors from order vector.
125 //randomize group order
126 if (globaldata->getJumble() == "1") { random_shuffle(lookup.begin(), lookup.end()); }
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);
137 outputFileHandle << endl;
143 //get next line to process
144 if (format == "sharedfile") {
145 order = input->getSharedOrderVector();
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.
158 //reset groups parameter
159 globaldata->Groups.clear(); globaldata->setGroups("");
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";
168 cout << "An unknown error has occurred in the SummarySharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
173 //**********************************************************************************************************************
175 void SummarySharedCommand::getSharedVectors(){
178 //create and initialize vector of sharedvectors, one for each group
179 for (int i = 0; i < globaldata->Groups.size(); i++) {
180 SharedRAbundVector* temp = new SharedRAbundVector(order->getNumBins());
181 temp->setLabel(order->getLabel());
182 temp->setGroup(globaldata->Groups[i]);
183 lookup.push_back(temp);
186 int numSeqs = order->size();
187 //sample all the members
188 for(int i=0;i<numSeqs;i++){
190 individual chosen = order->get(i);
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);
203 catch(exception& e) {
204 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
208 cout << "An unknown error has occurred in the SummarySharedCommand class function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
214 //**********************************************************************************************************************
215 void SummarySharedCommand::setGroups() {
217 //if the user has not entered specific groups to analyze then do them all
218 if (globaldata->Groups.size() != 0) {
219 if (globaldata->Groups[0] != "all") {
220 //check that groups are valid
221 for (int i = 0; i < globaldata->Groups.size(); i++) {
222 if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
223 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
224 // erase the invalid group from globaldata->Groups
225 globaldata->Groups.erase(globaldata->Groups.begin()+i);
229 //if the user only entered invalid groups
230 if ((globaldata->Groups.size() == 0) || (globaldata->Groups.size() == 1)) {
231 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;
232 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
233 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
236 }else{//user has enter "all" and wants the default groups
237 globaldata->Groups.clear();
238 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
239 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
241 globaldata->setGroups("");
244 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
245 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
250 catch(exception& e) {
251 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
255 cout << "An unknown error has occurred in the SummarySharedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
260 /***********************************************************/