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 = ""; m->mothurOut("You must provide an accnos file."); m->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 == "")) { m->mothurOut("You must provide one of the following: fasta, name, group, alignreport or list."); m->mothurOutEndLine(); abort = true; }
129 if (outputDir != "") { okay++; }
130 if (usedDups != "") { okay++; }
132 if ((usedDups != "") && (namefile == "")) { m->mothurOut("You may only use dups with the name option."); m->mothurOutEndLine(); abort = true; }
134 if (parameters.size() > okay) { m->mothurOut("You may only enter one of the following: fasta, name, group, alignreport, or list."); m->mothurOutEndLine(); abort = true; }
138 catch(exception& e) {
139 m->errorOut(e, "RemoveSeqsCommand", "RemoveSeqsCommand");
143 //**********************************************************************************************************************
145 void RemoveSeqsCommand::help(){
147 m->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 m->mothurOut("It outputs a file containing the sequences NOT in the .accnos file.\n");
149 m->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 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");
151 m->mothurOut("The remove.seqs command should be in the following format: remove.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
152 m->mothurOut("Example remove.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
153 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
155 catch(exception& e) {
156 m->errorOut(e, "RemoveSeqsCommand", "help");
161 //**********************************************************************************************************************
163 int RemoveSeqsCommand::execute(){
166 if (abort == true) { return 0; }
168 //get names you want to keep
171 if (m->control_pressed) { return 0; }
173 //read through the correct file and output lines you want to keep
174 if (fastafile != "") { readFasta(); }
175 else if (namefile != "") { readName(); }
176 else if (groupfile != "") { readGroup(); }
177 else if (alignfile != "") { readAlign(); }
178 else if (listfile != "") { readList(); }
180 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
182 if (outputNames.size() != 0) {
183 m->mothurOutEndLine();
184 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
185 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
186 m->mothurOutEndLine();
192 catch(exception& e) {
193 m->errorOut(e, "RemoveSeqsCommand", "execute");
198 //**********************************************************************************************************************
199 int RemoveSeqsCommand::readFasta(){
201 if (outputDir == "") { outputDir += hasPath(fastafile); }
202 string outputFileName = getRootName(fastafile) + "pick" + getExtension(fastafile);
204 openOutputFile(outputFileName, out);
207 openInputFile(fastafile, in);
210 bool wroteSomething = false;
213 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
215 Sequence currSeq(in);
216 name = currSeq.getName();
219 //if this name is in the accnos file
220 if (names.count(name) == 0) {
221 wroteSomething = true;
223 currSeq.printSequence(out);
224 }else { names.erase(name); }
231 if (wroteSomething == false) {
232 m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();
233 remove(outputFileName.c_str());
234 }else { outputNames.push_back(outputFileName); }
239 catch(exception& e) {
240 m->errorOut(e, "RemoveSeqsCommand", "readFasta");
244 //**********************************************************************************************************************
245 int RemoveSeqsCommand::readList(){
247 if (outputDir == "") { outputDir += hasPath(listfile); }
248 string outputFileName = getRootName(listfile) + "pick" + getExtension(listfile);
250 openOutputFile(outputFileName, out);
253 openInputFile(listfile, in);
255 bool wroteSomething = false;
258 //read in list vector
261 //make a new list vector
263 newList.setLabel(list.getLabel());
266 for (int i = 0; i < list.getNumBins(); i++) {
267 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
269 //parse out names that are in accnos file
270 string binnames = list.get(i);
272 string newNames = "";
273 while (binnames.find_first_of(',') != -1) {
274 string name = binnames.substr(0,binnames.find_first_of(','));
275 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
277 //if that name is in the .accnos file, add it
278 if (names.count(name) == 0) { newNames += name + ","; }
282 if (names.count(binnames) == 0) { newNames += binnames; }
284 //if there are names in this bin add to new list
285 if (newNames != "") { newList.push_back(newNames); }
288 //print new listvector
289 if (newList.getNumBins() != 0) {
290 wroteSomething = true;
299 if (wroteSomething == false) {
300 m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();
301 remove(outputFileName.c_str());
302 }else { outputNames.push_back(outputFileName); }
307 catch(exception& e) {
308 m->errorOut(e, "RemoveSeqsCommand", "readList");
312 //**********************************************************************************************************************
313 int RemoveSeqsCommand::readName(){
315 if (outputDir == "") { outputDir += hasPath(namefile); }
316 string outputFileName = getRootName(namefile) + "pick" + getExtension(namefile);
317 string outputFileName2 = getRootName(namefile) + "dups.accnos";
320 if (dups) { openOutputFile(outputFileName2, out2); }
321 bool wroteDups = false;
324 openOutputFile(outputFileName, out);
327 openInputFile(namefile, in);
328 string name, firstCol, secondCol;
330 bool wroteSomething = false;
333 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); if (dups) { out2.close(); remove(outputFileName2.c_str()); } return 0; }
338 vector<string> parsedNames;
339 //parse second column saving each name
340 while (secondCol.find_first_of(',') != -1) {
341 name = secondCol.substr(0,secondCol.find_first_of(','));
342 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
343 parsedNames.push_back(name);
347 //get name after last ,
348 parsedNames.push_back(secondCol);
350 vector<string> validSecond; validSecond.clear();
351 for (int i = 0; i < parsedNames.size(); i++) {
352 if (names.count(parsedNames[i]) == 0) {
353 validSecond.push_back(parsedNames[i]);
357 if ((dups) && (validSecond.size() != parsedNames.size())) {
359 for (int i = 0; i < parsedNames.size(); i++) { out2 << parsedNames[i] << endl; }
361 //if the name in the first column is in the set then print it and any other names in second column also in set
362 if (names.count(firstCol) == 0) {
364 wroteSomething = true;
366 out << firstCol << '\t';
368 //you know you have at least one valid second since first column is valid
369 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
370 out << validSecond[validSecond.size()-1] << endl;
372 //make first name in set you come to first column and then add the remaining names to second column
375 //you want part of this row
376 if (validSecond.size() != 0) {
378 wroteSomething = true;
380 out << validSecond[0] << '\t';
382 //you know you have at least one valid second since first column is valid
383 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
384 out << validSecond[validSecond.size()-1] << endl;
393 if (dups) { out2.close(); }
394 if (wroteDups == false) {
395 remove(outputFileName2.c_str());
396 }else { outputNames.push_back(outputFileName2); }
398 if (wroteSomething == false) {
399 m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();
400 remove(outputFileName.c_str());
401 }else { outputNames.push_back(outputFileName); }
405 catch(exception& e) {
406 m->errorOut(e, "RemoveSeqsCommand", "readName");
411 //**********************************************************************************************************************
412 int RemoveSeqsCommand::readGroup(){
414 if (outputDir == "") { outputDir += hasPath(groupfile); }
415 string outputFileName = getRootName(groupfile) + "pick" + getExtension(groupfile);
417 openOutputFile(outputFileName, out);
420 openInputFile(groupfile, in);
423 bool wroteSomething = false;
426 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
428 in >> name; //read from first column
429 in >> group; //read from second column
431 //if this name is in the accnos file
432 if (names.count(name) == 0) {
433 wroteSomething = true;
434 out << name << '\t' << group << endl;
435 }else { names.erase(name); }
442 if (wroteSomething == false) {
443 m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();
444 remove(outputFileName.c_str());
445 }else { outputNames.push_back(outputFileName); }
449 catch(exception& e) {
450 m->errorOut(e, "RemoveSeqsCommand", "readGroup");
455 //**********************************************************************************************************************
456 //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
457 int RemoveSeqsCommand::readAlign(){
459 if (outputDir == "") { outputDir += hasPath(alignfile); }
460 string outputFileName = getRootName(getRootName(alignfile)) + "pick.align.report";
462 openOutputFile(outputFileName, out);
465 openInputFile(alignfile, in);
468 bool wroteSomething = false;
470 //read column headers
471 for (int i = 0; i < 16; i++) {
472 if (!in.eof()) { in >> junk; out << junk << '\t'; }
478 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
480 in >> name; //read from first column
482 //if this name is in the accnos file
483 if (names.count(name) == 0) {
484 wroteSomething = true;
489 for (int i = 0; i < 15; i++) {
490 if (!in.eof()) { in >> junk; out << junk << '\t'; }
495 }else {//still read just don't do anything with it
499 for (int i = 0; i < 15; i++) {
500 if (!in.eof()) { in >> junk; }
510 if (wroteSomething == false) {
511 m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine();
512 remove(outputFileName.c_str());
513 }else { outputNames.push_back(outputFileName); }
518 catch(exception& e) {
519 m->errorOut(e, "RemoveSeqsCommand", "readAlign");
523 //**********************************************************************************************************************
524 void RemoveSeqsCommand::readAccnos(){
528 openInputFile(accnosfile, in);
541 catch(exception& e) {
542 m->errorOut(e, "RemoveSeqsCommand", "readAccnos");
547 //**********************************************************************************************************************