X-Git-Url: https://git.donarmstrong.com/?p=mothur.git;a=blobdiff_plain;f=command.hpp;h=37186529ab1f75e5747138d74f976c5df3c1bbe9;hp=3e655abb19ed35729792e3f5783f119095c6fc61;hb=615301e57c25e241356a9c2380648d117709458d;hpb=0470f6d037aacb3563c3f7010708120a4a67d4e6 diff --git a/command.hpp b/command.hpp index 3e655ab..3718652 100644 --- a/command.hpp +++ b/command.hpp @@ -1,26 +1,130 @@ #ifndef COMMAND_HPP #define COMMAND_HPP - +//test2 /* * command.h - * + * nast * * Created by Pat Schloss on 10/23/08. * Copyright 2008 Patrick D. Schloss. All rights reserved. * */ -/*This class is a parent to all the command classes. It has one pure int execute(). */ +/*This class is a parent to all the command classes. */ #include "mothur.h" #include "optionparser.h" #include "validparameter.h" +#include "mothurout.h" +#include "commandparameter.h" + class Command { + public: + Command() { m = MothurOut::getInstance(); } + + //needed by gui + virtual string getCommandName() = 0; + virtual string getCommandCategory() = 0; + virtual string getHelpString() = 0; + virtual string getCitation() = 0; + virtual string getDescription() = 0; + + virtual map > getOutputFiles() { return outputTypes; } + string getOutputFileName(string type, map variableParts) { //uses the pattern to create an output filename for a given type and input file name. + try { + string filename = ""; + map >::iterator it; + + //is this a type this command creates + it = outputTypes.find(type); + if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); } + else { + + string patternTemp = getOutputPattern(type); + vector patterns; + m->splitAtDash(patternTemp, patterns); + + //find pattern to use based on number of variables passed in + string pattern = ""; + bool foundPattern = false; + vector numVariablesPerPattern; + for (int i = 0; i < patterns.size(); i++) { + int numVariables = 0; + for (int j = 0; j < patterns[i].length(); j++) { if (patterns[i][j] == '[') { numVariables++; } } + numVariablesPerPattern.push_back(numVariables); + + if (numVariables == variableParts.size()) { pattern = patterns[i]; foundPattern = true; break; } + } + + //if you didn't find an exact match do we have something that might work + if (!foundPattern) { + for (int i = 0; i < numVariablesPerPattern.size(); i++) { + if (numVariablesPerPattern[i] < variableParts.size()) { pattern = patterns[i]; foundPattern = true; break; } + } + if (!foundPattern) { m->mothurOut("[ERROR]: Not enough variable pieces for " + type + ".\n"); m->control_pressed = true; } + } + + if (pattern != "") { + int numVariables = 0; + for (int i = 0; i < pattern.length(); i++) { if (pattern[i] == '[') { numVariables++; } } + + vector pieces; + m->splitAtComma(pattern, pieces); + + + for (int i = 0; i < pieces.size(); i++) { + if (pieces[i][0] == '[') { + map::iterator it = variableParts.find(pieces[i]); + if (it == variableParts.end()) { + m->mothurOut("[ERROR]: Did not provide variable for " + pieces[i] + ".\n"); m->control_pressed = true; + }else { + if (it->second != "") { + if (it->first == "[filename]") { filename += it->second; } + else if (it->first == "[extension]") { + if (filename.length() > 0) { //rip off last "." + filename = filename.substr(0, filename.length()-1); + } + filename += it->second + "."; + }else { filename += it->second + "."; } + } + } + }else { + filename += pieces[i] + "."; + } + } + if (filename.length() > 0) { //rip off last "." + filename = filename.substr(0, filename.length()-1); + } + } + } + return filename; + } + catch(exception& e) { + m->errorOut(e, "command", "getOutputFileName"); + exit(1); + } + } + + virtual string getOutputPattern(string) = 0; //pass in type, returns something like: [filename],align or [filename],[distance],subsample.shared strings in [] means its a variable. This is used by the gui to predict output file names. use variable keywords: [filename], [distance], [group], [extension], [tag] + virtual vector setParameters() = 0; //to fill parameters + virtual vector getParameters() { return parameters; } + virtual int execute() = 0; virtual void help() = 0; + void citation() { m->mothurOutEndLine(); m->mothurOut(getCitation()); m->mothurOutEndLine(); } + virtual ~Command() { } + + protected: + MothurOut* m; + bool calledHelp; + + map > outputTypes; + vector parameters; + + map >::iterator itTypes; }; #endif