2 * bootstrapsharedcommand.cpp
5 * Created by Sarah Westcott on 4/16/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "bootstrapsharedcommand.h"
11 #include "sharedjabund.h"
12 #include "sharedsorabund.h"
13 #include "sharedjclass.h"
14 #include "sharedsorclass.h"
15 #include "sharedjest.h"
16 #include "sharedsorest.h"
17 #include "sharedthetayc.h"
18 #include "sharedthetan.h"
19 #include "sharedmorisitahorn.h"
20 #include "sharedbraycurtis.h"
23 //**********************************************************************************************************************
25 BootSharedCommand::BootSharedCommand(){
27 globaldata = GlobalData::getInstance();
28 format = globaldata->getFormat();
29 convert(globaldata->getIters(), iters);
30 validCalculator = new ValidCalculators();
31 util = new SharedUtil();
35 for (i=0; i<globaldata->Estimators.size(); i++) {
36 if (validCalculator->isValidCalculator("boot", globaldata->Estimators[i]) == true) {
37 if (globaldata->Estimators[i] == "jabund") {
38 treeCalculators.push_back(new JAbund());
39 }else if (globaldata->Estimators[i] == "sorabund") {
40 treeCalculators.push_back(new SorAbund());
41 }else if (globaldata->Estimators[i] == "jclass") {
42 treeCalculators.push_back(new Jclass());
43 }else if (globaldata->Estimators[i] == "sorclass") {
44 treeCalculators.push_back(new SorClass());
45 }else if (globaldata->Estimators[i] == "jest") {
46 treeCalculators.push_back(new Jest());
47 }else if (globaldata->Estimators[i] == "sorest") {
48 treeCalculators.push_back(new SorEst());
49 }else if (globaldata->Estimators[i] == "thetayc") {
50 treeCalculators.push_back(new ThetaYC());
51 }else if (globaldata->Estimators[i] == "thetan") {
52 treeCalculators.push_back(new ThetaN());
53 }else if (globaldata->Estimators[i] == "morisitahorn") {
54 treeCalculators.push_back(new MorHorn());
55 }else if (globaldata->Estimators[i] == "braycurtis") {
56 treeCalculators.push_back(new BrayCurtis());
62 for (int i=0; i < treeCalculators.size(); i++) {
67 //reset calc for next command
68 globaldata->setCalc("");
72 cout << "Standard Error: " << e.what() << " has occurred in the BootSharedCommand class Function BootSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
76 cout << "An unknown error has occurred in the BootSharedCommand class function BootSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
80 //**********************************************************************************************************************
82 BootSharedCommand::~BootSharedCommand(){
88 //**********************************************************************************************************************
90 int BootSharedCommand::execute(){
94 //if the users entered no valid calculators don't execute command
95 if (treeCalculators.size() == 0) { return 0; }
98 read = new ReadOTUFile(globaldata->inputFileName);
99 read->read(&*globaldata);
100 input = globaldata->ginput;
101 order = input->getSharedOrderVector();
102 SharedOrderVector* lastOrder = order;
104 //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label.
105 set<string> processedLabels;
106 set<string> userLabels = globaldata->labels;
109 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups, "treegroup");
110 numGroups = globaldata->Groups.size();
112 //clear globaldatas old tree names if any
113 globaldata->Treenames.clear();
115 //fills globaldatas tree names
116 globaldata->Treenames = globaldata->Groups;
118 //create treemap class from groupmap for tree class to use
119 tmap = new TreeMap();
120 tmap->makeSim(globaldata->gGroupmap);
121 globaldata->gTreemap = tmap;
123 while((order != NULL) && ((globaldata->allLines == 1) || (userLabels.size() != 0))) {
125 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){
127 cout << order->getLabel() << '\t' << count << endl;
130 processedLabels.insert(order->getLabel());
131 userLabels.erase(order->getLabel());
133 //you have a label the user want that is smaller than this line and the last line has not already been processed
136 if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastOrder->getLabel()) != 1)) {
138 cout << lastOrder->getLabel() << '\t' << count << endl;
141 processedLabels.insert(lastOrder->getLabel());
142 userLabels.erase(lastOrder->getLabel());
145 if (count != 1) { delete lastOrder; }
148 //get next line to process
149 order = input->getSharedOrderVector();
153 //output error messages about any remaining user labels
154 set<string>::iterator it;
155 bool needToRun = false;
156 for (it = userLabels.begin(); it != userLabels.end(); it++) {
157 cout << "Your file does not include the label "<< *it;
158 if (processedLabels.count(lastOrder->getLabel()) != 1) {
159 cout << ". I will use " << lastOrder->getLabel() << "." << endl;
162 cout << ". Please refer to " << lastOrder->getLabel() << "." << endl;
166 //run last line if you need to
167 if (needToRun == true) {
169 cout << lastOrder->getLabel() << '\t' << count << endl;
174 //reset groups parameter
175 globaldata->Groups.clear(); globaldata->setGroups("");
179 catch(exception& e) {
180 cout << "Standard Error: " << e.what() << " has occurred in the BootSharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
184 cout << "An unknown error has occurred in the BootSharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
188 //**********************************************************************************************************************
190 void BootSharedCommand::createTree(ostream* out){
195 //do merges and create tree structure by setting parents and children
196 //there are numGroups - 1 merges to do
197 for (int i = 0; i < (numGroups - 1); i++) {
199 float largest = -1.0;
201 //find largest value in sims matrix by searching lower triangle
202 for (int j = 1; j < simMatrix.size(); j++) {
203 for (int k = 0; k < j; k++) {
204 if (simMatrix[j][k] > largest) { largest = simMatrix[j][k]; row = j; column = k; }
208 //set non-leaf node info and update leaves to know their parents
210 t->tree[numGroups + i].setChildren(index[row], index[column]);
213 t->tree[index[row]].setParent(numGroups + i);
214 t->tree[index[column]].setParent(numGroups + i);
216 //blength = distance / 2;
217 float blength = ((1.0 - largest) / 2);
220 t->tree[index[row]].setBranchLength(blength - t->tree[index[row]].getLengthToLeaves());
221 t->tree[index[column]].setBranchLength(blength - t->tree[index[column]].getLengthToLeaves());
223 //set your length to leaves to your childs length plus branchlength
224 t->tree[numGroups + i].setLengthToLeaves(t->tree[index[row]].getLengthToLeaves() + t->tree[index[row]].getBranchLength());
228 index[row] = numGroups+i;
229 index[column] = numGroups+i;
231 //zero out highest value that caused the merge.
232 simMatrix[row][column] = -1.0;
233 simMatrix[column][row] = -1.0;
235 //merge values in simsMatrix
236 for (int n = 0; n < simMatrix.size(); n++) {
237 //row becomes merge of 2 groups
238 simMatrix[row][n] = (simMatrix[row][n] + simMatrix[column][n]) / 2;
239 simMatrix[n][row] = simMatrix[row][n];
241 simMatrix[column][n] = -1.0;
242 simMatrix[n][column] = -1.0;
256 catch(exception& e) {
257 cout << "Standard Error: " << e.what() << " has occurred in the BootSharedCommand class Function createTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
261 cout << "An unknown error has occurred in the BootSharedCommand class function createTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
265 /***********************************************************/
266 void BootSharedCommand::printSims() {
268 cout << "simsMatrix" << endl;
269 for (int m = 0; m < simMatrix.size(); m++) {
270 for (int n = 0; n < simMatrix.size(); n++) {
271 cout << simMatrix[m][n] << '\t';
277 catch(exception& e) {
278 cout << "Standard Error: " << e.what() << " has occurred in the BootSharedCommand class Function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
282 cout << "An unknown error has occurred in the BootSharedCommand class function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
286 /***********************************************************/
287 void BootSharedCommand::process(SharedOrderVector* order) {
290 vector<SharedRAbundVector*> subset;
292 //open an ostream for each calc to print to
293 for (int z = 0; z < treeCalculators.size(); z++) {
294 //create a new filename
295 outputFile = getRootName(globaldata->inputFileName) + treeCalculators[z]->getName() + ".boot" + order->getLabel() + ".tre";
296 openOutputFile(outputFile, *(out[z]));
299 //create a file for each calculator with the 1000 trees in it.
300 for (int p = 0; p < iters; p++) {
302 util->getSharedVectorswithReplacement(globaldata->Groups, lookup, order); //fills group vectors from order vector.
304 //for each calculator
305 for(int i = 0 ; i < treeCalculators.size(); i++) {
307 //initialize simMatrix
309 simMatrix.resize(numGroups);
310 for (int m = 0; m < simMatrix.size(); m++) {
311 for (int j = 0; j < simMatrix.size(); j++) {
312 simMatrix[m].push_back(0.0);
318 for (int g = 0; g < numGroups; g++) { index[g] = g; }
320 for (int k = 0; k < lookup.size(); k++) { // pass cdd each set of groups to commpare
321 for (int l = k; l < lookup.size(); l++) {
322 if (k != l) { //we dont need to similiarity of a groups to itself
323 subset.clear(); //clear out old pair of sharedrabunds
324 //add new pair of sharedrabunds
325 subset.push_back(lookup[k]); subset.push_back(lookup[l]);
327 //get estimated similarity between 2 groups
328 data = treeCalculators[i]->getValues(subset); //saves the calculator outputs
329 //save values in similarity matrix
330 simMatrix[k][l] = data[0];
331 simMatrix[l][k] = data[0];
336 //creates tree from similarity matrix and write out file
340 //close ostream for each calc
341 for (int z = 0; z < treeCalculators.size(); z++) { out[z]->close(); }
344 catch(exception& e) {
345 cout << "Standard Error: " << e.what() << " has occurred in the BootSharedCommand class Function process. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
349 cout << "An unknown error has occurred in the BootSharedCommand class function process. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
353 /***********************************************************/