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