]> git.donarmstrong.com Git - mothur.git/blobdiff - getoturepcommand.cpp
added load.logfile command. changed summary.single output for subsample=t.
[mothur.git] / getoturepcommand.cpp
index 9ed887a72ff7354c9396ba2d2c63f3b4a460afd3..4ebde29f912f6e9a02d88dbee301488b52d4a082 100644 (file)
@@ -40,7 +40,7 @@ inline bool compareGroup(repStruct left, repStruct right){
 vector<string> GetOTURepCommand::setParameters(){      
        try {
                CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(plist);
-               CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
+               CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pfasta);
                CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pgroup);
                CommandParameter pphylip("phylip", "InputTypes", "", "", "PhylipColumn", "PhylipColumn", "none",false,false); parameters.push_back(pphylip);
                CommandParameter pname("name", "InputTypes", "", "", "none", "none", "ColumnName",false,false); parameters.push_back(pname);
@@ -68,7 +68,7 @@ vector<string> GetOTURepCommand::setParameters(){
 string GetOTURepCommand::getHelpString(){      
        try {
                string helpString = "";
-               helpString += "The get.oturep command parameters are phylip, column, list, fasta, name, group, large, weighted, cutoff, precision, groups, sorted and label.  The fasta and list parameters are required, as well as phylip or column and name, unless you have valid current files.\n";
+               helpString += "The get.oturep command parameters are phylip, column, list, fasta, name, group, large, weighted, cutoff, precision, groups, sorted and label.  The list parameter is required, as well as phylip or column and name, unless you have valid current files.\n";
                helpString += "The label parameter allows you to select what distance levels you would like a output files created for, and is separated by dashes.\n";
                helpString += "The phylip or column parameter is required, but only one may be used.  If you use a column file the name filename is required. \n";
                helpString += "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";
@@ -95,6 +95,27 @@ string GetOTURepCommand::getHelpString(){
        }
 }
 //**********************************************************************************************************************
+string GetOTURepCommand::getOutputFileNameTag(string type, string inputName=""){       
+       try {
+        string outputFileName = "";
+               map<string, vector<string> >::iterator it;
+        
+        //is this a type this command creates
+        it = outputTypes.find(type);
+        if (it == outputTypes.end()) {  m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
+        else {
+            if (type == "fasta")            {   outputFileName =  "rep.fasta";   }
+            else if (type == "name")        {   outputFileName =  "rep.names";   }
+            else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true;  }
+        }
+        return outputFileName;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "GetOTURepCommand", "getOutputFileNameTag");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
 GetOTURepCommand::GetOTURepCommand(){  
        try {
                abort = true; calledHelp = true; 
@@ -197,11 +218,7 @@ GetOTURepCommand::GetOTURepCommand(string option)  {
                        
                        //check for required parameters
                        fastafile = validParameter.validFile(parameters, "fasta", true);
-                       if (fastafile == "not found") {                                 
-                               fastafile = m->getFastaFile(); 
-                               if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
-                               else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
-                       }
+                       if (fastafile == "not found") { fastafile = ""; }
                        else if (fastafile == "not open") { abort = true; }     
                        else { m->setFastaFile(fastafile); }
                
@@ -232,10 +249,10 @@ GetOTURepCommand::GetOTURepCommand(string option)  {
                        if ((phylipfile == "") && (columnfile == "")) { //is there are current file available for either of these?
                                //give priority to column, then phylip
                                columnfile = m->getColumnFile(); 
-                               if (columnfile != "") {  m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
+                               if (columnfile != "") {  distFile = columnfile; format = "column"; m->mothurOut("Using " + columnfile + " as input file for the column parameter."); m->mothurOutEndLine(); }
                                else { 
                                        phylipfile = m->getPhylipFile(); 
-                                       if (phylipfile != "") {  m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
+                                       if (phylipfile != "") {  distFile = phylipfile; format = "phylip"; m->mothurOut("Using " + phylipfile + " as input file for the phylip parameter."); m->mothurOutEndLine(); }
                                        else { 
                                                m->mothurOut("No valid current files. You must provide a phylip or column file before you can use the get.oturep command."); m->mothurOutEndLine(); 
                                                abort = true;
@@ -290,7 +307,7 @@ GetOTURepCommand::GetOTURepCommand(string option)  {
                                        m->splitAtDash(groups, Groups);
                                }
                        }
-                       m->Groups = Groups;
+                       m->setGroups(Groups);
                        
                        string temp = validParameter.validFile(parameters, "large", false);             if (temp == "not found") {      temp = "F";     }
                        large = m->isTrue(temp);
@@ -301,10 +318,10 @@ GetOTURepCommand::GetOTURepCommand(string option)  {
                        if ((weighted) && (namefile == "")) { m->mothurOut("You cannot set weighted to true unless you provide a namesfile."); m->mothurOutEndLine(); abort = true; }
                        
                        temp = validParameter.validFile(parameters, "precision", false);                        if (temp == "not found") { temp = "100"; }
-                       convert(temp, precision); 
+                       m->mothurConvert(temp, precision); 
                        
                        temp = validParameter.validFile(parameters, "cutoff", false);                   if (temp == "not found") { temp = "10.0"; }
-                       convert(temp, cutoff); 
+                       m->mothurConvert(temp, cutoff); 
                        cutoff += (5 / (precision * 10.0));
                }
        }
@@ -429,7 +446,9 @@ int GetOTURepCommand::execute(){
                        
                        if (Groups.size() != 0) {
                                SharedUtil* util = new SharedUtil();
-                               util->setGroups(Groups, groupMap->namesOfGroups, "getoturep");
+                               vector<string> gNamesOfGroups = groupMap->getNamesOfGroups();
+                               util->setGroups(Groups, gNamesOfGroups, "getoturep");
+                               groupMap->setNamesOfGroups(gNamesOfGroups);
                                delete util;
                        }
                }
@@ -535,21 +554,30 @@ int GetOTURepCommand::execute(){
                
                if (!weighted) { nameFileMap.clear(); }
                
-               //read fastafile
-               fasta = new FastaMap();
-               fasta->readFastaFile(fastafile);
-               
-               //if user gave a namesfile then use it
-               if (namefile != "") {   readNamesFile();        }
-               
-               //output create and output the .rep.fasta files
-               map<string, string>::iterator itNameFile;
-               for (itNameFile = outputNameFiles.begin(); itNameFile != outputNameFiles.end(); itNameFile++) {
-                       processNames(itNameFile->first, itNameFile->second);
+                               
+               if (fastafile != "") {
+                       //read fastafile
+                       fasta = new FastaMap();
+                       fasta->readFastaFile(fastafile);
+                       
+                       //if user gave a namesfile then use it
+                       if (namefile != "") {   readNamesFile();        }
+                       
+                       //output create and output the .rep.fasta files
+                       map<string, string>::iterator itNameFile;
+                       for (itNameFile = outputNameFiles.begin(); itNameFile != outputNameFiles.end(); itNameFile++) {
+                               processFastaNames(itNameFile->first, itNameFile->second);
+                       }
+               }else {
+                       //output create and output the .rep.fasta files
+                       map<string, string>::iterator itNameFile;
+                       for (itNameFile = outputNameFiles.begin(); itNameFile != outputNameFiles.end(); itNameFile++) {
+                               processNames(itNameFile->first, itNameFile->second);
+                       }
                }
                
-               delete fasta;
-               if (groupfile != "") { delete groupMap;  }
+                               
+               if (groupfile != "") { delete groupMap; }
                
                if (m->control_pressed) {  return 0; }
                
@@ -764,7 +792,7 @@ int GetOTURepCommand::process(ListVector* processList) {
                map<string, ofstream*> filehandles;
                
                if (Groups.size() == 0) { //you don't want to use groups
-                       outputNamesFile  = outputDir + m->getRootName(m->getSimpleName(listfile)) + processList->getLabel() + ".rep.names";
+                       outputNamesFile  = outputDir + m->getRootName(m->getSimpleName(listfile)) + processList->getLabel() + "." + getOutputFileNameTag("name");
                        m->openOutputFile(outputNamesFile, newNamesOutput);
                        outputNames.push_back(outputNamesFile); outputTypes["name"].push_back(outputNamesFile); 
                        outputNameFiles[outputNamesFile] = processList->getLabel();
@@ -773,7 +801,7 @@ int GetOTURepCommand::process(ListVector* processList) {
                        for (int i=0; i<Groups.size(); i++) {
                                temp = new ofstream;
                                filehandles[Groups[i]] = temp;
-                               outputNamesFile = outputDir + m->getRootName(m->getSimpleName(listfile)) + processList->getLabel() + "." + Groups[i] + ".rep.names";
+                               outputNamesFile = outputDir + m->getRootName(m->getSimpleName(listfile)) + processList->getLabel() + "." + Groups[i] + "." + getOutputFileNameTag("name");
                                
                                m->openOutputFile(outputNamesFile, *(temp));
                                outputNames.push_back(outputNamesFile); outputTypes["name"].push_back(outputNamesFile);
@@ -856,12 +884,12 @@ int GetOTURepCommand::process(ListVector* processList) {
        }
 }
 //**********************************************************************************************************************
-int GetOTURepCommand::processNames(string filename, string label) {
+int GetOTURepCommand::processFastaNames(string filename, string label) {
        try{
 
                //create output file
                if (outputDir == "") { outputDir += m->hasPath(listfile); }
-               string outputFileName = outputDir + m->getRootName(m->getSimpleName(listfile)) + label + ".rep.fasta";
+               string outputFileName = outputDir + m->getRootName(m->getSimpleName(listfile)) + label + "." + getOutputFileNameTag("fasta");
                m->openOutputFile(outputFileName, out);
                vector<repStruct> reps;
                outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
@@ -960,6 +988,40 @@ int GetOTURepCommand::processNames(string filename, string label) {
                return 0;
 
        }
+       catch(exception& e) {
+               m->errorOut(e, "GetOTURepCommand", "processFastaNames");
+               exit(1);
+       }
+}
+//**********************************************************************************************************************
+int GetOTURepCommand::processNames(string filename, string label) {
+       try{
+               
+               //create output file
+               if (outputDir == "") { outputDir += m->hasPath(listfile); }
+               
+               ofstream out2;
+               string tempNameFile = filename + ".temp";
+               m->openOutputFile(tempNameFile, out2);
+               
+               ifstream in;
+               m->openInputFile(filename, in);
+               
+               int i = 0;
+               string rep, binnames;
+               while (!in.eof()) {
+                       if (m->control_pressed) { break; }
+                       in >> i >> rep >> binnames; m->gobble(in);
+                       out2 << rep << '\t' << binnames << endl;
+               }
+               in.close();
+               out2.close();
+               
+               m->mothurRemove(filename);
+               rename(tempNameFile.c_str(), filename.c_str());
+               
+               return 0;
+       }
        catch(exception& e) {
                m->errorOut(e, "GetOTURepCommand", "processNames");
                exit(1);