]> git.donarmstrong.com Git - mothur.git/blob - commandfactory.cpp
pat's updates on 7/19/10
[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 #include "clearcutcommand.h"
79 #include "catchallcommand.h"
80 #include "splitabundcommand.h"
81 #include "clustersplitcommand.h"
82 #include "classifyotucommand.h"
83 #include "degapseqscommand.h"
84 #include "getrelabundcommand.h"
85 #include "sensspeccommand.h"
86 #include "sffinfocommand.h"
87 #include "seqerrorcommand.h"
88
89 /*******************************************************/
90
91 /******************************************************/
92 CommandFactory* CommandFactory::getInstance() {
93         if( _uniqueInstance == 0) {
94                 _uniqueInstance = new CommandFactory();
95         }
96         return _uniqueInstance;
97 }
98 /***********************************************************/
99
100 /***********************************************************/
101 //note: This class is resposible for knowing which commands are mpiEnabled,
102 //If a command is not enabled only process 0 will execute the command. 
103 //This avoids redundant outputs on pieces of code we have not paralellized. 
104 //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. 
105 //example:  commands["dist.seqs"] = "MPIEnabled";
106
107 CommandFactory::CommandFactory(){
108         string s = "";
109         m = MothurOut::getInstance();
110         
111         command = new NoCommand(s);
112         
113         outputDir = ""; inputDir = "";
114         logFileName = "";
115         append = false;
116         
117         //initialize list of valid commands
118         commands["read.dist"]                   = "read.dist"; 
119         commands["read.otu"]                    = "read.otu";
120         commands["read.tree"]                   = "read.tree"; 
121         commands["bin.seqs"]                    = "bin.seqs"; 
122         commands["get.oturep"]                  = "get.oturep";
123         commands["cluster"]                             = "cluster"; 
124         commands["unique.seqs"]                 = "unique.seqs"; 
125         commands["dist.shared"]                 = "dist.shared";
126         commands["collect.single"]              = "collect.single"; 
127         commands["collect.shared"]              = "collect.shared"; 
128         commands["rarefaction.single"]  = "rarefaction.single"; 
129         commands["rarefaction.shared"]  = "rarefaction.shared"; 
130         commands["summary.single"]              = "summary.single"; 
131         commands["summary.shared"]              = "summary.shared"; 
132         commands["parsimony"]                   = "parsimony";
133         commands["unifrac.weighted"]    = "unifrac.weighted"; 
134         commands["unifrac.unweighted"]  = "unifrac.unweighted"; 
135         commands["libshuff"]                    = "libshuff";
136         commands["tree.shared"]                 = "tree.shared";
137         commands["heatmap.bin"]                 = "heatmap.bin";
138         commands["heatmap.sim"]                 = "heatmap.sim";
139         commands["venn"]                                = "venn";
140         commands["get.group"]           = "get.group";
141         commands["get.label"]           = "get.label";
142         commands["get.sabund"]          = "get.sabund";
143         commands["get.rabund"]          = "get.rabund";
144         commands["bootstrap.shared"]    = "bootstrap.shared";
145         //commands["consensus"]                 = "consensus";
146         commands["help"]                                = "help";
147         commands["reverse.seqs"]                = "reverse.seqs";
148         commands["trim.seqs"]                   = "trim.seqs";
149         commands["list.seqs"]                   = "list.seqs";
150         commands["get.seqs"]                    = "get.seqs";
151         commands["remove.seqs"]                 = "get.seqs";
152         commands["system"]                              = "system";
153         commands["align.check"]                 = "align.check";
154         commands["get.sharedseqs"]              = "get.sharedseqs";
155         commands["get.otulist"]                 = "get.otulist";
156         commands["hcluster"]                    = "hcluster"; 
157         commands["phylotype"]                   = "phylotype";
158         commands["mgcluster"]                   = "mgcluster";
159         commands["pre.cluster"]                 = "pre.cluster";
160         commands["pcoa"]                                = "pcoa";
161         commands["otu.hierarchy"]               = "otu.hierarchy";
162         commands["set.dir"]                             = "set.dir";
163         commands["merge.files"]                 = "merge.files";
164         commands["parse.list"]                  = "parse.list";
165         commands["parse.sff"]                   = "parse.sff";
166         commands["set.logfile"]                 = "set.logfile";
167         commands["phylo.diversity"]             = "phylo.diversity";
168         commands["make.group"]                  = "make.group";
169         commands["chop.seqs"]                   = "chop.seqs";
170         commands["clearcut"]                    = "clearcut";
171         commands["catchall"]                    = "catchall";
172         commands["split.abund"]                 = "split.abund";
173         commands["classify.otu"]                = "classify.otu";
174         commands["degap.seqs"]                  = "degap.seqs";
175         commands["get.relabund"]                = "get.relabund";
176         commands["sffinfo"]                             = "sffinfo";
177         commands["classify.seqs"]               = "MPIEnabled"; 
178         commands["dist.seqs"]                   = "MPIEnabled";
179         commands["filter.seqs"]                 = "MPIEnabled";
180         commands["align.seqs"]                  = "MPIEnabled";
181         commands["chimera.seqs"]                = "chimera.seqs";
182         commands["chimera.ccode"]               = "MPIEnabled";
183         commands["chimera.check"]               = "MPIEnabled";
184         commands["chimera.slayer"]              = "MPIEnabled";
185         commands["chimera.pintail"]             = "MPIEnabled";
186         commands["chimera.bellerophon"] = "MPIEnabled";
187         commands["screen.seqs"]                 = "MPIEnabled";
188         commands["summary.seqs"]                = "MPIEnabled";
189         commands["cluster.split"]               = "MPIEnabled";
190         commands["sens.spec"]                   = "sens.spec";
191         commands["seq.error"]                   = "seq.error";
192         commands["quit"]                                = "MPIEnabled"; 
193
194 }
195 /***********************************************************/
196
197 /***********************************************************/
198 bool CommandFactory::MPIEnabled(string commandName) {
199         bool mpi = false;
200         it = commands.find(commandName);
201         if (it != commands.end()) { 
202                 if (it->second == "MPIEnabled") { return true; }
203         }
204         return mpi;
205 }
206 /***********************************************************/
207
208 /***********************************************************/
209 CommandFactory::~CommandFactory(){
210         _uniqueInstance = 0;
211         delete command;
212 }
213
214 /***********************************************************/
215
216 /***********************************************************/
217 //This function calls the appropriate command fucntions based on user input.
218 Command* CommandFactory::getCommand(string commandName, string optionString){
219         try {
220                 delete command;   //delete the old command
221                 
222                 //user has opted to redirect output from dir where input files are located to some other place
223                 if (outputDir != "") { 
224                         if (optionString != "") { optionString += ", outputdir=" + outputDir; }
225                         else { optionString += "outputdir=" + outputDir; }
226                 }
227                 
228                 //user has opted to redirect input from dir where mothur.exe is located to some other place
229                 if (inputDir != "") { 
230                         if (optionString != "") { optionString += ", inputdir=" + inputDir; }
231                         else { optionString += "inputdir=" + inputDir; }
232                 }
233                 
234                 if(commandName == "read.dist")                                  {       command = new ReadDistCommand(optionString);                            }
235                 else if(commandName == "read.otu")                              {       command = new ReadOtuCommand(optionString);                                     }
236                 else if(commandName == "read.tree")                             {       command = new ReadTreeCommand(optionString);                            }
237                 else if(commandName == "cluster")                               {       command = new ClusterCommand(optionString);                                     }
238                 else if(commandName == "unique.seqs")                   {       command = new DeconvoluteCommand(optionString);                         }
239                 else if(commandName == "parsimony")                             {       command = new ParsimonyCommand(optionString);                           }
240                 else if(commandName == "help")                                  {       command = new HelpCommand(optionString);                                        }
241                 else if(commandName == "quit")                                  {       command = new QuitCommand(optionString);                                        }
242                 else if(commandName == "collect.single")                {       command = new CollectCommand(optionString);                                     }
243                 else if(commandName == "collect.shared")                {       command = new CollectSharedCommand(optionString);                       }
244                 else if(commandName == "rarefaction.single")    {       command = new RareFactCommand(optionString);                            }
245                 else if(commandName == "rarefaction.shared")    {       command = new RareFactSharedCommand(optionString);                      }
246                 else if(commandName == "summary.single")                {       command = new SummaryCommand(optionString);                                     }
247                 else if(commandName == "summary.shared")                {       command = new SummarySharedCommand(optionString);                       }
248                 else if(commandName == "unifrac.weighted")              {       command = new UnifracWeightedCommand(optionString);                     }
249                 else if(commandName == "unifrac.unweighted")    {       command = new UnifracUnweightedCommand(optionString);           }
250                 else if(commandName == "get.group")             {   command = new GetgroupCommand(optionString);                                }
251                 else if(commandName == "get.label")             {   command = new GetlabelCommand(optionString);                                }
252                 else if(commandName == "get.sabund")            {   command = new GetSAbundCommand(optionString);                               }
253                 else if(commandName == "get.rabund")            {   command = new GetRAbundCommand(optionString);                               }
254                 else if(commandName == "libshuff")              {   command = new LibShuffCommand(optionString);                                }
255                 else if(commandName == "heatmap.bin")                   {   command = new HeatMapCommand(optionString);                                 }
256                 else if(commandName == "heatmap.sim")                   {   command = new HeatMapSimCommand(optionString);                              }
257                 else if(commandName == "filter.seqs")                   {   command = new FilterSeqsCommand(optionString);                              }
258                 else if(commandName == "venn")                                  {   command = new VennCommand(optionString);                                    }
259                 else if(commandName == "bin.seqs")                              {   command = new BinSeqCommand(optionString);                                  }
260                 else if(commandName == "get.oturep")                    {   command = new GetOTURepCommand(optionString);                               }
261                 else if(commandName == "tree.shared")                   {   command = new TreeGroupCommand(optionString);                               }
262                 else if(commandName == "dist.shared")                   {   command = new MatrixOutputCommand(optionString);                    }
263                 else if(commandName == "bootstrap.shared")              {   command = new BootSharedCommand(optionString);                              }
264                 else if(commandName == "consensus")                             {   command = new ConcensusCommand(optionString);                               }
265                 else if(commandName == "dist.seqs")                             {   command = new DistanceCommand(optionString);                                }
266                 else if(commandName == "align.seqs")                    {   command = new AlignCommand(optionString);                                   }
267                 else if(commandName == "summary.seqs")                  {       command = new SeqSummaryCommand(optionString);                          }
268                 else if(commandName == "screen.seqs")                   {       command = new ScreenSeqsCommand(optionString);                          }
269                 else if(commandName == "reverse.seqs")                  {       command = new ReverseSeqsCommand(optionString);                         }
270                 else if(commandName == "trim.seqs")                             {       command = new TrimSeqsCommand(optionString);                            }
271                 else if(commandName == "chimera.seqs")                  {       command = new ChimeraSeqsCommand(optionString);                         }
272                 else if(commandName == "list.seqs")                             {       command = new ListSeqsCommand(optionString);                            }
273                 else if(commandName == "get.seqs")                              {       command = new GetSeqsCommand(optionString);                                     }
274                 else if(commandName == "remove.seqs")                   {       command = new RemoveSeqsCommand(optionString);                          }
275                 else if(commandName == "merge.files")                   {       command = new MergeFileCommand(optionString);                           }
276                 else if(commandName == "system")                                {       command = new SystemCommand(optionString);                                      }
277                 else if(commandName == "align.check")                   {       command = new AlignCheckCommand(optionString);                          }
278                 else if(commandName == "get.sharedseqs")                {       command = new GetSharedOTUCommand(optionString);                        }
279                 else if(commandName == "get.otulist")                   {       command = new GetListCountCommand(optionString);                        }
280                 else if(commandName == "hcluster")                              {       command = new HClusterCommand(optionString);                            }
281                 else if(commandName == "classify.seqs")                 {       command = new ClassifySeqsCommand(optionString);                        }
282                 else if(commandName == "chimera.ccode")                 {       command = new ChimeraCcodeCommand(optionString);                        }
283                 else if(commandName == "chimera.check")                 {       command = new ChimeraCheckCommand(optionString);                        }
284                 else if(commandName == "chimera.slayer")                {       command = new ChimeraSlayerCommand(optionString);                       }
285                 else if(commandName == "chimera.pintail")               {       command = new ChimeraPintailCommand(optionString);                      }
286                 else if(commandName == "chimera.bellerophon")   {       command = new ChimeraBellerophonCommand(optionString);          }
287                 else if(commandName == "phylotype")                             {       command = new PhylotypeCommand(optionString);                           }
288                 else if(commandName == "mgcluster")                             {       command = new MGClusterCommand(optionString);                           }
289                 else if(commandName == "pre.cluster")                   {       command = new PreClusterCommand(optionString);                          }
290                 else if(commandName == "pcoa")                                  {       command = new PCACommand(optionString);                                         }
291                 else if(commandName == "otu.hierarchy")                 {       command = new OtuHierarchyCommand(optionString);                        }
292                 else if(commandName == "set.dir")                               {       command = new SetDirectoryCommand(optionString);                        }
293                 else if(commandName == "set.logfile")                   {       command = new SetLogFileCommand(optionString);                          }
294                 else if(commandName == "parse.list")                    {       command = new ParseListCommand(optionString);                           }
295                 else if(commandName == "parse.sff")                             {       command = new ParseSFFCommand(optionString);                            }
296                 else if(commandName == "phylo.diversity")               {       command = new PhyloDiversityCommand(optionString);                      }
297                 else if(commandName == "make.group")                    {       command = new MakeGroupCommand(optionString);                           }
298                 else if(commandName == "chop.seqs")                             {       command = new ChopSeqsCommand(optionString);                            }
299                 else if(commandName == "clearcut")                              {       command = new ClearcutCommand(optionString);                            }
300                 else if(commandName == "catchall")                              {       command = new CatchAllCommand(optionString);                            }
301                 else if(commandName == "split.abund")                   {       command = new SplitAbundCommand(optionString);                          }
302                 else if(commandName == "cluster.split")                 {       command = new ClusterSplitCommand(optionString);                        }
303                 else if(commandName == "classify.otu")                  {       command = new ClassifyOtuCommand(optionString);                         }
304                 else if(commandName == "degap.seqs")                    {       command = new DegapSeqsCommand(optionString);                           }
305                 else if(commandName == "get.relabund")                  {       command = new GetRelAbundCommand(optionString);                         }
306                 else if(commandName == "sens.spec")                             {       command = new SensSpecCommand(optionString);                            }
307                 else if(commandName == "seq.error")                             {       command = new SeqErrorCommand(optionString);                            }
308                 else if(commandName == "sffinfo")                               {       command = new SffInfoCommand(optionString);                                     }
309                 else                                                                                    {       command = new NoCommand(optionString);                                          }
310
311                 return command;
312         }
313         catch(exception& e) {
314                 m->errorOut(e, "CommandFactory", "getCommand");
315                 exit(1);
316         }
317 }
318 /***********************************************************/
319 //This function is used to interrupt a command
320 Command* CommandFactory::getCommand(){
321         try {
322                 delete command;   //delete the old command
323
324                 string s = "";
325             command = new NoCommand(s);
326         
327                 return command;
328         }
329         catch(exception& e) {
330                 m->errorOut(e, "CommandFactory", "getCommand");
331                 exit(1);
332         }
333 }
334 /***********************************************************************/
335 bool CommandFactory::isValidCommand(string command) {
336         try {   
337         
338                 //is the command in the map
339                 if ((commands.find(command)) != (commands.end())) {
340                         return true;
341                 }else{
342                         m->mothurOut(command + " is not a valid command in Mothur.  Valid commands are ");
343                         for (it = commands.begin(); it != commands.end(); it++) {
344                                 m->mothurOut(it->first + ", ");
345                         }
346                         m->mothurOutEndLine();
347                         return false;
348                 }
349                 
350         }
351         catch(exception& e) {
352                 m->errorOut(e, "CommandFactory", "isValidCommand");
353                 exit(1);
354         }
355 }
356
357 /***********************************************************************/
358 void CommandFactory::printCommands(ostream& out) {
359         try {   
360                 out << "Valid commands are: ";
361                 for (it = commands.begin(); it != commands.end(); it++) {
362                         out << it->first << ",";
363                 }
364                 out << endl;
365         }
366         catch(exception& e) {
367                 m->errorOut(e, "CommandFactory", "printCommands");
368                 exit(1);
369         }
370 }
371 /***********************************************************************/
372
373
374
375