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"
22 //**********************************************************************************************************************
24 TreeGroupCommand::TreeGroupCommand(){
26 globaldata = GlobalData::getInstance();
27 format = globaldata->getFormat();
28 validCalculator = new ValidCalculators();
29 util = new SharedUtil();
32 for (i=0; i<globaldata->Estimators.size(); i++) {
33 if (validCalculator->isValidCalculator("treegroup", globaldata->Estimators[i]) == true) {
34 if (globaldata->Estimators[i] == "sharedjabund") {
35 treeCalculators.push_back(new SharedJAbund());
36 }else if (globaldata->Estimators[i] == "sharedsorensonabund") {
37 treeCalculators.push_back(new SharedSorAbund());
38 }else if (globaldata->Estimators[i] == "sharedjclass") {
39 treeCalculators.push_back(new SharedJclass());
40 }else if (globaldata->Estimators[i] == "sharedsorclass") {
41 treeCalculators.push_back(new SharedSorClass());
42 }else if (globaldata->Estimators[i] == "sharedjest") {
43 treeCalculators.push_back(new SharedJest());
44 }else if (globaldata->Estimators[i] == "sharedsorest") {
45 treeCalculators.push_back(new SharedSorEst());
46 }else if (globaldata->Estimators[i] == "sharedthetayc") {
47 treeCalculators.push_back(new SharedThetaYC());
48 }else if (globaldata->Estimators[i] == "sharedthetan") {
49 treeCalculators.push_back(new SharedThetaN());
50 }else if (globaldata->Estimators[i] == "sharedmorisitahorn") {
51 treeCalculators.push_back(new SharedMorHorn());
56 //reset calc for next command
57 globaldata->setCalc("");
61 cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function TreeGroupCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
65 cout << "An unknown error has occurred in the TreeGroupCommand class function TreeGroupCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
69 //**********************************************************************************************************************
71 TreeGroupCommand::~TreeGroupCommand(){
77 //**********************************************************************************************************************
79 int TreeGroupCommand::execute(){
84 //if the users entered no valid calculators don't execute command
85 if (treeCalculators.size() == 0) { return 0; }
87 if (format == "sharedfile") {
88 read = new ReadPhilFile(globaldata->inputFileName);
89 read->read(&*globaldata);
91 input = globaldata->ginput;
92 order = input->getSharedOrderVector();
94 //you are using a list and a groupfile
95 read = new ReadPhilFile(globaldata->inputFileName);
96 read->read(&*globaldata);
98 input = globaldata->ginput;
99 SharedList = globaldata->gSharedList;
100 order = SharedList->getSharedOrderVector();
104 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups, "treegroup");
105 numGroups = globaldata->Groups.size();
107 for (int i = 0; i < numGroups; i++) { groupNames += globaldata->Groups[i]; }
109 //clear globaldatas old tree names if any
110 globaldata->Treenames.clear();
112 //fills globaldatas tree names
113 globaldata->Treenames = globaldata->Groups;
115 //create treemap class from groupmap for tree class to use
116 tmap = new TreeMap();
117 tmap->makeSim(globaldata->gGroupmap);
118 globaldata->gTreemap = tmap;
120 while(order != NULL){
122 if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){
124 cout << order->getLabel() << '\t' << count << endl;
125 util->getSharedVectors(globaldata->Groups, lookup, order); //fills group vectors from order vector.
127 //for each calculator
128 for(int i = 0 ; i < treeCalculators.size(); i++) {
130 //initialize simMatrix
132 simMatrix.resize(numGroups);
133 for (int m = 0; m < simMatrix.size(); m++) {
134 for (int j = 0; j < simMatrix.size(); j++) {
135 simMatrix[m].push_back(0.0);
141 for (int g = 0; g < numGroups; g++) { index[g] = g; }
143 //create a new filename
144 outputFile = getRootName(globaldata->inputFileName) + treeCalculators[i]->getName() + "." + order->getLabel() + groupNames + ".tre";
146 for (int k = 0; k < lookup.size(); k++) { // pass cdd each set of groups to commpare
147 for (int l = k; l < lookup.size(); l++) {
148 if (k != l) { //we dont need to similiarity of a groups to itself
149 //get estimated similarity between 2 groups
150 data = treeCalculators[i]->getValues(lookup[k], lookup[l]); //saves the calculator outputs
151 //save values in similarity matrix
152 simMatrix[k][l] = data[0];
153 simMatrix[l][k] = data[0];
158 //creates tree from similarity matrix and write out file
163 //get next line to process
164 if (format == "sharedfile") {
165 order = input->getSharedOrderVector();
167 //you are using a list and a groupfile
168 SharedList = input->getSharedListVector(); //get new list vector to process
169 if (SharedList != NULL) {
170 order = SharedList->getSharedOrderVector(); //gets new order vector with group info.
178 //reset groups parameter
179 globaldata->Groups.clear(); globaldata->setGroups("");
183 catch(exception& e) {
184 cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
188 cout << "An unknown error has occurred in the TreeGroupCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
192 //**********************************************************************************************************************
194 void TreeGroupCommand::createTree(){
199 //do merges and create tree structure by setting parents and children
200 //there are numGroups - 1 merges to do
201 for (int i = 0; i < (numGroups - 1); i++) {
205 //find largest value in sims matrix by searching lower triangle
206 for (int j = 1; j < simMatrix.size(); j++) {
207 for (int k = 0; k < j; k++) {
208 if (simMatrix[j][k] > largest) { largest = simMatrix[j][k]; row = j; column = k; }
212 //set non-leaf node info and update leaves to know their parents
214 t->tree[numGroups + i].setChildren(index[row], index[column]);
217 t->tree[index[row]].setParent(numGroups + i);
218 t->tree[index[column]].setParent(numGroups + i);
220 //blength = distance / 2;
221 float blength = ((1.0 - largest) / 2);
224 t->tree[index[row]].setBranchLength(blength - t->tree[index[row]].getLengthToLeaves());
225 t->tree[index[column]].setBranchLength(blength - t->tree[index[column]].getLengthToLeaves());
227 //set your length to leaves to your childs length plus branchlength
228 t->tree[numGroups + i].setLengthToLeaves(t->tree[index[row]].getLengthToLeaves() + t->tree[index[row]].getBranchLength());
232 index[row] = numGroups+i;
233 index[column] = numGroups+i;
235 //zero out highest value that caused the merge.
236 simMatrix[row][column] = 0.0;
237 simMatrix[column][row] = 0.0;
239 //merge values in simsMatrix
240 for (int n = 0; n < simMatrix.size(); n++) {
241 //row becomes merge of 2 groups
242 simMatrix[row][n] = (simMatrix[row][n] + simMatrix[column][n]) / 2;
243 simMatrix[n][row] = simMatrix[row][n];
245 simMatrix[column][n] = 0.0;
246 simMatrix[n][column] = 0.0;
254 t->createNewickFile(outputFile);
260 catch(exception& e) {
261 cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function createTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
265 cout << "An unknown error has occurred in the TreeGroupCommand class function createTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
269 /***********************************************************/
270 void TreeGroupCommand::printSims() {
272 cout << "simsMatrix" << endl;
273 for (int m = 0; m < simMatrix.size(); m++) {
274 for (int n = 0; n < simMatrix.size(); n++) {
275 cout << simMatrix[m][n] << '\t';
281 catch(exception& e) {
282 cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
286 cout << "An unknown error has occurred in the TreeGroupCommand class function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
290 /***********************************************************/