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"
22 //**********************************************************************************************************************
24 BootSharedCommand::BootSharedCommand(){
26 globaldata = GlobalData::getInstance();
27 format = globaldata->getFormat();
28 convert(globaldata->getIters(), iters);
29 validCalculator = new ValidCalculators();
30 util = new SharedUtil();
34 for (i=0; i<globaldata->Estimators.size(); i++) {
35 if (validCalculator->isValidCalculator("boot", globaldata->Estimators[i]) == true) {
36 if (globaldata->Estimators[i] == "jabund") {
37 treeCalculators.push_back(new JAbund());
38 }else if (globaldata->Estimators[i] == "sorensonabund") {
39 treeCalculators.push_back(new SorAbund());
40 }else if (globaldata->Estimators[i] == "jclass") {
41 treeCalculators.push_back(new Jclass());
42 }else if (globaldata->Estimators[i] == "sorclass") {
43 treeCalculators.push_back(new SorClass());
44 }else if (globaldata->Estimators[i] == "jest") {
45 treeCalculators.push_back(new Jest());
46 }else if (globaldata->Estimators[i] == "sorest") {
47 treeCalculators.push_back(new SorEst());
48 }else if (globaldata->Estimators[i] == "thetayc") {
49 treeCalculators.push_back(new ThetaYC());
50 }else if (globaldata->Estimators[i] == "thetan") {
51 treeCalculators.push_back(new ThetaN());
52 }else if (globaldata->Estimators[i] == "morisitahorn") {
53 treeCalculators.push_back(new MorHorn());
59 for (int i=0; i < treeCalculators.size(); i++) {
64 //reset calc for next command
65 globaldata->setCalc("");
69 cout << "Standard Error: " << e.what() << " has occurred in the BootSharedCommand class Function BootSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
73 cout << "An unknown error has occurred in the BootSharedCommand class function BootSharedCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
77 //**********************************************************************************************************************
79 BootSharedCommand::~BootSharedCommand(){
85 //**********************************************************************************************************************
87 int BootSharedCommand::execute(){
92 //if the users entered no valid calculators don't execute command
93 if (treeCalculators.size() == 0) { return 0; }
95 if (format == "sharedfile") {
96 read = new ReadOTUFile(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 ReadOTUFile(globaldata->inputFileName);
104 read->read(&*globaldata);
106 input = globaldata->ginput;
107 SharedList = globaldata->gSharedList;
108 order = SharedList->getSharedOrderVector();
112 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups, "treegroup");
113 numGroups = globaldata->Groups.size();
115 //clear globaldatas old tree names if any
116 globaldata->Treenames.clear();
118 //fills globaldatas tree names
119 globaldata->Treenames = globaldata->Groups;
121 //create treemap class from groupmap for tree class to use
122 tmap = new TreeMap();
123 tmap->makeSim(globaldata->gGroupmap);
124 globaldata->gTreemap = tmap;
126 while(order != NULL){
128 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){
130 cout << order->getLabel() << '\t' << count << endl;
132 //open an ostream for each calc to print to
133 for (int z = 0; z < treeCalculators.size(); z++) {
134 //create a new filename
135 outputFile = getRootName(globaldata->inputFileName) + treeCalculators[z]->getName() + ".boot" + order->getLabel() + ".tre";
136 openOutputFile(outputFile, *(out[z]));
139 //create a file for each calculator with the 1000 trees in it.
140 for (int p = 0; p < iters; p++) {
142 util->getSharedVectorswithReplacement(globaldata->Groups, lookup, order); //fills group vectors from order vector.
144 //for each calculator
145 for(int i = 0 ; i < treeCalculators.size(); i++) {
147 //initialize simMatrix
149 simMatrix.resize(numGroups);
150 for (int m = 0; m < simMatrix.size(); m++) {
151 for (int j = 0; j < simMatrix.size(); j++) {
152 simMatrix[m].push_back(0.0);
158 for (int g = 0; g < numGroups; g++) { index[g] = g; }
160 for (int k = 0; k < lookup.size(); k++) { // pass cdd each set of groups to commpare
161 for (int l = k; l < lookup.size(); l++) {
162 if (k != l) { //we dont need to similiarity of a groups to itself
163 //get estimated similarity between 2 groups
164 data = treeCalculators[i]->getValues(lookup[k], lookup[l]); //saves the calculator outputs
165 //save values in similarity matrix
166 simMatrix[k][l] = data[0];
167 simMatrix[l][k] = data[0];
172 //creates tree from similarity matrix and write out file
176 //close ostream for each calc
177 for (int z = 0; z < treeCalculators.size(); z++) { out[z]->close(); }
180 //get next line to process
181 if (format == "sharedfile") {
182 order = input->getSharedOrderVector();
184 //you are using a list and a groupfile
185 SharedList = input->getSharedListVector(); //get new list vector to process
186 if (SharedList != NULL) {
187 order = SharedList->getSharedOrderVector(); //gets new order vector with group info.
195 //reset groups parameter
196 globaldata->Groups.clear(); globaldata->setGroups("");
200 catch(exception& e) {
201 cout << "Standard Error: " << e.what() << " has occurred in the BootSharedCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
205 cout << "An unknown error has occurred in the BootSharedCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
209 //**********************************************************************************************************************
211 void BootSharedCommand::createTree(ostream* out){
216 //do merges and create tree structure by setting parents and children
217 //there are numGroups - 1 merges to do
218 for (int i = 0; i < (numGroups - 1); i++) {
222 //find largest value in sims matrix by searching lower triangle
223 for (int j = 1; j < simMatrix.size(); j++) {
224 for (int k = 0; k < j; k++) {
225 if (simMatrix[j][k] > largest) { largest = simMatrix[j][k]; row = j; column = k; }
229 //set non-leaf node info and update leaves to know their parents
231 t->tree[numGroups + i].setChildren(index[row], index[column]);
234 t->tree[index[row]].setParent(numGroups + i);
235 t->tree[index[column]].setParent(numGroups + i);
237 //blength = distance / 2;
238 float blength = ((1.0 - largest) / 2);
241 t->tree[index[row]].setBranchLength(blength - t->tree[index[row]].getLengthToLeaves());
242 t->tree[index[column]].setBranchLength(blength - t->tree[index[column]].getLengthToLeaves());
244 //set your length to leaves to your childs length plus branchlength
245 t->tree[numGroups + i].setLengthToLeaves(t->tree[index[row]].getLengthToLeaves() + t->tree[index[row]].getBranchLength());
249 index[row] = numGroups+i;
250 index[column] = numGroups+i;
252 //zero out highest value that caused the merge.
253 simMatrix[row][column] = 0.0;
254 simMatrix[column][row] = 0.0;
256 //merge values in simsMatrix
257 for (int n = 0; n < simMatrix.size(); n++) {
258 //row becomes merge of 2 groups
259 simMatrix[row][n] = (simMatrix[row][n] + simMatrix[column][n]) / 2;
260 simMatrix[n][row] = simMatrix[row][n];
262 simMatrix[column][n] = 0.0;
263 simMatrix[n][column] = 0.0;
277 catch(exception& e) {
278 cout << "Standard Error: " << e.what() << " has occurred in the BootSharedCommand class Function createTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
282 cout << "An unknown error has occurred in the BootSharedCommand class function createTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
286 /***********************************************************/
287 void BootSharedCommand::printSims() {
289 cout << "simsMatrix" << endl;
290 for (int m = 0; m < simMatrix.size(); m++) {
291 for (int n = 0; n < simMatrix.size(); n++) {
292 cout << simMatrix[m][n] << '\t';
298 catch(exception& e) {
299 cout << "Standard Error: " << e.what() << " has occurred in the BootSharedCommand class Function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
303 cout << "An unknown error has occurred in the BootSharedCommand class function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
307 /***********************************************************/