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