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