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();
281 catch(exception& e) {
282 m->errorOut(e, "RemoveSeqsCommand", "execute");
287 //**********************************************************************************************************************
288 int RemoveSeqsCommand::readFasta(){
290 string thisOutputDir = outputDir;
291 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
292 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
295 m->openOutputFile(outputFileName, out);
298 m->openInputFile(fastafile, in);
301 bool wroteSomething = false;
304 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
306 Sequence currSeq(in);
307 name = currSeq.getName();
310 //if this name is in the accnos file
311 if (names.count(name) == 0) {
312 wroteSomething = true;
314 currSeq.printSequence(out);
322 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
323 outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName);
328 catch(exception& e) {
329 m->errorOut(e, "RemoveSeqsCommand", "readFasta");
333 //**********************************************************************************************************************
334 int RemoveSeqsCommand::readQual(){
336 string thisOutputDir = outputDir;
337 if (outputDir == "") { thisOutputDir += m->hasPath(qualfile); }
338 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(qualfile)) + "pick" + m->getExtension(qualfile);
340 m->openOutputFile(outputFileName, out);
344 m->openInputFile(qualfile, in);
347 bool wroteSomething = false;
351 string saveName = "";
357 if (name.length() != 0) {
358 saveName = name.substr(1);
361 if (c == 10 || c == 13){ break; }
368 char letter= in.get();
369 if(letter == '>'){ in.putback(letter); break; }
370 else{ scores += letter; }
375 if (names.count(saveName) == 0) {
376 wroteSomething = true;
378 out << name << endl << scores;
387 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
388 outputNames.push_back(outputFileName); outputTypes["qfile"].push_back(outputFileName);
393 catch(exception& e) {
394 m->errorOut(e, "RemoveSeqsCommand", "readQual");
398 //**********************************************************************************************************************
399 int RemoveSeqsCommand::readList(){
401 string thisOutputDir = outputDir;
402 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
403 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
406 m->openOutputFile(outputFileName, out);
409 m->openInputFile(listfile, in);
411 bool wroteSomething = false;
414 //read in list vector
417 //make a new list vector
419 newList.setLabel(list.getLabel());
422 for (int i = 0; i < list.getNumBins(); i++) {
423 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
425 //parse out names that are in accnos file
426 string binnames = list.get(i);
428 string newNames = "";
429 while (binnames.find_first_of(',') != -1) {
430 string name = binnames.substr(0,binnames.find_first_of(','));
431 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
433 //if that name is in the .accnos file, add it
434 if (names.count(name) == 0) { newNames += name + ","; }
438 if (names.count(binnames) == 0) { newNames += binnames + ","; }
440 //if there are names in this bin add to new list
441 if (newNames != "") {
442 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
443 newList.push_back(newNames);
447 //print new listvector
448 if (newList.getNumBins() != 0) {
449 wroteSomething = true;
458 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
459 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
464 catch(exception& e) {
465 m->errorOut(e, "RemoveSeqsCommand", "readList");
469 //**********************************************************************************************************************
470 int RemoveSeqsCommand::readName(){
472 string thisOutputDir = outputDir;
473 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
474 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
477 m->openOutputFile(outputFileName, out);
480 m->openInputFile(namefile, in);
481 string name, firstCol, secondCol;
483 bool wroteSomething = false;
486 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
488 in >> firstCol; m->gobble(in);
491 vector<string> parsedNames;
492 //parse second column saving each name
493 while (secondCol.find_first_of(',') != -1) {
494 name = secondCol.substr(0,secondCol.find_first_of(','));
495 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
496 parsedNames.push_back(name);
499 //get name after last ,
500 parsedNames.push_back(secondCol);
502 vector<string> validSecond; validSecond.clear();
503 for (int i = 0; i < parsedNames.size(); i++) {
504 if (names.count(parsedNames[i]) == 0) {
505 validSecond.push_back(parsedNames[i]);
509 if ((dups) && (validSecond.size() != parsedNames.size())) { //if dups is true and we want to get rid of anyone, get rid of everyone
510 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
512 //if the name in the first column is in the set then print it and any other names in second column also in set
513 if (names.count(firstCol) == 0) {
515 wroteSomething = true;
517 out << firstCol << '\t';
519 //you know you have at least one valid second since first column is valid
520 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
521 out << validSecond[validSecond.size()-1] << endl;
523 //make first name in set you come to first column and then add the remaining names to second column
526 //you want part of this row
527 if (validSecond.size() != 0) {
529 wroteSomething = true;
531 out << validSecond[0] << '\t';
533 //you know you have at least one valid second since first column is valid
534 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
535 out << validSecond[validSecond.size()-1] << endl;
544 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
545 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
549 catch(exception& e) {
550 m->errorOut(e, "RemoveSeqsCommand", "readName");
555 //**********************************************************************************************************************
556 int RemoveSeqsCommand::readGroup(){
558 string thisOutputDir = outputDir;
559 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
560 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
563 m->openOutputFile(outputFileName, out);
566 m->openInputFile(groupfile, in);
569 bool wroteSomething = false;
572 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
574 in >> name; //read from first column
575 in >> group; //read from second column
577 //if this name is in the accnos file
578 if (names.count(name) == 0) {
579 wroteSomething = true;
580 out << name << '\t' << group << endl;
588 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
589 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
593 catch(exception& e) {
594 m->errorOut(e, "RemoveSeqsCommand", "readGroup");
598 //**********************************************************************************************************************
599 int RemoveSeqsCommand::readTax(){
601 string thisOutputDir = outputDir;
602 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
603 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
605 m->openOutputFile(outputFileName, out);
608 m->openInputFile(taxfile, in);
611 bool wroteSomething = false;
614 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
616 in >> name; //read from first column
617 in >> tax; //read from second column
619 //if this name is in the accnos file
620 if (names.count(name) == 0) {
621 wroteSomething = true;
622 out << name << '\t' << tax << endl;
630 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
631 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
635 catch(exception& e) {
636 m->errorOut(e, "RemoveSeqsCommand", "readTax");
640 //**********************************************************************************************************************
641 //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
642 int RemoveSeqsCommand::readAlign(){
644 string thisOutputDir = outputDir;
645 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
646 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
649 m->openOutputFile(outputFileName, out);
652 m->openInputFile(alignfile, in);
655 bool wroteSomething = false;
657 //read column headers
658 for (int i = 0; i < 16; i++) {
659 if (!in.eof()) { in >> junk; out << junk << '\t'; }
665 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
667 in >> name; //read from first column
669 //if this name is in the accnos file
670 if (names.count(name) == 0) {
671 wroteSomething = true;
676 for (int i = 0; i < 15; i++) {
677 if (!in.eof()) { in >> junk; out << junk << '\t'; }
682 }else {//still read just don't do anything with it
685 for (int i = 0; i < 15; i++) {
686 if (!in.eof()) { in >> junk; }
696 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
697 outputTypes["alignreport"].push_back(outputFileName); outputNames.push_back(outputFileName);
702 catch(exception& e) {
703 m->errorOut(e, "RemoveSeqsCommand", "readAlign");
707 //**********************************************************************************************************************
708 void RemoveSeqsCommand::readAccnos(){
712 m->openInputFile(accnosfile, in);
725 catch(exception& e) {
726 m->errorOut(e, "RemoveSeqsCommand", "readAccnos");
731 //**********************************************************************************************************************