]> git.donarmstrong.com Git - mothur.git/blob - getcommandinfocommand.cpp
working on citations
[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(); remove((output+".temp").c_str()); }
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                                 //outputTypes - makes something like outputTypes=fasta-name-qfile
117                                 map<string, vector<string> > thisOutputTypes = thisCommand->getOutputFiles();
118                                 map<string, vector<string> >::iterator itTypes;
119                                 
120                                 if (thisOutputTypes.size() == 0) { out << "outputTypes=none" << endl; }
121                                 else {
122                                         string types = "";
123                                         for (itTypes = thisOutputTypes.begin(); itTypes != thisOutputTypes.end(); itTypes++) {  types += itTypes->first + "-";  }
124                                         //rip off last -
125                                         types = types.substr(0, types.length()-1);
126                                         out << "outputTypes=" << types << endl;
127                                 }
128                                 
129                                 vector<string> booleans; vector<string> numbers; vector<string> multiples; vector<string> Strings;
130                                 vector<string> inputGroupNames; map<string, string> inputTypes;
131                                 
132                                 getInfo(thisCommand->getParameters(), booleans, numbers, multiples, Strings, inputGroupNames, inputTypes);
133                                 
134                                 //output booleans
135                                 out << "Boolean=" << booleans.size() << endl;
136                                 for (int i = 0; i < booleans.size(); i++) { out << booleans[i] << endl; }
137                                 
138                                 //output mulitples
139                                 out << "Multiple=" << multiples.size() << endl;
140                                 for (int i = 0; i < multiples.size(); i++) { out << multiples[i] << endl; }
141                                 
142                                 //output numbers
143                                 out << "Numbers=" << numbers.size() << endl;
144                                 for (int i = 0; i < numbers.size(); i++) { out << numbers[i] << endl; }
145                                 
146                                 //output strings
147                                 out << "String=" << Strings.size() << endl;
148                                 for (int i = 0; i < Strings.size(); i++) { out << Strings[i] << endl; }
149                                 
150                                 //output groups
151                                 out << "inputGroupNames=" << inputGroupNames.size() << endl;
152                                 for (int i = 0; i < inputGroupNames.size(); i++) { out << inputGroupNames[i] << endl; }
153                                 
154                                 //output input types
155                                 if (inputTypes.size() == 0) { out << "inputTypes=" << endl; }
156                                 else {
157                                         string types = "";
158                                         for (map<string, string>::iterator it2 = inputTypes.begin(); it2 != inputTypes.end(); it2++) {  types += it2->first + "-";      }
159                                         //rip off last -
160                                         types = types.substr(0, types.length()-1);
161                                         out << "inputTypes=" << types << endl;
162                                         
163                                         for (map<string, string>::iterator it2 = inputTypes.begin(); it2 != inputTypes.end(); it2++) {  
164                                                 out << it2->first << "=" << it2->second << endl;
165                                         }
166                                 }
167                                 
168                         }
169                 }
170                 
171                 out.close();
172                 
173                 ofstream out2;
174                 m->openOutputFile(output, out2);
175                 out2 << numNonHidden << endl; 
176                 out2.close();
177                 
178                 m->appendFiles(output+".temp", output);
179                 remove((output+".temp").c_str());
180         
181                 m->mothurOutEndLine();
182                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
183                 m->mothurOut(output); m->mothurOutEndLine();    
184                 m->mothurOutEndLine();
185                 
186                 
187                 return 0;
188         }
189         catch(exception& e) {
190                 m->errorOut(e, "GetCommandInfoCommand", "execute");
191                 exit(1);
192         }
193 }
194 //**********************************************************************************************************************
195
196 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         try {
198                 
199                 map<string, set<string> > groups;
200                 map<string,  set<string> >::iterator itGroups;
201                 
202                 for (int i = 0; i < para.size(); i++) {
203                         if ((para[i].name == "inputdir") || (para[i].name == "outputdir")) {} //ignore
204                         else {
205                                 if (para[i].type == "Boolean") {
206                                         string temp = para[i].name + "=" + para[i].optionsDefault;
207                                         booleans.push_back(temp);
208                                 }else if (para[i].type == "Multiple") {
209                                         string multAllowed = "F";
210                                         if (para[i].multipleSelectionAllowed) { multAllowed = "T"; }
211                                         string temp = para[i].name + "=" + para[i].options + "|" + para[i].optionsDefault + "|" + multAllowed;
212                                         multiples.push_back(temp);
213                                 }else if (para[i].type == "Number") {
214                                         string temp = para[i].name + "=" + para[i].optionsDefault;
215                                         numbers.push_back(temp);
216                                 }else if (para[i].type == "String") {
217                                         string temp = para[i].name + "=" + para[i].optionsDefault;
218                                         strings.push_back(temp);
219                                 }else if (para[i].type == "InputTypes") {
220                                         string required = "F";
221                                         if (para[i].required) { required = "T"; }
222                                         string temp = required + "|" + para[i].chooseOnlyOneGroup + "|" + para[i].chooseAtLeastOneGroup + "|" + para[i].linkedGroup;
223                                         inputTypes[para[i].name] = temp;
224                                         
225                                         //add choose only one groups
226                                         groups[para[i].chooseOnlyOneGroup].insert(para[i].name);
227                                         
228                                         //add at least one group names
229                                         groups[para[i].chooseAtLeastOneGroup].insert(para[i].name);
230                                         
231                                         //add at linked group names
232                                         groups[para[i].linkedGroup].insert(para[i].name);
233                                                   
234                                 }else { m->mothurOut("[ERROR]: " + para[i].type + " is an unknown parameter type, please correct."); m->mothurOutEndLine(); }
235                         }
236                 }
237                 
238                 for (itGroups = groups.begin(); itGroups != groups.end(); itGroups++) {
239                         if (itGroups->first != "none") {
240                                 set<string> tempNames = itGroups->second;
241                                 string temp = itGroups->first + "=";
242                                 for (set<string>::iterator itNames = tempNames.begin(); itNames != tempNames.end(); itNames++) {
243                                         temp += *itNames + "-";
244                                 }
245                                 //rip off last -
246                                 temp = temp.substr(0, temp.length()-1);
247                                 inputGroupNames.push_back(temp);
248                         }
249                 }
250                 
251                 return 0;
252                 
253         }
254         catch(exception& e) {
255                 m->errorOut(e, "GetCommandInfoCommand", "getInfo");
256                 exit(1);
257         }
258 }
259 //**********************************************************************************************************************/