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::getValidParameters(){
17 string Array[] = {"fasta","name", "group", "alignreport", "accnos", "qfile","list","taxonomy","outputdir","inputdir", "dups" };
18 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
22 m->errorOut(e, "RemoveSeqsCommand", "getValidParameters");
26 //**********************************************************************************************************************
27 RemoveSeqsCommand::RemoveSeqsCommand(){
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;
37 outputTypes["qfile"] = tempOutNames;
40 m->errorOut(e, "RemoveSeqsCommand", "RemoveSeqsCommand");
44 //**********************************************************************************************************************
45 vector<string> RemoveSeqsCommand::getRequiredParameters(){
47 string Array[] = {"accnos"};
48 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
52 m->errorOut(e, "RemoveSeqsCommand", "getRequiredParameters");
56 //**********************************************************************************************************************
57 vector<string> RemoveSeqsCommand::getRequiredFiles(){
59 vector<string> myArray;
63 m->errorOut(e, "RemoveSeqsCommand", "getRequiredFiles");
67 //**********************************************************************************************************************
68 RemoveSeqsCommand::RemoveSeqsCommand(string option) {
70 abort = false; calledHelp = false;
72 //allow user to run help
73 if(option == "help") { help(); abort = true; calledHelp = true; }
76 //valid paramters for this command
77 string Array[] = {"fasta","name", "group", "alignreport", "accnos", "qfile", "list","taxonomy","outputdir","inputdir", "dups" };
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;
99 outputTypes["qfile"] = tempOutNames;
101 //if the user changes the output directory command factory will send this info to us in the output parameter
102 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
104 //if the user changes the input directory command factory will send this info to us in the output parameter
105 string inputDir = validParameter.validFile(parameters, "inputdir", false);
106 if (inputDir == "not found"){ inputDir = ""; }
109 it = parameters.find("alignreport");
110 //user has given a template file
111 if(it != parameters.end()){
112 path = m->hasPath(it->second);
113 //if the user has not given a path then, add inputdir. else leave path alone.
114 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
117 it = parameters.find("fasta");
118 //user has given a template file
119 if(it != parameters.end()){
120 path = m->hasPath(it->second);
121 //if the user has not given a path then, add inputdir. else leave path alone.
122 if (path == "") { parameters["fasta"] = inputDir + it->second; }
125 it = parameters.find("accnos");
126 //user has given a template file
127 if(it != parameters.end()){
128 path = m->hasPath(it->second);
129 //if the user has not given a path then, add inputdir. else leave path alone.
130 if (path == "") { parameters["accnos"] = inputDir + it->second; }
133 it = parameters.find("list");
134 //user has given a template file
135 if(it != parameters.end()){
136 path = m->hasPath(it->second);
137 //if the user has not given a path then, add inputdir. else leave path alone.
138 if (path == "") { parameters["list"] = inputDir + it->second; }
141 it = parameters.find("name");
142 //user has given a template file
143 if(it != parameters.end()){
144 path = m->hasPath(it->second);
145 //if the user has not given a path then, add inputdir. else leave path alone.
146 if (path == "") { parameters["name"] = inputDir + it->second; }
149 it = parameters.find("group");
150 //user has given a template file
151 if(it != parameters.end()){
152 path = m->hasPath(it->second);
153 //if the user has not given a path then, add inputdir. else leave path alone.
154 if (path == "") { parameters["group"] = inputDir + it->second; }
157 it = parameters.find("taxonomy");
158 //user has given a template file
159 if(it != parameters.end()){
160 path = m->hasPath(it->second);
161 //if the user has not given a path then, add inputdir. else leave path alone.
162 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
165 it = parameters.find("qfile");
166 //user has given a template file
167 if(it != parameters.end()){
168 path = m->hasPath(it->second);
169 //if the user has not given a path then, add inputdir. else leave path alone.
170 if (path == "") { parameters["qfile"] = inputDir + it->second; }
175 //check for required parameters
176 accnosfile = validParameter.validFile(parameters, "accnos", true);
177 if (accnosfile == "not open") { abort = true; }
178 else if (accnosfile == "not found") { accnosfile = ""; m->mothurOut("You must provide an accnos file."); m->mothurOutEndLine(); abort = true; }
180 fastafile = validParameter.validFile(parameters, "fasta", true);
181 if (fastafile == "not open") { abort = true; }
182 else if (fastafile == "not found") { fastafile = ""; }
184 namefile = validParameter.validFile(parameters, "name", true);
185 if (namefile == "not open") { abort = true; }
186 else if (namefile == "not found") { namefile = ""; }
188 groupfile = validParameter.validFile(parameters, "group", true);
189 if (groupfile == "not open") { abort = true; }
190 else if (groupfile == "not found") { groupfile = ""; }
192 alignfile = validParameter.validFile(parameters, "alignreport", true);
193 if (alignfile == "not open") { abort = true; }
194 else if (alignfile == "not found") { alignfile = ""; }
196 listfile = validParameter.validFile(parameters, "list", true);
197 if (listfile == "not open") { abort = true; }
198 else if (listfile == "not found") { listfile = ""; }
200 taxfile = validParameter.validFile(parameters, "taxonomy", true);
201 if (taxfile == "not open") { abort = true; }
202 else if (taxfile == "not found") { taxfile = ""; }
204 qualfile = validParameter.validFile(parameters, "qfile", true);
205 if (qualfile == "not open") { abort = true; }
206 else if (qualfile == "not found") { qualfile = ""; }
209 string usedDups = "true";
210 string temp = validParameter.validFile(parameters, "dups", false);
211 if (temp == "not found") {
212 if (namefile != "") { temp = "true"; }
213 else { temp = "false"; usedDups = ""; }
215 dups = m->isTrue(temp);
217 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; }
219 if ((usedDups != "") && (namefile == "")) { m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine(); abort = true; }
223 catch(exception& e) {
224 m->errorOut(e, "RemoveSeqsCommand", "RemoveSeqsCommand");
228 //**********************************************************************************************************************
230 void RemoveSeqsCommand::help(){
232 m->mothurOut("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");
233 m->mothurOut("It outputs a file containing the sequences NOT in the .accnos file.\n");
234 m->mothurOut("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");
235 m->mothurOut("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");
236 m->mothurOut("The remove.seqs command should be in the following format: remove.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
237 m->mothurOut("Example remove.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
238 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
240 catch(exception& e) {
241 m->errorOut(e, "RemoveSeqsCommand", "help");
246 //**********************************************************************************************************************
248 int RemoveSeqsCommand::execute(){
251 if (abort == true) { if (calledHelp) { return 0; } return 2; }
253 //get names you want to keep
256 if (m->control_pressed) { return 0; }
258 //read through the correct file and output lines you want to keep
259 if (namefile != "") { readName(); }
260 if (fastafile != "") { readFasta(); }
261 if (groupfile != "") { readGroup(); }
262 if (alignfile != "") { readAlign(); }
263 if (listfile != "") { readList(); }
264 if (taxfile != "") { readTax(); }
265 if (qualfile != "") { readQual(); }
267 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
269 m->mothurOut("Removed " + toString(names.size()) + " sequences."); m->mothurOutEndLine();
271 if (outputNames.size() != 0) {
272 m->mothurOutEndLine();
273 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
274 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
275 m->mothurOutEndLine();
277 //set fasta file as new current fastafile
279 itTypes = outputTypes.find("fasta");
280 if (itTypes != outputTypes.end()) {
281 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
284 itTypes = outputTypes.find("name");
285 if (itTypes != outputTypes.end()) {
286 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
289 itTypes = outputTypes.find("group");
290 if (itTypes != outputTypes.end()) {
291 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
294 itTypes = outputTypes.find("list");
295 if (itTypes != outputTypes.end()) {
296 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
299 itTypes = outputTypes.find("taxonomy");
300 if (itTypes != outputTypes.end()) {
301 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
304 itTypes = outputTypes.find("qfile");
305 if (itTypes != outputTypes.end()) {
306 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
313 catch(exception& e) {
314 m->errorOut(e, "RemoveSeqsCommand", "execute");
319 //**********************************************************************************************************************
320 int RemoveSeqsCommand::readFasta(){
322 string thisOutputDir = outputDir;
323 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
324 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
327 m->openOutputFile(outputFileName, out);
330 m->openInputFile(fastafile, in);
333 bool wroteSomething = false;
336 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
338 Sequence currSeq(in);
339 name = currSeq.getName();
342 //if this name is in the accnos file
343 if (names.count(name) == 0) {
344 wroteSomething = true;
346 currSeq.printSequence(out);
354 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
355 outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName);
360 catch(exception& e) {
361 m->errorOut(e, "RemoveSeqsCommand", "readFasta");
365 //**********************************************************************************************************************
366 int RemoveSeqsCommand::readQual(){
368 string thisOutputDir = outputDir;
369 if (outputDir == "") { thisOutputDir += m->hasPath(qualfile); }
370 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(qualfile)) + "pick" + m->getExtension(qualfile);
372 m->openOutputFile(outputFileName, out);
376 m->openInputFile(qualfile, in);
379 bool wroteSomething = false;
383 string saveName = "";
389 if (name.length() != 0) {
390 saveName = name.substr(1);
393 if (c == 10 || c == 13){ break; }
400 char letter= in.get();
401 if(letter == '>'){ in.putback(letter); break; }
402 else{ scores += letter; }
407 if (names.count(saveName) == 0) {
408 wroteSomething = true;
410 out << name << endl << scores;
419 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
420 outputNames.push_back(outputFileName); outputTypes["qfile"].push_back(outputFileName);
425 catch(exception& e) {
426 m->errorOut(e, "RemoveSeqsCommand", "readQual");
430 //**********************************************************************************************************************
431 int RemoveSeqsCommand::readList(){
433 string thisOutputDir = outputDir;
434 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
435 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
438 m->openOutputFile(outputFileName, out);
441 m->openInputFile(listfile, in);
443 bool wroteSomething = false;
446 //read in list vector
449 //make a new list vector
451 newList.setLabel(list.getLabel());
454 for (int i = 0; i < list.getNumBins(); i++) {
455 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
457 //parse out names that are in accnos file
458 string binnames = list.get(i);
460 string newNames = "";
461 while (binnames.find_first_of(',') != -1) {
462 string name = binnames.substr(0,binnames.find_first_of(','));
463 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
465 //if that name is in the .accnos file, add it
466 if (names.count(name) == 0) { newNames += name + ","; }
470 if (names.count(binnames) == 0) { newNames += binnames + ","; }
472 //if there are names in this bin add to new list
473 if (newNames != "") {
474 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
475 newList.push_back(newNames);
479 //print new listvector
480 if (newList.getNumBins() != 0) {
481 wroteSomething = true;
490 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
491 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
496 catch(exception& e) {
497 m->errorOut(e, "RemoveSeqsCommand", "readList");
501 //**********************************************************************************************************************
502 int RemoveSeqsCommand::readName(){
504 string thisOutputDir = outputDir;
505 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
506 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
509 m->openOutputFile(outputFileName, out);
512 m->openInputFile(namefile, in);
513 string name, firstCol, secondCol;
515 bool wroteSomething = false;
518 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
520 in >> firstCol; m->gobble(in);
523 vector<string> parsedNames;
524 //parse second column saving each name
525 while (secondCol.find_first_of(',') != -1) {
526 name = secondCol.substr(0,secondCol.find_first_of(','));
527 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
528 parsedNames.push_back(name);
531 //get name after last ,
532 parsedNames.push_back(secondCol);
534 vector<string> validSecond; validSecond.clear();
535 for (int i = 0; i < parsedNames.size(); i++) {
536 if (names.count(parsedNames[i]) == 0) {
537 validSecond.push_back(parsedNames[i]);
541 if ((dups) && (validSecond.size() != parsedNames.size())) { //if dups is true and we want to get rid of anyone, get rid of everyone
542 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
544 //if the name in the first column is in the set then print it and any other names in second column also in set
545 if (names.count(firstCol) == 0) {
547 wroteSomething = true;
549 out << firstCol << '\t';
551 //you know you have at least one valid second since first column is valid
552 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
553 out << validSecond[validSecond.size()-1] << endl;
555 //make first name in set you come to first column and then add the remaining names to second column
558 //you want part of this row
559 if (validSecond.size() != 0) {
561 wroteSomething = true;
563 out << validSecond[0] << '\t';
565 //you know you have at least one valid second since first column is valid
566 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
567 out << validSecond[validSecond.size()-1] << endl;
576 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
577 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
581 catch(exception& e) {
582 m->errorOut(e, "RemoveSeqsCommand", "readName");
587 //**********************************************************************************************************************
588 int RemoveSeqsCommand::readGroup(){
590 string thisOutputDir = outputDir;
591 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
592 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
595 m->openOutputFile(outputFileName, out);
598 m->openInputFile(groupfile, in);
601 bool wroteSomething = false;
604 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
606 in >> name; //read from first column
607 in >> group; //read from second column
609 //if this name is in the accnos file
610 if (names.count(name) == 0) {
611 wroteSomething = true;
612 out << name << '\t' << group << endl;
620 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
621 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
625 catch(exception& e) {
626 m->errorOut(e, "RemoveSeqsCommand", "readGroup");
630 //**********************************************************************************************************************
631 int RemoveSeqsCommand::readTax(){
633 string thisOutputDir = outputDir;
634 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
635 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
637 m->openOutputFile(outputFileName, out);
640 m->openInputFile(taxfile, in);
643 bool wroteSomething = false;
646 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
648 in >> name; //read from first column
649 in >> tax; //read from second column
651 //if this name is in the accnos file
652 if (names.count(name) == 0) {
653 wroteSomething = true;
654 out << name << '\t' << tax << endl;
662 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
663 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
667 catch(exception& e) {
668 m->errorOut(e, "RemoveSeqsCommand", "readTax");
672 //**********************************************************************************************************************
673 //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
674 int RemoveSeqsCommand::readAlign(){
676 string thisOutputDir = outputDir;
677 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
678 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
681 m->openOutputFile(outputFileName, out);
684 m->openInputFile(alignfile, in);
687 bool wroteSomething = false;
689 //read column headers
690 for (int i = 0; i < 16; i++) {
691 if (!in.eof()) { in >> junk; out << junk << '\t'; }
697 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
699 in >> name; //read from first column
701 //if this name is in the accnos file
702 if (names.count(name) == 0) {
703 wroteSomething = true;
708 for (int i = 0; i < 15; i++) {
709 if (!in.eof()) { in >> junk; out << junk << '\t'; }
714 }else {//still read just don't do anything with it
717 for (int i = 0; i < 15; i++) {
718 if (!in.eof()) { in >> junk; }
728 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
729 outputTypes["alignreport"].push_back(outputFileName); outputNames.push_back(outputFileName);
734 catch(exception& e) {
735 m->errorOut(e, "RemoveSeqsCommand", "readAlign");
739 //**********************************************************************************************************************
740 void RemoveSeqsCommand::readAccnos(){
744 m->openInputFile(accnosfile, in);
757 catch(exception& e) {
758 m->errorOut(e, "RemoveSeqsCommand", "readAccnos");
763 //**********************************************************************************************************************