5 * Created by Sarah Westcott on 7/8/09.
6 * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved.
10 #include "getseqscommand.h"
11 #include "sequence.hpp"
12 #include "listvector.hpp"
14 //**********************************************************************************************************************
15 vector<string> GetSeqsCommand::setParameters(){
17 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pfasta);
18 CommandParameter pname("name", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pname);
19 CommandParameter pgroup("group", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pgroup);
20 CommandParameter plist("list", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(plist);
21 CommandParameter ptaxonomy("taxonomy", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(ptaxonomy);
22 CommandParameter palignreport("alignreport", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(palignreport);
23 CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "FNGLT", "none",false,false); parameters.push_back(pqfile);
24 CommandParameter paccnos("accnos", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(paccnos);
25 CommandParameter pdups("dups", "Boolean", "", "T", "", "", "",false,false); parameters.push_back(pdups);
26 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
27 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
28 CommandParameter paccnos2("accnos2", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(paccnos2);
30 vector<string> myArray;
31 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
35 m->errorOut(e, "GetSeqsCommand", "setParameters");
39 //**********************************************************************************************************************
40 string GetSeqsCommand::getHelpString(){
42 string helpString = "";
43 helpString += "The get.seqs command reads an .accnos file and any of the following file types: fasta, name, group, list, taxonomy, quality or alignreport file.\n";
44 helpString += "It outputs a file containing only the sequences in the .accnos file.\n";
45 helpString += "The get.seqs command parameters are accnos, fasta, name, group, list, taxonomy, qfile, alignreport and dups. You must provide accnos unless you have a valid current accnos file, and at least one of the other parameters.\n";
46 helpString += "The dups parameter allows you to add the entire line from a name file if you add any name from the line. default=false. \n";
47 helpString += "The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n";
48 helpString += "Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n";
49 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
53 m->errorOut(e, "GetSeqsCommand", "getHelpString");
58 //**********************************************************************************************************************
59 GetSeqsCommand::GetSeqsCommand(){
61 abort = true; calledHelp = true;
63 vector<string> tempOutNames;
64 outputTypes["fasta"] = tempOutNames;
65 outputTypes["taxonomy"] = tempOutNames;
66 outputTypes["name"] = tempOutNames;
67 outputTypes["group"] = tempOutNames;
68 outputTypes["alignreport"] = tempOutNames;
69 outputTypes["list"] = tempOutNames;
70 outputTypes["qfile"] = tempOutNames;
71 outputTypes["accnosreport"] = tempOutNames;
74 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
78 //**********************************************************************************************************************
79 string GetSeqsCommand::getOutputFileNameTag(string type, string inputName=""){
81 string outputFileName = "";
82 map<string, vector<string> >::iterator it;
84 //is this a type this command creates
85 it = outputTypes.find(type);
86 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
88 if (type == "fasta") { outputFileName = "pick" + m->getExtension(inputName); }
89 else if (type == "taxonomy") { outputFileName = "pick" + m->getExtension(inputName); }
90 else if (type == "name") { outputFileName = "pick" + m->getExtension(inputName); }
91 else if (type == "group") { outputFileName = "pick" + m->getExtension(inputName); }
92 else if (type == "list") { outputFileName = "pick" + m->getExtension(inputName); }
93 else if (type == "qfile") { outputFileName = "pick" + m->getExtension(inputName); }
94 else if (type == "accnosreport"){ outputFileName = "accnos.report"; }
95 else if (type == "alignreport") { outputFileName = "pick.align.report"; }
96 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
98 return outputFileName;
100 catch(exception& e) {
101 m->errorOut(e, "GetSeqsCommand", "getOutputFileNameTag");
105 //**********************************************************************************************************************
106 GetSeqsCommand::GetSeqsCommand(string option) {
108 abort = false; calledHelp = false;
110 //allow user to run help
111 if(option == "help") { help(); abort = true; calledHelp = true; }
112 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
115 vector<string> myArray = setParameters();
117 OptionParser parser(option);
118 map<string,string> parameters = parser.getParameters();
120 ValidParameters validParameter;
121 map<string,string>::iterator it;
123 //check to make sure all parameters are valid for command
124 for (it = parameters.begin(); it != parameters.end(); it++) {
125 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
128 //initialize outputTypes
129 vector<string> tempOutNames;
130 outputTypes["fasta"] = tempOutNames;
131 outputTypes["taxonomy"] = tempOutNames;
132 outputTypes["name"] = tempOutNames;
133 outputTypes["group"] = tempOutNames;
134 outputTypes["alignreport"] = tempOutNames;
135 outputTypes["list"] = tempOutNames;
136 outputTypes["qfile"] = tempOutNames;
137 outputTypes["accnosreport"] = tempOutNames;
139 //if the user changes the output directory command factory will send this info to us in the output parameter
140 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
142 //if the user changes the input directory command factory will send this info to us in the output parameter
143 string inputDir = validParameter.validFile(parameters, "inputdir", false);
144 if (inputDir == "not found"){ inputDir = ""; }
147 it = parameters.find("alignreport");
148 //user has given a template file
149 if(it != parameters.end()){
150 path = m->hasPath(it->second);
151 //if the user has not given a path then, add inputdir. else leave path alone.
152 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
155 it = parameters.find("fasta");
156 //user has given a template file
157 if(it != parameters.end()){
158 path = m->hasPath(it->second);
159 //if the user has not given a path then, add inputdir. else leave path alone.
160 if (path == "") { parameters["fasta"] = inputDir + it->second; }
163 it = parameters.find("accnos");
164 //user has given a template file
165 if(it != parameters.end()){
166 path = m->hasPath(it->second);
167 //if the user has not given a path then, add inputdir. else leave path alone.
168 if (path == "") { parameters["accnos"] = inputDir + it->second; }
171 it = parameters.find("accnos2");
172 //user has given a template file
173 if(it != parameters.end()){
174 path = m->hasPath(it->second);
175 //if the user has not given a path then, add inputdir. else leave path alone.
176 if (path == "") { parameters["accnos2"] = inputDir + it->second; }
179 it = parameters.find("list");
180 //user has given a template file
181 if(it != parameters.end()){
182 path = m->hasPath(it->second);
183 //if the user has not given a path then, add inputdir. else leave path alone.
184 if (path == "") { parameters["list"] = inputDir + it->second; }
187 it = parameters.find("name");
188 //user has given a template file
189 if(it != parameters.end()){
190 path = m->hasPath(it->second);
191 //if the user has not given a path then, add inputdir. else leave path alone.
192 if (path == "") { parameters["name"] = inputDir + it->second; }
195 it = parameters.find("group");
196 //user has given a template file
197 if(it != parameters.end()){
198 path = m->hasPath(it->second);
199 //if the user has not given a path then, add inputdir. else leave path alone.
200 if (path == "") { parameters["group"] = inputDir + it->second; }
203 it = parameters.find("taxonomy");
204 //user has given a template file
205 if(it != parameters.end()){
206 path = m->hasPath(it->second);
207 //if the user has not given a path then, add inputdir. else leave path alone.
208 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
211 it = parameters.find("qfile");
212 //user has given a template file
213 if(it != parameters.end()){
214 path = m->hasPath(it->second);
215 //if the user has not given a path then, add inputdir. else leave path alone.
216 if (path == "") { parameters["qfile"] = inputDir + it->second; }
221 //check for required parameters
222 accnosfile = validParameter.validFile(parameters, "accnos", true);
223 if (accnosfile == "not open") { abort = true; }
224 else if (accnosfile == "not found") {
225 accnosfile = m->getAccnosFile();
226 if (accnosfile != "") { m->mothurOut("Using " + accnosfile + " as input file for the accnos parameter."); m->mothurOutEndLine(); }
228 m->mothurOut("You have no valid accnos file and accnos is required."); m->mothurOutEndLine();
231 }else { m->setAccnosFile(accnosfile); }
233 if (accnosfile2 == "not found") { accnosfile2 = ""; }
235 fastafile = validParameter.validFile(parameters, "fasta", true);
236 if (fastafile == "not open") { fastafile = ""; abort = true; }
237 else if (fastafile == "not found") { fastafile = ""; }
238 else { m->setFastaFile(fastafile); }
240 namefile = validParameter.validFile(parameters, "name", true);
241 if (namefile == "not open") { namefile = ""; abort = true; }
242 else if (namefile == "not found") { namefile = ""; }
243 else { m->setNameFile(namefile); }
245 groupfile = validParameter.validFile(parameters, "group", true);
246 if (groupfile == "not open") { abort = true; }
247 else if (groupfile == "not found") { groupfile = ""; }
248 else { m->setGroupFile(groupfile); }
250 alignfile = validParameter.validFile(parameters, "alignreport", true);
251 if (alignfile == "not open") { abort = true; }
252 else if (alignfile == "not found") { alignfile = ""; }
254 listfile = validParameter.validFile(parameters, "list", true);
255 if (listfile == "not open") { abort = true; }
256 else if (listfile == "not found") { listfile = ""; }
257 else { m->setListFile(listfile); }
259 taxfile = validParameter.validFile(parameters, "taxonomy", true);
260 if (taxfile == "not open") { taxfile = ""; abort = true; }
261 else if (taxfile == "not found") { taxfile = ""; }
262 else { m->setTaxonomyFile(taxfile); }
264 qualfile = validParameter.validFile(parameters, "qfile", true);
265 if (qualfile == "not open") { abort = true; }
266 else if (qualfile == "not found") { qualfile = ""; }
267 else { m->setQualFile(qualfile); }
269 accnosfile2 = validParameter.validFile(parameters, "accnos2", true);
270 if (accnosfile2 == "not open") { abort = true; }
271 else if (accnosfile2 == "not found") { accnosfile2 = ""; }
274 string usedDups = "true";
275 string temp = validParameter.validFile(parameters, "dups", false); if (temp == "not found") { temp = "true"; usedDups = ""; }
276 dups = m->isTrue(temp);
278 if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == "") && (taxfile == "") && (qualfile == "") && (accnosfile2 == "")) { m->mothurOut("You must provide one of the following: fasta, name, group, alignreport, taxonomy, quality or listfile."); m->mothurOutEndLine(); abort = true; }
280 if ((namefile == "") && ((fastafile != "") || (taxfile != ""))){
281 vector<string> files; files.push_back(fastafile); files.push_back(taxfile);
282 parser.getNameFile(files);
287 catch(exception& e) {
288 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
292 //**********************************************************************************************************************
294 int GetSeqsCommand::execute(){
297 if (abort == true) { if (calledHelp) { return 0; } return 2; }
299 //get names you want to keep
300 names = m->readAccnos(accnosfile);
302 if (m->control_pressed) { return 0; }
304 //read through the correct file and output lines you want to keep
305 if (namefile != "") { readName(); }
306 if (fastafile != "") { readFasta(); }
307 if (groupfile != "") { readGroup(); }
308 if (alignfile != "") { readAlign(); }
309 if (listfile != "") { readList(); }
310 if (taxfile != "") { readTax(); }
311 if (qualfile != "") { readQual(); }
312 if (accnosfile2 != "") { compareAccnos(); }
314 if (m->debug) { runSanityCheck(); }
316 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
319 if (outputNames.size() != 0) {
320 m->mothurOutEndLine();
321 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
322 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
323 m->mothurOutEndLine();
325 //set fasta file as new current fastafile
327 itTypes = outputTypes.find("fasta");
328 if (itTypes != outputTypes.end()) {
329 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
332 itTypes = outputTypes.find("name");
333 if (itTypes != outputTypes.end()) {
334 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
337 itTypes = outputTypes.find("group");
338 if (itTypes != outputTypes.end()) {
339 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
342 itTypes = outputTypes.find("list");
343 if (itTypes != outputTypes.end()) {
344 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
347 itTypes = outputTypes.find("taxonomy");
348 if (itTypes != outputTypes.end()) {
349 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
352 itTypes = outputTypes.find("qfile");
353 if (itTypes != outputTypes.end()) {
354 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
362 catch(exception& e) {
363 m->errorOut(e, "GetSeqsCommand", "execute");
368 //**********************************************************************************************************************
369 int GetSeqsCommand::readFasta(){
371 string thisOutputDir = outputDir;
372 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
373 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + getOutputFileNameTag("fasta", fastafile);
375 m->openOutputFile(outputFileName, out);
379 m->openInputFile(fastafile, in);
382 bool wroteSomething = false;
383 int selectedCount = 0;
385 if (m->debug) { set<string> temp; sanity["fasta"] = temp; }
389 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
391 Sequence currSeq(in);
392 name = currSeq.getName();
395 //if this name is in the accnos file
396 if (names.count(name) != 0) {
397 wroteSomething = true;
399 currSeq.printSequence(out);
402 if (m->debug) { sanity["fasta"].insert(name); }
411 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
412 outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
414 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your fasta file."); m->mothurOutEndLine();
419 catch(exception& e) {
420 m->errorOut(e, "GetSeqsCommand", "readFasta");
424 //**********************************************************************************************************************
425 int GetSeqsCommand::readQual(){
427 string thisOutputDir = outputDir;
428 if (outputDir == "") { thisOutputDir += m->hasPath(qualfile); }
429 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(qualfile)) + getOutputFileNameTag("qfile", qualfile);
431 m->openOutputFile(outputFileName, out);
435 m->openInputFile(qualfile, in);
438 bool wroteSomething = false;
439 int selectedCount = 0;
441 if (m->debug) { set<string> temp; sanity["qual"] = temp; }
444 string saveName = "";
450 if (name.length() != 0) {
451 saveName = name.substr(1);
454 if (c == 10 || c == 13){ break; }
461 char letter= in.get();
462 if(letter == '>'){ in.putback(letter); break; }
463 else{ scores += letter; }
468 if (names.count(saveName) != 0) {
469 wroteSomething = true;
471 out << name << endl << scores;
473 if (m->debug) { sanity["qual"].insert(name); }
482 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
483 outputNames.push_back(outputFileName); outputTypes["qfile"].push_back(outputFileName);
485 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your quality file."); m->mothurOutEndLine();
491 catch(exception& e) {
492 m->errorOut(e, "GetSeqsCommand", "readQual");
496 //**********************************************************************************************************************
497 int GetSeqsCommand::readList(){
499 string thisOutputDir = outputDir;
500 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
501 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + getOutputFileNameTag("list", listfile);
503 m->openOutputFile(outputFileName, out);
506 m->openInputFile(listfile, in);
508 bool wroteSomething = false;
509 int selectedCount = 0;
511 if (m->debug) { set<string> temp; sanity["list"] = temp; }
517 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
519 //read in list vector
522 //make a new list vector
524 newList.setLabel(list.getLabel());
527 for (int i = 0; i < list.getNumBins(); i++) {
529 //parse out names that are in accnos file
530 string binnames = list.get(i);
532 string newNames = "";
533 while (binnames.find_first_of(',') != -1) {
534 string name = binnames.substr(0,binnames.find_first_of(','));
535 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
537 //if that name is in the .accnos file, add it
538 if (names.count(name) != 0) { newNames += name + ","; selectedCount++; if (m->debug) { sanity["list"].insert(name); } }
542 if (names.count(binnames) != 0) { newNames += binnames + ","; selectedCount++; if (m->debug) { sanity["list"].insert(binnames); } }
544 //if there are names in this bin add to new list
545 if (newNames != "") {
546 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
547 newList.push_back(newNames);
551 //print new listvector
552 if (newList.getNumBins() != 0) {
553 wroteSomething = true;
562 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
563 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
565 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your list file."); m->mothurOutEndLine();
570 catch(exception& e) {
571 m->errorOut(e, "GetSeqsCommand", "readList");
575 //**********************************************************************************************************************
576 int GetSeqsCommand::readName(){
578 string thisOutputDir = outputDir;
579 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
580 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + getOutputFileNameTag("name", namefile);
582 m->openOutputFile(outputFileName, out);
586 m->openInputFile(namefile, in);
587 string name, firstCol, secondCol;
589 bool wroteSomething = false;
590 int selectedCount = 0;
592 if (m->debug) { set<string> temp; sanity["name"] = temp; }
593 if (m->debug) { set<string> temp; sanity["dupname"] = temp; }
597 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
603 if (dups) { hold = secondCol; }
605 vector<string> parsedNames;
606 m->splitAtComma(secondCol, parsedNames);
608 vector<string> validSecond;
609 for (int i = 0; i < parsedNames.size(); i++) {
610 if (names.count(parsedNames[i]) != 0) {
611 validSecond.push_back(parsedNames[i]);
612 if (m->debug) { sanity["dupname"].insert(parsedNames[i]); }
616 if ((dups) && (validSecond.size() != 0)) { //dups = true and we want to add someone, then add everyone
617 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); if (m->debug) { sanity["dupname"].insert(parsedNames[i]); } }
618 out << firstCol << '\t' << hold << endl;
619 wroteSomething = true;
620 selectedCount += parsedNames.size();
621 if (m->debug) { sanity["name"].insert(firstCol); }
623 selectedCount += validSecond.size();
625 //if the name in the first column is in the set then print it and any other names in second column also in set
626 if (names.count(firstCol) != 0) {
628 wroteSomething = true;
630 out << firstCol << '\t';
632 //you know you have at least one valid second since first column is valid
633 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
634 out << validSecond[validSecond.size()-1] << endl;
636 if (m->debug) { sanity["name"].insert(firstCol); }
639 //make first name in set you come to first column and then add the remaining names to second column
641 //you want part of this row
642 if (validSecond.size() != 0) {
644 wroteSomething = true;
646 out << validSecond[0] << '\t';
648 //you know you have at least one valid second since first column is valid
649 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
650 out << validSecond[validSecond.size()-1] << endl;
652 if (m->debug) { sanity["name"].insert(validSecond[0]); }
661 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
662 outputNames.push_back(outputFileName); outputTypes["name"].push_back(outputFileName);
664 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your name file."); m->mothurOutEndLine();
669 catch(exception& e) {
670 m->errorOut(e, "GetSeqsCommand", "readName");
675 //**********************************************************************************************************************
676 int GetSeqsCommand::readGroup(){
678 string thisOutputDir = outputDir;
679 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
680 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + getOutputFileNameTag("group", groupfile);
682 m->openOutputFile(outputFileName, out);
686 m->openInputFile(groupfile, in);
689 bool wroteSomething = false;
690 int selectedCount = 0;
692 if (m->debug) { set<string> temp; sanity["group"] = temp; }
696 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
699 in >> name; //read from first column
700 in >> group; //read from second column
702 //if this name is in the accnos file
703 if (names.count(name) != 0) {
704 wroteSomething = true;
706 out << name << '\t' << group << endl;
709 if (m->debug) { sanity["group"].insert(name); }
717 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
718 outputNames.push_back(outputFileName); outputTypes["group"].push_back(outputFileName);
720 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your group file."); m->mothurOutEndLine();
726 catch(exception& e) {
727 m->errorOut(e, "GetSeqsCommand", "readGroup");
731 //**********************************************************************************************************************
732 int GetSeqsCommand::readTax(){
734 string thisOutputDir = outputDir;
735 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
736 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + getOutputFileNameTag("taxonomy", taxfile);
738 m->openOutputFile(outputFileName, out);
741 m->openInputFile(taxfile, in);
744 bool wroteSomething = false;
745 int selectedCount = 0;
747 if (m->debug) { set<string> temp; sanity["tax"] = temp; }
751 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
753 in >> name; //read from first column
754 in >> tax; //read from second column
756 //if this name is in the accnos file
757 if (names.count(name) != 0) {
758 wroteSomething = true;
760 out << name << '\t' << tax << endl;
763 if (m->debug) { sanity["tax"].insert(name); }
771 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
772 outputNames.push_back(outputFileName); outputTypes["taxonomy"].push_back(outputFileName);
774 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your taxonomy file."); m->mothurOutEndLine();
779 catch(exception& e) {
780 m->errorOut(e, "GetSeqsCommand", "readTax");
784 //**********************************************************************************************************************
785 //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
786 int GetSeqsCommand::readAlign(){
788 string thisOutputDir = outputDir;
789 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
790 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + getOutputFileNameTag("alignreport");
792 m->openOutputFile(outputFileName, out);
796 m->openInputFile(alignfile, in);
799 bool wroteSomething = false;
800 int selectedCount = 0;
802 //read column headers
803 for (int i = 0; i < 16; i++) {
804 if (!in.eof()) { in >> junk; out << junk << '\t'; }
811 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
814 in >> name; //read from first column
816 //if this name is in the accnos file
817 if (names.count(name) != 0) {
818 wroteSomething = true;
824 for (int i = 0; i < 15; i++) {
825 if (!in.eof()) { in >> junk; out << junk << '\t'; }
830 }else {//still read just don't do anything with it
832 for (int i = 0; i < 15; i++) {
833 if (!in.eof()) { in >> junk; }
843 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
844 outputNames.push_back(outputFileName); outputTypes["alignreport"].push_back(outputFileName);
846 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your alignreport file."); m->mothurOutEndLine();
851 catch(exception& e) {
852 m->errorOut(e, "GetSeqsCommand", "readAlign");
856 //**********************************************************************************************************************
857 //just looking at common mistakes.
858 int GetSeqsCommand::runSanityCheck(){
860 string thisOutputDir = outputDir;
861 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
862 string filename = outputDir + "get.seqs.debug.report";
865 m->openOutputFile(filename, out);
868 //compare fasta, name, qual and taxonomy if given to make sure they contain the same seqs
869 if (fastafile != "") {
870 if (namefile != "") { //compare with fasta
871 if (sanity["fasta"] != sanity["name"]) { //create mismatch file
872 createMisMatchFile(out, fastafile, namefile, sanity["fasta"], sanity["name"]);
875 if (qualfile != "") {
876 if (sanity["fasta"] != sanity["qual"]) { //create mismatch file
877 createMisMatchFile(out, fastafile, qualfile, sanity["fasta"], sanity["qual"]);
881 if (sanity["fasta"] != sanity["tax"]) { //create mismatch file
882 createMisMatchFile(out, fastafile, taxfile, sanity["fasta"], sanity["tax"]);
887 //compare dupnames, groups and list if given to make sure they match
888 if (namefile != "") {
889 if (groupfile != "") {
890 if (sanity["dupname"] != sanity["group"]) { //create mismatch file
891 createMisMatchFile(out, namefile, groupfile, sanity["dupname"], sanity["group"]);
894 if (listfile != "") {
895 if (sanity["dupname"] != sanity["list"]) { //create mismatch file
896 createMisMatchFile(out, namefile, listfile, sanity["dupname"], sanity["list"]);
901 if ((groupfile != "") && (fastafile != "")) {
902 if (sanity["fasta"] != sanity["group"]) { //create mismatch file
903 createMisMatchFile(out, fastafile, groupfile, sanity["fasta"], sanity["group"]);
910 if (m->isBlank(filename)) { m->mothurRemove(filename); }
911 else { m->mothurOut("\n[DEBUG]: " + filename + " contains the file mismatches.\n");outputNames.push_back(filename); outputTypes["debug"].push_back(filename); }
915 catch(exception& e) {
916 m->errorOut(e, "GetSeqsCommand", "runSanityCheck");
920 //**********************************************************************************************************************
921 //just looking at common mistakes.
922 int GetSeqsCommand::createMisMatchFile(ofstream& out, string filename1, string filename2, set<string> set1, set<string> set2){
924 out << "****************************************" << endl << endl;
925 out << "Names unique to " << filename1 << ":\n";
927 //remove names in set1 that are also in set2
928 for (set<string>::iterator it = set1.begin(); it != set1.end();) {
931 if (set2.count(name) == 0) { out << name << endl; } //name unique to set1
932 else { set2.erase(name); } //you are in both so erase
936 out << "\nNames unique to " << filename2 << ":\n";
938 for (set<string>::iterator it = set2.begin(); it != set2.end(); it++) { out << *it << endl; }
940 out << "****************************************" << endl << endl;
944 catch(exception& e) {
945 m->errorOut(e, "GetSeqsCommand", "runSanityCheck");
949 //**********************************************************************************************************************
951 int GetSeqsCommand::compareAccnos(){
954 string thisOutputDir = outputDir;
955 if (outputDir == "") { thisOutputDir += m->hasPath(accnosfile); }
956 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(accnosfile)) + getOutputFileNameTag("accnosreport");
958 m->openOutputFile(outputFileName, out);
961 m->openInputFile(accnosfile2, in);
964 set<string> namesAccnos2;
965 set<string> namesDups;
966 set<string> namesAccnos = names;
968 map<string, int> nameCount;
970 if (namefile != "") {
972 m->openInputFile(namefile, inName);
975 while(!inName.eof()){
977 if (m->control_pressed) { inName.close(); return 0; }
979 string thisname, repnames;
981 inName >> thisname; m->gobble(inName); //read from first column
982 inName >> repnames; //read from second column
984 int num = m->getNumNames(repnames);
985 nameCount[thisname] = num;
995 if (namesAccnos.count(name) == 0){ //name unique to accnos2
996 int pos = name.find_last_of('_');
997 string tempName = name;
998 if (pos != string::npos) { tempName = tempName.substr(pos+1); cout << tempName << endl; }
999 if (namesAccnos.count(tempName) == 0){
1000 namesAccnos2.insert(name);
1001 }else { //you are in both so erase
1002 namesAccnos.erase(name);
1003 namesDups.insert(name);
1005 }else { //you are in both so erase
1006 namesAccnos.erase(name);
1007 namesDups.insert(name);
1014 out << "Names in both files : " + toString(namesDups.size()) << endl;
1015 m->mothurOut("Names in both files : " + toString(namesDups.size())); m->mothurOutEndLine();
1017 for (set<string>::iterator it = namesDups.begin(); it != namesDups.end(); it++) {
1019 if (namefile != "") { out << '\t' << nameCount[(*it)]; }
1023 out << "Names unique to " + accnosfile + " : " + toString(namesAccnos.size()) << endl;
1024 m->mothurOut("Names unique to " + accnosfile + " : " + toString(namesAccnos.size())); m->mothurOutEndLine();
1026 for (set<string>::iterator it = namesAccnos.begin(); it != namesAccnos.end(); it++) {
1028 if (namefile != "") { out << '\t' << nameCount[(*it)]; }
1032 out << "Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size()) << endl;
1033 m->mothurOut("Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size())); m->mothurOutEndLine();
1035 for (set<string>::iterator it = namesAccnos2.begin(); it != namesAccnos2.end(); it++) {
1037 if (namefile != "") { out << '\t' << nameCount[(*it)]; }
1043 outputNames.push_back(outputFileName); outputTypes["accnosreport"].push_back(outputFileName);
1048 catch(exception& e) {
1049 m->errorOut(e, "GetSeqsCommand", "compareAccnos");
1055 //**********************************************************************************************************************