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 GetSeqsCommand::GetSeqsCommand(string option) {
81 abort = false; calledHelp = false;
83 //allow user to run help
84 if(option == "help") { help(); abort = true; calledHelp = true; }
85 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
88 vector<string> myArray = setParameters();
90 OptionParser parser(option);
91 map<string,string> parameters = parser.getParameters();
93 ValidParameters validParameter;
94 map<string,string>::iterator it;
96 //check to make sure all parameters are valid for command
97 for (it = parameters.begin(); it != parameters.end(); it++) {
98 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
101 //initialize outputTypes
102 vector<string> tempOutNames;
103 outputTypes["fasta"] = tempOutNames;
104 outputTypes["taxonomy"] = tempOutNames;
105 outputTypes["name"] = tempOutNames;
106 outputTypes["group"] = tempOutNames;
107 outputTypes["alignreport"] = tempOutNames;
108 outputTypes["list"] = tempOutNames;
109 outputTypes["qfile"] = tempOutNames;
110 outputTypes["accnosreport"] = tempOutNames;
112 //if the user changes the output directory command factory will send this info to us in the output parameter
113 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
115 //if the user changes the input directory command factory will send this info to us in the output parameter
116 string inputDir = validParameter.validFile(parameters, "inputdir", false);
117 if (inputDir == "not found"){ inputDir = ""; }
120 it = parameters.find("alignreport");
121 //user has given a template file
122 if(it != parameters.end()){
123 path = m->hasPath(it->second);
124 //if the user has not given a path then, add inputdir. else leave path alone.
125 if (path == "") { parameters["alignreport"] = inputDir + it->second; }
128 it = parameters.find("fasta");
129 //user has given a template file
130 if(it != parameters.end()){
131 path = m->hasPath(it->second);
132 //if the user has not given a path then, add inputdir. else leave path alone.
133 if (path == "") { parameters["fasta"] = inputDir + it->second; }
136 it = parameters.find("accnos");
137 //user has given a template file
138 if(it != parameters.end()){
139 path = m->hasPath(it->second);
140 //if the user has not given a path then, add inputdir. else leave path alone.
141 if (path == "") { parameters["accnos"] = inputDir + it->second; }
144 it = parameters.find("accnos2");
145 //user has given a template file
146 if(it != parameters.end()){
147 path = m->hasPath(it->second);
148 //if the user has not given a path then, add inputdir. else leave path alone.
149 if (path == "") { parameters["accnos2"] = inputDir + it->second; }
152 it = parameters.find("list");
153 //user has given a template file
154 if(it != parameters.end()){
155 path = m->hasPath(it->second);
156 //if the user has not given a path then, add inputdir. else leave path alone.
157 if (path == "") { parameters["list"] = inputDir + it->second; }
160 it = parameters.find("name");
161 //user has given a template file
162 if(it != parameters.end()){
163 path = m->hasPath(it->second);
164 //if the user has not given a path then, add inputdir. else leave path alone.
165 if (path == "") { parameters["name"] = inputDir + it->second; }
168 it = parameters.find("group");
169 //user has given a template file
170 if(it != parameters.end()){
171 path = m->hasPath(it->second);
172 //if the user has not given a path then, add inputdir. else leave path alone.
173 if (path == "") { parameters["group"] = inputDir + it->second; }
176 it = parameters.find("taxonomy");
177 //user has given a template file
178 if(it != parameters.end()){
179 path = m->hasPath(it->second);
180 //if the user has not given a path then, add inputdir. else leave path alone.
181 if (path == "") { parameters["taxonomy"] = inputDir + it->second; }
184 it = parameters.find("qfile");
185 //user has given a template file
186 if(it != parameters.end()){
187 path = m->hasPath(it->second);
188 //if the user has not given a path then, add inputdir. else leave path alone.
189 if (path == "") { parameters["qfile"] = inputDir + it->second; }
194 //check for required parameters
195 accnosfile = validParameter.validFile(parameters, "accnos", true);
196 if (accnosfile == "not open") { abort = true; }
197 else if (accnosfile == "not found") {
198 accnosfile = m->getAccnosFile();
199 if (accnosfile != "") { m->mothurOut("Using " + accnosfile + " as input file for the accnos parameter."); m->mothurOutEndLine(); }
201 m->mothurOut("You have no valid accnos file and accnos is required."); m->mothurOutEndLine();
204 }else { m->setAccnosFile(accnosfile); }
206 if (accnosfile2 == "not found") { accnosfile2 = ""; }
208 fastafile = validParameter.validFile(parameters, "fasta", true);
209 if (fastafile == "not open") { fastafile = ""; abort = true; }
210 else if (fastafile == "not found") { fastafile = ""; }
211 else { m->setFastaFile(fastafile); }
213 namefile = validParameter.validFile(parameters, "name", true);
214 if (namefile == "not open") { namefile = ""; abort = true; }
215 else if (namefile == "not found") { namefile = ""; }
216 else { m->setNameFile(namefile); }
218 groupfile = validParameter.validFile(parameters, "group", true);
219 if (groupfile == "not open") { abort = true; }
220 else if (groupfile == "not found") { groupfile = ""; }
221 else { m->setGroupFile(groupfile); }
223 alignfile = validParameter.validFile(parameters, "alignreport", true);
224 if (alignfile == "not open") { abort = true; }
225 else if (alignfile == "not found") { alignfile = ""; }
227 listfile = validParameter.validFile(parameters, "list", true);
228 if (listfile == "not open") { abort = true; }
229 else if (listfile == "not found") { listfile = ""; }
230 else { m->setListFile(listfile); }
232 taxfile = validParameter.validFile(parameters, "taxonomy", true);
233 if (taxfile == "not open") { taxfile = ""; abort = true; }
234 else if (taxfile == "not found") { taxfile = ""; }
235 else { m->setTaxonomyFile(taxfile); }
237 qualfile = validParameter.validFile(parameters, "qfile", true);
238 if (qualfile == "not open") { abort = true; }
239 else if (qualfile == "not found") { qualfile = ""; }
240 else { m->setQualFile(qualfile); }
242 accnosfile2 = validParameter.validFile(parameters, "accnos2", true);
243 if (accnosfile2 == "not open") { abort = true; }
244 else if (accnosfile2 == "not found") { accnosfile2 = ""; }
247 string usedDups = "true";
248 string temp = validParameter.validFile(parameters, "dups", false); if (temp == "not found") { temp = "true"; usedDups = ""; }
249 dups = m->isTrue(temp);
251 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; }
253 if ((namefile == "") && ((fastafile != "") || (taxfile != ""))){
254 vector<string> files; files.push_back(fastafile); files.push_back(taxfile);
255 parser.getNameFile(files);
260 catch(exception& e) {
261 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
265 //**********************************************************************************************************************
267 int GetSeqsCommand::execute(){
270 if (abort == true) { if (calledHelp) { return 0; } return 2; }
272 //get names you want to keep
275 if (m->control_pressed) { return 0; }
277 //read through the correct file and output lines you want to keep
278 if (namefile != "") { readName(); }
279 if (fastafile != "") { readFasta(); }
280 if (groupfile != "") { readGroup(); }
281 if (alignfile != "") { readAlign(); }
282 if (listfile != "") { readList(); }
283 if (taxfile != "") { readTax(); }
284 if (qualfile != "") { readQual(); }
285 if (accnosfile2 != "") { compareAccnos(); }
287 if (m->debug) { runSanityCheck(); }
289 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
292 if (outputNames.size() != 0) {
293 m->mothurOutEndLine();
294 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
295 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
296 m->mothurOutEndLine();
298 //set fasta file as new current fastafile
300 itTypes = outputTypes.find("fasta");
301 if (itTypes != outputTypes.end()) {
302 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
305 itTypes = outputTypes.find("name");
306 if (itTypes != outputTypes.end()) {
307 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
310 itTypes = outputTypes.find("group");
311 if (itTypes != outputTypes.end()) {
312 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
315 itTypes = outputTypes.find("list");
316 if (itTypes != outputTypes.end()) {
317 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
320 itTypes = outputTypes.find("taxonomy");
321 if (itTypes != outputTypes.end()) {
322 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
325 itTypes = outputTypes.find("qfile");
326 if (itTypes != outputTypes.end()) {
327 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
335 catch(exception& e) {
336 m->errorOut(e, "GetSeqsCommand", "execute");
341 //**********************************************************************************************************************
342 int GetSeqsCommand::readFasta(){
344 string thisOutputDir = outputDir;
345 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
346 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(fastafile)) + "pick" + m->getExtension(fastafile);
348 m->openOutputFile(outputFileName, out);
352 m->openInputFile(fastafile, in);
355 bool wroteSomething = false;
356 int selectedCount = 0;
358 if (m->debug) { set<string> temp; sanity["fasta"] = temp; }
362 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
364 Sequence currSeq(in);
365 name = currSeq.getName();
368 //if this name is in the accnos file
369 if (names.count(name) != 0) {
370 wroteSomething = true;
372 currSeq.printSequence(out);
375 if (m->debug) { sanity["fasta"].insert(name); }
384 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
385 outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
387 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your fasta file."); m->mothurOutEndLine();
392 catch(exception& e) {
393 m->errorOut(e, "GetSeqsCommand", "readFasta");
397 //**********************************************************************************************************************
398 int GetSeqsCommand::readQual(){
400 string thisOutputDir = outputDir;
401 if (outputDir == "") { thisOutputDir += m->hasPath(qualfile); }
402 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(qualfile)) + "pick" + m->getExtension(qualfile);
404 m->openOutputFile(outputFileName, out);
408 m->openInputFile(qualfile, in);
411 bool wroteSomething = false;
412 int selectedCount = 0;
414 if (m->debug) { set<string> temp; sanity["qual"] = temp; }
417 string saveName = "";
423 if (name.length() != 0) {
424 saveName = name.substr(1);
427 if (c == 10 || c == 13){ break; }
434 char letter= in.get();
435 if(letter == '>'){ in.putback(letter); break; }
436 else{ scores += letter; }
441 if (names.count(saveName) != 0) {
442 wroteSomething = true;
444 out << name << endl << scores;
446 if (m->debug) { sanity["qual"].insert(name); }
455 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
456 outputNames.push_back(outputFileName); outputTypes["qfile"].push_back(outputFileName);
458 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your quality file."); m->mothurOutEndLine();
464 catch(exception& e) {
465 m->errorOut(e, "GetSeqsCommand", "readQual");
469 //**********************************************************************************************************************
470 int GetSeqsCommand::readList(){
472 string thisOutputDir = outputDir;
473 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
474 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(listfile)) + "pick" + m->getExtension(listfile);
476 m->openOutputFile(outputFileName, out);
479 m->openInputFile(listfile, in);
481 bool wroteSomething = false;
482 int selectedCount = 0;
484 if (m->debug) { set<string> temp; sanity["list"] = temp; }
490 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
492 //read in list vector
495 //make a new list vector
497 newList.setLabel(list.getLabel());
500 for (int i = 0; i < list.getNumBins(); i++) {
502 //parse out names that are in accnos file
503 string binnames = list.get(i);
505 string newNames = "";
506 while (binnames.find_first_of(',') != -1) {
507 string name = binnames.substr(0,binnames.find_first_of(','));
508 binnames = binnames.substr(binnames.find_first_of(',')+1, binnames.length());
510 //if that name is in the .accnos file, add it
511 if (names.count(name) != 0) { newNames += name + ","; selectedCount++; if (m->debug) { sanity["list"].insert(name); } }
515 if (names.count(binnames) != 0) { newNames += binnames + ","; selectedCount++; if (m->debug) { sanity["list"].insert(binnames); } }
517 //if there are names in this bin add to new list
518 if (newNames != "") {
519 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
520 newList.push_back(newNames);
524 //print new listvector
525 if (newList.getNumBins() != 0) {
526 wroteSomething = true;
535 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
536 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
538 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your list file."); m->mothurOutEndLine();
543 catch(exception& e) {
544 m->errorOut(e, "GetSeqsCommand", "readList");
548 //**********************************************************************************************************************
549 int GetSeqsCommand::readName(){
551 string thisOutputDir = outputDir;
552 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
553 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(namefile)) + "pick" + m->getExtension(namefile);
555 m->openOutputFile(outputFileName, out);
559 m->openInputFile(namefile, in);
560 string name, firstCol, secondCol;
562 bool wroteSomething = false;
563 int selectedCount = 0;
565 if (m->debug) { set<string> temp; sanity["name"] = temp; }
566 if (m->debug) { set<string> temp; sanity["dupname"] = temp; }
570 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
576 if (dups) { hold = secondCol; }
578 vector<string> parsedNames;
579 m->splitAtComma(secondCol, parsedNames);
581 vector<string> validSecond;
582 for (int i = 0; i < parsedNames.size(); i++) {
583 if (names.count(parsedNames[i]) != 0) {
584 validSecond.push_back(parsedNames[i]);
585 if (m->debug) { sanity["dupname"].insert(parsedNames[i]); }
589 if ((dups) && (validSecond.size() != 0)) { //dups = true and we want to add someone, then add everyone
590 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); if (m->debug) { sanity["dupname"].insert(parsedNames[i]); } }
591 out << firstCol << '\t' << hold << endl;
592 wroteSomething = true;
593 selectedCount += parsedNames.size();
594 if (m->debug) { sanity["name"].insert(firstCol); }
596 selectedCount += validSecond.size();
598 //if the name in the first column is in the set then print it and any other names in second column also in set
599 if (names.count(firstCol) != 0) {
601 wroteSomething = true;
603 out << firstCol << '\t';
605 //you know you have at least one valid second since first column is valid
606 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
607 out << validSecond[validSecond.size()-1] << endl;
609 if (m->debug) { sanity["name"].insert(firstCol); }
612 //make first name in set you come to first column and then add the remaining names to second column
614 //you want part of this row
615 if (validSecond.size() != 0) {
617 wroteSomething = true;
619 out << validSecond[0] << '\t';
621 //you know you have at least one valid second since first column is valid
622 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
623 out << validSecond[validSecond.size()-1] << endl;
625 if (m->debug) { sanity["name"].insert(validSecond[0]); }
634 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
635 outputNames.push_back(outputFileName); outputTypes["name"].push_back(outputFileName);
637 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your name file."); m->mothurOutEndLine();
642 catch(exception& e) {
643 m->errorOut(e, "GetSeqsCommand", "readName");
648 //**********************************************************************************************************************
649 int GetSeqsCommand::readGroup(){
651 string thisOutputDir = outputDir;
652 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
653 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(groupfile)) + "pick" + m->getExtension(groupfile);
655 m->openOutputFile(outputFileName, out);
659 m->openInputFile(groupfile, in);
662 bool wroteSomething = false;
663 int selectedCount = 0;
665 if (m->debug) { set<string> temp; sanity["group"] = temp; }
669 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
672 in >> name; //read from first column
673 in >> group; //read from second column
675 //if this name is in the accnos file
676 if (names.count(name) != 0) {
677 wroteSomething = true;
679 out << name << '\t' << group << endl;
682 if (m->debug) { sanity["group"].insert(name); }
690 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
691 outputNames.push_back(outputFileName); outputTypes["group"].push_back(outputFileName);
693 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your group file."); m->mothurOutEndLine();
699 catch(exception& e) {
700 m->errorOut(e, "GetSeqsCommand", "readGroup");
704 //**********************************************************************************************************************
705 int GetSeqsCommand::readTax(){
707 string thisOutputDir = outputDir;
708 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
709 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(taxfile)) + "pick" + m->getExtension(taxfile);
711 m->openOutputFile(outputFileName, out);
714 m->openInputFile(taxfile, in);
717 bool wroteSomething = false;
718 int selectedCount = 0;
720 if (m->debug) { set<string> temp; sanity["tax"] = temp; }
724 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
726 in >> name; //read from first column
727 in >> tax; //read from second column
729 //if this name is in the accnos file
730 if (names.count(name) != 0) {
731 wroteSomething = true;
733 out << name << '\t' << tax << endl;
736 if (m->debug) { sanity["tax"].insert(name); }
744 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
745 outputNames.push_back(outputFileName); outputTypes["taxonomy"].push_back(outputFileName);
747 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your taxonomy file."); m->mothurOutEndLine();
752 catch(exception& e) {
753 m->errorOut(e, "GetSeqsCommand", "readTax");
757 //**********************************************************************************************************************
758 //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
759 int GetSeqsCommand::readAlign(){
761 string thisOutputDir = outputDir;
762 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
763 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(alignfile)) + "pick.align.report";
765 m->openOutputFile(outputFileName, out);
769 m->openInputFile(alignfile, in);
772 bool wroteSomething = false;
773 int selectedCount = 0;
775 //read column headers
776 for (int i = 0; i < 16; i++) {
777 if (!in.eof()) { in >> junk; out << junk << '\t'; }
784 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
787 in >> name; //read from first column
789 //if this name is in the accnos file
790 if (names.count(name) != 0) {
791 wroteSomething = true;
797 for (int i = 0; i < 15; i++) {
798 if (!in.eof()) { in >> junk; out << junk << '\t'; }
803 }else {//still read just don't do anything with it
805 for (int i = 0; i < 15; i++) {
806 if (!in.eof()) { in >> junk; }
816 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
817 outputNames.push_back(outputFileName); outputTypes["alignreport"].push_back(outputFileName);
819 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your alignreport file."); m->mothurOutEndLine();
824 catch(exception& e) {
825 m->errorOut(e, "GetSeqsCommand", "readAlign");
829 //**********************************************************************************************************************
831 int GetSeqsCommand::readAccnos(){
835 m->openInputFile(accnosfile, in);
850 catch(exception& e) {
851 m->errorOut(e, "GetSeqsCommand", "readAccnos");
855 //**********************************************************************************************************************
856 //just looking at common mistakes.
857 int GetSeqsCommand::runSanityCheck(){
859 string thisOutputDir = outputDir;
860 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
861 string filename = outputDir + "get.seqs.debug.report";
864 m->openOutputFile(filename, out);
867 //compare fasta, name, qual and taxonomy if given to make sure they contain the same seqs
868 if (fastafile != "") {
869 if (namefile != "") { //compare with fasta
870 if (sanity["fasta"] != sanity["name"]) { //create mismatch file
871 createMisMatchFile(out, fastafile, namefile, sanity["fasta"], sanity["name"]);
874 if (qualfile != "") {
875 if (sanity["fasta"] != sanity["qual"]) { //create mismatch file
876 createMisMatchFile(out, fastafile, qualfile, sanity["fasta"], sanity["qual"]);
880 if (sanity["fasta"] != sanity["tax"]) { //create mismatch file
881 createMisMatchFile(out, fastafile, taxfile, sanity["fasta"], sanity["tax"]);
886 //compare dupnames, groups and list if given to make sure they match
887 if (namefile != "") {
888 if (groupfile != "") {
889 if (sanity["dupname"] != sanity["group"]) { //create mismatch file
890 createMisMatchFile(out, namefile, groupfile, sanity["dupname"], sanity["group"]);
893 if (listfile != "") {
894 if (sanity["dupname"] != sanity["list"]) { //create mismatch file
895 createMisMatchFile(out, namefile, listfile, sanity["dupname"], sanity["list"]);
900 if ((groupfile != "") && (fastafile != "")) {
901 if (sanity["fasta"] != sanity["group"]) { //create mismatch file
902 createMisMatchFile(out, fastafile, groupfile, sanity["fasta"], sanity["group"]);
909 if (m->isBlank(filename)) { m->mothurRemove(filename); }
910 else { m->mothurOut("\n[DEBUG]: " + filename + " contains the file mismatches.\n");outputNames.push_back(filename); outputTypes["debug"].push_back(filename); }
914 catch(exception& e) {
915 m->errorOut(e, "GetSeqsCommand", "runSanityCheck");
919 //**********************************************************************************************************************
920 //just looking at common mistakes.
921 int GetSeqsCommand::createMisMatchFile(ofstream& out, string filename1, string filename2, set<string> set1, set<string> set2){
923 out << "****************************************" << endl << endl;
924 out << "Names unique to " << filename1 << ":\n";
926 //remove names in set1 that are also in set2
927 for (set<string>::iterator it = set1.begin(); it != set1.end();) {
930 if (set2.count(name) == 0) { out << name << endl; } //name unique to set1
931 else { set2.erase(name); } //you are in both so erase
935 out << "\nNames unique to " << filename2 << ":\n";
937 for (set<string>::iterator it = set2.begin(); it != set2.end(); it++) { out << *it << endl; }
939 out << "****************************************" << endl << endl;
943 catch(exception& e) {
944 m->errorOut(e, "GetSeqsCommand", "runSanityCheck");
948 //**********************************************************************************************************************
950 int GetSeqsCommand::compareAccnos(){
953 string thisOutputDir = outputDir;
954 if (outputDir == "") { thisOutputDir += m->hasPath(accnosfile); }
955 string outputFileName = thisOutputDir + m->getRootName(m->getSimpleName(accnosfile)) + "accnos.report";
957 m->openOutputFile(outputFileName, out);
960 m->openInputFile(accnosfile2, in);
963 set<string> namesAccnos2;
964 set<string> namesDups;
965 set<string> namesAccnos = names;
967 map<string, int> nameCount;
969 if (namefile != "") {
971 m->openInputFile(namefile, inName);
974 while(!inName.eof()){
976 if (m->control_pressed) { inName.close(); return 0; }
978 string thisname, repnames;
980 inName >> thisname; m->gobble(inName); //read from first column
981 inName >> repnames; //read from second column
983 int num = m->getNumNames(repnames);
984 nameCount[thisname] = num;
994 if (namesAccnos.count(name) == 0){ //name unique to accnos2
995 int pos = name.find_last_of('_');
996 string tempName = name;
997 if (pos != string::npos) { tempName = tempName.substr(pos+1); cout << tempName << endl; }
998 if (namesAccnos.count(tempName) == 0){
999 namesAccnos2.insert(name);
1000 }else { //you are in both so erase
1001 namesAccnos.erase(name);
1002 namesDups.insert(name);
1004 }else { //you are in both so erase
1005 namesAccnos.erase(name);
1006 namesDups.insert(name);
1013 out << "Names in both files : " + toString(namesDups.size()) << endl;
1014 m->mothurOut("Names in both files : " + toString(namesDups.size())); m->mothurOutEndLine();
1016 for (set<string>::iterator it = namesDups.begin(); it != namesDups.end(); it++) {
1018 if (namefile != "") { out << '\t' << nameCount[(*it)]; }
1022 out << "Names unique to " + accnosfile + " : " + toString(namesAccnos.size()) << endl;
1023 m->mothurOut("Names unique to " + accnosfile + " : " + toString(namesAccnos.size())); m->mothurOutEndLine();
1025 for (set<string>::iterator it = namesAccnos.begin(); it != namesAccnos.end(); it++) {
1027 if (namefile != "") { out << '\t' << nameCount[(*it)]; }
1031 out << "Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size()) << endl;
1032 m->mothurOut("Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size())); m->mothurOutEndLine();
1034 for (set<string>::iterator it = namesAccnos2.begin(); it != namesAccnos2.end(); it++) {
1036 if (namefile != "") { out << '\t' << nameCount[(*it)]; }
1042 outputNames.push_back(outputFileName); outputTypes["accnosreport"].push_back(outputFileName);
1047 catch(exception& e) {
1048 m->errorOut(e, "GetSeqsCommand", "readAccnos");
1054 //**********************************************************************************************************************