]> git.donarmstrong.com Git - mothur.git/blobdiff - treegroupscommand.cpp
added parse.list command
[mothur.git] / treegroupscommand.cpp
index dae04f556490260903f99b4f349d6faea249e26e..e8f68e7f34c52258e480bd8e6f369a200734df1f 100644 (file)
@@ -27,7 +27,6 @@ TreeGroupCommand::TreeGroupCommand(string option){
                globaldata = GlobalData::getInstance();
                abort = false;
                allLines = 1;
-               lines.clear();
                labels.clear();
                Groups.clear();
                Estimators.clear();
@@ -37,37 +36,68 @@ TreeGroupCommand::TreeGroupCommand(string option){
                
                else {
                        //valid paramters for this command
-                       string Array[] =  {"line","label","calc","groups", "phylip", "column", "name", "precision","cutoff"};
+                       string Array[] =  {"label","calc","groups", "phylip", "column", "name", "precision","cutoff","outputdir","inputdir"};
                        vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                        
                        OptionParser parser(option);
                        map<string, string> parameters = parser. getParameters();
                        
                        ValidParameters validParameter;
+                       map<string, string>::iterator it;
                
                        //check to make sure all parameters are valid for command
-                       for (map<string, string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
+                       for (it = parameters.begin(); it != parameters.end(); it++) { 
                                if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
                        }
                        
+                       //if the user changes the input directory command factory will send this info to us in the output parameter 
+                       string inputDir = validParameter.validFile(parameters, "inputdir", false);              
+                       if (inputDir == "not found"){   inputDir = "";          }
+                       else {
+                               string path;
+                               it = parameters.find("phylip");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = hasPath(it->second);
+                                       //if the user has not given a path then, add inputdir. else leave path alone.
+                                       if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
+                               }
+                               
+                               it = parameters.find("column");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = hasPath(it->second);
+                                       //if the user has not given a path then, add inputdir. else leave path alone.
+                                       if (path == "") {       parameters["column"] = inputDir + it->second;           }
+                               }
+                               
+                               it = parameters.find("name");
+                               //user has given a template file
+                               if(it != parameters.end()){ 
+                                       path = hasPath(it->second);
+                                       //if the user has not given a path then, add inputdir. else leave path alone.
+                                       if (path == "") {       parameters["name"] = inputDir + it->second;             }
+                               }
+                       }
+                       
+                       format = globaldata->getFormat();
+                       
                        //required parameters
                        phylipfile = validParameter.validFile(parameters, "phylip", true);
                        if (phylipfile == "not open") { abort = true; }
                        else if (phylipfile == "not found") { phylipfile = ""; }        
-                       else {  format = "phylip";      }
+                       else {  format = "phylip";  globaldata->setPhylipFile(phylipfile);      }
                        
                        columnfile = validParameter.validFile(parameters, "column", true);
                        if (columnfile == "not open") { abort = true; } 
                        else if (columnfile == "not found") { columnfile = ""; }
-                       else {  format = "column";      }
+                       else {  format = "column"; globaldata->setColumnFile(columnfile);       }
                        
                        namefile = validParameter.validFile(parameters, "name", true);
                        if (namefile == "not open") { abort = true; }   
                        else if (namefile == "not found") { namefile = ""; }
                        else {  globaldata->setNameFile(namefile);      }
                        
-                       format = globaldata->getFormat();
-                       
                        //error checking on files                       
                        if ((globaldata->getSharedFile() == "") && ((phylipfile == "") && (columnfile == "")))  { mothurOut("You must run the read.otu command or provide a distance file before running the tree.shared command."); mothurOutEndLine(); abort = true; }
                        else if ((phylipfile != "") && (columnfile != "")) { mothurOut("When running the tree.shared command with a distance file you may not use both the column and the phylip parameters."); mothurOutEndLine(); abort = true; }
@@ -78,13 +108,6 @@ TreeGroupCommand::TreeGroupCommand(string option){
 
                        //check for optional parameter and set defaults
                        // ...at some point should added some additional type checking...
-                       line = validParameter.validFile(parameters, "line", false);                             
-                       if (line == "not found") { line = "";  }
-                       else { 
-                               if(line != "all") {  splitAtDash(line, lines);  allLines = 0;  }
-                               else { allLines = 1;  }
-                       }
-                       
                        label = validParameter.validFile(parameters, "label", false);                   
                        if (label == "not found") { label = ""; }
                        else { 
@@ -92,13 +115,10 @@ TreeGroupCommand::TreeGroupCommand(string option){
                                else { allLines = 1;  }
                        }
                        
-                       //make sure user did not use both the line and label parameters
-                       if ((line != "") && (label != "")) { mothurOut("You cannot use both the line and label parameters at the same time. "); mothurOutEndLine(); abort = true; }
-                       //if the user has not specified any line or labels use the ones from read.otu
-                       else if((line == "") && (label == "")) {  
+                       //if the user has not specified any labels use the ones from read.otu
+                       if(label == "") {  
                                allLines = globaldata->allLines; 
                                labels = globaldata->labels; 
-                               lines = globaldata->lines;
                        }
                                
                        groups = validParameter.validFile(parameters, "groups", false);                 
@@ -122,6 +142,12 @@ TreeGroupCommand::TreeGroupCommand(string option){
                        temp = validParameter.validFile(parameters, "cutoff", false);                   if (temp == "not found") { temp = "10"; }
                        convert(temp, cutoff); 
                        cutoff += (5 / (precision * 10.0));
+                       
+                       //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  
+                       }
 
                                
                        if (abort == false) {
@@ -172,13 +198,13 @@ void TreeGroupCommand::help(){
        try {
                mothurOut("The tree.shared command creates a .tre to represent the similiarity between groups or sequences.\n");
                mothurOut("The tree.shared command can only be executed after a successful read.otu command or by providing a distance file.\n");
-               mothurOut("The tree.shared command parameters are groups, calc, phylip, column, name, cutoff, precision, line and label.  You may not use line and label at the same time.\n");
+               mothurOut("The tree.shared command parameters are groups, calc, phylip, column, name, cutoff, precision and label.\n");
                mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like included used.\n");
-               mothurOut("The group names are separated by dashes. The line and label allow you to select what distance levels you would like trees created for, and are also separated by dashes.\n");
+               mothurOut("The group names are separated by dashes. The label allow you to select what distance levels you would like trees created for, and are also separated by dashes.\n");
                mothurOut("The phylip or column parameter are required if you do not run the read.otu command first, and only one may be used.  If you use a column file the name filename is required. \n");
                mothurOut("If you do not provide a cutoff value 10.00 is assumed. If you do not provide a precision value then 100 is assumed.\n");
-               mothurOut("The tree.shared command should be in the following format: tree.shared(groups=yourGroups, calc=yourCalcs, line=yourLines, label=yourLabels).\n");
-               mothurOut("Example tree.shared(groups=A-B-C, line=1-3-5, calc=jabund-sorabund).\n");
+               mothurOut("The tree.shared command should be in the following format: tree.shared(groups=yourGroups, calc=yourCalcs, label=yourLabels).\n");
+               mothurOut("Example tree.shared(groups=A-B-C, calc=jabund-sorabund).\n");
                mothurOut("The default value for groups is all the groups in your groupfile.\n");
                mothurOut("The default value for calc is jclass-thetayc.\n");
                mothurOut("The tree.shared command outputs a .tre file for each calculator you specify at each distance you choose.\n");
@@ -199,9 +225,9 @@ void TreeGroupCommand::help(){
 TreeGroupCommand::~TreeGroupCommand(){
        if (abort == false) {
                
-               if (format == "sharedfile") { delete read;  delete input; globaldata->ginput = NULL;}
+               if (format == "sharedfile") { delete read;  delete input; globaldata->ginput = NULL; }
                else { delete readMatrix;  delete matrix; delete list; }
-               delete tmap;
+               delete tmap;  globaldata->gTreemap = NULL;
                delete validCalculator;
        }
        
@@ -273,7 +299,7 @@ int TreeGroupCommand::execute(){
                        makeSimsDist();
 
                        //create a new filename
-                       outputFile = getRootName(globaldata->inputFileName) + "tre";    
+                       outputFile = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + "tre"; 
                                
                        createTree();
                        mothurOut("Tree complete. "); mothurOutEndLine();
@@ -427,7 +453,6 @@ void TreeGroupCommand::makeSimsDist() {
 /***********************************************************/
 void TreeGroupCommand::makeSimsShared() {
        try {
-               int count = 1;  
        
                //clear globaldatas old tree names if any
                globaldata->Treenames.clear();
@@ -442,29 +467,32 @@ void TreeGroupCommand::makeSimsShared() {
                
                set<string> processedLabels;
                set<string> userLabels = labels;
-               set<int> userLines = lines;
-
+               
                //as long as you are not at the end of the file or done wih the lines you want
-               while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0) || (userLines.size() != 0))) {
+               while((lookup[0] != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
                
-                       if(allLines == 1 || lines.count(count) == 1 || labels.count(lookup[0]->getLabel()) == 1){                       
-                               mothurOut(lookup[0]->getLabel() + "\t" + toString(count)); mothurOutEndLine();
+                       if(allLines == 1 || labels.count(lookup[0]->getLabel()) == 1){                  
+                               mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
                                process(lookup);
                                
                                processedLabels.insert(lookup[0]->getLabel());
                                userLabels.erase(lookup[0]->getLabel());
-                               userLines.erase(count);
                        }
                        
                        if ((anyLabelsToProcess(lookup[0]->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
+                               string saveLabel = lookup[0]->getLabel();
+                       
                                for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
                                lookup = input->getSharedRAbundVectors(lastLabel);
 
-                               mothurOut(lookup[0]->getLabel() + "\t" + toString(count)); mothurOutEndLine();
+                               mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
                                process(lookup);
                                        
                                processedLabels.insert(lookup[0]->getLabel());
                                userLabels.erase(lookup[0]->getLabel());
+                               
+                               //restore real lastlabel to save below
+                               lookup[0]->setLabel(saveLabel);
                        }
 
                        lastLabel = lookup[0]->getLabel();                      
@@ -472,7 +500,6 @@ void TreeGroupCommand::makeSimsShared() {
                        //get next line to process
                        for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
                        lookup = input->getSharedRAbundVectors();
-                       count++;
                }
                
                //output error messages about any remaining user labels
@@ -488,12 +515,12 @@ void TreeGroupCommand::makeSimsShared() {
                        }
                }
                
-               //run last line if you need to
+               //run last label if you need to
                if (needToRun == true)  {
-                       for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  } 
+                       for (int i = 0; i < lookup.size(); i++) {  if (lookup[i] != NULL) {             delete lookup[i]; }             } 
                        lookup = input->getSharedRAbundVectors(lastLabel);
 
-                       mothurOut(lookup[0]->getLabel() + "\t" + toString(count)); mothurOutEndLine();
+                       mothurOut(lookup[0]->getLabel()); mothurOutEndLine();
                        process(lookup);
                        for (int i = 0; i < lookup.size(); i++) {  delete lookup[i];  }         
                }
@@ -529,7 +556,7 @@ void TreeGroupCommand::process(vector<SharedRAbundVector*> thisLookup) {
                                        for (int g = 0; g < numGroups; g++) {   index[g] = g;   }
                
                                        //create a new filename
-                                       outputFile = getRootName(globaldata->inputFileName) + treeCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".tre";                         
+                                       outputFile = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + treeCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".tre";                              
                                                                                                
                                        for (int k = 0; k < thisLookup.size(); k++) { 
                                                for (int l = k; l < thisLookup.size(); l++) {