5 // Created by Sarah Westcott on 5/21/12.
6 // Copyright (c) 2012 Schloss Lab. All rights reserved.
9 #include "removeotulabelscommand.h"
11 //**********************************************************************************************************************
12 vector<string> RemoveOtuLabelsCommand::setParameters(){
14 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none","",false,true,true); parameters.push_back(paccnos);
15 CommandParameter pconstaxonomy("constaxonomy", "InputTypes", "", "", "none", "FNGLT", "none","constaxonomy",false,false); parameters.push_back(pconstaxonomy);
16 CommandParameter potucorr("otucorr", "InputTypes", "", "", "none", "FNGLT", "none","otucorr",false,false); parameters.push_back(potucorr);
17 CommandParameter pcorraxes("corraxes", "InputTypes", "", "", "none", "FNGLT", "none","corraxes",false,false); parameters.push_back(pcorraxes);
18 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
19 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
21 vector<string> myArray;
22 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
26 m->errorOut(e, "RemoveOtuLabelsCommand", "setParameters");
30 //**********************************************************************************************************************
31 string RemoveOtuLabelsCommand::getHelpString(){
33 string helpString = "";
34 helpString += "The remove.otulabels command can be used to remove specific otus with the output from classify.otu, otu.association, or corr.axes.\n";
35 helpString += "The remove.otulabels parameters are: constaxonomy, otucorr, corraxes, and accnos.\n";
36 helpString += "The constaxonomy parameter is input the results of the classify.otu command.\n";
37 helpString += "The otucorr parameter is input the results of the otu.association command.\n";
38 helpString += "The corraxes parameter is input the results of the corr.axes command.\n";
39 helpString += "The remove.otulabels commmand should be in the following format: \n";
40 helpString += "remove.otulabels(accnos=yourListOfOTULabels, corraxes=yourCorrAxesFile)\n";
44 m->errorOut(e, "RemoveOtuLabelsCommand", "getHelpString");
48 //**********************************************************************************************************************
49 string RemoveOtuLabelsCommand::getOutputPattern(string type) {
53 if (type == "constaxonomy") { pattern = "[filename],pick,[extension]"; }
54 else if (type == "otucorr") { pattern = "[filename],pick,[extension]"; }
55 else if (type == "corraxes") { pattern = "[filename],pick,[extension]"; }
56 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
61 m->errorOut(e, "RemoveOtuLabelsCommand", "getOutputPattern");
65 //**********************************************************************************************************************
66 RemoveOtuLabelsCommand::RemoveOtuLabelsCommand(){
68 abort = true; calledHelp = true;
70 vector<string> tempOutNames;
71 outputTypes["constaxonomy"] = tempOutNames;
72 outputTypes["otucorr"] = tempOutNames;
73 outputTypes["corraxes"] = tempOutNames;
76 m->errorOut(e, "RemoveOtuLabelsCommand", "RemoveOtuLabelsCommand");
80 //**********************************************************************************************************************
81 RemoveOtuLabelsCommand::RemoveOtuLabelsCommand(string option) {
83 abort = false; calledHelp = false;
85 //allow user to run help
86 if(option == "help") { help(); abort = true; calledHelp = true; }
87 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
90 //valid paramters for this command
91 vector<string> myArray = setParameters();
93 OptionParser parser(option);
94 map<string,string> parameters = parser.getParameters();
96 ValidParameters validParameter;
97 map<string,string>::iterator it;
98 //check to make sure all parameters are valid for command
99 for (it = parameters.begin(); it != parameters.end(); it++) {
100 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
104 //if the user changes the input directory command factory will send this info to us in the output parameter
105 string inputDir = validParameter.validFile(parameters, "inputdir", false);
106 if (inputDir == "not found"){ inputDir = ""; }
109 //edit file types below to include only the types you added as parameters
112 it = parameters.find("constaxonomy");
113 //user has given a template file
114 if(it != parameters.end()){
115 path = m->hasPath(it->second);
116 //if the user has not given a path then, add inputdir. else leave path alone.
117 if (path == "") { parameters["constaxonomy"] = inputDir + it->second; }
120 it = parameters.find("accnos");
121 //user has given a template file
122 if(it != parameters.end()){
123 path = m->hasPath(it->second);
124 //if the user has not given a path then, add inputdir. else leave path alone.
125 if (path == "") { parameters["accnos"] = inputDir + it->second; }
128 it = parameters.find("corraxes");
129 //user has given a template file
130 if(it != parameters.end()){
131 path = m->hasPath(it->second);
132 //if the user has not given a path then, add inputdir. else leave path alone.
133 if (path == "") { parameters["corraxes"] = inputDir + it->second; }
136 it = parameters.find("otucorr");
137 //user has given a template file
138 if(it != parameters.end()){
139 path = m->hasPath(it->second);
140 //if the user has not given a path then, add inputdir. else leave path alone.
141 if (path == "") { parameters["otucorr"] = inputDir + it->second; }
145 vector<string> tempOutNames;
146 outputTypes["constaxonomy"] = tempOutNames;
147 outputTypes["otucorr"] = tempOutNames;
148 outputTypes["corraxes"] = tempOutNames;
150 //check for parameters
151 accnosfile = validParameter.validFile(parameters, "accnos", true);
152 if (accnosfile == "not open") { abort = true; }
153 else if (accnosfile == "not found") {
154 accnosfile = m->getAccnosFile();
155 if (accnosfile != "") { m->mothurOut("Using " + accnosfile + " as input file for the accnos parameter."); m->mothurOutEndLine(); }
157 m->mothurOut("You have no valid accnos file and accnos is required."); m->mothurOutEndLine();
160 }else { m->setAccnosFile(accnosfile); }
162 constaxonomyfile = validParameter.validFile(parameters, "constaxonomy", true);
163 if (constaxonomyfile == "not open") { constaxonomyfile = ""; abort = true; }
164 else if (constaxonomyfile == "not found") { constaxonomyfile = ""; }
166 corraxesfile = validParameter.validFile(parameters, "corraxes", true);
167 if (corraxesfile == "not open") { corraxesfile = ""; abort = true; }
168 else if (corraxesfile == "not found") { corraxesfile = ""; }
170 otucorrfile = validParameter.validFile(parameters, "otucorr", true);
171 if (otucorrfile == "not open") { otucorrfile = ""; abort = true; }
172 else if (otucorrfile == "not found") { otucorrfile = ""; }
175 //if the user changes the output directory command factory will send this info to us in the output parameter
176 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
178 if ((constaxonomyfile == "") && (corraxesfile == "") && (otucorrfile == "")) { m->mothurOut("You must provide one of the following: constaxonomy, corraxes or otucorr."); m->mothurOutEndLine(); abort = true; }
182 catch(exception& e) {
183 m->errorOut(e, "RemoveOtuLabelsCommand", "RemoveOtuLabelsCommand");
187 //**********************************************************************************************************************
189 int RemoveOtuLabelsCommand::execute(){
192 if (abort == true) { if (calledHelp) { return 0; } return 2; }
194 //get labels you want to keep
195 labels = m->readAccnos(accnosfile);
197 if (m->control_pressed) { return 0; }
199 //read through the correct file and output lines you want to keep
200 if (constaxonomyfile != "") { readClassifyOtu(); }
201 if (corraxesfile != "") { readCorrAxes(); }
202 if (otucorrfile != "") { readOtuAssociation(); }
204 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
206 //output files created by command
207 m->mothurOutEndLine();
208 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
209 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
210 m->mothurOutEndLine();
214 catch(exception& e) {
215 m->errorOut(e, "GetOtuLabelsCommand", "execute");
219 //**********************************************************************************************************************
220 int RemoveOtuLabelsCommand::readClassifyOtu(){
222 string thisOutputDir = outputDir;
223 if (outputDir == "") { thisOutputDir += m->hasPath(constaxonomyfile); }
224 map<string, string> variables;
225 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(constaxonomyfile));
226 variables["[extension]"] = m->getExtension(constaxonomyfile);
227 string outputFileName = getOutputFileName("constaxonomy", variables);
229 m->openOutputFile(outputFileName, out);
232 m->openInputFile(constaxonomyfile, in);
234 bool wroteSomething = false;
235 int removedCount = 0;
238 string headers = m->getline(in);
239 out << headers << endl;
243 if (m->control_pressed) { break; }
245 string otu = ""; string tax = "unknown";
248 in >> otu >> size >> tax; m->gobble(in);
250 if (labels.count(otu) == 0) {
251 wroteSomething = true;
252 out << otu << '\t' << size << '\t' << tax << endl;
253 }else { removedCount++; }
258 if (wroteSomething == false) { m->mothurOut("Your file only contains labels from the .accnos file."); m->mothurOutEndLine(); }
259 outputNames.push_back(outputFileName); outputTypes["constaxonomy"].push_back(outputFileName);
261 m->mothurOut("Removed " + toString(removedCount) + " otus from your constaxonomy file."); m->mothurOutEndLine();
266 catch(exception& e) {
267 m->errorOut(e, "RemoveOtuLabelsCommand", "readClassifyOtu");
271 //**********************************************************************************************************************
272 int RemoveOtuLabelsCommand::readOtuAssociation(){
274 string thisOutputDir = outputDir;
275 if (outputDir == "") { thisOutputDir += m->hasPath(otucorrfile); }
276 map<string, string> variables;
277 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(otucorrfile));
278 variables["[extension]"] = m->getExtension(otucorrfile);
279 string outputFileName = getOutputFileName("otucorr", variables);
281 m->openOutputFile(outputFileName, out);
284 m->openInputFile(otucorrfile, in);
286 bool wroteSomething = false;
287 int removedCount = 0;
290 string headers = m->getline(in);
291 out << headers << endl;
295 if (m->control_pressed) { break; }
300 string line = m->getline(in); m->gobble(in);
302 if ((labels.count(otu1) == 0) && (labels.count(otu2) == 0)){
303 wroteSomething = true;
305 out << otu1 << '\t' << otu2 << '\t' << line << endl;
306 }else { removedCount++; }
311 if (wroteSomething == false) { m->mothurOut("Your file only contains labels from the .accnos file."); m->mothurOutEndLine(); }
312 outputNames.push_back(outputFileName); outputTypes["otucorr"].push_back(outputFileName);
314 m->mothurOut("Removed " + toString(removedCount) + " lines from your otu.corr file."); m->mothurOutEndLine();
319 catch(exception& e) {
320 m->errorOut(e, "RemoveOtuLabelsCommand", "readOtuAssociation");
324 //**********************************************************************************************************************
325 int RemoveOtuLabelsCommand::readCorrAxes(){
327 string thisOutputDir = outputDir;
328 if (outputDir == "") { thisOutputDir += m->hasPath(corraxesfile); }
329 map<string, string> variables;
330 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(corraxesfile));
331 variables["[extension]"] = m->getExtension(corraxesfile);
332 string outputFileName = getOutputFileName("corraxes", variables);
334 m->openOutputFile(outputFileName, out);
338 m->openInputFile(corraxesfile, in);
340 bool wroteSomething = false;
341 int removedCount = 0;
344 string headers = m->getline(in);
345 out << headers << endl;
349 if (m->control_pressed) { break; }
353 string line = m->getline(in); m->gobble(in);
355 if (labels.count(otu) == 0) {
356 wroteSomething = true;
358 out << otu << '\t' << line << endl;
359 }else { removedCount++; }
364 if (wroteSomething == false) { m->mothurOut("Your file only contains labels from the .accnos file."); m->mothurOutEndLine(); }
365 outputNames.push_back(outputFileName); outputTypes["corraxes"].push_back(outputFileName);
367 m->mothurOut("Removed " + toString(removedCount) + " lines from your corr.axes file."); m->mothurOutEndLine();
372 catch(exception& e) {
373 m->errorOut(e, "RemoveOtuLabelsCommand", "readCorrAxes");
377 //**********************************************************************************************************************