]> git.donarmstrong.com Git - mothur.git/blob - getcommandinfocommand.cpp
changes while testing
[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                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
49                 
50                 else {
51                         vector<string> myArray = setParameters();
52                         
53                         OptionParser parser(option);
54                         map<string, string> parameters = parser.getParameters();
55                         
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;  }
60                         }
61                         
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; } 
64                         
65                 }
66         }
67         catch(exception& e) {
68                 m->errorOut(e, "GetCommandInfoCommand", "GetCommandInfoCommand");
69                 exit(1);
70         }
71 }
72 //**********************************************************************************************************************
73
74 int GetCommandInfoCommand::execute(){
75         try {
76                 
77                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
78                 
79                 commandFactory = CommandFactory::getInstance();
80                 
81                 ofstream out;
82                 m->openOutputFile(output+".temp", out);
83                 
84                 int numNonHidden = 0;
85                 
86                 out << "mothurLocation=" << m->getFullPathName(m->argv) << endl;
87                 out << "mothurVersion=" << m->getVersion() << endl;
88                 
89                 map<string, string> commands = commandFactory->getListCommands();
90                 map<string, string>::iterator it;
91                 
92                 //loop through each command outputting info
93                 for (it = commands.begin(); it != commands.end(); it++) {
94                         
95                         if (m->control_pressed) { m->mothurOut("[ERROR]: did not complete making the file."); m->mothurOutEndLine(); out.close(); m->mothurRemove((output+".temp")); }
96                         
97                         Command* thisCommand = commandFactory->getCommand(it->first);
98                         
99                         //don't add hidden commands
100                         if (thisCommand->getCommandCategory() != "Hidden") {
101                                 numNonHidden++;
102                                 
103                                 //general info
104                                 out << "commandName=" << thisCommand->getCommandName() << endl;
105                                 //cout << thisCommand->getCommandName() << " current citation = " << thisCommand->getCitation() << endl;
106                                 out << "commandCategory=" << thisCommand->getCommandCategory() << endl;
107                                 
108                                 //remove /n from help string since gui reads line by line
109                                 string myhelpString = thisCommand->getHelpString();
110                                 string newHelpString = "";
111                                 for (int i = 0; i < myhelpString.length(); i++) { 
112                                         if (myhelpString[i] != '\n') { newHelpString += myhelpString[i]; }
113                                 }
114                                 out << "help=" << newHelpString << endl;
115                                 
116                                 //remove /n from citation string since gui reads line by line
117                                 string mycitationString = thisCommand->getCitation();
118                                 string newCitationString = "";
119                                 for (int i = 0; i < mycitationString.length(); i++) { 
120                                         if (mycitationString[i] != '\n') { newCitationString += mycitationString[i]; }
121                                 }
122                                 out << "citation=" << newCitationString << endl;
123                                 
124                                 out << "description=" << thisCommand->getDescription() << endl;
125                                 
126                                 //outputTypes - makes something like outputTypes=fasta-name-qfile
127                                 map<string, vector<string> > thisOutputTypes = thisCommand->getOutputFiles();
128                                 map<string, vector<string> >::iterator itTypes;
129                                 
130                                 if (thisOutputTypes.size() == 0) { out << "outputTypesNames=0" << endl; }
131                                 else {
132                                         //string types = "";
133                                         //for (itTypes = thisOutputTypes.begin(); itTypes != thisOutputTypes.end(); itTypes++) {        types += itTypes->first + "-";  }
134                                         //rip off last -
135                                         //types = types.substr(0, types.length()-1);
136                                         out << "outputTypesNames=" << thisOutputTypes.size() << endl;
137                     
138                     for (itTypes = thisOutputTypes.begin(); itTypes != thisOutputTypes.end(); itTypes++) {
139                         out << itTypes->first << "=" << thisCommand->getOutputPattern(itTypes->first) << endl;
140                     }
141                                 }
142                                 
143                                 vector<string> booleans; vector<string> numbers; vector<string> multiples; vector<string> Strings;
144                                 vector<string> inputGroupNames; map<string, string> inputTypes;
145                                 
146                                 getInfo(thisCommand->getParameters(), booleans, numbers, multiples, Strings, inputGroupNames, inputTypes);
147                                 
148                                 //output booleans
149                                 out << "Boolean=" << booleans.size() << endl;
150                                 for (int i = 0; i < booleans.size(); i++) { out << booleans[i] << endl; }
151                                 
152                                 //output mulitples
153                                 out << "Multiple=" << multiples.size() << endl;
154                                 for (int i = 0; i < multiples.size(); i++) { out << multiples[i] << endl; }
155                                 
156                                 //output numbers
157                                 out << "Numbers=" << numbers.size() << endl;
158                                 for (int i = 0; i < numbers.size(); i++) { out << numbers[i] << endl; }
159                                 
160                                 //output strings
161                                 out << "String=" << Strings.size() << endl;
162                                 for (int i = 0; i < Strings.size(); i++) { out << Strings[i] << endl; }
163                                 
164                                 //output groups
165                                 out << "inputGroupNames=" << inputGroupNames.size() << endl;
166                                 for (int i = 0; i < inputGroupNames.size(); i++) { out << inputGroupNames[i] << endl; }
167                                 
168                                 //output input types
169                                 if (inputTypes.size() == 0) { out << "inputTypes=" << endl; }
170                                 else {
171                                         string types = "";
172                                         for (map<string, string>::iterator it2 = inputTypes.begin(); it2 != inputTypes.end(); it2++) {  types += it2->first + "-";      }
173                                         //rip off last -
174                                         types = types.substr(0, types.length()-1);
175                                         out << "inputTypes=" << types << endl;
176                                         
177                                         for (map<string, string>::iterator it2 = inputTypes.begin(); it2 != inputTypes.end(); it2++) {  
178                                                 out << it2->first << "=" << it2->second << endl;
179                                         }
180                                 }
181                                 
182                         }
183                 }
184                 
185                 out.close();
186                 
187                 ofstream out2;
188                 m->openOutputFile(output, out2);
189                 out2 << numNonHidden << endl; 
190                 out2.close();
191                 
192                 m->appendFiles(output+".temp", output);
193                 m->mothurRemove((output+".temp"));
194         
195                 m->mothurOutEndLine();
196                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
197                 m->mothurOut(output); m->mothurOutEndLine();    
198                 m->mothurOutEndLine();
199                 
200                 
201                 return 0;
202         }
203         catch(exception& e) {
204                 m->errorOut(e, "GetCommandInfoCommand", "execute");
205                 exit(1);
206         }
207 }
208 //**********************************************************************************************************************
209
210 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){
211         try {
212                 
213                 map<string, set<string> > groups;
214                 map<string,  set<string> >::iterator itGroups;
215                 
216                 for (int i = 0; i < para.size(); i++) {
217                         if ((para[i].name == "inputdir") || (para[i].name == "outputdir")) {} //ignore
218                         else {
219                 string important = "|F";
220                 if (para[i].important || para[i].required) { important = "|T"; }
221                 
222                 string outputType = "|none";
223                 if (para[i].outputTypes != "") { outputType = "|" + para[i].outputTypes; }
224                 
225                                 if (para[i].type == "Boolean") {
226                                         string temp = para[i].name + "=" + para[i].optionsDefault + important + outputType;
227                                         booleans.push_back(temp);
228                                 }else if (para[i].type == "Multiple") {
229                                         string multAllowed = "F";
230                                         if (para[i].multipleSelectionAllowed) { multAllowed = "T"; }
231                                         string temp = para[i].name + "=" + para[i].options + "|" + para[i].optionsDefault + "|" + multAllowed + important + outputType;
232                                         multiples.push_back(temp);
233                                 }else if (para[i].type == "Number") {
234                                         string temp = para[i].name + "=" + para[i].optionsDefault + important + outputType;
235                                         numbers.push_back(temp);
236                                 }else if (para[i].type == "String") {
237                                         string temp = para[i].name + "=" + para[i].optionsDefault + important + outputType;
238                                         strings.push_back(temp);
239                                 }else if (para[i].type == "InputTypes") {
240                                         string required = "F";
241                                         if (para[i].required) { required = "T"; }
242                                         string temp = required + important + "|" + para[i].chooseOnlyOneGroup + "|" + para[i].chooseAtLeastOneGroup + "|" + para[i].linkedGroup + outputType;
243                                         inputTypes[para[i].name] = temp;
244                                         
245                                         //add choose only one groups
246                     vector<string> tempGroups;
247                     m->splitAtDash(para[i].chooseOnlyOneGroup, tempGroups);
248                     for (int l = 0; l < tempGroups.size(); l++) {
249                         groups[tempGroups[l]].insert(para[i].name);
250                     }
251                                         tempGroups.clear();
252                     
253                                         //add at least one group names
254                     m->splitAtDash(para[i].chooseAtLeastOneGroup, tempGroups);
255                     for (int l = 0; l < tempGroups.size(); l++) {
256                         groups[tempGroups[l]].insert(para[i].name);
257                     }
258                                         tempGroups.clear();
259                                         
260                                         
261                                         //add at linked group names
262                     m->splitAtDash(para[i].linkedGroup, tempGroups);
263                     for (int l = 0; l < tempGroups.size(); l++) {
264                         groups[tempGroups[l]].insert(para[i].name);
265                     }
266                                         tempGroups.clear();
267                                                   
268                                 }else { m->mothurOut("[ERROR]: " + para[i].type + " is an unknown parameter type, please correct."); m->mothurOutEndLine(); }
269                         }
270                 }
271                 
272                 for (itGroups = groups.begin(); itGroups != groups.end(); itGroups++) {
273                         if (itGroups->first != "none") {
274                                 set<string> tempNames = itGroups->second;
275                                 string temp = itGroups->first + "=";
276                                 for (set<string>::iterator itNames = tempNames.begin(); itNames != tempNames.end(); itNames++) {
277                                         temp += *itNames + "-";
278                                 }
279                                 //rip off last -
280                                 temp = temp.substr(0, temp.length()-1);
281                                 inputGroupNames.push_back(temp);
282                         }
283                 }
284                 
285                 return 0;
286                 
287         }
288         catch(exception& e) {
289                 m->errorOut(e, "GetCommandInfoCommand", "getInfo");
290                 exit(1);
291         }
292 }
293 //**********************************************************************************************************************/