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