]> git.donarmstrong.com Git - mothur.git/blob - treegroupscommand.cpp
started shared utilities, updates to venn and heatmap added tree.groups command
[mothur.git] / treegroupscommand.cpp
1 /*
2  *  treegroupscommand.cpp
3  *  Mothur
4  *
5  *  Created by Sarah Westcott on 4/8/09.
6  *  Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
7  *
8  */
9
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
21
22 //**********************************************************************************************************************
23
24 TreeGroupCommand::TreeGroupCommand(){
25         try {
26                 globaldata = GlobalData::getInstance();
27                 format = globaldata->getFormat();
28                 validCalculator = new ValidCalculators();
29                 util = new SharedUtil();
30                 
31                 int i;
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());
52                                 }
53                         }
54                 }
55                 
56                 //reset calc for next command
57                 globaldata->setCalc("");
58
59         }
60         catch(exception& e) {
61                 cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function TreeGroupCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
62                 exit(1);
63         }
64         catch(...) {
65                 cout << "An unknown error has occurred in the TreeGroupCommand class function TreeGroupCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
66                 exit(1);
67         }       
68 }
69 //**********************************************************************************************************************
70
71 TreeGroupCommand::~TreeGroupCommand(){
72         delete input;
73         delete read;
74         delete util;
75 }
76
77 //**********************************************************************************************************************
78
79 int TreeGroupCommand::execute(){
80         try {
81                 int count = 1;  
82                 EstOutput data;
83         
84                 //if the users entered no valid calculators don't execute command
85                 if (treeCalculators.size() == 0) { return 0; }
86
87                 if (format == "sharedfile") {
88                         read = new ReadPhilFile(globaldata->inputFileName);     
89                         read->read(&*globaldata); 
90                         
91                         input = globaldata->ginput;
92                         order = input->getSharedOrderVector();
93                 }else {
94                         //you are using a list and a groupfile
95                         read = new ReadPhilFile(globaldata->inputFileName);     
96                         read->read(&*globaldata); 
97                 
98                         input = globaldata->ginput;
99                         SharedList = globaldata->gSharedList;
100                         order = SharedList->getSharedOrderVector();
101                 }
102                 
103                 //set users groups
104                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups, "treegroup");
105                 numGroups = globaldata->Groups.size();
106                 groupNames = "";
107                 for (int i = 0; i < numGroups; i++) { groupNames += globaldata->Groups[i]; }
108                 
109                 //clear globaldatas old tree names if any
110                 globaldata->Treenames.clear();
111                 
112                 //fills globaldatas tree names
113                 globaldata->Treenames = globaldata->Groups;
114                 
115                 //create treemap class from groupmap for tree class to use
116                 tmap = new TreeMap();
117                 tmap->makeSim(globaldata->gGroupmap);
118                 globaldata->gTreemap = tmap;
119                         
120                 while(order != NULL){
121                 
122                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){                       
123                                 
124                                 cout << order->getLabel() << '\t' << count << endl;
125                                 util->getSharedVectors(globaldata->Groups, lookup, order);  //fills group vectors from order vector.
126                                 
127                                 //for each calculator                                                                                           
128                                 for(int i = 0 ; i < treeCalculators.size(); i++) {
129                                         
130                                         //initialize simMatrix
131                                         simMatrix.clear();
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);
136                                                 }
137                                         }
138                                 
139                                         //initialize index
140                                         index.clear();
141                                         for (int g = 0; g < numGroups; g++) {   index[g] = g;   }
142                 
143                                         //create a new filename
144                                         outputFile = getRootName(globaldata->inputFileName) + treeCalculators[i]->getName() + "." + order->getLabel() + groupNames + ".tre";
145                                                         
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];
154                                                         }
155                                                 }
156                                         }
157                                         
158                                         //creates tree from similarity matrix and write out file
159                                         createTree();
160                                 }
161                         }
162                 
163                         //get next line to process
164                         if (format == "sharedfile") {
165                                 order = input->getSharedOrderVector();
166                         }else {
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.
171                                 }else {
172                                         break;
173                                 }
174                         }
175                         count++;
176                 }
177                 
178                 //reset groups parameter
179                 globaldata->Groups.clear();  globaldata->setGroups("");
180
181                 return 0;
182         }
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";
185                 exit(1);
186         }
187         catch(...) {
188                 cout << "An unknown error has occurred in the TreeGroupCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
189                 exit(1);
190         }               
191 }
192 //**********************************************************************************************************************
193
194 void TreeGroupCommand::createTree(){
195         try {
196                 //create tree
197                 t = new Tree();
198                 
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++) {
202                         
203                         float largest = 0.0;
204                         int row, column;
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;  }
209                                 }
210                         }
211
212                         //set non-leaf node info and update leaves to know their parents
213                         //non-leaf
214                         t->tree[numGroups + i].setChildren(index[row], index[column]);
215                         
216                         //parents
217                         t->tree[index[row]].setParent(numGroups + i);
218                         t->tree[index[column]].setParent(numGroups + i);
219                         
220                         //blength = distance / 2;
221                         float blength = ((1.0 - largest) / 2);
222                         
223                         //branchlengths
224                         t->tree[index[row]].setBranchLength(blength - t->tree[index[row]].getLengthToLeaves());
225                         t->tree[index[column]].setBranchLength(blength - t->tree[index[column]].getLengthToLeaves());
226                         
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());
229                         
230                         
231                         //update index 
232                         index[row] = numGroups+i;
233                         index[column] = numGroups+i;
234                         
235                         //zero out highest value that caused the merge.
236                         simMatrix[row][column] = 0.0;
237                         simMatrix[column][row] = 0.0;
238                         
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];
244                                 //delete column
245                                 simMatrix[column][n] = 0.0;
246                                 simMatrix[n][column] = 0.0;
247                         }
248                 }
249         
250                 //assemble tree
251                 t->assembleTree();
252                 
253                 //print newick file
254                 t->createNewickFile(outputFile);
255                 
256                 //delete tree
257                 delete t;
258         
259         }
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";
262                 exit(1);
263         }
264         catch(...) {
265                 cout << "An unknown error has occurred in the TreeGroupCommand class function createTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
266                 exit(1);
267         }
268 }
269 /***********************************************************/
270 void TreeGroupCommand::printSims() {
271         try {
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'; 
276                         }
277                         cout << endl;
278                 }
279
280         }
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";
283                 exit(1);
284         }
285         catch(...) {
286                 cout << "An unknown error has occurred in the TreeGroupCommand class function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
287                 exit(1);
288         }               
289 }
290 /***********************************************************/
291
292