5 * Created by westcott on 4/6/11.
6 * Copyright 2011 Schloss Lab. All rights reserved.
10 #include "getcommandinfocommand.h"
12 //**********************************************************************************************************************
13 vector<string> GetCommandInfoCommand::setParameters(){
15 CommandParameter poutput("output", "String", "", "", "", "", "",false,false); parameters.push_back(poutput);
16 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
17 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
19 vector<string> myArray;
20 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
24 m->errorOut(e, "GetCommandInfoCommand", "setParameters");
28 //**********************************************************************************************************************
29 string GetCommandInfoCommand::getHelpString(){
31 string helpString = "";
32 helpString += "This command is used by the gui to get the information about current commands available in mothur.\n";
36 m->errorOut(e, "GetCommandInfoCommand", "getHelpString");
40 //**********************************************************************************************************************
42 GetCommandInfoCommand::GetCommandInfoCommand(string option) {
44 abort = false; calledHelp = false;
46 //allow user to run help
47 if(option == "help") { help(); abort = true; calledHelp = true; }
50 vector<string> myArray = setParameters();
52 OptionParser parser(option);
53 map<string, string> parameters = parser.getParameters();
55 ValidParameters validParameter;
56 //check to make sure all parameters are valid for command
57 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
58 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
61 output = validParameter.validFile(parameters, "output", false);
62 if (output == "not found") { output = ""; m->mothurOut("You must provide an output filename."); m->mothurOutEndLine(); abort=true; }
67 m->errorOut(e, "GetCommandInfoCommand", "GetCommandInfoCommand");
71 //**********************************************************************************************************************
73 int GetCommandInfoCommand::execute(){
76 if (abort == true) { if (calledHelp) { return 0; } return 2; }
78 commandFactory = CommandFactory::getInstance();
81 m->openOutputFile(output+".temp", out);
85 out << "mothurLocation=" << m->getFullPathName(m->argv) << endl;
86 out << "mothurVersion=" << m->getVersion() << endl;
88 map<string, string> commands = commandFactory->getListCommands();
89 map<string, string>::iterator it;
91 //loop through each command outputting info
92 for (it = commands.begin(); it != commands.end(); it++) {
94 if (m->control_pressed) { m->mothurOut("[ERROR]: did not complete making the file."); m->mothurOutEndLine(); out.close(); remove((output+".temp").c_str()); }
96 Command* thisCommand = commandFactory->getCommand(it->first);
98 //don't add hidden commands
99 if (thisCommand->getCommandCategory() != "Hidden") {
103 out << "commandName=" << thisCommand->getCommandName() << endl;
104 out << "commandCategory=" << thisCommand->getCommandCategory() << endl;
106 //remove /n from help string since gui reads line by line
107 string myhelpString = thisCommand->getHelpString();
108 string newHelpString = "";
109 for (int i = 0; i < myhelpString.length(); i++) {
110 if (myhelpString[i] != '\n') { newHelpString += myhelpString[i]; }
112 out << "help=" << newHelpString << endl;
114 //outputTypes - makes something like outputTypes=fasta-name-qfile
115 map<string, vector<string> > thisOutputTypes = thisCommand->getOutputFiles();
116 map<string, vector<string> >::iterator itTypes;
118 if (thisOutputTypes.size() == 0) { out << "outputTypes=none" << endl; }
121 for (itTypes = thisOutputTypes.begin(); itTypes != thisOutputTypes.end(); itTypes++) { types += itTypes->first + "-"; }
123 types = types.substr(0, types.length()-1);
124 out << "outputTypes=" << types << endl;
127 vector<string> booleans; vector<string> numbers; vector<string> multiples; vector<string> Strings;
128 vector<string> inputGroupNames; map<string, string> inputTypes;
130 getInfo(thisCommand->getParameters(), booleans, numbers, multiples, Strings, inputGroupNames, inputTypes);
133 out << "Boolean=" << booleans.size() << endl;
134 for (int i = 0; i < booleans.size(); i++) { out << booleans[i] << endl; }
137 out << "Multiple=" << multiples.size() << endl;
138 for (int i = 0; i < multiples.size(); i++) { out << multiples[i] << endl; }
141 out << "Numbers=" << numbers.size() << endl;
142 for (int i = 0; i < numbers.size(); i++) { out << numbers[i] << endl; }
145 out << "String=" << Strings.size() << endl;
146 for (int i = 0; i < Strings.size(); i++) { out << Strings[i] << endl; }
149 out << "inputGroupNames=" << inputGroupNames.size() << endl;
150 for (int i = 0; i < inputGroupNames.size(); i++) { out << inputGroupNames[i] << endl; }
153 if (inputTypes.size() == 0) { out << "inputTypes=" << endl; }
156 for (map<string, string>::iterator it2 = inputTypes.begin(); it2 != inputTypes.end(); it2++) { types += it2->first + "-"; }
158 types = types.substr(0, types.length()-1);
159 out << "inputTypes=" << types << endl;
161 for (map<string, string>::iterator it2 = inputTypes.begin(); it2 != inputTypes.end(); it2++) {
162 out << it2->first << "=" << it2->second << endl;
172 m->openOutputFile(output, out2);
173 out2 << numNonHidden << endl;
176 m->appendFiles(output+".temp", output);
177 remove((output+".temp").c_str());
179 m->mothurOutEndLine();
180 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
181 m->mothurOut(output); m->mothurOutEndLine();
182 m->mothurOutEndLine();
187 catch(exception& e) {
188 m->errorOut(e, "GetCommandInfoCommand", "execute");
192 //**********************************************************************************************************************
194 int GetCommandInfoCommand::getInfo(vector<CommandParameter> para, vector<string>& booleans, vector<string>& numbers, vector<string>& multiples, vector<string>& strings, vector<string>& inputGroupNames, map<string, string>& inputTypes){
197 map<string, set<string> > groups;
198 map<string, set<string> >::iterator itGroups;
200 for (int i = 0; i < para.size(); i++) {
201 if ((para[i].name == "inputdir") || (para[i].name == "outputdir")) {} //ignore
203 if (para[i].type == "Boolean") {
204 string temp = para[i].name + "=" + para[i].optionsDefault;
205 booleans.push_back(temp);
206 }else if (para[i].type == "Multiple") {
207 string multAllowed = "F";
208 if (para[i].multipleSelectionAllowed) { multAllowed = "T"; }
209 string temp = para[i].name + "=" + para[i].options + "|" + para[i].optionsDefault + "|" + multAllowed;
210 multiples.push_back(temp);
211 }else if (para[i].type == "Number") {
212 string temp = para[i].name + "=" + para[i].optionsDefault;
213 numbers.push_back(temp);
214 }else if (para[i].type == "String") {
215 string temp = para[i].name + "=" + para[i].optionsDefault;
216 strings.push_back(temp);
217 }else if (para[i].type == "InputTypes") {
218 string required = "F";
219 if (para[i].required) { required = "T"; }
220 string temp = required + "|" + para[i].chooseOnlyOneGroup + "|" + para[i].chooseAtLeastOneGroup + "|" + para[i].linkedGroup;
221 inputTypes[para[i].name] = temp;
223 //add choose only one groups
224 groups[para[i].chooseOnlyOneGroup].insert(para[i].name);
226 //add at least one group names
227 groups[para[i].chooseAtLeastOneGroup].insert(para[i].name);
229 //add at linked group names
230 groups[para[i].linkedGroup].insert(para[i].name);
232 }else { m->mothurOut("[ERROR]: " + para[i].type + " is an unknown parameter type, please correct."); m->mothurOutEndLine(); }
236 for (itGroups = groups.begin(); itGroups != groups.end(); itGroups++) {
237 if (itGroups->first != "none") {
238 set<string> tempNames = itGroups->second;
239 string temp = itGroups->first + "=";
240 for (set<string>::iterator itNames = tempNames.begin(); itNames != tempNames.end(); itNames++) {
241 temp += *itNames + "-";
244 temp = temp.substr(0, temp.length()-1);
245 inputGroupNames.push_back(temp);
252 catch(exception& e) {
253 m->errorOut(e, "GetCommandInfoCommand", "getInfo");
257 //**********************************************************************************************************************/