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