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