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; }
48 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
51 vector<string> myArray = setParameters();
53 OptionParser parser(option);
54 map<string, string> parameters = parser.getParameters();
56 ValidParameters validParameter;
57 //check to make sure all parameters are valid for command
58 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
59 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
62 output = validParameter.validFile(parameters, "output", false);
63 if (output == "not found") { output = ""; m->mothurOut("You must provide an output filename."); m->mothurOutEndLine(); abort=true; }
68 m->errorOut(e, "GetCommandInfoCommand", "GetCommandInfoCommand");
72 //**********************************************************************************************************************
74 int GetCommandInfoCommand::execute(){
77 if (abort == true) { if (calledHelp) { return 0; } return 2; }
79 commandFactory = CommandFactory::getInstance();
82 m->openOutputFile(output+".temp", out);
86 out << "mothurLocation=" << m->getFullPathName(m->argv) << endl;
87 out << "mothurVersion=" << m->getVersion() << endl;
89 map<string, string> commands = commandFactory->getListCommands();
90 map<string, string>::iterator it;
92 //loop through each command outputting info
93 for (it = commands.begin(); it != commands.end(); it++) {
95 if (m->control_pressed) { m->mothurOut("[ERROR]: did not complete making the file."); m->mothurOutEndLine(); out.close(); remove((output+".temp").c_str()); }
97 Command* thisCommand = commandFactory->getCommand(it->first);
99 //don't add hidden commands
100 if (thisCommand->getCommandCategory() != "Hidden") {
104 out << "commandName=" << thisCommand->getCommandName() << endl;
105 out << "commandCategory=" << thisCommand->getCommandCategory() << endl;
107 //remove /n from help string since gui reads line by line
108 string myhelpString = thisCommand->getHelpString();
109 string newHelpString = "";
110 for (int i = 0; i < myhelpString.length(); i++) {
111 if (myhelpString[i] != '\n') { newHelpString += myhelpString[i]; }
113 out << "help=" << newHelpString << endl;
115 //outputTypes - makes something like outputTypes=fasta-name-qfile
116 map<string, vector<string> > thisOutputTypes = thisCommand->getOutputFiles();
117 map<string, vector<string> >::iterator itTypes;
119 if (thisOutputTypes.size() == 0) { out << "outputTypes=none" << endl; }
122 for (itTypes = thisOutputTypes.begin(); itTypes != thisOutputTypes.end(); itTypes++) { types += itTypes->first + "-"; }
124 types = types.substr(0, types.length()-1);
125 out << "outputTypes=" << types << endl;
128 vector<string> booleans; vector<string> numbers; vector<string> multiples; vector<string> Strings;
129 vector<string> inputGroupNames; map<string, string> inputTypes;
131 getInfo(thisCommand->getParameters(), booleans, numbers, multiples, Strings, inputGroupNames, inputTypes);
134 out << "Boolean=" << booleans.size() << endl;
135 for (int i = 0; i < booleans.size(); i++) { out << booleans[i] << endl; }
138 out << "Multiple=" << multiples.size() << endl;
139 for (int i = 0; i < multiples.size(); i++) { out << multiples[i] << endl; }
142 out << "Numbers=" << numbers.size() << endl;
143 for (int i = 0; i < numbers.size(); i++) { out << numbers[i] << endl; }
146 out << "String=" << Strings.size() << endl;
147 for (int i = 0; i < Strings.size(); i++) { out << Strings[i] << endl; }
150 out << "inputGroupNames=" << inputGroupNames.size() << endl;
151 for (int i = 0; i < inputGroupNames.size(); i++) { out << inputGroupNames[i] << endl; }
154 if (inputTypes.size() == 0) { out << "inputTypes=" << endl; }
157 for (map<string, string>::iterator it2 = inputTypes.begin(); it2 != inputTypes.end(); it2++) { types += it2->first + "-"; }
159 types = types.substr(0, types.length()-1);
160 out << "inputTypes=" << types << endl;
162 for (map<string, string>::iterator it2 = inputTypes.begin(); it2 != inputTypes.end(); it2++) {
163 out << it2->first << "=" << it2->second << endl;
173 m->openOutputFile(output, out2);
174 out2 << numNonHidden << endl;
177 m->appendFiles(output+".temp", output);
178 remove((output+".temp").c_str());
180 m->mothurOutEndLine();
181 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
182 m->mothurOut(output); m->mothurOutEndLine();
183 m->mothurOutEndLine();
188 catch(exception& e) {
189 m->errorOut(e, "GetCommandInfoCommand", "execute");
193 //**********************************************************************************************************************
195 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){
198 map<string, set<string> > groups;
199 map<string, set<string> >::iterator itGroups;
201 for (int i = 0; i < para.size(); i++) {
202 if ((para[i].name == "inputdir") || (para[i].name == "outputdir")) {} //ignore
204 if (para[i].type == "Boolean") {
205 string temp = para[i].name + "=" + para[i].optionsDefault;
206 booleans.push_back(temp);
207 }else if (para[i].type == "Multiple") {
208 string multAllowed = "F";
209 if (para[i].multipleSelectionAllowed) { multAllowed = "T"; }
210 string temp = para[i].name + "=" + para[i].options + "|" + para[i].optionsDefault + "|" + multAllowed;
211 multiples.push_back(temp);
212 }else if (para[i].type == "Number") {
213 string temp = para[i].name + "=" + para[i].optionsDefault;
214 numbers.push_back(temp);
215 }else if (para[i].type == "String") {
216 string temp = para[i].name + "=" + para[i].optionsDefault;
217 strings.push_back(temp);
218 }else if (para[i].type == "InputTypes") {
219 string required = "F";
220 if (para[i].required) { required = "T"; }
221 string temp = required + "|" + para[i].chooseOnlyOneGroup + "|" + para[i].chooseAtLeastOneGroup + "|" + para[i].linkedGroup;
222 inputTypes[para[i].name] = temp;
224 //add choose only one groups
225 groups[para[i].chooseOnlyOneGroup].insert(para[i].name);
227 //add at least one group names
228 groups[para[i].chooseAtLeastOneGroup].insert(para[i].name);
230 //add at linked group names
231 groups[para[i].linkedGroup].insert(para[i].name);
233 }else { m->mothurOut("[ERROR]: " + para[i].type + " is an unknown parameter type, please correct."); m->mothurOutEndLine(); }
237 for (itGroups = groups.begin(); itGroups != groups.end(); itGroups++) {
238 if (itGroups->first != "none") {
239 set<string> tempNames = itGroups->second;
240 string temp = itGroups->first + "=";
241 for (set<string>::iterator itNames = tempNames.begin(); itNames != tempNames.end(); itNames++) {
242 temp += *itNames + "-";
245 temp = temp.substr(0, temp.length()-1);
246 inputGroupNames.push_back(temp);
253 catch(exception& e) {
254 m->errorOut(e, "GetCommandInfoCommand", "getInfo");
258 //**********************************************************************************************************************/