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