8 * Created by Pat Schloss on 10/23/08.
9 * Copyright 2008 Patrick D. Schloss. All rights reserved.
13 /*This class is a parent to all the command classes. */
17 #include "optionparser.h"
18 #include "validparameter.h"
19 #include "mothurout.h"
20 #include "commandparameter.h"
26 Command() { m = MothurOut::getInstance(); }
29 virtual string getCommandName() = 0;
30 virtual string getCommandCategory() = 0;
31 virtual string getHelpString() = 0;
32 virtual string getCitation() = 0;
33 virtual string getDescription() = 0;
35 virtual map<string, vector<string> > getOutputFiles() { return outputTypes; }
36 string getOutputFileName(string type, map<string, string> variableParts) { //uses the pattern to create an output filename for a given type and input file name.
39 map<string, vector<string> >::iterator it;
41 //is this a type this command creates
42 it = outputTypes.find(type);
43 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
46 string patternTemp = getOutputPattern(type);
47 vector<string> patterns;
48 m->splitAtDash(patternTemp, patterns);
50 //find pattern to use based on number of variables passed in
52 bool foundPattern = false;
53 vector<int> numVariablesPerPattern;
54 for (int i = 0; i < patterns.size(); i++) {
56 for (int j = 0; j < patterns[i].length(); j++) { if (patterns[i][j] == '[') { numVariables++; } }
57 numVariablesPerPattern.push_back(numVariables);
59 if (numVariables == variableParts.size()) { pattern = patterns[i]; foundPattern = true; break; }
62 //if you didn't find an exact match do we have something that might work
64 for (int i = 0; i < numVariablesPerPattern.size(); i++) {
65 if (numVariablesPerPattern[i] < variableParts.size()) { pattern = patterns[i]; foundPattern = true; break; }
67 if (!foundPattern) { m->mothurOut("[ERROR]: Not enough variable pieces for " + type + ".\n"); m->control_pressed = true; }
72 for (int i = 0; i < pattern.length(); i++) { if (pattern[i] == '[') { numVariables++; } }
74 vector<string> pieces;
75 m->splitAtComma(pattern, pieces);
78 for (int i = 0; i < pieces.size(); i++) {
79 if (pieces[i][0] == '[') {
80 map<string, string>::iterator it = variableParts.find(pieces[i]);
81 if (it == variableParts.end()) {
82 m->mothurOut("[ERROR]: Did not provide variable for " + pieces[i] + ".\n"); m->control_pressed = true;
84 if (it->second != "") {
85 if (it->first == "[filename]") { filename += it->second; }
86 else if (it->first == "[extension]") {
87 if (filename.length() > 0) { //rip off last "."
88 filename = filename.substr(0, filename.length()-1);
90 filename += it->second + ".";
91 }else { filename += it->second + "."; }
95 filename += pieces[i] + ".";
98 if (filename.length() > 0) { //rip off last "."
99 filename = filename.substr(0, filename.length()-1);
105 catch(exception& e) {
106 m->errorOut(e, "command", "getOutputFileName");
111 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]
112 virtual vector<string> setParameters() = 0; //to fill parameters
113 virtual vector<CommandParameter> getParameters() { return parameters; }
115 virtual int execute() = 0;
116 virtual void help() = 0;
117 void citation() { m->mothurOutEndLine(); m->mothurOut(getCitation()); m->mothurOutEndLine(); }
118 virtual ~Command() { }
124 map<string, vector<string> > outputTypes;
125 vector<CommandParameter> parameters;
127 map<string, vector<string> >::iterator itTypes;