]> git.donarmstrong.com Git - mothur.git/blob - setcurrentcommand.cpp
added current as option is lists of file names, processors now outputted with current...
[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                 
87                 else {
88                         //valid paramters for this command
89                         vector<string> myArray = setParameters();
90                         
91                         OptionParser parser(option);
92                         map<string,string> parameters = parser.getParameters();
93                         
94                         ValidParameters validParameter;
95                         map<string,string>::iterator it;
96                         //check to make sure all parameters are valid for command
97                         for (it = parameters.begin(); it != parameters.end(); it++) { 
98                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
99                         }
100                         
101                         
102                         //if the user changes the input directory command factory will send this info to us in the output parameter 
103                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
104                         if (inputDir == "not found"){   inputDir = "";          }
105                         else {
106                                 string path;
107                                 it = parameters.find("phylip");
108                                 //user has given a template file
109                                 if(it != parameters.end()){ 
110                                         path = m->hasPath(it->second);
111                                         //if the user has not given a path then, add inputdir. else leave path alone.
112                                         if (path == "") {       parameters["phylip"] = inputDir + it->second;           }
113                                 }
114                                 
115                                 it = parameters.find("column");
116                                 //user has given a template file
117                                 if(it != parameters.end()){ 
118                                         path = m->hasPath(it->second);
119                                         //if the user has not given a path then, add inputdir. else leave path alone.
120                                         if (path == "") {       parameters["column"] = inputDir + it->second;           }
121                                 }
122                                 
123                                 it = parameters.find("fasta");
124                                 //user has given a template file
125                                 if(it != parameters.end()){ 
126                                         path = m->hasPath(it->second);
127                                         //if the user has not given a path then, add inputdir. else leave path alone.
128                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
129                                 }
130                                 
131                                 it = parameters.find("list");
132                                 //user has given a template file
133                                 if(it != parameters.end()){ 
134                                         path = m->hasPath(it->second);
135                                         //if the user has not given a path then, add inputdir. else leave path alone.
136                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
137                                 }
138                                 
139                                 it = parameters.find("rabund");
140                                 //user has given a template file
141                                 if(it != parameters.end()){ 
142                                         path = m->hasPath(it->second);
143                                         //if the user has not given a path then, add inputdir. else leave path alone.
144                                         if (path == "") {       parameters["rabund"] = inputDir + it->second;           }
145                                 }
146                                 
147                                 it = parameters.find("sabund");
148                                 //user has given a template file
149                                 if(it != parameters.end()){ 
150                                         path = m->hasPath(it->second);
151                                         //if the user has not given a path then, add inputdir. else leave path alone.
152                                         if (path == "") {       parameters["sabund"] = inputDir + it->second;           }
153                                 }
154                                 
155                                 it = parameters.find("name");
156                                 //user has given a template file
157                                 if(it != parameters.end()){ 
158                                         path = m->hasPath(it->second);
159                                         //if the user has not given a path then, add inputdir. else leave path alone.
160                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
161                                 }
162                                 
163                                 it = parameters.find("group");
164                                 //user has given a template file
165                                 if(it != parameters.end()){ 
166                                         path = m->hasPath(it->second);
167                                         //if the user has not given a path then, add inputdir. else leave path alone.
168                                         if (path == "") {       parameters["group"] = inputDir + it->second;            }
169                                 }
170                                 
171                                 it = parameters.find("design");
172                                 //user has given a template file
173                                 if(it != parameters.end()){ 
174                                         path = m->hasPath(it->second);
175                                         //if the user has not given a path then, add inputdir. else leave path alone.
176                                         if (path == "") {       parameters["design"] = inputDir + it->second;           }
177                                 }
178                                 
179                                 it = parameters.find("order");
180                                 //user has given a template file
181                                 if(it != parameters.end()){ 
182                                         path = m->hasPath(it->second);
183                                         //if the user has not given a path then, add inputdir. else leave path alone.
184                                         if (path == "") {       parameters["order"] = inputDir + it->second;            }
185                                 }
186                                 
187                                 it = parameters.find("tree");
188                                 //user has given a template file
189                                 if(it != parameters.end()){ 
190                                         path = m->hasPath(it->second);
191                                         //if the user has not given a path then, add inputdir. else leave path alone.
192                                         if (path == "") {       parameters["tree"] = inputDir + it->second;             }
193                                 }
194                                 
195                                 it = parameters.find("shared");
196                                 //user has given a template file
197                                 if(it != parameters.end()){ 
198                                         path = m->hasPath(it->second);
199                                         //if the user has not given a path then, add inputdir. else leave path alone.
200                                         if (path == "") {       parameters["shared"] = inputDir + it->second;           }
201                                 }
202                                 
203                                 it = parameters.find("ordergroup");
204                                 //user has given a template file
205                                 if(it != parameters.end()){ 
206                                         path = m->hasPath(it->second);
207                                         //if the user has not given a path then, add inputdir. else leave path alone.
208                                         if (path == "") {       parameters["ordergroup"] = inputDir + it->second;               }
209                                 }
210                                 
211                                 it = parameters.find("relabund");
212                                 //user has given a template file
213                                 if(it != parameters.end()){ 
214                                         path = m->hasPath(it->second);
215                                         //if the user has not given a path then, add inputdir. else leave path alone.
216                                         if (path == "") {       parameters["relabund"] = inputDir + it->second;         }
217                                 }
218                                 
219                                 it = parameters.find("fasta");
220                                 //user has given a template file
221                                 if(it != parameters.end()){ 
222                                         path = m->hasPath(it->second);
223                                         //if the user has not given a path then, add inputdir. else leave path alone.
224                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
225                                 }
226                                 
227                                 it = parameters.find("qfile");
228                                 //user has given a template file
229                                 if(it != parameters.end()){ 
230                                         path = m->hasPath(it->second);
231                                         //if the user has not given a path then, add inputdir. else leave path alone.
232                                         if (path == "") {       parameters["qfile"] = inputDir + it->second;            }
233                                 }
234                                 
235                                 it = parameters.find("sff");
236                                 //user has given a template file
237                                 if(it != parameters.end()){ 
238                                         path = m->hasPath(it->second);
239                                         //if the user has not given a path then, add inputdir. else leave path alone.
240                                         if (path == "") {       parameters["sff"] = inputDir + it->second;              }
241                                 }
242                                 
243                                 it = parameters.find("oligos");
244                                 //user has given a template file
245                                 if(it != parameters.end()){ 
246                                         path = m->hasPath(it->second);
247                                         //if the user has not given a path then, add inputdir. else leave path alone.
248                                         if (path == "") {       parameters["oligos"] = inputDir + it->second;           }
249                                 }
250                                 
251                                 it = parameters.find("accnos");
252                                 //user has given a template file
253                                 if(it != parameters.end()){ 
254                                         path = m->hasPath(it->second);
255                                         //if the user has not given a path then, add inputdir. else leave path alone.
256                                         if (path == "") {       parameters["accnos"] = inputDir + it->second;           }
257                                 }
258
259                                 it = parameters.find("taxonomy");
260                                 //user has given a template file
261                                 if(it != parameters.end()){ 
262                                         path = m->hasPath(it->second);
263                                         //if the user has not given a path then, add inputdir. else leave path alone.
264                                         if (path == "") {       parameters["taxonomy"] = inputDir + it->second;         }
265                                 }
266                                 
267                                 it = parameters.find("flow");
268                                 //user has given a template file
269                                 if(it != parameters.end()){ 
270                                         path = m->hasPath(it->second);
271                                         //if the user has not given a path then, add inputdir. else leave path alone.
272                                         if (path == "") {       parameters["flow"] = inputDir + it->second;             }
273                                 }
274                         }
275                         
276                         //check for parameters
277                         phylipfile = validParameter.validFile(parameters, "phylip", true);
278                         if (phylipfile == "not open") { m->mothurOut("Ignoring: " + parameters["phylip"]); m->mothurOutEndLine(); phylipfile = ""; }
279                         else if (phylipfile == "not found") {  phylipfile = "";  }      
280                         if (phylipfile != "") { m->setPhylipFile(phylipfile); }
281                         
282                         columnfile = validParameter.validFile(parameters, "column", true);
283                         if (columnfile == "not open") { m->mothurOut("Ignoring: " + parameters["column"]); m->mothurOutEndLine(); columnfile = ""; }
284                         else if (columnfile == "not found") {  columnfile = "";  }      
285                         if (columnfile != "") { m->setColumnFile(columnfile); }
286                         
287                         listfile = validParameter.validFile(parameters, "list", true);
288                         if (listfile == "not open") { m->mothurOut("Ignoring: " + parameters["list"]); m->mothurOutEndLine(); listfile = ""; }
289                         else if (listfile == "not found") {  listfile = "";  }  
290                         if (listfile != "") { m->setListFile(listfile); }
291                         
292                         rabundfile = validParameter.validFile(parameters, "rabund", true);
293                         if (rabundfile == "not open") { m->mothurOut("Ignoring: " + parameters["rabund"]); m->mothurOutEndLine(); rabundfile = ""; }
294                         else if (rabundfile == "not found") {  rabundfile = "";  }      
295                         if (rabundfile != "") { m->setRabundFile(rabundfile); }
296                         
297                         sabundfile = validParameter.validFile(parameters, "sabund", true);
298                         if (sabundfile == "not open") { m->mothurOut("Ignoring: " + parameters["sabund"]); m->mothurOutEndLine(); sabundfile = ""; }
299                         else if (sabundfile == "not found") {  sabundfile = "";  }      
300                         if (sabundfile != "") { m->setSabundFile(sabundfile); }
301                         
302                         namefile = validParameter.validFile(parameters, "name", true);
303                         if (namefile == "not open") { m->mothurOut("Ignoring: " + parameters["name"]); m->mothurOutEndLine(); namefile = ""; }
304                         else if (namefile == "not found") {  namefile = "";  }  
305                         if (namefile != "") { m->setNameFile(namefile); }
306                         
307                         groupfile = validParameter.validFile(parameters, "group", true);
308                         if (groupfile == "not open") { m->mothurOut("Ignoring: " + parameters["group"]); m->mothurOutEndLine(); groupfile = ""; }
309                         else if (groupfile == "not found") {  groupfile = "";  }
310                         if (groupfile != "") { m->setGroupFile(groupfile); }
311                         
312                         designfile = validParameter.validFile(parameters, "design", true);
313                         if (designfile == "not open") { m->mothurOut("Ignoring: " + parameters["design"]); m->mothurOutEndLine(); designfile = ""; }
314                         else if (designfile == "not found") {  designfile = "";  }      
315                         if (designfile != "") { m->setDesignFile(designfile); }
316                         
317                         orderfile = validParameter.validFile(parameters, "order", true);
318                         if (orderfile == "not open") { m->mothurOut("Ignoring: " + parameters["order"]); m->mothurOutEndLine(); orderfile = ""; }
319                         else if (orderfile == "not found") {  orderfile = "";  }
320                         if (orderfile != "") { m->setOrderFile(orderfile); }
321                         
322                         treefile = validParameter.validFile(parameters, "tree", true);
323                         if (treefile == "not open") { m->mothurOut("Ignoring: " + parameters["tree"]); m->mothurOutEndLine(); treefile = ""; }
324                         else if (treefile == "not found") {  treefile = "";  }  
325                         if (treefile != "") { m->setTreeFile(treefile); }
326                         
327                         sharedfile = validParameter.validFile(parameters, "shared", true);
328                         if (sharedfile == "not open") { m->mothurOut("Ignoring: " + parameters["shared"]); m->mothurOutEndLine(); sharedfile = ""; }
329                         else if (sharedfile == "not found") {  sharedfile = "";  }      
330                         if (sharedfile != "") { m->setSharedFile(sharedfile); }
331                         
332                         ordergroupfile = validParameter.validFile(parameters, "ordergroup", true);
333                         if (ordergroupfile == "not open") { m->mothurOut("Ignoring: " + parameters["ordergroup"]); m->mothurOutEndLine(); ordergroupfile = ""; }
334                         else if (ordergroupfile == "not found") {  ordergroupfile = "";  }      
335                         if (ordergroupfile != "") { m->setOrderGroupFile(ordergroupfile); }
336                         
337                         relabundfile = validParameter.validFile(parameters, "relabund", true);
338                         if (relabundfile == "not open") { m->mothurOut("Ignoring: " + parameters["relabund"]); m->mothurOutEndLine(); relabundfile = ""; }
339                         else if (relabundfile == "not found") {  relabundfile = "";  }  
340                         if (relabundfile != "") { m->setRelAbundFile(relabundfile); }
341                         
342                         fastafile = validParameter.validFile(parameters, "fasta", true);
343                         if (fastafile == "not open") { m->mothurOut("Ignoring: " + parameters["fasta"]); m->mothurOutEndLine(); fastafile = ""; }
344                         else if (fastafile == "not found") {  fastafile = "";  }
345                         if (fastafile != "") { m->setFastaFile(fastafile); }
346                         
347                         qualfile = validParameter.validFile(parameters, "qfile", true);
348                         if (qualfile == "not open") { m->mothurOut("Ignoring: " + parameters["qfile"]); m->mothurOutEndLine(); qualfile = ""; }
349                         else if (qualfile == "not found") {  qualfile = "";  }  
350                         if (qualfile != "") { m->setQualFile(qualfile); }
351                         
352                         sfffile = validParameter.validFile(parameters, "sff", true);
353                         if (sfffile == "not open") { m->mothurOut("Ignoring: " + parameters["sff"]); m->mothurOutEndLine(); sfffile = ""; }
354                         else if (sfffile == "not found") {  sfffile = "";  }    
355                         if (sfffile != "") { m->setSFFFile(sfffile); }
356                         
357                         oligosfile = validParameter.validFile(parameters, "oligos", true);
358                         if (oligosfile == "not open") { m->mothurOut("Ignoring: " + parameters["oligos"]); m->mothurOutEndLine(); oligosfile = ""; }
359                         else if (oligosfile == "not found") {  oligosfile = "";  }      
360                         if (oligosfile != "") { m->setOligosFile(oligosfile); }
361                         
362                         accnosfile = validParameter.validFile(parameters, "accnos", true);
363                         if (accnosfile == "not open") { m->mothurOut("Ignoring: " + parameters["accnos"]); m->mothurOutEndLine(); accnosfile = ""; }
364                         else if (accnosfile == "not found") {  accnosfile = "";  }      
365                         if (accnosfile != "") { m->setAccnosFile(accnosfile); }
366                         
367                         taxonomyfile = validParameter.validFile(parameters, "taxonomy", true);
368                         if (taxonomyfile == "not open") { m->mothurOut("Ignoring: " + parameters["taxonomy"]); m->mothurOutEndLine(); taxonomyfile = ""; }
369                         else if (taxonomyfile == "not found") {  taxonomyfile = "";  }  
370                         if (taxonomyfile != "") { m->setTaxonomyFile(taxonomyfile); }
371                         
372                         flowfile = validParameter.validFile(parameters, "flow", true);
373                         if (flowfile == "not open") { m->mothurOut("Ignoring: " + parameters["flow"]); m->mothurOutEndLine(); flowfile = ""; }
374                         else if (flowfile == "not found") {  flowfile = "";  }  
375                         if (flowfile != "") { m->setFlowFile(flowfile); }
376                         
377                         processors = validParameter.validFile(parameters, "processors", false);
378                         if (processors == "not found") {  processors = "1";  }  
379                         if (processors != "") { m->setProcessors(processors); }
380                         
381                         clearTypes = validParameter.validFile(parameters, "clear", false);                      
382                         if (clearTypes == "not found") { clearTypes = ""; }
383                         else { m->splitAtDash(clearTypes, types);       }
384                 }
385                 
386         }
387         catch(exception& e) {
388                 m->errorOut(e, "SetCurrentCommand", "SetCurrentCommand");
389                 exit(1);
390         }
391 }
392 //**********************************************************************************************************************
393
394 int SetCurrentCommand::execute(){
395         try {
396                 
397                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
398                 
399                 //user wants to clear a type
400                 if (types.size() != 0) {
401                         for (int i = 0; i < types.size(); i++) {
402                                 
403                                 if (m->control_pressed) { break; }
404                                 
405                                 //look for file types
406                                 if (types[i] == "fasta") {
407                                         m->setFastaFile("");
408                                 }else if (types[i] == "qfile") {
409                                         m->setQualFile("");
410                                 }else if (types[i] == "phylip") {
411                                         m->setPhylipFile("");
412                                 }else if (types[i] == "column") {
413                                         m->setColumnFile("");
414                                 }else if (types[i] == "list") {
415                                         m->setListFile("");
416                                 }else if (types[i] == "rabund") {
417                                         m->setRabundFile("");
418                                 }else if (types[i] == "sabund") {
419                                         m->setSabundFile("");
420                                 }else if (types[i] == "name") {
421                                         m->setNameFile("");
422                                 }else if (types[i] == "group") {
423                                         m->setGroupFile("");
424                                 }else if (types[i] == "order") {
425                                         m->setOrderFile("");
426                                 }else if (types[i] == "ordergroup") {
427                                         m->setOrderGroupFile("");
428                                 }else if (types[i] == "tree") {
429                                         m->setTreeFile("");
430                                 }else if (types[i] == "shared") {
431                                         m->setSharedFile("");
432                                 }else if (types[i] == "relabund") {
433                                         m->setRelAbundFile("");
434                                 }else if (types[i] == "design") {
435                                         m->setDesignFile("");
436                                 }else if (types[i] == "sff") {
437                                         m->setSFFFile("");
438                                 }else if (types[i] == "oligos") {
439                                         m->setOligosFile("");
440                                 }else if (types[i] == "accnos") {
441                                         m->setAccnosFile("");
442                                 }else if (types[i] == "taxonomy") {
443                                         m->setTaxonomyFile("");
444                                 }else if (types[i] == "flow") {
445                                         m->setFlowFile("");
446                                 }else if (types[i] == "processors") {
447                                         m->setProcessors("1");
448                                 }else if (types[i] == "all") {
449                                         m->clearCurrentFiles();
450                                 }else {
451                                         m->mothurOut("[ERROR]: mothur does not save a current file for " + types[i]); m->mothurOutEndLine();
452                                 }
453                         }
454                 }
455                 
456                 m->mothurOutEndLine(); m->mothurOut("Current files saved by mothur:"); m->mothurOutEndLine();
457                 m->printCurrentFiles();
458                 
459                 return 0;       
460         }
461         
462         catch(exception& e) {
463                 m->errorOut(e, "SetCurrentCommand", "execute");
464                 exit(1);
465         }
466 }
467
468 //**********************************************************************************************************************
469
470
471