From 315e38cf393c82be238da5b32574f225a020d25c Mon Sep 17 00:00:00 2001 From: westcott Date: Tue, 26 Jan 2010 19:36:30 +0000 Subject: [PATCH] added set.dir command and modified commands to redirect input and output, removed get.line command. --- Mothur.xcodeproj/project.pbxproj | 12 +-- aligncommand.cpp | 46 ++++++--- aligncommand.h | 2 +- bellerophon.cpp | 9 +- bellerophon.h | 2 +- binsequencecommand.cpp | 49 +++++++++- binsequencecommand.h | 2 +- bootstrapsharedcommand.cpp | 16 +++- bootstrapsharedcommand.h | 2 +- chimera.h | 4 +- chimeracheckrdp.cpp | 4 +- chimeracheckrdp.h | 2 +- chimeraseqscommand.cpp | 74 +++++++++++++-- chimeraseqscommand.h | 2 +- classifyseqscommand.cpp | 50 +++++++++- classifyseqscommand.h | 2 +- clustercommand.cpp | 10 +- clustercommand.h | 2 +- collectcommand.cpp | 8 +- collectcommand.h | 2 +- collectsharedcommand.cpp | 11 ++- collectsharedcommand.h | 2 +- commandfactory.cpp | 17 +++- commandfactory.hpp | 4 + deconvolutecommand.cpp | 38 +++++++- deconvolutecommand.h | 2 +- distancecommand.cpp | 35 +++++-- distancecommand.h | 2 +- engine.hpp | 3 +- filterseqscommand.cpp | 37 +++++++- filterseqscommand.h | 2 +- getgroupcommand.cpp | 19 +++- getlabelcommand.cpp | 2 - getlinecommand.cpp | 90 ------------------ getlinecommand.h | 30 ------ getlistcountcommand.cpp | 27 +++++- getlistcountcommand.h | 2 +- getoturepcommand.cpp | 71 +++++++++++++- getoturepcommand.h | 2 +- getrabundcommand.cpp | 10 +- getsabundcommand.cpp | 10 +- getseqscommand.cpp | 85 +++++++++++++++-- getseqscommand.h | 2 +- getsharedotucommand.cpp | 61 ++++++++++-- getsharedotucommand.h | 2 +- groupmap.cpp | 14 ++- groupmap.h | 2 +- hclustercommand.cpp | 43 ++++++++- hclustercommand.h | 2 +- heatmap.cpp | 7 +- heatmap.h | 4 +- heatmapcommand.cpp | 10 +- heatmapcommand.h | 2 +- heatmapsim.cpp | 6 +- heatmapsim.h | 4 +- heatmapsimcommand.cpp | 45 ++++++++- heatmapsimcommand.h | 2 +- helpcommand.cpp | 3 - libshuffcommand.cpp | 12 ++- libshuffcommand.h | 2 +- listseqscommand.cpp | 104 +++++++++++++++++++- listseqscommand.h | 3 +- matrixoutputcommand.cpp | 10 +- matrixoutputcommand.h | 2 +- mergefilecommand.cpp | 17 +++- mgclustercommand.cpp | 37 +++++++- mgclustercommand.h | 2 +- mothur.cpp | 10 +- mothur.h | 157 ++++++++++++++++++++++++++++--- otuhierarchycommand.cpp | 28 +++++- otuhierarchycommand.h | 2 +- parsimonycommand.cpp | 18 +++- parsimonycommand.h | 2 +- pcacommand.cpp | 65 +++++++------ pcacommand.h | 2 +- phylotypecommand.cpp | 33 ++++++- phylotypecommand.h | 2 +- pintail.cpp | 12 +-- pintail.h | 2 +- preclustercommand.cpp | 40 +++++++- preclustercommand.h | 2 +- quitcommand.cpp | 2 - rarefactcommand.cpp | 10 +- rarefactcommand.h | 1 + rarefactsharedcommand.cpp | 10 +- rarefactsharedcommand.h | 2 +- readdistcommand.cpp | 60 ++++++++++-- readdistcommand.h | 2 +- readotucommand.cpp | 74 +++++++++++++-- readotucommand.h | 2 +- readtreecommand.cpp | 28 +++++- removeseqscommand.cpp | 75 ++++++++++++++- removeseqscommand.h | 2 +- reversecommand.cpp | 29 +++++- reversecommand.h | 2 +- screenseqscommand.cpp | 69 +++++++++++--- screenseqscommand.h | 2 +- secondarystructurecommand.cpp | 35 ++++++- secondarystructurecommand.h | 2 +- seqsummarycommand.cpp | 27 +++++- seqsummarycommand.h | 2 +- setdircommand.cpp | 140 +++++++++++++++++++++++++++ setdircommand.h | 36 +++++++ sharedcommand.cpp | 16 ++-- sharedcommand.h | 4 +- summarycommand.cpp | 13 ++- summarycommand.h | 2 +- summarysharedcommand.cpp | 10 +- summarysharedcommand.h | 2 +- systemcommand.cpp | 12 ++- treegroupscommand.cpp | 53 +++++++++-- treegroupscommand.h | 2 +- trimseqscommand.cpp | 54 +++++++++-- trimseqscommand.h | 2 +- unifracunweightedcommand.cpp | 16 +++- unifracunweightedcommand.h | 2 +- unifracweightedcommand.cpp | 16 +++- unifracweightedcommand.h | 2 +- venn.cpp | 12 +-- venn.h | 4 +- venncommand.cpp | 10 +- venncommand.h | 2 +- 122 files changed, 1925 insertions(+), 495 deletions(-) delete mode 100644 getlinecommand.cpp delete mode 100644 getlinecommand.h create mode 100644 setdircommand.cpp create mode 100644 setdircommand.h diff --git a/Mothur.xcodeproj/project.pbxproj b/Mothur.xcodeproj/project.pbxproj index a44e1f3..b0db018 100644 --- a/Mothur.xcodeproj/project.pbxproj +++ b/Mothur.xcodeproj/project.pbxproj @@ -160,9 +160,9 @@ 8DD76F6A0486A84900D96B5E /* Mothur.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = C6859E8B029090EE04C91782 /* Mothur.1 */; }; A7027F2810DFF86D00BF45FE /* preclustercommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7027F2710DFF86D00BF45FE /* preclustercommand.cpp */; }; A704E8A31106045D00870157 /* otuhierarchycommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A704E8A21106045D00870157 /* otuhierarchycommand.cpp */; }; + A70B00C8110885EB002F453A /* setdircommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A70B00C7110885EB002F453A /* setdircommand.cpp */; }; A70B53AA0F4CD7AD0064797E /* getgroupcommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A70B53A40F4CD7AD0064797E /* getgroupcommand.cpp */; }; A70B53AB0F4CD7AD0064797E /* getlabelcommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A70B53A60F4CD7AD0064797E /* getlabelcommand.cpp */; }; - A70B53AC0F4CD7AD0064797E /* getlinecommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A70B53A80F4CD7AD0064797E /* getlinecommand.cpp */; }; A70DECD91063D8B40057C03C /* secondarystructurecommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A70DECD81063D8B40057C03C /* secondarystructurecommand.cpp */; }; A727E84A10D14568001A8432 /* readblast.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A727E84910D14568001A8432 /* readblast.cpp */; }; A7283FF81056CAE100D0CC69 /* chimeracheckrdp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A7283FF71056CAE100D0CC69 /* chimeracheckrdp.cpp */; }; @@ -535,12 +535,12 @@ A7027F2710DFF86D00BF45FE /* preclustercommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = preclustercommand.cpp; sourceTree = SOURCE_ROOT; }; A704E8A11106045D00870157 /* otuhierarchycommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = otuhierarchycommand.h; sourceTree = SOURCE_ROOT; }; A704E8A21106045D00870157 /* otuhierarchycommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = otuhierarchycommand.cpp; sourceTree = SOURCE_ROOT; }; + A70B00C6110885EB002F453A /* setdircommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = setdircommand.h; sourceTree = SOURCE_ROOT; }; + A70B00C7110885EB002F453A /* setdircommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = setdircommand.cpp; sourceTree = SOURCE_ROOT; }; A70B53A40F4CD7AD0064797E /* getgroupcommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = getgroupcommand.cpp; sourceTree = SOURCE_ROOT; }; A70B53A50F4CD7AD0064797E /* getgroupcommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = getgroupcommand.h; sourceTree = SOURCE_ROOT; }; A70B53A60F4CD7AD0064797E /* getlabelcommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = getlabelcommand.cpp; sourceTree = SOURCE_ROOT; }; A70B53A70F4CD7AD0064797E /* getlabelcommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = getlabelcommand.h; sourceTree = SOURCE_ROOT; }; - A70B53A80F4CD7AD0064797E /* getlinecommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = getlinecommand.cpp; sourceTree = SOURCE_ROOT; }; - A70B53A90F4CD7AD0064797E /* getlinecommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = getlinecommand.h; sourceTree = SOURCE_ROOT; }; A70DECD71063D8B40057C03C /* secondarystructurecommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = secondarystructurecommand.h; sourceTree = SOURCE_ROOT; }; A70DECD81063D8B40057C03C /* secondarystructurecommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = secondarystructurecommand.cpp; sourceTree = SOURCE_ROOT; }; A727E84810D14568001A8432 /* readblast.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = readblast.h; sourceTree = SOURCE_ROOT; }; @@ -914,8 +914,6 @@ A70B53A40F4CD7AD0064797E /* getgroupcommand.cpp */, A70B53A70F4CD7AD0064797E /* getlabelcommand.h */, A70B53A60F4CD7AD0064797E /* getlabelcommand.cpp */, - A70B53A90F4CD7AD0064797E /* getlinecommand.h */, - A70B53A80F4CD7AD0064797E /* getlinecommand.cpp */, A73329CD1083B3B3003B10C5 /* getlistcountcommand.h */, A73329CE1083B3B3003B10C5 /* getlistcountcommand.cpp */, 370B88050F8A4EE4005AB382 /* getoturepcommand.h */, @@ -980,6 +978,8 @@ 371B30B20FD7EE67000414CA /* screenseqscommand.cpp */, 3799A94F0FD6A58C00E33EDE /* seqsummarycommand.h */, 3799A94E0FD6A58C00E33EDE /* seqsummarycommand.cpp */, + A70B00C6110885EB002F453A /* setdircommand.h */, + A70B00C7110885EB002F453A /* setdircommand.cpp */, 37D928270F21331F001D4494 /* sharedcommand.h */, 37D928260F21331F001D4494 /* sharedcommand.cpp */, 37D928470F21331F001D4494 /* summarycommand.h */, @@ -1226,7 +1226,6 @@ 37AFC71F0F445386005F492D /* sharedsobscollectsummary.cpp in Sources */, A70B53AA0F4CD7AD0064797E /* getgroupcommand.cpp in Sources */, A70B53AB0F4CD7AD0064797E /* getlabelcommand.cpp in Sources */, - A70B53AC0F4CD7AD0064797E /* getlinecommand.cpp in Sources */, EB9303EB0F534D9400E8EF26 /* logsd.cpp in Sources */, EB9303F90F53517300E8EF26 /* geom.cpp in Sources */, EB6E68190F5F1C780044E17B /* qstat.cpp in Sources */, @@ -1331,6 +1330,7 @@ A7D86C7D10FE09AB00865661 /* formatcolumn.cpp in Sources */, A7D86C8C10FE09FE00865661 /* formatphylip.cpp in Sources */, A704E8A31106045D00870157 /* otuhierarchycommand.cpp in Sources */, + A70B00C8110885EB002F453A /* setdircommand.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/aligncommand.cpp b/aligncommand.cpp index 3b3afc1..7841557 100644 --- a/aligncommand.cpp +++ b/aligncommand.cpp @@ -39,27 +39,44 @@ AlignCommand::AlignCommand(string option){ else { //valid paramters for this command - string AlignArray[] = {"template","candidate","search","ksize","align","match","mismatch","gapopen","gapextend", "processors","flip","threshold"}; + string AlignArray[] = {"template","candidate","search","ksize","align","match","mismatch","gapopen","gapextend", "processors","flip","threshold","outputdir","inputdir"}; vector myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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 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 = ""; } + + //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("template"); + //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["template"] = inputDir + it->second; } + } + } + //check for required parameters templateFileName = validParameter.validFile(parameters, "template", true); if (templateFileName == "not found") { mothurOut("template is a required parameter for the align.seqs command."); mothurOutEndLine(); abort = true; - } - else if (templateFileName == "not open") { abort = true; } + }else if (templateFileName == "not open") { abort = true; } candidateFileName = validParameter.validFile(parameters, "candidate", false); if (candidateFileName == "not found") { mothurOut("candidate is a required parameter for the align.seqs command."); mothurOutEndLine(); abort = true; } @@ -68,6 +85,12 @@ AlignCommand::AlignCommand(string option){ //go through files and make sure they are good, if not, then disregard them for (int i = 0; i < candidateFileNames.size(); i++) { + if (inputDir != "") { + string path = hasPath(candidateFileNames[i]); + //if the user has not given a path then, add inputdir. else leave path alone. + if (path == "") { candidateFileNames[i] = inputDir + candidateFileNames[i]; } + } + int ableToOpen; ifstream in; ableToOpen = openInputFile(candidateFileNames[i], in); @@ -149,8 +172,8 @@ void AlignCommand::help(){ mothurOut("The mistmatch parameter allows you to specify the penalty for having different bases. The default is -1.0.\n"); mothurOut("The gapopen parameter allows you to specify the penalty for opening a gap in an alignment. The default is -2.0.\n"); mothurOut("The gapextend parameter allows you to specify the penalty for extending a gap in an alignment. The default is -1.0.\n"); - mothurOut("The flip parameter is used to specify whether or not you want mothur to try the reverse compement if a sequence falls below the threshold. The default is false.\n"); - mothurOut("The threshold is used to specify a cutoff at which an alignment is deemed 'bad' and the reverse complement may be tried. \n"); + mothurOut("The flip parameter is used to specify whether or not you want mothur to try the reverse complement if a sequence falls below the threshold. The default is false.\n"); + mothurOut("The threshold is used to specify a cutoff at which an alignment is deemed 'bad' and the reverse complement may be tried. The default threshold is 0.50, meaning 50% of the bases are removed in the alignment.\n"); mothurOut("If the flip parameter is set to true the reverse complement of the sequence is aligned and the better alignment is reported.\n"); mothurOut("The default for the threshold parameter is 0.50, meaning at least 50% of the bases must remain or the sequence is reported as potentially reversed.\n"); mothurOut("The align.seqs command should be in the following format: \n"); @@ -187,9 +210,10 @@ int AlignCommand::execute(){ for (int s = 0; s < candidateFileNames.size(); s++) { mothurOut("Aligning sequences from " + candidateFileNames[s] + " ..." ); mothurOutEndLine(); - string alignFileName = candidateFileNames[s].substr(0,candidateFileNames[s].find_last_of(".")+1) + "align"; - string reportFileName = candidateFileNames[s].substr(0,candidateFileNames[s].find_last_of(".")+1) + "align.report"; - string accnosFileName = candidateFileNames[s].substr(0,candidateFileNames[s].find_last_of(".")+1) + "flip.accnos"; + if (outputDir == "") { outputDir += hasPath(candidateFileNames[s]); } + string alignFileName = outputDir + getRootName(getSimpleName(candidateFileNames[s])) + "align"; + string reportFileName = outputDir + getRootName(getSimpleName(candidateFileNames[s])) + "align.report"; + string accnosFileName = outputDir + getRootName(getSimpleName(candidateFileNames[s])) + "flip.accnos"; int numFastaSeqs = 0; for (int i = 0; i < lines.size(); i++) { delete lines[i]; } lines.clear(); @@ -283,13 +307,13 @@ int AlignCommand::execute(){ } #else ifstream inFASTA; - openInputFile(candidateFileName[s], inFASTA); + openInputFile(candidateFileNames[s], inFASTA); numFastaSeqs=count(istreambuf_iterator(inFASTA),istreambuf_iterator(), '>'); inFASTA.close(); lines.push_back(new linePair(0, numFastaSeqs)); - driver(lines[0], alignFileName, reportFileName, accnosFileName); + driver(lines[0], alignFileName, reportFileName, accnosFileName, candidateFileNames[s]); //delete accnos file if its blank else report to user if (isBlank(accnosFileName)) { remove(accnosFileName.c_str()); } diff --git a/aligncommand.h b/aligncommand.h index 0a2e7ba..8042983 100644 --- a/aligncommand.h +++ b/aligncommand.h @@ -41,7 +41,7 @@ private: void appendAlignFiles(string, string); void appendReportFiles(string, string); - string candidateFileName, templateFileName, distanceFileName, search, align; + string candidateFileName, templateFileName, distanceFileName, search, align, outputDir; float match, misMatch, gapOpen, gapExtend, threshold; int processors, kmerSize; vector candidateFileNames; diff --git a/bellerophon.cpp b/bellerophon.cpp index a60a838..7c64d47 100644 --- a/bellerophon.cpp +++ b/bellerophon.cpp @@ -15,9 +15,10 @@ //*************************************************************************************************************** -Bellerophon::Bellerophon(string name) { +Bellerophon::Bellerophon(string name, string o) { try { fastafile = name; + outputDir = o; } catch(exception& e) { errorOut(e, "Bellerophon", "Bellerophon"); @@ -81,12 +82,16 @@ int Bellerophon::getChimeras() { //do soft filter if (filter) { string optionString = "fasta=" + fastafile + ", soft=50"; + if (outputDir != "") { optionString += ", outputdir=" + outputDir; } + filterSeqs = new FilterSeqsCommand(optionString); filterSeqs->execute(); delete filterSeqs; //reset fastafile to filtered file - fastafile = getRootName(fastafile) + "filter.fasta"; + if (outputDir == "") { fastafile = getRootName(fastafile) + "filter.fasta"; } + else { fastafile = outputDir + getRootName(getSimpleName(fastafile)) + "filter.fasta"; } + } distCalculator = new eachGapDist(); diff --git a/bellerophon.h b/bellerophon.h index 85c5550..aa53f98 100644 --- a/bellerophon.h +++ b/bellerophon.h @@ -25,7 +25,7 @@ typedef map SeqMap; //maps sequence to all distance for that seqeun class Bellerophon : public Chimera { public: - Bellerophon(string); + Bellerophon(string, string); ~Bellerophon() {}; int getChimeras(); diff --git a/binsequencecommand.cpp b/binsequencecommand.cpp index ad90ff4..6618eb6 100644 --- a/binsequencecommand.cpp +++ b/binsequencecommand.cpp @@ -22,19 +22,27 @@ BinSeqCommand::BinSeqCommand(string option){ else { //valid paramters for this command - string AlignArray[] = {"fasta","label","name", "group"}; + string AlignArray[] = {"fasta","label","name", "group","outputdir","inputdir"}; vector myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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 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->getListFile()); //if user entered a file with a path then preserve it + } + + //make sure the user has already run the read.otu command if (globaldata->getListFile() == "") { mothurOut("You must read a listfile before running the bin.seqs command."); @@ -42,6 +50,36 @@ BinSeqCommand::BinSeqCommand(string option){ 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("fasta"); + //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["fasta"] = 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; } + } + + it = parameters.find("group"); + //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["group"] = inputDir + it->second; } + } + } + //check for required parameters fastafile = validParameter.validFile(parameters, "fasta", true); @@ -77,7 +115,9 @@ BinSeqCommand::BinSeqCommand(string option){ fasta = new FastaMap(); if (groupfile != "") { groupMap = new GroupMap(groupfile); - groupMap->readMap(); + + int error = groupMap->readMap(); + if (error == 1) { delete groupMap; abort = true; } } } @@ -258,7 +298,8 @@ void BinSeqCommand::readNamesFile() { int BinSeqCommand::process(ListVector* list) { try { string binnames, name, sequence; - string outputFileName = getRootName(globaldata->getListFile()) + list->getLabel() + ".fasta"; + + string outputFileName = outputDir + getRootName(getSimpleName(globaldata->getListFile())) + list->getLabel() + ".fasta"; openOutputFile(outputFileName, out); mothurOut(list->getLabel()); mothurOutEndLine(); diff --git a/binsequencecommand.h b/binsequencecommand.h index a183151..2804235 100644 --- a/binsequencecommand.h +++ b/binsequencecommand.h @@ -38,7 +38,7 @@ private: GroupMap* groupMap; bool abort, allLines; set labels; //holds labels to be used - string filename, fastafile, namesfile, groupfile, label; + string filename, fastafile, namesfile, groupfile, label, outputDir; ofstream out; ifstream in, inNames; diff --git a/bootstrapsharedcommand.cpp b/bootstrapsharedcommand.cpp index 1b682c7..4f44880 100644 --- a/bootstrapsharedcommand.cpp +++ b/bootstrapsharedcommand.cpp @@ -36,7 +36,7 @@ BootSharedCommand::BootSharedCommand(string option){ else { //valid paramters for this command - string Array[] = {"label","calc","groups","iters"}; + string Array[] = {"label","calc","groups","iters","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); @@ -49,6 +49,13 @@ BootSharedCommand::BootSharedCommand(string option){ if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } } + //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 + } + + //make sure the user has already run the read.otu command if (globaldata->getSharedFile() == "") { if (globaldata->getListFile() == "") { mothurOut("You must read a list and a group, or a shared before you can use the bootstrap.shared command."); mothurOutEndLine(); abort = true; } @@ -373,12 +380,11 @@ void BootSharedCommand::process(SharedOrderVector* order) { try{ EstOutput data; vector subset; - - + //open an ostream for each calc to print to for (int z = 0; z < treeCalculators.size(); z++) { //create a new filename - outputFile = getRootName(globaldata->inputFileName) + treeCalculators[z]->getName() + ".boot" + order->getLabel() + ".tre"; + outputFile = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + treeCalculators[z]->getName() + ".boot" + order->getLabel() + ".tre"; openOutputFile(outputFile, *(out[z])); } @@ -446,7 +452,7 @@ void BootSharedCommand::process(SharedOrderVector* order) { //set global data to calc trees globaldata->gTree = trees[k]; - string filename = getRootName(globaldata->inputFileName) + treeCalculators[k]->getName() + ".boot" + order->getLabel(); + string filename = getRootName(getSimpleName(globaldata->inputFileName)) + treeCalculators[k]->getName() + ".boot" + order->getLabel(); consensus = new ConcensusCommand(filename); consensus->execute(); delete consensus; diff --git a/bootstrapsharedcommand.h b/bootstrapsharedcommand.h index 794da84..6b63233 100644 --- a/bootstrapsharedcommand.h +++ b/bootstrapsharedcommand.h @@ -56,7 +56,7 @@ private: bool abort, allLines; set labels; //holds labels to be used - string outputFile, calc, groups, label; + string outputFile, calc, groups, label, outputDir; int numGroups, iters; vector Estimators, Groups; //holds estimators to be used diff --git a/chimera.h b/chimera.h index 339f2c6..8b3b0eb 100644 --- a/chimera.h +++ b/chimera.h @@ -59,8 +59,8 @@ class Chimera { public: Chimera(){}; - Chimera(string); Chimera(string, string); + Chimera(string, string, string); virtual ~Chimera(){}; virtual void setFilter(bool f) { filter = f; } virtual void setCorrection(bool c) { correction = c; } @@ -97,7 +97,7 @@ class Chimera { bool filter, correction, svg, unaligned; int processors, window, increment, numWanted, kmerSize, match, misMatch, minSim, parents, iters; float divR; - string seqMask, quanfile, filterString, name; + string seqMask, quanfile, filterString, name, outputDir; }; diff --git a/chimeracheckrdp.cpp b/chimeracheckrdp.cpp index 1278513..04c143f 100644 --- a/chimeracheckrdp.cpp +++ b/chimeracheckrdp.cpp @@ -10,7 +10,7 @@ #include "chimeracheckrdp.h" //*************************************************************************************************************** -ChimeraCheckRDP::ChimeraCheckRDP(string filename, string temp) { fastafile = filename; templateFile = temp; } +ChimeraCheckRDP::ChimeraCheckRDP(string filename, string temp, string o) { fastafile = filename; templateFile = temp; outputDir = o; } //*************************************************************************************************************** ChimeraCheckRDP::~ChimeraCheckRDP() { @@ -338,7 +338,7 @@ void ChimeraCheckRDP::getCutoff() { void ChimeraCheckRDP::makeSVGpic(vector info, int query) { try{ - string file = querySeqs[query]->getName() + ".chimeracheck.svg"; + string file = outputDir + querySeqs[query]->getName() + ".chimeracheck.svg"; ofstream outsvg; openOutputFile(file, outsvg); diff --git a/chimeracheckrdp.h b/chimeracheckrdp.h index e885af5..6193d6d 100644 --- a/chimeracheckrdp.h +++ b/chimeracheckrdp.h @@ -25,7 +25,7 @@ class ChimeraCheckRDP : public Chimera { public: - ChimeraCheckRDP(string, string); + ChimeraCheckRDP(string, string, string); ~ChimeraCheckRDP(); int getChimeras(); diff --git a/chimeraseqscommand.cpp b/chimeraseqscommand.cpp index 9bd13e3..63d02f5 100644 --- a/chimeraseqscommand.cpp +++ b/chimeraseqscommand.cpp @@ -26,24 +26,78 @@ ChimeraSeqsCommand::ChimeraSeqsCommand(string option){ else { //valid paramters for this command - string Array[] = {"fasta", "filter", "correction", "processors", "method", "window", "increment", "template", "conservation", "quantile", "mask", "numwanted", "ksize", "svg", "name", "match","mismatch", "divergence", "minsim", "parents", "iters" }; + string Array[] = {"fasta", "filter", "correction", "processors", "method", "window", "increment", "template", "conservation", "quantile", "mask", "numwanted", "ksize", "svg", "name", "match","mismatch", "divergence", "minsim", "parents", "iters","outputdir","inputdir" }; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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("fasta"); + //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["fasta"] = inputDir + it->second; } + } + + it = parameters.find("template"); + //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["template"] = inputDir + it->second; } + } + + it = parameters.find("conservation"); + //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["conservation"] = inputDir + it->second; } + } + + it = parameters.find("quantile"); + //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["quantile"] = 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; } + } + } + + //check for required parameters fastafile = validParameter.validFile(parameters, "fasta", true); if (fastafile == "not open") { abort = true; } else if (fastafile == "not found") { fastafile = ""; mothurOut("fasta is a required parameter for the chimera.seqs command."); mothurOutEndLine(); abort = true; } + //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(fastafile); //if user entered a file with a path then preserve it + } + templatefile = validParameter.validFile(parameters, "template", true); if (templatefile == "not open") { abort = true; } else if (templatefile == "not found") { templatefile = ""; } @@ -63,6 +117,12 @@ ChimeraSeqsCommand::ChimeraSeqsCommand(string option){ maskfile = validParameter.validFile(parameters, "mask", false); if (maskfile == "not found") { maskfile = ""; } else if (maskfile != "default") { + if (inputDir != "") { + string path = hasPath(maskfile); + //if the user has not given a path then, add inputdir. else leave path alone. + if (path == "") { maskfile = inputDir + maskfile; } + } + ifstream in; int ableToOpen = openInputFile(maskfile, in); if (ableToOpen == 1) { abort = true; } @@ -192,10 +252,10 @@ int ChimeraSeqsCommand::execute(){ if (abort == true) { return 0; } - if (method == "bellerophon") { chimera = new Bellerophon(fastafile); } - else if (method == "pintail") { chimera = new Pintail(fastafile, templatefile); } + if (method == "bellerophon") { chimera = new Bellerophon(fastafile, outputDir); } + else if (method == "pintail") { chimera = new Pintail(fastafile, templatefile, outputDir); } else if (method == "ccode") { chimera = new Ccode(fastafile, templatefile); } - else if (method == "chimeracheck") { chimera = new ChimeraCheckRDP(fastafile, templatefile); } + else if (method == "chimeracheck") { chimera = new ChimeraCheckRDP(fastafile, templatefile, outputDir); } else if (method == "chimeraslayer") { chimera = new ChimeraSlayer(fastafile, templatefile); } else { mothurOut("Not a valid method."); mothurOutEndLine(); return 0; } @@ -231,8 +291,8 @@ int ChimeraSeqsCommand::execute(){ //there was a problem if (error == 1) { return 0; } - - string outputFileName = getRootName(fastafile) + method + maskfile + ".chimeras"; + + string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + method + maskfile + ".chimeras"; ofstream out; openOutputFile(outputFileName, out); diff --git a/chimeraseqscommand.h b/chimeraseqscommand.h index f19a79d..46e2bc8 100644 --- a/chimeraseqscommand.h +++ b/chimeraseqscommand.h @@ -28,7 +28,7 @@ public: private: bool abort; - string method, fastafile, templatefile, consfile, quanfile, maskfile, namefile; + string method, fastafile, templatefile, consfile, quanfile, maskfile, namefile, outputDir; bool filter, correction, svg, printAll; int processors, midpoint, averageLeft, averageRight, window, iters, increment, numwanted, ksize, match, mismatch, parents, minSimilarity; float divR; diff --git a/classifyseqscommand.cpp b/classifyseqscommand.cpp index 473d4d1..0d9e797 100644 --- a/classifyseqscommand.cpp +++ b/classifyseqscommand.cpp @@ -25,19 +25,45 @@ ClassifySeqsCommand::ClassifySeqsCommand(string option){ else { //valid paramters for this command - string AlignArray[] = {"template","fasta","name","search","ksize","method","processors","taxonomy","match","mismatch","gapopen","gapextend","numwanted","cutoff","probs","iters"}; + string AlignArray[] = {"template","fasta","name","search","ksize","method","processors","taxonomy","match","mismatch","gapopen","gapextend","numwanted","cutoff","probs","iters", "outputdir","inputdir"}; vector myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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 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 = ""; } + + //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("template"); + //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["template"] = inputDir + it->second; } + } + + it = parameters.find("taxonomy"); + //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["taxonomy"] = inputDir + it->second; } + } + } + //check for required parameters templateFileName = validParameter.validFile(parameters, "template", true); if (templateFileName == "not found") { @@ -54,6 +80,12 @@ ClassifySeqsCommand::ClassifySeqsCommand(string option){ //go through files and make sure they are good, if not, then disregard them for (int i = 0; i < fastaFileNames.size(); i++) { + if (inputDir != "") { + string path = hasPath(fastaFileNames[i]); + //if the user has not given a path then, add inputdir. else leave path alone. + if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; } + } + int ableToOpen; ifstream in; ableToOpen = openInputFile(fastaFileNames[i], in); @@ -88,6 +120,12 @@ ClassifySeqsCommand::ClassifySeqsCommand(string option){ //go through files and make sure they are good, if not, then disregard them for (int i = 0; i < namefileNames.size(); i++) { + if (inputDir != "") { + string path = hasPath(namefileNames[i]); + //if the user has not given a path then, add inputdir. else leave path alone. + if (path == "") { namefileNames[i] = inputDir + namefileNames[i]; } + } + int ableToOpen; ifstream in; ableToOpen = openInputFile(namefileNames[i], in); @@ -227,9 +265,11 @@ int ClassifySeqsCommand::execute(){ } mothurOut("Classifying sequences from " + fastaFileNames[s] + " ..." ); mothurOutEndLine(); - string newTaxonomyFile = getRootName(fastaFileNames[s]) + getRootName(taxonomyFileName) + "taxonomy"; - string tempTaxonomyFile = getRootName(fastaFileNames[s]) + "taxonomy.temp"; - string taxSummary = getRootName(fastaFileNames[s]) + getRootName(taxonomyFileName) + "tax.summary"; + + if (outputDir == "") { outputDir += hasPath(fastaFileNames[s]); } + string newTaxonomyFile = outputDir + getRootName(getSimpleName(fastaFileNames[s])) + getRootName(getSimpleName(taxonomyFileName)) + "taxonomy"; + string tempTaxonomyFile = outputDir + getRootName(getSimpleName(fastaFileNames[s])) + "taxonomy.temp"; + string taxSummary = outputDir + getRootName(getSimpleName(fastaFileNames[s])) + getRootName(getSimpleName(taxonomyFileName)) + "tax.summary"; int start = time(NULL); int numFastaSeqs = 0; diff --git a/classifyseqscommand.h b/classifyseqscommand.h index 0255bd2..2223593 100644 --- a/classifyseqscommand.h +++ b/classifyseqscommand.h @@ -48,7 +48,7 @@ private: Classify* classify; - string fastaFileName, templateFileName, distanceFileName, namefile, search, method, taxonomyFileName; + string fastaFileName, templateFileName, distanceFileName, namefile, search, method, taxonomyFileName, outputDir; int processors, kmerSize, numWanted, cutoff, iters; float match, misMatch, gapOpen, gapExtend; bool abort, probs; diff --git a/clustercommand.cpp b/clustercommand.cpp index 93ba1f9..9d09d2c 100644 --- a/clustercommand.cpp +++ b/clustercommand.cpp @@ -21,7 +21,7 @@ ClusterCommand::ClusterCommand(string option){ else { //valid paramters for this command - string Array[] = {"cutoff","precision","method","showabund","timing"}; + string Array[] = {"cutoff","precision","method","showabund","timing","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); @@ -36,6 +36,9 @@ ClusterCommand::ClusterCommand(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 = ""; } + //error checking to make sure they read a distance file if ((globaldata->gSparseMatrix == NULL) || (globaldata->gListVector == NULL)) { mothurOut("Before you use the cluster command, you first need to read in a distance matrix."); mothurOutEndLine(); @@ -85,8 +88,9 @@ ClusterCommand::ClusterCommand(string option){ else if(method == "nearest"){ cluster = new SingleLinkage(rabund, list, matrix, cutoff); } else if(method == "average"){ cluster = new AverageLinkage(rabund, list, matrix, cutoff); } tag = cluster->getTag(); - - fileroot = getRootName(globaldata->inputFileName); + + if (outputDir == "") { outputDir += hasPath(globaldata->inputFileName); } + fileroot = outputDir + getRootName(getSimpleName(globaldata->inputFileName)); openOutputFile(fileroot+ tag + ".sabund", sabundFile); openOutputFile(fileroot+ tag + ".rabund", rabundFile); diff --git a/clustercommand.h b/clustercommand.h index ad9fe85..5c21efc 100644 --- a/clustercommand.h +++ b/clustercommand.h @@ -45,7 +45,7 @@ private: bool abort; - string method, fileroot, tag; + string method, fileroot, tag, outputDir; double cutoff; string showabund, timing; int precision, length; diff --git a/collectcommand.cpp b/collectcommand.cpp index beb70eb..a31883c 100644 --- a/collectcommand.cpp +++ b/collectcommand.cpp @@ -44,7 +44,7 @@ CollectCommand::CollectCommand(string option){ else { //valid paramters for this command - string Array[] = {"freq","label","calc","abund","size"}; + string Array[] = {"freq","label","calc","abund","size","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); @@ -57,6 +57,9 @@ CollectCommand::CollectCommand(string option){ if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } } + //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 = ""; } + //make sure the user has already run the read.otu command if ((globaldata->getSharedFile() == "") && (globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "")) { mothurOut("You must read a list, sabund, rabund or shared file before you can use the collect.single command."); mothurOutEndLine(); abort = true; } @@ -136,7 +139,8 @@ int CollectCommand::execute(){ for (int p = 0; p < inputFileNames.size(); p++) { - string fileNameRoot = getRootName(inputFileNames[p]); + if (outputDir == "") { outputDir += hasPath(inputFileNames[p]); } + string fileNameRoot = outputDir + getRootName(getSimpleName(inputFileNames[p])); globaldata->inputFileName = inputFileNames[p]; if (inputFileNames.size() > 1) { diff --git a/collectcommand.h b/collectcommand.h index 46dd9dc..0df1bab 100644 --- a/collectcommand.h +++ b/collectcommand.h @@ -54,7 +54,7 @@ private: bool abort, allLines; set labels; //holds labels to be used - string label, calc; + string label, calc, outputDir; vector Estimators; vector inputFileNames; vector groups; diff --git a/collectsharedcommand.cpp b/collectsharedcommand.cpp index e68890c..a8d5069 100644 --- a/collectsharedcommand.cpp +++ b/collectsharedcommand.cpp @@ -50,7 +50,7 @@ CollectSharedCommand::CollectSharedCommand(string option){ else { //valid paramters for this command - string Array[] = {"freq","label","calc","groups","all"}; + string Array[] = {"freq","label","calc","groups","all","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); @@ -63,6 +63,10 @@ CollectSharedCommand::CollectSharedCommand(string option){ if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } } + //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 = ""; } + + //make sure the user has already run the read.otu command if (globaldata->getSharedFile() == "") { if (globaldata->getListFile() == "") { mothurOut("You must read a list and a group, or a shared before you can use the collect.shared command."); mothurOutEndLine(); abort = true; } @@ -107,8 +111,9 @@ CollectSharedCommand::CollectSharedCommand(string option){ all = isTrue(temp); if (abort == false) { - - string fileNameRoot = getRootName(globaldata->inputFileName); + + if (outputDir == "") { outputDir += hasPath(globaldata->inputFileName); } + string fileNameRoot = outputDir + getRootName(getSimpleName(globaldata->inputFileName)); format = globaldata->getFormat(); int i; diff --git a/collectsharedcommand.h b/collectsharedcommand.h index 139a0cf..10efcc8 100644 --- a/collectsharedcommand.h +++ b/collectsharedcommand.h @@ -47,7 +47,7 @@ private: bool abort, allLines, all; set labels; //holds labels to be used - string label, calc, groups; + string label, calc, groups, outputDir; vector Estimators, Groups; diff --git a/commandfactory.cpp b/commandfactory.cpp index 0da5986..ddb7029 100644 --- a/commandfactory.cpp +++ b/commandfactory.cpp @@ -16,7 +16,6 @@ #include "collectsharedcommand.h" #include "getgroupcommand.h" #include "getlabelcommand.h" -#include "getlinecommand.h" #include "rarefactcommand.h" #include "summarycommand.h" #include "summarysharedcommand.h" @@ -64,6 +63,7 @@ #include "preclustercommand.h" #include "pcacommand.h" #include "otuhierarchycommand.h" +#include "setdircommand.h" /*******************************************************/ @@ -78,10 +78,10 @@ CommandFactory* CommandFactory::getInstance() { /***********************************************************/ CommandFactory::CommandFactory(){ - _uniqueInstance = 0; string s = ""; command = new NoCommand(s); + outputDir = ""; inputDir = ""; //initialize list of valid commands commands["read.dist"] = "read.dist"; @@ -109,7 +109,6 @@ CommandFactory::CommandFactory(){ commands["venn"] = "venn"; commands["get.group"] = "get.group"; commands["get.label"] = "get.label"; - commands["get.line"] = "get.line"; commands["get.sabund"] = "get.sabund"; commands["get.rabund"] = "get.rabund"; commands["bootstrap.shared"] = "bootstrap.shared"; @@ -137,12 +136,13 @@ CommandFactory::CommandFactory(){ commands["pre.cluster"] = "pre.cluster"; commands["pca"] = "pca"; commands["otu.hierarchy"] = "otu.hierarchy"; - + commands["set.dir"] = "set.dir"; } /***********************************************************/ /***********************************************************/ CommandFactory::~CommandFactory(){ + _uniqueInstance = 0; delete command; } @@ -153,7 +153,14 @@ CommandFactory::~CommandFactory(){ Command* CommandFactory::getCommand(string commandName, string optionString){ try { delete command; //delete the old command + + //user has opted to redirect output from dir where input files are located to some other place + if (outputDir != "") { optionString += ", outputdir=" + outputDir; } + + //user has opted to redirect input from dir where mothur.exe is located to some other place + if (inputDir != "") { optionString += ", inputdir=" + inputDir; } + if(commandName == "read.dist") { command = new ReadDistCommand(optionString); } else if(commandName == "read.otu") { command = new ReadOtuCommand(optionString); } else if(commandName == "read.tree") { command = new ReadTreeCommand(optionString); } @@ -172,7 +179,6 @@ Command* CommandFactory::getCommand(string commandName, string optionString){ else if(commandName == "unifrac.unweighted") { command = new UnifracUnweightedCommand(optionString); } else if(commandName == "get.group") { command = new GetgroupCommand(optionString); } else if(commandName == "get.label") { command = new GetlabelCommand(optionString); } - else if(commandName == "get.line") { command = new GetlineCommand(optionString); } else if(commandName == "get.sabund") { command = new GetSAbundCommand(optionString); } else if(commandName == "get.rabund") { command = new GetRAbundCommand(optionString); } else if(commandName == "libshuff") { command = new LibShuffCommand(optionString); } @@ -208,6 +214,7 @@ Command* CommandFactory::getCommand(string commandName, string optionString){ else if(commandName == "pre.cluster") { command = new PreClusterCommand(optionString); } else if(commandName == "pca") { command = new PCACommand(optionString); } else if(commandName == "otu.hierarchy") { command = new OtuHierarchyCommand(optionString); } + else if(commandName == "set.dir") { command = new SetDirectoryCommand(optionString); } else { command = new NoCommand(optionString); } return command; diff --git a/commandfactory.hpp b/commandfactory.hpp index 70e2630..d84be62 100644 --- a/commandfactory.hpp +++ b/commandfactory.hpp @@ -21,11 +21,15 @@ 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; } private: Command* command; map commands; map::iterator it; + string outputDir, inputDir; static CommandFactory* _uniqueInstance; CommandFactory( const CommandFactory& ); // Disable copy constructor diff --git a/deconvolutecommand.cpp b/deconvolutecommand.cpp index a936c82..00c4084 100644 --- a/deconvolutecommand.cpp +++ b/deconvolutecommand.cpp @@ -19,24 +19,54 @@ DeconvoluteCommand::DeconvoluteCommand(string option) { else { //valid paramters for this command - string Array[] = {"fasta", "name"}; + string Array[] = {"fasta", "name","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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("fasta"); + //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["fasta"] = 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; } + } + } + + //check for required parameters inFastaName = validParameter.validFile(parameters, "fasta", true); if (inFastaName == "not open") { abort = true; } else if (inFastaName == "not found") { inFastaName = ""; mothurOut("fasta is a required parameter for the unique.seqs command."); mothurOutEndLine(); abort = true; } + //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(inFastaName); //if user entered a file with a path then preserve it + } + oldNameMapFName = validParameter.validFile(parameters, "name", true); if (oldNameMapFName == "not open") { abort = true; } else if (oldNameMapFName == "not found"){ oldNameMapFName = ""; } @@ -72,8 +102,8 @@ int DeconvoluteCommand::execute() { if (abort == true) { return 0; } //prepare filenames and open files - string outNameFile = (getRootName(inFastaName) + "names"); - string outFastaFile = (getRootName(inFastaName) + "unique" + getExtension(inFastaName)); + string outNameFile = outputDir + getRootName(getSimpleName(inFastaName)) + "names"; + string outFastaFile = outputDir + getRootName(getSimpleName(inFastaName)) + "unique" + getExtension(inFastaName); FastaMap fastamap; diff --git a/deconvolutecommand.h b/deconvolutecommand.h index 635e53f..4cb6aac 100644 --- a/deconvolutecommand.h +++ b/deconvolutecommand.h @@ -25,7 +25,7 @@ public: void help(); private: - string inFastaName, oldNameMapFName; + string inFastaName, oldNameMapFName, outputDir; bool abort; }; diff --git a/distancecommand.cpp b/distancecommand.cpp index 0422532..5364cbf 100644 --- a/distancecommand.cpp +++ b/distancecommand.cpp @@ -26,19 +26,34 @@ DistanceCommand::DistanceCommand(string option){ else { //valid paramters for this command - string Array[] = {"fasta", "phylip", "calc", "countends", "cutoff", "processors"}; + string Array[] = {"fasta", "phylip", "calc", "countends", "cutoff", "processors", "outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it2; //check to make sure all parameters are valid for command - for (map::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) { + for (it2 = parameters.begin(); it2 != parameters.end(); it2++) { if (validParameter.isValidParameter(it2->first, myArray, it2->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; + it2 = parameters.find("fasta"); + //user has given a template file + if(it2 != parameters.end()){ + path = hasPath(it2->second); + //if the user has not given a path then, add inputdir. else leave path alone. + if (path == "") { parameters["fasta"] = inputDir + it2->second; } + } + } + //check for required parameters fastafile = validParameter.validFile(parameters, "fasta", true); if (fastafile == "not found") { mothurOut("fasta is a required parameter for the dist.seqs command."); mothurOutEndLine(); abort = true; } @@ -49,6 +64,12 @@ DistanceCommand::DistanceCommand(string option){ alignDB = SequenceDB(inFASTA); inFASTA.close(); } + + //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(fastafile); //if user entered a file with a path then preserve it + } //check for optional parameter and set defaults // ...at some point should added some additional type checking... @@ -146,21 +167,15 @@ int DistanceCommand::execute(){ //doses the user want the phylip formatted file as well if (isTrue(phylip) == true) { - outputFile = getRootName(fastafile) + "phylip.dist"; + outputFile = outputDir + getRootName(getSimpleName(fastafile)) + "phylip.dist"; remove(outputFile.c_str()); //output numSeqs to phylip formatted dist file }else { //user wants column format - outputFile = getRootName(fastafile) + "dist"; + outputFile = outputDir + getRootName(getSimpleName(fastafile)) + "dist"; remove(outputFile.c_str()); } - //# if defined (_WIN32) - //figure out how to implement the fork and wait commands in windows - // driver(distCalculator, seqDB, 0, numSeqs, distFile, phylipFile, cutoff); - //# endif - - #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) //if you don't need to fork anything if(processors == 1){ diff --git a/distancecommand.h b/distancecommand.h index 542f428..c7aaf46 100644 --- a/distancecommand.h +++ b/distancecommand.h @@ -34,7 +34,7 @@ private: Dist* distCalculator; SequenceDB alignDB; - string countends, phylip, fastafile, calc; + string countends, phylip, fastafile, calc, outputDir; int processors; float cutoff; map processIDS; //end line, processid diff --git a/engine.hpp b/engine.hpp index d3f156e..cbcc99a 100644 --- a/engine.hpp +++ b/engine.hpp @@ -26,7 +26,8 @@ public: virtual ~Engine(){} virtual bool getInput() = 0; virtual string getCommand(); - vector getOptions() { return options; } + virtual string getOutputDir() { return cFactory->getOutputDir(); } + vector getOptions() { return options; } protected: vector options; CommandFactory* cFactory; diff --git a/filterseqscommand.cpp b/filterseqscommand.cpp index a689704..8c61ab2 100644 --- a/filterseqscommand.cpp +++ b/filterseqscommand.cpp @@ -21,23 +21,52 @@ FilterSeqsCommand::FilterSeqsCommand(string option){ else { //valid paramters for this command - string Array[] = {"fasta", "trump", "soft", "hard", "vertical"}; + string Array[] = {"fasta", "trump", "soft", "hard", "vertical", "outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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("fasta"); + //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["fasta"] = inputDir + it->second; } + } + + it = parameters.find("hard"); + //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["hard"] = inputDir + it->second; } + } + } + //check for required parameters fastafile = validParameter.validFile(parameters, "fasta", true); if (fastafile == "not found") { mothurOut("fasta is a required parameter for the filter.seqs command."); mothurOutEndLine(); abort = true; } else if (fastafile == "not open") { abort = true; } + + //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(fastafile); //if user entered a file with a path then preserve it + } //check for optional parameter and set defaults // ...at some point should added some additional type checking... @@ -140,14 +169,14 @@ int FilterSeqsCommand::execute() { filter = F.getFilter(); ofstream outFilter; - string filterFile = getRootName(fastafile) + "filter"; + string filterFile = outputDir + getRootName(getSimpleName(fastafile)) + "filter"; openOutputFile(filterFile, outFilter); outFilter << filter << endl; outFilter.close(); ifstream inFasta2; openInputFile(fastafile, inFasta2); - string filteredFasta = getRootName(fastafile) + "filter.fasta"; + string filteredFasta = outputDir + getRootName(getSimpleName(fastafile)) + "filter.fasta"; ofstream outFASTA; openOutputFile(filteredFasta, outFASTA); diff --git a/filterseqscommand.h b/filterseqscommand.h index 611cbbc..0946396 100644 --- a/filterseqscommand.h +++ b/filterseqscommand.h @@ -23,7 +23,7 @@ public: void help(); private: - string vertical, filter, fastafile, hard; + string vertical, filter, fastafile, hard, outputDir; int alignmentLength; char trump; diff --git a/getgroupcommand.cpp b/getgroupcommand.cpp index 9014031..c754f8e 100644 --- a/getgroupcommand.cpp +++ b/getgroupcommand.cpp @@ -19,7 +19,21 @@ GetgroupCommand::GetgroupCommand(string option){ if(option == "help") { help(); abort = true; } else { - if (option != "") { mothurOut("There are no valid parameters for the get.group command."); mothurOutEndLine(); abort = true; } + //valid paramters for this command + string Array[] = {"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; } + } + + //if the user changes the output directory command factory will send this info to us in the output parameter + string outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; } if ((globaldata->getSharedFile() == "")) { mothurOut("You must use the read.otu command to read a groupfile or a sharedfile before you can use the get.group command."); mothurOutEndLine(); abort = true; } @@ -29,7 +43,8 @@ GetgroupCommand::GetgroupCommand(string option){ openInputFile(sharedfile, in); //open output file - outputFile = getRootName(sharedfile) + "bootGroups"; + if (outputDir == "") { outputDir += hasPath(sharedfile); } + outputFile = outputDir + getRootName(getSimpleName(sharedfile)) + "bootGroups"; openOutputFile(outputFile, out); } diff --git a/getlabelcommand.cpp b/getlabelcommand.cpp index be2a989..2ba3054 100644 --- a/getlabelcommand.cpp +++ b/getlabelcommand.cpp @@ -20,8 +20,6 @@ GetlabelCommand::GetlabelCommand(string option){ if(option == "help") { help(); abort = true; } else { - if (option != "") { mothurOut("There are no valid parameters for the get.label command."); mothurOutEndLine(); abort = true; } - if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "")) { mothurOut("You must read a list, sabund or rabund before you can use the get.label command."); mothurOutEndLine(); abort = true; } } diff --git a/getlinecommand.cpp b/getlinecommand.cpp deleted file mode 100644 index f1ec55c..0000000 --- a/getlinecommand.cpp +++ /dev/null @@ -1,90 +0,0 @@ -/* - * GetlineCommand.cpp - * Mothur - * - * Created by Thomas Ryabin on 1/30/09. - * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved. - * - */ - -#include "getlinecommand.h" - -//********************************************************************************************************************** -GetlineCommand::GetlineCommand(string option){ - try { - globaldata = GlobalData::getInstance(); - abort = false; - - //allow user to run help - if(option == "help") { help(); abort = true; } - - else { - if (option != "") { mothurOut("There are no valid parameters for the get.line command."); mothurOutEndLine(); abort = true; } - - if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "")) { mothurOut("You must read a list, sabund or rabund before you can use the get.line command."); mothurOutEndLine(); abort = true; } - } - - } - catch(exception& e) { - errorOut(e, "GetlineCommand", "GetlineCommand"); - exit(1); - } -} -//********************************************************************************************************************** - -void GetlineCommand::help(){ - try { - mothurOut("The get.line command can only be executed after a successful read.otu command.\n"); - mothurOut("You may not use any parameters with the get.line command.\n"); - mothurOut("The get.line command should be in the following format: \n"); - mothurOut("get.line()\n"); - mothurOut("Example get.line().\n"); - } - catch(exception& e) { - errorOut(e, "GetlineCommand", "help"); - exit(1); - } -} - -//********************************************************************************************************************** - -GetlineCommand::~GetlineCommand(){ -} - -//********************************************************************************************************************** - -int GetlineCommand::execute(){ - try { - - if (abort == true) { return 0; } - - ifstream in; - openInputFile(globaldata->inputFileName, in); - string label; - int numBins = 0; - int count = -1; - int line = 1; - while(in.good()) { - if(count > numBins) - count = 0; - if(count == 0) { - mothurOut(toString(line)); mothurOutEndLine(); - in >> numBins; - line++; - } - in >> label; - count++; - } - - in.close(); - return 0; - } - - catch(exception& e) { - errorOut(e, "GetlineCommand", "execute"); - exit(1); - } -} - - - diff --git a/getlinecommand.h b/getlinecommand.h deleted file mode 100644 index 68870b3..0000000 --- a/getlinecommand.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef GETLINECOMMAND_H -#define GETLINECOMMAND_H - -/* - * getlinecommand.h - * Mothur - * - * Created by Thomas Ryabin on 1/30/09. - * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved. - * - */ - -#include "command.hpp" -#include "readmatrix.hpp" - -class GlobalData; - -class GetlineCommand : public Command { -public: - GetlineCommand(string); - ~GetlineCommand(); - int execute(); - void help(); - -private: - GlobalData* globaldata; - bool abort; -}; - -#endif diff --git a/getlistcountcommand.cpp b/getlistcountcommand.cpp index 94437fd..fcc42ce 100644 --- a/getlistcountcommand.cpp +++ b/getlistcountcommand.cpp @@ -22,21 +22,39 @@ GetListCountCommand::GetListCountCommand(string option){ else { //valid paramters for this command - string AlignArray[] = {"list","label"}; + string AlignArray[] = {"list","label","outputdir","inputdir"}; vector myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; - + map::iterator it; + //check to make sure all parameters are valid for command - for (map::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; } } string ranRead = globaldata->getListFile(); + //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("list"); + //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["list"] = inputDir + it->second; } + } + } + + //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 = ""; } + //check for required parameters listfile = validParameter.validFile(parameters, "list", true); if ((listfile == "not found") && (globaldata->getListFile() == "")) { mothurOut("You must read a listfile before running the get.listcount command."); mothurOutEndLine(); abort = true; } @@ -179,7 +197,8 @@ int GetListCountCommand::execute(){ void GetListCountCommand::process(ListVector* list) { try { string binnames, name, sequence; - string outputFileName = getRootName(listfile) + list->getLabel() + ".otu"; + if (outputDir == "") { outputDir += hasPath(listfile); } + string outputFileName = outputDir + getRootName(getSimpleName(listfile)) + list->getLabel() + ".otu"; openOutputFile(outputFileName, out); mothurOut(list->getLabel()); mothurOutEndLine(); diff --git a/getlistcountcommand.h b/getlistcountcommand.h index 468ea80..bd9d202 100644 --- a/getlistcountcommand.h +++ b/getlistcountcommand.h @@ -34,7 +34,7 @@ private: bool abort, allLines; set labels; //holds labels to be used - string label, listfile; + string label, listfile, outputDir; ofstream out; void process(ListVector*); diff --git a/getoturepcommand.cpp b/getoturepcommand.cpp index d37ee19..6b9cee9 100644 --- a/getoturepcommand.cpp +++ b/getoturepcommand.cpp @@ -48,19 +48,78 @@ GetOTURepCommand::GetOTURepCommand(string option){ help(); abort = true; } else { //valid paramters for this command - string Array[] = {"fasta","list","label","name", "group", "sorted", "phylip","column","large","cutoff","precision"}; + string Array[] = {"fasta","list","label","name", "group", "sorted", "phylip","column","large","cutoff","precision","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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("list"); + //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["list"] = inputDir + it->second; } + } + + it = parameters.find("fasta"); + //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["fasta"] = inputDir + it->second; } + } + + 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; } + } + + it = parameters.find("group"); + //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["group"] = inputDir + it->second; } + } + } + + + //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 = ""; } + //check for required parameters fastafile = validParameter.validFile(parameters, "fasta", true); if (fastafile == "not found") { mothurOut("fasta is a required parameter for the get.oturep command."); mothurOutEndLine(); abort = true; } @@ -104,7 +163,8 @@ GetOTURepCommand::GetOTURepCommand(string option){ else { //read in group map info. groupMap = new GroupMap(groupfile); - groupMap->readMap(); + int error = groupMap->readMap(); + if (error == 1) { delete groupMap; abort = true; } } sorted = validParameter.validFile(parameters, "sorted", false); if (sorted == "not found"){ sorted = ""; } @@ -490,12 +550,13 @@ int GetOTURepCommand::process(ListVector* processList) { string nameRep, name, sequence; //create output file - string outputFileName = getRootName(listfile) + processList->getLabel() + ".rep.fasta"; + if (outputDir == "") { outputDir += hasPath(listfile); } + string outputFileName = outputDir + getRootName(getSimpleName(listfile)) + processList->getLabel() + ".rep.fasta"; openOutputFile(outputFileName, out); vector reps; ofstream newNamesOutput; - string outputNamesFile = getRootName(listfile) + processList->getLabel() + ".rep.names"; + string outputNamesFile = outputDir + getRootName(getSimpleName(listfile)) + processList->getLabel() + ".rep.names"; openOutputFile(outputNamesFile, newNamesOutput); //for each bin in the list vector diff --git a/getoturepcommand.h b/getoturepcommand.h index 0e8a121..ceea898 100644 --- a/getoturepcommand.h +++ b/getoturepcommand.h @@ -53,7 +53,7 @@ private: ReadMatrix* readMatrix; FormatMatrix* formatMatrix; NameAssignment* nameMap; - string filename, fastafile, listfile, namefile, groupfile, label, sorted, phylipfile, columnfile, distFile, format; + string filename, fastafile, listfile, namefile, groupfile, label, sorted, phylipfile, columnfile, distFile, format, outputDir; ofstream out; ifstream in, inNames, inRow; bool abort, allLines, groupError, large; diff --git a/getrabundcommand.cpp b/getrabundcommand.cpp index 2ed5b7b..df9754b 100644 --- a/getrabundcommand.cpp +++ b/getrabundcommand.cpp @@ -23,7 +23,7 @@ GetRAbundCommand::GetRAbundCommand(string option){ else { //valid paramters for this command - string Array[] = {"label","sorted"}; + string Array[] = {"label","sorted","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); @@ -36,6 +36,12 @@ GetRAbundCommand::GetRAbundCommand(string option){ if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } } + //if the user changes the output directory command factory will send this info to us in the output parameter + string 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 + } + //make sure the user has already run the read.otu command if (globaldata->getListFile() == "") { mothurOut("You must read a listfile before you can use the get.rabund command."); mothurOutEndLine(); abort = true; } @@ -60,7 +66,7 @@ GetRAbundCommand::GetRAbundCommand(string option){ } if (abort == false) { - filename = getRootName(globaldata->inputFileName) + "rabund"; + filename = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + "rabund"; openOutputFile(filename, out); } } diff --git a/getsabundcommand.cpp b/getsabundcommand.cpp index 3dd58c1..10b998e 100644 --- a/getsabundcommand.cpp +++ b/getsabundcommand.cpp @@ -23,7 +23,7 @@ GetSAbundCommand::GetSAbundCommand(string option){ else { //valid paramters for this command - string Array[] = {"label"}; + string Array[] = {"label","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); @@ -36,6 +36,12 @@ GetSAbundCommand::GetSAbundCommand(string option){ if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } } + //if the user changes the output directory command factory will send this info to us in the output parameter + string 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 + } + //make sure the user has already run the read.otu command if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "")) { mothurOut("You must read a list or rabund before you can use the get.sabund command."); mothurOutEndLine(); abort = true; } @@ -55,7 +61,7 @@ GetSAbundCommand::GetSAbundCommand(string option){ } if (abort == false) { - filename = getRootName(globaldata->inputFileName) + "sabund"; + filename = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + "sabund"; openOutputFile(filename, out); } } diff --git a/getseqscommand.cpp b/getseqscommand.cpp index 03ccb82..d63dc79 100644 --- a/getseqscommand.cpp +++ b/getseqscommand.cpp @@ -22,19 +22,78 @@ GetSeqsCommand::GetSeqsCommand(string option){ else { //valid paramters for this command - string Array[] = {"fasta","name", "group", "alignreport", "accnos", "list"}; + string Array[] = {"fasta","name", "group", "alignreport", "accnos", "list","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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 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 = ""; } + + //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("alignreport"); + //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["alignreport"] = inputDir + it->second; } + } + + it = parameters.find("fasta"); + //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["fasta"] = inputDir + it->second; } + } + + it = parameters.find("accnos"); + //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["accnos"] = inputDir + it->second; } + } + + it = parameters.find("list"); + //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["list"] = 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; } + } + + it = parameters.find("group"); + //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["group"] = inputDir + it->second; } + } + } + + //check for required parameters accnosfile = validParameter.validFile(parameters, "accnos", true); if (accnosfile == "not open") { abort = true; } @@ -62,7 +121,10 @@ GetSeqsCommand::GetSeqsCommand(string option){ if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == "")) { mothurOut("You must provide one of the following: fasta, name, group, alignreport or listfile."); mothurOutEndLine(); abort = true; } - if (parameters.size() > 2) { mothurOut("You may only enter one of the following: fasta, name, group, alignreport or listfile."); mothurOutEndLine(); abort = true; } + int okay = 2; + if (outputDir != "") { okay++; } + + if (parameters.size() > okay) { mothurOut("You may only enter one of the following: fasta, name, group, alignreport or listfile."); mothurOutEndLine(); abort = true; } } } @@ -117,7 +179,8 @@ int GetSeqsCommand::execute(){ //********************************************************************************************************************** void GetSeqsCommand::readFasta(){ try { - string outputFileName = getRootName(fastafile) + "pick" + getExtension(fastafile); + if (outputDir == "") { outputDir += hasPath(fastafile); } + string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "pick" + getExtension(fastafile); ofstream out; openOutputFile(outputFileName, out); @@ -160,7 +223,8 @@ void GetSeqsCommand::readFasta(){ //********************************************************************************************************************** void GetSeqsCommand::readList(){ try { - string outputFileName = getRootName(listfile) + "pick" + getExtension(listfile); + if (outputDir == "") { outputDir += hasPath(listfile); } + string outputFileName = outputDir + getRootName(getSimpleName(listfile)) + "pick" + getExtension(listfile); ofstream out; openOutputFile(outputFileName, out); @@ -224,8 +288,8 @@ void GetSeqsCommand::readList(){ //********************************************************************************************************************** void GetSeqsCommand::readName(){ try { - - string outputFileName = getRootName(namefile) + "pick" + getExtension(namefile); + if (outputDir == "") { outputDir += hasPath(namefile); } + string outputFileName = outputDir + getRootName(getSimpleName(namefile)) + "pick" + getExtension(namefile); ofstream out; openOutputFile(outputFileName, out); @@ -307,8 +371,8 @@ void GetSeqsCommand::readName(){ //********************************************************************************************************************** void GetSeqsCommand::readGroup(){ try { - - string outputFileName = getRootName(groupfile) + "pick" + getExtension(groupfile); + if (outputDir == "") { outputDir += hasPath(groupfile); } + string outputFileName = outputDir + getRootName(getSimpleName(groupfile)) + "pick" + getExtension(groupfile); ofstream out; openOutputFile(outputFileName, out); @@ -353,7 +417,8 @@ void GetSeqsCommand::readGroup(){ //alignreport file has a column header line then all other lines contain 16 columns. we just want the first column since that contains the name void GetSeqsCommand::readAlign(){ try { - string outputFileName = getRootName(getRootName(alignfile)) + "pick.align.report"; + if (outputDir == "") { outputDir += hasPath(alignfile); } + string outputFileName = outputDir + getRootName(getSimpleName(alignfile)) + "pick.align.report"; ofstream out; openOutputFile(outputFileName, out); diff --git a/getseqscommand.h b/getseqscommand.h index 8b38a76..24f3968 100644 --- a/getseqscommand.h +++ b/getseqscommand.h @@ -23,7 +23,7 @@ class GetSeqsCommand : public Command { private: set names; - string accnosfile, fastafile, namefile, groupfile, alignfile, listfile; + string accnosfile, fastafile, namefile, groupfile, alignfile, listfile, outputDir; bool abort; void readFasta(); diff --git a/getsharedotucommand.cpp b/getsharedotucommand.cpp index f224b47..7af1ac6 100644 --- a/getsharedotucommand.cpp +++ b/getsharedotucommand.cpp @@ -25,19 +25,54 @@ GetSharedOTUCommand::GetSharedOTUCommand(string option){ else { //valid paramters for this command - string Array[] = {"label","unique","shared","fasta","list","group","output"}; + string Array[] = {"label","unique","shared","fasta","list","group","output","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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 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 = ""; } + + //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("fasta"); + //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["fasta"] = inputDir + it->second; } + } + + it = parameters.find("list"); + //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["list"] = inputDir + it->second; } + } + + it = parameters.find("group"); + //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["group"] = inputDir + it->second; } + } + } + + //check for required parameters listfile = validParameter.validFile(parameters, "list", true); if (listfile == "not open") { abort = true; } @@ -65,6 +100,7 @@ GetSharedOTUCommand::GetSharedOTUCommand(string option){ groups = validParameter.validFile(parameters, "unique", false); if (groups == "not found") { groups = ""; } else { + userGroups = "unique." + groups; splitAtDash(groups, Groups); globaldata->Groups = Groups; @@ -73,6 +109,7 @@ GetSharedOTUCommand::GetSharedOTUCommand(string option){ groups = validParameter.validFile(parameters, "shared", false); if (groups == "not found") { groups = ""; } else { + userGroups = groups; splitAtDash(groups, Groups); globaldata->Groups = Groups; unique = false; @@ -128,11 +165,18 @@ int GetSharedOTUCommand::execute(){ if (abort == true) { return 0; } groupMap = new GroupMap(groupfile); - groupMap->readMap(); + int error = groupMap->readMap(); + if (error == 1) { delete groupMap; return 0; } + globaldata->gGroupmap = groupMap; if (Groups.size() == 0) { Groups = groupMap->namesOfGroups; + + //make string for outputfile name + userGroups = "unique."; + for(int i = 0; i < Groups.size(); i++) { userGroups += Groups[i] + "-"; } + userGroups = userGroups.substr(0, userGroups.length()-1); } //put groups in map to find easier @@ -238,10 +282,12 @@ void GetSharedOTUCommand::process(ListVector* shared) { ofstream outNames; string outputFileNames; + + if (outputDir == "") { outputDir += hasPath(listfile); } if (output != "accnos") { - outputFileNames = getRootName(listfile) + shared->getLabel() + ".shared.seqs"; + outputFileNames = outputDir + getRootName(getSimpleName(listfile)) + shared->getLabel() + userGroups + ".shared.seqs"; }else { - outputFileNames = getRootName(listfile) + shared->getLabel() + ".accnos"; + outputFileNames = outputDir + getRootName(getSimpleName(listfile)) + shared->getLabel() + userGroups + ".accnos"; } openOutputFile(outputFileNames, outNames); @@ -340,8 +386,9 @@ void GetSharedOTUCommand::process(ListVector* shared) { }else { mothurOut("\t" + toString(num)); mothurOutEndLine(); } //if fasta file provided output new fasta file - if ((fastafile != "") && wroteSomething) { - string outputFileFasta = getRootName(fastafile) + shared->getLabel() + ".shared.fasta"; + if ((fastafile != "") && wroteSomething) { + if (outputDir == "") { outputDir += hasPath(fastafile); } + string outputFileFasta = outputDir + getRootName(getSimpleName(fastafile)) + shared->getLabel() + userGroups + ".shared.fasta"; ofstream outFasta; openOutputFile(outputFileFasta, outFasta); diff --git a/getsharedotucommand.h b/getsharedotucommand.h index 3b14b39..5b5516c 100644 --- a/getsharedotucommand.h +++ b/getsharedotucommand.h @@ -33,7 +33,7 @@ class GetSharedOTUCommand : public Command { GroupMap* groupMap; set labels; - string fastafile, label, groups, listfile, groupfile, output; + string fastafile, label, groups, listfile, groupfile, output, userGroups, outputDir; bool abort, allLines, unique; vector Groups; map groupFinder; diff --git a/groupmap.cpp b/groupmap.cpp index b627a6b..598e301 100644 --- a/groupmap.cpp +++ b/groupmap.cpp @@ -21,8 +21,9 @@ GroupMap::~GroupMap(){} /************************************************************/ -void GroupMap::readMap() { +int GroupMap::readMap() { string seqName, seqGroup; + int error = 0; while(fileHandle){ fileHandle >> seqName; //read from first column @@ -30,12 +31,17 @@ void GroupMap::readMap() { setNamesOfGroups(seqGroup); - groupmap[seqName] = seqGroup; //store data in map - seqsPerGroup[seqGroup]++; //increment number of seqs in that group - + it = groupmap.find(seqName); + + if (it != groupmap.end()) { error = 1; mothurOut("Your groupfile contains more than 1 sequence named " + seqName + ", sequence names must be unique. Please correct."); mothurOutEndLine(); } + else { + groupmap[seqName] = seqGroup; //store data in map + seqsPerGroup[seqGroup]++; //increment number of seqs in that group + } gobble(fileHandle); } fileHandle.close(); + return error; } /************************************************************/ int GroupMap::getNumGroups() { return namesOfGroups.size(); } diff --git a/groupmap.h b/groupmap.h index fd6c146..a99ad11 100644 --- a/groupmap.h +++ b/groupmap.h @@ -19,7 +19,7 @@ public: GroupMap() {}; GroupMap(string); ~GroupMap(); - void readMap(); + int readMap(); int getNumGroups(); bool isValidGroup(string); //return true if string is a valid group string getGroup(string); diff --git a/hclustercommand.cpp b/hclustercommand.cpp index c39b002..365c80e 100644 --- a/hclustercommand.cpp +++ b/hclustercommand.cpp @@ -21,16 +21,17 @@ HClusterCommand::HClusterCommand(string option){ else { //valid paramters for this command - string Array[] = {"cutoff","precision","method","phylip","column","name","sorted","showabund","timing"}; + string Array[] = {"cutoff","precision","method","phylip","column","name","sorted","showabund","timing","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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; } @@ -38,6 +39,39 @@ HClusterCommand::HClusterCommand(string option){ globaldata->newRead(); + //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; } + } + } + + //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 = ""; } + //check for required parameters phylipfile = validParameter.validFile(parameters, "phylip", true); if (phylipfile == "not open") { abort = true; } @@ -93,8 +127,9 @@ HClusterCommand::HClusterCommand(string option){ if (abort == false) { - - fileroot = getRootName(distfile); + + if (outputDir == "") { outputDir += hasPath(distfile); } + fileroot = outputDir + getRootName(getSimpleName(distfile)); if (method == "furthest") { tag = "fn"; } else if (method == "nearest") { tag = "nn"; } diff --git a/hclustercommand.h b/hclustercommand.h index 4c8fa9c..443d870 100644 --- a/hclustercommand.h +++ b/hclustercommand.h @@ -47,7 +47,7 @@ private: ReadCluster* read; bool abort, sorted, print_start; - string method, fileroot, tag, distfile, format, phylipfile, columnfile, namefile, sort, showabund, timing; + string method, fileroot, tag, distfile, format, phylipfile, columnfile, namefile, sort, showabund, timing, outputDir; double cutoff; int precision, length; ofstream sabundFile, rabundFile, listFile; diff --git a/heatmap.cpp b/heatmap.cpp index e4e947e..2bd020e 100644 --- a/heatmap.cpp +++ b/heatmap.cpp @@ -10,12 +10,13 @@ #include "heatmap.h" //********************************************************************************************************************** -HeatMap::HeatMap(string sort, string scale){ +HeatMap::HeatMap(string sort, string scale, string dir){ try { globaldata = GlobalData::getInstance(); // format = globaldata->getFormat(); sorted = sort; scaler = scale; + outputDir = dir; } catch(exception& e) { errorOut(e, "HeatMap", "HeatMap"); @@ -57,7 +58,7 @@ void HeatMap::getPic(RAbundVector* rabund) { } - string filenamesvg = getRootName(globaldata->inputFileName) + rabund->getLabel() + ".heatmap.bin.svg"; + string filenamesvg = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + rabund->getLabel() + ".heatmap.bin.svg"; openOutputFile(filenamesvg, outsvg); //svg image @@ -133,7 +134,7 @@ void HeatMap::getPic(vector lookup) { } } - string filenamesvg = getRootName(globaldata->inputFileName) + lookup[0]->getLabel() + ".heatmap.bin.svg"; + string filenamesvg = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + lookup[0]->getLabel() + ".heatmap.bin.svg"; openOutputFile(filenamesvg, outsvg); //svg image diff --git a/heatmap.h b/heatmap.h index 56a56e4..438d84b 100644 --- a/heatmap.h +++ b/heatmap.h @@ -19,7 +19,7 @@ class HeatMap { public: - HeatMap(string, string); + HeatMap(string, string, string); ~HeatMap(){}; void getPic(RAbundVector*); @@ -30,7 +30,7 @@ class HeatMap { void printLegend(int, float); GlobalData* globaldata; - string format, sorted, groupComb, scaler; + string format, sorted, groupComb, scaler, outputDir; ofstream outsvg; }; diff --git a/heatmapcommand.cpp b/heatmapcommand.cpp index 1992ce5..04f1245 100644 --- a/heatmapcommand.cpp +++ b/heatmapcommand.cpp @@ -24,7 +24,7 @@ HeatMapCommand::HeatMapCommand(string option){ else { //valid paramters for this command - string AlignArray[] = {"groups","label","sorted","scale"}; + string AlignArray[] = {"groups","label","sorted","scale","outputdir","inputdir"}; vector myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string))); OptionParser parser(option); @@ -37,6 +37,12 @@ HeatMapCommand::HeatMapCommand(string option){ if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } } + //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 + } + //make sure the user has already run the read.otu command if ((globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "") && (globaldata->getSharedFile() == "")) { mothurOut("You must read a list, rabund, sabund, or a list and a group, or a shared before you can use the heatmap.bin command."); mothurOutEndLine(); abort = true; @@ -69,7 +75,7 @@ HeatMapCommand::HeatMapCommand(string option){ scale = validParameter.validFile(parameters, "scale", false); if (scale == "not found") { scale = "log10"; } if (abort == false) { - heatmap = new HeatMap(sorted, scale); + heatmap = new HeatMap(sorted, scale, outputDir); format = globaldata->getFormat(); } } diff --git a/heatmapcommand.h b/heatmapcommand.h index 5c00b28..83f6c0f 100644 --- a/heatmapcommand.h +++ b/heatmapcommand.h @@ -39,7 +39,7 @@ private: bool abort, allLines; set labels; //holds labels to be used - string format, groups, sorted, scale, label; + string format, groups, sorted, scale, label, outputDir; vector Groups; diff --git a/heatmapsim.cpp b/heatmapsim.cpp index c68d0cc..b29e98b 100644 --- a/heatmapsim.cpp +++ b/heatmapsim.cpp @@ -20,7 +20,7 @@ #include "sharedbraycurtis.h" //********************************************************************************************************************** -HeatMapSim::HeatMapSim(){ +HeatMapSim::HeatMapSim(string dir) : outputDir(dir) { globaldata = GlobalData::getInstance(); } //********************************************************************************************************************** @@ -32,7 +32,7 @@ void HeatMapSim::getPic(vector lookup, vector //make file for each calculator selected for (int m = 0; m < calcs.size(); m++) { - string filenamesvg = getRootName(globaldata->inputFileName) + lookup[0]->getLabel() + calcs[m]->getName() + ".heatmap.sim.svg"; + string filenamesvg = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + lookup[0]->getLabel() + calcs[m]->getName() + ".heatmap.sim.svg"; openOutputFile(filenamesvg, outsvg); //svg image @@ -107,7 +107,7 @@ void HeatMapSim::getPic(vector< vector > dists, vector groups) { vector sims; - string filenamesvg = getRootName(globaldata->inputFileName) + "heatmap.sim.svg"; + string filenamesvg = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + "heatmap.sim.svg"; openOutputFile(filenamesvg, outsvg); //svg image diff --git a/heatmapsim.h b/heatmapsim.h index d38683c..ffafb7d 100644 --- a/heatmapsim.h +++ b/heatmapsim.h @@ -20,7 +20,7 @@ class HeatMapSim { public: - HeatMapSim(); + HeatMapSim(string); ~HeatMapSim(){}; void getPic(vector, vector); @@ -30,7 +30,7 @@ class HeatMapSim { void printLegend(int, float); GlobalData* globaldata; - string format, groupComb; + string format, groupComb, outputDir; ofstream outsvg; }; diff --git a/heatmapsimcommand.cpp b/heatmapsimcommand.cpp index 3287e62..cef7906 100644 --- a/heatmapsimcommand.cpp +++ b/heatmapsimcommand.cpp @@ -36,31 +36,64 @@ HeatMapSimCommand::HeatMapSimCommand(string option){ else { //valid paramters for this command - string AlignArray[] = {"groups","label", "calc","phylip","column","name"}; + string AlignArray[] = {"groups","label", "calc","phylip","column","name","outputdir","inputdir"}; vector myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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; } } format = ""; + //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 = ""; } + //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; } + } + } + //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"; if (outputDir == "") { outputDir += hasPath(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"; if (outputDir == "") { outputDir += hasPath(columnfile); } } namefile = validParameter.validFile(parameters, "name", true); if (namefile == "not open") { abort = true; } @@ -80,6 +113,8 @@ HeatMapSimCommand::HeatMapSimCommand(string option){ //check for optional parameter and set defaults // ...at some point should added some additional type checking... if (format == "shared") { + if (outputDir == "") { outputDir += hasPath(globaldata->getSharedFile()); } + label = validParameter.validFile(parameters, "label", false); if (label == "not found") { label = ""; } else { @@ -190,7 +225,7 @@ int HeatMapSimCommand::execute(){ if (abort == true) { return 0; } - heatmap = new HeatMapSim(); + heatmap = new HeatMapSim(outputDir); if (format == "shared") { runCommandShared(); diff --git a/heatmapsimcommand.h b/heatmapsimcommand.h index 49924eb..838babd 100644 --- a/heatmapsimcommand.h +++ b/heatmapsimcommand.h @@ -39,7 +39,7 @@ private: map::iterator it; bool abort, allLines; set labels; //holds labels to be used - string format, groups, label, calc, phylipfile, columnfile, namefile; + string format, groups, label, calc, phylipfile, columnfile, namefile, outputDir; vector Estimators, Groups; int runCommandShared(); diff --git a/helpcommand.cpp b/helpcommand.cpp index e0ef2a3..948c317 100644 --- a/helpcommand.cpp +++ b/helpcommand.cpp @@ -12,9 +12,6 @@ //********************************************************************************************************************** HelpCommand::HelpCommand(string option){ - - - if (option != "") { mothurOut("There are no valid parameters for the help() command."); mothurOutEndLine(); } validCommands = CommandFactory::getInstance(); } diff --git a/libshuffcommand.cpp b/libshuffcommand.cpp index 74acd84..cfccdf6 100644 --- a/libshuffcommand.cpp +++ b/libshuffcommand.cpp @@ -32,7 +32,7 @@ LibShuffCommand::LibShuffCommand(string option){ else { //valid paramters for this command - string Array[] = {"iters","groups","step","form","cutoff"}; + string Array[] = {"iters","groups","step","form","cutoff","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); @@ -45,6 +45,12 @@ LibShuffCommand::LibShuffCommand(string option){ if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } } + //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->getPhylipFile()); //if user entered a file with a path then preserve it + } + //make sure the user has already run the read.dist command if ((globaldata->gMatrix == NULL) || (globaldata->gGroupmap == NULL)) { mothurOut("You must read in a matrix and groupfile using the read.dist command, before you use the libshuff command. "); mothurOutEndLine(); abort = true;; @@ -186,7 +192,7 @@ void LibShuffCommand::printCoverageFile() { try { ofstream outCov; - summaryFile = getRootName(globaldata->getPhylipFile()) + "libshuff.coverage"; + summaryFile = outputDir + getRootName(getSimpleName(globaldata->getPhylipFile())) + "libshuff.coverage"; openOutputFile(summaryFile, outCov); outCov.setf(ios::fixed, ios::floatfield); outCov.setf(ios::showpoint); //cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint); @@ -273,7 +279,7 @@ void LibShuffCommand::printSummaryFile() { try { ofstream outSum; - summaryFile = getRootName(globaldata->getPhylipFile()) + "libshuff.summary"; + summaryFile = outputDir + getRootName(getSimpleName(globaldata->getPhylipFile())) + "libshuff.summary"; openOutputFile(summaryFile, outSum); outSum.setf(ios::fixed, ios::floatfield); outSum.setf(ios::showpoint); diff --git a/libshuffcommand.h b/libshuffcommand.h index 1e5726d..da5f9e8 100644 --- a/libshuffcommand.h +++ b/libshuffcommand.h @@ -43,7 +43,7 @@ private: vector > > savedMinValues; bool abort; - string outputFile, groups, userform, savegroups; + string outputFile, groups, userform, savegroups, outputDir; vector Groups; //holds groups to be used }; diff --git a/listseqscommand.cpp b/listseqscommand.cpp index 3087b48..d0b00bd 100644 --- a/listseqscommand.cpp +++ b/listseqscommand.cpp @@ -9,6 +9,7 @@ #include "listseqscommand.h" #include "sequence.hpp" +#include "listvector.hpp" //********************************************************************************************************************** @@ -21,19 +22,69 @@ ListSeqsCommand::ListSeqsCommand(string option){ else { //valid paramters for this command - string Array[] = {"fasta","name", "group", "alignreport" }; + string Array[] = {"fasta","name", "group", "alignreport","list","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //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; } } + //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 = ""; } + + //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("alignreport"); + //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["alignreport"] = inputDir + it->second; } + } + + it = parameters.find("fasta"); + //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["fasta"] = inputDir + it->second; } + } + + it = parameters.find("list"); + //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["list"] = 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; } + } + + it = parameters.find("group"); + //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["group"] = inputDir + it->second; } + } + } + //check for required parameters fastafile = validParameter.validFile(parameters, "fasta", true); if (fastafile == "not open") { abort = true; } @@ -51,9 +102,17 @@ ListSeqsCommand::ListSeqsCommand(string option){ if (alignfile == "not open") { abort = true; } else if (alignfile == "not found") { alignfile = ""; } - if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "")) { mothurOut("You must provide a file."); mothurOutEndLine(); abort = true; } + listfile = validParameter.validFile(parameters, "list", true); + if (listfile == "not open") { abort = true; } + else if (listfile == "not found") { listfile = ""; } + + + if ((fastafile == "") && (namefile == "") && (listfile == "") && (groupfile == "") && (alignfile == "")) { mothurOut("You must provide a file."); mothurOutEndLine(); abort = true; } + + int okay = 1; + if (outputDir != "") { okay++; } - if (parameters.size() > 1) { mothurOut("You may only enter one file."); mothurOutEndLine(); abort = true; } + if (parameters.size() > okay) { mothurOut("You may only enter one file."); mothurOutEndLine(); abort = true; } } } @@ -66,7 +125,7 @@ ListSeqsCommand::ListSeqsCommand(string option){ void ListSeqsCommand::help(){ try { - mothurOut("The list.seqs command reads a fasta, name, group or alignreport file and outputs a .accnos file containing sequence names.\n"); + mothurOut("The list.seqs command reads a fasta, name, group, list or alignreport file and outputs a .accnos file containing sequence names.\n"); mothurOut("The list.seqs command parameters are fasta, name, group and alignreport. You must provide one of these parameters.\n"); mothurOut("The list.seqs command should be in the following format: list.seqs(fasta=yourFasta).\n"); mothurOut("Example list.seqs(fasta=amazon.fasta).\n"); @@ -90,11 +149,15 @@ int ListSeqsCommand::execute(){ else if (namefile != "") { inputFileName = namefile; readName(); } else if (groupfile != "") { inputFileName = groupfile; readGroup(); } else if (alignfile != "") { inputFileName = alignfile; readAlign(); } + else if (listfile != "") { inputFileName = listfile; readList(); } //sort in alphabetical order sort(names.begin(), names.end()); - string outputFileName = getRootName(inputFileName) + "accnos"; + if (outputDir == "") { outputDir += hasPath(inputFileName); } + + string outputFileName = outputDir + getRootName(getSimpleName(inputFileName)) + "accnos"; + ofstream out; openOutputFile(outputFileName, out); @@ -137,6 +200,37 @@ void ListSeqsCommand::readFasta(){ exit(1); } } +//********************************************************************************************************************** +void ListSeqsCommand::readList(){ + try { + ifstream in; + openInputFile(listfile, in); + + if(!in.eof()){ + //read in list vector + ListVector list(in); + + //for each bin + for (int i = 0; i < list.getNumBins(); i++) { + string binnames = list.get(i); + + while (binnames.find_first_of(',') != -1) { + string name = binnames.substr(0,binnames.find_first_of(',')); + binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length()); + names.push_back(name); + } + + names.push_back(binnames); + } + } + in.close(); + + } + catch(exception& e) { + errorOut(e, "ListSeqsCommand", "readList"); + exit(1); + } +} //********************************************************************************************************************** void ListSeqsCommand::readName(){ diff --git a/listseqscommand.h b/listseqscommand.h index 4e0febd..04d9e1d 100644 --- a/listseqscommand.h +++ b/listseqscommand.h @@ -23,13 +23,14 @@ class ListSeqsCommand : public Command { private: vector names; - string fastafile, namefile, groupfile, alignfile, inputFileName; + string fastafile, namefile, groupfile, alignfile, inputFileName, outputDir, listfile; bool abort; void readFasta(); void readName(); void readGroup(); void readAlign(); + void readList(); }; diff --git a/matrixoutputcommand.cpp b/matrixoutputcommand.cpp index 5069e0f..fb74875 100644 --- a/matrixoutputcommand.cpp +++ b/matrixoutputcommand.cpp @@ -36,7 +36,7 @@ MatrixOutputCommand::MatrixOutputCommand(string option){ else { //valid paramters for this command - string Array[] = {"label","calc","groups"}; + string Array[] = {"label","calc","groups","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); @@ -49,6 +49,12 @@ MatrixOutputCommand::MatrixOutputCommand(string option){ if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } } + //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 + } + //make sure the user has already run the read.otu command if (globaldata->getSharedFile() == "") { if (globaldata->getListFile() == "") { mothurOut("You must read a list and a group, or a shared before you can use the dist.shared command."); mothurOutEndLine(); abort = true; } @@ -309,7 +315,7 @@ void MatrixOutputCommand::process(vector thisLookup){ } } - exportFileName = getRootName(globaldata->inputFileName) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".dist"; + exportFileName = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + matrixCalculators[i]->getName() + "." + thisLookup[0]->getLabel() + ".dist"; openOutputFile(exportFileName, out); printSims(out); out.close(); diff --git a/matrixoutputcommand.h b/matrixoutputcommand.h index 02e8fe6..6c1ce89 100644 --- a/matrixoutputcommand.h +++ b/matrixoutputcommand.h @@ -48,7 +48,7 @@ private: bool abort, allLines; set labels; //holds labels to be used - string outputFile, calc, groups, label; + string outputFile, calc, groups, label, outputDir; vector Estimators, Groups; //holds estimators to be used diff --git a/mergefilecommand.cpp b/mergefilecommand.cpp index fcf6389..d597339 100644 --- a/mergefilecommand.cpp +++ b/mergefilecommand.cpp @@ -21,7 +21,7 @@ MergeFileCommand::MergeFileCommand(string option){ } else { //valid paramters for this command - string Array[] = {"input", "output"}; + string Array[] = {"input", "output","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); @@ -34,10 +34,18 @@ MergeFileCommand::MergeFileCommand(string option){ 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 = ""; } + string fileList = validParameter.validFile(parameters, "input", false); if(fileList == "not found") { mothurOut("you must enter two or more file names"); mothurOutEndLine(); abort=true; } else{ splitAtDash(fileList, fileNames); } + //if the user changes the output directory command factory will send this info to us in the output parameter + string outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found") { outputDir = ""; } + + numInputFiles = fileNames.size(); ifstream testFile; if(numInputFiles == 0){ @@ -46,6 +54,12 @@ MergeFileCommand::MergeFileCommand(string option){ } else{ for(int i=0;i myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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("blast"); + //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["blast"] = 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; } + } + } + + //check for required parameters blastfile = validParameter.validFile(parameters, "blast", true); if (blastfile == "not open") { abort = true; } else if (blastfile == "not found") { blastfile = ""; } + //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(blastfile); //if user entered a file with a path then preserve it + } + namefile = validParameter.validFile(parameters, "name", true); if (namefile == "not open") { abort = true; } else if (namefile == "not found") { namefile = ""; } @@ -120,7 +150,7 @@ int MGClusterCommand::execute(){ nameMap->readMap(); }else{ nameMap= new NameAssignment(); } - string fileroot = getRootName(blastfile); + string fileroot = outputDir + getRootName(getSimpleName(blastfile)); string tag = ""; time_t start; float previousDist = 0.00000; @@ -210,7 +240,6 @@ int MGClusterCommand::execute(){ delete cluster; }else { //use hcluster to cluster - tag = "fn"; //get distmatrix and overlap overlapFile = read->getOverlapFile(); distFile = read->getDistFile(); diff --git a/mgclustercommand.h b/mgclustercommand.h index 2ac80c9..89b72fc 100644 --- a/mgclustercommand.h +++ b/mgclustercommand.h @@ -38,7 +38,7 @@ private: ListVector oldList; vector overlapMatrix; - string blastfile, method, namefile, overlapFile, distFile; + string blastfile, method, namefile, overlapFile, distFile, outputDir; ofstream sabundFile, rabundFile, listFile; float cutoff, penalty; int precision, length, precisionLength; diff --git a/mothur.cpp b/mothur.cpp index ab4fea0..d4177e3 100644 --- a/mothur.cpp +++ b/mothur.cpp @@ -99,14 +99,10 @@ int main(int argc, char *argv[]){ mothur = new InteractEngine(argv[0]); } - //used to intercept the terminate signal, so instead of terminating mothur it will end a command - //void (*prev_fn)(int); - //prev_fn = signal(SIGTERM, mothur->terminateCommand(0)); - - //if (prev_fn==SIG_IGN) signal (SIGTERM,SIG_IGN); - - while(bail == 0) { bail = mothur->getInput(); } + + string outputDir = mothur->getOutputDir(); + logFileName = outputDir + logFileName; delete mothur; diff --git a/mothur.h b/mothur.h index 8bd8d7f..6381e51 100644 --- a/mothur.h +++ b/mothur.h @@ -23,6 +23,7 @@ #include #include + //exception #include #include @@ -61,6 +62,7 @@ //#include #else #include //allows unbuffered screen capture from stdin + #include //get cwd #endif using namespace std; @@ -391,11 +393,18 @@ inline string getSimpleName(string longName){ string simpleName = longName; - if(longName.find_last_of("/") != longName.npos){ - int pos = longName.find_last_of('/')+1; - simpleName = longName.substr(pos, longName.length()); - } + size_t found; + found=longName.find_last_of("/\\"); + if(found != longName.npos){ + simpleName = longName.substr(found+1); + } + + //if(longName.find_last_of("/") != longName.npos){ + // int pos = longName.find_last_of('/')+1; + // simpleName = longName.substr(pos, longName.length()); + //} + return simpleName; } @@ -428,9 +437,24 @@ inline string getPathName(string longName){ int pos = longName.find_last_of('/')+1; rootPathName = longName.substr(0, pos); } - + return rootPathName; } +/***********************************************************************/ + +inline string hasPath(string longName){ + + string path = ""; + + size_t found; + found=longName.find_last_of("/\\"); + + if(found != longName.npos){ + path = longName.substr(0, found+1); + } + + return path; +} /***********************************************************************/ @@ -462,11 +486,114 @@ inline bool isBlank(string fileName){ } /***********************************************************************/ +inline string getFullPathName(string fileName){ + + string path = hasPath(fileName); + string newFileName = getSimpleName(fileName); + int pos; + + if (path == "") { return fileName; } //its a simple name + else { //we need to complete the pathname + // ex. ../../../filename + // cwd = /user/work/desktop + + string cwd; + //get current working directory + #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) + if (path.rfind("./") == -1) { return fileName; } //already complete name + + char* cwdpath; + size_t size; + cwdpath=getcwd(cwdpath,size); + cwd = cwdpath; + + //rip off first '/' + string simpleCWD; + if (cwd.length() > 0) { simpleCWD = cwd.substr(1); } + + //break apart the current working directory + vector dirs; + while (simpleCWD.find_first_of('/') != -1) { + string dir = simpleCWD.substr(0,simpleCWD.find_first_of('/')); + simpleCWD = simpleCWD.substr(simpleCWD.find_first_of('/')+1, simpleCWD.length()); + dirs.push_back(dir); + } + //get last one // ex. ../../../filename = /user/work/desktop/filename + dirs.push_back(simpleCWD); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop + + int index = dirs.size()-1; + + while((pos = path.rfind("./")) != -1) { //while you don't have a complete path + if (path[(pos-1)] == '.') { //you want your parent directory ../ + path = path.substr(0, pos-1); + index--; + if (index == 0) { break; } + }else if (path[(pos-1)] == '/') { //you want the current working dir ./ + path = path.substr(0, pos); + }else if (pos == 1) { break; + }else { mothurOut("cannot resolve path for " + fileName); mothurOutEndLine(); return fileName; } + } + + for (int i = index; i >= 0; i--) { + newFileName = dirs[i] + "/" + newFileName; + } + + newFileName = "/" + newFileName; + return newFileName; + + #else + if (path.rfind(".\\") == -1) { return fileName; } //already complete name + + char *cwdpath = NULL; + cwdpath = getcwd(NULL, 0); // or _getcwd + if ( cwdpath != NULL) { cwd = cwdpath; } + else { cwd = ""; } + + //break apart the current working directory + vector dirs; + while (cwd.find_first_of('\\') != -1) { + string dir = cwd.substr(0,cwd.find_first_of('\\')); + cwd = cwd.substr(cwd.find_first_of('\\')+1, cwd.length()); + dirs.push_back(dir); + + } + //get last one + dirs.push_back(cwd); //ex. dirs[0] = user, dirs[1] = work, dirs[2] = desktop + + int index = dirs.size()-1; + + while((pos = path.rfind(".\\")) != -1) { //while you don't have a complete path + if (path[(pos-1)] == '.') { //you want your parent directory ../ + path = path.substr(0, pos-1); + index--; + if (index == 0) { break; } + }else if (path[(pos-1)] == '\\') { //you want the current working dir ./ + path = path.substr(0, pos); + }else if (pos == 1) { break; + }else { mothurOut("cannot resolve path for " + fileName); mothurOutEndLine(); return fileName; } + } + + for (int i = index; i >= 0; i--) { + newFileName = dirs[i] + "\\" + newFileName; + } + + return newFileName; + + #endif + } + +} +/***********************************************************************/ + inline int openInputFile(string fileName, ifstream& fileHandle, string m){ + + //get full path name + string completeFileName = getFullPathName(fileName); + //string completeFileName = fileName; - fileHandle.open(fileName.c_str()); + fileHandle.open(completeFileName.c_str()); if(!fileHandle) { - mothurOut("Error: Could not open " + fileName); mothurOutEndLine(); + mothurOut("Error: Could not open " + completeFileName); mothurOutEndLine(); return 1; } else { @@ -479,16 +606,18 @@ inline int openInputFile(string fileName, ifstream& fileHandle, string m){ /***********************************************************************/ inline int openInputFile(string fileName, ifstream& fileHandle){ - - fileHandle.open(fileName.c_str()); + //get full path name + string completeFileName = getFullPathName(fileName); + + fileHandle.open(completeFileName.c_str()); if(!fileHandle) { - mothurOut("Error: Could not open " + fileName); mothurOutEndLine(); + mothurOut("Error: Could not open " + completeFileName); mothurOutEndLine(); return 1; } else { //check for blank file gobble(fileHandle); - if (fileHandle.eof()) { mothurOut(fileName + " is blank. Please correct."); mothurOutEndLine(); return 1; } + if (fileHandle.eof()) { mothurOut(completeFileName + " is blank. Please correct."); mothurOutEndLine(); return 1; } return 0; } @@ -498,10 +627,12 @@ inline int openInputFile(string fileName, ifstream& fileHandle){ /***********************************************************************/ inline int openOutputFile(string fileName, ofstream& fileHandle){ + + string completeFileName = getFullPathName(fileName); - fileHandle.open(fileName.c_str(), ios::trunc); + fileHandle.open(completeFileName.c_str(), ios::trunc); if(!fileHandle) { - mothurOut("Error: Could not open " + fileName); mothurOutEndLine(); + mothurOut("Error: Could not open " + completeFileName); mothurOutEndLine(); return 1; } else { diff --git a/otuhierarchycommand.cpp b/otuhierarchycommand.cpp index d095138..bc87211 100644 --- a/otuhierarchycommand.cpp +++ b/otuhierarchycommand.cpp @@ -18,23 +18,43 @@ OtuHierarchyCommand::OtuHierarchyCommand(string option){ else { //valid paramters for this command - string Array[] = {"list","label"}; + string Array[] = {"list","label","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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("list"); + //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["list"] = inputDir + it->second; } + } + } + listFile = validParameter.validFile(parameters, "list", true); if (listFile == "not found") { mothurOut("list is a required parameter for the otu.hierarchy command."); mothurOutEndLine(); abort = true; } else if (listFile == "not open") { abort = true; } - + + //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(listFile); //if user entered a file with a path then preserve it + } //check for optional parameter and set defaults // ...at some point should added some additional type checking... @@ -111,7 +131,7 @@ int OtuHierarchyCommand::execute(){ } ofstream out; - string outputFileName = getRootName(listFile) + lists[0].getLabel() + "-" + lists[1].getLabel() + ".otu.hierarchy"; + string outputFileName = outputDir + getRootName(getSimpleName(listFile)) + lists[0].getLabel() + "-" + lists[1].getLabel() + ".otu.hierarchy"; openOutputFile(outputFileName, out); //go through each bin in "big" otu and output the bins in "little" otu which created it diff --git a/otuhierarchycommand.h b/otuhierarchycommand.h index 39795dc..c6749ef 100644 --- a/otuhierarchycommand.h +++ b/otuhierarchycommand.h @@ -25,7 +25,7 @@ public: private: bool abort; set labels; //holds labels to be used - string label, listFile; + string label, listFile, outputDir; vector getListVectors(); diff --git a/parsimonycommand.cpp b/parsimonycommand.cpp index 6ffb30b..da997c1 100644 --- a/parsimonycommand.cpp +++ b/parsimonycommand.cpp @@ -21,7 +21,7 @@ ParsimonyCommand::ParsimonyCommand(string option) { else { //valid paramters for this command - string Array[] = {"random","groups","iters"}; + string Array[] = {"random","groups","iters","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); @@ -41,7 +41,10 @@ ParsimonyCommand::ParsimonyCommand(string option) { if (globaldata->gTree.size() == 0) { mothurOut("You must read a treefile and a groupfile or set the randomtree parameter to the output filename you wish, before you may execute the parsimony command."); mothurOutEndLine(); abort = true; } } - + + //if the user changes the output directory command factory will send this info to us in the output parameter + string outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; } + //check for optional parameter and set defaults // ...at some point should added some additional type checking... groups = validParameter.validFile(parameters, "groups", false); @@ -60,13 +63,18 @@ ParsimonyCommand::ParsimonyCommand(string option) { if (randomtree == "") { T = globaldata->gTree; tmap = globaldata->gTreemap; - output = new ColumnFile(globaldata->getTreeFile() + ".parsimony", itersString); - sumFile = globaldata->getTreeFile() + ".psummary"; + + if(outputDir == "") { outputDir += hasPath(globaldata->getTreeFile()); } + output = new ColumnFile(outputDir + getSimpleName(globaldata->getTreeFile()) + ".parsimony", itersString); + + sumFile = outputDir + getSimpleName(globaldata->getTreeFile()) + ".psummary"; openOutputFile(sumFile, outSum); }else { //user wants random distribution savetmap = globaldata->gTreemap; getUserInput(); - output = new ColumnFile(randomtree, itersString); + + if(outputDir == "") { outputDir += hasPath(randomtree); } + output = new ColumnFile(outputDir+ getSimpleName(randomtree), itersString); } //set users groups to analyze diff --git a/parsimonycommand.h b/parsimonycommand.h index 0f12d42..f855d86 100644 --- a/parsimonycommand.h +++ b/parsimonycommand.h @@ -38,7 +38,7 @@ private: TreeMap* savetmap; Parsimony* pars; vector groupComb; // AB. AC, BC... - string sumFile, randomtree, allGroups; + string sumFile, randomtree, allGroups, outputDir; int iters, numGroups, numComp, counter; vector numEachGroup; //vector containing the number of sequences in each group the users wants for random distrib. vector< vector > userTreeScores; //scores for users trees for each comb. diff --git a/pcacommand.cpp b/pcacommand.cpp index 09324c9..77b16e8 100644 --- a/pcacommand.cpp +++ b/pcacommand.cpp @@ -21,51 +21,47 @@ PCACommand::PCACommand(string option){ else { //valid paramters for this command - string Array[] = {"phylip"}; + string Array[] = {"phylip","outputdir", "inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser. getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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; } + } + } + //required parameters phylipfile = validParameter.validFile(parameters, "phylip", true); if (phylipfile == "not open") { abort = true; } else if (phylipfile == "not found") { phylipfile = ""; abort = true; } else { filename = phylipfile; } - //columnfile = validParameter.validFile(parameters, "column", true); - //if (columnfile == "not open") { abort = true; } - //else if (columnfile == "not found") { columnfile = ""; } - //else { format = "column"; } - - //namefile = validParameter.validFile(parameters, "name", true); - //if (namefile == "not open") { abort = true; } - //else if (namefile == "not found") { namefile = ""; } - + //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(phylipfile); //if user entered a file with a path then preserve it + } //error checking on files if (phylipfile == "") { mothurOut("You must provide a distance file before running the pca command."); mothurOutEndLine(); abort = true; } - //if ((phylipfile == "") && (columnfile == "")) { mothurOut("You must provide a distance file before running the pca command."); mothurOutEndLine(); abort = true; } - //else if ((phylipfile != "") && (columnfile != "")) { mothurOut("You may not use both the column and the phylip parameters."); mothurOutEndLine(); abort = true; } - - //if (columnfile != "") { - // if (namefile == "") { mothurOut("You need to provide a namefile if you are going to use the column format."); mothurOutEndLine(); abort = true; } - //} - - //string temp = validParameter.validFile(parameters, "lt", false); if (temp == "not found") { temp = "false"; } - //bool lt = isTrue(temp); - - //if (lt) { matrix = 2; } - //else { matrix = 1; } - - } } @@ -101,13 +97,16 @@ int PCACommand::execute(){ vector names; vector > D; - fbase = filename; - if(fbase.find_last_of(".")!=string::npos){ - fbase.erase(fbase.find_last_of(".")+1); - } - else{ - fbase += "."; - } + //fbase = filename; + //if(fbase.find_last_of(".")!=string::npos){ + // fbase.erase(fbase.find_last_of(".")+1); + //} + //else{ + // fbase += "."; + //} + + fbase = outputDir + getRootName(getSimpleName(filename)); + read(filename, names, D); double offset = 0.0000; diff --git a/pcacommand.h b/pcacommand.h index 143e83d..a447ffc 100644 --- a/pcacommand.h +++ b/pcacommand.h @@ -25,7 +25,7 @@ public: private: bool abort; - string phylipfile, columnfile, namefile, format, filename, fbase; + string phylipfile, columnfile, namefile, format, filename, fbase, outputDir; float cutoff, precision; void get_comment(istream&, char, char); diff --git a/phylotypecommand.cpp b/phylotypecommand.cpp index 8ba0a09..2dbd53a 100644 --- a/phylotypecommand.cpp +++ b/phylotypecommand.cpp @@ -24,19 +24,34 @@ PhylotypeCommand::PhylotypeCommand(string option){ else { //valid paramters for this command - string AlignArray[] = {"taxonomy","cutoff","label"}; + string AlignArray[] = {"taxonomy","cutoff","label","outputdir","inputdir"}; vector myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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("taxonomy"); + //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["taxonomy"] = inputDir + it->second; } + } + } + taxonomyFileName = validParameter.validFile(parameters, "taxonomy", true); if (taxonomyFileName == "not found") { mothurOut("taxonomy is a required parameter for the phylotype command."); @@ -44,6 +59,12 @@ PhylotypeCommand::PhylotypeCommand(string option){ abort = true; }else if (taxonomyFileName == "not open") { abort = true; } + //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(taxonomyFileName); //if user entered a file with a path then preserve it + } + string temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "-1"; } convert(temp, cutoff); @@ -112,14 +133,16 @@ int PhylotypeCommand::execute(){ bool done = false; if (tree->get(leaves[0]).parent == -1) { mothurOut("Empty Tree"); mothurOutEndLine(); done = true; } + string fileroot = outputDir + getRootName(getSimpleName(taxonomyFileName)); + ofstream outList; - string outputListFile = getRootName(taxonomyFileName) + "tx.list"; + string outputListFile = fileroot + "tx.list"; openOutputFile(outputListFile, outList); ofstream outSabund; - string outputSabundFile = getRootName(taxonomyFileName) + "tx.sabund"; + string outputSabundFile = fileroot + "tx.sabund"; openOutputFile(outputSabundFile, outSabund); ofstream outRabund; - string outputRabundFile = getRootName(taxonomyFileName) + "tx.rabund"; + string outputRabundFile = fileroot + "tx.rabund"; openOutputFile(outputRabundFile, outRabund); int count = 1; diff --git a/phylotypecommand.h b/phylotypecommand.h index 0322daf..b61c039 100644 --- a/phylotypecommand.h +++ b/phylotypecommand.h @@ -26,7 +26,7 @@ public: private: bool abort, allLines; - string taxonomyFileName, label; + string taxonomyFileName, label, outputDir; set labels; //holds labels to be used int cutoff; diff --git a/pintail.cpp b/pintail.cpp index 2698fe4..6adfdf9 100644 --- a/pintail.cpp +++ b/pintail.cpp @@ -18,7 +18,7 @@ inline bool compareQuanMembers(quanMember left, quanMember right){ } //*************************************************************************************************************** -Pintail::Pintail(string filename, string temp) { fastafile = filename; templateFile = temp; } +Pintail::Pintail(string filename, string temp, string o) { fastafile = filename; templateFile = temp; outputDir = o; } //*************************************************************************************************************** Pintail::~Pintail() { @@ -161,7 +161,7 @@ int Pintail::getChimeras() { mothurOut("Getting conservation... "); cout.flush(); if (consfile == "") { mothurOut("Calculating probability of conservation for your template sequences. This can take a while... I will output the frequency of the highest base in each position to a .freq file so that you can input them using the conservation parameter next time you run this command. Providing the .freq file will improve speed. "); cout.flush(); - probabilityProfile = decalc->calcFreq(templateSeqs, templateFile); + probabilityProfile = decalc->calcFreq(templateSeqs, outputDir + getSimpleName(templateFile)); mothurOut("Done."); mothurOutEndLine(); }else { probabilityProfile = readFreq(); } @@ -281,13 +281,13 @@ int Pintail::getChimeras() { string noOutliers, outliers; if ((!filter) && (seqMask == "")) { - noOutliers = getRootName(templateFile) + "pintail.quan"; + noOutliers = outputDir + getRootName(getSimpleName(templateFile)) + "pintail.quan"; }else if ((filter) && (seqMask == "")) { - noOutliers = getRootName(templateFile) + "pintail.filtered.quan"; + noOutliers = outputDir + getRootName(getSimpleName(templateFile)) + "pintail.filtered.quan"; }else if ((!filter) && (seqMask != "")) { - noOutliers = getRootName(templateFile) + "pintail.masked.quan"; + noOutliers = outputDir + getRootName(getSimpleName(templateFile)) + "pintail.masked.quan"; }else if ((filter) && (seqMask != "")) { - noOutliers = getRootName(templateFile) + "pintail.filtered.masked.quan"; + noOutliers = outputDir + getRootName(getSimpleName(templateFile)) + "pintail.filtered.masked.quan"; } //outliers = getRootName(templateFile) + "pintail.quanYESOUTLIERS"; diff --git a/pintail.h b/pintail.h index 516c682..26154b2 100644 --- a/pintail.h +++ b/pintail.h @@ -24,7 +24,7 @@ class Pintail : public Chimera { public: - Pintail(string, string); + Pintail(string, string, string); ~Pintail(); int getChimeras(); diff --git a/preclustercommand.cpp b/preclustercommand.cpp index 89e6cbf..d638bd2 100644 --- a/preclustercommand.cpp +++ b/preclustercommand.cpp @@ -22,28 +22,56 @@ PreClusterCommand::PreClusterCommand(string option){ else { //valid paramters for this command - string Array[] = {"fasta", "name", "diffs"}; + string Array[] = {"fasta", "name", "diffs", "outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command for (map::iterator it2 = parameters.begin(); it2 != parameters.end(); it2++) { if (validParameter.isValidParameter(it2->first, myArray, it2->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("fasta"); + //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["fasta"] = 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; } + } + } + //check for required parameters fastafile = validParameter.validFile(parameters, "fasta", true); if (fastafile == "not found") { mothurOut("fasta is a required parameter for the pre.cluster command."); mothurOutEndLine(); abort = true; } else if (fastafile == "not open") { abort = true; } + //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(fastafile); //if user entered a file with a path then preserve it + } + //check for optional parameter and set defaults // ...at some point should added some additional type checking... namefile = validParameter.validFile(parameters, "name", true); - if (namefile == "not found") { namefile = ""; } else if (namefile == "not open") { abort = true; } // else { readNameFile(); } @@ -132,9 +160,11 @@ int PreClusterCommand::execute(){ }//end if active i if(i % 100 == 0) { cout << i << '\t' << numSeqs - count << '\t' << count << endl; } } - - string newFastaFile = getRootName(fastafile) + "precluster" + getExtension(fastafile); - string newNamesFile = getRootName(fastafile) + "precluster.names"; + + string fileroot = outputDir + getRootName(getSimpleName(fastafile)); + + string newFastaFile = fileroot + "precluster" + getExtension(fastafile); + string newNamesFile = fileroot + "precluster.names"; mothurOut("Total number of sequences before precluster was " + toString(alignSeqs.size()) + "."); mothurOutEndLine(); diff --git a/preclustercommand.h b/preclustercommand.h index 5113fa4..3a1157d 100644 --- a/preclustercommand.h +++ b/preclustercommand.h @@ -38,7 +38,7 @@ public: private: int diffs, length; bool abort; - string fastafile, namefile; + string fastafile, namefile, outputDir; vector alignSeqs; //maps the number of identical seqs to a sequence // map names; //represents the names file first column maps to second column // map sizes; //this map a seq name to the number of identical seqs in the names file diff --git a/quitcommand.cpp b/quitcommand.cpp index 99dc46b..05e2758 100644 --- a/quitcommand.cpp +++ b/quitcommand.cpp @@ -16,8 +16,6 @@ QuitCommand::QuitCommand(string option){ //allow user to run help if(option == "help") { help(); abort = true; } - - else if (option != "") { mothurOut("There are no valid parameters for the quit command."); mothurOutEndLine(); abort = true; } } //********************************************************************************************************************** diff --git a/rarefactcommand.cpp b/rarefactcommand.cpp index f2de77b..97d6332 100644 --- a/rarefactcommand.cpp +++ b/rarefactcommand.cpp @@ -35,7 +35,7 @@ RareFactCommand::RareFactCommand(string option){ else { //valid paramters for this command - string Array[] = {"iters","freq","label","calc","abund"}; + string Array[] = {"iters","freq","label","calc","abund","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); @@ -48,6 +48,12 @@ RareFactCommand::RareFactCommand(string option){ if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } } + //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 + } + //make sure the user has already run the read.otu command if ((globaldata->getSharedFile() == "") && (globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "")) { mothurOut("You must read a list, sabund, rabund or shared file before you can use the rarefact.single command."); mothurOutEndLine(); abort = true; } @@ -127,7 +133,7 @@ int RareFactCommand::execute(){ for (int p = 0; p < inputFileNames.size(); p++) { - string fileNameRoot = getRootName(inputFileNames[p]); + string fileNameRoot = outputDir + getRootName(getSimpleName(inputFileNames[p])); globaldata->inputFileName = inputFileNames[p]; if (inputFileNames.size() > 1) { diff --git a/rarefactcommand.h b/rarefactcommand.h index 3213af4..dce1a0d 100644 --- a/rarefactcommand.h +++ b/rarefactcommand.h @@ -44,6 +44,7 @@ private: vector Estimators; vector inputFileNames; vector groups; + string outputDir; vector parseSharedFile(string); diff --git a/rarefactsharedcommand.cpp b/rarefactsharedcommand.cpp index 83fb35e..1d13a56 100644 --- a/rarefactsharedcommand.cpp +++ b/rarefactsharedcommand.cpp @@ -28,7 +28,7 @@ RareFactSharedCommand::RareFactSharedCommand(string option){ else { //valid paramters for this command - string Array[] = {"iters","label","calc","groups", "jumble"}; + string Array[] = {"iters","label","calc","groups", "jumble","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); @@ -46,6 +46,12 @@ RareFactSharedCommand::RareFactSharedCommand(string option){ if (globaldata->getListFile() == "") { mothurOut("You must read a list and a group, or a shared before you can use the collect.shared command."); mothurOutEndLine(); abort = true; } else if (globaldata->getGroupFile() == "") { mothurOut("You must read a list and a group, or a shared before you can use the collect.shared command."); mothurOutEndLine(); abort = true; } } + + //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 + } //check for optional parameter and set defaults @@ -88,7 +94,7 @@ RareFactSharedCommand::RareFactSharedCommand(string option){ if (abort == false) { - string fileNameRoot = getRootName(globaldata->inputFileName); + string fileNameRoot = outputDir + getRootName(getSimpleName(globaldata->inputFileName)); // format = globaldata->getFormat(); diff --git a/rarefactsharedcommand.h b/rarefactsharedcommand.h index a141988..aa321e8 100644 --- a/rarefactsharedcommand.h +++ b/rarefactsharedcommand.h @@ -39,7 +39,7 @@ private: bool abort, allLines, jumble; set labels; //holds labels to be used - string label, calc, groups; + string label, calc, groups, outputDir; vector Estimators, Groups; diff --git a/readdistcommand.cpp b/readdistcommand.cpp index 9d38e76..9cb2046 100644 --- a/readdistcommand.cpp +++ b/readdistcommand.cpp @@ -22,21 +22,63 @@ ReadDistCommand::ReadDistCommand(string option){ else { //valid paramters for this command - string Array[] = {"phylip", "column", "name", "cutoff", "precision", "group"}; + string Array[] = {"phylip", "column", "name", "cutoff", "precision", "group","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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; } } globaldata->newRead(); + //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; } + } + + it = parameters.find("group"); + //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["group"] = inputDir + it->second; } + } + } + + //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 = ""; } + //check for required parameters phylipfile = validParameter.validFile(parameters, "phylip", true); if (phylipfile == "not open") { abort = true; } @@ -92,10 +134,12 @@ ReadDistCommand::ReadDistCommand(string option){ else if (format == "phylip") { read = new ReadPhylipMatrix(distFileName); } else if (format == "matrix") { groupMap = new GroupMap(groupfile); - groupMap->readMap(); - - if (globaldata->gGroupmap != NULL) { delete globaldata->gGroupmap; } - globaldata->gGroupmap = groupMap; + int error = groupMap->readMap(); + if (error == 1) { delete groupMap; abort = true; } + else { + if (globaldata->gGroupmap != NULL) { delete globaldata->gGroupmap; } + globaldata->gGroupmap = groupMap; + } } if (format != "matrix" ) { @@ -169,7 +213,9 @@ int ReadDistCommand::execute(){ if (matrix->getNumSeqs() < groupMap->getNumSeqs()) { mothurOut("Your distance file contains " + toString(matrix->getNumSeqs()) + " sequences, and your group file contains " + toString(groupMap->getNumSeqs()) + " sequences."); mothurOutEndLine(); //create new group file - string newGroupFile = getRootName(groupfile) + "editted.groups"; + if(outputDir == "") { outputDir += hasPath(groupfile); } + + string newGroupFile = outputDir + getRootName(getSimpleName(groupfile)) + "editted.groups"; ofstream outGroups; openOutputFile(newGroupFile, outGroups); diff --git a/readdistcommand.h b/readdistcommand.h index 868d72e..1f852be 100644 --- a/readdistcommand.h +++ b/readdistcommand.h @@ -39,7 +39,7 @@ private: FullMatrix* matrix; GroupMap* groupMap; string distFileName, format, method; - string phylipfile, columnfile, namefile, groupfile; + string phylipfile, columnfile, namefile, groupfile, outputDir; NameAssignment* nameMap; bool abort; diff --git a/readotucommand.cpp b/readotucommand.cpp index b58dc60..e55e980 100644 --- a/readotucommand.cpp +++ b/readotucommand.cpp @@ -21,21 +21,81 @@ ReadOtuCommand::ReadOtuCommand(string option){ else { //valid paramters for this command - string Array[] = {"list","order","shared", "label","group","sabund", "rabund","groups"}; + string Array[] = {"list","order","shared", "label","group","sabund", "rabund","groups","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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; } } globaldata->newRead(); + //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("list"); + //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["list"] = inputDir + it->second; } + } + + it = parameters.find("order"); + //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["order"] = inputDir + it->second; } + } + + it = parameters.find("shared"); + //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["shared"] = inputDir + it->second; } + } + + it = parameters.find("group"); + //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["group"] = inputDir + it->second; } + } + + it = parameters.find("sabund"); + //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["sabund"] = inputDir + it->second; } + } + + it = parameters.find("rabund"); + //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["rabund"] = inputDir + it->second; } + } + + } + + + //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 = ""; } + //check for required parameters listfile = validParameter.validFile(parameters, "list", true); if (listfile == "not open") { abort = true; } @@ -63,7 +123,10 @@ ReadOtuCommand::ReadOtuCommand(string option){ else { globaldata->setGroupFile(groupfile); groupMap = new GroupMap(groupfile); - groupMap->readMap(); + + int error = groupMap->readMap(); + if (error == 1) { abort = true; } + globaldata->gGroupmap = groupMap; } @@ -154,9 +217,8 @@ int ReadOtuCommand::execute(){ if (globaldata->getFormat() == "shared") { - shared = new SharedCommand(); + shared = new SharedCommand(outputDir); int okay = shared->execute(); - delete shared; //problem with shared if (okay == 1) { @@ -169,8 +231,8 @@ int ReadOtuCommand::execute(){ globaldata->setFormat("sharedfile"); globaldata->setListFile(""); globaldata->setGroupFile(""); - globaldata->setSharedFile(getRootName(filename) + "shared"); } + delete shared; } return 0; } diff --git a/readotucommand.h b/readotucommand.h index 543642b..beb2f4b 100644 --- a/readotucommand.h +++ b/readotucommand.h @@ -29,7 +29,7 @@ private: InputData* input; Command* shared; GroupMap* groupMap; - string filename, listfile, orderfile, sharedfile, label, groupfile, sabundfile, rabundfile, format, groups; + string filename, listfile, orderfile, sharedfile, label, groupfile, sabundfile, rabundfile, format, groups, outputDir; vector Groups; bool abort, allLines; diff --git a/readtreecommand.cpp b/readtreecommand.cpp index 0f07ac2..5b371f5 100644 --- a/readtreecommand.cpp +++ b/readtreecommand.cpp @@ -20,21 +20,45 @@ ReadTreeCommand::ReadTreeCommand(string option){ else { //valid paramters for this command - string Array[] = {"tree","group"}; + string Array[] = {"tree","group","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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; } } globaldata->newRead(); + //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("tree"); + //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["tree"] = inputDir + it->second; } + } + + it = parameters.find("group"); + //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["group"] = inputDir + it->second; } + } + } + + //check for required parameters treefile = validParameter.validFile(parameters, "tree", true); if (treefile == "not open") { abort = true; } diff --git a/removeseqscommand.cpp b/removeseqscommand.cpp index 790cf79..a8f03e0 100644 --- a/removeseqscommand.cpp +++ b/removeseqscommand.cpp @@ -22,19 +22,78 @@ RemoveSeqsCommand::RemoveSeqsCommand(string option){ else { //valid paramters for this command - string Array[] = {"fasta","name", "group", "alignreport", "accnos", "list" }; + string Array[] = {"fasta","name", "group", "alignreport", "accnos", "list","outputdir","inputdir" }; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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 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 = ""; } + + //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("alignreport"); + //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["alignreport"] = inputDir + it->second; } + } + + it = parameters.find("fasta"); + //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["fasta"] = inputDir + it->second; } + } + + it = parameters.find("accnos"); + //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["accnos"] = inputDir + it->second; } + } + + it = parameters.find("list"); + //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["list"] = 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; } + } + + it = parameters.find("group"); + //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["group"] = inputDir + it->second; } + } + } + + //check for required parameters accnosfile = validParameter.validFile(parameters, "accnos", true); if (accnosfile == "not open") { abort = true; } @@ -62,7 +121,10 @@ RemoveSeqsCommand::RemoveSeqsCommand(string option){ if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == "")) { mothurOut("You must provide one of the following: fasta, name, group, alignreport or list."); mothurOutEndLine(); abort = true; } - if (parameters.size() > 2) { mothurOut("You may only enter one of the following: fasta, name, group, alignreport, or list."); mothurOutEndLine(); abort = true; } + int okay = 2; + if (outputDir != "") { okay++; } + + if (parameters.size() > okay) { mothurOut("You may only enter one of the following: fasta, name, group, alignreport, or list."); mothurOutEndLine(); abort = true; } } } @@ -117,6 +179,7 @@ int RemoveSeqsCommand::execute(){ //********************************************************************************************************************** void RemoveSeqsCommand::readFasta(){ try { + if (outputDir == "") { outputDir += hasPath(fastafile); } string outputFileName = getRootName(fastafile) + "pick" + getExtension(fastafile); ofstream out; openOutputFile(outputFileName, out); @@ -158,6 +221,7 @@ void RemoveSeqsCommand::readFasta(){ //********************************************************************************************************************** void RemoveSeqsCommand::readList(){ try { + if (outputDir == "") { outputDir += hasPath(listfile); } string outputFileName = getRootName(listfile) + "pick" + getExtension(listfile); ofstream out; openOutputFile(outputFileName, out); @@ -222,7 +286,7 @@ void RemoveSeqsCommand::readList(){ //********************************************************************************************************************** void RemoveSeqsCommand::readName(){ try { - + if (outputDir == "") { outputDir += hasPath(namefile); } string outputFileName = getRootName(namefile) + "pick" + getExtension(namefile); ofstream out; @@ -305,7 +369,7 @@ void RemoveSeqsCommand::readName(){ //********************************************************************************************************************** void RemoveSeqsCommand::readGroup(){ try { - + if (outputDir == "") { outputDir += hasPath(groupfile); } string outputFileName = getRootName(groupfile) + "pick" + getExtension(groupfile); ofstream out; openOutputFile(outputFileName, out); @@ -348,6 +412,7 @@ void RemoveSeqsCommand::readGroup(){ //alignreport file has a column header line then all other lines contain 16 columns. we just want the first column since that contains the name void RemoveSeqsCommand::readAlign(){ try { + if (outputDir == "") { outputDir += hasPath(alignfile); } string outputFileName = getRootName(getRootName(alignfile)) + "pick.align.report"; ofstream out; openOutputFile(outputFileName, out); diff --git a/removeseqscommand.h b/removeseqscommand.h index 1ad1eee..1dcb0fc 100644 --- a/removeseqscommand.h +++ b/removeseqscommand.h @@ -23,7 +23,7 @@ class RemoveSeqsCommand : public Command { private: set names; - string accnosfile, fastafile, namefile, groupfile, alignfile, listfile; + string accnosfile, fastafile, namefile, groupfile, alignfile, listfile, outputDir; bool abort; void readFasta(); diff --git a/reversecommand.cpp b/reversecommand.cpp index 37e9568..2742401 100644 --- a/reversecommand.cpp +++ b/reversecommand.cpp @@ -22,24 +22,45 @@ ReverseSeqsCommand::ReverseSeqsCommand(string option){ else { //valid paramters for this command - string Array[] = {"fasta"}; + string Array[] = {"fasta", "outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; - + map::iterator it; + //check to make sure all parameters are valid for command - for (map::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("fasta"); + //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["fasta"] = inputDir + it->second; } + } + } + //check for required parameters fasta = validParameter.validFile(parameters, "fasta", true); if (fasta == "not open") { abort = true; } else if (fasta == "not found") { fasta = ""; mothurOut("fasta is a required parameter for the reverse.seqs command."); mothurOutEndLine(); abort = true; } + //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(fasta); //if user entered a file with a path then preserve it + } + } } catch(exception& e) { @@ -78,7 +99,7 @@ int ReverseSeqsCommand::execute(){ openInputFile(fasta, inFASTA); ofstream outFASTA; - string reverseFile = getRootName(fasta) + "rc" + getExtension(fasta); + string reverseFile = outputDir + getRootName(getSimpleName(fasta)) + "rc" + getExtension(fasta); openOutputFile(reverseFile, outFASTA); while(!inFASTA.eof()){ diff --git a/reversecommand.h b/reversecommand.h index b47ee3a..aad4543 100644 --- a/reversecommand.h +++ b/reversecommand.h @@ -23,7 +23,7 @@ public: private: bool abort; - string fasta; + string fasta, outputDir; }; diff --git a/screenseqscommand.cpp b/screenseqscommand.cpp index 32426f3..48bf171 100644 --- a/screenseqscommand.cpp +++ b/screenseqscommand.cpp @@ -22,19 +22,58 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option){ else { //valid paramters for this command string AlignArray[] = {"fasta", "start", "end", "maxambig", "maxhomop", "minlength", "maxlength", - "name", "group", "alignreport"}; + "name", "group", "alignreport","outputdir","inputdir"}; vector myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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("fasta"); + //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["fasta"] = inputDir + it->second; } + } + + it = parameters.find("group"); + //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["group"] = 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; } + } + + it = parameters.find("alignreport"); + //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["alignreport"] = inputDir + it->second; } + } + } + //check for required parameters fastafile = validParameter.validFile(parameters, "fasta", true); if (fastafile == "not found") { mothurOut("fasta is a required parameter for the screen.seqs command."); mothurOutEndLine(); abort = true; } @@ -52,6 +91,12 @@ ScreenSeqsCommand::ScreenSeqsCommand(string option){ if (alignreport == "not open") { abort = true; } else if (alignreport == "not found") { alignreport = ""; } + //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(fastafile); //if user entered a file with a path then preserve it + } + //check for optional parameter and set defaults // ...at some point should added some additional type checking... string temp; @@ -123,8 +168,8 @@ int ScreenSeqsCommand::execute(){ set badSeqNames; - string goodSeqFile = getRootName(fastafile) + "good" + getExtension(fastafile); - string badSeqFile = getRootName(fastafile) + "bad" + getExtension(fastafile); + string goodSeqFile = outputDir + getRootName(getSimpleName(fastafile)) + "good" + getExtension(fastafile); + string badSeqFile = outputDir + getRootName(getSimpleName(fastafile)) + "bad" + getExtension(fastafile); ofstream goodSeqOut; openOutputFile(goodSeqFile, goodSeqOut); ofstream badSeqOut; openOutputFile(badSeqFile, badSeqOut); @@ -176,8 +221,8 @@ void ScreenSeqsCommand::screenNameGroupFile(set badSeqNames){ string seqName, seqList, group; set::iterator it; - string goodNameFile = getRootName(namefile) + "good" + getExtension(namefile); - string badNameFile = getRootName(namefile) + "bad" + getExtension(namefile); + string goodNameFile = outputDir + getRootName(getSimpleName(namefile)) + "good" + getExtension(namefile); + string badNameFile = outputDir + getRootName(getSimpleName(namefile)) + "bad" + getExtension(namefile); ofstream goodNameOut; openOutputFile(goodNameFile, goodNameOut); ofstream badNameOut; openOutputFile(badNameFile, badNameOut); @@ -222,8 +267,8 @@ void ScreenSeqsCommand::screenNameGroupFile(set badSeqNames){ ifstream inputGroups; openInputFile(groupfile, inputGroups); - string goodGroupFile = getRootName(groupfile) + "good" + getExtension(groupfile); - string badGroupFile = getRootName(groupfile) + "bad" + getExtension(groupfile); + string goodGroupFile = outputDir + getRootName(getSimpleName(groupfile)) + "good" + getExtension(groupfile); + string badGroupFile = outputDir + getRootName(getSimpleName(groupfile)) + "bad" + getExtension(groupfile); ofstream goodGroupOut; openOutputFile(goodGroupFile, goodGroupOut); ofstream badGroupOut; openOutputFile(badGroupFile, badGroupOut); @@ -265,8 +310,8 @@ void ScreenSeqsCommand::screenGroupFile(set badSeqNames){ string seqName, group; set::iterator it; - string goodGroupFile = getRootName(groupfile) + "good" + getExtension(groupfile); - string badGroupFile = getRootName(groupfile) + "bad" + getExtension(groupfile); + string goodGroupFile = outputDir + getRootName(getSimpleName(groupfile)) + "good" + getExtension(groupfile); + string badGroupFile = outputDir + getRootName(getSimpleName(groupfile)) + "bad" + getExtension(groupfile); ofstream goodGroupOut; openOutputFile(goodGroupFile, goodGroupOut); ofstream badGroupOut; openOutputFile(badGroupFile, badGroupOut); @@ -308,8 +353,8 @@ void ScreenSeqsCommand::screenAlignReport(set badSeqNames){ string seqName, group; set::iterator it; - string goodAlignReportFile = getRootName(alignreport) + "good" + getExtension(alignreport); - string badAlignReportFile = getRootName(alignreport) + "bad" + getExtension(alignreport); + string goodAlignReportFile = outputDir + getRootName(getSimpleName(alignreport)) + "good" + getExtension(alignreport); + string badAlignReportFile = outputDir + getRootName(getSimpleName(alignreport)) + "bad" + getExtension(alignreport); ofstream goodAlignReportOut; openOutputFile(goodAlignReportFile, goodAlignReportOut); ofstream badAlignReportOut; openOutputFile(badAlignReportFile, badAlignReportOut); diff --git a/screenseqscommand.h b/screenseqscommand.h index 269275d..5b44091 100644 --- a/screenseqscommand.h +++ b/screenseqscommand.h @@ -26,7 +26,7 @@ private: void screenAlignReport(set); bool abort; - string fastafile, namefile, groupfile, alignreport; + string fastafile, namefile, groupfile, alignreport, outputDir; int startPos, endPos, maxAmbig, maxHomoP, minLength, maxLength; }; diff --git a/secondarystructurecommand.cpp b/secondarystructurecommand.cpp index c2c104e..e2302d3 100644 --- a/secondarystructurecommand.cpp +++ b/secondarystructurecommand.cpp @@ -21,19 +21,42 @@ AlignCheckCommand::AlignCheckCommand(string option){ else { //valid paramters for this command - string Array[] = {"fasta","map"}; + string Array[] = {"fasta","map", "outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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("fasta"); + //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["fasta"] = inputDir + it->second; } + } + + it = parameters.find("map"); + //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["map"] = inputDir + it->second; } + } + } + //check for required parameters mapfile = validParameter.validFile(parameters, "map", true); if (mapfile == "not open") { abort = true; } @@ -43,6 +66,12 @@ AlignCheckCommand::AlignCheckCommand(string option){ if (fastafile == "not open") { abort = true; } else if (fastafile == "not found") { fastafile = ""; mothurOut("You must provide an fasta file."); mothurOutEndLine(); abort = true; } + //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(fastafile); //if user entered a file with a path then preserve it + } + } } @@ -82,7 +111,7 @@ int AlignCheckCommand::execute(){ openInputFile(fastafile, in); ofstream out; - string outfile = getRootName(fastafile) + "align.check"; + string outfile = outputDir + getRootName(getSimpleName(fastafile)) + "align.check"; openOutputFile(outfile, out); out << "name" << '\t' << "pound" << '\t' << "dash" << '\t' << "plus" << '\t' << "equal" << '\t'; diff --git a/secondarystructurecommand.h b/secondarystructurecommand.h index a82cf4e..dffb4cb 100644 --- a/secondarystructurecommand.h +++ b/secondarystructurecommand.h @@ -39,7 +39,7 @@ class AlignCheckCommand : public Command { private: vector structMap; - string mapfile, fastafile; + string mapfile, fastafile, outputDir; bool abort; int seqLength; diff --git a/seqsummarycommand.cpp b/seqsummarycommand.cpp index 9ab27f9..8b07b85 100644 --- a/seqsummarycommand.cpp +++ b/seqsummarycommand.cpp @@ -21,24 +21,45 @@ SeqSummaryCommand::SeqSummaryCommand(string option){ else { //valid paramters for this command - string Array[] = {"fasta"}; + string Array[] = {"fasta","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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("fasta"); + //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["fasta"] = inputDir + it->second; } + } + } + //check for required parameters fastafile = validParameter.validFile(parameters, "fasta", true); if (fastafile == "not open") { abort = true; } else if (fastafile == "not found") { fastafile = ""; mothurOut("fasta is a required parameter for the summary.seqs command."); mothurOutEndLine(); abort = true; } + //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(fastafile); //if user entered a file with a path then preserve it + } + } } catch(exception& e) { @@ -78,7 +99,7 @@ int SeqSummaryCommand::execute(){ int numSeqs = 0; ofstream outSummary; - string summaryFile = fastafile + ".summary"; + string summaryFile = outputDir + getSimpleName(fastafile) + ".summary"; openOutputFile(summaryFile, outSummary); vector startPosition; diff --git a/seqsummarycommand.h b/seqsummarycommand.h index 6d48284..e6b3b87 100644 --- a/seqsummarycommand.h +++ b/seqsummarycommand.h @@ -22,7 +22,7 @@ public: private: bool abort; - string fastafile; + string fastafile, outputDir; }; diff --git a/setdircommand.cpp b/setdircommand.cpp new file mode 100644 index 0000000..a23b61d --- /dev/null +++ b/setdircommand.cpp @@ -0,0 +1,140 @@ +/* + * setoutdircommand.cpp + * Mothur + * + * Created by westcott on 1/21/10. + * Copyright 2010 Schloss Lab. All rights reserved. + * + */ + +#include "setdircommand.h" + +//********************************************************************************************************************** + +SetDirectoryCommand::SetDirectoryCommand(string option){ + try { + abort = false; + + //allow user to run help + if(option == "help") { help(); abort = true; } + + else { + //valid paramters for this command + string Array[] = {"output","input","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; } + } + + output = validParameter.validFile(parameters, "output", false); + if (output == "not found") { output = ""; } + + input = validParameter.validFile(parameters, "input", false); + if (input == "not found") { input = ""; } + + if ((input == "") && (output == "")) { + mothurOut("You must provide either an input or output for the set.outdir command."); mothurOutEndLine(); abort = true; + } + } + } + catch(exception& e) { + errorOut(e, "SetDirectoryCommand", "SetDirectoryCommand"); + exit(1); + } +} +//********************************************************************************************************************** + +void SetDirectoryCommand::help(){ + try { + mothurOut("The set.dir command can be used to direct the output files generated by mothur to a specific place, the directory must exist.\n"); + mothurOut("The set.dir command can also be used to specify the directory where your input files are located, the directory must exist.\n"); + mothurOut("The set.dir command parameter is output and is required.\n"); + mothurOut("To return the output to the same directory as the input files you may enter: output=clear.\n"); + mothurOut("To return the input to the same directory as the mothur.exe you may enter: input=clear.\n"); + mothurOut("The set.dir command should be in the following format: set.dir(output=yourOutputDirectory, input=yourInputDirectory).\n"); + mothurOut("Example set.outdir(output=/Users/lab/desktop/outputs, input=/Users/lab/desktop/inputs).\n"); + mothurOut("Note: No spaces between parameter labels (i.e. output), '=' and parameters (i.e.yourOutputDirectory).\n\n"); + } + catch(exception& e) { + errorOut(e, "SetDirectoryCommand", "help"); + exit(1); + } +} +//********************************************************************************************************************** + +SetDirectoryCommand::~SetDirectoryCommand(){} + +//********************************************************************************************************************** + +int SetDirectoryCommand::execute(){ + try { + + if (abort == true) { return 0; } + + commandFactory = CommandFactory::getInstance(); + + + //redirect output + if ((output == "clear") || (output == "")) { output = ""; commandFactory->setOutputDirectory(output); } + else { + //add / to name if needed + string lastChar = output.substr(output.length()-1); + #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) + if (lastChar != "/") { output += "/"; } + #else + if (lastChar != "\\") { output += "\\"; } + #endif + + //test to make sure directory exists + output = getFullPathName(output); + string outTemp = output + "temp"; + ofstream out; + out.open(outTemp.c_str(), ios::trunc); + if(!out) { + mothurOut(output + " directory does not exist or is not writable."); mothurOutEndLine(); + }else{ + remove(outTemp.c_str()); + mothurOut("Changing output directory to " + output); mothurOutEndLine(); + commandFactory->setOutputDirectory(output); + } + } + + //redirect input + if ((input == "clear") || (input == "")) { input = ""; commandFactory->setInputDirectory(input); } + else { + //add / to name if needed + string lastChar = input.substr(input.length()-1); + #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) + if (lastChar != "/") { input += "/"; } + #else + if (lastChar != "\\") { input += "\\"; } + #endif + + //test to make sure directory exists + input = getFullPathName(input); + string inTemp = input + "temp"; + ofstream in; + in.open(inTemp.c_str(), ios::trunc); + if(!in) { + mothurOut(input + " directory does not exist or is not writable."); mothurOutEndLine(); + }else{ + remove(inTemp.c_str()); + mothurOut("Changing input directory to " + input); mothurOutEndLine(); + commandFactory->setInputDirectory(input); + } + } + + return 0; + } + catch(exception& e) { + errorOut(e, "SetDirectoryCommand", "SetDirectoryCommand"); + exit(1); + } +} +//**********************************************************************************************************************/ diff --git a/setdircommand.h b/setdircommand.h new file mode 100644 index 0000000..6634965 --- /dev/null +++ b/setdircommand.h @@ -0,0 +1,36 @@ +#ifndef SETDIRCOMMAND_H +#define SETDIRCOMMAND_H + +/* + * setoutdircommand.h + * Mothur + * + * Created by westcott on 1/21/10. + * Copyright 2010 Schloss Lab. All rights reserved. + * + */ + +#include "command.hpp" +#include "commandfactory.hpp" + +/**********************************************************/ + +class SetDirectoryCommand : public Command { + +public: + SetDirectoryCommand(string); + ~SetDirectoryCommand(); + int execute(); + void help(); + +private: + CommandFactory* commandFactory; + string output, input; + bool abort; + +}; + +/**********************************************************/ + +#endif + diff --git a/sharedcommand.cpp b/sharedcommand.cpp index 89f57eb..efc45c1 100644 --- a/sharedcommand.cpp +++ b/sharedcommand.cpp @@ -11,14 +11,17 @@ //********************************************************************************************************************** -SharedCommand::SharedCommand(){ +SharedCommand::SharedCommand(string o) : outputDir(o) { try { globaldata = GlobalData::getInstance(); //getting output filename filename = globaldata->inputFileName; - filename = getRootName(filename); + if (outputDir == "") { outputDir += hasPath(filename); } + + filename = outputDir + getRootName(getSimpleName(filename)); filename = filename + "shared"; + openOutputFile(filename, out); pickedGroups = false; @@ -41,7 +44,7 @@ SharedCommand::SharedCommand(){ } //set fileroot - fileroot = getRootName(globaldata->getListFile()); + fileroot = outputDir + getRootName(getSimpleName(globaldata->getListFile())); //clears file before we start to write to it below for (int i=0; iinputFileName << endl; //read in listfile read = new ReadOTUFile(globaldata->inputFileName); read->read(&*globaldata); @@ -196,6 +199,7 @@ int SharedCommand::execute(){ delete it3->second; } + globaldata->setSharedFile(filename); return 0; } @@ -288,10 +292,10 @@ void SharedCommand::createMisMatchFile() { string name = names.substr(0,names.find_first_of(',')); names = names.substr(names.find_first_of(',')+1, names.length()); string group = groupMap->getGroup(name); - cout << name << endl; + if(group == "not found") { outMisMatch << name << endl; } } - cout << names << endl; + //get last name string group = groupMap->getGroup(names); if(group == "not found") { outMisMatch << names << endl; } diff --git a/sharedcommand.h b/sharedcommand.h index 5b922b6..3d39b8b 100644 --- a/sharedcommand.h +++ b/sharedcommand.h @@ -26,7 +26,7 @@ class GlobalData; class SharedCommand : public Command { public: - SharedCommand(); + SharedCommand(string); ~SharedCommand(); int execute(); void help() {} @@ -44,7 +44,7 @@ private: GroupMap* groupMap; vector groups; ofstream out; - string filename, fileroot; + string filename, fileroot, outputDir; bool firsttime, pickedGroups; map filehandles; map::iterator it3; diff --git a/summarycommand.cpp b/summarycommand.cpp index afb4758..0322824 100644 --- a/summarycommand.cpp +++ b/summarycommand.cpp @@ -44,7 +44,7 @@ SummaryCommand::SummaryCommand(string option){ else { //valid paramters for this command - string Array[] = {"label","calc","abund","size"}; + string Array[] = {"label","calc","abund","size","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); @@ -59,7 +59,13 @@ SummaryCommand::SummaryCommand(string option){ //make sure the user has already run the read.otu command if ((globaldata->getSharedFile() == "") && (globaldata->getListFile() == "") && (globaldata->getRabundFile() == "") && (globaldata->getSabundFile() == "")) { mothurOut("You must read a list, sabund, rabund or shared file before you can use the summary.single command."); mothurOutEndLine(); abort = true; } - + + //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 + } + //check for optional parameter and set defaults // ...at some point should added some additional type checking... label = validParameter.validFile(parameters, "label", false); @@ -133,13 +139,14 @@ int SummaryCommand::execute(){ for (int p = 0; p < inputFileNames.size(); p++) { - string fileNameRoot = getRootName(inputFileNames[p]) + "summary"; + string fileNameRoot = outputDir + getRootName(getSimpleName(inputFileNames[p])) + "summary"; globaldata->inputFileName = inputFileNames[p]; if (inputFileNames.size() > 1) { mothurOutEndLine(); mothurOut("Processing group " + groups[p]); mothurOutEndLine(); mothurOutEndLine(); } + sumCalculators.clear(); validCalculator = new ValidCalculators(); diff --git a/summarycommand.h b/summarycommand.h index 1258c65..6f04fc7 100644 --- a/summarycommand.h +++ b/summarycommand.h @@ -37,7 +37,7 @@ private: bool abort, allLines; set labels; //holds labels to be used - string label, calc; + string label, calc, outputDir; vector Estimators; vector inputFileNames; vector groups; diff --git a/summarysharedcommand.cpp b/summarysharedcommand.cpp index e39d70b..081f5ef 100644 --- a/summarysharedcommand.cpp +++ b/summarysharedcommand.cpp @@ -48,7 +48,7 @@ SummarySharedCommand::SummarySharedCommand(string option){ else { //valid paramters for this command - string Array[] = {"label","calc","groups","all"}; + string Array[] = {"label","calc","groups","all","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); @@ -66,6 +66,12 @@ SummarySharedCommand::SummarySharedCommand(string option){ mothurOut("You must read a list and a group, or a shared before you can use the summary.shared command."); mothurOutEndLine(); abort = true; } + //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->getSharedFile()); //if user entered a file with a path then preserve it + } + //check for optional parameter and set defaults // ...at some point should added some additional type checking... label = validParameter.validFile(parameters, "label", false); @@ -151,7 +157,7 @@ SummarySharedCommand::SummarySharedCommand(string option){ } } - outputFileName = ((getRootName(globaldata->inputFileName)) + "shared.summary"); + outputFileName = outputDir + getRootName(getSimpleName(globaldata->inputFileName)) + "shared.summary"; openOutputFile(outputFileName, outputFileHandle); mult = false; } diff --git a/summarysharedcommand.h b/summarysharedcommand.h index 090c9f4..351b135 100644 --- a/summarysharedcommand.h +++ b/summarysharedcommand.h @@ -39,7 +39,7 @@ private: string label, calc, groups; vector Estimators, Groups; vector lookup; - string outputFileName, format, outAllFileName; + string outputFileName, format, outAllFileName, outputDir; ofstream outputFileHandle, outAll; void process(vector); diff --git a/systemcommand.cpp b/systemcommand.cpp index f341d0a..489a2f1 100644 --- a/systemcommand.cpp +++ b/systemcommand.cpp @@ -20,7 +20,17 @@ SystemCommand::SystemCommand(string option){ else { if (option == "") { mothurOut("You must enter a command to run."); mothurOutEndLine(); abort = true; } - else { command = option; } + else { + //check for outputdir and inputdir parameters + int commaPos = option.find_first_of(','); + + //if there is a comma then grab string up to that pos + if (commaPos != option.npos) { + option = option.substr(0, commaPos); + } + + command = option; + } } } diff --git a/treegroupscommand.cpp b/treegroupscommand.cpp index c115c54..e8f68e7 100644 --- a/treegroupscommand.cpp +++ b/treegroupscommand.cpp @@ -36,37 +36,68 @@ TreeGroupCommand::TreeGroupCommand(string option){ else { //valid paramters for this command - string Array[] = {"label","calc","groups", "phylip", "column", "name", "precision","cutoff"}; + string Array[] = {"label","calc","groups", "phylip", "column", "name", "precision","cutoff","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); map parameters = parser. getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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; } @@ -111,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) { @@ -262,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(); @@ -519,7 +556,7 @@ void TreeGroupCommand::process(vector 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++) { diff --git a/treegroupscommand.h b/treegroupscommand.h index 3f2822c..e23dc77 100644 --- a/treegroupscommand.h +++ b/treegroupscommand.h @@ -67,7 +67,7 @@ private: bool abort, allLines; set labels; //holds labels to be used - string phylipfile, columnfile, namefile, calc, groups, label; + string phylipfile, columnfile, namefile, calc, groups, label, outputDir; vector Estimators, Groups; //holds estimators to be used //if the users enters label "0.06" and there is no "0.06" in their file use the next lowest label. diff --git a/trimseqscommand.cpp b/trimseqscommand.cpp index b185da0..4dc07a7 100644 --- a/trimseqscommand.cpp +++ b/trimseqscommand.cpp @@ -21,7 +21,8 @@ TrimSeqsCommand::TrimSeqsCommand(string option){ else { //valid paramters for this command - string AlignArray[] = {"fasta", "flip", "oligos", "maxambig", "maxhomop", "minlength", "maxlength", "qfile", "qthreshold", "qaverage", "allfiles", "qtrim"}; + string AlignArray[] = {"fasta", "flip", "oligos", "maxambig", "maxhomop", "minlength", "maxlength", "qfile", + "qthreshold", "qaverage", "allfiles", "qtrim", "outputdir","inputdir"}; vector myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string))); @@ -29,17 +30,54 @@ TrimSeqsCommand::TrimSeqsCommand(string option){ map parameters = parser.getParameters(); ValidParameters validParameter; + map::iterator it; //check to make sure all parameters are valid for command - for (map::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("fasta"); + //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["fasta"] = inputDir + it->second; } + } + + it = parameters.find("oligos"); + //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["oligos"] = inputDir + it->second; } + } + + it = parameters.find("qfile"); + //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["qfile"] = inputDir + it->second; } + } + } + + //check for required parameters fastaFile = validParameter.validFile(parameters, "fasta", true); if (fastaFile == "not found") { mothurOut("fasta is a required parameter for the screen.seqs command."); mothurOutEndLine(); abort = true; } else if (fastaFile == "not open") { abort = true; } - + + //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(fastaFile); //if user entered a file with a path then preserve it + } //check for optional parameter and set defaults // ...at some point should added some additional type checking... @@ -150,19 +188,19 @@ int TrimSeqsCommand::execute(){ openInputFile(fastaFile, inFASTA); ofstream outFASTA; - string trimSeqFile = getRootName(fastaFile) + "trim.fasta"; + string trimSeqFile = outputDir + getRootName(getSimpleName(fastaFile)) + "trim.fasta"; openOutputFile(trimSeqFile, outFASTA); ofstream outGroups; vector fastaFileNames; if(oligoFile != ""){ - string groupFile = getRootName(fastaFile) + "groups"; + string groupFile = outputDir + getRootName(getSimpleName(fastaFile)) + "groups"; openOutputFile(groupFile, outGroups); getOligos(fastaFileNames); } ofstream scrapFASTA; - string scrapSeqFile = getRootName(fastaFile) + "scrap.fasta"; + string scrapSeqFile = outputDir + getRootName(getSimpleName(fastaFile)) + "scrap.fasta"; openOutputFile(scrapSeqFile, scrapFASTA); ifstream qFile; @@ -249,7 +287,7 @@ int TrimSeqsCommand::execute(){ string seqName; openInputFile(getRootName(fastaFile) + groupVector[i] + ".fasta", inFASTA); ofstream outGroups; - openOutputFile(getRootName(fastaFile) + groupVector[i] + ".groups", outGroups); + openOutputFile(outputDir + getRootName(getSimpleName(fastaFile)) + groupVector[i] + ".groups", outGroups); while(!inFASTA.eof()){ if(inFASTA.get() == '>'){ @@ -311,7 +349,7 @@ void TrimSeqsCommand::getOligos(vector& outFASTAVec){ groupVector.push_back(group); if(allFiles){ - outFASTAVec.push_back(new ofstream((getRootName(fastaFile) + group + ".fasta").c_str(), ios::ate)); + outFASTAVec.push_back(new ofstream((outputDir + getRootName(getSimpleName(fastaFile)) + group + ".fasta").c_str(), ios::ate)); } } } diff --git a/trimseqscommand.h b/trimseqscommand.h index ca02961..9b02be9 100644 --- a/trimseqscommand.h +++ b/trimseqscommand.h @@ -34,7 +34,7 @@ private: bool compareDNASeq(string, string); bool abort; - string fastaFile, oligoFile, qFileName; + string fastaFile, oligoFile, qFileName, outputDir; bool flip, allFiles, qtrim; int numFPrimers, numRPrimers, maxAmbig, maxHomoP, minLength, maxLength, qThreshold, qAverage; diff --git a/unifracunweightedcommand.cpp b/unifracunweightedcommand.cpp index 1512284..957a4aa 100644 --- a/unifracunweightedcommand.cpp +++ b/unifracunweightedcommand.cpp @@ -21,7 +21,7 @@ UnifracUnweightedCommand::UnifracUnweightedCommand(string option) { else { //valid paramters for this command - string Array[] = {"groups","iters","distance","random"}; + string Array[] = {"groups","iters","distance","random", "outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); @@ -36,7 +36,13 @@ UnifracUnweightedCommand::UnifracUnweightedCommand(string option) { if (globaldata->gTree.size() == 0) {//no trees were read mothurOut("You must execute the read.tree command, before you may execute the unifrac.unweighted command."); mothurOutEndLine(); abort = true; } - + + //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 + } + //check for optional parameter and set defaults // ...at some point should added some additional type checking... groups = validParameter.validFile(parameters, "groups", false); @@ -67,7 +73,7 @@ UnifracUnweightedCommand::UnifracUnweightedCommand(string option) { if (abort == false) { T = globaldata->gTree; tmap = globaldata->gTreemap; - sumFile = globaldata->getTreeFile() + ".uwsummary"; + sumFile = outputDir + getSimpleName(globaldata->getTreeFile()) + ".uwsummary"; openOutputFile(sumFile, outSum); util = new SharedUtil(); @@ -129,7 +135,7 @@ int UnifracUnweightedCommand::execute() { for (int i = 0; i < T.size(); i++) { counter = 0; - if (random) { output = new ColumnFile(globaldata->getTreeFile() + toString(i+1) + ".unweighted", itersString); } + if (random) { output = new ColumnFile(outputDir + getSimpleName(globaldata->getTreeFile()) + toString(i+1) + ".unweighted", itersString); } //get unweighted for users tree rscoreFreq.resize(numComp); @@ -272,7 +278,7 @@ void UnifracUnweightedCommand::printUWSummaryFile(int i) { /***********************************************************/ void UnifracUnweightedCommand::createPhylipFile(int i) { try { - string phylipFileName = globaldata->getTreeFile() + toString(i+1) + ".unweighted.dist"; + string phylipFileName = outputDir + getSimpleName(globaldata->getTreeFile()) + toString(i+1) + ".unweighted.dist"; ofstream out; openOutputFile(phylipFileName, out); diff --git a/unifracunweightedcommand.h b/unifracunweightedcommand.h index 155fa75..c98abc4 100644 --- a/unifracunweightedcommand.h +++ b/unifracunweightedcommand.h @@ -46,7 +46,7 @@ class UnifracUnweightedCommand : public Command { vector< map > rCumul; //map -vector entry for each combination. bool abort, phylip, random; - string groups, itersString; + string groups, itersString, outputDir; vector Groups; //holds groups to be used ofstream outSum, out; diff --git a/unifracweightedcommand.cpp b/unifracweightedcommand.cpp index ea79475..a9f9570 100644 --- a/unifracweightedcommand.cpp +++ b/unifracweightedcommand.cpp @@ -21,7 +21,7 @@ UnifracWeightedCommand::UnifracWeightedCommand(string option) { else { //valid paramters for this command - string Array[] = {"groups","iters","distance","random"}; + string Array[] = {"groups","iters","distance","random","outputdir","inputdir"}; vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); OptionParser parser(option); @@ -36,7 +36,13 @@ UnifracWeightedCommand::UnifracWeightedCommand(string option) { if (globaldata->gTree.size() == 0) {//no trees were read mothurOut("You must execute the read.tree command, before you may execute the unifrac.weighted command."); mothurOutEndLine(); abort = true; } - + + //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 + } + //check for optional parameter and set defaults // ...at some point should added some additional type checking... groups = validParameter.validFile(parameters, "groups", false); @@ -61,7 +67,7 @@ UnifracWeightedCommand::UnifracWeightedCommand(string option) { if (abort == false) { T = globaldata->gTree; tmap = globaldata->gTreemap; - sumFile = globaldata->getTreeFile() + ".wsummary"; + sumFile = outputDir + getSimpleName(globaldata->getTreeFile()) + ".wsummary"; openOutputFile(sumFile, outSum); util = new SharedUtil(); @@ -125,7 +131,7 @@ int UnifracWeightedCommand::execute() { rScores.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC... uScores.resize(numComp); //data[0] = weightedscore AB, data[1] = weightedscore AC... - if (random) { output = new ColumnFile(globaldata->getTreeFile() + toString(i+1) + ".weighted", itersString); } + if (random) { output = new ColumnFile(outputDir + getSimpleName(globaldata->getTreeFile()) + toString(i+1) + ".weighted", itersString); } userData = weighted->getValues(T[i]); //userData[0] = weightedscore @@ -283,7 +289,7 @@ void UnifracWeightedCommand::createPhylipFile() { //for each tree for (int i = 0; i < T.size(); i++) { - string phylipFileName = globaldata->getTreeFile() + toString(i+1) + ".weighted.dist"; + string phylipFileName = outputDir + getSimpleName(globaldata->getTreeFile()) + toString(i+1) + ".weighted.dist"; ofstream out; openOutputFile(phylipFileName, out); diff --git a/unifracweightedcommand.h b/unifracweightedcommand.h index 9c0ad99..c36152d 100644 --- a/unifracweightedcommand.h +++ b/unifracweightedcommand.h @@ -39,7 +39,7 @@ class UnifracWeightedCommand : public Command { Tree* randT; //random tree TreeMap* tmap; Weighted* weighted; - string sumFile; + string sumFile, outputDir; int iters, numGroups, numComp, counter; EstOutput userData; //weighted score info for user tree EstOutput randomData; //weighted score info for random trees diff --git a/venn.cpp b/venn.cpp index 8370701..986c68d 100644 --- a/venn.cpp +++ b/venn.cpp @@ -16,7 +16,7 @@ //********************************************************************************************************************** -Venn::Venn(){ +Venn::Venn(string o) : outputDir(o) { try { globaldata = GlobalData::getInstance(); @@ -31,7 +31,7 @@ void Venn::getPic(SAbundVector* sabund, vector vCalcs) { try { for(int i=0;iinputFileName + ".venn." + sabund->getLabel() + vCalcs[i]->getName() + ".svg"; + string filenamesvg = outputDir + getSimpleName(globaldata->inputFileName) + ".venn." + sabund->getLabel() + vCalcs[i]->getName() + ".svg"; openOutputFile(filenamesvg, outsvg); vector data = vCalcs[i]->getValues(sabund); @@ -73,7 +73,7 @@ void Venn::getPic(vector lookup, vector vCalcs //make a file for each calculator for(int i=0;iinputFileName) + lookup[0]->getLabel() + ".venn." + vCalcs[i]->getName() + ".svg"; + string filenamesvg = outputDir + getSimpleName(globaldata->inputFileName) + lookup[0]->getLabel() + ".venn." + vCalcs[i]->getName() + ".svg"; openOutputFile(filenamesvg, outsvg); //in essence you want to run it like a single @@ -122,7 +122,7 @@ void Venn::getPic(vector lookup, vector vCalcs //make a file for each calculator for(int i=0;iinputFileName) + lookup[0]->getLabel() + ".venn." + vCalcs[i]->getName() + ".svg"; + string filenamesvg = outputDir + getSimpleName(globaldata->inputFileName) + lookup[0]->getLabel() + ".venn." + vCalcs[i]->getName() + ".svg"; openOutputFile(filenamesvg, outsvg); //get estimates for sharedAB @@ -191,7 +191,7 @@ void Venn::getPic(vector lookup, vector vCalcs for(int i=0;iinputFileName) + lookup[0]->getLabel() + ".venn." + vCalcs[i]->getName() + ".svg"; + string filenamesvg = outputDir + getSimpleName(globaldata->inputFileName) + lookup[0]->getLabel() + ".venn." + vCalcs[i]->getName() + ".svg"; openOutputFile(filenamesvg, outsvg); if (vCalcs[i]->getName() == "sharedace") { @@ -446,7 +446,7 @@ void Venn::getPic(vector lookup, vector vCalcs if ((vCalcs[i]->getName() != "sharedsobs") && (vCalcs[i]->getName() != "sharedchao")) { mothurOut(vCalcs[i]->getName() + " is not a valid calculator with four groups. It will be disregarded. "); mothurOutEndLine(); } else{ - string filenamesvg = getRootName(globaldata->inputFileName) + lookup[0]->getLabel() + ".venn." + vCalcs[i]->getName() + ".svg"; + string filenamesvg = outputDir + getSimpleName(globaldata->inputFileName) + lookup[0]->getLabel() + ".venn." + vCalcs[i]->getName() + ".svg"; openOutputFile(filenamesvg, outsvg); diff --git a/venn.h b/venn.h index 8902cf4..cf6a5ee 100644 --- a/venn.h +++ b/venn.h @@ -20,7 +20,7 @@ class Venn { public: - Venn(); + Venn(string); ~Venn(){}; void getPic(SAbundVector*, vector); @@ -29,7 +29,7 @@ public: private: GlobalData* globaldata; Calculator* singleCalc; - string groupComb; + string groupComb, outputDir; ofstream outsvg; }; diff --git a/venncommand.cpp b/venncommand.cpp index e26dbcc..437f044 100644 --- a/venncommand.cpp +++ b/venncommand.cpp @@ -31,7 +31,7 @@ VennCommand::VennCommand(string option){ else { //valid paramters for this command - string AlignArray[] = {"groups","label","calc", "abund"}; + string AlignArray[] = {"groups","label","calc", "abund","outputdir","inputdir"}; vector myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string))); OptionParser parser(option); @@ -48,6 +48,12 @@ VennCommand::VennCommand(string option){ if ((globaldata->getListFile() == "") && (globaldata->getSharedFile() == "")) { mothurOut("You must read a list, or a list and a group, or a shared before you can use the venn command."); mothurOutEndLine(); abort = true; } + + //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 + } //check for optional parameter and set defaults // ...at some point should added some additional type checking... @@ -122,7 +128,7 @@ VennCommand::VennCommand(string option){ } } - venn = new Venn(); + venn = new Venn(outputDir); } } diff --git a/venncommand.h b/venncommand.h index cc6b333..aa1e856 100644 --- a/venncommand.h +++ b/venncommand.h @@ -42,7 +42,7 @@ private: bool abort, allLines; set labels; //holds labels to be used - string format, groups, calc, label; + string format, groups, calc, label, outputDir; vector Estimators, Groups; -- 2.39.2