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