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 //**********************************************************************************************************************
16 RemoveSeqsCommand::RemoveSeqsCommand(string option) {
20 //allow user to run help
21 if(option == "help") { help(); abort = true; }
24 //valid paramters for this command
25 string Array[] = {"fasta","name", "group", "alignreport", "accnos", "list","taxonomy","outputdir","inputdir", "dups" };
26 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
28 OptionParser parser(option);
29 map<string,string> parameters = parser.getParameters();
31 ValidParameters validParameter;
32 map<string,string>::iterator it;
34 //check to make sure all parameters are valid for command
35 for (it = parameters.begin(); it != parameters.end(); it++) {
36 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
39 //if the user changes the output directory command factory will send this info to us in the output parameter
40 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
42 //if the user changes the input directory command factory will send this info to us in the output parameter
43 string inputDir = validParameter.validFile(parameters, "inputdir", false);
44 if (inputDir == "not found"){ inputDir = ""; }
47 it = parameters.find("alignreport");
48 //user has given a template file
49 if(it != parameters.end()){
50 path = m->hasPath(it->second);
51 //if the user has not given a path then, add inputdir. else leave path alone.
52 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
55 it = parameters.find("fasta");
56 //user has given a template file
57 if(it != parameters.end()){
58 path = m->hasPath(it->second);
59 //if the user has not given a path then, add inputdir. else leave path alone.
60 if (path == "") { parameters["fasta"] = inputDir + it->second; }
63 it = parameters.find("accnos");
64 //user has given a template file
65 if(it != parameters.end()){
66 path = m->hasPath(it->second);
67 //if the user has not given a path then, add inputdir. else leave path alone.
68 if (path == "") { parameters["accnos"] = inputDir + it->second; }
71 it = parameters.find("list");
72 //user has given a template file
73 if(it != parameters.end()){
74 path = m->hasPath(it->second);
75 //if the user has not given a path then, add inputdir. else leave path alone.
76 if (path == "") { parameters["list"] = inputDir + it->second; }
79 it = parameters.find("name");
80 //user has given a template file
81 if(it != parameters.end()){
82 path = m->hasPath(it->second);
83 //if the user has not given a path then, add inputdir. else leave path alone.
84 if (path == "") { parameters["name"] = inputDir + it->second; }
87 it = parameters.find("group");
88 //user has given a template file
89 if(it != parameters.end()){
90 path = m->hasPath(it->second);
91 //if the user has not given a path then, add inputdir. else leave path alone.
92 if (path == "") { parameters["group"] = inputDir + it->second; }
95 it = parameters.find("taxonomy");
96 //user has given a template file
97 if(it != parameters.end()){
98 path = m->hasPath(it->second);
99 //if the user has not given a path then, add inputdir. else leave path alone.
100 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
105 //check for required parameters
106 accnosfile = validParameter.validFile(parameters, "accnos", true);
107 if (accnosfile == "not open") { abort = true; }
108 else if (accnosfile == "not found") { accnosfile = ""; m->mothurOut("You must provide an accnos file."); m->mothurOutEndLine(); abort = true; }
110 fastafile = validParameter.validFile(parameters, "fasta", true);
111 if (fastafile == "not open") { abort = true; }
112 else if (fastafile == "not found") { fastafile = ""; }
114 namefile = validParameter.validFile(parameters, "name", true);
115 if (namefile == "not open") { abort = true; }
116 else if (namefile == "not found") { namefile = ""; }
118 groupfile = validParameter.validFile(parameters, "group", true);
119 if (groupfile == "not open") { abort = true; }
120 else if (groupfile == "not found") { groupfile = ""; }
122 alignfile = validParameter.validFile(parameters, "alignreport", true);
123 if (alignfile == "not open") { abort = true; }
124 else if (alignfile == "not found") { alignfile = ""; }
126 listfile = validParameter.validFile(parameters, "list", true);
127 if (listfile == "not open") { abort = true; }
128 else if (listfile == "not found") { listfile = ""; }
130 taxfile = validParameter.validFile(parameters, "taxonomy", true);
131 if (taxfile == "not open") { abort = true; }
132 else if (taxfile == "not found") { taxfile = ""; }
135 string usedDups = "true";
136 string temp = validParameter.validFile(parameters, "dups", false);
137 if (temp == "not found") {
138 if (namefile != "") { temp = "true"; }
139 else { temp = "false"; usedDups = ""; }
141 dups = m->isTrue(temp);
143 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; }
145 if ((usedDups != "") && (namefile == "")) { m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine(); abort = true; }
149 catch(exception& e) {
150 m->errorOut(e, "RemoveSeqsCommand", "RemoveSeqsCommand");
154 //**********************************************************************************************************************
156 void RemoveSeqsCommand::help(){
158 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");
159 m->mothurOut("It outputs a file containing the sequences NOT in the .accnos file.\n");
160 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");
161 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");
162 m->mothurOut("The remove.seqs command should be in the following format: remove.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
163 m->mothurOut("Example remove.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
164 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
166 catch(exception& e) {
167 m->errorOut(e, "RemoveSeqsCommand", "help");
172 //**********************************************************************************************************************
174 int RemoveSeqsCommand::execute(){
177 if (abort == true) { return 0; }
179 //get names you want to keep
182 if (m->control_pressed) { return 0; }
184 //read through the correct file and output lines you want to keep
185 if (namefile != "") { readName(); }
186 if (fastafile != "") { readFasta(); }
187 if (groupfile != "") { readGroup(); }
188 if (alignfile != "") { readAlign(); }
189 if (listfile != "") { readList(); }
190 if (taxfile != "") { readTax(); }
192 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
194 if (outputNames.size() != 0) {
195 m->mothurOutEndLine();
196 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
197 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
198 m->mothurOutEndLine();
204 catch(exception& e) {
205 m->errorOut(e, "RemoveSeqsCommand", "execute");
210 //**********************************************************************************************************************
211 int RemoveSeqsCommand::readFasta(){
213 if (outputDir == "") { outputDir += m->hasPath(fastafile); }
214 string outputFileName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
216 m->openOutputFile(outputFileName, out);
219 m->openInputFile(fastafile, in);
222 bool wroteSomething = false;
225 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
227 Sequence currSeq(in);
228 name = currSeq.getName();
231 //if this name is in the accnos file
232 if (names.count(name) == 0) {
233 wroteSomething = true;
235 currSeq.printSequence(out);
243 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
244 outputNames.push_back(outputFileName);
249 catch(exception& e) {
250 m->errorOut(e, "RemoveSeqsCommand", "readFasta");
254 //**********************************************************************************************************************
255 int RemoveSeqsCommand::readList(){
257 if (outputDir == "") { outputDir += m->hasPath(listfile); }
258 string outputFileName = outputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
260 m->openOutputFile(outputFileName, out);
263 m->openInputFile(listfile, in);
265 bool wroteSomething = false;
268 //read in list vector
271 //make a new list vector
273 newList.setLabel(list.getLabel());
276 for (int i = 0; i < list.getNumBins(); i++) {
277 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
279 //parse out names that are in accnos file
280 string binnames = list.get(i);
282 string newNames = "";
283 while (binnames.find_first_of(',') != -1) {
284 string name = binnames.substr(0,binnames.find_first_of(','));
285 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
287 //if that name is in the .accnos file, add it
288 if (names.count(name) == 0) { newNames += name + ","; }
292 if (names.count(binnames) == 0) { newNames += binnames + ","; }
294 //if there are names in this bin add to new list
295 if (newNames != "") {
296 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
297 newList.push_back(newNames);
301 //print new listvector
302 if (newList.getNumBins() != 0) {
303 wroteSomething = true;
312 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
313 outputNames.push_back(outputFileName);
318 catch(exception& e) {
319 m->errorOut(e, "RemoveSeqsCommand", "readList");
323 //**********************************************************************************************************************
324 int RemoveSeqsCommand::readName(){
326 if (outputDir == "") { outputDir += m->hasPath(namefile); }
327 string outputFileName = outputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
330 m->openOutputFile(outputFileName, out);
333 m->openInputFile(namefile, in);
334 string name, firstCol, secondCol;
336 bool wroteSomething = false;
339 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
344 vector<string> parsedNames;
345 //parse second column saving each name
346 while (secondCol.find_first_of(',') != -1) {
347 name = secondCol.substr(0,secondCol.find_first_of(','));
348 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
349 parsedNames.push_back(name);
352 //get name after last ,
353 parsedNames.push_back(secondCol);
355 vector<string> validSecond; validSecond.clear();
356 for (int i = 0; i < parsedNames.size(); i++) {
357 if (names.count(parsedNames[i]) == 0) {
358 validSecond.push_back(parsedNames[i]);
362 if ((dups) && (validSecond.size() != parsedNames.size())) { //if dups is true and we want to get rid of anyone, get rid of everyone
363 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
365 //if the name in the first column is in the set then print it and any other names in second column also in set
366 if (names.count(firstCol) == 0) {
368 wroteSomething = true;
370 out << firstCol << '\t';
372 //you know you have at least one valid second since first column is valid
373 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
374 out << validSecond[validSecond.size()-1] << endl;
376 //make first name in set you come to first column and then add the remaining names to second column
379 //you want part of this row
380 if (validSecond.size() != 0) {
382 wroteSomething = true;
384 out << validSecond[0] << '\t';
386 //you know you have at least one valid second since first column is valid
387 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
388 out << validSecond[validSecond.size()-1] << endl;
397 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
398 outputNames.push_back(outputFileName);
402 catch(exception& e) {
403 m->errorOut(e, "RemoveSeqsCommand", "readName");
408 //**********************************************************************************************************************
409 int RemoveSeqsCommand::readGroup(){
411 if (outputDir == "") { outputDir += m->hasPath(groupfile); }
412 string outputFileName = outputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
414 m->openOutputFile(outputFileName, out);
417 m->openInputFile(groupfile, in);
420 bool wroteSomething = false;
423 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
425 in >> name; //read from first column
426 in >> group; //read from second column
428 //if this name is in the accnos file
429 if (names.count(name) == 0) {
430 wroteSomething = true;
431 out << name << '\t' << group << endl;
439 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
440 outputNames.push_back(outputFileName);
444 catch(exception& e) {
445 m->errorOut(e, "RemoveSeqsCommand", "readGroup");
449 //**********************************************************************************************************************
450 int RemoveSeqsCommand::readTax(){
452 if (outputDir == "") { outputDir += m->hasPath(taxfile); }
453 string outputFileName = outputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
455 m->openOutputFile(outputFileName, out);
458 m->openInputFile(taxfile, in);
461 bool wroteSomething = false;
464 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
466 in >> name; //read from first column
467 in >> tax; //read from second column
469 //if this name is in the accnos file
470 if (names.count(name) == 0) {
471 wroteSomething = true;
472 out << name << '\t' << tax << endl;
480 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
481 outputNames.push_back(outputFileName);
485 catch(exception& e) {
486 m->errorOut(e, "RemoveSeqsCommand", "readTax");
490 //**********************************************************************************************************************
491 //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
492 int RemoveSeqsCommand::readAlign(){
494 if (outputDir == "") { outputDir += m->hasPath(alignfile); }
495 string outputFileName = outputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
497 m->openOutputFile(outputFileName, out);
500 m->openInputFile(alignfile, in);
503 bool wroteSomething = false;
505 //read column headers
506 for (int i = 0; i < 16; i++) {
507 if (!in.eof()) { in >> junk; out << junk << '\t'; }
513 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
515 in >> name; //read from first column
517 //if this name is in the accnos file
518 if (names.count(name) == 0) {
519 wroteSomething = true;
524 for (int i = 0; i < 15; i++) {
525 if (!in.eof()) { in >> junk; out << junk << '\t'; }
530 }else {//still read just don't do anything with it
533 for (int i = 0; i < 15; i++) {
534 if (!in.eof()) { in >> junk; }
544 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
545 outputNames.push_back(outputFileName);
550 catch(exception& e) {
551 m->errorOut(e, "RemoveSeqsCommand", "readAlign");
555 //**********************************************************************************************************************
556 void RemoveSeqsCommand::readAccnos(){
560 m->openInputFile(accnosfile, in);
573 catch(exception& e) {
574 m->errorOut(e, "RemoveSeqsCommand", "readAccnos");
579 //**********************************************************************************************************************