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 "sharedkstest.h"
24 #include "sharedbdiversity.h"
25 #include "sharedochiai.h"
26 #include "sharedanderbergs.h"
27 #include "sharedkulczynski.h"
28 #include "sharedkulczynskicody.h"
29 #include "sharedlennon.h"
30 #include "sharedmorisitahorn.h"
31 #include "sharedbraycurtis.h"
34 //**********************************************************************************************************************
36 SummarySharedCommand::SummarySharedCommand(){
38 globaldata = GlobalData::getInstance();
39 outputFileName = ((getRootName(globaldata->inputFileName)) + "shared.summary");
40 openOutputFile(outputFileName, outputFileHandle);
41 format = globaldata->getFormat();
42 validCalculator = new ValidCalculators();
45 for (i=0; i<globaldata->Estimators.size(); i++) {
46 if (validCalculator->isValidCalculator("sharedsummary", globaldata->Estimators[i]) == true) {
47 if (globaldata->Estimators[i] == "sharedsobs") {
48 sumCalculators.push_back(new SharedSobsCS());
49 }else if (globaldata->Estimators[i] == "sharedchao") {
50 sumCalculators.push_back(new SharedChao1());
51 }else if (globaldata->Estimators[i] == "sharedace") {
52 sumCalculators.push_back(new SharedAce());
53 }else if (globaldata->Estimators[i] == "sharedjabund") {
54 sumCalculators.push_back(new SharedJAbund());
55 }else if (globaldata->Estimators[i] == "sharedsorensonabund") {
56 sumCalculators.push_back(new SharedSorAbund());
57 }else if (globaldata->Estimators[i] == "sharedjclass") {
58 sumCalculators.push_back(new SharedJclass());
59 }else if (globaldata->Estimators[i] == "sharedsorclass") {
60 sumCalculators.push_back(new SharedSorClass());
61 }else if (globaldata->Estimators[i] == "sharedjest") {
62 sumCalculators.push_back(new SharedJest());
63 }else if (globaldata->Estimators[i] == "sharedsorest") {
64 sumCalculators.push_back(new SharedSorEst());
65 }else if (globaldata->Estimators[i] == "sharedthetayc") {
66 sumCalculators.push_back(new SharedThetaYC());
67 }else if (globaldata->Estimators[i] == "sharedthetan") {
68 sumCalculators.push_back(new SharedThetaN());
69 }else if (globaldata->Estimators[i] == "sharedkstest") {
70 sumCalculators.push_back(new SharedKSTest());
71 }else if (globaldata->Estimators[i] == "sharednseqs") {
72 sumCalculators.push_back(new SharedNSeqs());
73 }else if (globaldata->Estimators[i] == "sharedochiai") {
74 sumCalculators.push_back(new SharedOchiai());
75 }else if (globaldata->Estimators[i] == "sharedanderberg") {
76 sumCalculators.push_back(new SharedAnderberg());
77 }else if (globaldata->Estimators[i] == "sharedkulczynski") {
78 sumCalculators.push_back(new SharedKulczynski());
79 }else if (globaldata->Estimators[i] == "sharedkulczynskicody") {
80 sumCalculators.push_back(new SharedKulczynskiCody());
81 }else if (globaldata->Estimators[i] == "sharedlennon") {
82 sumCalculators.push_back(new SharedLennon());
83 }else if (globaldata->Estimators[i] == "sharedmorisitahorn") {
84 sumCalculators.push_back(new SharedMorHorn());
85 }else if (globaldata->Estimators[i] == "sharedbraycurtis") {
86 sumCalculators.push_back(new SharedBrayCurtis());
88 else if (globaldata->Estimators[i] == "sharedbdiversity") {
89 sumCalculators.push_back(new SharedBDiversity());
94 //reset calc for next command
95 globaldata->setCalc("");
99 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
103 cout << "An unknown error has occurred in the SummarySharedCommand class function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
107 //**********************************************************************************************************************
109 SummarySharedCommand::~SummarySharedCommand(){
114 //**********************************************************************************************************************
116 int SummarySharedCommand::execute(){
120 //if the users entered no valid calculators don't execute command
121 if (sumCalculators.size() == 0) { return 0; }
123 if (format == "sharedfile") {
124 read = new ReadPhilFile(globaldata->inputFileName);
125 read->read(&*globaldata);
127 input = globaldata->ginput;
128 order = input->getSharedOrderVector();
130 //you are using a list and a groupfile
131 read = new ReadPhilFile(globaldata->inputFileName);
132 read->read(&*globaldata);
134 input = globaldata->ginput;
135 SharedList = globaldata->gSharedList;
136 order = SharedList->getSharedOrderVector();
142 //output estimator names as column headers
143 outputFileHandle << "label" <<'\t' << "comparison" << '\t';
144 for(int i=0;i<sumCalculators.size();i++){
145 outputFileHandle << '\t' << sumCalculators[i]->getName();
147 outputFileHandle << endl;
149 while(order != NULL){
151 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){
153 cout << order->getLabel() << '\t' << count << endl;
154 getSharedVectors(); //fills group vectors from order vector.
156 //randomize group order
157 if (globaldata->getJumble() == "1") { random_shuffle(lookup.begin(), lookup.end()); }
160 for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
161 for (int l = n; l < lookup.size(); l++) {
162 outputFileHandle << order->getLabel() << '\t' << (lookup[k]->getGroup() + lookup[l]->getGroup()) << '\t' << '\t'; //print out label and group
163 outputFileHandle << order->getLabel() << '\t';
165 //sort groups to be alphanumeric
166 if (lookup[k]->getGroup() > lookup[l]->getGroup()) {
167 outputFileHandle << (lookup[l]->getGroup() +'\t' + lookup[k]->getGroup()) << '\t'; //print out groups
169 outputFileHandle << (lookup[k]->getGroup() +'\t' + lookup[l]->getGroup()) << '\t'; //print out groups
172 for(int i=0;i<sumCalculators.size();i++){
173 sumCalculators[i]->getValues(lookup[k], lookup[l]); //saves the calculator outputs
174 outputFileHandle << '\t';
175 sumCalculators[i]->print(outputFileHandle);
177 outputFileHandle << endl;
183 //get next line to process
184 if (format == "sharedfile") {
185 order = input->getSharedOrderVector();
187 //you are using a list and a groupfile
188 SharedList = input->getSharedListVector(); //get new list vector to process
189 if (SharedList != NULL) {
190 order = SharedList->getSharedOrderVector(); //gets new order vector with group info.
198 //reset groups parameter
199 globaldata->Groups.clear(); globaldata->setGroups("");
203 catch(exception& e) {
204 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
208 cout << "An unknown error has occurred in the SummarySharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
213 //**********************************************************************************************************************
215 void SummarySharedCommand::getSharedVectors(){
218 //create and initialize vector of sharedvectors, one for each group
219 for (int i = 0; i < globaldata->Groups.size(); i++) {
220 SharedRAbundVector* temp = new SharedRAbundVector(order->getNumBins());
221 temp->setLabel(order->getLabel());
222 temp->setGroup(globaldata->Groups[i]);
223 lookup.push_back(temp);
226 int numSeqs = order->size();
227 //sample all the members
228 for(int i=0;i<numSeqs;i++){
230 individual chosen = order->get(i);
233 //set info for sharedvector in chosens group
234 for (int j = 0; j < lookup.size(); j++) {
235 if (chosen.group == lookup[j]->getGroup()) {
236 abundance = lookup[j]->getAbundance(chosen.bin);
237 lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
243 catch(exception& e) {
244 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
248 cout << "An unknown error has occurred in the SummarySharedCommand class function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
254 //**********************************************************************************************************************
255 void SummarySharedCommand::setGroups() {
257 //if the user has not entered specific groups to analyze then do them all
258 if (globaldata->Groups.size() != 0) {
259 if (globaldata->Groups[0] != "all") {
260 //check that groups are valid
261 for (int i = 0; i < globaldata->Groups.size(); i++) {
262 if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
263 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
264 // erase the invalid group from globaldata->Groups
265 globaldata->Groups.erase(globaldata->Groups.begin()+i);
269 //if the user only entered invalid groups
270 if ((globaldata->Groups.size() == 0) || (globaldata->Groups.size() == 1)) {
271 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;
272 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
273 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
276 }else{//user has enter "all" and wants the default groups
277 globaldata->Groups.clear();
278 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
279 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
281 globaldata->setGroups("");
284 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
285 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
290 catch(exception& e) {
291 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
295 cout << "An unknown error has occurred in the SummarySharedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
300 /***********************************************************/