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"
13 #include "counttable.h"
15 //**********************************************************************************************************************
16 vector<string> RemoveSeqsCommand::setParameters(){
18 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "FNGLT", "none","fasta",false,false,true); parameters.push_back(pfasta);
19 CommandParameter pname("name", "InputTypes", "", "", "NameCount", "FNGLT", "none","name",false,false,true); parameters.push_back(pname);
20 CommandParameter pcount("count", "InputTypes", "", "", "NameCount-CountGroup", "FNGLT", "none","count",false,false,true); parameters.push_back(pcount);
21 CommandParameter pgroup("group", "InputTypes", "", "", "CountGroup", "FNGLT", "none","group",false,false,true); parameters.push_back(pgroup);
22 CommandParameter plist("list", "InputTypes", "", "", "none", "FNGLT", "none","list",false,false,true); parameters.push_back(plist);
23 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "FNGLT", "none","taxonomy",false,false,true); parameters.push_back(ptaxonomy);
24 CommandParameter palignreport("alignreport", "InputTypes", "", "", "none", "FNGLT", "none","alignreport",false,false); parameters.push_back(palignreport);
25 CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "FNGLT", "none","qfile",false,false); parameters.push_back(pqfile);
26 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none","",false,true,true); parameters.push_back(paccnos);
27 CommandParameter pdups("dups", "Boolean", "", "T", "", "", "","",false,false); parameters.push_back(pdups);
28 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
29 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
31 vector<string> myArray;
32 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
36 m->errorOut(e, "RemoveSeqsCommand", "setParameters");
40 //**********************************************************************************************************************
41 string RemoveSeqsCommand::getHelpString(){
43 string helpString = "";
44 helpString += "The remove.seqs command reads an .accnos file and at least one of the following file types: fasta, name, group, count, list, taxonomy, quality or alignreport file.\n";
45 helpString += "It outputs a file containing the sequences NOT in the .accnos file.\n";
46 helpString += "The remove.seqs command parameters are accnos, fasta, name, group, count, list, taxonomy, qfile, alignreport and dups. You must provide accnos and at least one of the file parameters.\n";
47 helpString += "The dups parameter allows you to remove the entire line from a name file if you remove any name from the line. default=true. \n";
48 helpString += "The remove.seqs command should be in the following format: remove.seqs(accnos=yourAccnos, fasta=yourFasta).\n";
49 helpString += "Example remove.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n";
50 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
54 m->errorOut(e, "RemoveSeqsCommand", "getHelpString");
58 //**********************************************************************************************************************
59 string RemoveSeqsCommand::getOutputPattern(string type) {
63 if (type == "fasta") { pattern = "[filename],pick,[extension]"; }
64 else if (type == "taxonomy") { pattern = "[filename],pick,[extension]"; }
65 else if (type == "name") { pattern = "[filename],pick,[extension]"; }
66 else if (type == "group") { pattern = "[filename],pick,[extension]"; }
67 else if (type == "count") { pattern = "[filename],pick,[extension]"; }
68 else if (type == "list") { pattern = "[filename],pick,[extension]"; }
69 else if (type == "qfile") { pattern = "[filename],pick,[extension]"; }
70 else if (type == "alignreport") { pattern = "[filename],pick.align.report"; }
71 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
76 m->errorOut(e, "GetSeqsCommand", "getOutputPattern");
80 //**********************************************************************************************************************
81 RemoveSeqsCommand::RemoveSeqsCommand(){
83 abort = true; calledHelp = true;
85 vector<string> tempOutNames;
86 outputTypes["fasta"] = tempOutNames;
87 outputTypes["taxonomy"] = tempOutNames;
88 outputTypes["name"] = tempOutNames;
89 outputTypes["group"] = tempOutNames;
90 outputTypes["alignreport"] = tempOutNames;
91 outputTypes["list"] = tempOutNames;
92 outputTypes["qfile"] = tempOutNames;
93 outputTypes["count"] = tempOutNames;
96 m->errorOut(e, "RemoveSeqsCommand", "RemoveSeqsCommand");
100 //**********************************************************************************************************************
101 RemoveSeqsCommand::RemoveSeqsCommand(string option) {
103 abort = false; calledHelp = false;
105 //allow user to run help
106 if(option == "help") { help(); abort = true; calledHelp = true; }
107 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
110 vector<string> myArray = setParameters();
112 OptionParser parser(option);
113 map<string,string> parameters = parser.getParameters();
115 ValidParameters validParameter;
116 map<string,string>::iterator it;
118 //check to make sure all parameters are valid for command
119 for (it = parameters.begin(); it != parameters.end(); it++) {
120 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
123 //initialize outputTypes
124 vector<string> tempOutNames;
125 outputTypes["fasta"] = tempOutNames;
126 outputTypes["taxonomy"] = tempOutNames;
127 outputTypes["name"] = tempOutNames;
128 outputTypes["group"] = tempOutNames;
129 outputTypes["alignreport"] = tempOutNames;
130 outputTypes["list"] = tempOutNames;
131 outputTypes["qfile"] = tempOutNames;
132 outputTypes["count"] = tempOutNames;
134 //if the user changes the output directory command factory will send this info to us in the output parameter
135 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
137 //if the user changes the input directory command factory will send this info to us in the output parameter
138 string inputDir = validParameter.validFile(parameters, "inputdir", false);
139 if (inputDir == "not found"){ inputDir = ""; }
142 it = parameters.find("alignreport");
143 //user has given a template file
144 if(it != parameters.end()){
145 path = m->hasPath(it->second);
146 //if the user has not given a path then, add inputdir. else leave path alone.
147 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
150 it = parameters.find("fasta");
151 //user has given a template file
152 if(it != parameters.end()){
153 path = m->hasPath(it->second);
154 //if the user has not given a path then, add inputdir. else leave path alone.
155 if (path == "") { parameters["fasta"] = inputDir + it->second; }
158 it = parameters.find("accnos");
159 //user has given a template file
160 if(it != parameters.end()){
161 path = m->hasPath(it->second);
162 //if the user has not given a path then, add inputdir. else leave path alone.
163 if (path == "") { parameters["accnos"] = inputDir + it->second; }
166 it = parameters.find("list");
167 //user has given a template file
168 if(it != parameters.end()){
169 path = m->hasPath(it->second);
170 //if the user has not given a path then, add inputdir. else leave path alone.
171 if (path == "") { parameters["list"] = inputDir + it->second; }
174 it = parameters.find("name");
175 //user has given a template file
176 if(it != parameters.end()){
177 path = m->hasPath(it->second);
178 //if the user has not given a path then, add inputdir. else leave path alone.
179 if (path == "") { parameters["name"] = inputDir + it->second; }
182 it = parameters.find("group");
183 //user has given a template file
184 if(it != parameters.end()){
185 path = m->hasPath(it->second);
186 //if the user has not given a path then, add inputdir. else leave path alone.
187 if (path == "") { parameters["group"] = inputDir + it->second; }
190 it = parameters.find("taxonomy");
191 //user has given a template file
192 if(it != parameters.end()){
193 path = m->hasPath(it->second);
194 //if the user has not given a path then, add inputdir. else leave path alone.
195 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
198 it = parameters.find("qfile");
199 //user has given a template file
200 if(it != parameters.end()){
201 path = m->hasPath(it->second);
202 //if the user has not given a path then, add inputdir. else leave path alone.
203 if (path == "") { parameters["qfile"] = inputDir + it->second; }
206 it = parameters.find("count");
207 //user has given a template file
208 if(it != parameters.end()){
209 path = m->hasPath(it->second);
210 //if the user has not given a path then, add inputdir. else leave path alone.
211 if (path == "") { parameters["count"] = inputDir + it->second; }
216 //check for required parameters
217 accnosfile = validParameter.validFile(parameters, "accnos", true);
218 if (accnosfile == "not open") { abort = true; }
219 else if (accnosfile == "not found") {
220 accnosfile = m->getAccnosFile();
221 if (accnosfile != "") { m->mothurOut("Using " + accnosfile + " as input file for the accnos parameter."); m->mothurOutEndLine(); }
223 m->mothurOut("You have no valid accnos file and accnos is required."); m->mothurOutEndLine();
226 }else { m->setAccnosFile(accnosfile); }
228 fastafile = validParameter.validFile(parameters, "fasta", true);
229 if (fastafile == "not open") { fastafile = ""; abort = true; }
230 else if (fastafile == "not found") { fastafile = ""; }
231 else { m->setFastaFile(fastafile); }
233 namefile = validParameter.validFile(parameters, "name", true);
234 if (namefile == "not open") { namefile = ""; abort = true; }
235 else if (namefile == "not found") { namefile = ""; }
236 else { m->setNameFile(namefile); }
238 groupfile = validParameter.validFile(parameters, "group", true);
239 if (groupfile == "not open") { abort = true; }
240 else if (groupfile == "not found") { groupfile = ""; }
241 else { m->setGroupFile(groupfile); }
243 alignfile = validParameter.validFile(parameters, "alignreport", true);
244 if (alignfile == "not open") { abort = true; }
245 else if (alignfile == "not found") { alignfile = ""; }
247 listfile = validParameter.validFile(parameters, "list", true);
248 if (listfile == "not open") { abort = true; }
249 else if (listfile == "not found") { listfile = ""; }
250 else { m->setListFile(listfile); }
252 taxfile = validParameter.validFile(parameters, "taxonomy", true);
253 if (taxfile == "not open") { abort = true; }
254 else if (taxfile == "not found") { taxfile = ""; }
255 else { m->setTaxonomyFile(taxfile); }
257 qualfile = validParameter.validFile(parameters, "qfile", true);
258 if (qualfile == "not open") { abort = true; }
259 else if (qualfile == "not found") { qualfile = ""; }
260 else { m->setQualFile(qualfile); }
262 string usedDups = "true";
263 string temp = validParameter.validFile(parameters, "dups", false);
264 if (temp == "not found") {
265 if (namefile != "") { temp = "true"; }
266 else { temp = "false"; usedDups = ""; }
268 dups = m->isTrue(temp);
270 countfile = validParameter.validFile(parameters, "count", true);
271 if (countfile == "not open") { countfile = ""; abort = true; }
272 else if (countfile == "not found") { countfile = ""; }
273 else { m->setCountTableFile(countfile); }
275 if ((namefile != "") && (countfile != "")) {
276 m->mothurOut("[ERROR]: you may only use one of the following: name or count."); m->mothurOutEndLine(); abort = true;
279 if ((groupfile != "") && (countfile != "")) {
280 m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true;
283 if ((countfile == "") && (fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == "") && (taxfile == "") && (qualfile == "")) { m->mothurOut("You must provide at least one of the following: fasta, name, group, taxonomy, quality, alignreport or list."); m->mothurOutEndLine(); abort = true; }
285 if (countfile == "") {
286 if ((fastafile != "") && (namefile == "")) {
287 vector<string> files; files.push_back(fastafile);
288 parser.getNameFile(files);
294 catch(exception& e) {
295 m->errorOut(e, "RemoveSeqsCommand", "RemoveSeqsCommand");
299 //**********************************************************************************************************************
301 int RemoveSeqsCommand::execute(){
304 if (abort == true) { if (calledHelp) { return 0; } return 2; }
306 //get names you want to keep
307 names = m->readAccnos(accnosfile);
309 if (m->control_pressed) { return 0; }
311 if (countfile != "") {
312 if ((fastafile != "") || (listfile != "") || (taxfile != "")) {
313 m->mothurOut("\n[NOTE]: The count file should contain only unique names, so mothur assumes your fasta, list and taxonomy files also contain only uniques.\n\n");
317 //read through the correct file and output lines you want to keep
318 if (namefile != "") { readName(); }
319 if (fastafile != "") { readFasta(); }
320 if (groupfile != "") { readGroup(); }
321 if (alignfile != "") { readAlign(); }
322 if (listfile != "") { readList(); }
323 if (taxfile != "") { readTax(); }
324 if (qualfile != "") { readQual(); }
325 if (countfile != "") { readCount(); }
327 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
329 if (outputNames.size() != 0) {
330 m->mothurOutEndLine();
331 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
332 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
333 m->mothurOutEndLine();
335 //set fasta file as new current fastafile
337 itTypes = outputTypes.find("fasta");
338 if (itTypes != outputTypes.end()) {
339 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
342 itTypes = outputTypes.find("name");
343 if (itTypes != outputTypes.end()) {
344 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
347 itTypes = outputTypes.find("group");
348 if (itTypes != outputTypes.end()) {
349 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
352 itTypes = outputTypes.find("list");
353 if (itTypes != outputTypes.end()) {
354 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
357 itTypes = outputTypes.find("taxonomy");
358 if (itTypes != outputTypes.end()) {
359 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
362 itTypes = outputTypes.find("qfile");
363 if (itTypes != outputTypes.end()) {
364 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
367 itTypes = outputTypes.find("count");
368 if (itTypes != outputTypes.end()) {
369 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); }
376 catch(exception& e) {
377 m->errorOut(e, "RemoveSeqsCommand", "execute");
382 //**********************************************************************************************************************
383 int RemoveSeqsCommand::readFasta(){
385 string thisOutputDir = outputDir;
386 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
387 map<string, string> variables;
388 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(fastafile));
389 variables["[extension]"] = m->getExtension(fastafile);
390 string outputFileName = getOutputFileName("fasta", variables);
393 m->openOutputFile(outputFileName, out);
396 m->openInputFile(fastafile, in);
399 bool wroteSomething = false;
400 int removedCount = 0;
403 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
405 Sequence currSeq(in);
407 if (!dups) {//adjust name if needed
408 map<string, string>::iterator it = uniqueMap.find(currSeq.getName());
409 if (it != uniqueMap.end()) { currSeq.setName(it->second); }
412 name = currSeq.getName();
415 //if this name is in the accnos file
416 if (names.count(name) == 0) {
417 wroteSomething = true;
419 currSeq.printSequence(out);
420 }else { removedCount++; }
427 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
428 outputTypes["fasta"].push_back(outputFileName); outputNames.push_back(outputFileName);
430 m->mothurOut("Removed " + toString(removedCount) + " sequences from your fasta file."); m->mothurOutEndLine();
435 catch(exception& e) {
436 m->errorOut(e, "RemoveSeqsCommand", "readFasta");
440 //**********************************************************************************************************************
441 int RemoveSeqsCommand::readQual(){
443 string thisOutputDir = outputDir;
444 if (outputDir == "") { thisOutputDir += m->hasPath(qualfile); }
445 map<string, string> variables;
446 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(qualfile));
447 variables["[extension]"] = m->getExtension(qualfile);
448 string outputFileName = getOutputFileName("qfile", variables);
450 m->openOutputFile(outputFileName, out);
454 m->openInputFile(qualfile, in);
457 bool wroteSomething = false;
458 int removedCount = 0;
462 string saveName = "";
468 if (name.length() != 0) {
469 saveName = name.substr(1);
472 if (c == 10 || c == 13 || c == -1){ break; }
479 char letter= in.get();
480 if(letter == '>'){ in.putback(letter); break; }
481 else{ scores += letter; }
486 if (!dups) {//adjust name if needed
487 map<string, string>::iterator it = uniqueMap.find(saveName);
488 if (it != uniqueMap.end()) { name = ">" + it->second; saveName = it->second; }
491 if (names.count(saveName) == 0) {
492 wroteSomething = true;
494 out << name << endl << scores;
495 }else { removedCount++; }
503 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
504 outputNames.push_back(outputFileName); outputTypes["qfile"].push_back(outputFileName);
506 m->mothurOut("Removed " + toString(removedCount) + " sequences from your quality file."); m->mothurOutEndLine();
511 catch(exception& e) {
512 m->errorOut(e, "RemoveSeqsCommand", "readQual");
516 //**********************************************************************************************************************
517 int RemoveSeqsCommand::readCount(){
520 string thisOutputDir = outputDir;
521 if (outputDir == "") { thisOutputDir += m->hasPath(countfile); }
522 map<string, string> variables;
523 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(countfile));
524 variables["[extension]"] = m->getExtension(countfile);
525 string outputFileName = getOutputFileName("count", variables);
528 m->openOutputFile(outputFileName, out);
531 m->openInputFile(countfile, in);
533 bool wroteSomething = false;
534 int removedCount = 0;
536 string headers = m->getline(in); m->gobble(in);
537 out << headers << endl;
539 string name, rest; int thisTotal;
542 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
544 in >> name; m->gobble(in);
545 in >> thisTotal; m->gobble(in);
546 rest = m->getline(in); m->gobble(in);
547 if (m->debug) { m->mothurOut("[DEBUG]: " + name + '\t' + rest + "\n"); }
549 if (names.count(name) == 0) {
550 out << name << '\t' << thisTotal << '\t' << rest << endl;
551 wroteSomething = true;
552 }else { removedCount += thisTotal; }
557 //check for groups that have been eliminated
559 if (ct.testGroups(outputFileName)) {
560 ct.readTable(outputFileName, true);
561 ct.printTable(outputFileName);
565 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
566 outputTypes["count"].push_back(outputFileName); outputNames.push_back(outputFileName);
568 m->mothurOut("Removed " + toString(removedCount) + " sequences from your count file."); m->mothurOutEndLine();
572 catch(exception& e) {
573 m->errorOut(e, "RemoveSeqsCommand", "readCount");
577 //**********************************************************************************************************************
578 int RemoveSeqsCommand::readList(){
580 string thisOutputDir = outputDir;
581 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
582 map<string, string> variables;
583 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(listfile));
584 variables["[extension]"] = m->getExtension(listfile);
585 string outputFileName = getOutputFileName("list", variables);
587 m->openOutputFile(outputFileName, out);
590 m->openInputFile(listfile, in);
592 bool wroteSomething = false;
593 int removedCount = 0;
599 //read in list vector
602 //make a new list vector
604 newList.setLabel(list.getLabel());
607 for (int i = 0; i < list.getNumBins(); i++) {
608 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
610 //parse out names that are in accnos file
611 string binnames = list.get(i);
613 string newNames = "";
614 while (binnames.find_first_of(',') != -1) {
615 string name = binnames.substr(0,binnames.find_first_of(','));
616 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
618 //if that name is in the .accnos file, add it
619 if (names.count(name) == 0) { newNames += name + ","; }
620 else { removedCount++; }
624 if (names.count(binnames) == 0) { newNames += binnames + ","; }
625 else { removedCount++; }
627 //if there are names in this bin add to new list
628 if (newNames != "") {
629 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
630 newList.push_back(newNames);
634 //print new listvector
635 if (newList.getNumBins() != 0) {
636 wroteSomething = true;
645 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
646 outputTypes["list"].push_back(outputFileName); outputNames.push_back(outputFileName);
648 m->mothurOut("Removed " + toString(removedCount) + " sequences from your list file."); m->mothurOutEndLine();
653 catch(exception& e) {
654 m->errorOut(e, "RemoveSeqsCommand", "readList");
658 //**********************************************************************************************************************
659 int RemoveSeqsCommand::readName(){
661 string thisOutputDir = outputDir;
662 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
663 map<string, string> variables;
664 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(namefile));
665 variables["[extension]"] = m->getExtension(namefile);
666 string outputFileName = getOutputFileName("name", variables);
668 m->openOutputFile(outputFileName, out);
671 m->openInputFile(namefile, in);
672 string name, firstCol, secondCol;
674 bool wroteSomething = false;
675 int removedCount = 0;
678 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
680 in >> firstCol; m->gobble(in);
683 vector<string> parsedNames;
684 m->splitAtComma(secondCol, parsedNames);
686 vector<string> validSecond; validSecond.clear();
687 for (int i = 0; i < parsedNames.size(); i++) {
688 if (names.count(parsedNames[i]) == 0) {
689 validSecond.push_back(parsedNames[i]);
693 if ((dups) && (validSecond.size() != parsedNames.size())) { //if dups is true and we want to get rid of anyone, get rid of everyone
694 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); }
695 removedCount += parsedNames.size();
697 removedCount += parsedNames.size()-validSecond.size();
698 //if the name in the first column is in the set then print it and any other names in second column also in set
699 if (names.count(firstCol) == 0) {
701 wroteSomething = true;
703 out << firstCol << '\t';
705 //you know you have at least one valid second since first column is valid
706 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
707 out << validSecond[validSecond.size()-1] << endl;
709 //make first name in set you come to first column and then add the remaining names to second column
712 //you want part of this row
713 if (validSecond.size() != 0) {
715 wroteSomething = true;
717 out << validSecond[0] << '\t';
718 //we are changing the unique name in the fasta file
719 uniqueMap[firstCol] = validSecond[0];
721 //you know you have at least one valid second since first column is valid
722 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
723 out << validSecond[validSecond.size()-1] << endl;
732 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
733 outputTypes["name"].push_back(outputFileName); outputNames.push_back(outputFileName);
735 m->mothurOut("Removed " + toString(removedCount) + " sequences from your name file."); m->mothurOutEndLine();
739 catch(exception& e) {
740 m->errorOut(e, "RemoveSeqsCommand", "readName");
745 //**********************************************************************************************************************
746 int RemoveSeqsCommand::readGroup(){
748 string thisOutputDir = outputDir;
749 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
750 map<string, string> variables;
751 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(groupfile));
752 variables["[extension]"] = m->getExtension(groupfile);
753 string outputFileName = getOutputFileName("group", variables);
755 m->openOutputFile(outputFileName, out);
758 m->openInputFile(groupfile, in);
761 bool wroteSomething = false;
762 int removedCount = 0;
765 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
767 in >> name; //read from first column
768 in >> group; //read from second column
770 //if this name is in the accnos file
771 if (names.count(name) == 0) {
772 wroteSomething = true;
773 out << name << '\t' << group << endl;
774 }else { removedCount++; }
781 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
782 outputTypes["group"].push_back(outputFileName); outputNames.push_back(outputFileName);
784 m->mothurOut("Removed " + toString(removedCount) + " sequences from your group file."); m->mothurOutEndLine();
789 catch(exception& e) {
790 m->errorOut(e, "RemoveSeqsCommand", "readGroup");
794 //**********************************************************************************************************************
795 int RemoveSeqsCommand::readTax(){
797 string thisOutputDir = outputDir;
798 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
799 map<string, string> variables;
800 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(taxfile));
801 variables["[extension]"] = m->getExtension(taxfile);
802 string outputFileName = getOutputFileName("taxonomy", variables);
804 m->openOutputFile(outputFileName, out);
807 m->openInputFile(taxfile, in);
810 bool wroteSomething = false;
811 int removedCount = 0;
814 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
816 in >> name; m->gobble(in); //read from first column
817 in >> tax; //read from second column
819 if (!dups) {//adjust name if needed
820 map<string, string>::iterator it = uniqueMap.find(name);
821 if (it != uniqueMap.end()) { name = it->second; }
824 //if this name is in the accnos file
825 if (names.count(name) == 0) {
826 wroteSomething = true;
828 out << name << '\t' << tax << endl;
829 }else { removedCount++; }
836 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
837 outputTypes["taxonomy"].push_back(outputFileName); outputNames.push_back(outputFileName);
839 m->mothurOut("Removed " + toString(removedCount) + " sequences from your taxonomy file."); m->mothurOutEndLine();
843 catch(exception& e) {
844 m->errorOut(e, "RemoveSeqsCommand", "readTax");
848 //**********************************************************************************************************************
849 //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
850 int RemoveSeqsCommand::readAlign(){
852 string thisOutputDir = outputDir;
853 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
854 map<string, string> variables;
855 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(alignfile));
856 string outputFileName = getOutputFileName("alignreport", variables);
859 m->openOutputFile(outputFileName, out);
862 m->openInputFile(alignfile, in);
865 bool wroteSomething = false;
866 int removedCount = 0;
868 //read column headers
869 for (int i = 0; i < 16; i++) {
870 if (!in.eof()) { in >> junk; out << junk << '\t'; }
876 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
878 in >> name; //read from first column
880 if (!dups) {//adjust name if needed
881 map<string, string>::iterator it = uniqueMap.find(name);
882 if (it != uniqueMap.end()) { name = it->second; }
885 //if this name is in the accnos file
886 if (names.count(name) == 0) {
887 wroteSomething = true;
892 for (int i = 0; i < 15; i++) {
893 if (!in.eof()) { in >> junk; out << junk << '\t'; }
898 }else {//still read just don't do anything with it
902 for (int i = 0; i < 15; i++) {
903 if (!in.eof()) { in >> junk; }
913 if (wroteSomething == false) { m->mothurOut("Your file contains only sequences from the .accnos file."); m->mothurOutEndLine(); }
914 outputTypes["alignreport"].push_back(outputFileName); outputNames.push_back(outputFileName);
916 m->mothurOut("Removed " + toString(removedCount) + " sequences from your alignreport file."); m->mothurOutEndLine();
922 catch(exception& e) {
923 m->errorOut(e, "RemoveSeqsCommand", "readAlign");
927 //**********************************************************************************************************************