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