]> git.donarmstrong.com Git - mothur.git/blobdiff - clearcutcommand.cpp
initial add of metastats
[mothur.git] / clearcutcommand.cpp
index f8d389c60d81654f944e6b892ef7ad19a6d91859..c76de383fce839d31dd95e56bb7288a6df617d3c 100644 (file)
@@ -8,7 +8,8 @@
  */
 
 #include "clearcutcommand.h"
-#include "globaldata.hpp"
+#include "clearcut.h"
+
 
 /**************************************************************************************/
 ClearcutCommand::ClearcutCommand(string option)  {     
@@ -21,7 +22,7 @@ ClearcutCommand::ClearcutCommand(string option)  {
                else {
                        //valid paramters for this command
                        string Array[] =  {"fasta","phylip","version","verbose","quiet","seed","norandom","shuffle","neighbor","expblen",
-                                                               "expdist","ntrees","matrixout","stdout","kimura","jukes","protein","DNA","stdin","outputdir","inputdir"};
+                                                               "expdist","ntrees","matrixout","stdout","kimura","jukes","protein","DNA","outputdir","inputdir"};
                        vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
                        
                        OptionParser parser(option);
@@ -43,7 +44,7 @@ ClearcutCommand::ClearcutCommand(string option)  {
                                it = parameters.find("fasta");
                                //user has given a template file
                                if(it != parameters.end()){ 
-                                       path = hasPath(it->second);
+                                       path = m->hasPath(it->second);
                                        //if the user has not given a path then, add inputdir. else leave path alone.
                                        if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
                                }
@@ -51,7 +52,7 @@ ClearcutCommand::ClearcutCommand(string option)  {
                                it = parameters.find("phylip");
                                //user has given a template file
                                if(it != parameters.end()){ 
-                                       path = hasPath(it->second);
+                                       path = m->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;           }
                                }
@@ -73,56 +74,53 @@ ClearcutCommand::ClearcutCommand(string option)  {
 
                        
                        //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 = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(inputFile);      }
                        
                        string temp;
                        temp = validParameter.validFile(parameters, "version", false);          if (temp == "not found"){       temp = "F";                     }
-                       version = isTrue(temp);
+                       version = m->isTrue(temp);
                        
                        temp = validParameter.validFile(parameters, "verbose", false);          if (temp == "not found"){       temp = "F";                     }
-                       verbose = isTrue(temp); 
+                       verbose = m->isTrue(temp); 
                        
                        temp = validParameter.validFile(parameters, "quiet", false);            if (temp == "not found"){       temp = "F";                     }
-                       quiet = isTrue(temp); 
+                       quiet = m->isTrue(temp); 
                        
                        seed = validParameter.validFile(parameters, "seed", false);                     if (seed == "not found"){       seed = "*";                     }
                        
                        temp = validParameter.validFile(parameters, "norandom", false);         if (temp == "not found"){       temp = "F";                     }
-                       norandom = isTrue(temp); 
+                       norandom = m->isTrue(temp); 
                        
                        temp = validParameter.validFile(parameters, "shuffle", false);          if (temp == "not found"){       temp = "F";                     }
-                       shuffle = isTrue(temp); 
-                       
-                       temp = validParameter.validFile(parameters, "neighbor", false);         if (temp == "not found"){       temp = "F";                     }
-                       neighbor = isTrue(temp); 
+                       shuffle = m->isTrue(temp); 
                        
-                       temp = validParameter.validFile(parameters, "stdin", false);            if (temp == "not found"){       temp = "F";                     }
-                       stdin = isTrue(temp); 
+                       temp = validParameter.validFile(parameters, "neighbor", false);         if (temp == "not found"){       temp = "T";                     }
+                       neighbor = m->isTrue(temp); 
                        
                        temp = validParameter.validFile(parameters, "DNA", false);                      if (temp == "not found"){       temp = "F";                     }
-                       DNA = isTrue(temp);
+                       DNA = m->isTrue(temp);
                        
                        temp = validParameter.validFile(parameters, "protein", false);          if (temp == "not found"){       temp = "F";                     }
-                       protein = isTrue(temp);
+                       protein = m->isTrue(temp);
                        
                        temp = validParameter.validFile(parameters, "jukes", false);            if (temp == "not found"){       temp = "F";                     }
-                       jukes = isTrue(temp);
+                       jukes = m->isTrue(temp);
                        
                        temp = validParameter.validFile(parameters, "kimura", false);           if (temp == "not found"){       temp = "F";                     }
-                       kimura = isTrue(temp);
+                       kimura = m->isTrue(temp);
                        
                        temp = validParameter.validFile(parameters, "stdout", false);           if (temp == "not found"){       temp = "F";                     }
-                       stdout = isTrue(temp); 
+                       stdoutWanted = m->isTrue(temp); 
                        
                        matrixout = validParameter.validFile(parameters, "matrixout", false);   if (matrixout == "not found"){  matrixout = "";         }
                        
                        ntrees = validParameter.validFile(parameters, "ntrees", false);         if (ntrees == "not found"){     ntrees = "1";           }
                        
                        temp = validParameter.validFile(parameters, "expblen", false);          if (temp == "not found"){       temp = "F";                     }
-                       expblen = isTrue(temp);
+                       expblen = m->isTrue(temp);
                        
                        temp = validParameter.validFile(parameters, "expdist", false);          if (temp == "not found"){       temp = "F";                     }
-                       expdist = isTrue(temp);
+                       expdist = m->isTrue(temp);
                        
                        if ((fastafile != "") && ((!DNA) && (!protein))) { m->mothurOut("You must specify the type of sequences you are using: DNA or protein"); m->mothurOutEndLine(); abort=true; }
                }
@@ -139,8 +137,7 @@ void ClearcutCommand::help(){
        try {
                m->mothurOut("The clearcut command interfaces mothur with the clearcut program written by Initiative for Bioinformatics and Evolutionary Studies (IBEST) at the University of Idaho.\n");
                m->mothurOut("For more information about clearcut refer to http://bioinformatics.hungry.com/clearcut/ \n");
-               m->mothurOut("The clearcut executable must be in a folder called clearcut in the same folder as your mothur executable, similar to mothur's requirements for using blast. \n");
-               m->mothurOut("The clearcut command parameters are phylip, fasta, version, verbose, quiet, seed, norandom, shuffle, neighbor, expblen, expdist, ntrees, matrixout, stdout, kimura, jukes, protein, DNA, stdin. \n");
+               m->mothurOut("The clearcut command parameters are phylip, fasta, version, verbose, quiet, seed, norandom, shuffle, neighbor, expblen, expdist, ntrees, matrixout, stdout, kimura, jukes, protein, DNA. \n");
                m->mothurOut("The phylip parameter allows you to enter your phylip formatted distance matrix. \n");
                m->mothurOut("The fasta parameter allows you to enter your aligned fasta file, if you enter a fastafile you specify if the sequences are DNA or protein using the DNA or protein parameters. \n");
                
@@ -150,9 +147,8 @@ void ClearcutCommand::help(){
                m->mothurOut("The seed parameter allows you to explicitly set the PRNG seed to a specific value. \n");
                m->mothurOut("The norandom parameter allows you to attempt joins deterministically, default=F. \n");
                m->mothurOut("The shuffle parameter allows you to randomly shuffle the distance matrix, default=F. \n");
-               m->mothurOut("The neighbor parameter allows you to use traditional Neighbor-Joining algorithm, default=F. \n");
+               m->mothurOut("The neighbor parameter allows you to use traditional Neighbor-Joining algorithm, default=T. \n");
                
-               m->mothurOut("The stdin parameter reads input from STDIN, default=F. \n");
                m->mothurOut("The DNA parameter allows you to indicate your fasta file contains DNA sequences, default=F. \n");
                m->mothurOut("The protein parameter allows you to indicate your fasta file contains protein sequences, default=F. \n");
                
@@ -165,6 +161,7 @@ void ClearcutCommand::help(){
                m->mothurOut("The clearcut command should be in the following format: \n");
                m->mothurOut("clearcut(phylip=yourDistanceFile) \n");
                m->mothurOut("Example: clearcut(phylip=abrecovery.phylip.dist) \n");    
+               
        }
        catch(exception& e) {
                m->errorOut(e, "ClearcutCommand", "help");
@@ -177,52 +174,86 @@ int ClearcutCommand::execute() {
        try {
                
                if (abort == true) { return 0; }
-                               
+               
                //prepare filename
-               string outputName = outputDir + getRootName(getSimpleName(inputFile)) + "tre";
+               string outputName = outputDir + m->getRootName(m->getSimpleName(inputFile)) + "tre";
                
-               //get location of clearcut
-               GlobalData* globaldata = GlobalData::getInstance();
-               string path = globaldata->argv;
-               path = path.substr(0, (path.find_last_of('m')));
-
-               string clearcutCommand = path + "clearcut/clearcut ";
+               vector<char*> cPara;
                
+               char* tempClearcut = new char[8];  
+               strcpy(tempClearcut, "clearcut"); 
+               cPara.push_back(tempClearcut);
+                               
                //you gave us a distance matrix
-               if (phylipfile != "") { clearcutCommand += "--distance ";       }
+               if (phylipfile != "") {  char* temp = new char[10];  strcpy(temp, "--distance");  cPara.push_back(temp);        }
                
                //you gave us a fastafile
-               if (fastafile != "") { clearcutCommand += "--alignment ";       }
-               
-               if (version)                    {  clearcutCommand += "--version ";             }
-               if (verbose)                    {  clearcutCommand += "--verbose ";             }
-               if (quiet)                              {  clearcutCommand += "--quiet ";               }
-               if (seed != "*")                {  clearcutCommand += "--seed=" + seed + " "; }
-               if (norandom)                   {  clearcutCommand += "--norandom ";    }
-               if (shuffle)                    {  clearcutCommand += "--shuffle ";             }
-               if (neighbor)                   {  clearcutCommand += "--neighbor ";    }
-               
-               if (stdin)                              {  clearcutCommand += "--stdin ";               }
-               else                                    {  clearcutCommand += "--in=" + inputFile + " "; }
-               if (stdout)                             {  clearcutCommand += "--stdout ";              }
-               else                                    {  clearcutCommand += "--out=" + outputName + " "; }
-               
-               if (DNA)                                {  clearcutCommand += "--DNA ";                 }
-               if (protein)                    {  clearcutCommand += "--protein ";             }
-               if (jukes)                              {  clearcutCommand += "--jukes ";               }
-               if (kimura)                             {  clearcutCommand += "--kimura ";              }
-               if (matrixout != "")    {  clearcutCommand += "--matrixout=" + matrixout + " "; }
-               if (ntrees != "1")              {  clearcutCommand += "--ntrees=" + ntrees + " "; }
-               if (expblen)                    {  clearcutCommand += "--expblen ";             }
-               if (expdist)                    {  clearcutCommand += "--expdist ";             }
-               
-               //run clearcut
-               system(clearcutCommand.c_str());
-               
-               if (!stdout) {  
+               if (fastafile != "") { char* temp = new char[11];  strcpy(temp, "--alignment");  cPara.push_back(temp);         }
+               
+               if (version)                    {  char* temp = new char[9];  strcpy(temp, "--version");  cPara.push_back(temp);        }
+               if (verbose)                    {  char* temp = new char[9];  strcpy(temp, "--verbose");  cPara.push_back(temp);        }
+               if (quiet)                              {  char* temp = new char[7];  strcpy(temp, "--quiet");  cPara.push_back(temp);  }
+               if (seed != "*")                {  
+                       string tempSeed = "--seed=" + seed;
+                       char* temp = new char[tempSeed.length()];
+                       strcpy(temp, tempSeed.c_str());
+                       cPara.push_back(temp);
+               }
+               if (norandom)                   {  char* temp = new char[10];  strcpy(temp, "--norandom");  cPara.push_back(temp);      }
+               if (shuffle)                    {  char* temp = new char[9];  strcpy(temp, "--shuffle");  cPara.push_back(temp);        }
+               if (neighbor)                   {  char* temp = new char[10];  strcpy(temp, "--neighbor");  cPara.push_back(temp);      }
+               
+               string tempIn = "--in=" + inputFile;  
+               char* tempI = new char[tempIn.length()];
+               strcpy(tempI, tempIn.c_str());
+               cPara.push_back(tempI);
+               
+               if (stdoutWanted)               {  char* temp = new char[8];  strcpy(temp, "--stdout");  cPara.push_back(temp); }
+               else{  
+                       string tempOut = "--out=" + outputName;  
+                       
+                       char* temp = new char[tempOut.length()];
+                       strcpy(temp, tempOut.c_str());
+                       cPara.push_back(temp);
+               }
+                       
+               if (DNA)                                {  char* temp = new char[5];  strcpy(temp, "--DNA");  cPara.push_back(temp);            }
+               if (protein)                    {  char* temp = new char[9];  strcpy(temp, "--protein");  cPara.push_back(temp);        }
+               if (jukes)                              {  char* temp = new char[7];  strcpy(temp, "--jukes");  cPara.push_back(temp);          }
+               if (kimura)                             { char* temp = new char[8];  strcpy(temp, "--kimura");  cPara.push_back(temp);          }
+               if (matrixout != "")    {  
+                       string tempMatrix =  "--matrixout=" + outputDir + matrixout; 
+                       char* temp = new char[tempMatrix.length()];
+                       strcpy(temp, tempMatrix.c_str());
+                       cPara.push_back(temp);
+               }
+
+               if (ntrees != "1")              {  
+                       string tempNtrees = "--ntrees=" + ntrees; 
+                       char* temp = new char[tempNtrees.length()];
+                       strcpy(temp, tempNtrees.c_str());
+                       cPara.push_back(temp);
+               }
+
+               if (expblen)                    { char* temp = new char[9];  strcpy(temp, "--expblen");  cPara.push_back(temp);         }
+               if (expdist)                    { char* temp = new char[9];  strcpy(temp, "--expdist");  cPara.push_back(temp); }
+               
+               char** clearcutParameters;
+               clearcutParameters = new char*[cPara.size()];
+               for (int i = 0; i < cPara.size(); i++) {  clearcutParameters[i] = cPara[i];  }
+               int numArgs = cPara.size();
+               
+               clearcut_main(numArgs, clearcutParameters); 
+               
+               //free memory
+               for(int i = 0; i < cPara.size(); i++)  {  delete[] cPara[i];  }
+               delete[] clearcutParameters; 
+               
+               if (!stdoutWanted) {    
                        m->mothurOutEndLine();
                        m->mothurOut("Output File Names: "); m->mothurOutEndLine();
-                       m->mothurOut(outputName); m->mothurOutEndLine();        
+                       m->mothurOut(outputName); m->mothurOutEndLine();
+                       if (matrixout != "")    {  m->mothurOut(outputDir+matrixout); m->mothurOutEndLine();  }
                        m->mothurOutEndLine();
                }