]> git.donarmstrong.com Git - mothur.git/blob - treegroupscommand.cpp
This is mothur v 1.2.0 - the April ~24, 2009 release
[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 #include "sharedbraycurtis.h"
21
22
23 //**********************************************************************************************************************
24
25 TreeGroupCommand::TreeGroupCommand(){
26         try {
27                 globaldata = GlobalData::getInstance();
28                 format = globaldata->getFormat();
29                 validCalculator = new ValidCalculators();
30                 util = new SharedUtil();
31                 
32                 int i;
33                 for (i=0; i<globaldata->Estimators.size(); i++) {
34                         if (validCalculator->isValidCalculator("treegroup", globaldata->Estimators[i]) == true) { 
35                                 if (globaldata->Estimators[i] == "jabund") {    
36                                         treeCalculators.push_back(new JAbund());
37                                 }else if (globaldata->Estimators[i] == "sorabund") { 
38                                         treeCalculators.push_back(new SorAbund());
39                                 }else if (globaldata->Estimators[i] == "jclass") { 
40                                         treeCalculators.push_back(new Jclass());
41                                 }else if (globaldata->Estimators[i] == "sorclass") { 
42                                         treeCalculators.push_back(new SorClass());
43                                 }else if (globaldata->Estimators[i] == "jest") { 
44                                         treeCalculators.push_back(new Jest());
45                                 }else if (globaldata->Estimators[i] == "sorest") { 
46                                         treeCalculators.push_back(new SorEst());
47                                 }else if (globaldata->Estimators[i] == "thetayc") { 
48                                         treeCalculators.push_back(new ThetaYC());
49                                 }else if (globaldata->Estimators[i] == "thetan") { 
50                                         treeCalculators.push_back(new ThetaN());
51                                 }else if (globaldata->Estimators[i] == "morisitahorn") { 
52                                         treeCalculators.push_back(new MorHorn());
53                                 }else if (globaldata->Estimators[i] == "braycurtis") { 
54                                         treeCalculators.push_back(new BrayCurtis());
55                                 }
56                         }
57                 }
58                 
59                 //reset calc for next command
60                 globaldata->setCalc("");
61
62         }
63         catch(exception& e) {
64                 cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function TreeGroupCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
65                 exit(1);
66         }
67         catch(...) {
68                 cout << "An unknown error has occurred in the TreeGroupCommand class function TreeGroupCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
69                 exit(1);
70         }       
71 }
72 //**********************************************************************************************************************
73
74 TreeGroupCommand::~TreeGroupCommand(){
75         delete input;
76         delete read;
77         delete util;
78 }
79
80 //**********************************************************************************************************************
81
82 int TreeGroupCommand::execute(){
83         try {
84                 int count = 1;  
85                 EstOutput data;
86         
87                 //if the users entered no valid calculators don't execute command
88                 if (treeCalculators.size() == 0) { return 0; }
89
90                 if (format == "sharedfile") {
91                         read = new ReadOTUFile(globaldata->inputFileName);      
92                         read->read(&*globaldata); 
93                         
94                         input = globaldata->ginput;
95                         order = input->getSharedOrderVector();
96                 }else {
97                         //you are using a list and a groupfile
98                         read = new ReadOTUFile(globaldata->inputFileName);      
99                         read->read(&*globaldata); 
100                 
101                         input = globaldata->ginput;
102                         SharedList = globaldata->gSharedList;
103                         order = SharedList->getSharedOrderVector();
104                 }
105                 
106                 //set users groups
107                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups, "treegroup");
108                 numGroups = globaldata->Groups.size();
109                 groupNames = "";
110                 for (int i = 0; i < numGroups; i++) { groupNames += globaldata->Groups[i]; }
111                 
112                 //clear globaldatas old tree names if any
113                 globaldata->Treenames.clear();
114                 
115                 //fills globaldatas tree names
116                 globaldata->Treenames = globaldata->Groups;
117                 
118                 //create treemap class from groupmap for tree class to use
119                 tmap = new TreeMap();
120                 tmap->makeSim(globaldata->gGroupmap);
121                 globaldata->gTreemap = tmap;
122                         
123                 while(order != NULL){
124                 
125                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){                       
126                                 
127                                 cout << order->getLabel() << '\t' << count << endl;
128                                 util->getSharedVectors(globaldata->Groups, lookup, order);  //fills group vectors from order vector.
129                                 
130                                 //for each calculator                                                                                           
131                                 for(int i = 0 ; i < treeCalculators.size(); i++) {
132                                         
133                                         //initialize simMatrix
134                                         simMatrix.clear();
135                                         simMatrix.resize(numGroups);
136                                         for (int m = 0; m < simMatrix.size(); m++)      {
137                                                 for (int j = 0; j < simMatrix.size(); j++)      {
138                                                         simMatrix[m].push_back(0.0);
139                                                 }
140                                         }
141                                 
142                                         //initialize index
143                                         index.clear();
144                                         for (int g = 0; g < numGroups; g++) {   index[g] = g;   }
145                 
146                                         //create a new filename
147                                         outputFile = getRootName(globaldata->inputFileName) + treeCalculators[i]->getName() + "." + order->getLabel() + ".tre";
148                                                         
149                                         for (int k = 0; k < lookup.size(); k++) { 
150                                                 for (int l = k; l < lookup.size(); l++) {
151                                                         if (k != l) { //we dont need to similiarity of a groups to itself
152                                                                 //get estimated similarity between 2 groups
153                                                                 data = treeCalculators[i]->getValues(lookup[k], lookup[l]); //saves the calculator outputs
154                                                                 //save values in similarity matrix
155                                                                 simMatrix[k][l] = data[0];
156                                                                 simMatrix[l][k] = data[0];
157                                                         }
158                                                 }
159                                         }
160                                         
161                                         //creates tree from similarity matrix and write out file
162                                         createTree();
163                                 }
164                         }
165                 
166                         //get next line to process
167                         if (format == "sharedfile") {
168                                 order = input->getSharedOrderVector();
169                         }else {
170                                 //you are using a list and a groupfile
171                                 SharedList = input->getSharedListVector(); //get new list vector to process
172                                 if (SharedList != NULL) {
173                                         order = SharedList->getSharedOrderVector(); //gets new order vector with group info.
174                                 }else {
175                                         break;
176                                 }
177                         }
178                         count++;
179                 }
180                 
181                 //reset groups parameter
182                 globaldata->Groups.clear();  globaldata->setGroups("");
183
184                 return 0;
185         }
186         catch(exception& e) {
187                 cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
188                 exit(1);
189         }
190         catch(...) {
191                 cout << "An unknown error has occurred in the TreeGroupCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
192                 exit(1);
193         }               
194 }
195 //**********************************************************************************************************************
196
197 void TreeGroupCommand::createTree(){
198         try {
199                 //create tree
200                 t = new Tree();
201                 
202                 //do merges and create tree structure by setting parents and children
203                 //there are numGroups - 1 merges to do
204                 for (int i = 0; i < (numGroups - 1); i++) {
205                         
206                         float largest = 0.0;
207                         int row, column;
208                         //find largest value in sims matrix by searching lower triangle
209                         for (int j = 1; j < simMatrix.size(); j++) {
210                                 for (int k = 0; k < j; k++) {
211                                         if (simMatrix[j][k] > largest) {  largest = simMatrix[j][k]; row = j; column = k;  }
212                                 }
213                         }
214
215                         //set non-leaf node info and update leaves to know their parents
216                         //non-leaf
217                         t->tree[numGroups + i].setChildren(index[row], index[column]);
218                         
219                         //parents
220                         t->tree[index[row]].setParent(numGroups + i);
221                         t->tree[index[column]].setParent(numGroups + i);
222                         
223                         //blength = distance / 2;
224                         float blength = ((1.0 - largest) / 2);
225                         
226                         //branchlengths
227                         t->tree[index[row]].setBranchLength(blength - t->tree[index[row]].getLengthToLeaves());
228                         t->tree[index[column]].setBranchLength(blength - t->tree[index[column]].getLengthToLeaves());
229                         
230                         //set your length to leaves to your childs length plus branchlength
231                         t->tree[numGroups + i].setLengthToLeaves(t->tree[index[row]].getLengthToLeaves() + t->tree[index[row]].getBranchLength());
232                         
233                         
234                         //update index 
235                         index[row] = numGroups+i;
236                         index[column] = numGroups+i;
237                         
238                         //zero out highest value that caused the merge.
239                         simMatrix[row][column] = 0.0;
240                         simMatrix[column][row] = 0.0;
241                         
242                         //merge values in simsMatrix
243                         for (int n = 0; n < simMatrix.size(); n++)      {
244                                 //row becomes merge of 2 groups
245                                 simMatrix[row][n] = (simMatrix[row][n] + simMatrix[column][n]) / 2;
246                                 simMatrix[n][row] = simMatrix[row][n];
247                                 //delete column
248                                 simMatrix[column][n] = 0.0;
249                                 simMatrix[n][column] = 0.0;
250                         }
251                 }
252         
253                 //assemble tree
254                 t->assembleTree();
255                 
256                 //print newick file
257                 t->createNewickFile(outputFile);
258                 
259                 //delete tree
260                 delete t;
261         
262         }
263         catch(exception& e) {
264                 cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function createTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
265                 exit(1);
266         }
267         catch(...) {
268                 cout << "An unknown error has occurred in the TreeGroupCommand class function createTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
269                 exit(1);
270         }
271 }
272 /***********************************************************/
273 void TreeGroupCommand::printSims() {
274         try {
275                 cout << "simsMatrix" << endl;
276                 for (int m = 0; m < simMatrix.size(); m++)      {
277                         for (int n = 0; n < simMatrix.size(); n++)      {
278                                 cout << simMatrix[m][n] << '\t'; 
279                         }
280                         cout << endl;
281                 }
282
283         }
284         catch(exception& e) {
285                 cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
286                 exit(1);
287         }
288         catch(...) {
289                 cout << "An unknown error has occurred in the TreeGroupCommand class function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
290                 exit(1);
291         }               
292 }
293 /***********************************************************/
294
295