2 * removelineagecommand.cpp
5 * Created by westcott on 9/24/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "removelineagecommand.h"
11 #include "sequence.hpp"
12 #include "listvector.hpp"
14 //**********************************************************************************************************************
15 vector<string> RemoveLineageCommand::getValidParameters(){
17 string Array[] = {"fasta","name", "group", "alignreport", "taxon", "dups", "list","taxonomy","outputdir","inputdir"};
18 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
22 m->errorOut(e, "RemoveLineageCommand", "getValidParameters");
26 //**********************************************************************************************************************
27 RemoveLineageCommand::RemoveLineageCommand(){
30 //initialize outputTypes
31 vector<string> tempOutNames;
32 outputTypes["fasta"] = tempOutNames;
33 outputTypes["taxonomy"] = tempOutNames;
34 outputTypes["name"] = tempOutNames;
35 outputTypes["group"] = tempOutNames;
36 outputTypes["alignreport"] = tempOutNames;
37 outputTypes["list"] = tempOutNames;
40 m->errorOut(e, "RemoveLineageCommand", "RemoveLineageCommand");
44 //**********************************************************************************************************************
45 vector<string> RemoveLineageCommand::getRequiredParameters(){
47 string Array[] = {"taxonomy"};
48 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
52 m->errorOut(e, "RemoveLineageCommand", "getRequiredParameters");
56 //**********************************************************************************************************************
57 vector<string> RemoveLineageCommand::getRequiredFiles(){
59 vector<string> myArray;
63 m->errorOut(e, "RemoveLineageCommand", "getRequiredFiles");
67 //**********************************************************************************************************************
68 RemoveLineageCommand::RemoveLineageCommand(string option) {
72 //allow user to run help
73 if(option == "help") { help(); abort = true; }
76 //valid paramters for this command
77 string Array[] = {"fasta","name", "group", "alignreport", "taxon", "dups", "list","taxonomy","outputdir","inputdir"};
78 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
80 OptionParser parser(option);
81 map<string,string> parameters = parser.getParameters();
83 ValidParameters validParameter;
84 map<string,string>::iterator it;
86 //check to make sure all parameters are valid for command
87 for (it = parameters.begin(); it != parameters.end(); it++) {
88 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
91 //initialize outputTypes
92 vector<string> tempOutNames;
93 outputTypes["fasta"] = tempOutNames;
94 outputTypes["taxonomy"] = tempOutNames;
95 outputTypes["name"] = tempOutNames;
96 outputTypes["group"] = tempOutNames;
97 outputTypes["alignreport"] = tempOutNames;
98 outputTypes["list"] = tempOutNames;
100 //if the user changes the output directory command factory will send this info to us in the output parameter
101 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
103 //if the user changes the input directory command factory will send this info to us in the output parameter
104 string inputDir = validParameter.validFile(parameters, "inputdir", false);
105 if (inputDir == "not found"){ inputDir = ""; }
108 it = parameters.find("alignreport");
109 //user has given a template file
110 if(it != parameters.end()){
111 path = m->hasPath(it->second);
112 //if the user has not given a path then, add inputdir. else leave path alone.
113 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
116 it = parameters.find("fasta");
117 //user has given a template file
118 if(it != parameters.end()){
119 path = m->hasPath(it->second);
120 //if the user has not given a path then, add inputdir. else leave path alone.
121 if (path == "") { parameters["fasta"] = inputDir + it->second; }
124 it = parameters.find("list");
125 //user has given a template file
126 if(it != parameters.end()){
127 path = m->hasPath(it->second);
128 //if the user has not given a path then, add inputdir. else leave path alone.
129 if (path == "") { parameters["list"] = inputDir + it->second; }
132 it = parameters.find("name");
133 //user has given a template file
134 if(it != parameters.end()){
135 path = m->hasPath(it->second);
136 //if the user has not given a path then, add inputdir. else leave path alone.
137 if (path == "") { parameters["name"] = inputDir + it->second; }
140 it = parameters.find("group");
141 //user has given a template file
142 if(it != parameters.end()){
143 path = m->hasPath(it->second);
144 //if the user has not given a path then, add inputdir. else leave path alone.
145 if (path == "") { parameters["group"] = inputDir + it->second; }
148 it = parameters.find("taxonomy");
149 //user has given a template file
150 if(it != parameters.end()){
151 path = m->hasPath(it->second);
152 //if the user has not given a path then, add inputdir. else leave path alone.
153 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
158 //check for required parameters
159 fastafile = validParameter.validFile(parameters, "fasta", true);
160 if (fastafile == "not open") { abort = true; }
161 else if (fastafile == "not found") { fastafile = ""; }
163 namefile = validParameter.validFile(parameters, "name", true);
164 if (namefile == "not open") { abort = true; }
165 else if (namefile == "not found") { namefile = ""; }
167 groupfile = validParameter.validFile(parameters, "group", true);
168 if (groupfile == "not open") { abort = true; }
169 else if (groupfile == "not found") { groupfile = ""; }
171 alignfile = validParameter.validFile(parameters, "alignreport", true);
172 if (alignfile == "not open") { abort = true; }
173 else if (alignfile == "not found") { alignfile = ""; }
175 listfile = validParameter.validFile(parameters, "list", true);
176 if (listfile == "not open") { abort = true; }
177 else if (listfile == "not found") { listfile = ""; }
179 taxfile = validParameter.validFile(parameters, "taxonomy", true);
180 if (taxfile == "not open") { abort = true; }
181 else if (taxfile == "not found") { taxfile = ""; m->mothurOut("The taxonomy parameter is required for the get.lineage command."); m->mothurOutEndLine(); abort = true; }
183 string usedDups = "true";
184 string temp = validParameter.validFile(parameters, "dups", false); if (temp == "not found") { temp = "false"; usedDups = ""; }
185 dups = m->isTrue(temp);
187 taxons = validParameter.validFile(parameters, "taxon", false);
188 if (taxons == "not found") { taxons = ""; m->mothurOut("No taxons given, please correct."); m->mothurOutEndLine(); abort = true; }
191 if (taxons[0] == '\'') { taxons = taxons.substr(1); }
192 if (taxons[(taxons.length()-1)] == '\'') { taxons = taxons.substr(0, (taxons.length()-1)); }
196 if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == "") && (taxfile == "")) { m->mothurOut("You must provide one of the following: fasta, name, group, alignreport, taxonomy or listfile."); m->mothurOutEndLine(); abort = true; }
198 if ((usedDups != "") && (namefile == "")) { m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine(); abort = true; }
203 catch(exception& e) {
204 m->errorOut(e, "RemoveLineageCommand", "RemoveLineageCommand");
208 //**********************************************************************************************************************
210 void RemoveLineageCommand::help(){
212 m->mothurOut("The remove.lineage command reads a taxonomy file and any of the following file types: fasta, name, group, list or alignreport file.\n");
213 m->mothurOut("It outputs a file containing only the sequences from the taxonomy file that are not from the taxon you requested to be removed.\n");
214 m->mothurOut("The remove.lineage command parameters are taxon, fasta, name, group, list, taxonomy, alignreport and dups. You must provide taxonomy and taxon.\n");
215 m->mothurOut("The dups parameter allows you to add the entire line from a name file if you add any name from the line. default=false. \n");
216 m->mothurOut("The taxon parameter allows you to select the taxons you would like to remove.\n");
217 m->mothurOut("The remove.lineage command should be in the following format: remove.lineage(taxonomy=yourTaxonomyFile, taxon=yourTaxons).\n");
218 m->mothurOut("Example remove.lineage(taxonomy=amazon.silva.taxonomy, taxon=Bacteria;Firmicutes;Bacilli;Lactobacillales;).\n");
219 m->mothurOut("Note: If you are running mothur in script mode you must wrap the taxon in ' characters so mothur will ignore the ; in the taxon.\n");
220 m->mothurOut("Example remove.lineage(taxonomy=amazon.silva.taxonomy, taxon='Bacteria;Firmicutes;Bacilli;Lactobacillales;').\n");
221 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
223 catch(exception& e) {
224 m->errorOut(e, "RemoveLineageCommand", "help");
229 //**********************************************************************************************************************
231 int RemoveLineageCommand::execute(){
234 if (abort == true) { return 0; }
236 if (m->control_pressed) { return 0; }
238 //read through the correct file and output lines you want to keep
239 if (taxfile != "") { readTax(); } //fills the set of names to remove
240 if (namefile != "") { readName(); }
241 if (fastafile != "") { readFasta(); }
242 if (groupfile != "") { readGroup(); }
243 if (alignfile != "") { readAlign(); }
244 if (listfile != "") { readList(); }
247 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
249 if (outputNames.size() != 0) {
250 m->mothurOutEndLine();
251 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
252 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
253 m->mothurOutEndLine();
259 catch(exception& e) {
260 m->errorOut(e, "RemoveLineageCommand", "execute");
265 //**********************************************************************************************************************
266 int RemoveLineageCommand::readFasta(){
268 string thisOutputDir = outputDir;
269 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
270 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
273 m->openOutputFile(outputFileName, out);
276 m->openInputFile(fastafile, in);
279 bool wroteSomething = false;
282 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
284 Sequence currSeq(in);
285 name = currSeq.getName();
288 //if this name is in the accnos file
289 if (names.count(name) == 0) {
290 wroteSomething = true;
292 currSeq.printSequence(out);
300 if (wroteSomething == false) { m->mothurOut("Your fasta file contains only sequences from " + taxons + "."); m->mothurOutEndLine(); }
301 outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
306 catch(exception& e) {
307 m->errorOut(e, "RemoveLineageCommand", "readFasta");
311 //**********************************************************************************************************************
312 int RemoveLineageCommand::readList(){
314 string thisOutputDir = outputDir;
315 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
316 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
319 m->openOutputFile(outputFileName, out);
322 m->openInputFile(listfile, in);
324 bool wroteSomething = false;
327 //read in list vector
330 //make a new list vector
332 newList.setLabel(list.getLabel());
335 for (int i = 0; i < list.getNumBins(); i++) {
336 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
338 //parse out names that are in accnos file
339 string binnames = list.get(i);
341 string newNames = "";
342 while (binnames.find_first_of(',') != -1) {
343 string name = binnames.substr(0,binnames.find_first_of(','));
344 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
346 //if that name is in the .accnos file, add it
347 if (names.count(name) == 0) { newNames += name + ","; }
351 if (names.count(binnames) == 0) { newNames += binnames + ","; }
353 //if there are names in this bin add to new list
354 if (newNames != "") {
355 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
356 newList.push_back(newNames);
360 //print new listvector
361 if (newList.getNumBins() != 0) {
362 wroteSomething = true;
371 if (wroteSomething == false) { m->mothurOut("Your list file contains only sequences from " + taxons + "."); m->mothurOutEndLine(); }
372 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
377 catch(exception& e) {
378 m->errorOut(e, "RemoveLineageCommand", "readList");
382 //**********************************************************************************************************************
383 int RemoveLineageCommand::readName(){
385 string thisOutputDir = outputDir;
386 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
387 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
390 m->openOutputFile(outputFileName, out);
393 m->openInputFile(namefile, in);
394 string name, firstCol, secondCol;
396 bool wroteSomething = false;
399 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
404 vector<string> parsedNames;
405 //parse second column saving each name
406 while (secondCol.find_first_of(',') != -1) {
407 name = secondCol.substr(0,secondCol.find_first_of(','));
408 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
409 parsedNames.push_back(name);
412 //get name after last ,
413 parsedNames.push_back(secondCol);
415 vector<string> validSecond; validSecond.clear();
416 for (int i = 0; i < parsedNames.size(); i++) {
417 if (names.count(parsedNames[i]) == 0) {
418 validSecond.push_back(parsedNames[i]);
422 if ((dups) && (validSecond.size() != parsedNames.size())) { //if dups is true and we want to get rid of anyone, get rid of everyone
423 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
425 //if the name in the first column is in the set then print it and any other names in second column also in set
426 if (names.count(firstCol) == 0) {
428 wroteSomething = true;
430 out << firstCol << '\t';
432 //you know you have at least one valid second since first column is valid
433 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
434 out << validSecond[validSecond.size()-1] << endl;
436 //make first name in set you come to first column and then add the remaining names to second column
439 //you want part of this row
440 if (validSecond.size() != 0) {
442 wroteSomething = true;
444 out << validSecond[0] << '\t';
446 //you know you have at least one valid second since first column is valid
447 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
448 out << validSecond[validSecond.size()-1] << endl;
457 if (wroteSomething == false) { m->mothurOut("Your name file contains only sequences from " + taxons + "."); m->mothurOutEndLine(); }
458 outputNames.push_back(outputFileName); outputTypes["name"].push_back(outputFileName);
462 catch(exception& e) {
463 m->errorOut(e, "RemoveLineageCommand", "readName");
468 //**********************************************************************************************************************
469 int RemoveLineageCommand::readGroup(){
471 string thisOutputDir = outputDir;
472 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
473 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
476 m->openOutputFile(outputFileName, out);
479 m->openInputFile(groupfile, in);
482 bool wroteSomething = false;
485 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
487 in >> name; //read from first column
488 in >> group; //read from second column
490 //if this name is in the accnos file
491 if (names.count(name) == 0) {
492 wroteSomething = true;
493 out << name << '\t' << group << endl;
501 if (wroteSomething == false) { m->mothurOut("Your group file contains only sequences from " + taxons + "."); m->mothurOutEndLine(); }
502 outputNames.push_back(outputFileName); outputTypes["group"].push_back(outputFileName);
506 catch(exception& e) {
507 m->errorOut(e, "RemoveLineageCommand", "readGroup");
511 //**********************************************************************************************************************
512 int RemoveLineageCommand::readTax(){
514 string thisOutputDir = outputDir;
515 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
516 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
518 m->openOutputFile(outputFileName, out);
521 m->openInputFile(taxfile, in);
524 bool wroteSomething = false;
528 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
530 in >> name; //read from first column
531 in >> tax; //read from second column
535 //if the users file contains confidence scores we want to ignore them when searching for the taxons
536 int hasConfidences = tax.find_first_of('(');
537 if (hasConfidences != string::npos) {
538 newtax = removeConfidences(tax);
541 int pos = newtax.find(taxons);
543 if (pos == string::npos) {
544 wroteSomething = true;
545 out << name << '\t' << tax << endl;
546 }else{ //this sequence contains the taxon the user wants to remove
555 if (!wroteSomething) { m->mothurOut("Your taxonomy file contains only sequences from " + taxons + "."); m->mothurOutEndLine(); }
556 outputNames.push_back(outputFileName); outputTypes["taxonomy"].push_back(outputFileName);
561 catch(exception& e) {
562 m->errorOut(e, "RemoveLineageCommand", "readTax");
566 /**************************************************************************************************/
567 string RemoveLineageCommand::removeConfidences(string tax) {
571 int taxLength = tax.length();
572 for(int i=0;i<taxLength;i++){
574 taxon = taxon.substr(0, taxon.find_first_of('(')); //rip off confidence
584 catch(exception& e) {
585 m->errorOut(e, "RemoveLineageCommand", "removeConfidences");
589 //**********************************************************************************************************************
590 //alignreport file has a column header line then all other lines contain 16 columns. we just want the first column since that contains the name
591 int RemoveLineageCommand::readAlign(){
593 string thisOutputDir = outputDir;
594 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
595 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
598 m->openOutputFile(outputFileName, out);
601 m->openInputFile(alignfile, in);
604 bool wroteSomething = false;
606 //read column headers
607 for (int i = 0; i < 16; i++) {
608 if (!in.eof()) { in >> junk; out << junk << '\t'; }
614 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
616 in >> name; //read from first column
618 //if this name is in the accnos file
619 if (names.count(name) == 0) {
620 wroteSomething = true;
625 for (int i = 0; i < 15; i++) {
626 if (!in.eof()) { in >> junk; out << junk << '\t'; }
631 }else {//still read just don't do anything with it
634 for (int i = 0; i < 15; i++) {
635 if (!in.eof()) { in >> junk; }
645 if (wroteSomething == false) { m->mothurOut("Your align file contains only sequences from " + taxons + "."); m->mothurOutEndLine(); }
646 outputNames.push_back(outputFileName); outputTypes["alignreport"].push_back(outputFileName);
651 catch(exception& e) {
652 m->errorOut(e, "RemoveLineageCommand", "readAlign");
656 //**********************************************************************************************************************