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