]> 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 if (it->first == "[extension]") { 
79                                                 if (filename.length() > 0) { //rip off last "."
80                                                     filename = filename.substr(0, filename.length()-1);
81                                                 }
82                                                 filename += it->second + "."; 
83                                             }else { filename += it->second + "."; }
84                                         }
85                                     }
86                                 }else {
87                                     filename += pieces[i] + ".";
88                                 }
89                             }
90                             if (filename.length() > 0) { //rip off last "."
91                                 filename = filename.substr(0, filename.length()-1);
92                             }
93                         }
94                     }
95                     return filename;
96                 }
97                 catch(exception& e) {
98                     m->errorOut(e, "command", "getOutputFileName");
99                     exit(1);
100                 }
101         }
102         
103         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]
104                 virtual vector<string> setParameters() = 0; //to fill parameters
105                 virtual vector<CommandParameter> getParameters() { return parameters; }
106         
107                 virtual int execute() = 0;
108                 virtual void help() = 0;
109                 void citation() { m->mothurOutEndLine(); m->mothurOut(getCitation()); m->mothurOutEndLine(); }
110                 virtual ~Command() { }
111         
112         protected:
113                 MothurOut* m;
114                 bool calledHelp;
115                         
116                 map<string, vector<string> > outputTypes;
117                 vector<CommandParameter> parameters;
118         
119                 map<string, vector<string> >::iterator itTypes;
120 };
121
122 #endif