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", "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;
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) {
72 //allow user to run help
73 if(option == "help") { help(); abort = true; }
76 //valid paramters for this command
77 string Array[] = {"fasta","name", "group", "alignreport", "accnos", "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;
100 //if the user changes the output directory command factory will send this info to us in the output parameter
101 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
103 //if the user changes the input directory command factory will send this info to us in the output parameter
104 string inputDir = validParameter.validFile(parameters, "inputdir", false);
105 if (inputDir == "not found"){ inputDir = ""; }
108 it = parameters.find("alignreport");
109 //user has given a template file
110 if(it != parameters.end()){
111 path = m->hasPath(it->second);
112 //if the user has not given a path then, add inputdir. else leave path alone.
113 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
116 it = parameters.find("fasta");
117 //user has given a template file
118 if(it != parameters.end()){
119 path = m->hasPath(it->second);
120 //if the user has not given a path then, add inputdir. else leave path alone.
121 if (path == "") { parameters["fasta"] = inputDir + it->second; }
124 it = parameters.find("accnos");
125 //user has given a template file
126 if(it != parameters.end()){
127 path = m->hasPath(it->second);
128 //if the user has not given a path then, add inputdir. else leave path alone.
129 if (path == "") { parameters["accnos"] = inputDir + it->second; }
132 it = parameters.find("list");
133 //user has given a template file
134 if(it != parameters.end()){
135 path = m->hasPath(it->second);
136 //if the user has not given a path then, add inputdir. else leave path alone.
137 if (path == "") { parameters["list"] = inputDir + it->second; }
140 it = parameters.find("name");
141 //user has given a template file
142 if(it != parameters.end()){
143 path = m->hasPath(it->second);
144 //if the user has not given a path then, add inputdir. else leave path alone.
145 if (path == "") { parameters["name"] = inputDir + it->second; }
148 it = parameters.find("group");
149 //user has given a template file
150 if(it != parameters.end()){
151 path = m->hasPath(it->second);
152 //if the user has not given a path then, add inputdir. else leave path alone.
153 if (path == "") { parameters["group"] = inputDir + it->second; }
156 it = parameters.find("taxonomy");
157 //user has given a template file
158 if(it != parameters.end()){
159 path = m->hasPath(it->second);
160 //if the user has not given a path then, add inputdir. else leave path alone.
161 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
166 //check for required parameters
167 accnosfile = validParameter.validFile(parameters, "accnos", true);
168 if (accnosfile == "not open") { abort = true; }
169 else if (accnosfile == "not found") { accnosfile = ""; m->mothurOut("You must provide an accnos file."); m->mothurOutEndLine(); abort = true; }
171 fastafile = validParameter.validFile(parameters, "fasta", true);
172 if (fastafile == "not open") { abort = true; }
173 else if (fastafile == "not found") { fastafile = ""; }
175 namefile = validParameter.validFile(parameters, "name", true);
176 if (namefile == "not open") { abort = true; }
177 else if (namefile == "not found") { namefile = ""; }
179 groupfile = validParameter.validFile(parameters, "group", true);
180 if (groupfile == "not open") { abort = true; }
181 else if (groupfile == "not found") { groupfile = ""; }
183 alignfile = validParameter.validFile(parameters, "alignreport", true);
184 if (alignfile == "not open") { abort = true; }
185 else if (alignfile == "not found") { alignfile = ""; }
187 listfile = validParameter.validFile(parameters, "list", true);
188 if (listfile == "not open") { abort = true; }
189 else if (listfile == "not found") { listfile = ""; }
191 taxfile = validParameter.validFile(parameters, "taxonomy", true);
192 if (taxfile == "not open") { abort = true; }
193 else if (taxfile == "not found") { taxfile = ""; }
196 string usedDups = "true";
197 string temp = validParameter.validFile(parameters, "dups", false);
198 if (temp == "not found") {
199 if (namefile != "") { temp = "true"; }
200 else { temp = "false"; usedDups = ""; }
202 dups = m->isTrue(temp);
204 if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == "") && (taxfile == "")) { m->mothurOut("You must provide at least one of the following: fasta, name, group, taxonomy, alignreport or list."); m->mothurOutEndLine(); abort = true; }
206 if ((usedDups != "") && (namefile == "")) { m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine(); abort = true; }
210 catch(exception& e) {
211 m->errorOut(e, "RemoveSeqsCommand", "RemoveSeqsCommand");
215 //**********************************************************************************************************************
217 void RemoveSeqsCommand::help(){
219 m->mothurOut("The remove.seqs command reads an .accnos file and at least one of the following file types: fasta, name, group, list, taxonomy or alignreport file.\n");
220 m->mothurOut("It outputs a file containing the sequences NOT in the .accnos file.\n");
221 m->mothurOut("The remove.seqs command parameters are accnos, fasta, name, group, list, taxonomy, alignreport and dups. You must provide accnos and at least one of the file parameters.\n");
222 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");
223 m->mothurOut("The remove.seqs command should be in the following format: remove.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
224 m->mothurOut("Example remove.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
225 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
227 catch(exception& e) {
228 m->errorOut(e, "RemoveSeqsCommand", "help");
233 //**********************************************************************************************************************
235 int RemoveSeqsCommand::execute(){
238 if (abort == true) { return 0; }
240 //get names you want to keep
243 if (m->control_pressed) { return 0; }
245 //read through the correct file and output lines you want to keep
246 if (namefile != "") { readName(); }
247 if (fastafile != "") { readFasta(); }
248 if (groupfile != "") { readGroup(); }
249 if (alignfile != "") { readAlign(); }
250 if (listfile != "") { readList(); }
251 if (taxfile != "") { readTax(); }
253 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
255 if (outputNames.size() != 0) {
256 m->mothurOutEndLine();
257 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
258 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
259 m->mothurOutEndLine();
265 catch(exception& e) {
266 m->errorOut(e, "RemoveSeqsCommand", "execute");
271 //**********************************************************************************************************************
272 int RemoveSeqsCommand::readFasta(){
274 string thisOutputDir = outputDir;
275 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
276 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
279 m->openOutputFile(outputFileName, out);
282 m->openInputFile(fastafile, in);
285 bool wroteSomething = false;
288 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
290 Sequence currSeq(in);
291 name = currSeq.getName();
294 //if this name is in the accnos file
295 if (names.count(name) == 0) {
296 wroteSomething = true;
298 currSeq.printSequence(out);
306 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
307 outputTypes["fasta"].push_back(outputFileName);
312 catch(exception& e) {
313 m->errorOut(e, "RemoveSeqsCommand", "readFasta");
317 //**********************************************************************************************************************
318 int RemoveSeqsCommand::readList(){
320 string thisOutputDir = outputDir;
321 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
322 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
325 m->openOutputFile(outputFileName, out);
328 m->openInputFile(listfile, in);
330 bool wroteSomething = false;
333 //read in list vector
336 //make a new list vector
338 newList.setLabel(list.getLabel());
341 for (int i = 0; i < list.getNumBins(); i++) {
342 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
344 //parse out names that are in accnos file
345 string binnames = list.get(i);
347 string newNames = "";
348 while (binnames.find_first_of(',') != -1) {
349 string name = binnames.substr(0,binnames.find_first_of(','));
350 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
352 //if that name is in the .accnos file, add it
353 if (names.count(name) == 0) { newNames += name + ","; }
357 if (names.count(binnames) == 0) { newNames += binnames + ","; }
359 //if there are names in this bin add to new list
360 if (newNames != "") {
361 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
362 newList.push_back(newNames);
366 //print new listvector
367 if (newList.getNumBins() != 0) {
368 wroteSomething = true;
377 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
378 outputTypes["list"].push_back(outputFileName);
383 catch(exception& e) {
384 m->errorOut(e, "RemoveSeqsCommand", "readList");
388 //**********************************************************************************************************************
389 int RemoveSeqsCommand::readName(){
391 string thisOutputDir = outputDir;
392 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
393 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
396 m->openOutputFile(outputFileName, out);
399 m->openInputFile(namefile, in);
400 string name, firstCol, secondCol;
402 bool wroteSomething = false;
405 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
410 vector<string> parsedNames;
411 //parse second column saving each name
412 while (secondCol.find_first_of(',') != -1) {
413 name = secondCol.substr(0,secondCol.find_first_of(','));
414 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
415 parsedNames.push_back(name);
418 //get name after last ,
419 parsedNames.push_back(secondCol);
421 vector<string> validSecond; validSecond.clear();
422 for (int i = 0; i < parsedNames.size(); i++) {
423 if (names.count(parsedNames[i]) == 0) {
424 validSecond.push_back(parsedNames[i]);
428 if ((dups) && (validSecond.size() != parsedNames.size())) { //if dups is true and we want to get rid of anyone, get rid of everyone
429 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
431 //if the name in the first column is in the set then print it and any other names in second column also in set
432 if (names.count(firstCol) == 0) {
434 wroteSomething = true;
436 out << firstCol << '\t';
438 //you know you have at least one valid second since first column is valid
439 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
440 out << validSecond[validSecond.size()-1] << endl;
442 //make first name in set you come to first column and then add the remaining names to second column
445 //you want part of this row
446 if (validSecond.size() != 0) {
448 wroteSomething = true;
450 out << validSecond[0] << '\t';
452 //you know you have at least one valid second since first column is valid
453 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
454 out << validSecond[validSecond.size()-1] << endl;
463 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
464 outputTypes["name"].push_back(outputFileName);
468 catch(exception& e) {
469 m->errorOut(e, "RemoveSeqsCommand", "readName");
474 //**********************************************************************************************************************
475 int RemoveSeqsCommand::readGroup(){
477 string thisOutputDir = outputDir;
478 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
479 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
482 m->openOutputFile(outputFileName, out);
485 m->openInputFile(groupfile, in);
488 bool wroteSomething = false;
491 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
493 in >> name; //read from first column
494 in >> group; //read from second column
496 //if this name is in the accnos file
497 if (names.count(name) == 0) {
498 wroteSomething = true;
499 out << name << '\t' << group << endl;
507 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
508 outputTypes["group"].push_back(outputFileName);
512 catch(exception& e) {
513 m->errorOut(e, "RemoveSeqsCommand", "readGroup");
517 //**********************************************************************************************************************
518 int RemoveSeqsCommand::readTax(){
520 string thisOutputDir = outputDir;
521 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
522 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
524 m->openOutputFile(outputFileName, out);
527 m->openInputFile(taxfile, in);
530 bool wroteSomething = false;
533 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
535 in >> name; //read from first column
536 in >> tax; //read from second column
538 //if this name is in the accnos file
539 if (names.count(name) == 0) {
540 wroteSomething = true;
541 out << name << '\t' << tax << endl;
549 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
550 outputTypes["taxonomy"].push_back(outputFileName);
554 catch(exception& e) {
555 m->errorOut(e, "RemoveSeqsCommand", "readTax");
559 //**********************************************************************************************************************
560 //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
561 int RemoveSeqsCommand::readAlign(){
563 string thisOutputDir = outputDir;
564 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
565 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
568 m->openOutputFile(outputFileName, out);
571 m->openInputFile(alignfile, in);
574 bool wroteSomething = false;
576 //read column headers
577 for (int i = 0; i < 16; i++) {
578 if (!in.eof()) { in >> junk; out << junk << '\t'; }
584 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
586 in >> name; //read from first column
588 //if this name is in the accnos file
589 if (names.count(name) == 0) {
590 wroteSomething = true;
595 for (int i = 0; i < 15; i++) {
596 if (!in.eof()) { in >> junk; out << junk << '\t'; }
601 }else {//still read just don't do anything with it
604 for (int i = 0; i < 15; i++) {
605 if (!in.eof()) { in >> junk; }
615 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
616 outputTypes["alignreport"].push_back(outputFileName);
621 catch(exception& e) {
622 m->errorOut(e, "RemoveSeqsCommand", "readAlign");
626 //**********************************************************************************************************************
627 void RemoveSeqsCommand::readAccnos(){
631 m->openInputFile(accnosfile, in);
644 catch(exception& e) {
645 m->errorOut(e, "RemoveSeqsCommand", "readAccnos");
650 //**********************************************************************************************************************