2 * summarytaxcommand.cpp
5 * Created by westcott on 9/23/11.
6 * Copyright 2011 Schloss Lab. All rights reserved.
10 #include "summarytaxcommand.h"
11 #include "phylosummary.h"
13 //**********************************************************************************************************************
14 vector<string> SummaryTaxCommand::setParameters(){
16 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(ptaxonomy);
17 CommandParameter pname("name", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
18 CommandParameter pgroup("group", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(pgroup);
19 CommandParameter preftaxonomy("reftaxonomy", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(preftaxonomy);
20 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
21 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
23 vector<string> myArray;
24 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
28 m->errorOut(e, "SummaryTaxCommand", "setParameters");
32 //**********************************************************************************************************************
33 string SummaryTaxCommand::getHelpString(){
35 string helpString = "";
36 helpString += "The summary.tax command reads a taxonomy file and an optional name file, and summarizes the taxonomy information.\n";
37 helpString += "The summary.tax command parameters are taxonomy, group and name. taxonomy is required, unless you have a valid current taxonomy file.\n";
38 helpString += "The name parameter allows you to enter a name file associated with your taxonomy file. \n";
39 helpString += "The group parameter allows you add a group file so you can have the summary totals broken up by group.\n";
40 helpString += "The reftaxonomy parameter allows you give the name of the reference taxonomy file used when you classified your sequences. It is not required, but providing it will keep the rankIDs in the summary file static.\n";
41 helpString += "The summary.tax command should be in the following format: \n";
42 helpString += "summary.tax(taxonomy=yourTaxonomyFile) \n";
43 helpString += "Note: No spaces between parameter labels (i.e. taxonomy), '=' and parameters (i.e.yourTaxonomyFile).\n";
47 m->errorOut(e, "SummaryTaxCommand", "getHelpString");
51 //**********************************************************************************************************************
52 string SummaryTaxCommand::getOutputFileNameTag(string type, string inputName=""){
54 string outputFileName = "";
55 map<string, vector<string> >::iterator it;
57 //is this a type this command creates
58 it = outputTypes.find(type);
59 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
61 if (type == "summary") { outputFileName = "tax.summary"; }
62 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
64 return outputFileName;
67 m->errorOut(e, "SummaryTaxCommand", "getOutputFileNameTag");
71 //**********************************************************************************************************************
72 SummaryTaxCommand::SummaryTaxCommand(){
74 abort = true; calledHelp = true;
76 vector<string> tempOutNames;
77 outputTypes["summary"] = tempOutNames;
80 m->errorOut(e, "SummaryTaxCommand", "SummaryTaxCommand");
84 //***************************************************************************************************************
86 SummaryTaxCommand::SummaryTaxCommand(string option) {
88 abort = false; calledHelp = false;
90 //allow user to run help
91 if(option == "help") { help(); abort = true; calledHelp = true; }
92 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
95 vector<string> myArray = setParameters();
97 OptionParser parser(option);
98 map<string,string> parameters = parser.getParameters();
100 ValidParameters validParameter;
101 map<string,string>::iterator it;
103 //check to make sure all parameters are valid for command
104 for (it = parameters.begin(); it != parameters.end(); it++) {
105 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
108 //if the user changes the input directory command factory will send this info to us in the output parameter
109 string inputDir = validParameter.validFile(parameters, "inputdir", false);
110 if (inputDir == "not found"){ inputDir = ""; }
113 it = parameters.find("taxonomy");
114 //user has given a template file
115 if(it != parameters.end()){
116 path = m->hasPath(it->second);
117 //if the user has not given a path then, add inputdir. else leave path alone.
118 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
121 it = parameters.find("name");
122 //user has given a template file
123 if(it != parameters.end()){
124 path = m->hasPath(it->second);
125 //if the user has not given a path then, add inputdir. else leave path alone.
126 if (path == "") { parameters["name"] = inputDir + it->second; }
129 it = parameters.find("group");
130 //user has given a template file
131 if(it != parameters.end()){
132 path = m->hasPath(it->second);
133 //if the user has not given a path then, add inputdir. else leave path alone.
134 if (path == "") { parameters["group"] = inputDir + it->second; }
137 it = parameters.find("reftaxonomy");
138 //user has given a template file
139 if(it != parameters.end()){
140 path = m->hasPath(it->second);
141 //if the user has not given a path then, add inputdir. else leave path alone.
142 if (path == "") { parameters["reftaxonomy"] = inputDir + it->second; }
147 //initialize outputTypes
148 vector<string> tempOutNames;
149 outputTypes["summary"] = tempOutNames;
151 //check for required parameters
152 taxfile = validParameter.validFile(parameters, "taxonomy", true);
153 if (taxfile == "not open") { abort = true; }
154 else if (taxfile == "not found") {
155 taxfile = m->getTaxonomyFile();
156 if (taxfile != "") { m->mothurOut("Using " + taxfile + " as input file for the taxonomy parameter."); m->mothurOutEndLine(); }
157 else { m->mothurOut("You have no current taxonomy file and the taxonomy parameter is required."); m->mothurOutEndLine(); abort = true; }
158 }else { m->setTaxonomyFile(taxfile); }
160 namefile = validParameter.validFile(parameters, "name", true);
161 if (namefile == "not open") { namefile = ""; abort = true; }
162 else if (namefile == "not found") { namefile = ""; }
163 else { m->setNameFile(namefile); }
165 groupfile = validParameter.validFile(parameters, "group", true);
166 if (groupfile == "not open") { groupfile = ""; abort = true; }
167 else if (groupfile == "not found") { groupfile = ""; }
168 else { m->setGroupFile(groupfile); }
170 refTaxonomy = validParameter.validFile(parameters, "reftaxonomy", true);
171 if (refTaxonomy == "not found") { refTaxonomy = ""; m->mothurOut("reftaxonomy is not required, but if given will keep the rankIDs in the summary file static."); m->mothurOutEndLine(); }
172 else if (refTaxonomy == "not open") { refTaxonomy = ""; abort = true; }
174 //if the user changes the output directory command factory will send this info to us in the output parameter
175 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
177 outputDir += m->hasPath(taxfile); //if user entered a file with a path then preserve it
180 if (namefile == "") {
181 vector<string> files; files.push_back(taxfile);
182 parser.getNameFile(files);
187 catch(exception& e) {
188 m->errorOut(e, "SummaryTaxCommand", "SummaryTaxCommand");
192 //***************************************************************************************************************
194 int SummaryTaxCommand::execute(){
197 if (abort == true) { if (calledHelp) { return 0; } return 2; }
198 int start = time(NULL);
200 PhyloSummary* taxaSum;
201 if (refTaxonomy != "") {
202 taxaSum = new PhyloSummary(refTaxonomy, groupfile);
204 taxaSum = new PhyloSummary(groupfile);
207 if (m->control_pressed) { delete taxaSum; return 0; }
210 if (namefile == "") { numSeqs = taxaSum->summarize(taxfile); }
212 map<string, vector<string> > nameMap;
213 map<string, vector<string> >::iterator itNames;
214 m->readNames(namefile, nameMap);
216 if (m->control_pressed) { delete taxaSum; return 0; }
219 m->openInputFile(taxfile, in);
221 //read in users taxonomy file and add sequences to tree
225 in >> name >> taxon; m->gobble(in);
227 itNames = nameMap.find(name);
229 if (itNames == nameMap.end()) {
230 m->mothurOut("[ERROR]: " + name + " is not in your name file please correct."); m->mothurOutEndLine(); exit(1);
232 for (int i = 0; i < itNames->second.size(); i++) {
234 taxaSum->addSeqToTree(itNames->second[i], taxon); //add it as many times as there are identical seqs
236 itNames->second.clear();
237 nameMap.erase(itNames->first);
243 if (m->control_pressed) { delete taxaSum; return 0; }
247 string summaryFile = outputDir + m->getRootName(m->getSimpleName(taxfile)) + getOutputFileNameTag("summary");
248 m->openOutputFile(summaryFile, outTaxTree);
249 taxaSum->print(outTaxTree);
254 if (m->control_pressed) { m->mothurRemove(summaryFile); return 0; }
256 m->mothurOutEndLine();
257 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to create the summary file for " + toString(numSeqs) + " sequences."); m->mothurOutEndLine(); m->mothurOutEndLine();
258 m->mothurOutEndLine();
259 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
260 m->mothurOut(summaryFile); m->mothurOutEndLine(); outputNames.push_back(summaryFile); outputTypes["summary"].push_back(summaryFile);
261 m->mothurOutEndLine();
265 catch(exception& e) {
266 m->errorOut(e, "SummaryTaxCommand", "execute");
270 /**************************************************************************************/