2 // getotulabelscommand.cpp
5 // Created by Sarah Westcott on 5/21/12.
6 // Copyright (c) 2012 Schloss Lab. All rights reserved.
9 #include "getotulabelscommand.h"
11 //**********************************************************************************************************************
12 vector<string> GetOtuLabelsCommand::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, "GetOtuLabelsCommand", "setParameters");
30 //**********************************************************************************************************************
31 string GetOtuLabelsCommand::getHelpString(){
33 string helpString = "";
34 helpString += "The get.otulabels command can be used to select specific otus with the output from classify.otu, otu.association, or corr.axes.\n";
35 helpString += "The get.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 get.otulabels commmand should be in the following format: \n";
40 helpString += "get.otulabels(accnos=yourListOfOTULabels, corraxes=yourCorrAxesFile)\n";
44 m->errorOut(e, "GetOtuLabelsCommand", "getHelpString");
48 //**********************************************************************************************************************
49 GetOtuLabelsCommand::GetOtuLabelsCommand(){
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, "GetOtuLabelsCommand", "GetOtuLabelsCommand");
63 //**********************************************************************************************************************
64 GetOtuLabelsCommand::GetOtuLabelsCommand(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, "GetOtuLabelsCommand", "GetOtuLabelsCommand");
170 //**********************************************************************************************************************
172 int GetOtuLabelsCommand::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 GetOtuLabelsCommand::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 selectedCount = 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;
234 out << otu << '\t' << size << '\t' << tax << endl;
240 if (wroteSomething == false) { m->mothurOut("Your file does not contain any labels from the .accnos file."); m->mothurOutEndLine(); }
241 outputNames.push_back(outputFileName); outputTypes["constaxonomy"].push_back(outputFileName);
243 m->mothurOut("Selected " + toString(selectedCount) + " otus from your constaxonomy file."); m->mothurOutEndLine();
248 catch(exception& e) {
249 m->errorOut(e, "GetOtuLabelsCommand", "readClassifyOtu");
253 //**********************************************************************************************************************
254 int GetOtuLabelsCommand::readOtuAssociation(){
256 string thisOutputDir = outputDir;
257 if (outputDir == "") { thisOutputDir += m->hasPath(otucorrfile); }
258 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(otucorrfile)) + "pick.corr";
260 m->openOutputFile(outputFileName, out);
263 m->openInputFile(otucorrfile, in);
265 bool wroteSomething = false;
266 int selectedCount = 0;
269 string headers = m->getline(in);
270 out << headers << endl;
274 if (m->control_pressed) { break; }
279 string line = m->getline(in); m->gobble(in);
281 if ((labels.count(otu1) != 0) && (labels.count(otu2) != 0)){
282 wroteSomething = true;
285 out << otu1 << '\t' << otu2 << '\t' << line << endl;
291 if (wroteSomething == false) { m->mothurOut("Your file does not contain any labels from the .accnos file."); m->mothurOutEndLine(); }
292 outputNames.push_back(outputFileName); outputTypes["otu.corr"].push_back(outputFileName);
294 m->mothurOut("Selected " + toString(selectedCount) + " lines from your otu.corr file."); m->mothurOutEndLine();
299 catch(exception& e) {
300 m->errorOut(e, "GetOtuLabelsCommand", "readOtuAssociation");
304 //**********************************************************************************************************************
305 int GetOtuLabelsCommand::readCorrAxes(){
307 string thisOutputDir = outputDir;
308 if (outputDir == "") { thisOutputDir += m->hasPath(corraxesfile); }
309 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(corraxesfile)) + "pick.axes";
311 m->openOutputFile(outputFileName, out);
315 m->openInputFile(corraxesfile, in);
317 bool wroteSomething = false;
318 int selectedCount = 0;
321 string headers = m->getline(in);
322 out << headers << endl;
326 if (m->control_pressed) { break; }
330 string line = m->getline(in); m->gobble(in);
332 if (labels.count(otu) != 0) {
333 wroteSomething = true;
336 out << otu << '\t' << line << endl;
342 if (wroteSomething == false) { m->mothurOut("Your file does not contain any labels from the .accnos file."); m->mothurOutEndLine(); }
343 outputNames.push_back(outputFileName); outputTypes["corr.axes"].push_back(outputFileName);
345 m->mothurOut("Selected " + toString(selectedCount) + " lines from your corr.axes file."); m->mothurOutEndLine();
350 catch(exception& e) {
351 m->errorOut(e, "GetOtuLabelsCommand", "readCorrAxes");
356 //**********************************************************************************************************************
357 int GetOtuLabelsCommand::readAccnos(){
361 m->openInputFile(accnosfile, in);
376 catch(exception& e) {
377 m->errorOut(e, "GetOtuLabelsCommand", "readAccnos");
381 //**********************************************************************************************************************