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();
128 //if this name is in the accnos file
129 if (names.count(name) == 0) {
130 wroteSomething = true;
132 currSeq.printSequence(out);
133 }else { names.erase(name); }
140 if (wroteSomething == false) {
141 mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
142 remove(outputFileName.c_str());
146 catch(exception& e) {
147 errorOut(e, "RemoveSeqsCommand", "readFasta");
152 //**********************************************************************************************************************
153 void RemoveSeqsCommand::readName(){
156 string outputFileName = getRootName(namefile) + "pick" + getExtension(namefile);
159 openOutputFile(outputFileName, out);
162 openInputFile(namefile, in);
163 string name, firstCol, secondCol;
165 bool wroteSomething = false;
172 vector<string> parsedNames;
173 //parse second column saving each name
174 while (secondCol.find_first_of(',') != -1) {
175 name = secondCol.substr(0,secondCol.find_first_of(','));
176 secondCol = secondCol.substr(secondCol.find_first_of(',')+1, secondCol.length());
177 parsedNames.push_back(name);
181 //get name after last ,
182 parsedNames.push_back(secondCol);
184 vector<string> validSecond; validSecond.clear();
185 for (int i = 0; i < parsedNames.size(); i++) {
186 if (names.count(parsedNames[i]) == 0) {
187 validSecond.push_back(parsedNames[i]);
191 //if the name in the first column is in the set then print it and any other names in second column also in set
192 if (names.count(firstCol) == 0) {
194 wroteSomething = true;
196 out << firstCol << '\t';
198 //you know you have at least one valid second since first column is valid
199 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
200 out << validSecond[validSecond.size()-1] << endl;
202 //make first name in set you come to first column and then add the remaining names to second column
205 //you want part of this row
206 if (validSecond.size() != 0) {
208 wroteSomething = true;
210 out << validSecond[0] << '\t';
212 //you know you have at least one valid second since first column is valid
213 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
214 out << validSecond[validSecond.size()-1] << endl;
223 if (wroteSomething == false) {
224 mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
225 remove(outputFileName.c_str());
229 catch(exception& e) {
230 errorOut(e, "RemoveSeqsCommand", "readName");
235 //**********************************************************************************************************************
236 void RemoveSeqsCommand::readGroup(){
239 string outputFileName = getRootName(groupfile) + "pick" + getExtension(groupfile);
241 openOutputFile(outputFileName, out);
244 openInputFile(groupfile, in);
247 bool wroteSomething = false;
251 in >> name; //read from first column
252 in >> group; //read from second column
254 //if this name is in the accnos file
255 if (names.count(name) == 0) {
256 wroteSomething = true;
257 out << name << '\t' << group << endl;
258 }else { names.erase(name); }
265 if (wroteSomething == false) {
266 mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
267 remove(outputFileName.c_str());
271 catch(exception& e) {
272 errorOut(e, "RemoveSeqsCommand", "readGroup");
277 //**********************************************************************************************************************
278 //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
279 void RemoveSeqsCommand::readAlign(){
281 string outputFileName = getRootName(getRootName(alignfile)) + "pick.align.report";
283 openOutputFile(outputFileName, out);
286 openInputFile(alignfile, in);
289 bool wroteSomething = false;
291 //read column headers
292 for (int i = 0; i < 16; i++) {
293 if (!in.eof()) { in >> junk; out << junk << '\t'; }
300 in >> name; //read from first column
302 //if this name is in the accnos file
303 if (names.count(name) == 0) {
304 wroteSomething = true;
309 for (int i = 0; i < 15; i++) {
310 if (!in.eof()) { in >> junk; out << junk << '\t'; }
315 }else {//still read just don't do anything with it
319 for (int i = 0; i < 15; i++) {
320 if (!in.eof()) { in >> junk; }
330 if (wroteSomething == false) {
331 mothurOut("Your file contains only sequences from the .accnos file."); mothurOutEndLine();
332 remove(outputFileName.c_str());
336 catch(exception& e) {
337 errorOut(e, "RemoveSeqsCommand", "readAlign");
341 //**********************************************************************************************************************
342 void RemoveSeqsCommand::readAccnos(){
346 openInputFile(accnosfile, in);
359 catch(exception& e) {
360 errorOut(e, "RemoveSeqsCommand", "readAccnos");
365 //**********************************************************************************************************************