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 = 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; }
95 it = parameters.find("taxonomy");
96 //user has given a template file
97 if(it != parameters.end()){
98 path = 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); if (temp == "not found") { temp = "false"; usedDups = ""; }
139 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; }
141 if ((usedDups != "") && (namefile == "")) { m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine(); abort = true; }
145 catch(exception& e) {
146 m->errorOut(e, "RemoveSeqsCommand", "RemoveSeqsCommand");
150 //**********************************************************************************************************************
152 void RemoveSeqsCommand::help(){
154 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");
155 m->mothurOut("It outputs a file containing the sequences NOT in the .accnos file.\n");
156 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");
157 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=false. If dups=true, then remove.seqs outputs a new .accnos file containing all the sequences removed. \n");
158 m->mothurOut("The remove.seqs command should be in the following format: remove.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
159 m->mothurOut("Example remove.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
160 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
162 catch(exception& e) {
163 m->errorOut(e, "RemoveSeqsCommand", "help");
168 //**********************************************************************************************************************
170 int RemoveSeqsCommand::execute(){
173 if (abort == true) { return 0; }
175 //get names you want to keep
178 if (m->control_pressed) { return 0; }
180 //read through the correct file and output lines you want to keep
181 if (fastafile != "") { readFasta(); }
182 if (namefile != "") { readName(); }
183 if (groupfile != "") { readGroup(); }
184 if (alignfile != "") { readAlign(); }
185 if (listfile != "") { readList(); }
186 if (taxfile != "") { readTax(); }
188 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
190 if (outputNames.size() != 0) {
191 m->mothurOutEndLine();
192 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
193 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
194 m->mothurOutEndLine();
200 catch(exception& e) {
201 m->errorOut(e, "RemoveSeqsCommand", "execute");
206 //**********************************************************************************************************************
207 int RemoveSeqsCommand::readFasta(){
209 if (outputDir == "") { outputDir += hasPath(fastafile); }
210 string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "pick" + getExtension(fastafile);
212 openOutputFile(outputFileName, out);
215 openInputFile(fastafile, in);
218 bool wroteSomething = false;
221 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
223 Sequence currSeq(in);
224 name = currSeq.getName();
227 //if this name is in the accnos file
228 if (names.count(name) == 0) {
229 wroteSomething = true;
231 currSeq.printSequence(out);
239 if (wroteSomething == false) {
240 m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();
241 remove(outputFileName.c_str());
242 }else { outputNames.push_back(outputFileName); }
247 catch(exception& e) {
248 m->errorOut(e, "RemoveSeqsCommand", "readFasta");
252 //**********************************************************************************************************************
253 int RemoveSeqsCommand::readList(){
255 if (outputDir == "") { outputDir += hasPath(listfile); }
256 string outputFileName = outputDir + getRootName(getSimpleName(listfile)) + "pick" + getExtension(listfile);
258 openOutputFile(outputFileName, out);
261 openInputFile(listfile, in);
263 bool wroteSomething = false;
266 //read in list vector
269 //make a new list vector
271 newList.setLabel(list.getLabel());
274 for (int i = 0; i < list.getNumBins(); i++) {
275 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
277 //parse out names that are in accnos file
278 string binnames = list.get(i);
280 string newNames = "";
281 while (binnames.find_first_of(',') != -1) {
282 string name = binnames.substr(0,binnames.find_first_of(','));
283 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
285 //if that name is in the .accnos file, add it
286 if (names.count(name) == 0) { newNames += name + ","; }
290 if (names.count(binnames) == 0) { newNames += binnames + ","; }
292 //if there are names in this bin add to new list
293 if (newNames != "") {
294 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
295 newList.push_back(newNames);
299 //print new listvector
300 if (newList.getNumBins() != 0) {
301 wroteSomething = true;
310 if (wroteSomething == false) {
311 m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();
312 remove(outputFileName.c_str());
313 }else { outputNames.push_back(outputFileName); }
318 catch(exception& e) {
319 m->errorOut(e, "RemoveSeqsCommand", "readList");
323 //**********************************************************************************************************************
324 int RemoveSeqsCommand::readName(){
326 if (outputDir == "") { outputDir += hasPath(namefile); }
327 string outputFileName = outputDir + getRootName(getSimpleName(namefile)) + "pick" + getExtension(namefile);
328 string outputFileName2 = outputDir + getRootName(getSimpleName(namefile)) + "dups.accnos";
331 if (dups) { openOutputFile(outputFileName2, out2); }
332 bool wroteDups = false;
335 openOutputFile(outputFileName, out);
338 openInputFile(namefile, in);
339 string name, firstCol, secondCol;
341 bool wroteSomething = false;
344 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); if (dups) { out2.close(); remove(outputFileName2.c_str()); } return 0; }
349 vector<string> parsedNames;
350 //parse second column saving each name
351 while (secondCol.find_first_of(',') != -1) {
352 name = secondCol.substr(0,secondCol.find_first_of(','));
353 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
354 parsedNames.push_back(name);
358 //get name after last ,
359 parsedNames.push_back(secondCol);
361 vector<string> validSecond; validSecond.clear();
362 for (int i = 0; i < parsedNames.size(); i++) {
363 if (names.count(parsedNames[i]) == 0) {
364 validSecond.push_back(parsedNames[i]);
368 if ((dups) && (validSecond.size() != parsedNames.size())) {
370 for (int i = 0; i < parsedNames.size(); i++) { out2 << parsedNames[i] << endl; }
372 //if the name in the first column is in the set then print it and any other names in second column also in set
373 if (names.count(firstCol) == 0) {
375 wroteSomething = true;
377 out << firstCol << '\t';
379 //you know you have at least one valid second since first column is valid
380 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
381 out << validSecond[validSecond.size()-1] << endl;
383 //make first name in set you come to first column and then add the remaining names to second column
386 //you want part of this row
387 if (validSecond.size() != 0) {
389 wroteSomething = true;
391 out << validSecond[0] << '\t';
393 //you know you have at least one valid second since first column is valid
394 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
395 out << validSecond[validSecond.size()-1] << endl;
404 if (dups) { out2.close(); }
405 if (wroteDups == false) {
406 remove(outputFileName2.c_str());
407 }else { outputNames.push_back(outputFileName2); }
409 if (wroteSomething == false) {
410 m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();
411 remove(outputFileName.c_str());
412 }else { outputNames.push_back(outputFileName); }
416 catch(exception& e) {
417 m->errorOut(e, "RemoveSeqsCommand", "readName");
422 //**********************************************************************************************************************
423 int RemoveSeqsCommand::readGroup(){
425 if (outputDir == "") { outputDir += hasPath(groupfile); }
426 string outputFileName = outputDir + getRootName(getSimpleName(groupfile)) + "pick" + getExtension(groupfile);
428 openOutputFile(outputFileName, out);
431 openInputFile(groupfile, in);
434 bool wroteSomething = false;
437 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
439 in >> name; //read from first column
440 in >> group; //read from second column
442 //if this name is in the accnos file
443 if (names.count(name) == 0) {
444 wroteSomething = true;
445 out << name << '\t' << group << endl;
453 if (wroteSomething == false) {
454 m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();
455 remove(outputFileName.c_str());
456 }else { outputNames.push_back(outputFileName); }
460 catch(exception& e) {
461 m->errorOut(e, "RemoveSeqsCommand", "readGroup");
465 //**********************************************************************************************************************
466 int RemoveSeqsCommand::readTax(){
468 if (outputDir == "") { outputDir += hasPath(taxfile); }
469 string outputFileName = outputDir + getRootName(getSimpleName(taxfile)) + "pick" + getExtension(taxfile);
471 openOutputFile(outputFileName, out);
474 openInputFile(taxfile, in);
477 bool wroteSomething = false;
480 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
482 in >> name; //read from first column
483 in >> tax; //read from second column
485 //if this name is in the accnos file
486 if (names.count(name) == 0) {
487 wroteSomething = true;
488 out << name << '\t' << tax << endl;
496 if (wroteSomething == false) {
497 m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();
498 remove(outputFileName.c_str());
499 }else { outputNames.push_back(outputFileName); }
503 catch(exception& e) {
504 m->errorOut(e, "RemoveSeqsCommand", "readTax");
508 //**********************************************************************************************************************
509 //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
510 int RemoveSeqsCommand::readAlign(){
512 if (outputDir == "") { outputDir += hasPath(alignfile); }
513 string outputFileName = outputDir + getRootName(getSimpleName(alignfile)) + "pick.align.report";
515 openOutputFile(outputFileName, out);
518 openInputFile(alignfile, in);
521 bool wroteSomething = false;
523 //read column headers
524 for (int i = 0; i < 16; i++) {
525 if (!in.eof()) { in >> junk; out << junk << '\t'; }
531 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
533 in >> name; //read from first column
535 //if this name is in the accnos file
536 if (names.count(name) == 0) {
537 wroteSomething = true;
542 for (int i = 0; i < 15; i++) {
543 if (!in.eof()) { in >> junk; out << junk << '\t'; }
548 }else {//still read just don't do anything with it
551 for (int i = 0; i < 15; i++) {
552 if (!in.eof()) { in >> junk; }
562 if (wroteSomething == false) {
563 m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();
564 remove(outputFileName.c_str());
565 }else { outputNames.push_back(outputFileName); }
570 catch(exception& e) {
571 m->errorOut(e, "RemoveSeqsCommand", "readAlign");
575 //**********************************************************************************************************************
576 void RemoveSeqsCommand::readAccnos(){
580 openInputFile(accnosfile, in);
593 catch(exception& e) {
594 m->errorOut(e, "RemoveSeqsCommand", "readAccnos");
599 //**********************************************************************************************************************