From: westcott Date: Wed, 28 Apr 2010 11:09:48 +0000 (+0000) Subject: added set.logfile command X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=commitdiff_plain;h=84f93a9e670eb7f7e69e0468b9be4c53890c07f2 added set.logfile command --- diff --git a/Mothur.xcodeproj/project.pbxproj b/Mothur.xcodeproj/project.pbxproj index cc3b4b6..3ee1d81 100644 --- a/Mothur.xcodeproj/project.pbxproj +++ b/Mothur.xcodeproj/project.pbxproj @@ -14,6 +14,8 @@ A7639F8D1175DF35008F5578 /* makefile */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.make; path = makefile; sourceTree = ""; }; A76AAD02117F322B003D8DA1 /* phylosummary.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = phylosummary.h; sourceTree = ""; }; A76AAD03117F322B003D8DA1 /* phylosummary.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = phylosummary.cpp; sourceTree = ""; }; + A76C4A1011876BAF0009460B /* setlogfilecommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = setlogfilecommand.h; sourceTree = SOURCE_ROOT; }; + A76C4A1111876BAF0009460B /* setlogfilecommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = setlogfilecommand.cpp; sourceTree = SOURCE_ROOT; }; A78254461164D7790002E2DD /* chimerapintailcommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = chimerapintailcommand.h; sourceTree = ""; }; A78254471164D7790002E2DD /* chimerapintailcommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = chimerapintailcommand.cpp; sourceTree = ""; }; A7825502116519F70002E2DD /* chimerabellerophoncommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = chimerabellerophoncommand.h; sourceTree = ""; }; @@ -734,6 +736,8 @@ A7DA20FD113FECD400BF472F /* seqsummarycommand.h */, A7DA2102113FECD400BF472F /* setdircommand.cpp */, A7DA2103113FECD400BF472F /* setdircommand.h */, + A76C4A1011876BAF0009460B /* setlogfilecommand.h */, + A76C4A1111876BAF0009460B /* setlogfilecommand.cpp */, A7DA210F113FECD400BF472F /* sharedcommand.cpp */, A7DA2110113FECD400BF472F /* sharedcommand.h */, A7DA2154113FECD400BF472F /* summarycommand.cpp */, diff --git a/commandfactory.cpp b/commandfactory.cpp index aaeec67..cad0442 100644 --- a/commandfactory.cpp +++ b/commandfactory.cpp @@ -71,6 +71,7 @@ #include "chimeraslayercommand.h" #include "chimerapintailcommand.h" #include "chimerabellerophoncommand.h" +#include "setlogfilecommand.h" /*******************************************************/ @@ -97,6 +98,8 @@ CommandFactory::CommandFactory(){ command = new NoCommand(s); outputDir = ""; inputDir = ""; + logFileName = ""; + append = false; //initialize list of valid commands commands["read.dist"] = "read.dist"; @@ -149,6 +152,7 @@ CommandFactory::CommandFactory(){ commands["merge.files"] = "merge.files"; commands["parse.list"] = "parse.list"; commands["parse.sff"] = "parse.sff"; + commands["set.logfile"] = "set.logfile"; commands["classify.seqs"] = "MPIEnabled"; commands["dist.seqs"] = "MPIEnabled"; commands["filter.seqs"] = "MPIEnabled"; @@ -260,6 +264,7 @@ Command* CommandFactory::getCommand(string commandName, string optionString){ else if(commandName == "pcoa") { command = new PCACommand(optionString); } else if(commandName == "otu.hierarchy") { command = new OtuHierarchyCommand(optionString); } else if(commandName == "set.dir") { command = new SetDirectoryCommand(optionString); } + else if(commandName == "set.logfile") { command = new SetLogFileCommand(optionString); } else if(commandName == "parse.list") { command = new ParseListCommand(optionString); } else if(commandName == "parse.sff") { command = new ParseSFFCommand(optionString); } else { command = new NoCommand(optionString); } diff --git a/commandfactory.hpp b/commandfactory.hpp index 3b6a17e..44e78fd 100644 --- a/commandfactory.hpp +++ b/commandfactory.hpp @@ -22,9 +22,12 @@ public: Command* getCommand(); bool isValidCommand(string); void printCommands(ostream&); - void setOutputDirectory(string o) { outputDir = o; } - void setInputDirectory(string i) { inputDir = i; } - string getOutputDir() { return outputDir; } + void setOutputDirectory(string o) { outputDir = o; } + void setInputDirectory(string i) { inputDir = i; } + void setLogfileName(string n, bool a) { logFileName = n; append = a; } + string getLogfileName() { return logFileName; } + bool getAppend() { return append; } + string getOutputDir() { return outputDir; } bool MPIEnabled(string); private: @@ -32,7 +35,8 @@ private: MothurOut* m; map commands; map::iterator it; - string outputDir, inputDir; + string outputDir, inputDir, logFileName; + bool append; static CommandFactory* _uniqueInstance; CommandFactory( const CommandFactory& ); // Disable copy constructor diff --git a/engine.hpp b/engine.hpp index 74687d2..0020a7f 100644 --- a/engine.hpp +++ b/engine.hpp @@ -27,7 +27,10 @@ public: virtual ~Engine(){} virtual bool getInput() = 0; virtual string getCommand(); - virtual string getOutputDir() { return cFactory->getOutputDir(); } + virtual string getOutputDir() { return cFactory->getOutputDir(); } + virtual string getLogFileName() { return cFactory->getLogfileName(); } + virtual bool getAppend() { return cFactory->getAppend(); } + vector getOptions() { return options; } protected: vector options; diff --git a/makefile b/makefile index d4aac7f..181b897 100644 --- a/makefile +++ b/makefile @@ -239,7 +239,8 @@ mothur : \ ./qstat.o\ ./shen.o\ ./logsd.o\ - ./geom.o + ./geom.o\ + ./setlogfilecommand.o $(CC) $(LNK_OPTIONS) \ ./sharedutilities.o\ ./treegroupscommand.o\ @@ -437,7 +438,8 @@ mothur : \ ./qstat.o\ ./shen.o\ ./logsd.o\ - ./geom.o\ + ./geom.o\ + ./setlogfilecommand.o\ -o ../Release/mothur clean : @@ -638,7 +640,8 @@ clean : ./qstat.o\ ./shen.o\ ./logsd.o\ - ./geom.o\ + ./geom.o\ + ./setlogfilecommand.o\ mothur install : mothur @@ -1623,9 +1626,13 @@ install : mothur ./chimerabellerophoncommand.o : chimerabellerophoncommand.cpp $(CC) $(CC_OPTIONS) chimerabellerophoncommand.cpp -c $(INCLUDE) -o ./chimerabellerophoncommand.o -# Item # 171 -- phylosummary -- +# Item # 197 -- phylosummary -- ./phylosummary.o : phylosummary.cpp $(CC) $(CC_OPTIONS) phylosummary.cpp -c $(INCLUDE) -o ./phylosummary.o + +# Item # 198 -- setlogfilecommand -- +./setlogfilecommand.o : setlogfilecommand.cpp + $(CC) $(CC_OPTIONS) setlogfilecommand.cpp -c $(INCLUDE) -o ./setlogfilecommand.o diff --git a/mothur.cpp b/mothur.cpp index 2d35c69..4f91c2e 100644 --- a/mothur.cpp +++ b/mothur.cpp @@ -1,171 +1,193 @@ -/* - * interface.cpp - * - * - * Created by Pat Schloss on 8/14/08. - * Copyright 2008 Patrick D. Schloss. All rights reserved. - * - */ - -#include "mothur.h" -#include "engine.hpp" -#include "globaldata.hpp" -#include "mothurout.h" - - -/**************************************************************************************************/ - -GlobalData* GlobalData::_uniqueInstance = 0; -CommandFactory* CommandFactory::_uniqueInstance = 0; -MothurOut* MothurOut::_uniqueInstance = 0; - -/***********************************************************************/ -volatile int ctrlc_pressed = 0; -void ctrlc_handler ( int sig ) { - MothurOut* m = MothurOut::getInstance(); - ctrlc_pressed = 1; - m->control_pressed = ctrlc_pressed; - - if (m->executing) { //if mid command quit execution, else quit mothur - m->mothurOutEndLine(); m->mothurOut("quitting command..."); m->mothurOutEndLine(); - }else{ - m->mothurOut("quitting mothur"); m->mothurOutEndLine(); - exit(1); - } -} -/***********************************************************************/ -int main(int argc, char *argv[]){ - MothurOut* m = MothurOut::getInstance(); - try { - - signal(SIGINT, ctrlc_handler ); - - time_t ltime = time(NULL); /* calendar time */ - string logFileName = "mothur." + toString(ltime) + ".logfile"; - - #ifdef USE_MPI - MPI_Init(&argc, &argv); - #endif - - m->setFileName(logFileName); - - - //version - #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) - system("clear"); - #if defined (__APPLE__) || (__MACH__) - m->mothurOutJustToLog("Mac version"); - m->mothurOutEndLine(); m->mothurOutEndLine(); - #else - m->mothurOutJustToLog("Linux version"); - >m->mothurOutEndLine(); m->mothurOutEndLine(); - #endif - - #else - system("CLS"); - m->mothurOutJustToLog("Windows version"); - m->mothurOutEndLine(); m->mothurOutEndLine(); - #endif - - #ifdef USE_READLINE - m->mothurOutJustToLog("Using ReadLine"); - m->mothurOutEndLine(); m->mothurOutEndLine(); - #endif - - //header - m->mothurOut("mothur v.1.9"); - m->mothurOutEndLine(); - m->mothurOut("Last updated: 4/16/2010"); - m->mothurOutEndLine(); - m->mothurOutEndLine(); - m->mothurOut("by"); - m->mothurOutEndLine(); - m->mothurOut("Patrick D. Schloss"); - m->mothurOutEndLine(); - m->mothurOutEndLine(); - m->mothurOut("Department of Microbiology & Immunology"); - m->mothurOutEndLine(); - m->mothurOut("University of Michigan"); - m->mothurOutEndLine(); - m->mothurOut("pschloss@umich.edu"); - m->mothurOutEndLine(); - m->mothurOut("http://www.mothur.org"); - m->mothurOutEndLine(); - m->mothurOutEndLine(); - m->mothurOut("When using, please cite:"); - m->mothurOutEndLine(); - m->mothurOut("Schloss, P.D., et al., Introducing mothur: Open-source, platform-independent, community-supported software for describing and comparing microbial communities. Appl Environ Microbiol, 2009. 75(23):7537-41."); - m->mothurOutEndLine(); - m->mothurOutEndLine(); - m->mothurOut("Distributed under the GNU General Public License"); - m->mothurOutEndLine(); - m->mothurOutEndLine(); - m->mothurOut("Type 'help()' for information on the commands that are available"); - m->mothurOutEndLine(); - m->mothurOutEndLine(); - m->mothurOut("Type 'quit()' to exit program"); - m->mothurOutEndLine(); - - #ifdef USE_MPI - m->mothurOutJustToLog("Using MPI\tversion "); - int version, subversion; - MPI_Get_version(&version, &subversion); - m->mothurOutJustToLog(toString(version) + "." + toString(subversion) + "\n"); - #endif - - //srand(54321); - srand( (unsigned)time( NULL ) ); - - Engine* mothur; - bool bail = 0; - string input; - - if(argc>1){ - input = argv[1]; - - if (input[0] == '#') { - m->mothurOutJustToLog("Script Mode"); - m->mothurOutEndLine(); m->mothurOutEndLine(); - - mothur = new ScriptEngine(argv[0], argv[1]); - }else{ - m->mothurOutJustToLog("Batch Mode"); - m->mothurOutEndLine(); m->mothurOutEndLine(); - - mothur = new BatchEngine(argv[0], argv[1]); - } - } - else{ - m->mothurOutJustToLog("Interactive Mode"); - m->mothurOutEndLine(); m->mothurOutEndLine(); - - mothur = new InteractEngine(argv[0]); - } - - while(bail == 0) { bail = mothur->getInput(); } - - string outputDir = mothur->getOutputDir(); - string newlogFileName = outputDir + logFileName; - - //closes logfile so we can rename - m->closeLog(); - - //need this because m->mothurOut makes the logfile, but doesn't know where to put it - rename(logFileName.c_str(), newlogFileName.c_str()); //logfile with timestamp - - delete mothur; - - #ifdef USE_MPI - MPI_Finalize(); - #endif - - return 0; - } - catch(exception& e) { - m->errorOut(e, "mothur", "main"); - exit(1); - } -} - -/**************************************************************************************************/ - +/* + * interface.cpp + * + * + * Created by Pat Schloss on 8/14/08. + * Copyright 2008 Patrick D. Schloss. All rights reserved. + * + */ + +#include "mothur.h" +#include "engine.hpp" +#include "globaldata.hpp" +#include "mothurout.h" + + +/**************************************************************************************************/ + +GlobalData* GlobalData::_uniqueInstance = 0; +CommandFactory* CommandFactory::_uniqueInstance = 0; +MothurOut* MothurOut::_uniqueInstance = 0; + +/***********************************************************************/ +volatile int ctrlc_pressed = 0; +void ctrlc_handler ( int sig ) { + MothurOut* m = MothurOut::getInstance(); + ctrlc_pressed = 1; + m->control_pressed = ctrlc_pressed; + + if (m->executing) { //if mid command quit execution, else quit mothur + m->mothurOutEndLine(); m->mothurOut("quitting command..."); m->mothurOutEndLine(); + }else{ + m->mothurOut("quitting mothur"); m->mothurOutEndLine(); + exit(1); + } +} +/***********************************************************************/ +int main(int argc, char *argv[]){ + MothurOut* m = MothurOut::getInstance(); + try { + + signal(SIGINT, ctrlc_handler ); + + time_t ltime = time(NULL); /* calendar time */ + string logFileName = "mothur." + toString(ltime) + ".logfile"; + + #ifdef USE_MPI + MPI_Init(&argc, &argv); + #endif + + m->setFileName(logFileName); + + + //version + #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) + system("clear"); + #if defined (__APPLE__) || (__MACH__) + m->mothurOutJustToLog("Mac version"); + m->mothurOutEndLine(); m->mothurOutEndLine(); + #else + m->mothurOutJustToLog("Linux version"); + >m->mothurOutEndLine(); m->mothurOutEndLine(); + #endif + + #else + system("CLS"); + m->mothurOutJustToLog("Windows version"); + m->mothurOutEndLine(); m->mothurOutEndLine(); + #endif + + #ifdef USE_READLINE + m->mothurOutJustToLog("Using ReadLine"); + m->mothurOutEndLine(); m->mothurOutEndLine(); + #endif + + //header + m->mothurOut("mothur v.1.9"); + m->mothurOutEndLine(); + m->mothurOut("Last updated: 4/16/2010"); + m->mothurOutEndLine(); + m->mothurOutEndLine(); + m->mothurOut("by"); + m->mothurOutEndLine(); + m->mothurOut("Patrick D. Schloss"); + m->mothurOutEndLine(); + m->mothurOutEndLine(); + m->mothurOut("Department of Microbiology & Immunology"); + m->mothurOutEndLine(); + m->mothurOut("University of Michigan"); + m->mothurOutEndLine(); + m->mothurOut("pschloss@umich.edu"); + m->mothurOutEndLine(); + m->mothurOut("http://www.mothur.org"); + m->mothurOutEndLine(); + m->mothurOutEndLine(); + m->mothurOut("When using, please cite:"); + m->mothurOutEndLine(); + m->mothurOut("Schloss, P.D., et al., Introducing mothur: Open-source, platform-independent, community-supported software for describing and comparing microbial communities. Appl Environ Microbiol, 2009. 75(23):7537-41."); + m->mothurOutEndLine(); + m->mothurOutEndLine(); + m->mothurOut("Distributed under the GNU General Public License"); + m->mothurOutEndLine(); + m->mothurOutEndLine(); + m->mothurOut("Type 'help()' for information on the commands that are available"); + m->mothurOutEndLine(); + m->mothurOutEndLine(); + m->mothurOut("Type 'quit()' to exit program"); + m->mothurOutEndLine(); + + #ifdef USE_MPI + m->mothurOutJustToLog("Using MPI\tversion "); + int version, subversion; + MPI_Get_version(&version, &subversion); + m->mothurOutJustToLog(toString(version) + "." + toString(subversion) + "\n"); + #endif + + //srand(54321); + srand( (unsigned)time( NULL ) ); + + Engine* mothur; + bool bail = 0; + string input; + + if(argc>1){ + input = argv[1]; + + if (input[0] == '#') { + m->mothurOutJustToLog("Script Mode"); + m->mothurOutEndLine(); m->mothurOutEndLine(); + + mothur = new ScriptEngine(argv[0], argv[1]); + }else{ + m->mothurOutJustToLog("Batch Mode"); + m->mothurOutEndLine(); m->mothurOutEndLine(); + + mothur = new BatchEngine(argv[0], argv[1]); + } + } + else{ + m->mothurOutJustToLog("Interactive Mode"); + m->mothurOutEndLine(); m->mothurOutEndLine(); + + mothur = new InteractEngine(argv[0]); + } + + while(bail == 0) { bail = mothur->getInput(); } + + //closes logfile so we can rename + m->closeLog(); + + string outputDir = mothur->getOutputDir(); + string tempLog = mothur->getLogFileName(); + bool append = mothur->getAppend(); + + string newlogFileName; + if (tempLog != "") { + newlogFileName = outputDir + tempLog; + + if (!append) { + //need this because m->mothurOut makes the logfile, but doesn't know where to put it + rename(logFileName.c_str(), newlogFileName.c_str()); //logfile with timestamp + + }else { + ofstream outNewLog; + openOutputFileAppend(newlogFileName, outNewLog); + outNewLog << endl << endl << "*********************************************************************************" << endl << endl; + outNewLog.close(); + + appendFiles(logFileName, newlogFileName); + remove(logFileName.c_str()); + } + }else{ + newlogFileName = outputDir + logFileName; + //need this because m->mothurOut makes the logfile, but doesn't know where to put it + rename(logFileName.c_str(), newlogFileName.c_str()); //logfile with timestamp + } + + + delete mothur; + + #ifdef USE_MPI + MPI_Finalize(); + #endif + + return 0; + } + catch(exception& e) { + m->errorOut(e, "mothur", "main"); + exit(1); + } +} + +/**************************************************************************************************/ + diff --git a/setlogfilecommand.cpp b/setlogfilecommand.cpp new file mode 100644 index 0000000..593664e --- /dev/null +++ b/setlogfilecommand.cpp @@ -0,0 +1,84 @@ +/* + * setlogfilecommand.cpp + * Mothur + * + * Created by westcott on 4/27/10. + * Copyright 2010 Schloss Lab. All rights reserved. + * + */ + +#include "setlogfilecommand.h" + +//********************************************************************************************************************** + +SetLogFileCommand::SetLogFileCommand(string option) { + try { + abort = false; + + //allow user to run help + if(option == "help") { help(); abort = true; } + + else { + //valid paramters for this command + string Array[] = {"name","append","outputdir","inputdir"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + + OptionParser parser(option); + map parameters = parser.getParameters(); + + ValidParameters validParameter; + //check to make sure all parameters are valid for command + for (map::iterator it = parameters.begin(); it != parameters.end(); it++) { + if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } + } + + name = validParameter.validFile(parameters, "name", false); + if (name == "not found") { m->mothurOut("name is a required parameter for the set.logfile command."); abort = true; } + + string temp = validParameter.validFile(parameters, "append", false); if (temp == "not found") { temp = "F"; } + append = isTrue(temp); + } + } + catch(exception& e) { + m->errorOut(e, "SetLogFileCommand", "SetLogFileCommand"); + exit(1); + } +} +//********************************************************************************************************************** + +void SetLogFileCommand::help(){ + try { + m->mothurOut("The set.logfile command can be used to provide a specific name for your logfile and/or to append the log generated by mothur to an existing file.\n"); + m->mothurOut("The set.logfile command parameters are name and append, name is required. Append is set to false by default.\n"); + m->mothurOut("The set.logfile command should be in the following format: set.logfile(name=yourLogFileName, append=T).\n"); + m->mothurOut("Example set.logfile(name=/Users/lab/desktop/output.txt, append=T).\n"); + m->mothurOut("Note: No spaces between parameter labels (i.e. name), '=' and parameters (i.e.yourLogFileName).\n\n"); + } + catch(exception& e) { + m->errorOut(e, "SetLogFileCommand", "help"); + exit(1); + } +} +//********************************************************************************************************************** + +SetLogFileCommand::~SetLogFileCommand(){} + +//********************************************************************************************************************** + +int SetLogFileCommand::execute(){ + try { + + if (abort == true) { return 0; } + + commandFactory = CommandFactory::getInstance(); + + commandFactory->setLogfileName(name, append); + + return 0; + } + catch(exception& e) { + m->errorOut(e, "SetLogFileCommand", "execute"); + exit(1); + } +} +//**********************************************************************************************************************/ diff --git a/setlogfilecommand.h b/setlogfilecommand.h new file mode 100644 index 0000000..d16bcd6 --- /dev/null +++ b/setlogfilecommand.h @@ -0,0 +1,37 @@ +#ifndef SETLOGFILECOMMAND_H +#define SETLOGFILECOMMAND_H + +/* + * setlogfilecommand.h + * Mothur + * + * Created by westcott on 4/27/10. + * Copyright 2010 Schloss Lab. All rights reserved. + * + */ + +#include "command.hpp" +#include "commandfactory.hpp" + +/**********************************************************/ + +class SetLogFileCommand : public Command { + +public: + SetLogFileCommand(string); + ~SetLogFileCommand(); + int execute(); + void help(); + +private: + CommandFactory* commandFactory; + string name; + bool abort, append; + +}; + +/**********************************************************/ + +#endif + +