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 if (outputNames.size() != 0) {
271 m->mothurOutEndLine();
272 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
273 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
274 m->mothurOutEndLine();
280 catch(exception& e) {
281 m->errorOut(e, "RemoveSeqsCommand", "execute");
286 //**********************************************************************************************************************
287 int RemoveSeqsCommand::readFasta(){
289 string thisOutputDir = outputDir;
290 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
291 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
294 m->openOutputFile(outputFileName, out);
297 m->openInputFile(fastafile, in);
300 bool wroteSomething = false;
303 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
305 Sequence currSeq(in);
306 name = currSeq.getName();
309 //if this name is in the accnos file
310 if (names.count(name) == 0) {
311 wroteSomething = true;
313 currSeq.printSequence(out);
321 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
322 outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName);
327 catch(exception& e) {
328 m->errorOut(e, "RemoveSeqsCommand", "readFasta");
332 //**********************************************************************************************************************
333 int RemoveSeqsCommand::readQual(){
335 string thisOutputDir = outputDir;
336 if (outputDir == "") { thisOutputDir += m->hasPath(qualfile); }
337 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(qualfile)) + "pick" + m->getExtension(qualfile);
339 m->openOutputFile(outputFileName, out);
343 m->openInputFile(qualfile, in);
346 bool wroteSomething = false;
350 string saveName = "";
356 if (name.length() != 0) {
357 saveName = name.substr(1);
360 if (c == 10 || c == 13){ break; }
367 char letter= in.get();
368 if(letter == '>'){ in.putback(letter); break; }
369 else{ scores += letter; }
374 if (names.count(saveName) == 0) {
375 wroteSomething = true;
377 out << name << endl << scores;
386 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
387 outputNames.push_back(outputFileName); outputTypes["qfile"].push_back(outputFileName);
392 catch(exception& e) {
393 m->errorOut(e, "RemoveSeqsCommand", "readQual");
397 //**********************************************************************************************************************
398 int RemoveSeqsCommand::readList(){
400 string thisOutputDir = outputDir;
401 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
402 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
405 m->openOutputFile(outputFileName, out);
408 m->openInputFile(listfile, in);
410 bool wroteSomething = false;
413 //read in list vector
416 //make a new list vector
418 newList.setLabel(list.getLabel());
421 for (int i = 0; i < list.getNumBins(); i++) {
422 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
424 //parse out names that are in accnos file
425 string binnames = list.get(i);
427 string newNames = "";
428 while (binnames.find_first_of(',') != -1) {
429 string name = binnames.substr(0,binnames.find_first_of(','));
430 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
432 //if that name is in the .accnos file, add it
433 if (names.count(name) == 0) { newNames += name + ","; }
437 if (names.count(binnames) == 0) { newNames += binnames + ","; }
439 //if there are names in this bin add to new list
440 if (newNames != "") {
441 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
442 newList.push_back(newNames);
446 //print new listvector
447 if (newList.getNumBins() != 0) {
448 wroteSomething = true;
457 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
458 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
463 catch(exception& e) {
464 m->errorOut(e, "RemoveSeqsCommand", "readList");
468 //**********************************************************************************************************************
469 int RemoveSeqsCommand::readName(){
471 string thisOutputDir = outputDir;
472 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
473 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
476 m->openOutputFile(outputFileName, out);
479 m->openInputFile(namefile, in);
480 string name, firstCol, secondCol;
482 bool wroteSomething = false;
485 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
487 in >> firstCol; m->gobble(in);
490 vector<string> parsedNames;
491 //parse second column saving each name
492 while (secondCol.find_first_of(',') != -1) {
493 name = secondCol.substr(0,secondCol.find_first_of(','));
494 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
495 parsedNames.push_back(name);
498 //get name after last ,
499 parsedNames.push_back(secondCol);
501 vector<string> validSecond; validSecond.clear();
502 for (int i = 0; i < parsedNames.size(); i++) {
503 if (names.count(parsedNames[i]) == 0) {
504 validSecond.push_back(parsedNames[i]);
508 if ((dups) && (validSecond.size() != parsedNames.size())) { //if dups is true and we want to get rid of anyone, get rid of everyone
509 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
511 //if the name in the first column is in the set then print it and any other names in second column also in set
512 if (names.count(firstCol) == 0) {
514 wroteSomething = true;
516 out << firstCol << '\t';
518 //you know you have at least one valid second since first column is valid
519 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
520 out << validSecond[validSecond.size()-1] << endl;
522 //make first name in set you come to first column and then add the remaining names to second column
525 //you want part of this row
526 if (validSecond.size() != 0) {
528 wroteSomething = true;
530 out << validSecond[0] << '\t';
532 //you know you have at least one valid second since first column is valid
533 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
534 out << validSecond[validSecond.size()-1] << endl;
543 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
544 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
548 catch(exception& e) {
549 m->errorOut(e, "RemoveSeqsCommand", "readName");
554 //**********************************************************************************************************************
555 int RemoveSeqsCommand::readGroup(){
557 string thisOutputDir = outputDir;
558 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
559 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
562 m->openOutputFile(outputFileName, out);
565 m->openInputFile(groupfile, in);
568 bool wroteSomething = false;
571 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
573 in >> name; //read from first column
574 in >> group; //read from second column
576 //if this name is in the accnos file
577 if (names.count(name) == 0) {
578 wroteSomething = true;
579 out << name << '\t' << group << endl;
587 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
588 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
592 catch(exception& e) {
593 m->errorOut(e, "RemoveSeqsCommand", "readGroup");
597 //**********************************************************************************************************************
598 int RemoveSeqsCommand::readTax(){
600 string thisOutputDir = outputDir;
601 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
602 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
604 m->openOutputFile(outputFileName, out);
607 m->openInputFile(taxfile, in);
610 bool wroteSomething = false;
613 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
615 in >> name; //read from first column
616 in >> tax; //read from second column
618 //if this name is in the accnos file
619 if (names.count(name) == 0) {
620 wroteSomething = true;
621 out << name << '\t' << tax << endl;
629 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
630 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
634 catch(exception& e) {
635 m->errorOut(e, "RemoveSeqsCommand", "readTax");
639 //**********************************************************************************************************************
640 //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
641 int RemoveSeqsCommand::readAlign(){
643 string thisOutputDir = outputDir;
644 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
645 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
648 m->openOutputFile(outputFileName, out);
651 m->openInputFile(alignfile, in);
654 bool wroteSomething = false;
656 //read column headers
657 for (int i = 0; i < 16; i++) {
658 if (!in.eof()) { in >> junk; out << junk << '\t'; }
664 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
666 in >> name; //read from first column
668 //if this name is in the accnos file
669 if (names.count(name) == 0) {
670 wroteSomething = true;
675 for (int i = 0; i < 15; i++) {
676 if (!in.eof()) { in >> junk; out << junk << '\t'; }
681 }else {//still read just don't do anything with it
684 for (int i = 0; i < 15; i++) {
685 if (!in.eof()) { in >> junk; }
695 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
696 outputTypes["alignreport"].push_back(outputFileName); outputNames.push_back(outputFileName);
701 catch(exception& e) {
702 m->errorOut(e, "RemoveSeqsCommand", "readAlign");
706 //**********************************************************************************************************************
707 void RemoveSeqsCommand::readAccnos(){
711 m->openInputFile(accnosfile, in);
724 catch(exception& e) {
725 m->errorOut(e, "RemoveSeqsCommand", "readAccnos");
730 //**********************************************************************************************************************