2 * treegroupscommand.cpp
5 * Created by Sarah Westcott on 4/8/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "treegroupscommand.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 TreeGroupCommand::TreeGroupCommand(){
27 globaldata = GlobalData::getInstance();
28 format = globaldata->getFormat();
29 validCalculator = new ValidCalculators();
32 for (i=0; i<globaldata->Estimators.size(); i++) {
33 if (validCalculator->isValidCalculator("treegroup", globaldata->Estimators[i]) == true) {
34 if (globaldata->Estimators[i] == "jabund") {
35 treeCalculators.push_back(new JAbund());
36 }else if (globaldata->Estimators[i] == "sorabund") {
37 treeCalculators.push_back(new SorAbund());
38 }else if (globaldata->Estimators[i] == "jclass") {
39 treeCalculators.push_back(new Jclass());
40 }else if (globaldata->Estimators[i] == "sorclass") {
41 treeCalculators.push_back(new SorClass());
42 }else if (globaldata->Estimators[i] == "jest") {
43 treeCalculators.push_back(new Jest());
44 }else if (globaldata->Estimators[i] == "sorest") {
45 treeCalculators.push_back(new SorEst());
46 }else if (globaldata->Estimators[i] == "thetayc") {
47 treeCalculators.push_back(new ThetaYC());
48 }else if (globaldata->Estimators[i] == "thetan") {
49 treeCalculators.push_back(new ThetaN());
50 }else if (globaldata->Estimators[i] == "morisitahorn") {
51 treeCalculators.push_back(new MorHorn());
52 }else if (globaldata->Estimators[i] == "braycurtis") {
53 treeCalculators.push_back(new BrayCurtis());
58 //reset calc for next command
59 globaldata->setCalc("");
63 cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function TreeGroupCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
67 cout << "An unknown error has occurred in the TreeGroupCommand class function TreeGroupCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
71 //**********************************************************************************************************************
73 TreeGroupCommand::~TreeGroupCommand(){
78 //**********************************************************************************************************************
80 int TreeGroupCommand::execute(){
84 vector<SharedRAbundVector*> subset;
86 //if the users entered no valid calculators don't execute command
87 if (treeCalculators.size() == 0) { return 0; }
90 read = new ReadOTUFile(globaldata->inputFileName);
91 read->read(&*globaldata);
93 input = globaldata->ginput;
94 lookup = input->getSharedRAbundVectors();
96 if (lookup.size() < 2) { cout << "You have not provided enough valid groups. I cannot run the command." << endl; return 0; }
98 numGroups = globaldata->Groups.size();
100 for (int i = 0; i < numGroups; i++) { groupNames += globaldata->Groups[i]; }
102 //clear globaldatas old tree names if any
103 globaldata->Treenames.clear();
105 //fills globaldatas tree names
106 globaldata->Treenames = globaldata->Groups;
108 //create treemap class from groupmap for tree class to use
109 tmap = new TreeMap();
110 tmap->makeSim(globaldata->gGroupmap);
111 globaldata->gTreemap = tmap;
113 while(lookup[0] != NULL){
115 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(lookup[0]->getLabel()) == 1){
117 cout << lookup[0]->getLabel() << '\t' << count << endl;
119 //for each calculator
120 for(int i = 0 ; i < treeCalculators.size(); i++) {
122 //initialize simMatrix
124 simMatrix.resize(numGroups);
125 for (int m = 0; m < simMatrix.size(); m++) {
126 for (int j = 0; j < simMatrix.size(); j++) {
127 simMatrix[m].push_back(0.0);
133 for (int g = 0; g < numGroups; g++) { index[g] = g; }
135 //create a new filename
136 outputFile = getRootName(globaldata->inputFileName) + treeCalculators[i]->getName() + "." + lookup[0]->getLabel() + ".tre";
139 for (int k = 0; k < lookup.size(); k++) {
140 for (int l = k; l < lookup.size(); l++) {
141 if (k != l) { //we dont need to similiarity of a groups to itself
142 //get estimated similarity between 2 groups
144 subset.clear(); //clear out old pair of sharedrabunds
145 //add new pair of sharedrabunds
146 subset.push_back(lookup[k]); subset.push_back(lookup[l]);
148 data = treeCalculators[i]->getValues(subset); //saves the calculator outputs
149 //save values in similarity matrix
150 simMatrix[k][l] = data[0];
151 simMatrix[l][k] = data[0];
156 //creates tree from similarity matrix and write out file
161 //prevent memory leak
162 for (int i = 0; i < lookup.size(); i++) { delete lookup[i]; }
164 //get next line to process
165 lookup = input->getSharedRAbundVectors();
169 //reset groups parameter
170 globaldata->Groups.clear(); globaldata->setGroups("");
174 catch(exception& e) {
175 cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
179 cout << "An unknown error has occurred in the TreeGroupCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
183 //**********************************************************************************************************************
185 void TreeGroupCommand::createTree(){
190 //do merges and create tree structure by setting parents and children
191 //there are numGroups - 1 merges to do
192 for (int i = 0; i < (numGroups - 1); i++) {
196 //find largest value in sims matrix by searching lower triangle
197 for (int j = 1; j < simMatrix.size(); j++) {
198 for (int k = 0; k < j; k++) {
199 if (simMatrix[j][k] > largest) { largest = simMatrix[j][k]; row = j; column = k; }
203 //set non-leaf node info and update leaves to know their parents
205 t->tree[numGroups + i].setChildren(index[row], index[column]);
208 t->tree[index[row]].setParent(numGroups + i);
209 t->tree[index[column]].setParent(numGroups + i);
211 //blength = distance / 2;
212 float blength = ((1.0 - largest) / 2);
215 t->tree[index[row]].setBranchLength(blength - t->tree[index[row]].getLengthToLeaves());
216 t->tree[index[column]].setBranchLength(blength - t->tree[index[column]].getLengthToLeaves());
218 //set your length to leaves to your childs length plus branchlength
219 t->tree[numGroups + i].setLengthToLeaves(t->tree[index[row]].getLengthToLeaves() + t->tree[index[row]].getBranchLength());
223 index[row] = numGroups+i;
224 index[column] = numGroups+i;
226 //zero out highest value that caused the merge.
227 simMatrix[row][column] = 0.0;
228 simMatrix[column][row] = 0.0;
230 //merge values in simsMatrix
231 for (int n = 0; n < simMatrix.size(); n++) {
232 //row becomes merge of 2 groups
233 simMatrix[row][n] = (simMatrix[row][n] + simMatrix[column][n]) / 2;
234 simMatrix[n][row] = simMatrix[row][n];
236 simMatrix[column][n] = 0.0;
237 simMatrix[n][column] = 0.0;
245 t->createNewickFile(outputFile);
251 catch(exception& e) {
252 cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function createTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
256 cout << "An unknown error has occurred in the TreeGroupCommand class function createTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
260 /***********************************************************/
261 void TreeGroupCommand::printSims(ostream& out) {
264 //output column headers
266 for (int i = 0; i < lookup.size(); i++) { out << lookup[i]->getGroup() << '\t'; }
270 for (int m = 0; m < simMatrix.size(); m++) {
271 out << lookup[m]->getGroup() << '\t';
272 for (int n = 0; n < simMatrix.size(); n++) {
273 out << simMatrix[m][n] << '\t';
279 catch(exception& e) {
280 cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
284 cout << "An unknown error has occurred in the TreeGroupCommand class function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
288 /***********************************************************/