]> git.donarmstrong.com Git - mothur.git/blob - command.hpp
Merge remote-tracking branch 'origin/master'
[mothur.git] / command.hpp
1 #ifndef COMMAND_HPP
2 #define COMMAND_HPP
3 //test2
4 /*
5  *  command.h
6  *  
7  *
8  *  Created by Pat Schloss on 10/23/08.
9  *  Copyright 2008 Patrick D. Schloss. All rights reserved.
10  *
11  */
12
13 /*This class is a parent to all the command classes.  */
14
15
16 #include "mothur.h"
17 #include "optionparser.h"
18 #include "validparameter.h"
19 #include "mothurout.h"
20 #include "commandparameter.h"
21
22
23 class Command {
24         
25         public:
26                 Command() {  m = MothurOut::getInstance();   } 
27                 
28                 //needed by gui
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;
34                 
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.  
37                try {
38                     string filename = "";
39                     map<string, vector<string> >::iterator it;
40                     
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"); }
44                     else {
45                         
46                         string patternTemp = getOutputPattern(type);
47                         vector<string> patterns;
48                         m->splitAtDash(patternTemp, patterns);
49                         
50                         //find pattern to use based on number of variables passed in
51                         string pattern = "";
52                         bool foundPattern = false;
53                         for (int i = 0; i < patterns.size(); i++) {
54                             int numVariables = 0;
55                             for (int j = 0; j < patterns[i].length(); j++) { if (patterns[i][j] == '[') { numVariables++; } }
56                             
57                             if (numVariables == variableParts.size()) { pattern = patterns[i]; foundPattern = true; break; }
58                         }
59                         
60                         if (!foundPattern) {  m->mothurOut("[ERROR]: Not enough variable pieces for " + type + ".\n"); m->control_pressed = true; }
61                         
62                         if (pattern != "") {
63                             int numVariables = 0;
64                             for (int i = 0; i < pattern.length(); i++) { if (pattern[i] == '[') { numVariables++; } }
65                             
66                             vector<string> pieces;
67                             m->splitAtComma(pattern, pieces);
68                             
69                         
70                             for (int i = 0; i < pieces.size(); i++) {
71                                 if (pieces[i][0] == '[') {
72                                     map<string, string>::iterator it = variableParts.find(pieces[i]);
73                                     if (it == variableParts.end()) {
74                                         m->mothurOut("[ERROR]: Did not provide variable for " + pieces[i] + ".\n"); m->control_pressed = true;
75                                     }else {
76                                         if (it->second != "") {
77                                             if (it->first != "[filename]") { filename += it->second + "."; }
78                                             else { filename += it->second; }
79                                         }
80                                     }
81                                 }else {
82                                     filename += pieces[i] + ".";
83                                 }
84                             }
85                             if (filename.length() > 0) { //rip off last "."
86                                 filename = filename.substr(0, filename.length()-1);
87                             }
88                         }
89                     }
90                     return filename;
91                 }
92                 catch(exception& e) {
93                     m->errorOut(e, "command", "getOutputFileName");
94                     exit(1);
95                 }
96         }
97         
98         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]
99                 virtual vector<string> setParameters() = 0; //to fill parameters
100                 virtual vector<CommandParameter> getParameters() { return parameters; }
101         
102                 virtual int execute() = 0;
103                 virtual void help() = 0;
104                 void citation() { m->mothurOutEndLine(); m->mothurOut(getCitation()); m->mothurOutEndLine(); }
105                 virtual ~Command() { }
106         
107         protected:
108                 MothurOut* m;
109                 bool calledHelp;
110                         
111                 map<string, vector<string> > outputTypes;
112                 vector<CommandParameter> parameters;
113         
114                 map<string, vector<string> >::iterator itTypes;
115 };
116
117 #endif