]> git.donarmstrong.com Git - mothur.git/blobdiff - bootstrapsharedcommand.cpp
added set.current and get.current commands and modified existing commands to update...
[mothur.git] / bootstrapsharedcommand.cpp
index b44ce4b15d8f4121db33d12285616da1c8fde8f7..afc3cf1533475b6bf78b8127ba34086193612371 100644 (file)
 #include "sharedmorisitahorn.h"
 #include "sharedbraycurtis.h"
 
-
 //**********************************************************************************************************************
-
+vector<string> BootSharedCommand::getValidParameters(){        
+       try {
+               string AlignArray[] =  {"label","calc","groups","iters","outputdir","inputdir"};
+               vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "BootSharedCommand", "getValidParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+BootSharedCommand::BootSharedCommand(){        
+       try {
+               abort = true; calledHelp = true; 
+               vector<string> tempOutNames;
+               outputTypes["tree"] = tempOutNames;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "BootSharedCommand", "BootSharedCommand");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> BootSharedCommand::getRequiredParameters(){     
+       try {
+               vector<string> myArray;
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "BootSharedCommand", "getRequiredParameters");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+vector<string> BootSharedCommand::getRequiredFiles(){  
+       try {
+               string AlignArray[] =  {"shared"};
+               vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
+               return myArray;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "BootSharedCommand", "getRequiredFiles");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
 BootSharedCommand::BootSharedCommand(string option) {
        try {
                globaldata = GlobalData::getInstance();
-               abort = false;
+               abort = false; calledHelp = false;   
                allLines = 1;
                labels.clear();
                Groups.clear();
                Estimators.clear();
                
                //allow user to run help
-               if(option == "help") { help(); abort = true; }
+               if(option == "help") { help(); abort = true; calledHelp = true; }
                
                else {
                        //valid paramters for this command
@@ -49,10 +94,14 @@ BootSharedCommand::BootSharedCommand(string option) {
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
                        
+                       //initialize outputTypes
+                       vector<string> tempOutNames;
+                       outputTypes["tree"] = tempOutNames;
+               
                        //if the user changes the output directory command factory will send this info to us in the output parameter 
                        outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  
                                outputDir = ""; 
-                               outputDir += hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it  
+                               outputDir += m->hasPath(globaldata->inputFileName); //if user entered a file with a path then preserve it       
                        }
 
                        
@@ -67,7 +116,7 @@ BootSharedCommand::BootSharedCommand(string option) {
                        label = validParameter.validFile(parameters, "label", false);                   
                        if (label == "not found") { label = ""; }
                        else { 
-                               if(label != "all") {  splitAtDash(label, labels);  allLines = 0;  }
+                               if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
                                else { allLines = 1;  }
                        }
                        
@@ -80,7 +129,7 @@ BootSharedCommand::BootSharedCommand(string option) {
                        groups = validParameter.validFile(parameters, "groups", false);                 
                        if (groups == "not found") { groups = ""; }
                        else { 
-                               splitAtDash(groups, Groups);
+                               m->splitAtDash(groups, Groups);
                                globaldata->Groups = Groups;
                        }
                                
@@ -89,7 +138,7 @@ BootSharedCommand::BootSharedCommand(string option) {
                        else { 
                                 if (calc == "default")  {  calc = "jclass-thetayc";  }
                        }
-                       splitAtDash(calc, Estimators);
+                       m->splitAtDash(calc, Estimators);
 
                        string temp;
                        temp = validParameter.validFile(parameters, "iters", false);  if (temp == "not found") { temp = "1000"; }
@@ -185,7 +234,7 @@ BootSharedCommand::~BootSharedCommand(){
 int BootSharedCommand::execute(){
        try {
        
-               if (abort == true) {    return 0;       }
+               if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
        
                util = new SharedUtil();        
        
@@ -219,24 +268,27 @@ int BootSharedCommand::execute(){
                globaldata->gTreemap = tmap;
                        
                while((order != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
-               
+                       if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str());  } globaldata->Groups.clear(); return 0;        } 
+       
                        if(allLines == 1 || labels.count(order->getLabel()) == 1){                      
                                
                                m->mothurOut(order->getLabel()); m->mothurOutEndLine();
-                               process(order);
+                               int error = process(order);
+                               if (error == 1) {  for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str());  } globaldata->Groups.clear(); return 0;        } 
                                
                                processedLabels.insert(order->getLabel());
                                userLabels.erase(order->getLabel());
                        }
                        
                        //you have a label the user want that is smaller than this label and the last label has not already been processed
-                       if ((anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
+                       if ((m->anyLabelsToProcess(order->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
                                string saveLabel = order->getLabel();
                                
                                delete order;
                                order = input->getSharedOrderVector(lastLabel);                                                                                                 
                                m->mothurOut(order->getLabel()); m->mothurOutEndLine();
-                               process(order);
+                               int error = process(order);
+                               if (error == 1) {  for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str());  } globaldata->Groups.clear(); return 0;        } 
 
                                processedLabels.insert(order->getLabel());
                                userLabels.erase(order->getLabel());
@@ -253,6 +305,9 @@ int BootSharedCommand::execute(){
                        order = input->getSharedOrderVector();
                }
                
+               
+               if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str());  } globaldata->Groups.clear(); return 0;        } 
+
                //output error messages about any remaining user labels
                set<string>::iterator it;
                bool needToRun = false;
@@ -266,19 +321,32 @@ int BootSharedCommand::execute(){
                        }
                }
                
+               if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str());  } globaldata->Groups.clear(); return 0;        } 
+
                //run last line if you need to
                if (needToRun == true)  {
                                if (order != NULL) {    delete order;   }
                                order = input->getSharedOrderVector(lastLabel);                                                                                                 
                                m->mothurOut(order->getLabel()); m->mothurOutEndLine();
-                               process(order);
+                               int error = process(order);
+                               if (error == 1) {  for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str());  } globaldata->Groups.clear(); return 0;        } 
+                               
                                delete order;
 
                }
                
+               if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str());  } globaldata->Groups.clear(); return 0;        } 
+
                //reset groups parameter
                globaldata->Groups.clear();  
                
+               //set first tree file as new current treefile
+               string currentTree = "";
+               itTypes = outputTypes.find("tree");
+               if (itTypes != outputTypes.end()) {
+                       if ((itTypes->second).size() != 0) { currentTree = (itTypes->second)[0]; m->setTreeFile(currentTree); }
+               }
+               
                m->mothurOutEndLine();
                m->mothurOut("Output File Names: "); m->mothurOutEndLine();
                for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
@@ -294,13 +362,15 @@ int BootSharedCommand::execute(){
 }
 //**********************************************************************************************************************
 
-void BootSharedCommand::createTree(ostream* out, Tree* t){
+int BootSharedCommand::createTree(ostream* out, Tree* t){
        try {
                
                //do merges and create tree structure by setting parents and children
                //there are numGroups - 1 merges to do
                for (int i = 0; i < (numGroups - 1); i++) {
                
+                       if (m->control_pressed) {  return 1; }
+               
                        float largest = -1000.0;
                        int row, column;
                        //find largest value in sims matrix by searching lower triangle
@@ -354,9 +424,13 @@ void BootSharedCommand::createTree(ostream* out, Tree* t){
 
                //assemble tree
                t->assembleTree();
+               
+               if (m->control_pressed) { return 1; }
        
                //print newick file
                t->print(*out);
+               
+               return 0;
        
        }
        catch(exception& e) {
@@ -382,7 +456,7 @@ void BootSharedCommand::printSims() {
        }
 }
 /***********************************************************/
-void BootSharedCommand::process(SharedOrderVector* order) {
+int BootSharedCommand::process(SharedOrderVector* order) {
        try{
                                EstOutput data;
                                vector<SharedRAbundVector*> subset;
@@ -390,9 +464,9 @@ void BootSharedCommand::process(SharedOrderVector* order) {
                                //open an ostream for each calc to print to
                                for (int z = 0; z < treeCalculators.size(); z++) {
                                        //create a new filename
-                                       outputFile = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + treeCalculators[z]->getName() + ".boot" + order->getLabel() + ".tre";
-                                       openOutputFile(outputFile, *(out[z]));
-                                       outputNames.push_back(outputFile);
+                                       outputFile = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + treeCalculators[z]->getName() + ".boot" + order->getLabel() + ".tre";
+                                       m->openOutputFile(outputFile, *(out[z]));
+                                       outputNames.push_back(outputFile); outputTypes["tree"].push_back(outputFile);
                                }
                                
                                m->mothurOut("Generating bootstrap trees..."); cout.flush();
@@ -400,18 +474,22 @@ void BootSharedCommand::process(SharedOrderVector* order) {
                                //create a file for each calculator with the 1000 trees in it.
                                for (int p = 0; p < iters; p++) {
                                        
+                                       if (m->control_pressed) {  return 1; }
+                                       
                                        util->getSharedVectorswithReplacement(globaldata->Groups, lookup, order);  //fills group vectors from order vector.
 
                                
                                        //for each calculator                                                                                           
                                        for(int i = 0 ; i < treeCalculators.size(); i++) {
-                                       
+                                               
+                                               if (m->control_pressed) {  return 1; }
+                                               
                                                //initialize simMatrix
                                                simMatrix.clear();
                                                simMatrix.resize(numGroups);
-                                               for (int m = 0; m < simMatrix.size(); m++)      {
+                                               for (int o = 0; o < simMatrix.size(); o++)      {
                                                        for (int j = 0; j < simMatrix.size(); j++)      {
-                                                               simMatrix[m].push_back(0.0);
+                                                               simMatrix[o].push_back(0.0);
                                                        }
                                                }
                                
@@ -437,9 +515,13 @@ void BootSharedCommand::process(SharedOrderVector* order) {
                                                
                                                tempTree = new Tree();
                                                
+                                               if (m->control_pressed) {   delete tempTree; return 1; }
+                                               
                                                //creates tree from similarity matrix and write out file
                                                createTree(out[i], tempTree);
                                                
+                                               if (m->control_pressed) {   delete tempTree; return 1; }
+                                               
                                                //save trees for consensus command.
                                                trees[i].push_back(tempTree);
                                        }
@@ -456,10 +538,12 @@ void BootSharedCommand::process(SharedOrderVector* order) {
                                        
                                        m->mothurOut("Generating consensus tree for " + treeCalculators[k]->getName()); m->mothurOutEndLine();
                                        
+                                       if (m->control_pressed) {  return 1; }
+                                       
                                        //set global data to calc trees
                                        globaldata->gTree = trees[k];
                                        
-                                       string filename = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + treeCalculators[k]->getName() + ".boot" + order->getLabel();
+                                       string filename = outputDir + m->getRootName(m->getSimpleName(globaldata->inputFileName)) + treeCalculators[k]->getName() + ".boot" + order->getLabel();
                                        consensus = new ConcensusCommand(filename);
                                        consensus->execute();
                                        delete consensus;
@@ -477,6 +561,8 @@ void BootSharedCommand::process(SharedOrderVector* order) {
                                        
                                //close ostream for each calc
                                for (int z = 0; z < treeCalculators.size(); z++) { out[z]->close(); }
+                               
+                               return 0;
        
        }
        catch(exception& e) {