]> git.donarmstrong.com Git - mothur.git/blob - getcommandinfocommand.cpp
added getCommandInfoCommand for gui
[mothur.git] / getcommandinfocommand.cpp
1 /*
2  *  getcommandinfo.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 4/6/11.
6  *  Copyright 2011 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "getcommandinfocommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> GetCommandInfoCommand::setParameters(){  
14         try {
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);
18                 
19                 vector<string> myArray;
20                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
21                 return myArray;
22         }
23         catch(exception& e) {
24                 m->errorOut(e, "GetCommandInfoCommand", "setParameters");
25                 exit(1);
26         }
27 }
28 //**********************************************************************************************************************
29 string GetCommandInfoCommand::getHelpString(){  
30         try {
31                 string helpString = "";
32                 helpString += "This command is used by the gui to get the information about current commands available in mothur.\n";
33                 return helpString;
34         }
35         catch(exception& e) {
36                 m->errorOut(e, "GetCommandInfoCommand", "getHelpString");
37                 exit(1);
38         }
39 }
40 //**********************************************************************************************************************
41
42 GetCommandInfoCommand::GetCommandInfoCommand(string option)  {
43         try {
44                 abort = false; calledHelp = false;   
45                 
46                 //allow user to run help
47                 if(option == "help") { help(); abort = true; calledHelp = true; }
48                 
49                 else {
50                         vector<string> myArray = setParameters();
51                         
52                         OptionParser parser(option);
53                         map<string, string> parameters = parser.getParameters();
54                         
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;  }
59                         }
60                         
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; } 
63                         
64                 }
65         }
66         catch(exception& e) {
67                 m->errorOut(e, "GetCommandInfoCommand", "GetCommandInfoCommand");
68                 exit(1);
69         }
70 }
71 //**********************************************************************************************************************
72
73 int GetCommandInfoCommand::execute(){
74         try {
75                 
76                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
77                 
78                 commandFactory = CommandFactory::getInstance();
79                 
80                 ofstream out;
81                 m->openOutputFile(output+".temp", out);
82                 
83                 int numNonHidden = 0;
84                 
85                 out << "mothurLocation=" << m->getFullPathName(m->argv) << endl;
86                 out << "mothurVersion=" << m->getVersion() << endl;
87                 
88                 map<string, string> commands = commandFactory->getListCommands();
89                 map<string, string>::iterator it;
90                 
91                 //loop through each command outputting info
92                 for (it = commands.begin(); it != commands.end(); it++) {
93                         
94                         if (m->control_pressed) { m->mothurOut("[ERROR]: did not complete making the file."); m->mothurOutEndLine(); out.close(); remove((output+".temp").c_str()); }
95                         
96                         Command* thisCommand = commandFactory->getCommand(it->first);
97                         
98                         //don't add hidden commands
99                         if (thisCommand->getCommandCategory() != "Hidden") {
100                                 numNonHidden++;
101                                 
102                                 //general info
103                                 out << "commandName=" << thisCommand->getCommandName() << endl;
104                                 out << "commandCategory=" << thisCommand->getCommandCategory() << endl;
105                                 
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]; }
111                                 }
112                                 out << "help=" << newHelpString << endl;
113                                 
114                                 //outputTypes - makes something like outputTypes=fasta-name-qfile
115                                 map<string, vector<string> > thisOutputTypes = thisCommand->getOutputFiles();
116                                 map<string, vector<string> >::iterator itTypes;
117                                 
118                                 if (thisOutputTypes.size() == 0) { out << "outputTypes=none" << endl; }
119                                 else {
120                                         string types = "";
121                                         for (itTypes = thisOutputTypes.begin(); itTypes != thisOutputTypes.end(); itTypes++) {  types += itTypes->first + "-";  }
122                                         //rip off last -
123                                         types = types.substr(0, types.length()-1);
124                                         out << "outputTypes=" << types << endl;
125                                 }
126                                 
127                                 vector<string> booleans; vector<string> numbers; vector<string> multiples; vector<string> Strings;
128                                 vector<string> inputGroupNames; map<string, string> inputTypes;
129                                 
130                                 getInfo(thisCommand->getParameters(), booleans, numbers, multiples, Strings, inputGroupNames, inputTypes);
131                                 
132                                 //output booleans
133                                 out << "Boolean=" << booleans.size() << endl;
134                                 for (int i = 0; i < booleans.size(); i++) { out << booleans[i] << endl; }
135                                 
136                                 //output mulitples
137                                 out << "Multiple=" << multiples.size() << endl;
138                                 for (int i = 0; i < multiples.size(); i++) { out << multiples[i] << endl; }
139                                 
140                                 //output numbers
141                                 out << "Numbers=" << numbers.size() << endl;
142                                 for (int i = 0; i < numbers.size(); i++) { out << numbers[i] << endl; }
143                                 
144                                 //output strings
145                                 out << "String=" << Strings.size() << endl;
146                                 for (int i = 0; i < Strings.size(); i++) { out << Strings[i] << endl; }
147                                 
148                                 //output groups
149                                 out << "inputGroupNames=" << inputGroupNames.size() << endl;
150                                 for (int i = 0; i < inputGroupNames.size(); i++) { out << inputGroupNames[i] << endl; }
151                                 
152                                 //output input types
153                                 if (inputTypes.size() == 0) { out << "inputTypes=" << endl; }
154                                 else {
155                                         string types = "";
156                                         for (map<string, string>::iterator it2 = inputTypes.begin(); it2 != inputTypes.end(); it2++) {  types += it2->first + "-";      }
157                                         //rip off last -
158                                         types = types.substr(0, types.length()-1);
159                                         out << "inputTypes=" << types << endl;
160                                         
161                                         for (map<string, string>::iterator it2 = inputTypes.begin(); it2 != inputTypes.end(); it2++) {  
162                                                 out << it2->first << "=" << it2->second << endl;
163                                         }
164                                 }
165                                 
166                         }
167                 }
168                 
169                 out.close();
170                 
171                 ofstream out2;
172                 m->openOutputFile(output, out2);
173                 out2 << numNonHidden << endl; 
174                 out2.close();
175                 
176                 m->appendFiles(output+".temp", output);
177                 remove((output+".temp").c_str());
178         
179                 m->mothurOutEndLine();
180                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
181                 m->mothurOut(output); m->mothurOutEndLine();    
182                 m->mothurOutEndLine();
183                 
184                 
185                 return 0;
186         }
187         catch(exception& e) {
188                 m->errorOut(e, "GetCommandInfoCommand", "execute");
189                 exit(1);
190         }
191 }
192 //**********************************************************************************************************************
193
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){
195         try {
196                 
197                 map<string, set<string> > groups;
198                 map<string,  set<string> >::iterator itGroups;
199                 
200                 for (int i = 0; i < para.size(); i++) {
201                         if ((para[i].name == "inputdir") || (para[i].name == "outputdir")) {} //ignore
202                         else {
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;
222                                         
223                                         //add choose only one groups
224                                         groups[para[i].chooseOnlyOneGroup].insert(para[i].name);
225                                         
226                                         //add at least one group names
227                                         groups[para[i].chooseAtLeastOneGroup].insert(para[i].name);
228                                         
229                                         //add at linked group names
230                                         groups[para[i].linkedGroup].insert(para[i].name);
231                                                   
232                                 }else { m->mothurOut("[ERROR]: " + para[i].type + " is an unknown parameter type, please correct."); m->mothurOutEndLine(); }
233                         }
234                 }
235                 
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 + "-";
242                                 }
243                                 //rip off last -
244                                 temp = temp.substr(0, temp.length()-1);
245                                 inputGroupNames.push_back(temp);
246                         }
247                 }
248                 
249                 return 0;
250                 
251         }
252         catch(exception& e) {
253                 m->errorOut(e, "GetCommandInfoCommand", "getInfo");
254                 exit(1);
255         }
256 }
257 //**********************************************************************************************************************/