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