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