2 * removeseqscommand.cpp
5 * Created by Sarah Westcott on 7/8/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "removeseqscommand.h"
11 #include "sequence.hpp"
12 #include "listvector.hpp"
14 //**********************************************************************************************************************
15 vector<string> RemoveSeqsCommand::setParameters(){
17 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pfasta);
18 CommandParameter pname("name", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pname);
19 CommandParameter pgroup("group", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pgroup);
20 CommandParameter plist("list", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(plist);
21 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(ptaxonomy);
22 CommandParameter palignreport("alignreport", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(palignreport);
23 CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pqfile);
24 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(paccnos);
25 CommandParameter pdups("dups", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pdups);
26 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
27 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
29 vector<string> myArray;
30 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
34 m->errorOut(e, "RemoveSeqsCommand", "setParameters");
38 //**********************************************************************************************************************
39 string RemoveSeqsCommand::getHelpString(){
41 string helpString = "";
42 helpString += "The remove.seqs command reads an .accnos file and at least one of the following file types: fasta, name, group, list, taxonomy, quality or alignreport file.\n";
43 helpString += "It outputs a file containing the sequences NOT in the .accnos file.\n";
44 helpString += "The remove.seqs command parameters are accnos, fasta, name, group, list, taxonomy, qfile, alignreport and dups. You must provide accnos and at least one of the file parameters.\n";
45 helpString += "The dups parameter allows you to remove the entire line from a name file if you remove any name from the line. default=true. \n";
46 helpString += "The remove.seqs command should be in the following format: remove.seqs(accnos=yourAccnos, fasta=yourFasta).\n";
47 helpString += "Example remove.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n";
48 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
52 m->errorOut(e, "RemoveSeqsCommand", "getHelpString");
58 //**********************************************************************************************************************
59 RemoveSeqsCommand::RemoveSeqsCommand(){
61 abort = true; calledHelp = true;
63 vector<string> tempOutNames;
64 outputTypes["fasta"] = tempOutNames;
65 outputTypes["taxonomy"] = tempOutNames;
66 outputTypes["name"] = tempOutNames;
67 outputTypes["group"] = tempOutNames;
68 outputTypes["alignreport"] = tempOutNames;
69 outputTypes["list"] = tempOutNames;
70 outputTypes["qfile"] = tempOutNames;
73 m->errorOut(e, "RemoveSeqsCommand", "RemoveSeqsCommand");
77 //**********************************************************************************************************************
78 RemoveSeqsCommand::RemoveSeqsCommand(string option) {
80 abort = false; calledHelp = false;
82 //allow user to run help
83 if(option == "help") { help(); abort = true; calledHelp = true; }
84 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
87 vector<string> myArray = setParameters();
89 OptionParser parser(option);
90 map<string,string> parameters = parser.getParameters();
92 ValidParameters validParameter;
93 map<string,string>::iterator it;
95 //check to make sure all parameters are valid for command
96 for (it = parameters.begin(); it != parameters.end(); it++) {
97 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
100 //initialize outputTypes
101 vector<string> tempOutNames;
102 outputTypes["fasta"] = tempOutNames;
103 outputTypes["taxonomy"] = tempOutNames;
104 outputTypes["name"] = tempOutNames;
105 outputTypes["group"] = tempOutNames;
106 outputTypes["alignreport"] = tempOutNames;
107 outputTypes["list"] = tempOutNames;
108 outputTypes["qfile"] = tempOutNames;
110 //if the user changes the output directory command factory will send this info to us in the output parameter
111 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
113 //if the user changes the input directory command factory will send this info to us in the output parameter
114 string inputDir = validParameter.validFile(parameters, "inputdir", false);
115 if (inputDir == "not found"){ inputDir = ""; }
118 it = parameters.find("alignreport");
119 //user has given a template file
120 if(it != parameters.end()){
121 path = m->hasPath(it->second);
122 //if the user has not given a path then, add inputdir. else leave path alone.
123 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
126 it = parameters.find("fasta");
127 //user has given a template file
128 if(it != parameters.end()){
129 path = m->hasPath(it->second);
130 //if the user has not given a path then, add inputdir. else leave path alone.
131 if (path == "") { parameters["fasta"] = inputDir + it->second; }
134 it = parameters.find("accnos");
135 //user has given a template file
136 if(it != parameters.end()){
137 path = m->hasPath(it->second);
138 //if the user has not given a path then, add inputdir. else leave path alone.
139 if (path == "") { parameters["accnos"] = inputDir + it->second; }
142 it = parameters.find("list");
143 //user has given a template file
144 if(it != parameters.end()){
145 path = m->hasPath(it->second);
146 //if the user has not given a path then, add inputdir. else leave path alone.
147 if (path == "") { parameters["list"] = inputDir + it->second; }
150 it = parameters.find("name");
151 //user has given a template file
152 if(it != parameters.end()){
153 path = m->hasPath(it->second);
154 //if the user has not given a path then, add inputdir. else leave path alone.
155 if (path == "") { parameters["name"] = inputDir + it->second; }
158 it = parameters.find("group");
159 //user has given a template file
160 if(it != parameters.end()){
161 path = m->hasPath(it->second);
162 //if the user has not given a path then, add inputdir. else leave path alone.
163 if (path == "") { parameters["group"] = inputDir + it->second; }
166 it = parameters.find("taxonomy");
167 //user has given a template file
168 if(it != parameters.end()){
169 path = m->hasPath(it->second);
170 //if the user has not given a path then, add inputdir. else leave path alone.
171 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
174 it = parameters.find("qfile");
175 //user has given a template file
176 if(it != parameters.end()){
177 path = m->hasPath(it->second);
178 //if the user has not given a path then, add inputdir. else leave path alone.
179 if (path == "") { parameters["qfile"] = inputDir + it->second; }
184 //check for required parameters
185 accnosfile = validParameter.validFile(parameters, "accnos", true);
186 if (accnosfile == "not open") { abort = true; }
187 else if (accnosfile == "not found") {
188 accnosfile = m->getAccnosFile();
189 if (accnosfile != "") { m->mothurOut("Using " + accnosfile + " as input file for the accnos parameter."); m->mothurOutEndLine(); }
191 m->mothurOut("You have no valid accnos file and accnos is required."); m->mothurOutEndLine();
196 fastafile = validParameter.validFile(parameters, "fasta", true);
197 if (fastafile == "not open") { abort = true; }
198 else if (fastafile == "not found") { fastafile = ""; }
200 namefile = validParameter.validFile(parameters, "name", true);
201 if (namefile == "not open") { abort = true; }
202 else if (namefile == "not found") { namefile = ""; }
204 groupfile = validParameter.validFile(parameters, "group", true);
205 if (groupfile == "not open") { abort = true; }
206 else if (groupfile == "not found") { groupfile = ""; }
208 alignfile = validParameter.validFile(parameters, "alignreport", true);
209 if (alignfile == "not open") { abort = true; }
210 else if (alignfile == "not found") { alignfile = ""; }
212 listfile = validParameter.validFile(parameters, "list", true);
213 if (listfile == "not open") { abort = true; }
214 else if (listfile == "not found") { listfile = ""; }
216 taxfile = validParameter.validFile(parameters, "taxonomy", true);
217 if (taxfile == "not open") { abort = true; }
218 else if (taxfile == "not found") { taxfile = ""; }
220 qualfile = validParameter.validFile(parameters, "qfile", true);
221 if (qualfile == "not open") { abort = true; }
222 else if (qualfile == "not found") { qualfile = ""; }
225 string usedDups = "true";
226 string temp = validParameter.validFile(parameters, "dups", false);
227 if (temp == "not found") {
228 if (namefile != "") { temp = "true"; }
229 else { temp = "false"; usedDups = ""; }
231 dups = m->isTrue(temp);
233 if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == "") && (taxfile == "") && (qualfile == "")) { m->mothurOut("You must provide at least one of the following: fasta, name, group, taxonomy, quality, alignreport or list."); m->mothurOutEndLine(); abort = true; }
238 catch(exception& e) {
239 m->errorOut(e, "RemoveSeqsCommand", "RemoveSeqsCommand");
243 //**********************************************************************************************************************
245 int RemoveSeqsCommand::execute(){
248 if (abort == true) { if (calledHelp) { return 0; } return 2; }
250 //get names you want to keep
253 if (m->control_pressed) { return 0; }
255 //read through the correct file and output lines you want to keep
256 if (namefile != "") { readName(); }
257 if (fastafile != "") { readFasta(); }
258 if (groupfile != "") { readGroup(); }
259 if (alignfile != "") { readAlign(); }
260 if (listfile != "") { readList(); }
261 if (taxfile != "") { readTax(); }
262 if (qualfile != "") { readQual(); }
264 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
266 m->mothurOut("Removed " + toString(names.size()) + " sequences."); m->mothurOutEndLine();
268 if (outputNames.size() != 0) {
269 m->mothurOutEndLine();
270 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
271 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
272 m->mothurOutEndLine();
274 //set fasta file as new current fastafile
276 itTypes = outputTypes.find("fasta");
277 if (itTypes != outputTypes.end()) {
278 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
281 itTypes = outputTypes.find("name");
282 if (itTypes != outputTypes.end()) {
283 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
286 itTypes = outputTypes.find("group");
287 if (itTypes != outputTypes.end()) {
288 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
291 itTypes = outputTypes.find("list");
292 if (itTypes != outputTypes.end()) {
293 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
296 itTypes = outputTypes.find("taxonomy");
297 if (itTypes != outputTypes.end()) {
298 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
301 itTypes = outputTypes.find("qfile");
302 if (itTypes != outputTypes.end()) {
303 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
310 catch(exception& e) {
311 m->errorOut(e, "RemoveSeqsCommand", "execute");
316 //**********************************************************************************************************************
317 int RemoveSeqsCommand::readFasta(){
319 string thisOutputDir = outputDir;
320 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
321 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
324 m->openOutputFile(outputFileName, out);
327 m->openInputFile(fastafile, in);
330 bool wroteSomething = false;
333 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
335 Sequence currSeq(in);
336 name = currSeq.getName();
339 //if this name is in the accnos file
340 if (names.count(name) == 0) {
341 wroteSomething = true;
343 currSeq.printSequence(out);
351 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
352 outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName);
357 catch(exception& e) {
358 m->errorOut(e, "RemoveSeqsCommand", "readFasta");
362 //**********************************************************************************************************************
363 int RemoveSeqsCommand::readQual(){
365 string thisOutputDir = outputDir;
366 if (outputDir == "") { thisOutputDir += m->hasPath(qualfile); }
367 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(qualfile)) + "pick" + m->getExtension(qualfile);
369 m->openOutputFile(outputFileName, out);
373 m->openInputFile(qualfile, in);
376 bool wroteSomething = false;
380 string saveName = "";
386 if (name.length() != 0) {
387 saveName = name.substr(1);
390 if (c == 10 || c == 13){ break; }
397 char letter= in.get();
398 if(letter == '>'){ in.putback(letter); break; }
399 else{ scores += letter; }
404 if (names.count(saveName) == 0) {
405 wroteSomething = true;
407 out << name << endl << scores;
416 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
417 outputNames.push_back(outputFileName); outputTypes["qfile"].push_back(outputFileName);
422 catch(exception& e) {
423 m->errorOut(e, "RemoveSeqsCommand", "readQual");
427 //**********************************************************************************************************************
428 int RemoveSeqsCommand::readList(){
430 string thisOutputDir = outputDir;
431 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
432 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
435 m->openOutputFile(outputFileName, out);
438 m->openInputFile(listfile, in);
440 bool wroteSomething = false;
443 //read in list vector
446 //make a new list vector
448 newList.setLabel(list.getLabel());
451 for (int i = 0; i < list.getNumBins(); i++) {
452 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
454 //parse out names that are in accnos file
455 string binnames = list.get(i);
457 string newNames = "";
458 while (binnames.find_first_of(',') != -1) {
459 string name = binnames.substr(0,binnames.find_first_of(','));
460 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
462 //if that name is in the .accnos file, add it
463 if (names.count(name) == 0) { newNames += name + ","; }
467 if (names.count(binnames) == 0) { newNames += binnames + ","; }
469 //if there are names in this bin add to new list
470 if (newNames != "") {
471 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
472 newList.push_back(newNames);
476 //print new listvector
477 if (newList.getNumBins() != 0) {
478 wroteSomething = true;
487 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
488 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
493 catch(exception& e) {
494 m->errorOut(e, "RemoveSeqsCommand", "readList");
498 //**********************************************************************************************************************
499 int RemoveSeqsCommand::readName(){
501 string thisOutputDir = outputDir;
502 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
503 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
506 m->openOutputFile(outputFileName, out);
509 m->openInputFile(namefile, in);
510 string name, firstCol, secondCol;
512 bool wroteSomething = false;
516 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
518 in >> firstCol; m->gobble(in);
521 vector<string> parsedNames;
522 m->splitAtComma(secondCol, parsedNames);
524 vector<string> validSecond; validSecond.clear();
525 for (int i = 0; i < parsedNames.size(); i++) {
526 if (names.count(parsedNames[i]) == 0) {
527 validSecond.push_back(parsedNames[i]);
531 if ((dups) && (validSecond.size() != parsedNames.size())) { //if dups is true and we want to get rid of anyone, get rid of everyone
532 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
534 //if the name in the first column is in the set then print it and any other names in second column also in set
535 if (names.count(firstCol) == 0) {
537 wroteSomething = true;
539 out << firstCol << '\t';
541 //you know you have at least one valid second since first column is valid
542 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
543 out << validSecond[validSecond.size()-1] << endl;
545 //make first name in set you come to first column and then add the remaining names to second column
548 //you want part of this row
549 if (validSecond.size() != 0) {
551 wroteSomething = true;
553 out << validSecond[0] << '\t';
555 //you know you have at least one valid second since first column is valid
556 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
557 out << validSecond[validSecond.size()-1] << endl;
566 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
567 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
571 catch(exception& e) {
572 m->errorOut(e, "RemoveSeqsCommand", "readName");
577 //**********************************************************************************************************************
578 int RemoveSeqsCommand::readGroup(){
580 string thisOutputDir = outputDir;
581 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
582 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
585 m->openOutputFile(outputFileName, out);
588 m->openInputFile(groupfile, in);
591 bool wroteSomething = false;
594 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
596 in >> name; //read from first column
597 in >> group; //read from second column
599 //if this name is in the accnos file
600 if (names.count(name) == 0) {
601 wroteSomething = true;
602 out << name << '\t' << group << endl;
610 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
611 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
615 catch(exception& e) {
616 m->errorOut(e, "RemoveSeqsCommand", "readGroup");
620 //**********************************************************************************************************************
621 int RemoveSeqsCommand::readTax(){
623 string thisOutputDir = outputDir;
624 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
625 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
627 m->openOutputFile(outputFileName, out);
630 m->openInputFile(taxfile, in);
633 bool wroteSomething = false;
636 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
638 in >> name; //read from first column
639 in >> tax; //read from second column
641 //if this name is in the accnos file
642 if (names.count(name) == 0) {
643 wroteSomething = true;
644 out << name << '\t' << tax << endl;
652 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
653 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
657 catch(exception& e) {
658 m->errorOut(e, "RemoveSeqsCommand", "readTax");
662 //**********************************************************************************************************************
663 //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
664 int RemoveSeqsCommand::readAlign(){
666 string thisOutputDir = outputDir;
667 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
668 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
671 m->openOutputFile(outputFileName, out);
674 m->openInputFile(alignfile, in);
677 bool wroteSomething = false;
679 //read column headers
680 for (int i = 0; i < 16; i++) {
681 if (!in.eof()) { in >> junk; out << junk << '\t'; }
687 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
689 in >> name; //read from first column
691 //if this name is in the accnos file
692 if (names.count(name) == 0) {
693 wroteSomething = true;
698 for (int i = 0; i < 15; i++) {
699 if (!in.eof()) { in >> junk; out << junk << '\t'; }
704 }else {//still read just don't do anything with it
707 for (int i = 0; i < 15; i++) {
708 if (!in.eof()) { in >> junk; }
718 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
719 outputTypes["alignreport"].push_back(outputFileName); outputNames.push_back(outputFileName);
724 catch(exception& e) {
725 m->errorOut(e, "RemoveSeqsCommand", "readAlign");
729 //**********************************************************************************************************************
730 void RemoveSeqsCommand::readAccnos(){
734 m->openInputFile(accnosfile, in);
747 catch(exception& e) {
748 m->errorOut(e, "RemoveSeqsCommand", "readAccnos");
753 //**********************************************************************************************************************