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(){
29 abort = true; calledHelp = true;
30 vector<string> tempOutNames;
31 outputTypes["fasta"] = tempOutNames;
32 outputTypes["taxonomy"] = tempOutNames;
33 outputTypes["name"] = tempOutNames;
34 outputTypes["group"] = tempOutNames;
35 outputTypes["alignreport"] = tempOutNames;
36 outputTypes["list"] = tempOutNames;
39 m->errorOut(e, "RemoveLineageCommand", "RemoveLineageCommand");
43 //**********************************************************************************************************************
44 vector<string> RemoveLineageCommand::getRequiredParameters(){
46 string Array[] = {"taxonomy"};
47 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
51 m->errorOut(e, "RemoveLineageCommand", "getRequiredParameters");
55 //**********************************************************************************************************************
56 vector<string> RemoveLineageCommand::getRequiredFiles(){
58 vector<string> myArray;
62 m->errorOut(e, "RemoveLineageCommand", "getRequiredFiles");
66 //**********************************************************************************************************************
67 RemoveLineageCommand::RemoveLineageCommand(string option) {
69 abort = false; calledHelp = false;
71 //allow user to run help
72 if(option == "help") { help(); abort = true; calledHelp = true; }
75 //valid paramters for this command
76 string Array[] = {"fasta","name", "group", "alignreport", "taxon", "dups", "list","taxonomy","outputdir","inputdir"};
77 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
79 OptionParser parser(option);
80 map<string,string> parameters = parser.getParameters();
82 ValidParameters validParameter;
83 map<string,string>::iterator it;
85 //check to make sure all parameters are valid for command
86 for (it = parameters.begin(); it != parameters.end(); it++) {
87 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
90 //initialize outputTypes
91 vector<string> tempOutNames;
92 outputTypes["fasta"] = tempOutNames;
93 outputTypes["taxonomy"] = tempOutNames;
94 outputTypes["name"] = tempOutNames;
95 outputTypes["group"] = tempOutNames;
96 outputTypes["alignreport"] = tempOutNames;
97 outputTypes["list"] = tempOutNames;
99 //if the user changes the output directory command factory will send this info to us in the output parameter
100 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
102 //if the user changes the input directory command factory will send this info to us in the output parameter
103 string inputDir = validParameter.validFile(parameters, "inputdir", false);
104 if (inputDir == "not found"){ inputDir = ""; }
107 it = parameters.find("alignreport");
108 //user has given a template file
109 if(it != parameters.end()){
110 path = m->hasPath(it->second);
111 //if the user has not given a path then, add inputdir. else leave path alone.
112 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
115 it = parameters.find("fasta");
116 //user has given a template file
117 if(it != parameters.end()){
118 path = m->hasPath(it->second);
119 //if the user has not given a path then, add inputdir. else leave path alone.
120 if (path == "") { parameters["fasta"] = inputDir + it->second; }
123 it = parameters.find("list");
124 //user has given a template file
125 if(it != parameters.end()){
126 path = m->hasPath(it->second);
127 //if the user has not given a path then, add inputdir. else leave path alone.
128 if (path == "") { parameters["list"] = inputDir + it->second; }
131 it = parameters.find("name");
132 //user has given a template file
133 if(it != parameters.end()){
134 path = m->hasPath(it->second);
135 //if the user has not given a path then, add inputdir. else leave path alone.
136 if (path == "") { parameters["name"] = inputDir + it->second; }
139 it = parameters.find("group");
140 //user has given a template file
141 if(it != parameters.end()){
142 path = m->hasPath(it->second);
143 //if the user has not given a path then, add inputdir. else leave path alone.
144 if (path == "") { parameters["group"] = inputDir + it->second; }
147 it = parameters.find("taxonomy");
148 //user has given a template file
149 if(it != parameters.end()){
150 path = m->hasPath(it->second);
151 //if the user has not given a path then, add inputdir. else leave path alone.
152 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
157 //check for required parameters
158 fastafile = validParameter.validFile(parameters, "fasta", true);
159 if (fastafile == "not open") { abort = true; }
160 else if (fastafile == "not found") { fastafile = ""; }
162 namefile = validParameter.validFile(parameters, "name", true);
163 if (namefile == "not open") { abort = true; }
164 else if (namefile == "not found") { namefile = ""; }
166 groupfile = validParameter.validFile(parameters, "group", true);
167 if (groupfile == "not open") { abort = true; }
168 else if (groupfile == "not found") { groupfile = ""; }
170 alignfile = validParameter.validFile(parameters, "alignreport", true);
171 if (alignfile == "not open") { abort = true; }
172 else if (alignfile == "not found") { alignfile = ""; }
174 listfile = validParameter.validFile(parameters, "list", true);
175 if (listfile == "not open") { abort = true; }
176 else if (listfile == "not found") { listfile = ""; }
178 taxfile = validParameter.validFile(parameters, "taxonomy", true);
179 if (taxfile == "not open") { abort = true; }
180 else if (taxfile == "not found") { taxfile = ""; m->mothurOut("The taxonomy parameter is required for the get.lineage command."); m->mothurOutEndLine(); abort = true; }
182 string usedDups = "true";
183 string temp = validParameter.validFile(parameters, "dups", false);
184 if (temp == "not found") {
185 if (namefile != "") { temp = "true"; }
186 else { temp = "false"; usedDups = ""; }
188 dups = m->isTrue(temp);
190 taxons = validParameter.validFile(parameters, "taxon", false);
191 if (taxons == "not found") { taxons = ""; m->mothurOut("No taxons given, please correct."); m->mothurOutEndLine(); abort = true; }
194 if (taxons[0] == '\'') { taxons = taxons.substr(1); }
195 if (taxons[(taxons.length()-1)] == '\'') { taxons = taxons.substr(0, (taxons.length()-1)); }
199 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; }
201 if ((usedDups != "") && (namefile == "")) { m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine(); abort = true; }
206 catch(exception& e) {
207 m->errorOut(e, "RemoveLineageCommand", "RemoveLineageCommand");
211 //**********************************************************************************************************************
213 void RemoveLineageCommand::help(){
215 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");
216 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");
217 m->mothurOut("The remove.lineage command parameters are taxon, fasta, name, group, list, taxonomy, alignreport and dups. You must provide taxonomy and taxon.\n");
218 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");
219 m->mothurOut("The taxon parameter allows you to select the taxons you would like to remove.\n");
220 m->mothurOut("You may enter your taxons with confidence scores, doing so will remove only those sequences that belong to the taxonomy and whose cofidence scores fall below the scores you give.\n");
221 m->mothurOut("If they belong to the taxonomy and have confidences above those you provide the sequence will not be removed.\n");
222 m->mothurOut("The remove.lineage command should be in the following format: remove.lineage(taxonomy=yourTaxonomyFile, taxon=yourTaxons).\n");
223 m->mothurOut("Example remove.lineage(taxonomy=amazon.silva.taxonomy, taxon=Bacteria;Firmicutes;Bacilli;Lactobacillales;).\n");
224 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");
225 m->mothurOut("Example remove.lineage(taxonomy=amazon.silva.taxonomy, taxon='Bacteria;Firmicutes;Bacilli;Lactobacillales;').\n");
226 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
228 catch(exception& e) {
229 m->errorOut(e, "RemoveLineageCommand", "help");
234 //**********************************************************************************************************************
236 int RemoveLineageCommand::execute(){
239 if (abort == true) { if (calledHelp) { return 0; } return 2; }
241 if (m->control_pressed) { return 0; }
243 //read through the correct file and output lines you want to keep
244 if (taxfile != "") { readTax(); } //fills the set of names to remove
245 if (namefile != "") { readName(); }
246 if (fastafile != "") { readFasta(); }
247 if (groupfile != "") { readGroup(); }
248 if (alignfile != "") { readAlign(); }
249 if (listfile != "") { readList(); }
252 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
254 if (outputNames.size() != 0) {
255 m->mothurOutEndLine();
256 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
257 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
258 m->mothurOutEndLine();
260 //set fasta file as new current fastafile
262 itTypes = outputTypes.find("fasta");
263 if (itTypes != outputTypes.end()) {
264 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
267 itTypes = outputTypes.find("name");
268 if (itTypes != outputTypes.end()) {
269 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
272 itTypes = outputTypes.find("group");
273 if (itTypes != outputTypes.end()) {
274 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
277 itTypes = outputTypes.find("list");
278 if (itTypes != outputTypes.end()) {
279 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
282 itTypes = outputTypes.find("taxonomy");
283 if (itTypes != outputTypes.end()) {
284 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
291 catch(exception& e) {
292 m->errorOut(e, "RemoveLineageCommand", "execute");
297 //**********************************************************************************************************************
298 int RemoveLineageCommand::readFasta(){
300 string thisOutputDir = outputDir;
301 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
302 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
305 m->openOutputFile(outputFileName, out);
308 m->openInputFile(fastafile, in);
311 bool wroteSomething = false;
314 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
316 Sequence currSeq(in);
317 name = currSeq.getName();
320 //if this name is in the accnos file
321 if (names.count(name) == 0) {
322 wroteSomething = true;
324 currSeq.printSequence(out);
332 if (wroteSomething == false) { m->mothurOut("Your fasta file contains only sequences from " + taxons + "."); m->mothurOutEndLine(); }
333 outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
338 catch(exception& e) {
339 m->errorOut(e, "RemoveLineageCommand", "readFasta");
343 //**********************************************************************************************************************
344 int RemoveLineageCommand::readList(){
346 string thisOutputDir = outputDir;
347 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
348 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
351 m->openOutputFile(outputFileName, out);
354 m->openInputFile(listfile, in);
356 bool wroteSomething = false;
359 //read in list vector
362 //make a new list vector
364 newList.setLabel(list.getLabel());
367 for (int i = 0; i < list.getNumBins(); i++) {
368 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
370 //parse out names that are in accnos file
371 string binnames = list.get(i);
373 string newNames = "";
374 while (binnames.find_first_of(',') != -1) {
375 string name = binnames.substr(0,binnames.find_first_of(','));
376 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
378 //if that name is in the .accnos file, add it
379 if (names.count(name) == 0) { newNames += name + ","; }
383 if (names.count(binnames) == 0) { newNames += binnames + ","; }
385 //if there are names in this bin add to new list
386 if (newNames != "") {
387 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
388 newList.push_back(newNames);
392 //print new listvector
393 if (newList.getNumBins() != 0) {
394 wroteSomething = true;
403 if (wroteSomething == false) { m->mothurOut("Your list file contains only sequences from " + taxons + "."); m->mothurOutEndLine(); }
404 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
409 catch(exception& e) {
410 m->errorOut(e, "RemoveLineageCommand", "readList");
414 //**********************************************************************************************************************
415 int RemoveLineageCommand::readName(){
417 string thisOutputDir = outputDir;
418 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
419 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
422 m->openOutputFile(outputFileName, out);
425 m->openInputFile(namefile, in);
426 string name, firstCol, secondCol;
428 bool wroteSomething = false;
431 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
436 vector<string> parsedNames;
437 m->splitAtComma(secondCol, parsedNames);
439 vector<string> validSecond; validSecond.clear();
440 for (int i = 0; i < parsedNames.size(); i++) {
441 if (names.count(parsedNames[i]) == 0) {
442 validSecond.push_back(parsedNames[i]);
446 if ((dups) && (validSecond.size() != parsedNames.size())) { //if dups is true and we want to get rid of anyone, get rid of everyone
447 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
449 //if the name in the first column is in the set then print it and any other names in second column also in set
450 if (names.count(firstCol) == 0) {
452 wroteSomething = true;
454 out << firstCol << '\t';
456 //you know you have at least one valid second since first column is valid
457 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
458 out << validSecond[validSecond.size()-1] << endl;
460 //make first name in set you come to first column and then add the remaining names to second column
463 //you want part of this row
464 if (validSecond.size() != 0) {
466 wroteSomething = true;
468 out << validSecond[0] << '\t';
470 //you know you have at least one valid second since first column is valid
471 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
472 out << validSecond[validSecond.size()-1] << endl;
481 if (wroteSomething == false) { m->mothurOut("Your name file contains only sequences from " + taxons + "."); m->mothurOutEndLine(); }
482 outputNames.push_back(outputFileName); outputTypes["name"].push_back(outputFileName);
486 catch(exception& e) {
487 m->errorOut(e, "RemoveLineageCommand", "readName");
492 //**********************************************************************************************************************
493 int RemoveLineageCommand::readGroup(){
495 string thisOutputDir = outputDir;
496 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
497 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
500 m->openOutputFile(outputFileName, out);
503 m->openInputFile(groupfile, in);
506 bool wroteSomething = false;
509 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
511 in >> name; //read from first column
512 in >> group; //read from second column
514 //if this name is in the accnos file
515 if (names.count(name) == 0) {
516 wroteSomething = true;
517 out << name << '\t' << group << endl;
525 if (wroteSomething == false) { m->mothurOut("Your group file contains only sequences from " + taxons + "."); m->mothurOutEndLine(); }
526 outputNames.push_back(outputFileName); outputTypes["group"].push_back(outputFileName);
530 catch(exception& e) {
531 m->errorOut(e, "RemoveLineageCommand", "readGroup");
535 //**********************************************************************************************************************
536 int RemoveLineageCommand::readTax(){
538 string thisOutputDir = outputDir;
539 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
540 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
542 m->openOutputFile(outputFileName, out);
545 m->openInputFile(taxfile, in);
548 bool wroteSomething = false;
550 bool taxonsHasConfidence = false;
551 vector< map<string, float> > searchTaxons;
552 string noConfidenceTaxons = taxons;
553 int hasConPos = taxons.find_first_of('(');
554 if (hasConPos != string::npos) {
555 taxonsHasConfidence = true;
556 searchTaxons = getTaxons(taxons);
557 noConfidenceTaxons = removeConfidences(taxons);
563 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
565 in >> name; //read from first column
566 in >> tax; //read from second column
570 //if the users file contains confidence scores we want to ignore them when searching for the taxons, unless the taxon has them
571 if (!taxonsHasConfidence) {
573 int hasConfidences = tax.find_first_of('(');
574 if (hasConfidences != string::npos) {
575 newtax = removeConfidences(tax);
578 int pos = newtax.find(taxons);
580 if (pos == string::npos) {
581 wroteSomething = true;
582 out << name << '\t' << tax << endl;
583 }else{ //this sequence contains the taxon the user wants to remove
587 }else{//if taxons has them and you don't them remove taxons
588 int hasConfidences = tax.find_first_of('(');
589 if (hasConfidences == string::npos) {
591 int pos = newtax.find(noConfidenceTaxons);
593 if (pos == string::npos) {
594 wroteSomething = true;
595 out << name << '\t' << tax << endl;
596 }else{ //this sequence contains the taxon the user wants to remove
599 }else { //both have confidences so we want to make sure the users confidences are greater then or equal to the taxons
600 //first remove confidences from both and see if the taxonomy exists
602 string noNewTax = tax;
603 int hasConfidences = tax.find_first_of('(');
604 if (hasConfidences != string::npos) {
605 noNewTax = removeConfidences(tax);
608 int pos = noNewTax.find(noConfidenceTaxons);
610 if (pos != string::npos) { //if yes, then are the confidences okay
613 vector< map<string, float> > usersTaxon = getTaxons(newtax);
615 //the usersTaxon is most likely longer than the searchTaxons, and searchTaxon[0] may relate to userTaxon[4]
616 //we want to "line them up", so we will find the the index where the searchstring starts
618 for (int i = 0; i < usersTaxon.size(); i++) {
620 if (usersTaxon[i].begin()->first == searchTaxons[0].begin()->first) {
623 bool goodspot = true;
624 //is this really the start, or are we dealing with a taxon of the same name?
625 while ((spot < searchTaxons.size()) && ((i+spot) < usersTaxon.size())) {
626 if (usersTaxon[i+spot].begin()->first != searchTaxons[spot].begin()->first) { goodspot = false; break; }
630 if (goodspot) { break; }
634 for (int i = 0; i < searchTaxons.size(); i++) {
636 if ((i+index) < usersTaxon.size()) { //just in case, should never be false
637 if (usersTaxon[i+index].begin()->second < searchTaxons[i].begin()->second) { //is the users cutoff less than the search taxons
647 //passed the test so remove you
651 wroteSomething = true;
652 out << name << '\t' << tax << endl;
655 wroteSomething = true;
656 out << name << '\t' << tax << endl;
669 if (!wroteSomething) { m->mothurOut("Your taxonomy file contains only sequences from " + taxons + "."); m->mothurOutEndLine(); }
670 outputNames.push_back(outputFileName); outputTypes["taxonomy"].push_back(outputFileName);
675 catch(exception& e) {
676 m->errorOut(e, "RemoveLineageCommand", "readTax");
680 /**************************************************************************************************/
681 vector< map<string, float> > RemoveLineageCommand::getTaxons(string tax) {
684 vector< map<string, float> > t;
686 int taxLength = tax.length();
687 for(int i=0;i<taxLength;i++){
690 int openParen = taxon.find_first_of('(');
691 int closeParen = taxon.find_last_of(')');
693 string newtaxon, confidence;
694 if ((openParen != string::npos) && (closeParen != string::npos)) {
695 newtaxon = taxon.substr(0, openParen); //rip off confidence
696 confidence = taxon.substr((openParen+1), (closeParen-openParen-1));
702 convert(confidence, con);
704 map<string, float> temp;
705 temp[newtaxon] = con;
717 catch(exception& e) {
718 m->errorOut(e, "RemoveLineageCommand", "getTaxons");
722 /**************************************************************************************************/
723 string RemoveLineageCommand::removeConfidences(string tax) {
727 int taxLength = tax.length();
728 for(int i=0;i<taxLength;i++){
730 taxon = taxon.substr(0, taxon.find_first_of('(')); //rip off confidence
740 catch(exception& e) {
741 m->errorOut(e, "RemoveLineageCommand", "removeConfidences");
745 //**********************************************************************************************************************
746 //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
747 int RemoveLineageCommand::readAlign(){
749 string thisOutputDir = outputDir;
750 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
751 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
754 m->openOutputFile(outputFileName, out);
757 m->openInputFile(alignfile, in);
760 bool wroteSomething = false;
762 //read column headers
763 for (int i = 0; i < 16; i++) {
764 if (!in.eof()) { in >> junk; out << junk << '\t'; }
770 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
772 in >> name; //read from first column
774 //if this name is in the accnos file
775 if (names.count(name) == 0) {
776 wroteSomething = true;
781 for (int i = 0; i < 15; i++) {
782 if (!in.eof()) { in >> junk; out << junk << '\t'; }
787 }else {//still read just don't do anything with it
790 for (int i = 0; i < 15; i++) {
791 if (!in.eof()) { in >> junk; }
801 if (wroteSomething == false) { m->mothurOut("Your align file contains only sequences from " + taxons + "."); m->mothurOutEndLine(); }
802 outputNames.push_back(outputFileName); outputTypes["alignreport"].push_back(outputFileName);
807 catch(exception& e) {
808 m->errorOut(e, "RemoveLineageCommand", "readAlign");
812 //**********************************************************************************************************************