]> git.donarmstrong.com Git - mothur.git/blob - setcurrentcommand.cpp
added citation function to commands
[mothur.git] / setcurrentcommand.cpp
1 /*
2  *  setcurrentcommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 3/16/11.
6  *  Copyright 2011 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "setcurrentcommand.h"
11
12 //**********************************************************************************************************************
13 vector<string> SetCurrentCommand::setParameters(){      
14         try {
15                 
16                 CommandParameter pprocessors("processors", "Number", "", "1", "", "", "",false,false); parameters.push_back(pprocessors);
17                 CommandParameter pflow("flow", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pflow);
18                 CommandParameter pphylip("phylip", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pphylip);
19                 CommandParameter pcolumn("column", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pcolumn);
20                 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pfasta);
21                 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
22                 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pgroup);
23                 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(plist);
24                 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(ptaxonomy);
25                 CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pqfile);
26                 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(paccnos);            
27                 CommandParameter prabund("rabund", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(prabund);
28                 CommandParameter psabund("sabund", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(psabund);
29                 CommandParameter pdesign("design", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pdesign);
30                 CommandParameter porder("order", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(porder);
31                 CommandParameter ptree("tree", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(ptree);
32                 CommandParameter pshared("shared", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pshared);
33                 CommandParameter pordergroup("ordergroup", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pordergroup);
34                 CommandParameter prelabund("relabund", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(prelabund);
35                 CommandParameter psff("sff", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(psff);
36                 CommandParameter poligos("oligos", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(poligos);
37                 CommandParameter pclear("clear", "String", "", "", "", "", "",false,false); parameters.push_back(pclear);
38                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
39                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
40                 
41                 vector<string> myArray;
42                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
43                 return myArray;
44         }
45         catch(exception& e) {
46                 m->errorOut(e, "SetCurrentCommand", "setParameters");
47                 exit(1);
48         }
49 }
50 //**********************************************************************************************************************
51 string SetCurrentCommand::getHelpString(){      
52         try {
53                 string helpString = "";
54                 helpString += "The set.current command allows you to set the current files saved by mothur.\n";
55                 helpString += "The set.current command parameters are: clear, phylip, column, list, rabund, sabund, name, group, design, order, tree, shared, ordergroup, relabund, fasta, qfile, sff, oligos, accnos, taxonomy.\n";
56                 helpString += "The clear paramter is used to indicate which file types you would like to clear values for, multiple types can be separated by dashes.\n";
57                 helpString += "The set.current command should be in the following format: \n";
58                 helpString += "set.current(fasta=yourFastaFile) or set.current(fasta=amazon.fasta, clear=name-accnos)\n";
59                 return helpString;
60         }
61         catch(exception& e) {
62                 m->errorOut(e, "SetCurrentCommand", "getHelpString");
63                 exit(1);
64         }
65 }
66
67
68 //**********************************************************************************************************************
69 SetCurrentCommand::SetCurrentCommand(){ 
70         try {
71                 abort = true; calledHelp = true;
72                 setParameters();
73         }
74         catch(exception& e) {
75                 m->errorOut(e, "SetCurrentCommand", "SetCurrentCommand");
76                 exit(1);
77         }
78 }
79 //**********************************************************************************************************************
80 SetCurrentCommand::SetCurrentCommand(string option)  {
81         try {
82                 abort = false; calledHelp = false;   
83                 
84                 //allow user to run help
85                 if(option == "help") { help(); abort = true; calledHelp = true; }
86                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
87                 
88                 else {
89                         //valid paramters for this command
90                         vector<string> myArray = setParameters();
91                         
92                         OptionParser parser(option);
93                         map<string,string> parameters = parser.getParameters();
94                         
95                         ValidParameters validParameter;
96                         map<string,string>::iterator it;
97                         //check to make sure all parameters are valid for command
98                         for (it = parameters.begin(); it != parameters.end(); it++) { 
99                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
100                         }
101                         
102                         
103                         //if the user changes the input directory command factory will send this info to us in the output parameter 
104                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
105                         if (inputDir == "not found"){   inputDir = "";          }
106                         else {
107                                 string path;
108                                 it = parameters.find("phylip");
109                                 //user has given a template file
110                                 if(it != parameters.end()){ 
111                                         path = m->hasPath(it->second);
112                                         //if the user has not given a path then, add inputdir. else leave path alone.
113                                         if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
114                                 }
115                                 
116                                 it = parameters.find("column");
117                                 //user has given a template file
118                                 if(it != parameters.end()){ 
119                                         path = m->hasPath(it->second);
120                                         //if the user has not given a path then, add inputdir. else leave path alone.
121                                         if (path == "") {       parameters["column"] = inputDir + it->second;           }
122                                 }
123                                 
124                                 it = parameters.find("fasta");
125                                 //user has given a template file
126                                 if(it != parameters.end()){ 
127                                         path = m->hasPath(it->second);
128                                         //if the user has not given a path then, add inputdir. else leave path alone.
129                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
130                                 }
131                                 
132                                 it = parameters.find("list");
133                                 //user has given a template file
134                                 if(it != parameters.end()){ 
135                                         path = m->hasPath(it->second);
136                                         //if the user has not given a path then, add inputdir. else leave path alone.
137                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
138                                 }
139                                 
140                                 it = parameters.find("rabund");
141                                 //user has given a template file
142                                 if(it != parameters.end()){ 
143                                         path = m->hasPath(it->second);
144                                         //if the user has not given a path then, add inputdir. else leave path alone.
145                                         if (path == "") {       parameters["rabund"] = inputDir + it->second;           }
146                                 }
147                                 
148                                 it = parameters.find("sabund");
149                                 //user has given a template file
150                                 if(it != parameters.end()){ 
151                                         path = m->hasPath(it->second);
152                                         //if the user has not given a path then, add inputdir. else leave path alone.
153                                         if (path == "") {       parameters["sabund"] = inputDir + it->second;           }
154                                 }
155                                 
156                                 it = parameters.find("name");
157                                 //user has given a template file
158                                 if(it != parameters.end()){ 
159                                         path = m->hasPath(it->second);
160                                         //if the user has not given a path then, add inputdir. else leave path alone.
161                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
162                                 }
163                                 
164                                 it = parameters.find("group");
165                                 //user has given a template file
166                                 if(it != parameters.end()){ 
167                                         path = m->hasPath(it->second);
168                                         //if the user has not given a path then, add inputdir. else leave path alone.
169                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
170                                 }
171                                 
172                                 it = parameters.find("design");
173                                 //user has given a template file
174                                 if(it != parameters.end()){ 
175                                         path = m->hasPath(it->second);
176                                         //if the user has not given a path then, add inputdir. else leave path alone.
177                                         if (path == "") {       parameters["design"] = inputDir + it->second;           }
178                                 }
179                                 
180                                 it = parameters.find("order");
181                                 //user has given a template file
182                                 if(it != parameters.end()){ 
183                                         path = m->hasPath(it->second);
184                                         //if the user has not given a path then, add inputdir. else leave path alone.
185                                         if (path == "") {       parameters["order"] = inputDir + it->second;            }
186                                 }
187                                 
188                                 it = parameters.find("tree");
189                                 //user has given a template file
190                                 if(it != parameters.end()){ 
191                                         path = m->hasPath(it->second);
192                                         //if the user has not given a path then, add inputdir. else leave path alone.
193                                         if (path == "") {       parameters["tree"] = inputDir + it->second;             }
194                                 }
195                                 
196                                 it = parameters.find("shared");
197                                 //user has given a template file
198                                 if(it != parameters.end()){ 
199                                         path = m->hasPath(it->second);
200                                         //if the user has not given a path then, add inputdir. else leave path alone.
201                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
202                                 }
203                                 
204                                 it = parameters.find("ordergroup");
205                                 //user has given a template file
206                                 if(it != parameters.end()){ 
207                                         path = m->hasPath(it->second);
208                                         //if the user has not given a path then, add inputdir. else leave path alone.
209                                         if (path == "") {       parameters["ordergroup"] = inputDir + it->second;               }
210                                 }
211                                 
212                                 it = parameters.find("relabund");
213                                 //user has given a template file
214                                 if(it != parameters.end()){ 
215                                         path = m->hasPath(it->second);
216                                         //if the user has not given a path then, add inputdir. else leave path alone.
217                                         if (path == "") {       parameters["relabund"] = inputDir + it->second;         }
218                                 }
219                                 
220                                 it = parameters.find("fasta");
221                                 //user has given a template file
222                                 if(it != parameters.end()){ 
223                                         path = m->hasPath(it->second);
224                                         //if the user has not given a path then, add inputdir. else leave path alone.
225                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
226                                 }
227                                 
228                                 it = parameters.find("qfile");
229                                 //user has given a template file
230                                 if(it != parameters.end()){ 
231                                         path = m->hasPath(it->second);
232                                         //if the user has not given a path then, add inputdir. else leave path alone.
233                                         if (path == "") {       parameters["qfile"] = inputDir + it->second;            }
234                                 }
235                                 
236                                 it = parameters.find("sff");
237                                 //user has given a template file
238                                 if(it != parameters.end()){ 
239                                         path = m->hasPath(it->second);
240                                         //if the user has not given a path then, add inputdir. else leave path alone.
241                                         if (path == "") {       parameters["sff"] = inputDir + it->second;              }
242                                 }
243                                 
244                                 it = parameters.find("oligos");
245                                 //user has given a template file
246                                 if(it != parameters.end()){ 
247                                         path = m->hasPath(it->second);
248                                         //if the user has not given a path then, add inputdir. else leave path alone.
249                                         if (path == "") {       parameters["oligos"] = inputDir + it->second;           }
250                                 }
251                                 
252                                 it = parameters.find("accnos");
253                                 //user has given a template file
254                                 if(it != parameters.end()){ 
255                                         path = m->hasPath(it->second);
256                                         //if the user has not given a path then, add inputdir. else leave path alone.
257                                         if (path == "") {       parameters["accnos"] = inputDir + it->second;           }
258                                 }
259
260                                 it = parameters.find("taxonomy");
261                                 //user has given a template file
262                                 if(it != parameters.end()){ 
263                                         path = m->hasPath(it->second);
264                                         //if the user has not given a path then, add inputdir. else leave path alone.
265                                         if (path == "") {       parameters["taxonomy"] = inputDir + it->second;         }
266                                 }
267                                 
268                                 it = parameters.find("flow");
269                                 //user has given a template file
270                                 if(it != parameters.end()){ 
271                                         path = m->hasPath(it->second);
272                                         //if the user has not given a path then, add inputdir. else leave path alone.
273                                         if (path == "") {       parameters["flow"] = inputDir + it->second;             }
274                                 }
275                         }
276                         
277                         //check for parameters
278                         phylipfile = validParameter.validFile(parameters, "phylip", true);
279                         if (phylipfile == "not open") { m->mothurOut("Ignoring: " + parameters["phylip"]); m->mothurOutEndLine(); phylipfile = ""; }
280                         else if (phylipfile == "not found") {  phylipfile = "";  }      
281                         if (phylipfile != "") { m->setPhylipFile(phylipfile); }
282                         
283                         columnfile = validParameter.validFile(parameters, "column", true);
284                         if (columnfile == "not open") { m->mothurOut("Ignoring: " + parameters["column"]); m->mothurOutEndLine(); columnfile = ""; }
285                         else if (columnfile == "not found") {  columnfile = "";  }      
286                         if (columnfile != "") { m->setColumnFile(columnfile); }
287                         
288                         listfile = validParameter.validFile(parameters, "list", true);
289                         if (listfile == "not open") { m->mothurOut("Ignoring: " + parameters["list"]); m->mothurOutEndLine(); listfile = ""; }
290                         else if (listfile == "not found") {  listfile = "";  }  
291                         if (listfile != "") { m->setListFile(listfile); }
292                         
293                         rabundfile = validParameter.validFile(parameters, "rabund", true);
294                         if (rabundfile == "not open") { m->mothurOut("Ignoring: " + parameters["rabund"]); m->mothurOutEndLine(); rabundfile = ""; }
295                         else if (rabundfile == "not found") {  rabundfile = "";  }      
296                         if (rabundfile != "") { m->setRabundFile(rabundfile); }
297                         
298                         sabundfile = validParameter.validFile(parameters, "sabund", true);
299                         if (sabundfile == "not open") { m->mothurOut("Ignoring: " + parameters["sabund"]); m->mothurOutEndLine(); sabundfile = ""; }
300                         else if (sabundfile == "not found") {  sabundfile = "";  }      
301                         if (sabundfile != "") { m->setSabundFile(sabundfile); }
302                         
303                         namefile = validParameter.validFile(parameters, "name", true);
304                         if (namefile == "not open") { m->mothurOut("Ignoring: " + parameters["name"]); m->mothurOutEndLine(); namefile = ""; }
305                         else if (namefile == "not found") {  namefile = "";  }  
306                         if (namefile != "") { m->setNameFile(namefile); }
307                         
308                         groupfile = validParameter.validFile(parameters, "group", true);
309                         if (groupfile == "not open") { m->mothurOut("Ignoring: " + parameters["group"]); m->mothurOutEndLine(); groupfile = ""; }
310                         else if (groupfile == "not found") {  groupfile = "";  }
311                         if (groupfile != "") { m->setGroupFile(groupfile); }
312                         
313                         designfile = validParameter.validFile(parameters, "design", true);
314                         if (designfile == "not open") { m->mothurOut("Ignoring: " + parameters["design"]); m->mothurOutEndLine(); designfile = ""; }
315                         else if (designfile == "not found") {  designfile = "";  }      
316                         if (designfile != "") { m->setDesignFile(designfile); }
317                         
318                         orderfile = validParameter.validFile(parameters, "order", true);
319                         if (orderfile == "not open") { m->mothurOut("Ignoring: " + parameters["order"]); m->mothurOutEndLine(); orderfile = ""; }
320                         else if (orderfile == "not found") {  orderfile = "";  }
321                         if (orderfile != "") { m->setOrderFile(orderfile); }
322                         
323                         treefile = validParameter.validFile(parameters, "tree", true);
324                         if (treefile == "not open") { m->mothurOut("Ignoring: " + parameters["tree"]); m->mothurOutEndLine(); treefile = ""; }
325                         else if (treefile == "not found") {  treefile = "";  }  
326                         if (treefile != "") { m->setTreeFile(treefile); }
327                         
328                         sharedfile = validParameter.validFile(parameters, "shared", true);
329                         if (sharedfile == "not open") { m->mothurOut("Ignoring: " + parameters["shared"]); m->mothurOutEndLine(); sharedfile = ""; }
330                         else if (sharedfile == "not found") {  sharedfile = "";  }      
331                         if (sharedfile != "") { m->setSharedFile(sharedfile); }
332                         
333                         ordergroupfile = validParameter.validFile(parameters, "ordergroup", true);
334                         if (ordergroupfile == "not open") { m->mothurOut("Ignoring: " + parameters["ordergroup"]); m->mothurOutEndLine(); ordergroupfile = ""; }
335                         else if (ordergroupfile == "not found") {  ordergroupfile = "";  }      
336                         if (ordergroupfile != "") { m->setOrderGroupFile(ordergroupfile); }
337                         
338                         relabundfile = validParameter.validFile(parameters, "relabund", true);
339                         if (relabundfile == "not open") { m->mothurOut("Ignoring: " + parameters["relabund"]); m->mothurOutEndLine(); relabundfile = ""; }
340                         else if (relabundfile == "not found") {  relabundfile = "";  }  
341                         if (relabundfile != "") { m->setRelAbundFile(relabundfile); }
342                         
343                         fastafile = validParameter.validFile(parameters, "fasta", true);
344                         if (fastafile == "not open") { m->mothurOut("Ignoring: " + parameters["fasta"]); m->mothurOutEndLine(); fastafile = ""; }
345                         else if (fastafile == "not found") {  fastafile = "";  }
346                         if (fastafile != "") { m->setFastaFile(fastafile); }
347                         
348                         qualfile = validParameter.validFile(parameters, "qfile", true);
349                         if (qualfile == "not open") { m->mothurOut("Ignoring: " + parameters["qfile"]); m->mothurOutEndLine(); qualfile = ""; }
350                         else if (qualfile == "not found") {  qualfile = "";  }  
351                         if (qualfile != "") { m->setQualFile(qualfile); }
352                         
353                         sfffile = validParameter.validFile(parameters, "sff", true);
354                         if (sfffile == "not open") { m->mothurOut("Ignoring: " + parameters["sff"]); m->mothurOutEndLine(); sfffile = ""; }
355                         else if (sfffile == "not found") {  sfffile = "";  }    
356                         if (sfffile != "") { m->setSFFFile(sfffile); }
357                         
358                         oligosfile = validParameter.validFile(parameters, "oligos", true);
359                         if (oligosfile == "not open") { m->mothurOut("Ignoring: " + parameters["oligos"]); m->mothurOutEndLine(); oligosfile = ""; }
360                         else if (oligosfile == "not found") {  oligosfile = "";  }      
361                         if (oligosfile != "") { m->setOligosFile(oligosfile); }
362                         
363                         accnosfile = validParameter.validFile(parameters, "accnos", true);
364                         if (accnosfile == "not open") { m->mothurOut("Ignoring: " + parameters["accnos"]); m->mothurOutEndLine(); accnosfile = ""; }
365                         else if (accnosfile == "not found") {  accnosfile = "";  }      
366                         if (accnosfile != "") { m->setAccnosFile(accnosfile); }
367                         
368                         taxonomyfile = validParameter.validFile(parameters, "taxonomy", true);
369                         if (taxonomyfile == "not open") { m->mothurOut("Ignoring: " + parameters["taxonomy"]); m->mothurOutEndLine(); taxonomyfile = ""; }
370                         else if (taxonomyfile == "not found") {  taxonomyfile = "";  }  
371                         if (taxonomyfile != "") { m->setTaxonomyFile(taxonomyfile); }
372                         
373                         flowfile = validParameter.validFile(parameters, "flow", true);
374                         if (flowfile == "not open") { m->mothurOut("Ignoring: " + parameters["flow"]); m->mothurOutEndLine(); flowfile = ""; }
375                         else if (flowfile == "not found") {  flowfile = "";  }  
376                         if (flowfile != "") { m->setFlowFile(flowfile); }
377                         
378                         processors = validParameter.validFile(parameters, "processors", false);
379                         if (processors == "not found") {  processors = "1";  }  
380                         if (processors != "") { m->setProcessors(processors); }
381                         
382                         clearTypes = validParameter.validFile(parameters, "clear", false);                      
383                         if (clearTypes == "not found") { clearTypes = ""; }
384                         else { m->splitAtDash(clearTypes, types);       }
385                 }
386                 
387         }
388         catch(exception& e) {
389                 m->errorOut(e, "SetCurrentCommand", "SetCurrentCommand");
390                 exit(1);
391         }
392 }
393 //**********************************************************************************************************************
394
395 int SetCurrentCommand::execute(){
396         try {
397                 
398                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
399                 
400                 //user wants to clear a type
401                 if (types.size() != 0) {
402                         for (int i = 0; i < types.size(); i++) {
403                                 
404                                 if (m->control_pressed) { break; }
405                                 
406                                 //look for file types
407                                 if (types[i] == "fasta") {
408                                         m->setFastaFile("");
409                                 }else if (types[i] == "qfile") {
410                                         m->setQualFile("");
411                                 }else if (types[i] == "phylip") {
412                                         m->setPhylipFile("");
413                                 }else if (types[i] == "column") {
414                                         m->setColumnFile("");
415                                 }else if (types[i] == "list") {
416                                         m->setListFile("");
417                                 }else if (types[i] == "rabund") {
418                                         m->setRabundFile("");
419                                 }else if (types[i] == "sabund") {
420                                         m->setSabundFile("");
421                                 }else if (types[i] == "name") {
422                                         m->setNameFile("");
423                                 }else if (types[i] == "group") {
424                                         m->setGroupFile("");
425                                 }else if (types[i] == "order") {
426                                         m->setOrderFile("");
427                                 }else if (types[i] == "ordergroup") {
428                                         m->setOrderGroupFile("");
429                                 }else if (types[i] == "tree") {
430                                         m->setTreeFile("");
431                                 }else if (types[i] == "shared") {
432                                         m->setSharedFile("");
433                                 }else if (types[i] == "relabund") {
434                                         m->setRelAbundFile("");
435                                 }else if (types[i] == "design") {
436                                         m->setDesignFile("");
437                                 }else if (types[i] == "sff") {
438                                         m->setSFFFile("");
439                                 }else if (types[i] == "oligos") {
440                                         m->setOligosFile("");
441                                 }else if (types[i] == "accnos") {
442                                         m->setAccnosFile("");
443                                 }else if (types[i] == "taxonomy") {
444                                         m->setTaxonomyFile("");
445                                 }else if (types[i] == "flow") {
446                                         m->setFlowFile("");
447                                 }else if (types[i] == "processors") {
448                                         m->setProcessors("1");
449                                 }else if (types[i] == "all") {
450                                         m->clearCurrentFiles();
451                                 }else {
452                                         m->mothurOut("[ERROR]: mothur does not save a current file for " + types[i]); m->mothurOutEndLine();
453                                 }
454                         }
455                 }
456                 
457                 m->mothurOutEndLine(); m->mothurOut("Current files saved by mothur:"); m->mothurOutEndLine();
458                 m->printCurrentFiles();
459                 
460                 return 0;       
461         }
462         
463         catch(exception& e) {
464                 m->errorOut(e, "SetCurrentCommand", "execute");
465                 exit(1);
466         }
467 }
468
469 //**********************************************************************************************************************
470
471
472