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