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"
13 //**********************************************************************************************************************
15 RemoveSeqsCommand::RemoveSeqsCommand(string option){
19 //allow user to run help
20 if(option == "help") { help(); abort = true; }
23 //valid paramters for this command
24 string Array[] = {"fasta","name", "group", "alignreport", "accnos" };
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27 OptionParser parser(option);
28 map<string,string> parameters = parser.getParameters();
30 ValidParameters validParameter;
32 //check to make sure all parameters are valid for command
33 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
34 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
37 //check for required parameters
38 accnosfile = validParameter.validFile(parameters, "accnos", true);
39 if (accnosfile == "not open") { abort = true; }
40 else if (accnosfile == "not found") { accnosfile = ""; mothurOut("You must provide an accnos file."); mothurOutEndLine(); abort = true; }
42 fastafile = validParameter.validFile(parameters, "fasta", true);
43 if (fastafile == "not open") { abort = true; }
44 else if (fastafile == "not found") { fastafile = ""; }
46 namefile = validParameter.validFile(parameters, "name", true);
47 if (namefile == "not open") { abort = true; }
48 else if (namefile == "not found") { namefile = ""; }
50 groupfile = validParameter.validFile(parameters, "group", true);
51 if (groupfile == "not open") { abort = true; }
52 else if (groupfile == "not found") { groupfile = ""; }
54 alignfile = validParameter.validFile(parameters, "alignreport", true);
55 if (alignfile == "not open") { abort = true; }
56 else if (alignfile == "not found") { alignfile = ""; }
58 if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "")) { mothurOut("You must provide one of the following: fasta, name, group, alignreport."); mothurOutEndLine(); abort = true; }
60 if (parameters.size() > 2) { mothurOut("You may only enter one of the following: fasta, name, group, alignreport."); mothurOutEndLine(); abort = true; }
65 errorOut(e, "RemoveSeqsCommand", "RemoveSeqsCommand");
69 //**********************************************************************************************************************
71 void RemoveSeqsCommand::help(){
73 mothurOut("The remove.seqs command reads an .accnos file and one of the following file types: fasta, name, group or alignreport file.\n");
74 mothurOut("It outputs a file containing the sequences NOT in the .accnos file.\n");
75 mothurOut("The remove.seqs command parameters are accnos, fasta, name, group and alignreport. You must provide accnos and one of the other parameters.\n");
76 mothurOut("The remove.seqs command should be in the following format: remove.seqs(accnos=yourAccnos, fasta=yourFasta).\n");
77 mothurOut("Example remove.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n");
78 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
81 errorOut(e, "RemoveSeqsCommand", "help");
86 //**********************************************************************************************************************
88 int RemoveSeqsCommand::execute(){
91 if (abort == true) { return 0; }
93 //get names you want to keep
96 //read through the correct file and output lines you want to keep
97 if (fastafile != "") { readFasta(); }
98 else if (namefile != "") { readName(); }
99 else if (groupfile != "") { readGroup(); }
100 else if (alignfile != "") { readAlign(); }
105 catch(exception& e) {
106 errorOut(e, "RemoveSeqsCommand", "execute");
111 //**********************************************************************************************************************
112 void RemoveSeqsCommand::readFasta(){
114 string outputFileName = getRootName(fastafile) + "pick" + getExtension(fastafile);
116 openOutputFile(outputFileName, out);
119 openInputFile(fastafile, in);
122 bool wroteSomething = false;
125 Sequence currSeq(in);
126 name = currSeq.getName();
129 //if this name is in the accnos file
130 if (names.count(name) == 0) {
131 wroteSomething = true;
133 currSeq.printSequence(out);
134 }else { names.erase(name); }
141 if (wroteSomething == false) {
142 mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
143 remove(outputFileName.c_str());
147 catch(exception& e) {
148 errorOut(e, "RemoveSeqsCommand", "readFasta");
153 //**********************************************************************************************************************
154 void RemoveSeqsCommand::readName(){
157 string outputFileName = getRootName(namefile) + "pick" + getExtension(namefile);
160 openOutputFile(outputFileName, out);
163 openInputFile(namefile, in);
164 string name, firstCol, secondCol;
166 bool wroteSomething = false;
173 vector<string> parsedNames;
174 //parse second column saving each name
175 while (secondCol.find_first_of(',') != -1) {
176 name = secondCol.substr(0,secondCol.find_first_of(','));
177 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
178 parsedNames.push_back(name);
182 //get name after last ,
183 parsedNames.push_back(secondCol);
185 vector<string> validSecond; validSecond.clear();
186 for (int i = 0; i < parsedNames.size(); i++) {
187 if (names.count(parsedNames[i]) == 0) {
188 validSecond.push_back(parsedNames[i]);
192 //if the name in the first column is in the set then print it and any other names in second column also in set
193 if (names.count(firstCol) == 0) {
195 wroteSomething = true;
197 out << firstCol << '\t';
199 //you know you have at least one valid second since first column is valid
200 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
201 out << validSecond[validSecond.size()-1] << endl;
203 //make first name in set you come to first column and then add the remaining names to second column
206 //you want part of this row
207 if (validSecond.size() != 0) {
209 wroteSomething = true;
211 out << validSecond[0] << '\t';
213 //you know you have at least one valid second since first column is valid
214 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
215 out << validSecond[validSecond.size()-1] << endl;
224 if (wroteSomething == false) {
225 mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
226 remove(outputFileName.c_str());
230 catch(exception& e) {
231 errorOut(e, "RemoveSeqsCommand", "readName");
236 //**********************************************************************************************************************
237 void RemoveSeqsCommand::readGroup(){
240 string outputFileName = getRootName(groupfile) + "pick" + getExtension(groupfile);
242 openOutputFile(outputFileName, out);
245 openInputFile(groupfile, in);
248 bool wroteSomething = false;
252 in >> name; //read from first column
253 in >> group; //read from second column
255 //if this name is in the accnos file
256 if (names.count(name) == 0) {
257 wroteSomething = true;
258 out << name << '\t' << group << endl;
259 }else { names.erase(name); }
266 if (wroteSomething == false) {
267 mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
268 remove(outputFileName.c_str());
272 catch(exception& e) {
273 errorOut(e, "RemoveSeqsCommand", "readGroup");
278 //**********************************************************************************************************************
279 //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
280 void RemoveSeqsCommand::readAlign(){
282 string outputFileName = getRootName(getRootName(alignfile)) + "pick.align.report";
284 openOutputFile(outputFileName, out);
287 openInputFile(alignfile, in);
290 bool wroteSomething = false;
292 //read column headers
293 for (int i = 0; i < 16; i++) {
294 if (!in.eof()) { in >> junk; out << junk << '\t'; }
301 in >> name; //read from first column
303 //if this name is in the accnos file
304 if (names.count(name) == 0) {
305 wroteSomething = true;
310 for (int i = 0; i < 15; i++) {
311 if (!in.eof()) { in >> junk; out << junk << '\t'; }
316 }else {//still read just don't do anything with it
320 for (int i = 0; i < 15; i++) {
321 if (!in.eof()) { in >> junk; }
331 if (wroteSomething == false) {
332 mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
333 remove(outputFileName.c_str());
337 catch(exception& e) {
338 errorOut(e, "RemoveSeqsCommand", "readAlign");
342 //**********************************************************************************************************************
343 void RemoveSeqsCommand::readAccnos(){
347 openInputFile(accnosfile, in);
360 catch(exception& e) {
361 errorOut(e, "RemoveSeqsCommand", "readAccnos");
366 //**********************************************************************************************************************