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(){
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;
38 outputTypes["qfile"] = tempOutNames;
41 m->errorOut(e, "RemoveSeqsCommand", "RemoveSeqsCommand");
45 //**********************************************************************************************************************
46 vector<string> RemoveSeqsCommand::getRequiredParameters(){
48 string Array[] = {"accnos"};
49 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
53 m->errorOut(e, "RemoveSeqsCommand", "getRequiredParameters");
57 //**********************************************************************************************************************
58 vector<string> RemoveSeqsCommand::getRequiredFiles(){
60 vector<string> myArray;
64 m->errorOut(e, "RemoveSeqsCommand", "getRequiredFiles");
68 //**********************************************************************************************************************
69 RemoveSeqsCommand::RemoveSeqsCommand(string option) {
73 //allow user to run help
74 if(option == "help") { help(); abort = true; }
77 //valid paramters for this command
78 string Array[] = {"fasta","name", "group", "alignreport", "accnos", "qfile", "list","taxonomy","outputdir","inputdir", "dups" };
79 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
81 OptionParser parser(option);
82 map<string,string> parameters = parser.getParameters();
84 ValidParameters validParameter;
85 map<string,string>::iterator it;
87 //check to make sure all parameters are valid for command
88 for (it = parameters.begin(); it != parameters.end(); it++) {
89 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
92 //initialize outputTypes
93 vector<string> tempOutNames;
94 outputTypes["fasta"] = tempOutNames;
95 outputTypes["taxonomy"] = tempOutNames;
96 outputTypes["name"] = tempOutNames;
97 outputTypes["group"] = tempOutNames;
98 outputTypes["alignreport"] = tempOutNames;
99 outputTypes["list"] = tempOutNames;
100 outputTypes["qfile"] = tempOutNames;
102 //if the user changes the output directory command factory will send this info to us in the output parameter
103 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
105 //if the user changes the input directory command factory will send this info to us in the output parameter
106 string inputDir = validParameter.validFile(parameters, "inputdir", false);
107 if (inputDir == "not found"){ inputDir = ""; }
110 it = parameters.find("alignreport");
111 //user has given a template file
112 if(it != parameters.end()){
113 path = m->hasPath(it->second);
114 //if the user has not given a path then, add inputdir. else leave path alone.
115 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
118 it = parameters.find("fasta");
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["fasta"] = inputDir + it->second; }
126 it = parameters.find("accnos");
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["accnos"] = inputDir + it->second; }
134 it = parameters.find("list");
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["list"] = inputDir + it->second; }
142 it = parameters.find("name");
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["name"] = inputDir + it->second; }
150 it = parameters.find("group");
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["group"] = inputDir + it->second; }
158 it = parameters.find("taxonomy");
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["taxonomy"] = inputDir + it->second; }
166 it = parameters.find("qfile");
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["qfile"] = inputDir + it->second; }
176 //check for required parameters
177 accnosfile = validParameter.validFile(parameters, "accnos", true);
178 if (accnosfile == "not open") { abort = true; }
179 else if (accnosfile == "not found") { accnosfile = ""; m->mothurOut("You must provide an accnos file."); m->mothurOutEndLine(); abort = true; }
181 fastafile = validParameter.validFile(parameters, "fasta", true);
182 if (fastafile == "not open") { abort = true; }
183 else if (fastafile == "not found") { fastafile = ""; }
185 namefile = validParameter.validFile(parameters, "name", true);
186 if (namefile == "not open") { abort = true; }
187 else if (namefile == "not found") { namefile = ""; }
189 groupfile = validParameter.validFile(parameters, "group", true);
190 if (groupfile == "not open") { abort = true; }
191 else if (groupfile == "not found") { groupfile = ""; }
193 alignfile = validParameter.validFile(parameters, "alignreport", true);
194 if (alignfile == "not open") { abort = true; }
195 else if (alignfile == "not found") { alignfile = ""; }
197 listfile = validParameter.validFile(parameters, "list", true);
198 if (listfile == "not open") { abort = true; }
199 else if (listfile == "not found") { listfile = ""; }
201 taxfile = validParameter.validFile(parameters, "taxonomy", true);
202 if (taxfile == "not open") { abort = true; }
203 else if (taxfile == "not found") { taxfile = ""; }
205 qualfile = validParameter.validFile(parameters, "qfile", true);
206 if (qualfile == "not open") { abort = true; }
207 else if (qualfile == "not found") { qualfile = ""; }
210 string usedDups = "true";
211 string temp = validParameter.validFile(parameters, "dups", false);
212 if (temp == "not found") {
213 if (namefile != "") { temp = "true"; }
214 else { temp = "false"; usedDups = ""; }
216 dups = m->isTrue(temp);
218 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; }
220 if ((usedDups != "") && (namefile == "")) { m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine(); abort = true; }
224 catch(exception& e) {
225 m->errorOut(e, "RemoveSeqsCommand", "RemoveSeqsCommand");
229 //**********************************************************************************************************************
231 void RemoveSeqsCommand::help(){
233 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");
234 m->mothurOut("It outputs a file containing the sequences NOT in the .accnos file.\n");
235 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");
236 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");
237 m->mothurOut("The remove.seqs command should be in the following format: remove.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
238 m->mothurOut("Example remove.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
239 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
241 catch(exception& e) {
242 m->errorOut(e, "RemoveSeqsCommand", "help");
247 //**********************************************************************************************************************
249 int RemoveSeqsCommand::execute(){
252 if (abort == true) { return 0; }
254 //get names you want to keep
257 if (m->control_pressed) { return 0; }
259 //read through the correct file and output lines you want to keep
260 if (namefile != "") { readName(); }
261 if (fastafile != "") { readFasta(); }
262 if (groupfile != "") { readGroup(); }
263 if (alignfile != "") { readAlign(); }
264 if (listfile != "") { readList(); }
265 if (taxfile != "") { readTax(); }
266 if (qualfile != "") { readQual(); }
268 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
270 m->mothurOut("Removed " + toString(names.size()) + " sequences."); m->mothurOutEndLine();
272 if (outputNames.size() != 0) {
273 m->mothurOutEndLine();
274 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
275 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
276 m->mothurOutEndLine();
282 catch(exception& e) {
283 m->errorOut(e, "RemoveSeqsCommand", "execute");
288 //**********************************************************************************************************************
289 int RemoveSeqsCommand::readFasta(){
291 string thisOutputDir = outputDir;
292 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
293 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
296 m->openOutputFile(outputFileName, out);
299 m->openInputFile(fastafile, in);
302 bool wroteSomething = false;
305 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
307 Sequence currSeq(in);
308 name = currSeq.getName();
311 //if this name is in the accnos file
312 if (names.count(name) == 0) {
313 wroteSomething = true;
315 currSeq.printSequence(out);
323 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
324 outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName);
329 catch(exception& e) {
330 m->errorOut(e, "RemoveSeqsCommand", "readFasta");
334 //**********************************************************************************************************************
335 int RemoveSeqsCommand::readQual(){
337 string thisOutputDir = outputDir;
338 if (outputDir == "") { thisOutputDir += m->hasPath(qualfile); }
339 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(qualfile)) + "pick" + m->getExtension(qualfile);
341 m->openOutputFile(outputFileName, out);
345 m->openInputFile(qualfile, in);
348 bool wroteSomething = false;
352 string saveName = "";
358 if (name.length() != 0) {
359 saveName = name.substr(1);
362 if (c == 10 || c == 13){ break; }
369 char letter= in.get();
370 if(letter == '>'){ in.putback(letter); break; }
371 else{ scores += letter; }
376 if (names.count(saveName) == 0) {
377 wroteSomething = true;
379 out << name << endl << scores;
388 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
389 outputNames.push_back(outputFileName); outputTypes["qfile"].push_back(outputFileName);
394 catch(exception& e) {
395 m->errorOut(e, "RemoveSeqsCommand", "readQual");
399 //**********************************************************************************************************************
400 int RemoveSeqsCommand::readList(){
402 string thisOutputDir = outputDir;
403 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
404 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
407 m->openOutputFile(outputFileName, out);
410 m->openInputFile(listfile, in);
412 bool wroteSomething = false;
415 //read in list vector
418 //make a new list vector
420 newList.setLabel(list.getLabel());
423 for (int i = 0; i < list.getNumBins(); i++) {
424 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
426 //parse out names that are in accnos file
427 string binnames = list.get(i);
429 string newNames = "";
430 while (binnames.find_first_of(',') != -1) {
431 string name = binnames.substr(0,binnames.find_first_of(','));
432 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
434 //if that name is in the .accnos file, add it
435 if (names.count(name) == 0) { newNames += name + ","; }
439 if (names.count(binnames) == 0) { newNames += binnames + ","; }
441 //if there are names in this bin add to new list
442 if (newNames != "") {
443 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
444 newList.push_back(newNames);
448 //print new listvector
449 if (newList.getNumBins() != 0) {
450 wroteSomething = true;
459 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
460 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
465 catch(exception& e) {
466 m->errorOut(e, "RemoveSeqsCommand", "readList");
470 //**********************************************************************************************************************
471 int RemoveSeqsCommand::readName(){
473 string thisOutputDir = outputDir;
474 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
475 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
478 m->openOutputFile(outputFileName, out);
481 m->openInputFile(namefile, in);
482 string name, firstCol, secondCol;
484 bool wroteSomething = false;
487 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
489 in >> firstCol; m->gobble(in);
492 vector<string> parsedNames;
493 //parse second column saving each name
494 while (secondCol.find_first_of(',') != -1) {
495 name = secondCol.substr(0,secondCol.find_first_of(','));
496 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
497 parsedNames.push_back(name);
500 //get name after last ,
501 parsedNames.push_back(secondCol);
503 vector<string> validSecond; validSecond.clear();
504 for (int i = 0; i < parsedNames.size(); i++) {
505 if (names.count(parsedNames[i]) == 0) {
506 validSecond.push_back(parsedNames[i]);
510 if ((dups) && (validSecond.size() != parsedNames.size())) { //if dups is true and we want to get rid of anyone, get rid of everyone
511 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
513 //if the name in the first column is in the set then print it and any other names in second column also in set
514 if (names.count(firstCol) == 0) {
516 wroteSomething = true;
518 out << firstCol << '\t';
520 //you know you have at least one valid second since first column is valid
521 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
522 out << validSecond[validSecond.size()-1] << endl;
524 //make first name in set you come to first column and then add the remaining names to second column
527 //you want part of this row
528 if (validSecond.size() != 0) {
530 wroteSomething = true;
532 out << validSecond[0] << '\t';
534 //you know you have at least one valid second since first column is valid
535 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
536 out << validSecond[validSecond.size()-1] << endl;
545 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
546 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
550 catch(exception& e) {
551 m->errorOut(e, "RemoveSeqsCommand", "readName");
556 //**********************************************************************************************************************
557 int RemoveSeqsCommand::readGroup(){
559 string thisOutputDir = outputDir;
560 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
561 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
564 m->openOutputFile(outputFileName, out);
567 m->openInputFile(groupfile, in);
570 bool wroteSomething = false;
573 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
575 in >> name; //read from first column
576 in >> group; //read from second column
578 //if this name is in the accnos file
579 if (names.count(name) == 0) {
580 wroteSomething = true;
581 out << name << '\t' << group << endl;
589 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
590 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
594 catch(exception& e) {
595 m->errorOut(e, "RemoveSeqsCommand", "readGroup");
599 //**********************************************************************************************************************
600 int RemoveSeqsCommand::readTax(){
602 string thisOutputDir = outputDir;
603 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
604 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
606 m->openOutputFile(outputFileName, out);
609 m->openInputFile(taxfile, in);
612 bool wroteSomething = false;
615 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
617 in >> name; //read from first column
618 in >> tax; //read from second column
620 //if this name is in the accnos file
621 if (names.count(name) == 0) {
622 wroteSomething = true;
623 out << name << '\t' << tax << endl;
631 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
632 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
636 catch(exception& e) {
637 m->errorOut(e, "RemoveSeqsCommand", "readTax");
641 //**********************************************************************************************************************
642 //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
643 int RemoveSeqsCommand::readAlign(){
645 string thisOutputDir = outputDir;
646 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
647 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
650 m->openOutputFile(outputFileName, out);
653 m->openInputFile(alignfile, in);
656 bool wroteSomething = false;
658 //read column headers
659 for (int i = 0; i < 16; i++) {
660 if (!in.eof()) { in >> junk; out << junk << '\t'; }
666 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
668 in >> name; //read from first column
670 //if this name is in the accnos file
671 if (names.count(name) == 0) {
672 wroteSomething = true;
677 for (int i = 0; i < 15; i++) {
678 if (!in.eof()) { in >> junk; out << junk << '\t'; }
683 }else {//still read just don't do anything with it
686 for (int i = 0; i < 15; i++) {
687 if (!in.eof()) { in >> junk; }
697 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
698 outputTypes["alignreport"].push_back(outputFileName); outputNames.push_back(outputFileName);
703 catch(exception& e) {
704 m->errorOut(e, "RemoveSeqsCommand", "readAlign");
708 //**********************************************************************************************************************
709 void RemoveSeqsCommand::readAccnos(){
713 m->openInputFile(accnosfile, in);
726 catch(exception& e) {
727 m->errorOut(e, "RemoveSeqsCommand", "readAccnos");
732 //**********************************************************************************************************************