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"
25 #include "sharedkulczynski.h"
26 #include "sharedkulczynskicody.h"
27 #include "sharedlennon.h"
28 #include "sharedmorisitahorn.h"
29 #include "sharedbraycurtis.h"
32 //**********************************************************************************************************************
34 SummarySharedCommand::SummarySharedCommand(){
36 globaldata = GlobalData::getInstance();
37 outputFileName = ((getRootName(globaldata->inputFileName)) + "shared.summary");
38 openOutputFile(outputFileName, outputFileHandle);
39 format = globaldata->getFormat();
40 validCalculator = new ValidCalculators();
43 for (i=0; i<globaldata->Estimators.size(); i++) {
44 if (validCalculator->isValidCalculator("sharedsummary", globaldata->Estimators[i]) == true) {
45 if (globaldata->Estimators[i] == "sharedsobs") {
46 sumCalculators.push_back(new SharedSobsCS());
47 }else if (globaldata->Estimators[i] == "sharedchao") {
48 sumCalculators.push_back(new SharedChao1());
49 }else if (globaldata->Estimators[i] == "sharedace") {
50 sumCalculators.push_back(new SharedAce());
51 }else if (globaldata->Estimators[i] == "sharedjabund") {
52 sumCalculators.push_back(new SharedJAbund());
53 }else if (globaldata->Estimators[i] == "sharedsorensonabund") {
54 sumCalculators.push_back(new SharedSorAbund());
55 }else if (globaldata->Estimators[i] == "sharedjclass") {
56 sumCalculators.push_back(new SharedJclass());
57 }else if (globaldata->Estimators[i] == "sharedsorclass") {
58 sumCalculators.push_back(new SharedSorClass());
59 }else if (globaldata->Estimators[i] == "sharedjest") {
60 sumCalculators.push_back(new SharedJest());
61 }else if (globaldata->Estimators[i] == "sharedsorest") {
62 sumCalculators.push_back(new SharedSorEst());
63 }else if (globaldata->Estimators[i] == "sharedthetayc") {
64 sumCalculators.push_back(new SharedThetaYC());
65 }else if (globaldata->Estimators[i] == "sharedthetan") {
66 sumCalculators.push_back(new SharedThetaN());
67 }else if (globaldata->Estimators[i] == "sharednseqs") {
68 sumCalculators.push_back(new SharedNSeqs());
69 }else if (globaldata->Estimators[i] == "sharedochiai") {
70 sumCalculators.push_back(new SharedOchiai());
71 }else if (globaldata->Estimators[i] == "sharedanderberg") {
72 sumCalculators.push_back(new SharedAnderberg());
73 }else if (globaldata->Estimators[i] == "sharedkulczynski") {
74 sumCalculators.push_back(new SharedKulczynski());
75 }else if (globaldata->Estimators[i] == "sharedkulczynskicody") {
76 sumCalculators.push_back(new SharedKulczynskiCody());
77 }else if (globaldata->Estimators[i] == "sharedlennon") {
78 sumCalculators.push_back(new SharedLennon());
79 }else if (globaldata->Estimators[i] == "sharedmorisitahorn") {
80 sumCalculators.push_back(new SharedMorHorn());
81 }else if (globaldata->Estimators[i] == "sharedbraycurtis") {
82 sumCalculators.push_back(new SharedBrayCurtis());
87 //reset calc for next command
88 globaldata->setCalc("");
92 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
96 cout << "An unknown error has occurred in the SummarySharedCommand class function SummarySharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
100 //**********************************************************************************************************************
102 SummarySharedCommand::~SummarySharedCommand(){
107 //**********************************************************************************************************************
109 int SummarySharedCommand::execute(){
113 //if the users entered no valid calculators don't execute command
114 if (sumCalculators.size() == 0) { return 0; }
116 if (format == "sharedfile") {
117 read = new ReadPhilFile(globaldata->inputFileName);
118 read->read(&*globaldata);
120 input = globaldata->ginput;
121 order = input->getSharedOrderVector();
123 //you are using a list and a groupfile
124 read = new ReadPhilFile(globaldata->inputFileName);
125 read->read(&*globaldata);
127 input = globaldata->ginput;
128 SharedList = globaldata->gSharedList;
129 order = SharedList->getSharedOrderVector();
135 //output estimator names as column headers
136 outputFileHandle << "label" <<'\t' << "comparison" << '\t';
137 for(int i=0;i<sumCalculators.size();i++){
138 outputFileHandle << '\t' << sumCalculators[i]->getName();
140 outputFileHandle << endl;
142 while(order != NULL){
144 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){
146 cout << order->getLabel() << '\t' << count << endl;
147 getSharedVectors(); //fills group vectors from order vector.
149 //randomize group order
150 if (globaldata->getJumble() == "1") { random_shuffle(lookup.begin(), lookup.end()); }
153 for (int k = 0; k < (lookup.size() - 1); k++) { // pass cdd each set of groups to commpare
154 for (int l = n; l < lookup.size(); l++) {
155 outputFileHandle << order->getLabel() << '\t';
157 //sort groups to be alphanumeric
158 if (lookup[k]->getGroup() > lookup[l]->getGroup()) {
159 outputFileHandle << (lookup[l]->getGroup() +'\t' + lookup[k]->getGroup()) << '\t'; //print out groups
161 outputFileHandle << (lookup[k]->getGroup() +'\t' + lookup[l]->getGroup()) << '\t'; //print out groups
164 for(int i=0;i<sumCalculators.size();i++){
165 sumCalculators[i]->getValues(lookup[k], lookup[l]); //saves the calculator outputs
166 outputFileHandle << '\t';
167 sumCalculators[i]->print(outputFileHandle);
169 outputFileHandle << endl;
175 //get next line to process
176 if (format == "sharedfile") {
177 order = input->getSharedOrderVector();
179 //you are using a list and a groupfile
180 SharedList = input->getSharedListVector(); //get new list vector to process
181 if (SharedList != NULL) {
182 order = SharedList->getSharedOrderVector(); //gets new order vector with group info.
190 //reset groups parameter
191 globaldata->Groups.clear(); globaldata->setGroups("");
195 catch(exception& e) {
196 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
200 cout << "An unknown error has occurred in the SummarySharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
205 //**********************************************************************************************************************
207 void SummarySharedCommand::getSharedVectors(){
210 //create and initialize vector of sharedvectors, one for each group
211 for (int i = 0; i < globaldata->Groups.size(); i++) {
212 SharedRAbundVector* temp = new SharedRAbundVector(order->getNumBins());
213 temp->setLabel(order->getLabel());
214 temp->setGroup(globaldata->Groups[i]);
215 lookup.push_back(temp);
218 int numSeqs = order->size();
219 //sample all the members
220 for(int i=0;i<numSeqs;i++){
222 individual chosen = order->get(i);
225 //set info for sharedvector in chosens group
226 for (int j = 0; j < lookup.size(); j++) {
227 if (chosen.group == lookup[j]->getGroup()) {
228 abundance = lookup[j]->getAbundance(chosen.bin);
229 lookup[j]->set(chosen.bin, (abundance + 1), chosen.group);
235 catch(exception& e) {
236 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
240 cout << "An unknown error has occurred in the SummarySharedCommand class function getSharedVectors. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
246 //**********************************************************************************************************************
247 void SummarySharedCommand::setGroups() {
249 //if the user has not entered specific groups to analyze then do them all
250 if (globaldata->Groups.size() != 0) {
251 if (globaldata->Groups[0] != "all") {
252 //check that groups are valid
253 for (int i = 0; i < globaldata->Groups.size(); i++) {
254 if (globaldata->gGroupmap->isValidGroup(globaldata->Groups[i]) != true) {
255 cout << globaldata->Groups[i] << " is not a valid group, and will be disregarded." << endl;
256 // erase the invalid group from globaldata->Groups
257 globaldata->Groups.erase(globaldata->Groups.begin()+i);
261 //if the user only entered invalid groups
262 if ((globaldata->Groups.size() == 0) || (globaldata->Groups.size() == 1)) {
263 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;
264 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
265 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
268 }else{//user has enter "all" and wants the default groups
269 globaldata->Groups.clear();
270 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
271 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
273 globaldata->setGroups("");
276 for (int i = 0; i < globaldata->gGroupmap->namesOfGroups.size(); i++) {
277 globaldata->Groups.push_back(globaldata->gGroupmap->namesOfGroups[i]);
282 catch(exception& e) {
283 cout << "Standard Error: " << e.what() << " has occurred in the SummarySharedCommand class Function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
287 cout << "An unknown error has occurred in the SummarySharedCommand class function setGroups. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
292 /***********************************************************/