File: command.hpp

package info (click to toggle)
mothur 1.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,684 kB
  • sloc: cpp: 161,854; makefile: 122; sh: 31
file content (153 lines) | stat: -rwxr-xr-x 7,301 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#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.  */


#include "mothur.h"
#include "optionparser.h"
#include "validparameter.h"
#include "mothurout.h"
#include "commandparameter.h"
#include "currentfile.h"
#include "utils.hpp"

class Command {
	
	public:
		Command() {
            m = MothurOut::getInstance();  current = CurrentFile::getInstance();
            inputdirs = current->getInputDir();
            outputdir = current->getOutputDir();
        }
		
		//needed by gui
		virtual string getCommandName() = 0;
		virtual string getCommandCategory() = 0;
		virtual string getHelpString() = 0;
		virtual string getCitation() = 0;
		virtual string getDescription() = 0;
        virtual string getCommonQuestions()     {  return "Common Questions for commands."; }
        virtual string getCommandParameters() {
            vector<string> parameterNames = setParameters();
            sort(parameterNames.begin(), parameterNames.end());
            string results = util.getStringFromVector(parameterNames, ", ");
            return results;
        }
		
		virtual map<string, vector<string> > getOutputFiles() { return outputTypes; }
        string getOutputFileName(string type, map<string, string> variableParts) {  //uses the pattern to create an output filename for a given type and input file name.  
               try {
                    string filename = "";
                    map<string, vector<string> >::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<string> patterns;
                        util.splitAtDash(patternTemp, patterns);
                        
                        //find pattern to use based on number of variables passed in
                        string pattern = "";
                        bool foundPattern = false;
                        vector<int> 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->setControl_pressed(true); }
                        }
                        
                        if (pattern != "") {
                            int numVariables = 0;
                            for (int i = 0; i < pattern.length(); i++) { if (pattern[i] == '[') { numVariables++; } }
                            
                            vector<string> pieces;
                            util.splitAtComma(pattern, pieces);
                            
                        
                            for (int i = 0; i < pieces.size(); i++) {
                                if (pieces[i][0] == '[') {
                                    map<string, string>::iterator it = variableParts.find(pieces[i]);
                                    if (it == variableParts.end()) {
                                        m->mothurOut("[ERROR]: Did not provide variable for " + pieces[i] + ".\n"); m->setControl_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 if ((it->first == "[group]") || (it->first == "[tag]")) {
                                                string group = it->second;
                                                for (int j = 0; j < group.length(); j++) {
                                                    if (group[j] == '-') { group[j] = '_'; }
                                                }
                                                filename += group + ".";
                                            }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<string> setParameters() = 0; //to fill parameters
		virtual vector<CommandParameter> getParameters() { return parameters; }
	
		virtual int execute() = 0;
		virtual void help() = 0;
		void citation()         { m->mothurOut("\n"+getCitation()+"\n");    }
        void commonQuestions()  {  m->mothurOut("\n"+getCommonQuestions()+"\n");   }
		virtual ~Command() { }
	
	protected:
		MothurOut* m;
        Utils util;
        CurrentFile* current;
		bool calledHelp;
        string outputdir;
			
        vector<string> inputdirs;
		map<string, vector<string> > outputTypes;
		vector<CommandParameter> parameters;
	
		map<string, vector<string> >::iterator itTypes;
};

#endif