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