]> git.donarmstrong.com Git - mothur.git/blob - command.hpp
changes while testing
[mothur.git] / command.hpp
1 #ifndef COMMAND_HPP
2 #define COMMAND_HPP
3 //test2
4 /*
5  *  command.h
6  *  nast
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                         vector<int> numVariablesPerPattern;
54                         for (int i = 0; i < patterns.size(); i++) {
55                             int numVariables = 0;
56                             for (int j = 0; j < patterns[i].length(); j++) { if (patterns[i][j] == '[') { numVariables++; } }
57                             numVariablesPerPattern.push_back(numVariables);
58                             
59                             if (numVariables == variableParts.size()) { pattern = patterns[i]; foundPattern = true; break; }
60                         }
61                         
62                         //if you didn't find an exact match do we have something that might work
63                         if (!foundPattern) {  
64                             for (int i = 0; i < numVariablesPerPattern.size(); i++) {
65                                 if (numVariablesPerPattern[i] < variableParts.size()) { pattern = patterns[i]; foundPattern = true; break; }
66                             }
67                             if (!foundPattern) {  m->mothurOut("[ERROR]: Not enough variable pieces for " + type + ".\n"); m->control_pressed = true; }
68                         }
69                         
70                         if (pattern != "") {
71                             int numVariables = 0;
72                             for (int i = 0; i < pattern.length(); i++) { if (pattern[i] == '[') { numVariables++; } }
73                             
74                             vector<string> pieces;
75                             m->splitAtComma(pattern, pieces);
76                             
77                         
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;
83                                     }else {
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);
89                                                 }
90                                                 filename += it->second + "."; 
91                                             }else { filename += it->second + "."; }
92                                         }
93                                     }
94                                 }else {
95                                     filename += pieces[i] + ".";
96                                 }
97                             }
98                             if (filename.length() > 0) { //rip off last "."
99                                 filename = filename.substr(0, filename.length()-1);
100                             }
101                         }
102                     }
103                     return filename;
104                 }
105                 catch(exception& e) {
106                     m->errorOut(e, "command", "getOutputFileName");
107                     exit(1);
108                 }
109         }
110         
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; }
114         
115                 virtual int execute() = 0;
116                 virtual void help() = 0;
117                 void citation() { m->mothurOutEndLine(); m->mothurOut(getCitation()); m->mothurOutEndLine(); }
118                 virtual ~Command() { }
119         
120         protected:
121                 MothurOut* m;
122                 bool calledHelp;
123                         
124                 map<string, vector<string> > outputTypes;
125                 vector<CommandParameter> parameters;
126         
127                 map<string, vector<string> >::iterator itTypes;
128 };
129
130 #endif