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 "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"
25 //**********************************************************************************************************************
27 SummarySharedCommand::SummarySharedCommand(){
29 globaldata = GlobalData::getInstance();
30 outputFileName = ((getRootName(globaldata->inputFileName)) + "shared.summary");
31 openOutputFile(outputFileName, outputFileHandle);
32 format = globaldata->getFormat();
33 validCalculator = new ValidCalculators();
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());
66 //reset calc for next command
67 globaldata->setCalc("");
71 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
75 cout << "An unknown error has occurred in the SummarySharedCommand class function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
79 //**********************************************************************************************************************
81 SummarySharedCommand::~SummarySharedCommand(){
86 //**********************************************************************************************************************
88 int SummarySharedCommand::execute(){
92 //if the users entered no valid calculators don't execute command
93 if (sumCalculators.size() == 0) { return 0; }
95 if (format == "sharedfile") {
96 read = new ReadPhilFile(globaldata->inputFileName);
97 read->read(&*globaldata);
99 input = globaldata->ginput;
100 order = input->getSharedOrderVector();
102 //you are using a list and a groupfile
103 read = new ReadPhilFile(globaldata->inputFileName);
104 read->read(&*globaldata);
106 input = globaldata->ginput;
107 SharedList = globaldata->gSharedList;
108 order = SharedList->getSharedOrderVector();
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();
119 outputFileHandle << endl;
121 while(order != NULL){
123 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){
125 cout << order->getLabel() << '\t' << count << endl;
126 getSharedVectors(); //fills group vectors from order vector.
128 //randomize group order
129 if (globaldata->getJumble() == "1") { random_shuffle(lookup.begin(), lookup.end()); }
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';
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
140 outputFileHandle << (lookup[k]->getGroup() +'\t' + lookup[l]->getGroup()) << '\t'; //print out groups
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);
148 outputFileHandle << endl;
154 //get next line to process
155 if (format == "sharedfile") {
156 order = input->getSharedOrderVector();
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.
169 //reset groups parameter
170 globaldata->Groups.clear(); globaldata->setGroups("");
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";
179 cout << "An unknown error has occurred in the SummarySharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
184 //**********************************************************************************************************************
186 void SummarySharedCommand::getSharedVectors(){
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);
197 int numSeqs = order->size();
198 //sample all the members
199 for(int i=0;i<numSeqs;i++){
201 individual chosen = order->get(i);
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);
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";
219 cout << "An unknown error has occurred in the SummarySharedCommand class function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
225 //**********************************************************************************************************************
226 void SummarySharedCommand::setGroups() {
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);
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]);
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]);
252 globaldata->setGroups("");
255 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
256 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
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";
266 cout << "An unknown error has occurred in the SummarySharedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
271 /***********************************************************/