From 791f42d21a85f794529fd4c912dcc27d873c25e8 Mon Sep 17 00:00:00 2001 From: Sarah Westcott Date: Tue, 29 May 2012 12:48:28 -0400 Subject: [PATCH] added code to check path for uchime and catchall executables --- catchallcommand.cpp | 32 +++++++++++++--- chimerauchimecommand.cpp | 37 +++++++++++++------ chimerauchimecommand.h | 41 +++++++++------------ engine.cpp | 79 ++-------------------------------------- engine.hpp | 1 - mothurout.cpp | 75 ++++++++++++++++++++++++++++++++++++++ mothurout.h | 1 + 7 files changed, 149 insertions(+), 117 deletions(-) diff --git a/catchallcommand.cpp b/catchallcommand.cpp index cc99e6d..329b56a 100644 --- a/catchallcommand.cpp +++ b/catchallcommand.cpp @@ -175,16 +175,14 @@ int CatchAllCommand::execute() { path = m->getFullPathName(path); if (m->debug) { m->mothurOut("[DEBUG]: mothur's path = " + path + "\n"); } - + savedOutputDir = outputDir; string catchAllCommandExe = ""; string catchAllTest = ""; #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) - catchAllCommandExe += "mono " + path + "CatchAllcmdL.exe "; if (outputDir == "") { outputDir = "./"; } //force full pathname to be created for catchall, this is necessary because if catchall is in the path it will look for input file whereever the exe is and not the cwd. catchAllTest = path + "CatchAllcmdL.exe"; #else - catchAllCommandExe += "\"" + path + "CatchAllcmdW.exe\"" + " "; if (outputDir == "") { outputDir = ".\\"; } //force full pathname to be created for catchall, this is necessary because if catchall is in the path it will look for input file whereever the exe is and not the cwd. catchAllTest = path + "CatchAllcmdW.exe"; #endif @@ -193,12 +191,34 @@ int CatchAllCommand::execute() { ifstream in; catchAllTest = m->getFullPathName(catchAllTest); int ableToOpen = m->openInputFile(catchAllTest, in, "no error"); in.close(); - if(ableToOpen == 1) { m->mothurOut("[ERROR]: " + catchAllTest + " file does not exist. mothur requires the catchall executable to run the catchall command."); m->mothurOutEndLine(); m->control_pressed = true; return 0; } + if(ableToOpen == 1) { + m->mothurOut(catchAllTest + " file does not exist. Checking path... \n"); + //check to see if uchime is in the path?? + + string programName = "CatchAllcmdW.exe"; +#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) + programName = "CatchAllcmdL.exe"; +#endif + string cLocation = m->findProgramPath(programName); + + ifstream in2; + ableToOpen = m->openInputFile(cLocation, in2, "no error"); in2.close(); + + if(ableToOpen == 1) { m->mothurOut("[ERROR]: " + cLocation + " file does not exist. mothur requires the catchall executable."); m->mothurOutEndLine(); return 0; } + else { m->mothurOut("Found catchall in your path, using " + cLocation + "\n"); catchAllTest = cLocation; } + } + catchAllTest = m->getFullPathName(catchAllTest); + +#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) + catchAllCommandExe += "mono " + catchAllTest + " "; +#else + catchAllCommandExe += "\"" + catchAllTest + "\" "; +#endif //prepare full output directory outputDir = m->getFullPathName(outputDir); - if (m->debug) { m->mothurOut("[DEBUG]: catchall location = " + catchAllCommandExe + ".\n [DEBUG]: outputDir = " + outputDir + ".\n"); } + if (m->debug) { m->mothurOut("[DEBUG]: catchall location = " + catchAllCommandExe + "\n[DEBUG]: outputDir = " + outputDir + "\n"); } vector inputFileNames; if (sharedfile != "") { inputFileNames = parseSharedFile(sharedfile); } @@ -219,7 +239,7 @@ int CatchAllCommand::execute() { string summaryfilename = outputDir + m->getRootName(m->getSimpleName(inputFileNames[p])) + "catchall.summary"; summaryfilename = m->getFullPathName(summaryfilename); - if (m->debug) { m->mothurOut("[DEBUG]: Input File = " + inputFileNames[p] + ".\n [DEBUG]: inputdata address = " + toString(&input) + ".\n [DEBUG]: sabund address = " + toString(&sabund) + ".\n"); } + if (m->debug) { m->mothurOut("[DEBUG]: Input File = " + inputFileNames[p] + ".\n[DEBUG]: inputdata address = " + toString(&input) + ".\n[DEBUG]: sabund address = " + toString(&sabund) + ".\n"); } ofstream out; m->openOutputFile(summaryfilename, out); diff --git a/chimerauchimecommand.cpp b/chimerauchimecommand.cpp index e28775f..be0421a 100644 --- a/chimerauchimecommand.cpp +++ b/chimerauchimecommand.cpp @@ -481,8 +481,26 @@ ChimeraUchimeCommand::ChimeraUchimeCommand(string option) { ifstream in; uchimeCommand = m->getFullPathName(uchimeCommand); int ableToOpen = m->openInputFile(uchimeCommand, in, "no error"); in.close(); - if(ableToOpen == 1) { m->mothurOut("[ERROR]: " + uchimeCommand + " file does not exist. mothur requires the uchime executable."); m->mothurOutEndLine(); abort = true; } - } + if(ableToOpen == 1) { + m->mothurOut(uchimeCommand + " file does not exist. Checking path... \n"); + //check to see if uchime is in the path?? + + string uLocation = m->findProgramPath("uchime"); + + + ifstream in2; +#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) + ableToOpen = m->openInputFile(uLocation, in2, "no error"); in2.close(); +#else + ableToOpen = m->openInputFile((uLocation + ".exe"), in2, "no error"); in2.close(); +#endif + + if(ableToOpen == 1) { m->mothurOut("[ERROR]: " + uLocation + " file does not exist. mothur requires the uchime executable."); m->mothurOutEndLine(); abort = true; } + else { m->mothurOut("Found uchime in your path, using " + uLocation + "\n");uchimeLocation = uLocation; } + }else { uchimeLocation = uchimeCommand; } + + uchimeLocation = m->getFullPathName(uchimeLocation); + } } catch(exception& e) { m->errorOut(e, "ChimeraSlayerCommand", "ChimeraSlayerCommand"); @@ -1010,16 +1028,10 @@ int ChimeraUchimeCommand::driver(string outputFName, string filename, string acc vector cPara; - string path = m->argv; - string tempPath = path; - for (int i = 0; i < path.length(); i++) { tempPath[i] = tolower(path[i]); } - path = path.substr(0, (tempPath.find_last_of('m'))); - - string uchimeCommand = path; + string uchimeCommand = uchimeLocation; #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) - uchimeCommand += "uchime "; + uchimeCommand += " "; #else - uchimeCommand += "uchime"; uchimeCommand = "\"" + uchimeCommand + "\""; #endif @@ -1258,6 +1270,7 @@ int ChimeraUchimeCommand::driver(string outputFName, string filename, string acc #else commandString = "\"" + commandString + "\""; #endif + if (m->debug) { m->mothurOut("[DEBUG]: uchime command = " + commandString + ".\n"); } system(commandString.c_str()); //free memory @@ -1430,7 +1443,7 @@ int ChimeraUchimeCommand::createProcesses(string outputFileName, string filename // Allocate memory for thread data. string extension = toString(i) + ".temp"; - uchimeData* tempUchime = new uchimeData(outputFileName+extension, templatefile, files[i], "", "", "", accnos+extension, alns+extension, dummy, m, 0, 0, i); + uchimeData* tempUchime = new uchimeData(outputFileName+extension, uchimeLocation, templatefile, files[i], "", "", "", accnos+extension, alns+extension, dummy, m, 0, 0, i); tempUchime->setBooleans(useAbskew, chimealns, useMinH, useMindiv, useXn, useDn, useXa, useChunks, useMinchunk, useIdsmoothwindow, useMinsmoothid, useMaxp, skipgaps, skipgaps2, useMinlen, useMaxlen, ucl, useQueryfract); tempUchime->setVariables(abskew, minh, mindiv, xn, dn, xa, chunks, minchunk, idsmoothwindow, minsmoothid, maxp, minlen, maxlen, queryfract); @@ -1562,7 +1575,7 @@ int ChimeraUchimeCommand::createProcessesGroups(SequenceParser& parser, string o // Allocate memory for thread data. string extension = toString(i) + ".temp"; - uchimeData* tempUchime = new uchimeData(outputFName+extension, templatefile, filename+extension, fastaFile, nameFile, groupFile, accnos+extension, alns+extension, groups, m, lines[i].start, lines[i].end, i); + uchimeData* tempUchime = new uchimeData(outputFName+extension, uchimeLocation, templatefile, filename+extension, fastaFile, nameFile, groupFile, accnos+extension, alns+extension, groups, m, lines[i].start, lines[i].end, i); tempUchime->setBooleans(useAbskew, chimealns, useMinH, useMindiv, useXn, useDn, useXa, useChunks, useMinchunk, useIdsmoothwindow, useMinsmoothid, useMaxp, skipgaps, skipgaps2, useMinlen, useMaxlen, ucl, useQueryfract); tempUchime->setVariables(abskew, minh, mindiv, xn, dn, xa, chunks, minchunk, idsmoothwindow, minsmoothid, maxp, minlen, maxlen, queryfract); diff --git a/chimerauchimecommand.h b/chimerauchimecommand.h index b7da889..dfb410f 100644 --- a/chimerauchimecommand.h +++ b/chimerauchimecommand.h @@ -45,7 +45,7 @@ private: int createProcesses(string, string, string, string, int&); bool abort, useAbskew, chimealns, useMinH, useMindiv, useXn, useDn, useXa, useChunks, useMinchunk, useIdsmoothwindow, useMinsmoothid, useMaxp, skipgaps, skipgaps2, useMinlen, useMaxlen, ucl, useQueryfract; - string fastafile, groupfile, templatefile, outputDir, namefile, abskew, minh, mindiv, xn, dn, xa, chunks, minchunk, idsmoothwindow, minsmoothid, maxp, minlen, maxlen, queryfract; + string fastafile, groupfile, templatefile, outputDir, namefile, abskew, minh, mindiv, xn, dn, xa, chunks, minchunk, idsmoothwindow, minsmoothid, maxp, minlen, maxlen, queryfract, uchimeLocation; int processors; @@ -74,7 +74,7 @@ struct uchimeData { string namefile; string groupfile; string outputFName; - string accnos, alns, filename, templatefile; + string accnos, alns, filename, templatefile, uchimeLocation; MothurOut* m; int start; int end; @@ -84,7 +84,7 @@ struct uchimeData { string abskew, minh, mindiv, xn, dn, xa, chunks, minchunk, idsmoothwindow, minsmoothid, maxp, minlen, maxlen, queryfract; uchimeData(){} - uchimeData(string o, string t, string file, string f, string n, string g, string ac, string al, vector gr, MothurOut* mout, int st, int en, int tid) { + uchimeData(string o, string uloc, string t, string file, string f, string n, string g, string ac, string al, vector gr, MothurOut* mout, int st, int en, int tid) { fastafile = f; namefile = n; groupfile = g; @@ -100,6 +100,7 @@ struct uchimeData { groups = gr; count = 0; numChimeras = 0; + uchimeLocation = uloc; } void setBooleans(bool Abskew, bool calns, bool MinH, bool Mindiv, bool Xn, bool Dn, bool Xa, bool Chunks, bool Minchunk, bool Idsmoothwindow, bool Minsmoothid, bool Maxp, bool skipgap, bool skipgap2, bool Minlen, bool Maxlen, bool uc, bool Queryfract) { useAbskew = Abskew; @@ -183,18 +184,8 @@ static DWORD WINAPI MyUchimeThreadFunction(LPVOID lpParam){ vector cPara; - string path = pDataArray->m->argv; - string tempPath = path; - for (int j = 0; j < path.length(); j++) { tempPath[j] = tolower(path[j]); } - path = path.substr(0, (tempPath.find_last_of('m'))); - - string uchimeCommand = path; -#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) - uchimeCommand += "uchime "; -#else - uchimeCommand += "uchime"; - uchimeCommand = "\"" + uchimeCommand + "\""; -#endif + string uchimeCommand = pDataArray->uchimeLocation; + uchimeCommand = "\"" + uchimeCommand + "\""; char* tempUchime; tempUchime= new char[uchimeCommand.length()+1]; @@ -403,10 +394,10 @@ static DWORD WINAPI MyUchimeThreadFunction(LPVOID lpParam){ //uchime_main(numArgs, uchimeParameters); //cout << "commandString = " << commandString << endl; -#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) -#else commandString = "\"" + commandString + "\""; -#endif + + if (pDataArray->m->debug) { pDataArray->m->mothurOut("[DEBUG]: uchime command = " + commandString + ".\n"); } + system(commandString.c_str()); //free memory @@ -505,11 +496,14 @@ static DWORD WINAPI MyUchimeSeqsThreadFunction(LPVOID lpParam){ vector cPara; - char* tempUchime; - tempUchime= new char[8]; - *tempUchime = '\0'; - strncat(tempUchime, "uchime ", 7); - cPara.push_back(tempUchime); + string uchimeCommand = pDataArray->uchimeLocation; + uchimeCommand = "\"" + uchimeCommand + "\""; + + char* tempUchime; + tempUchime= new char[uchimeCommand.length()+1]; + *tempUchime = '\0'; + strncat(tempUchime, uchimeCommand.c_str(), uchimeCommand.length()); + cPara.push_back(tempUchime); char* tempIn = new char[8]; *tempIn = '\0'; strncat(tempIn, "--input", 7); @@ -722,6 +716,7 @@ static DWORD WINAPI MyUchimeSeqsThreadFunction(LPVOID lpParam){ //uchime_main(numArgs, uchimeParameters); //cout << "commandString = " << commandString << endl; + if (pDataArray->m->debug) { pDataArray->m->mothurOut("[DEBUG]: uchime command = " + commandString + ".\n"); } system(commandString.c_str()); //free memory diff --git a/engine.cpp b/engine.cpp index 98d730c..48f782a 100644 --- a/engine.cpp +++ b/engine.cpp @@ -26,78 +26,7 @@ Engine::Engine(){ } } /***********************************************************************/ -string Engine::findMothursPath(){ - try { - - string envPath = getenv("PATH"); - string mothurPath = ""; - - //delimiting path char - char delim; - #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) - delim = ':'; - #else - delim = ';'; - #endif - - //break apart path variable by ':' - vector dirs; - mout->splitAtChar(envPath, dirs, delim); - - if (mout->debug) { mout->mothurOut("[DEBUG]: dir's in path: \n"); } - - //get path related to mothur - for (int i = 0; i < dirs.size(); i++) { - - if (mout->debug) { mout->mothurOut("[DEBUG]: " + dirs[i] + "\n"); } - - //to lower so we can find it - string tempLower = ""; - for (int j = 0; j < dirs[i].length(); j++) { tempLower += tolower(dirs[i][j]); } - - //is this mothurs path? - if (tempLower.find("mothur") != -1) { mothurPath = dirs[i]; break; } - } - - if (mout->debug) { mout->mothurOut("[DEBUG]: mothurPath = " + mothurPath + "\n"); } - - if (mothurPath != "") { - //add mothur so it looks like what argv would look like - #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) - mothurPath += "/mothur"; - #else - mothurPath += "\\mothur"; - #endif - }else { - //okay mothur is not in the path, so the folder mothur is in must be in the path - //lets find out which one - - //get path related to mothur - for (int i = 0; i < dirs.size(); i++) { - - //is this mothurs path? - ifstream in; - string tempIn = dirs[i]; - #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) - tempIn += "/mothur"; - #else - tempIn += "\\mothur"; - #endif - mout->openInputFile(tempIn, in, ""); - - //if this file exists - if (in) { in.close(); mothurPath = tempIn; break; if (mout->debug) { mout->mothurOut("[DEBUG]: found it, mothurPath = " + mothurPath + "\n"); } } - } - } - - return mothurPath; - - } - catch(exception& e) { - mout->errorOut(e, "Engine", "findMothursPath"); - exit(1); - } -} + /***********************************************************************/ InteractEngine::InteractEngine(string path){ @@ -106,7 +35,7 @@ InteractEngine::InteractEngine(string path){ string temppath = path.substr(0, (path.find_last_of("othur")-5)); //this will happen if you set the path variable to contain mothur's exe location - if (temppath == "") { path = findMothursPath(); } + if (temppath == "") { path = mout->findProgramPath("mothur"); } mout->argv = path; } @@ -273,7 +202,7 @@ BatchEngine::BatchEngine(string path, string batchFileName){ string temppath = path.substr(0, (path.find_last_of("othur")-5)); //this will happen if you set the path variable to contain mothur's exe location - if (temppath == "") { path = findMothursPath(); } + if (temppath == "") { path = mout->findProgramPath("mothur"); } mout->argv = path; @@ -441,7 +370,7 @@ ScriptEngine::ScriptEngine(string path, string commandString){ string temppath = path.substr(0, (path.find_last_of("othur")-5)); //this will happen if you set the path variable to contain mothur's exe location - if (temppath == "") { path = findMothursPath(); } + if (temppath == "") { path = mout->findProgramPath("mothur"); } mout->argv = path; diff --git a/engine.hpp b/engine.hpp index 5311da9..460e245 100644 --- a/engine.hpp +++ b/engine.hpp @@ -33,7 +33,6 @@ protected: vector options; CommandFactory* cFactory; MothurOut* mout; - string findMothursPath(); }; diff --git a/mothurout.cpp b/mothurout.cpp index 61b3951..6ecb86f 100644 --- a/mothurout.cpp +++ b/mothurout.cpp @@ -117,6 +117,81 @@ void MothurOut::clearCurrentFiles() { exit(1); } } +/***********************************************************************/ +string MothurOut::findProgramPath(string programName){ + try { + + string envPath = getenv("PATH"); + string pPath = ""; + + //delimiting path char + char delim; +#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) + delim = ':'; +#else + delim = ';'; +#endif + + //break apart path variable by ':' + vector dirs; + splitAtChar(envPath, dirs, delim); + + if (debug) { mothurOut("[DEBUG]: dir's in path: \n"); } + + //get path related to mothur + for (int i = 0; i < dirs.size(); i++) { + + if (debug) { mothurOut("[DEBUG]: " + dirs[i] + "\n"); } + + //to lower so we can find it + string tempLower = ""; + for (int j = 0; j < dirs[i].length(); j++) { tempLower += tolower(dirs[i][j]); } + + //is this mothurs path? + if (tempLower.find(programName) != -1) { pPath = dirs[i]; break; } + } + + if (debug) { mothurOut("[DEBUG]: programPath = " + pPath + "\n"); } + + if (pPath != "") { + //add programName so it looks like what argv would look like +#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) + pPath += "/" + programName; +#else + pPath += "\\" + programName; +#endif + }else { + //okay programName is not in the path, so the folder programName is in must be in the path + //lets find out which one + + //get path related to the program + for (int i = 0; i < dirs.size(); i++) { + + if (debug) { mothurOut("[DEBUG]: looking in " + dirs[i] + " for " + programName + " \n"); } + + //is this the programs path? + ifstream in; + string tempIn = dirs[i]; +#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) + tempIn += "/" + programName; +#else + tempIn += "\\" + programName; +#endif + openInputFile(tempIn, in, ""); + + //if this file exists + if (in) { in.close(); pPath = tempIn; if (debug) { mothurOut("[DEBUG]: found it, programPath = " + pPath + "\n"); } break; } + } + } + + return pPath; + + } + catch(exception& e) { + errorOut(e, "MothurOut", "findProgramPath"); + exit(1); + } +} /*********************************************************************************************/ void MothurOut::setFileName(string filename) { try { diff --git a/mothurout.h b/mothurout.h index 0e31f84..cc8bfb6 100644 --- a/mothurout.h +++ b/mothurout.h @@ -82,6 +82,7 @@ class MothurOut { int appendFiles(string, string); int renameFile(string, string); //oldname, newname string getFullPathName(string); + string findProgramPath(string programName); string hasPath(string); string getExtension(string); string getPathName(string); -- 2.39.2