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","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 = 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 = 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 = 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 = 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 = 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 = 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; }
97 //check for required parameters
98 accnosfile = validParameter.validFile(parameters, "accnos", true);
99 if (accnosfile == "not open") { abort = true; }
100 else if (accnosfile == "not found") { accnosfile = ""; mothurOut("You must provide an accnos file."); mothurOutEndLine(); abort = true; }
102 fastafile = validParameter.validFile(parameters, "fasta", true);
103 if (fastafile == "not open") { abort = true; }
104 else if (fastafile == "not found") { fastafile = ""; }
106 namefile = validParameter.validFile(parameters, "name", true);
107 if (namefile == "not open") { abort = true; }
108 else if (namefile == "not found") { namefile = ""; }
110 groupfile = validParameter.validFile(parameters, "group", true);
111 if (groupfile == "not open") { abort = true; }
112 else if (groupfile == "not found") { groupfile = ""; }
114 alignfile = validParameter.validFile(parameters, "alignreport", true);
115 if (alignfile == "not open") { abort = true; }
116 else if (alignfile == "not found") { alignfile = ""; }
118 listfile = validParameter.validFile(parameters, "list", true);
119 if (listfile == "not open") { abort = true; }
120 else if (listfile == "not found") { listfile = ""; }
122 string usedDups = "true";
123 string temp = validParameter.validFile(parameters, "dups", false); if (temp == "not found") { temp = "false"; usedDups = ""; }
126 if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == "")) { mothurOut("You must provide one of the following: fasta, name, group, alignreport or list."); mothurOutEndLine(); abort = true; }
129 if (outputDir != "") { okay++; }
130 if (usedDups != "") { okay++; }
132 if ((usedDups != "") && (namefile == "")) { mothurOut("You may only use dups with the name option."); mothurOutEndLine(); abort = true; }
134 if (parameters.size() > okay) { mothurOut("You may only enter one of the following: fasta, name, group, alignreport, or list."); mothurOutEndLine(); abort = true; }
138 catch(exception& e) {
139 errorOut(e, "RemoveSeqsCommand", "RemoveSeqsCommand");
143 //**********************************************************************************************************************
145 void RemoveSeqsCommand::help(){
147 mothurOut("The remove.seqs command reads an .accnos file and one of the following file types: fasta, name, group, list or alignreport file.\n");
148 mothurOut("It outputs a file containing the sequences NOT in the .accnos file.\n");
149 mothurOut("The remove.seqs command parameters are accnos, fasta, name, group, list, alignreport and dups. You must provide accnos and one of the file parameters.\n");
150 mothurOut("The dups parameter allows you to remove the entire line from a name file if you remove any name from the line. default=false. If dups=true, then remove.seqs outputs a new .accnos file containing all the sequences removed. \n");
151 mothurOut("The remove.seqs command should be in the following format: remove.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
152 mothurOut("Example remove.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
153 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
155 catch(exception& e) {
156 errorOut(e, "RemoveSeqsCommand", "help");
161 //**********************************************************************************************************************
163 int RemoveSeqsCommand::execute(){
166 if (abort == true) { return 0; }
168 //get names you want to keep
171 //read through the correct file and output lines you want to keep
172 if (fastafile != "") { readFasta(); }
173 else if (namefile != "") { readName(); }
174 else if (groupfile != "") { readGroup(); }
175 else if (alignfile != "") { readAlign(); }
176 else if (listfile != "") { readList(); }
181 catch(exception& e) {
182 errorOut(e, "RemoveSeqsCommand", "execute");
187 //**********************************************************************************************************************
188 void RemoveSeqsCommand::readFasta(){
190 if (outputDir == "") { outputDir += hasPath(fastafile); }
191 string outputFileName = getRootName(fastafile) + "pick" + getExtension(fastafile);
193 openOutputFile(outputFileName, out);
196 openInputFile(fastafile, in);
199 bool wroteSomething = false;
202 Sequence currSeq(in);
203 name = currSeq.getName();
206 //if this name is in the accnos file
207 if (names.count(name) == 0) {
208 wroteSomething = true;
210 currSeq.printSequence(out);
211 }else { names.erase(name); }
218 if (wroteSomething == false) {
219 mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
220 remove(outputFileName.c_str());
224 catch(exception& e) {
225 errorOut(e, "RemoveSeqsCommand", "readFasta");
229 //**********************************************************************************************************************
230 void RemoveSeqsCommand::readList(){
232 if (outputDir == "") { outputDir += hasPath(listfile); }
233 string outputFileName = getRootName(listfile) + "pick" + getExtension(listfile);
235 openOutputFile(outputFileName, out);
238 openInputFile(listfile, in);
240 bool wroteSomething = false;
243 //read in list vector
246 //make a new list vector
248 newList.setLabel(list.getLabel());
251 for (int i = 0; i < list.getNumBins(); i++) {
253 //parse out names that are in accnos file
254 string binnames = list.get(i);
256 string newNames = "";
257 while (binnames.find_first_of(',') != -1) {
258 string name = binnames.substr(0,binnames.find_first_of(','));
259 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
261 //if that name is in the .accnos file, add it
262 if (names.count(name) == 0) { newNames += name + ","; }
266 if (names.count(binnames) == 0) { newNames += binnames; }
268 //if there are names in this bin add to new list
269 if (newNames != "") { newList.push_back(newNames); }
272 //print new listvector
273 if (newList.getNumBins() != 0) {
274 wroteSomething = true;
283 if (wroteSomething == false) {
284 mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
285 remove(outputFileName.c_str());
289 catch(exception& e) {
290 errorOut(e, "RemoveSeqsCommand", "readList");
294 //**********************************************************************************************************************
295 void RemoveSeqsCommand::readName(){
297 if (outputDir == "") { outputDir += hasPath(namefile); }
298 string outputFileName = getRootName(namefile) + "pick" + getExtension(namefile);
299 string outputFileName2 = getRootName(namefile) + "dups.accnos";
302 if (dups) { openOutputFile(outputFileName2, out2); }
303 bool wroteDups = false;
306 openOutputFile(outputFileName, out);
309 openInputFile(namefile, in);
310 string name, firstCol, secondCol;
312 bool wroteSomething = false;
319 vector<string> parsedNames;
320 //parse second column saving each name
321 while (secondCol.find_first_of(',') != -1) {
322 name = secondCol.substr(0,secondCol.find_first_of(','));
323 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
324 parsedNames.push_back(name);
328 //get name after last ,
329 parsedNames.push_back(secondCol);
331 vector<string> validSecond; validSecond.clear();
332 for (int i = 0; i < parsedNames.size(); i++) {
333 if (names.count(parsedNames[i]) == 0) {
334 validSecond.push_back(parsedNames[i]);
338 if ((dups) && (validSecond.size() != parsedNames.size())) {
340 for (int i = 0; i < parsedNames.size(); i++) { out2 << parsedNames[i] << endl; }
342 //if the name in the first column is in the set then print it and any other names in second column also in set
343 if (names.count(firstCol) == 0) {
345 wroteSomething = true;
347 out << firstCol << '\t';
349 //you know you have at least one valid second since first column is valid
350 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
351 out << validSecond[validSecond.size()-1] << endl;
353 //make first name in set you come to first column and then add the remaining names to second column
356 //you want part of this row
357 if (validSecond.size() != 0) {
359 wroteSomething = true;
361 out << validSecond[0] << '\t';
363 //you know you have at least one valid second since first column is valid
364 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
365 out << validSecond[validSecond.size()-1] << endl;
374 if (dups) { out2.close(); }
375 if (wroteDups == false) {
376 remove(outputFileName2.c_str());
379 if (wroteSomething == false) {
380 mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
381 remove(outputFileName.c_str());
385 catch(exception& e) {
386 errorOut(e, "RemoveSeqsCommand", "readName");
391 //**********************************************************************************************************************
392 void RemoveSeqsCommand::readGroup(){
394 if (outputDir == "") { outputDir += hasPath(groupfile); }
395 string outputFileName = getRootName(groupfile) + "pick" + getExtension(groupfile);
397 openOutputFile(outputFileName, out);
400 openInputFile(groupfile, in);
403 bool wroteSomething = false;
407 in >> name; //read from first column
408 in >> group; //read from second column
410 //if this name is in the accnos file
411 if (names.count(name) == 0) {
412 wroteSomething = true;
413 out << name << '\t' << group << endl;
414 }else { names.erase(name); }
421 if (wroteSomething == false) {
422 mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
423 remove(outputFileName.c_str());
427 catch(exception& e) {
428 errorOut(e, "RemoveSeqsCommand", "readGroup");
433 //**********************************************************************************************************************
434 //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
435 void RemoveSeqsCommand::readAlign(){
437 if (outputDir == "") { outputDir += hasPath(alignfile); }
438 string outputFileName = getRootName(getRootName(alignfile)) + "pick.align.report";
440 openOutputFile(outputFileName, out);
443 openInputFile(alignfile, in);
446 bool wroteSomething = false;
448 //read column headers
449 for (int i = 0; i < 16; i++) {
450 if (!in.eof()) { in >> junk; out << junk << '\t'; }
457 in >> name; //read from first column
459 //if this name is in the accnos file
460 if (names.count(name) == 0) {
461 wroteSomething = true;
466 for (int i = 0; i < 15; i++) {
467 if (!in.eof()) { in >> junk; out << junk << '\t'; }
472 }else {//still read just don't do anything with it
476 for (int i = 0; i < 15; i++) {
477 if (!in.eof()) { in >> junk; }
487 if (wroteSomething == false) {
488 mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
489 remove(outputFileName.c_str());
493 catch(exception& e) {
494 errorOut(e, "RemoveSeqsCommand", "readAlign");
498 //**********************************************************************************************************************
499 void RemoveSeqsCommand::readAccnos(){
503 openInputFile(accnosfile, in);
516 catch(exception& e) {
517 errorOut(e, "RemoveSeqsCommand", "readAccnos");
522 //**********************************************************************************************************************