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); parameters.push_back(paccnos);
15 CommandParameter pconstaxonomy("constaxonomy", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pconstaxonomy);
16 CommandParameter potucorr("otucorr", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(potucorr);
17 CommandParameter pcorraxes("corraxes", "InputTypes", "", "", "none", "FNGLT", "none",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 RemoveOtuLabelsCommand::RemoveOtuLabelsCommand(){
51 abort = true; calledHelp = true;
53 vector<string> tempOutNames;
54 outputTypes["contaxonomy"] = tempOutNames;
55 outputTypes["otu.corr"] = tempOutNames;
56 outputTypes["corr.axes"] = tempOutNames;
59 m->errorOut(e, "RemoveOtuLabelsCommand", "RemoveOtuLabelsCommand");
63 //**********************************************************************************************************************
64 RemoveOtuLabelsCommand::RemoveOtuLabelsCommand(string option) {
66 abort = false; calledHelp = false;
68 //allow user to run help
69 if(option == "help") { help(); abort = true; calledHelp = true; }
70 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
73 //valid paramters for this command
74 vector<string> myArray = setParameters();
76 OptionParser parser(option);
77 map<string,string> parameters = parser.getParameters();
79 ValidParameters validParameter;
80 map<string,string>::iterator it;
81 //check to make sure all parameters are valid for command
82 for (it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
87 //if the user changes the input directory command factory will send this info to us in the output parameter
88 string inputDir = validParameter.validFile(parameters, "inputdir", false);
89 if (inputDir == "not found"){ inputDir = ""; }
92 //edit file types below to include only the types you added as parameters
95 it = parameters.find("constaxonomy");
96 //user has given a template file
97 if(it != parameters.end()){
98 path = m->hasPath(it->second);
99 //if the user has not given a path then, add inputdir. else leave path alone.
100 if (path == "") { parameters["constaxonomy"] = inputDir + it->second; }
103 it = parameters.find("accnos");
104 //user has given a template file
105 if(it != parameters.end()){
106 path = m->hasPath(it->second);
107 //if the user has not given a path then, add inputdir. else leave path alone.
108 if (path == "") { parameters["accnos"] = inputDir + it->second; }
111 it = parameters.find("corraxes");
112 //user has given a template file
113 if(it != parameters.end()){
114 path = m->hasPath(it->second);
115 //if the user has not given a path then, add inputdir. else leave path alone.
116 if (path == "") { parameters["corraxes"] = inputDir + it->second; }
119 it = parameters.find("otucorr");
120 //user has given a template file
121 if(it != parameters.end()){
122 path = m->hasPath(it->second);
123 //if the user has not given a path then, add inputdir. else leave path alone.
124 if (path == "") { parameters["otucorr"] = inputDir + it->second; }
128 vector<string> tempOutNames;
129 outputTypes["contaxonomy"] = tempOutNames;
130 outputTypes["otu.corr"] = tempOutNames;
131 outputTypes["corr.axes"] = tempOutNames;
133 //check for parameters
134 accnosfile = validParameter.validFile(parameters, "accnos", true);
135 if (accnosfile == "not open") { abort = true; }
136 else if (accnosfile == "not found") {
137 accnosfile = m->getAccnosFile();
138 if (accnosfile != "") { m->mothurOut("Using " + accnosfile + " as input file for the accnos parameter."); m->mothurOutEndLine(); }
140 m->mothurOut("You have no valid accnos file and accnos is required."); m->mothurOutEndLine();
143 }else { m->setAccnosFile(accnosfile); }
145 constaxonomyfile = validParameter.validFile(parameters, "constaxonomy", true);
146 if (constaxonomyfile == "not open") { constaxonomyfile = ""; abort = true; }
147 else if (constaxonomyfile == "not found") { constaxonomyfile = ""; }
149 corraxesfile = validParameter.validFile(parameters, "corraxes", true);
150 if (corraxesfile == "not open") { corraxesfile = ""; abort = true; }
151 else if (corraxesfile == "not found") { corraxesfile = ""; }
153 otucorrfile = validParameter.validFile(parameters, "otucorr", true);
154 if (otucorrfile == "not open") { otucorrfile = ""; abort = true; }
155 else if (otucorrfile == "not found") { otucorrfile = ""; }
158 //if the user changes the output directory command factory will send this info to us in the output parameter
159 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
161 if ((constaxonomyfile == "") && (corraxesfile == "") && (otucorrfile == "")) { m->mothurOut("You must provide one of the following: constaxonomy, corraxes or otucorr."); m->mothurOutEndLine(); abort = true; }
165 catch(exception& e) {
166 m->errorOut(e, "RemoveOtuLabelsCommand", "RemoveOtuLabelsCommand");
170 //**********************************************************************************************************************
172 int RemoveOtuLabelsCommand::execute(){
175 if (abort == true) { if (calledHelp) { return 0; } return 2; }
177 //get labels you want to keep
180 if (m->control_pressed) { return 0; }
182 //read through the correct file and output lines you want to keep
183 if (constaxonomyfile != "") { readClassifyOtu(); }
184 if (corraxesfile != "") { readCorrAxes(); }
185 if (otucorrfile != "") { readOtuAssociation(); }
187 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
189 //output files created by command
190 m->mothurOutEndLine();
191 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
192 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
193 m->mothurOutEndLine();
197 catch(exception& e) {
198 m->errorOut(e, "GetOtuLabelsCommand", "execute");
202 //**********************************************************************************************************************
203 int RemoveOtuLabelsCommand::readClassifyOtu(){
205 string thisOutputDir = outputDir;
206 if (outputDir == "") { thisOutputDir += m->hasPath(constaxonomyfile); }
207 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(constaxonomyfile)) + "pick.taxonomy";
209 m->openOutputFile(outputFileName, out);
212 m->openInputFile(constaxonomyfile, in);
214 bool wroteSomething = false;
215 int removedCount = 0;
218 string headers = m->getline(in);
219 out << headers << endl;
223 if (m->control_pressed) { break; }
225 string otu = ""; string tax = "unknown";
228 in >> otu >> size >> tax; m->gobble(in);
230 if (labels.count(otu) == 0) {
231 wroteSomething = true;
232 out << otu << '\t' << size << '\t' << tax << endl;
233 }else { removedCount++; }
238 if (wroteSomething == false) { m->mothurOut("Your file only contains labels from the .accnos file."); m->mothurOutEndLine(); }
239 outputNames.push_back(outputFileName); outputTypes["constaxonomy"].push_back(outputFileName);
241 m->mothurOut("Removed " + toString(removedCount) + " otus from your constaxonomy file."); m->mothurOutEndLine();
246 catch(exception& e) {
247 m->errorOut(e, "RemoveOtuLabelsCommand", "readClassifyOtu");
251 //**********************************************************************************************************************
252 int RemoveOtuLabelsCommand::readOtuAssociation(){
254 string thisOutputDir = outputDir;
255 if (outputDir == "") { thisOutputDir += m->hasPath(otucorrfile); }
256 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(otucorrfile)) + "pick.corr";
258 m->openOutputFile(outputFileName, out);
261 m->openInputFile(otucorrfile, in);
263 bool wroteSomething = false;
264 int removedCount = 0;
267 string headers = m->getline(in);
268 out << headers << endl;
272 if (m->control_pressed) { break; }
277 string line = m->getline(in); m->gobble(in);
279 if ((labels.count(otu1) == 0) && (labels.count(otu2) == 0)){
280 wroteSomething = true;
282 out << otu1 << '\t' << otu2 << '\t' << line << endl;
283 }else { removedCount++; }
288 if (wroteSomething == false) { m->mothurOut("Your file only contains labels from the .accnos file."); m->mothurOutEndLine(); }
289 outputNames.push_back(outputFileName); outputTypes["otu.corr"].push_back(outputFileName);
291 m->mothurOut("Removed " + toString(removedCount) + " lines from your otu.corr file."); m->mothurOutEndLine();
296 catch(exception& e) {
297 m->errorOut(e, "RemoveOtuLabelsCommand", "readOtuAssociation");
301 //**********************************************************************************************************************
302 int RemoveOtuLabelsCommand::readCorrAxes(){
304 string thisOutputDir = outputDir;
305 if (outputDir == "") { thisOutputDir += m->hasPath(corraxesfile); }
306 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(corraxesfile)) + "pick.axes";
308 m->openOutputFile(outputFileName, out);
312 m->openInputFile(corraxesfile, in);
314 bool wroteSomething = false;
315 int removedCount = 0;
318 string headers = m->getline(in);
319 out << headers << endl;
323 if (m->control_pressed) { break; }
327 string line = m->getline(in); m->gobble(in);
329 if (labels.count(otu) == 0) {
330 wroteSomething = true;
332 out << otu << '\t' << line << endl;
333 }else { removedCount++; }
338 if (wroteSomething == false) { m->mothurOut("Your file only contains labels from the .accnos file."); m->mothurOutEndLine(); }
339 outputNames.push_back(outputFileName); outputTypes["corr.axes"].push_back(outputFileName);
341 m->mothurOut("Removed " + toString(removedCount) + " lines from your corr.axes file."); m->mothurOutEndLine();
346 catch(exception& e) {
347 m->errorOut(e, "RemoveOtuLabelsCommand", "readCorrAxes");
352 //**********************************************************************************************************************
353 int RemoveOtuLabelsCommand::readAccnos(){
357 m->openInputFile(accnosfile, in);
372 catch(exception& e) {
373 m->errorOut(e, "RemoveOtuLabelsCommand", "readAccnos");
377 //**********************************************************************************************************************