]> git.donarmstrong.com Git - mothur.git/blob - treegroupscommand.cpp
added concensus command and updated calcs
[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                         read = new ReadOTUFile(globaldata->inputFileName);      
93                         read->read(&*globaldata); 
94                         
95                         input = globaldata->ginput;
96                         order = input->getSharedOrderVector();
97                 }else {
98                         //you are using a list and a groupfile
99                         read = new ReadOTUFile(globaldata->inputFileName);      
100                         read->read(&*globaldata); 
101                 
102                         input = globaldata->ginput;
103                         SharedList = globaldata->gSharedList;
104                         order = SharedList->getSharedOrderVector();
105                 }
106                 
107                 //set users groups
108                 util->setGroups(globaldata->Groups, globaldata->gGroupmap->namesOfGroups, "treegroup");
109                 numGroups = globaldata->Groups.size();
110                 groupNames = "";
111                 for (int i = 0; i < numGroups; i++) { groupNames += globaldata->Groups[i]; }
112                 
113                 //clear globaldatas old tree names if any
114                 globaldata->Treenames.clear();
115                 
116                 //fills globaldatas tree names
117                 globaldata->Treenames = globaldata->Groups;
118                 
119                 //create treemap class from groupmap for tree class to use
120                 tmap = new TreeMap();
121                 tmap->makeSim(globaldata->gGroupmap);
122                 globaldata->gTreemap = tmap;
123                         
124                 while(order != NULL){
125                 
126                         if(globaldata->allLines == 1 || globaldata->lines.count(count) == 1 || globaldata->labels.count(order->getLabel()) == 1){                       
127                                 
128                                 cout << order->getLabel() << '\t' << count << endl;
129                                 util->getSharedVectors(globaldata->Groups, lookup, order);  //fills group vectors from order vector.
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() + "." + order->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                         if (format == "sharedfile") {
174                                 order = input->getSharedOrderVector();
175                         }else {
176                                 //you are using a list and a groupfile
177                                 SharedList = input->getSharedListVector(); //get new list vector to process
178                                 if (SharedList != NULL) {
179                                         order = SharedList->getSharedOrderVector(); //gets new order vector with group info.
180                                 }else {
181                                         break;
182                                 }
183                         }
184                         count++;
185                 }
186                 
187                 //reset groups parameter
188                 globaldata->Groups.clear();  globaldata->setGroups("");
189
190                 return 0;
191         }
192         catch(exception& e) {
193                 cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
194                 exit(1);
195         }
196         catch(...) {
197                 cout << "An unknown error has occurred in the TreeGroupCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
198                 exit(1);
199         }               
200 }
201 //**********************************************************************************************************************
202
203 void TreeGroupCommand::createTree(){
204         try {
205                 //create tree
206                 t = new Tree();
207                 
208                 //do merges and create tree structure by setting parents and children
209                 //there are numGroups - 1 merges to do
210                 for (int i = 0; i < (numGroups - 1); i++) {
211                         
212                         float largest = 0.0;
213                         int row, column;
214                         //find largest value in sims matrix by searching lower triangle
215                         for (int j = 1; j < simMatrix.size(); j++) {
216                                 for (int k = 0; k < j; k++) {
217                                         if (simMatrix[j][k] > largest) {  largest = simMatrix[j][k]; row = j; column = k;  }
218                                 }
219                         }
220
221                         //set non-leaf node info and update leaves to know their parents
222                         //non-leaf
223                         t->tree[numGroups + i].setChildren(index[row], index[column]);
224                         
225                         //parents
226                         t->tree[index[row]].setParent(numGroups + i);
227                         t->tree[index[column]].setParent(numGroups + i);
228                         
229                         //blength = distance / 2;
230                         float blength = ((1.0 - largest) / 2);
231                         
232                         //branchlengths
233                         t->tree[index[row]].setBranchLength(blength - t->tree[index[row]].getLengthToLeaves());
234                         t->tree[index[column]].setBranchLength(blength - t->tree[index[column]].getLengthToLeaves());
235                         
236                         //set your length to leaves to your childs length plus branchlength
237                         t->tree[numGroups + i].setLengthToLeaves(t->tree[index[row]].getLengthToLeaves() + t->tree[index[row]].getBranchLength());
238                         
239                         
240                         //update index 
241                         index[row] = numGroups+i;
242                         index[column] = numGroups+i;
243                         
244                         //zero out highest value that caused the merge.
245                         simMatrix[row][column] = 0.0;
246                         simMatrix[column][row] = 0.0;
247                         
248                         //merge values in simsMatrix
249                         for (int n = 0; n < simMatrix.size(); n++)      {
250                                 //row becomes merge of 2 groups
251                                 simMatrix[row][n] = (simMatrix[row][n] + simMatrix[column][n]) / 2;
252                                 simMatrix[n][row] = simMatrix[row][n];
253                                 //delete column
254                                 simMatrix[column][n] = 0.0;
255                                 simMatrix[n][column] = 0.0;
256                         }
257                 }
258         
259                 //assemble tree
260                 t->assembleTree();
261                 
262                 //print newick file
263                 t->createNewickFile(outputFile);
264                 
265                 //delete tree
266                 delete t;
267         
268         }
269         catch(exception& e) {
270                 cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function createTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
271                 exit(1);
272         }
273         catch(...) {
274                 cout << "An unknown error has occurred in the TreeGroupCommand class function createTree. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
275                 exit(1);
276         }
277 }
278 /***********************************************************/
279 void TreeGroupCommand::printSims() {
280         try {
281                 cout << "simsMatrix" << endl;
282                 for (int m = 0; m < simMatrix.size(); m++)      {
283                         for (int n = 0; n < simMatrix.size(); n++)      {
284                                 cout << simMatrix[m][n] << '\t'; 
285                         }
286                         cout << endl;
287                 }
288
289         }
290         catch(exception& e) {
291                 cout << "Standard Error: " << e.what() << " has occurred in the TreeGroupCommand class Function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
292                 exit(1);
293         }
294         catch(...) {
295                 cout << "An unknown error has occurred in the TreeGroupCommand class function printSims. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
296                 exit(1);
297         }               
298 }
299 /***********************************************************/
300
301