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" };
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 if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == "")) { mothurOut("You must provide one of the following: fasta, name, group, alignreport or list."); mothurOutEndLine(); abort = true; }
125 if (outputDir != "") { okay++; }
127 if (parameters.size() > okay) { mothurOut("You may only enter one of the following: fasta, name, group, alignreport, or list."); mothurOutEndLine(); abort = true; }
131 catch(exception& e) {
132 errorOut(e, "RemoveSeqsCommand", "RemoveSeqsCommand");
136 //**********************************************************************************************************************
138 void RemoveSeqsCommand::help(){
140 mothurOut("The remove.seqs command reads an .accnos file and one of the following file types: fasta, name, group, list or alignreport file.\n");
141 mothurOut("It outputs a file containing the sequences NOT in the .accnos file.\n");
142 mothurOut("The remove.seqs command parameters are accnos, fasta, name, group, list and alignreport. You must provide accnos and one of the other parameters.\n");
143 mothurOut("The remove.seqs command should be in the following format: remove.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
144 mothurOut("Example remove.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
145 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
147 catch(exception& e) {
148 errorOut(e, "RemoveSeqsCommand", "help");
153 //**********************************************************************************************************************
155 int RemoveSeqsCommand::execute(){
158 if (abort == true) { return 0; }
160 //get names you want to keep
163 //read through the correct file and output lines you want to keep
164 if (fastafile != "") { readFasta(); }
165 else if (namefile != "") { readName(); }
166 else if (groupfile != "") { readGroup(); }
167 else if (alignfile != "") { readAlign(); }
168 else if (listfile != "") { readList(); }
173 catch(exception& e) {
174 errorOut(e, "RemoveSeqsCommand", "execute");
179 //**********************************************************************************************************************
180 void RemoveSeqsCommand::readFasta(){
182 if (outputDir == "") { outputDir += hasPath(fastafile); }
183 string outputFileName = getRootName(fastafile) + "pick" + getExtension(fastafile);
185 openOutputFile(outputFileName, out);
188 openInputFile(fastafile, in);
191 bool wroteSomething = false;
194 Sequence currSeq(in);
195 name = currSeq.getName();
198 //if this name is in the accnos file
199 if (names.count(name) == 0) {
200 wroteSomething = true;
202 currSeq.printSequence(out);
203 }else { names.erase(name); }
210 if (wroteSomething == false) {
211 mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
212 remove(outputFileName.c_str());
216 catch(exception& e) {
217 errorOut(e, "RemoveSeqsCommand", "readFasta");
221 //**********************************************************************************************************************
222 void RemoveSeqsCommand::readList(){
224 if (outputDir == "") { outputDir += hasPath(listfile); }
225 string outputFileName = getRootName(listfile) + "pick" + getExtension(listfile);
227 openOutputFile(outputFileName, out);
230 openInputFile(listfile, in);
232 bool wroteSomething = false;
235 //read in list vector
238 //make a new list vector
240 newList.setLabel(list.getLabel());
243 for (int i = 0; i < list.getNumBins(); i++) {
245 //parse out names that are in accnos file
246 string binnames = list.get(i);
248 string newNames = "";
249 while (binnames.find_first_of(',') != -1) {
250 string name = binnames.substr(0,binnames.find_first_of(','));
251 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
253 //if that name is in the .accnos file, add it
254 if (names.count(name) == 0) { newNames += name + ","; }
258 if (names.count(binnames) == 0) { newNames += binnames; }
260 //if there are names in this bin add to new list
261 if (newNames != "") { newList.push_back(newNames); }
264 //print new listvector
265 if (newList.getNumBins() != 0) {
266 wroteSomething = true;
275 if (wroteSomething == false) {
276 mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
277 remove(outputFileName.c_str());
281 catch(exception& e) {
282 errorOut(e, "RemoveSeqsCommand", "readList");
286 //**********************************************************************************************************************
287 void RemoveSeqsCommand::readName(){
289 if (outputDir == "") { outputDir += hasPath(namefile); }
290 string outputFileName = getRootName(namefile) + "pick" + getExtension(namefile);
293 openOutputFile(outputFileName, out);
296 openInputFile(namefile, in);
297 string name, firstCol, secondCol;
299 bool wroteSomething = false;
306 vector<string> parsedNames;
307 //parse second column saving each name
308 while (secondCol.find_first_of(',') != -1) {
309 name = secondCol.substr(0,secondCol.find_first_of(','));
310 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
311 parsedNames.push_back(name);
315 //get name after last ,
316 parsedNames.push_back(secondCol);
318 vector<string> validSecond; validSecond.clear();
319 for (int i = 0; i < parsedNames.size(); i++) {
320 if (names.count(parsedNames[i]) == 0) {
321 validSecond.push_back(parsedNames[i]);
325 //if the name in the first column is in the set then print it and any other names in second column also in set
326 if (names.count(firstCol) == 0) {
328 wroteSomething = true;
330 out << firstCol << '\t';
332 //you know you have at least one valid second since first column is valid
333 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
334 out << validSecond[validSecond.size()-1] << endl;
336 //make first name in set you come to first column and then add the remaining names to second column
339 //you want part of this row
340 if (validSecond.size() != 0) {
342 wroteSomething = true;
344 out << validSecond[0] << '\t';
346 //you know you have at least one valid second since first column is valid
347 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
348 out << validSecond[validSecond.size()-1] << endl;
357 if (wroteSomething == false) {
358 mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
359 remove(outputFileName.c_str());
363 catch(exception& e) {
364 errorOut(e, "RemoveSeqsCommand", "readName");
369 //**********************************************************************************************************************
370 void RemoveSeqsCommand::readGroup(){
372 if (outputDir == "") { outputDir += hasPath(groupfile); }
373 string outputFileName = getRootName(groupfile) + "pick" + getExtension(groupfile);
375 openOutputFile(outputFileName, out);
378 openInputFile(groupfile, in);
381 bool wroteSomething = false;
385 in >> name; //read from first column
386 in >> group; //read from second column
388 //if this name is in the accnos file
389 if (names.count(name) == 0) {
390 wroteSomething = true;
391 out << name << '\t' << group << endl;
392 }else { names.erase(name); }
399 if (wroteSomething == false) {
400 mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
401 remove(outputFileName.c_str());
405 catch(exception& e) {
406 errorOut(e, "RemoveSeqsCommand", "readGroup");
411 //**********************************************************************************************************************
412 //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
413 void RemoveSeqsCommand::readAlign(){
415 if (outputDir == "") { outputDir += hasPath(alignfile); }
416 string outputFileName = getRootName(getRootName(alignfile)) + "pick.align.report";
418 openOutputFile(outputFileName, out);
421 openInputFile(alignfile, in);
424 bool wroteSomething = false;
426 //read column headers
427 for (int i = 0; i < 16; i++) {
428 if (!in.eof()) { in >> junk; out << junk << '\t'; }
435 in >> name; //read from first column
437 //if this name is in the accnos file
438 if (names.count(name) == 0) {
439 wroteSomething = true;
444 for (int i = 0; i < 15; i++) {
445 if (!in.eof()) { in >> junk; out << junk << '\t'; }
450 }else {//still read just don't do anything with it
454 for (int i = 0; i < 15; i++) {
455 if (!in.eof()) { in >> junk; }
465 if (wroteSomething == false) {
466 mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
467 remove(outputFileName.c_str());
471 catch(exception& e) {
472 errorOut(e, "RemoveSeqsCommand", "readAlign");
476 //**********************************************************************************************************************
477 void RemoveSeqsCommand::readAccnos(){
481 openInputFile(accnosfile, in);
494 catch(exception& e) {
495 errorOut(e, "RemoveSeqsCommand", "readAccnos");
500 //**********************************************************************************************************************