]> git.donarmstrong.com Git - mothur.git/blob - commandfactory.cpp
2ac61e33e3252af5581b78b21168a5e2fbe760fb
[mothur.git] / commandfactory.cpp
1 /*
2  *  commandfactory.cpp
3  *  
4  *
5  *  Created by Pat Schloss on 10/25/08.
6  *  Copyright 2008 Patrick D. Schloss. All rights reserved.
7  *
8  */
9
10 #include "command.hpp"
11 #include "readdistcommand.h"
12 #include "readtreecommand.h"
13 #include "readotucommand.h"
14 #include "clustercommand.h"
15 #include "collectcommand.h"
16 #include "collectsharedcommand.h"
17 #include "getgroupcommand.h"
18 #include "getlabelcommand.h"
19 #include "rarefactcommand.h"
20 #include "summarycommand.h"
21 #include "summarysharedcommand.h"
22 #include "rarefactsharedcommand.h"
23 #include "quitcommand.h"
24 #include "helpcommand.h"
25 #include "commandfactory.hpp"
26 #include "deconvolutecommand.h"
27 #include "parsimonycommand.h"
28 #include "unifracunweightedcommand.h"
29 #include "unifracweightedcommand.h"
30 #include "libshuffcommand.h"
31 #include "heatmapcommand.h"
32 #include "heatmapsimcommand.h"
33 #include "filterseqscommand.h"
34 #include "venncommand.h"
35 #include "nocommands.h"
36 #include "binsequencecommand.h"
37 #include "getoturepcommand.h"
38 #include "treegroupscommand.h"
39 #include "bootstrapsharedcommand.h"
40 //#include "consensuscommand.h"
41 #include "distancecommand.h"
42 #include "aligncommand.h"
43 #include "matrixoutputcommand.h"
44 #include "getsabundcommand.h"
45 #include "getrabundcommand.h"
46 #include "seqsummarycommand.h"
47 #include "screenseqscommand.h"
48 #include "reversecommand.h"
49 #include "trimseqscommand.h"
50 #include "mergefilecommand.h"
51 #include "chimeraseqscommand.h"
52 #include "listseqscommand.h"
53 #include "getseqscommand.h"
54 #include "removeseqscommand.h"
55 #include "systemcommand.h"
56 #include "secondarystructurecommand.h"
57 #include "getsharedotucommand.h"
58 #include "getlistcountcommand.h"
59 #include "hclustercommand.h"
60 #include "classifyseqscommand.h"
61 #include "phylotypecommand.h"
62 #include "mgclustercommand.h"
63 #include "preclustercommand.h"
64 #include "pcacommand.h"
65 #include "otuhierarchycommand.h"
66 #include "setdircommand.h"
67 #include "parselistscommand.h"
68 #include "parsesffcommand.h"
69 #include "chimeraccodecommand.h"
70 #include "chimeracheckcommand.h"
71 #include "chimeraslayercommand.h"
72 #include "chimerapintailcommand.h"
73 #include "chimerabellerophoncommand.h"
74 #include "setlogfilecommand.h"
75 #include "phylodiversitycommand.h"
76 #include "makegroupcommand.h"
77 #include "chopseqscommand.h"
78
79 /*******************************************************/
80
81 /******************************************************/
82 CommandFactory* CommandFactory::getInstance() {
83         if( _uniqueInstance == 0) {
84                 _uniqueInstance = new CommandFactory();
85         }
86         return _uniqueInstance;
87 }
88 /***********************************************************/
89
90 /***********************************************************/
91 //note: This class is resposible for knowing which commands are mpiEnabled,
92 //If a command is not enabled only process 0 will execute the command. 
93 //This avoids redundant outputs on pieces of code we have not paralellized. 
94 //If you add mpi code to a existing command you need to modify the list below or the code will hang on MPI blocking commands like FIle_open. 
95 //example:  commands["dist.seqs"] = "MPIEnabled";
96
97 CommandFactory::CommandFactory(){
98         string s = "";
99         m = MothurOut::getInstance();
100         
101         command = new NoCommand(s);
102         
103         outputDir = ""; inputDir = "";
104         logFileName = "";
105         append = false;
106         
107         //initialize list of valid commands
108         commands["read.dist"]                   = "read.dist"; 
109         commands["read.otu"]                    = "read.otu";
110         commands["read.tree"]                   = "read.tree"; 
111         commands["bin.seqs"]                    = "bin.seqs"; 
112         commands["get.oturep"]                  = "get.oturep";
113         commands["cluster"]                             = "cluster"; 
114         commands["unique.seqs"]                 = "unique.seqs"; 
115         commands["dist.shared"]                 = "dist.shared";
116         commands["collect.single"]              = "collect.single"; 
117         commands["collect.shared"]              = "collect.shared"; 
118         commands["rarefaction.single"]  = "rarefaction.single"; 
119         commands["rarefaction.shared"]  = "rarefaction.shared"; 
120         commands["summary.single"]              = "summary.single"; 
121         commands["summary.shared"]              = "summary.shared"; 
122         commands["parsimony"]                   = "parsimony";
123         commands["unifrac.weighted"]    = "unifrac.weighted"; 
124         commands["unifrac.unweighted"]  = "unifrac.unweighted"; 
125         commands["libshuff"]                    = "libshuff";
126         commands["tree.shared"]                 = "tree.shared";
127         commands["heatmap.bin"]                 = "heatmap.bin";
128         commands["heatmap.sim"]                 = "heatmap.sim";
129         commands["venn"]                                = "venn";
130         commands["get.group"]           = "get.group";
131         commands["get.label"]           = "get.label";
132         commands["get.sabund"]          = "get.sabund";
133         commands["get.rabund"]          = "get.rabund";
134         commands["bootstrap.shared"]    = "bootstrap.shared";
135         //commands["consensus"]                 = "consensus";
136         commands["help"]                                = "help";
137         commands["reverse.seqs"]                = "reverse.seqs";
138         commands["trim.seqs"]                   = "trim.seqs";
139         commands["list.seqs"]                   = "list.seqs";
140         commands["get.seqs"]                    = "get.seqs";
141         commands["remove.seqs"]                 = "get.seqs";
142         commands["system"]                              = "system";
143         commands["align.check"]                 = "align.check";
144         commands["get.sharedseqs"]              = "get.sharedseqs";
145         commands["get.otulist"]                 = "get.otulist";
146         commands["hcluster"]                    = "hcluster"; 
147         commands["phylotype"]                   = "phylotype";
148         commands["mgcluster"]                   = "mgcluster";
149         commands["pre.cluster"]                 = "pre.cluster";
150         commands["pcoa"]                                = "pcoa";
151         commands["otu.hierarchy"]               = "otu.hierarchy";
152         commands["set.dir"]                             = "set.dir";
153         commands["merge.files"]                 = "merge.files";
154         commands["parse.list"]                  = "parse.list";
155         commands["parse.sff"]                   = "parse.sff";
156         commands["set.logfile"]                 = "set.logfile";
157         commands["phylo.diversity"]             = "phylo.diversity";
158         commands["make.group"]                  = "make.group";
159         commands["chop.seqs"]                   = "chop.seqs";
160         commands["classify.seqs"]               = "MPIEnabled"; 
161         commands["dist.seqs"]                   = "MPIEnabled";
162         commands["filter.seqs"]                 = "MPIEnabled";
163         commands["align.seqs"]                  = "MPIEnabled";
164         commands["chimera.seqs"]                = "chimera.seqs";
165         commands["chimera.ccode"]               = "MPIEnabled";
166         commands["chimera.check"]               = "MPIEnabled";
167         commands["chimera.slayer"]              = "MPIEnabled";
168         commands["chimera.pintail"]             = "MPIEnabled";
169         commands["chimera.bellerophon"] = "MPIEnabled";
170         commands["screen.seqs"]                 = "MPIEnabled";
171         commands["summary.seqs"]                = "MPIEnabled";
172         commands["quit"]                                = "MPIEnabled"; 
173
174 }
175 /***********************************************************/
176
177 /***********************************************************/
178 bool CommandFactory::MPIEnabled(string commandName) {
179         bool mpi = false;
180         it = commands.find(commandName);
181         if (it != commands.end()) { 
182                 if (it->second == "MPIEnabled") { return true; }
183         }
184         return mpi;
185 }
186 /***********************************************************/
187
188 /***********************************************************/
189 CommandFactory::~CommandFactory(){
190         _uniqueInstance = 0;
191         delete command;
192 }
193
194 /***********************************************************/
195
196 /***********************************************************/
197 //This function calls the appropriate command fucntions based on user input.
198 Command* CommandFactory::getCommand(string commandName, string optionString){
199         try {
200                 delete command;   //delete the old command
201                 
202                 //user has opted to redirect output from dir where input files are located to some other place
203                 if (outputDir != "") { 
204                         if (optionString != "") { optionString += ", outputdir=" + outputDir; }
205                         else { optionString += "outputdir=" + outputDir; }
206                 }
207                 
208                 //user has opted to redirect input from dir where mothur.exe is located to some other place
209                 if (inputDir != "") { 
210                         if (optionString != "") { optionString += ", inputdir=" + inputDir; }
211                         else { optionString += "inputdir=" + inputDir; }
212                 }
213                 
214                 if(commandName == "read.dist")                                  {       command = new ReadDistCommand(optionString);                            }
215                 else if(commandName == "read.otu")                              {       command = new ReadOtuCommand(optionString);                                     }
216                 else if(commandName == "read.tree")                             {       command = new ReadTreeCommand(optionString);                            }
217                 else if(commandName == "cluster")                               {       command = new ClusterCommand(optionString);                                     }
218                 else if(commandName == "unique.seqs")                   {       command = new DeconvoluteCommand(optionString);                         }
219                 else if(commandName == "parsimony")                             {       command = new ParsimonyCommand(optionString);                           }
220                 else if(commandName == "help")                                  {       command = new HelpCommand(optionString);                                        }
221                 else if(commandName == "quit")                                  {       command = new QuitCommand(optionString);                                        }
222                 else if(commandName == "collect.single")                {       command = new CollectCommand(optionString);                                     }
223                 else if(commandName == "collect.shared")                {       command = new CollectSharedCommand(optionString);                       }
224                 else if(commandName == "rarefaction.single")    {       command = new RareFactCommand(optionString);                            }
225                 else if(commandName == "rarefaction.shared")    {       command = new RareFactSharedCommand(optionString);                      }
226                 else if(commandName == "summary.single")                {       command = new SummaryCommand(optionString);                                     }
227                 else if(commandName == "summary.shared")                {       command = new SummarySharedCommand(optionString);                       }
228                 else if(commandName == "unifrac.weighted")              {       command = new UnifracWeightedCommand(optionString);                     }
229                 else if(commandName == "unifrac.unweighted")    {       command = new UnifracUnweightedCommand(optionString);           }
230                 else if(commandName == "get.group")             {   command = new GetgroupCommand(optionString);                                }
231                 else if(commandName == "get.label")             {   command = new GetlabelCommand(optionString);                                }
232                 else if(commandName == "get.sabund")            {   command = new GetSAbundCommand(optionString);                               }
233                 else if(commandName == "get.rabund")            {   command = new GetRAbundCommand(optionString);                               }
234                 else if(commandName == "libshuff")              {   command = new LibShuffCommand(optionString);                                }
235                 else if(commandName == "heatmap.bin")                   {   command = new HeatMapCommand(optionString);                                 }
236                 else if(commandName == "heatmap.sim")                   {   command = new HeatMapSimCommand(optionString);                              }
237                 else if(commandName == "filter.seqs")                   {   command = new FilterSeqsCommand(optionString);                              }
238                 else if(commandName == "venn")                                  {   command = new VennCommand(optionString);                                    }
239                 else if(commandName == "bin.seqs")                              {   command = new BinSeqCommand(optionString);                                  }
240                 else if(commandName == "get.oturep")                    {   command = new GetOTURepCommand(optionString);                               }
241                 else if(commandName == "tree.shared")                   {   command = new TreeGroupCommand(optionString);                               }
242                 else if(commandName == "dist.shared")                   {   command = new MatrixOutputCommand(optionString);                    }
243                 else if(commandName == "bootstrap.shared")              {   command = new BootSharedCommand(optionString);                              }
244                 //else if(commandName == "consensus")                   {   command = new ConcensusCommand(optionString);                               }
245                 else if(commandName == "dist.seqs")                             {   command = new DistanceCommand(optionString);                                }
246                 else if(commandName == "align.seqs")                    {   command = new AlignCommand(optionString);                                   }
247                 else if(commandName == "summary.seqs")                  {       command = new SeqSummaryCommand(optionString);                          }
248                 else if(commandName == "screen.seqs")                   {       command = new ScreenSeqsCommand(optionString);                          }
249                 else if(commandName == "reverse.seqs")                  {       command = new ReverseSeqsCommand(optionString);                         }
250                 else if(commandName == "trim.seqs")                             {       command = new TrimSeqsCommand(optionString);                            }
251                 else if(commandName == "chimera.seqs")                  {       command = new ChimeraSeqsCommand(optionString);                         }
252                 else if(commandName == "list.seqs")                             {       command = new ListSeqsCommand(optionString);                            }
253                 else if(commandName == "get.seqs")                              {       command = new GetSeqsCommand(optionString);                                     }
254                 else if(commandName == "remove.seqs")                   {       command = new RemoveSeqsCommand(optionString);                          }
255                 else if(commandName == "merge.files")                   {       command = new MergeFileCommand(optionString);                           }
256                 else if(commandName == "system")                                {       command = new SystemCommand(optionString);                                      }
257                 else if(commandName == "align.check")                   {       command = new AlignCheckCommand(optionString);                          }
258                 else if(commandName == "get.sharedseqs")                {       command = new GetSharedOTUCommand(optionString);                        }
259                 else if(commandName == "get.otulist")                   {       command = new GetListCountCommand(optionString);                        }
260                 else if(commandName == "hcluster")                              {       command = new HClusterCommand(optionString);                            }
261                 else if(commandName == "classify.seqs")                 {       command = new ClassifySeqsCommand(optionString);                        }
262                 else if(commandName == "chimera.ccode")                 {       command = new ChimeraCcodeCommand(optionString);                        }
263                 else if(commandName == "chimera.check")                 {       command = new ChimeraCheckCommand(optionString);                        }
264                 else if(commandName == "chimera.slayer")                {       command = new ChimeraSlayerCommand(optionString);                       }
265                 else if(commandName == "chimera.pintail")               {       command = new ChimeraPintailCommand(optionString);                      }
266                 else if(commandName == "chimera.bellerophon")   {       command = new ChimeraBellerophonCommand(optionString);          }
267                 else if(commandName == "phylotype")                             {       command = new PhylotypeCommand(optionString);                           }
268                 else if(commandName == "mgcluster")                             {       command = new MGClusterCommand(optionString);                           }
269                 else if(commandName == "pre.cluster")                   {       command = new PreClusterCommand(optionString);                          }
270                 else if(commandName == "pcoa")                                  {       command = new PCACommand(optionString);                                         }
271                 else if(commandName == "otu.hierarchy")                 {       command = new OtuHierarchyCommand(optionString);                        }
272                 else if(commandName == "set.dir")                               {       command = new SetDirectoryCommand(optionString);                        }
273                 else if(commandName == "set.logfile")                   {       command = new SetLogFileCommand(optionString);                          }
274                 else if(commandName == "parse.list")                    {       command = new ParseListCommand(optionString);                           }
275                 else if(commandName == "parse.sff")                             {       command = new ParseSFFCommand(optionString);                            }
276                 else if(commandName == "phylo.diversity")               {       command = new PhyloDiversityCommand(optionString);                      }
277                 else if(commandName == "make.group")                    {       command = new MakeGroupCommand(optionString);                           }
278                 else if(commandName == "chop.seqs")                             {       command = new ChopSeqsCommand(optionString);                            }
279                 else                                                                                    {       command = new NoCommand(optionString);                                          }
280
281                 return command;
282         }
283         catch(exception& e) {
284                 m->errorOut(e, "CommandFactory", "getCommand");
285                 exit(1);
286         }
287 }
288 /***********************************************************/
289 //This function is used to interrupt a command
290 Command* CommandFactory::getCommand(){
291         try {
292                 delete command;   //delete the old command
293
294                 string s = "";
295             command = new NoCommand(s);
296         
297                 return command;
298         }
299         catch(exception& e) {
300                 m->errorOut(e, "CommandFactory", "getCommand");
301                 exit(1);
302         }
303 }
304 /***********************************************************************/
305 bool CommandFactory::isValidCommand(string command) {
306         try {   
307         
308                 //is the command in the map
309                 if ((commands.find(command)) != (commands.end())) {
310                         return true;
311                 }else{
312                         m->mothurOut(command + " is not a valid command in Mothur.  Valid commands are ");
313                         for (it = commands.begin(); it != commands.end(); it++) {
314                                 m->mothurOut(it->first + ", ");
315                         }
316                         m->mothurOutEndLine();
317                         return false;
318                 }
319                 
320         }
321         catch(exception& e) {
322                 m->errorOut(e, "CommandFactory", "isValidCommand");
323                 exit(1);
324         }
325 }
326
327 /***********************************************************************/
328 void CommandFactory::printCommands(ostream& out) {
329         try {   
330                 out << "Valid commands are ";
331                 for (it = commands.begin(); it != commands.end(); it++) {
332                         out << it->first << ", ";
333                 }
334                 out << endl;
335         }
336         catch(exception& e) {
337                 m->errorOut(e, "CommandFactory", "printCommands");
338                 exit(1);
339         }
340 }
341 /***********************************************************************/
342
343
344
345