]> git.donarmstrong.com Git - mothur.git/blob - treegroupscommand.cpp
047ea6173103b5c2ad82c19c6213ab5bab9ed978
[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                 vector<SharedRAbundVector*> subset;
87                 
88                 //if the users entered no valid calculators don't execute command
89                 if (treeCalculators.size() == 0) { return 0; }
90
91                 if (format == "sharedfile") {
92                         //you have groups
93                         read = new ReadOTUFile(globaldata->inputFileName);      
94                         read->read(&*globaldata); 
95                         
96                         input = globaldata->ginput;
97                         lookup = input->getSharedRAbundVectors();
98                 }else {
99                         //you are using a list and a groupfile
100                         read = new ReadOTUFile(globaldata->inputFileName);      
101                         read->read(&*globaldata); 
102                 
103                         input = globaldata->ginput;
104                         SharedList = globaldata->gSharedList;
105                         lookup = SharedList->getSharedRAbundVector();
106                 }
107                 
108                 if (lookup.size() < 2) { cout << "You have not provided enough valid groups.  I cannot run the command." << endl; }
109                 
110                 numGroups = globaldata->Groups.size();
111                 groupNames = "";
112                 for (int i = 0; i < numGroups; i++) { groupNames += globaldata->Groups[i]; }
113                 
114                 //clear globaldatas old tree names if any
115                 globaldata->Treenames.clear();
116                 
117                 //fills globaldatas tree names
118                 globaldata->Treenames = globaldata->Groups;
119                 
120                 //create treemap class from groupmap for tree class to use
121                 tmap = new TreeMap();
122                 tmap->makeSim(globaldata->gGroupmap);
123                 globaldata->gTreemap = tmap;
124                         
125                 while(lookup[0] != NULL){
126                 
127                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(lookup[0]->getLabel()) == 1){                   
128                                 
129                                 cout << lookup[0]->getLabel() << '\t' << count << endl;
130                                 
131                                 //for each calculator                                                                                           
132                                 for(int i = 0 ; i < treeCalculators.size(); i++) {
133                                         
134                                         //initialize simMatrix
135                                         simMatrix.clear();
136                                         simMatrix.resize(numGroups);
137                                         for (int m = 0; m < simMatrix.size(); m++)      {
138                                                 for (int j = 0; j < simMatrix.size(); j++)      {
139                                                         simMatrix[m].push_back(0.0);
140                                                 }
141                                         }
142                                 
143                                         //initialize index
144                                         index.clear();
145                                         for (int g = 0; g < numGroups; g++) {   index[g] = g;   }
146                 
147                                         //create a new filename
148                                         outputFile = getRootName(globaldata->inputFileName) + treeCalculators[i]->getName() + "." + lookup[0]->getLabel() + ".tre";
149                                                         
150                                         for (int k = 0; k < lookup.size(); k++) { 
151                                                 for (int l = k; l < lookup.size(); l++) {
152                                                         if (k != l) { //we dont need to similiarity of a groups to itself
153                                                                 //get estimated similarity between 2 groups
154                                                                 
155                                                                 subset.clear(); //clear out old pair of sharedrabunds
156                                                                 //add new pair of sharedrabunds
157                                                                 subset.push_back(lookup[k]); subset.push_back(lookup[l]); 
158                                                                 
159                                                                 data = treeCalculators[i]->getValues(subset); //saves the calculator outputs
160                                                                 //save values in similarity matrix
161                                                                 simMatrix[k][l] = data[0];
162                                                                 simMatrix[l][k] = data[0];
163                                                         }
164                                                 }
165                                         }
166                                         
167                                         //creates tree from similarity matrix and write out file
168                                         createTree();
169                                 }
170                         }
171                 
172                         //get next line to process
173                         lookup = input->getSharedRAbundVectors();
174                         count++;
175                 }
176                 
177                 //reset groups parameter
178                 globaldata->Groups.clear();  globaldata->setGroups("");
179
180                 return 0;
181         }
182         catch(exception& e) {
183                 cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
184                 exit(1);
185         }
186         catch(...) {
187                 cout << "An unknown error has occurred in the TreeGroupCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
188                 exit(1);
189         }               
190 }
191 //**********************************************************************************************************************
192
193 void TreeGroupCommand::createTree(){
194         try {
195                 //create tree
196                 t = new Tree();
197                 
198                 //do merges and create tree structure by setting parents and children
199                 //there are numGroups - 1 merges to do
200                 for (int i = 0; i < (numGroups - 1); i++) {
201                         
202                         float largest = 0.0;
203                         int row, column;
204                         //find largest value in sims matrix by searching lower triangle
205                         for (int j = 1; j < simMatrix.size(); j++) {
206                                 for (int k = 0; k < j; k++) {
207                                         if (simMatrix[j][k] > largest) {  largest = simMatrix[j][k]; row = j; column = k;  }
208                                 }
209                         }
210
211                         //set non-leaf node info and update leaves to know their parents
212                         //non-leaf
213                         t->tree[numGroups + i].setChildren(index[row], index[column]);
214                         
215                         //parents
216                         t->tree[index[row]].setParent(numGroups + i);
217                         t->tree[index[column]].setParent(numGroups + i);
218                         
219                         //blength = distance / 2;
220                         float blength = ((1.0 - largest) / 2);
221                         
222                         //branchlengths
223                         t->tree[index[row]].setBranchLength(blength - t->tree[index[row]].getLengthToLeaves());
224                         t->tree[index[column]].setBranchLength(blength - t->tree[index[column]].getLengthToLeaves());
225                         
226                         //set your length to leaves to your childs length plus branchlength
227                         t->tree[numGroups + i].setLengthToLeaves(t->tree[index[row]].getLengthToLeaves() + t->tree[index[row]].getBranchLength());
228                         
229                         
230                         //update index 
231                         index[row] = numGroups+i;
232                         index[column] = numGroups+i;
233                         
234                         //zero out highest value that caused the merge.
235                         simMatrix[row][column] = 0.0;
236                         simMatrix[column][row] = 0.0;
237                         
238                         //merge values in simsMatrix
239                         for (int n = 0; n < simMatrix.size(); n++)      {
240                                 //row becomes merge of 2 groups
241                                 simMatrix[row][n] = (simMatrix[row][n] + simMatrix[column][n]) / 2;
242                                 simMatrix[n][row] = simMatrix[row][n];
243                                 //delete column
244                                 simMatrix[column][n] = 0.0;
245                                 simMatrix[n][column] = 0.0;
246                         }
247                 }
248         
249                 //assemble tree
250                 t->assembleTree();
251                 
252                 //print newick file
253                 t->createNewickFile(outputFile);
254                 
255                 //delete tree
256                 delete t;
257         
258         }
259         catch(exception& e) {
260                 cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function createTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
261                 exit(1);
262         }
263         catch(...) {
264                 cout << "An unknown error has occurred in the TreeGroupCommand class function createTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
265                 exit(1);
266         }
267 }
268 /***********************************************************/
269 void TreeGroupCommand::printSims() {
270         try {
271                 cout << "simsMatrix" << endl;
272                 for (int m = 0; m < simMatrix.size(); m++)      {
273                         for (int n = 0; n < simMatrix.size(); n++)      {
274                                 cout << simMatrix[m][n] << '\t'; 
275                         }
276                         cout << endl;
277                 }
278
279         }
280         catch(exception& e) {
281                 cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
282                 exit(1);
283         }
284         catch(...) {
285                 cout << "An unknown error has occurred in the TreeGroupCommand class function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
286                 exit(1);
287         }               
288 }
289 /***********************************************************/
290
291