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"
23 #include "sharedochiai.h"
24 #include "sharedanderberg.h"
27 //**********************************************************************************************************************
29 SummarySharedCommand::SummarySharedCommand(){
31 globaldata = GlobalData::getInstance();
32 outputFileName = ((getRootName(globaldata->inputFileName)) + "shared.summary");
33 openOutputFile(outputFileName, outputFileHandle);
34 format = globaldata->getFormat();
35 validCalculator = new ValidCalculators();
38 for (i=0; i<globaldata->Estimators.size(); i++) {
39 if (validCalculator->isValidCalculator("sharedsummary", globaldata->Estimators[i]) == true) {
40 if (globaldata->Estimators[i] == "sharedsobs") {
41 sumCalculators.push_back(new SharedSobsCS());
42 }else if (globaldata->Estimators[i] == "sharedchao") {
43 sumCalculators.push_back(new SharedChao1());
44 }else if (globaldata->Estimators[i] == "sharedace") {
45 sumCalculators.push_back(new SharedAce());
46 }else if (globaldata->Estimators[i] == "sharedjabund") {
47 sumCalculators.push_back(new SharedJAbund());
48 }else if (globaldata->Estimators[i] == "sharedsorensonabund") {
49 sumCalculators.push_back(new SharedSorAbund());
50 }else if (globaldata->Estimators[i] == "sharedjclass") {
51 sumCalculators.push_back(new SharedJclass());
52 }else if (globaldata->Estimators[i] == "sharedsorclass") {
53 sumCalculators.push_back(new SharedSorClass());
54 }else if (globaldata->Estimators[i] == "sharedjest") {
55 sumCalculators.push_back(new SharedJest());
56 }else if (globaldata->Estimators[i] == "sharedsorest") {
57 sumCalculators.push_back(new SharedSorEst());
58 }else if (globaldata->Estimators[i] == "sharedthetayc") {
59 sumCalculators.push_back(new SharedThetaYC());
60 }else if (globaldata->Estimators[i] == "sharedthetan") {
61 sumCalculators.push_back(new SharedThetaN());
62 }else if (globaldata->Estimators[i] == "sharednseqs") {
63 sumCalculators.push_back(new SharedNSeqs());
64 }else if (globaldata->Estimators[i] == "sharedochiai") {
65 sumCalculators.push_back(new SharedOchiai());
66 }else if (globaldata->Estimators[i] == "sharedanderberg") {
67 sumCalculators.push_back(new SharedAnderberg());
74 //reset calc for next command
75 globaldata->setCalc("");
79 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
83 cout << "An unknown error has occurred in the SummarySharedCommand class function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
87 //**********************************************************************************************************************
89 SummarySharedCommand::~SummarySharedCommand(){
94 //**********************************************************************************************************************
96 int SummarySharedCommand::execute(){
100 //if the users entered no valid calculators don't execute command
101 if (sumCalculators.size() == 0) { return 0; }
103 if (format == "sharedfile") {
104 read = new ReadPhilFile(globaldata->inputFileName);
105 read->read(&*globaldata);
107 input = globaldata->ginput;
108 order = input->getSharedOrderVector();
110 //you are using a list and a groupfile
111 read = new ReadPhilFile(globaldata->inputFileName);
112 read->read(&*globaldata);
114 input = globaldata->ginput;
115 SharedList = globaldata->gSharedList;
116 order = SharedList->getSharedOrderVector();
122 //output estimator names as column headers
123 outputFileHandle << "label" <<'\t' << "comparison" << '\t';
124 for(int i=0;i<sumCalculators.size();i++){
125 outputFileHandle << '\t' << sumCalculators[i]->getName();
127 outputFileHandle << endl;
129 while(order != NULL){
131 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){
133 cout << order->getLabel() << '\t' << count << endl;
134 getSharedVectors(); //fills group vectors from order vector.
136 //randomize group order
137 if (globaldata->getJumble() == "1") { random_shuffle(lookup.begin(), lookup.end()); }
140 for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
141 for (int l = n; l < lookup.size(); l++) {
142 outputFileHandle << order->getLabel() << '\t';
144 //sort groups to be alphanumeric
145 if (lookup[k]->getGroup() > lookup[l]->getGroup()) {
146 outputFileHandle << (lookup[l]->getGroup() +'\t' + lookup[k]->getGroup()) << '\t'; //print out groups
148 outputFileHandle << (lookup[k]->getGroup() +'\t' + lookup[l]->getGroup()) << '\t'; //print out groups
151 for(int i=0;i<sumCalculators.size();i++){
152 sumCalculators[i]->getValues(lookup[k], lookup[l]); //saves the calculator outputs
153 outputFileHandle << '\t';
154 sumCalculators[i]->print(outputFileHandle);
156 outputFileHandle << endl;
162 //get next line to process
163 if (format == "sharedfile") {
164 order = input->getSharedOrderVector();
166 //you are using a list and a groupfile
167 SharedList = input->getSharedListVector(); //get new list vector to process
168 if (SharedList != NULL) {
169 order = SharedList->getSharedOrderVector(); //gets new order vector with group info.
177 //reset groups parameter
178 globaldata->Groups.clear(); globaldata->setGroups("");
182 catch(exception& e) {
183 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
187 cout << "An unknown error has occurred in the SummarySharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
192 //**********************************************************************************************************************
194 void SummarySharedCommand::getSharedVectors(){
197 //create and initialize vector of sharedvectors, one for each group
198 for (int i = 0; i < globaldata->Groups.size(); i++) {
199 SharedRAbundVector* temp = new SharedRAbundVector(order->getNumBins());
200 temp->setLabel(order->getLabel());
201 temp->setGroup(globaldata->Groups[i]);
202 lookup.push_back(temp);
205 int numSeqs = order->size();
206 //sample all the members
207 for(int i=0;i<numSeqs;i++){
209 individual chosen = order->get(i);
212 //set info for sharedvector in chosens group
213 for (int j = 0; j < lookup.size(); j++) {
214 if (chosen.group == lookup[j]->getGroup()) {
215 abundance = lookup[j]->getAbundance(chosen.bin);
216 lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
222 catch(exception& e) {
223 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
227 cout << "An unknown error has occurred in the SummarySharedCommand class function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
233 //**********************************************************************************************************************
234 void SummarySharedCommand::setGroups() {
236 //if the user has not entered specific groups to analyze then do them all
237 if (globaldata->Groups.size() != 0) {
238 if (globaldata->Groups[0] != "all") {
239 //check that groups are valid
240 for (int i = 0; i < globaldata->Groups.size(); i++) {
241 if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
242 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
243 // erase the invalid group from globaldata->Groups
244 globaldata->Groups.erase(globaldata->Groups.begin()+i);
248 //if the user only entered invalid groups
249 if ((globaldata->Groups.size() == 0) || (globaldata->Groups.size() == 1)) {
250 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;
251 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
252 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
255 }else{//user has enter "all" and wants the default groups
256 globaldata->Groups.clear();
257 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
258 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
260 globaldata->setGroups("");
263 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
264 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
269 catch(exception& e) {
270 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
274 cout << "An unknown error has occurred in the SummarySharedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
279 /***********************************************************/