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"
13 #include "counttable.h"
15 //**********************************************************************************************************************
16 vector<string> GetSeqsCommand::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);
30 CommandParameter paccnos2("accnos2", "InputTypes", "", "", "none", "none", "none","",false,false); parameters.push_back(paccnos2);
32 vector<string> myArray;
33 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
37 m->errorOut(e, "GetSeqsCommand", "setParameters");
41 //**********************************************************************************************************************
42 string GetSeqsCommand::getHelpString(){
44 string helpString = "";
45 helpString += "The get.seqs command reads an .accnos file and any of the following file types: fasta, name, group, count, list, taxonomy, quality or alignreport file.\n";
46 helpString += "It outputs a file containing only the sequences in the .accnos file.\n";
47 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";
48 helpString += "The dups parameter allows you to add the entire line from a name file if you add any name from the line. default=true. \n";
49 helpString += "The get.seqs command should be in the following format: get.seqs(accnos=yourAccnos, fasta=yourFasta).\n";
50 helpString += "Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta).\n";
51 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
55 m->errorOut(e, "GetSeqsCommand", "getHelpString");
60 //**********************************************************************************************************************
61 GetSeqsCommand::GetSeqsCommand(){
63 abort = true; calledHelp = true;
65 vector<string> tempOutNames;
66 outputTypes["fasta"] = tempOutNames;
67 outputTypes["taxonomy"] = tempOutNames;
68 outputTypes["name"] = tempOutNames;
69 outputTypes["group"] = tempOutNames;
70 outputTypes["alignreport"] = tempOutNames;
71 outputTypes["list"] = tempOutNames;
72 outputTypes["qfile"] = tempOutNames;
73 outputTypes["count"] = tempOutNames;
74 outputTypes["accnosreport"] = tempOutNames;
77 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
81 //**********************************************************************************************************************
82 string GetSeqsCommand::getOutputPattern(string type) {
86 if (type == "fasta") { pattern = "[filename],pick,[extension]"; }
87 else if (type == "taxonomy") { pattern = "[filename],pick,[extension]"; }
88 else if (type == "name") { pattern = "[filename],pick,[extension]"; }
89 else if (type == "group") { pattern = "[filename],pick,[extension]"; }
90 else if (type == "count") { pattern = "[filename],pick,[extension]"; }
91 else if (type == "list") { pattern = "[filename],pick,[extension]"; }
92 else if (type == "qfile") { pattern = "[filename],pick,[extension]"; }
93 else if (type == "accnosreport") { pattern = "[filename],pick.accnos.report"; }
94 else if (type == "alignreport") { pattern = "[filename],pick.align.report"; }
95 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
100 m->errorOut(e, "GetSeqsCommand", "getOutputPattern");
104 //**********************************************************************************************************************
105 GetSeqsCommand::GetSeqsCommand(string option) {
107 abort = false; calledHelp = false;
109 //allow user to run help
110 if(option == "help") { help(); abort = true; calledHelp = true; }
111 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
114 vector<string> myArray = setParameters();
116 OptionParser parser(option);
117 map<string,string> parameters = parser.getParameters();
119 ValidParameters validParameter;
120 map<string,string>::iterator it;
122 //check to make sure all parameters are valid for command
123 for (it = parameters.begin(); it != parameters.end(); it++) {
124 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
127 //initialize outputTypes
128 vector<string> tempOutNames;
129 outputTypes["fasta"] = tempOutNames;
130 outputTypes["taxonomy"] = tempOutNames;
131 outputTypes["name"] = tempOutNames;
132 outputTypes["group"] = tempOutNames;
133 outputTypes["alignreport"] = tempOutNames;
134 outputTypes["list"] = tempOutNames;
135 outputTypes["qfile"] = tempOutNames;
136 outputTypes["accnosreport"] = tempOutNames;
137 outputTypes["count"] = 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; }
219 it = parameters.find("count");
220 //user has given a template file
221 if(it != parameters.end()){
222 path = m->hasPath(it->second);
223 //if the user has not given a path then, add inputdir. else leave path alone.
224 if (path == "") { parameters["count"] = inputDir + it->second; }
229 //check for required parameters
230 accnosfile = validParameter.validFile(parameters, "accnos", true);
231 if (accnosfile == "not open") { abort = true; }
232 else if (accnosfile == "not found") {
233 accnosfile = m->getAccnosFile();
234 if (accnosfile != "") { m->mothurOut("Using " + accnosfile + " as input file for the accnos parameter."); m->mothurOutEndLine(); }
236 m->mothurOut("You have no valid accnos file and accnos is required."); m->mothurOutEndLine();
239 }else { m->setAccnosFile(accnosfile); }
241 if (accnosfile2 == "not found") { accnosfile2 = ""; }
243 fastafile = validParameter.validFile(parameters, "fasta", true);
244 if (fastafile == "not open") { fastafile = ""; abort = true; }
245 else if (fastafile == "not found") { fastafile = ""; }
246 else { m->setFastaFile(fastafile); }
248 namefile = validParameter.validFile(parameters, "name", true);
249 if (namefile == "not open") { namefile = ""; abort = true; }
250 else if (namefile == "not found") { namefile = ""; }
251 else { m->setNameFile(namefile); }
253 groupfile = validParameter.validFile(parameters, "group", true);
254 if (groupfile == "not open") { abort = true; }
255 else if (groupfile == "not found") { groupfile = ""; }
256 else { m->setGroupFile(groupfile); }
258 alignfile = validParameter.validFile(parameters, "alignreport", true);
259 if (alignfile == "not open") { abort = true; }
260 else if (alignfile == "not found") { alignfile = ""; }
262 listfile = validParameter.validFile(parameters, "list", true);
263 if (listfile == "not open") { abort = true; }
264 else if (listfile == "not found") { listfile = ""; }
265 else { m->setListFile(listfile); }
267 taxfile = validParameter.validFile(parameters, "taxonomy", true);
268 if (taxfile == "not open") { taxfile = ""; abort = true; }
269 else if (taxfile == "not found") { taxfile = ""; }
270 else { m->setTaxonomyFile(taxfile); }
272 qualfile = validParameter.validFile(parameters, "qfile", true);
273 if (qualfile == "not open") { abort = true; }
274 else if (qualfile == "not found") { qualfile = ""; }
275 else { m->setQualFile(qualfile); }
277 accnosfile2 = validParameter.validFile(parameters, "accnos2", true);
278 if (accnosfile2 == "not open") { abort = true; }
279 else if (accnosfile2 == "not found") { accnosfile2 = ""; }
281 countfile = validParameter.validFile(parameters, "count", true);
282 if (countfile == "not open") { countfile = ""; abort = true; }
283 else if (countfile == "not found") { countfile = ""; }
284 else { m->setCountTableFile(countfile); }
286 if ((namefile != "") && (countfile != "")) {
287 m->mothurOut("[ERROR]: you may only use one of the following: name or count."); m->mothurOutEndLine(); abort = true;
290 if ((groupfile != "") && (countfile != "")) {
291 m->mothurOut("[ERROR]: you may only use one of the following: group or count."); m->mothurOutEndLine(); abort=true;
295 string usedDups = "true";
296 string temp = validParameter.validFile(parameters, "dups", false); if (temp == "not found") { temp = "true"; usedDups = ""; }
297 dups = m->isTrue(temp);
299 if ((fastafile == "") && (namefile == "") && (groupfile == "") && (alignfile == "") && (listfile == "") && (taxfile == "") && (qualfile == "") && (accnosfile2 == "") && (countfile == "")) { m->mothurOut("You must provide one of the following: fasta, name, group, count, alignreport, taxonomy, quality or listfile."); m->mothurOutEndLine(); abort = true; }
301 if (countfile == "") {
302 if ((namefile == "") && ((fastafile != "") || (taxfile != ""))){
303 vector<string> files; files.push_back(fastafile); files.push_back(taxfile);
304 parser.getNameFile(files);
310 catch(exception& e) {
311 m->errorOut(e, "GetSeqsCommand", "GetSeqsCommand");
315 //**********************************************************************************************************************
317 int GetSeqsCommand::execute(){
320 if (abort == true) { if (calledHelp) { return 0; } return 2; }
322 //get names you want to keep
323 names = m->readAccnos(accnosfile);
325 if (m->control_pressed) { return 0; }
327 if (countfile != "") {
328 if ((fastafile != "") || (listfile != "") || (taxfile != "")) {
329 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");
333 //read through the correct file and output lines you want to keep
334 if (namefile != "") { readName(); }
335 if (fastafile != "") { readFasta(); }
336 if (groupfile != "") { readGroup(); }
337 if (countfile != "") { readCount(); }
338 if (alignfile != "") { readAlign(); }
339 if (listfile != "") { readList(); }
340 if (taxfile != "") { readTax(); }
341 if (qualfile != "") { readQual(); }
342 if (accnosfile2 != "") { compareAccnos(); }
344 if (m->debug) { runSanityCheck(); }
346 if (m->control_pressed) { outputTypes.clear(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
349 if (outputNames.size() != 0) {
350 m->mothurOutEndLine();
351 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
352 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
353 m->mothurOutEndLine();
355 //set fasta file as new current fastafile
357 itTypes = outputTypes.find("fasta");
358 if (itTypes != outputTypes.end()) {
359 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
362 itTypes = outputTypes.find("name");
363 if (itTypes != outputTypes.end()) {
364 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setNameFile(current); }
367 itTypes = outputTypes.find("group");
368 if (itTypes != outputTypes.end()) {
369 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setGroupFile(current); }
372 itTypes = outputTypes.find("list");
373 if (itTypes != outputTypes.end()) {
374 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setListFile(current); }
377 itTypes = outputTypes.find("taxonomy");
378 if (itTypes != outputTypes.end()) {
379 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setTaxonomyFile(current); }
382 itTypes = outputTypes.find("qfile");
383 if (itTypes != outputTypes.end()) {
384 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
387 itTypes = outputTypes.find("count");
388 if (itTypes != outputTypes.end()) {
389 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setCountTableFile(current); }
396 catch(exception& e) {
397 m->errorOut(e, "GetSeqsCommand", "execute");
402 //**********************************************************************************************************************
403 int GetSeqsCommand::readFasta(){
405 string thisOutputDir = outputDir;
406 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
407 map<string, string> variables;
408 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(fastafile));
409 variables["[extension]"] = m->getExtension(fastafile);
410 string outputFileName = getOutputFileName("fasta", variables);
412 m->openOutputFile(outputFileName, out);
416 m->openInputFile(fastafile, in);
419 bool wroteSomething = false;
420 int selectedCount = 0;
422 if (m->debug) { set<string> temp; sanity["fasta"] = temp; }
426 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
428 Sequence currSeq(in);
429 name = currSeq.getName();
431 if (!dups) {//adjust name if needed
432 map<string, string>::iterator it = uniqueMap.find(name);
433 if (it != uniqueMap.end()) { currSeq.setName(it->second); }
436 name = currSeq.getName();
439 //if this name is in the accnos file
440 if (names.count(name) != 0) {
441 wroteSomething = true;
443 currSeq.printSequence(out);
446 if (m->debug) { sanity["fasta"].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["fasta"].push_back(outputFileName);
458 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your fasta file."); m->mothurOutEndLine();
463 catch(exception& e) {
464 m->errorOut(e, "GetSeqsCommand", "readFasta");
468 //**********************************************************************************************************************
469 int GetSeqsCommand::readQual(){
471 string thisOutputDir = outputDir;
472 if (outputDir == "") { thisOutputDir += m->hasPath(qualfile); }
473 map<string, string> variables;
474 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(qualfile));
475 variables["[extension]"] = m->getExtension(qualfile);
476 string outputFileName = getOutputFileName("qfile", variables);
478 m->openOutputFile(outputFileName, out);
482 m->openInputFile(qualfile, in);
485 bool wroteSomething = false;
486 int selectedCount = 0;
488 if (m->debug) { set<string> temp; sanity["qual"] = temp; }
491 string saveName = "";
497 if (!dups) {//adjust name if needed
498 map<string, string>::iterator it = uniqueMap.find(name);
499 if (it != uniqueMap.end()) { name = it->second; }
502 if (name.length() != 0) {
503 saveName = name.substr(1);
506 if (c == 10 || c == 13 || c == -1){ break; }
513 char letter= in.get();
514 if(letter == '>'){ in.putback(letter); break; }
515 else{ scores += letter; }
520 if (names.count(saveName) != 0) {
521 wroteSomething = true;
523 out << name << endl << scores;
525 if (m->debug) { sanity["qual"].insert(name); }
534 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
535 outputNames.push_back(outputFileName); outputTypes["qfile"].push_back(outputFileName);
537 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your quality file."); m->mothurOutEndLine();
543 catch(exception& e) {
544 m->errorOut(e, "GetSeqsCommand", "readQual");
548 //**********************************************************************************************************************
549 int GetSeqsCommand::readCount(){
551 string thisOutputDir = outputDir;
552 if (outputDir == "") { thisOutputDir += m->hasPath(countfile); }
553 map<string, string> variables;
554 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(countfile));
555 variables["[extension]"] = m->getExtension(countfile);
556 string outputFileName = getOutputFileName("count", variables);
559 m->openOutputFile(outputFileName, out);
562 m->openInputFile(countfile, in);
564 bool wroteSomething = false;
565 int selectedCount = 0;
567 string headers = m->getline(in); m->gobble(in);
568 out << headers << endl;
570 string name, rest; int thisTotal;
573 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
575 in >> name; m->gobble(in);
576 in >> thisTotal; m->gobble(in);
577 rest = m->getline(in); m->gobble(in);
578 if (m->debug) { m->mothurOut("[DEBUG]: " + name + '\t' + rest + "\n"); }
580 if (names.count(name) != 0) {
581 out << name << '\t' << thisTotal << '\t' << rest << endl;
582 wroteSomething = true;
583 selectedCount+= thisTotal;
589 //check for groups that have been eliminated
591 if (ct.testGroups(outputFileName)) {
592 ct.readTable(outputFileName, true);
593 ct.printTable(outputFileName);
596 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
597 outputTypes["count"].push_back(outputFileName); outputNames.push_back(outputFileName);
599 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your count file."); m->mothurOutEndLine();
603 catch(exception& e) {
604 m->errorOut(e, "GetSeqsCommand", "readCount");
609 //**********************************************************************************************************************
610 int GetSeqsCommand::readList(){
612 string thisOutputDir = outputDir;
613 if (outputDir == "") { thisOutputDir += m->hasPath(listfile); }
614 map<string, string> variables;
615 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(listfile));
616 variables["[extension]"] = m->getExtension(listfile);
617 string outputFileName = getOutputFileName("list", variables);
619 m->openOutputFile(outputFileName, out);
622 m->openInputFile(listfile, in);
624 bool wroteSomething = false;
625 int selectedCount = 0;
627 if (m->debug) { set<string> temp; sanity["list"] = temp; }
633 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
635 //read in list vector
638 //make a new list vector
640 newList.setLabel(list.getLabel());
643 for (int i = 0; i < list.getNumBins(); i++) {
645 //parse out names that are in accnos file
646 string binnames = list.get(i);
647 vector<string> bnames;
648 m->splitAtComma(binnames, bnames);
650 string newNames = "";
651 for (int i = 0; i < bnames.size(); i++) {
652 string name = bnames[i];
653 //if that name is in the .accnos file, add it
654 if (names.count(name) != 0) { newNames += name + ","; selectedCount++; if (m->debug) { sanity["list"].insert(name); } }
657 //if there are names in this bin add to new list
658 if (newNames != "") {
659 newNames = newNames.substr(0, newNames.length()-1); //rip off extra comma
660 newList.push_back(newNames);
664 //print new listvector
665 if (newList.getNumBins() != 0) {
666 wroteSomething = true;
675 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
676 outputNames.push_back(outputFileName); outputTypes["list"].push_back(outputFileName);
678 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your list file."); m->mothurOutEndLine();
683 catch(exception& e) {
684 m->errorOut(e, "GetSeqsCommand", "readList");
688 //**********************************************************************************************************************
689 int GetSeqsCommand::readName(){
691 string thisOutputDir = outputDir;
692 if (outputDir == "") { thisOutputDir += m->hasPath(namefile); }
693 map<string, string> variables;
694 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(namefile));
695 variables["[extension]"] = m->getExtension(namefile);
696 string outputFileName = getOutputFileName("name", variables);
698 m->openOutputFile(outputFileName, out);
702 m->openInputFile(namefile, in);
703 string name, firstCol, secondCol;
705 bool wroteSomething = false;
706 int selectedCount = 0;
708 if (m->debug) { set<string> temp; sanity["name"] = temp; }
709 if (m->debug) { set<string> temp; sanity["dupname"] = temp; }
713 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
715 in >> firstCol; m->gobble(in);
719 if (dups) { hold = secondCol; }
721 vector<string> parsedNames;
722 m->splitAtComma(secondCol, parsedNames);
724 vector<string> validSecond;
725 for (int i = 0; i < parsedNames.size(); i++) {
726 if (names.count(parsedNames[i]) != 0) {
727 validSecond.push_back(parsedNames[i]);
728 if (m->debug) { sanity["dupname"].insert(parsedNames[i]); }
732 if ((dups) && (validSecond.size() != 0)) { //dups = true and we want to add someone, then add everyone
733 for (int i = 0; i < parsedNames.size(); i++) { names.insert(parsedNames[i]); if (m->debug) { sanity["dupname"].insert(parsedNames[i]); } }
734 out << firstCol << '\t' << hold << endl;
735 wroteSomething = true;
736 selectedCount += parsedNames.size();
737 if (m->debug) { sanity["name"].insert(firstCol); }
740 selectedCount += validSecond.size();
742 //if the name in the first column is in the set then print it and any other names in second column also in set
743 if (names.count(firstCol) != 0) {
745 wroteSomething = true;
747 out << firstCol << '\t';
749 //you know you have at least one valid second since first column is valid
750 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
751 out << validSecond[validSecond.size()-1] << endl;
753 if (m->debug) { sanity["name"].insert(firstCol); }
756 //make first name in set you come to first column and then add the remaining names to second column
759 //you want part of this row
760 if (validSecond.size() != 0) {
762 wroteSomething = true;
764 out << validSecond[0] << '\t';
765 //we are changing the unique name in the fasta file
766 uniqueMap[firstCol] = validSecond[0];
768 //you know you have at least one valid second since first column is valid
769 for (int i = 0; i < validSecond.size()-1; i++) { out << validSecond[i] << ','; }
770 out << validSecond[validSecond.size()-1] << endl;
772 if (m->debug) { sanity["name"].insert(validSecond[0]); }
781 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
782 outputNames.push_back(outputFileName); outputTypes["name"].push_back(outputFileName);
784 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your name file."); m->mothurOutEndLine();
789 catch(exception& e) {
790 m->errorOut(e, "GetSeqsCommand", "readName");
795 //**********************************************************************************************************************
796 int GetSeqsCommand::readGroup(){
798 string thisOutputDir = outputDir;
799 if (outputDir == "") { thisOutputDir += m->hasPath(groupfile); }
800 map<string, string> variables;
801 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(groupfile));
802 variables["[extension]"] = m->getExtension(groupfile);
803 string outputFileName = getOutputFileName("group", variables);
805 m->openOutputFile(outputFileName, out);
809 m->openInputFile(groupfile, in);
812 bool wroteSomething = false;
813 int selectedCount = 0;
815 if (m->debug) { set<string> temp; sanity["group"] = temp; }
819 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
822 in >> name; //read from first column
823 in >> group; //read from second column
826 //if this name is in the accnos file
827 if (names.count(name) != 0) {
828 wroteSomething = true;
830 out << name << '\t' << group << endl;
833 if (m->debug) { sanity["group"].insert(name); }
841 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
842 outputNames.push_back(outputFileName); outputTypes["group"].push_back(outputFileName);
844 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your group file."); m->mothurOutEndLine();
850 catch(exception& e) {
851 m->errorOut(e, "GetSeqsCommand", "readGroup");
855 //**********************************************************************************************************************
856 int GetSeqsCommand::readTax(){
858 string thisOutputDir = outputDir;
859 if (outputDir == "") { thisOutputDir += m->hasPath(taxfile); }
860 map<string, string> variables;
861 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(taxfile));
862 variables["[extension]"] = m->getExtension(taxfile);
863 string outputFileName = getOutputFileName("taxonomy", variables);
865 m->openOutputFile(outputFileName, out);
868 m->openInputFile(taxfile, in);
871 bool wroteSomething = false;
872 int selectedCount = 0;
874 if (m->debug) { set<string> temp; sanity["tax"] = temp; }
878 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
880 in >> name; //read from first column
881 in >> tax; //read from second column
883 if (!dups) {//adjust name if needed
884 map<string, string>::iterator it = uniqueMap.find(name);
885 if (it != uniqueMap.end()) { name = it->second; }
888 //if this name is in the accnos file
889 if (names.count(name) != 0) {
890 wroteSomething = true;
892 out << name << '\t' << tax << endl;
895 if (m->debug) { sanity["tax"].insert(name); }
903 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
904 outputNames.push_back(outputFileName); outputTypes["taxonomy"].push_back(outputFileName);
906 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your taxonomy file."); m->mothurOutEndLine();
911 catch(exception& e) {
912 m->errorOut(e, "GetSeqsCommand", "readTax");
916 //**********************************************************************************************************************
917 //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
918 int GetSeqsCommand::readAlign(){
920 string thisOutputDir = outputDir;
921 if (outputDir == "") { thisOutputDir += m->hasPath(alignfile); }
922 map<string, string> variables;
923 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(alignfile));
924 string outputFileName = getOutputFileName("alignreport", variables);
926 m->openOutputFile(outputFileName, out);
930 m->openInputFile(alignfile, in);
933 bool wroteSomething = false;
934 int selectedCount = 0;
936 //read column headers
937 for (int i = 0; i < 16; i++) {
938 if (!in.eof()) { in >> junk; out << junk << '\t'; }
945 if (m->control_pressed) { in.close(); out.close(); m->mothurRemove(outputFileName); return 0; }
948 in >> name; //read from first column
950 if (!dups) {//adjust name if needed
951 map<string, string>::iterator it = uniqueMap.find(name);
952 if (it != uniqueMap.end()) { name = it->second; }
955 //if this name is in the accnos file
956 if (names.count(name) != 0) {
957 wroteSomething = true;
963 for (int i = 0; i < 15; i++) {
964 if (!in.eof()) { in >> junk; out << junk << '\t'; }
969 }else {//still read just don't do anything with it
971 for (int i = 0; i < 15; i++) {
972 if (!in.eof()) { in >> junk; }
982 if (wroteSomething == false) { m->mothurOut("Your file does not contain any sequence from the .accnos file."); m->mothurOutEndLine(); }
983 outputNames.push_back(outputFileName); outputTypes["alignreport"].push_back(outputFileName);
985 m->mothurOut("Selected " + toString(selectedCount) + " sequences from your alignreport file."); m->mothurOutEndLine();
990 catch(exception& e) {
991 m->errorOut(e, "GetSeqsCommand", "readAlign");
995 //**********************************************************************************************************************
996 //just looking at common mistakes.
997 int GetSeqsCommand::runSanityCheck(){
999 string thisOutputDir = outputDir;
1000 if (outputDir == "") { thisOutputDir += m->hasPath(fastafile); }
1001 string filename = outputDir + "get.seqs.debug.report";
1004 m->openOutputFile(filename, out);
1007 //compare fasta, name, qual and taxonomy if given to make sure they contain the same seqs
1008 if (fastafile != "") {
1009 if (namefile != "") { //compare with fasta
1010 if (sanity["fasta"] != sanity["name"]) { //create mismatch file
1011 createMisMatchFile(out, fastafile, namefile, sanity["fasta"], sanity["name"]);
1014 if (qualfile != "") {
1015 if (sanity["fasta"] != sanity["qual"]) { //create mismatch file
1016 createMisMatchFile(out, fastafile, qualfile, sanity["fasta"], sanity["qual"]);
1019 if (taxfile != "") {
1020 if (sanity["fasta"] != sanity["tax"]) { //create mismatch file
1021 createMisMatchFile(out, fastafile, taxfile, sanity["fasta"], sanity["tax"]);
1026 //compare dupnames, groups and list if given to make sure they match
1027 if (namefile != "") {
1028 if (groupfile != "") {
1029 if (sanity["dupname"] != sanity["group"]) { //create mismatch file
1030 createMisMatchFile(out, namefile, groupfile, sanity["dupname"], sanity["group"]);
1033 if (listfile != "") {
1034 if (sanity["dupname"] != sanity["list"]) { //create mismatch file
1035 createMisMatchFile(out, namefile, listfile, sanity["dupname"], sanity["list"]);
1040 if ((groupfile != "") && (fastafile != "")) {
1041 if (sanity["fasta"] != sanity["group"]) { //create mismatch file
1042 createMisMatchFile(out, fastafile, groupfile, sanity["fasta"], sanity["group"]);
1049 if (m->isBlank(filename)) { m->mothurRemove(filename); }
1050 else { m->mothurOut("\n[DEBUG]: " + filename + " contains the file mismatches.\n");outputNames.push_back(filename); outputTypes["debug"].push_back(filename); }
1054 catch(exception& e) {
1055 m->errorOut(e, "GetSeqsCommand", "runSanityCheck");
1059 //**********************************************************************************************************************
1060 //just looking at common mistakes.
1061 int GetSeqsCommand::createMisMatchFile(ofstream& out, string filename1, string filename2, set<string> set1, set<string> set2){
1063 out << "****************************************" << endl << endl;
1064 out << "Names unique to " << filename1 << ":\n";
1066 //remove names in set1 that are also in set2
1067 for (set<string>::iterator it = set1.begin(); it != set1.end();) {
1070 if (set2.count(name) == 0) { out << name << endl; } //name unique to set1
1071 else { set2.erase(name); } //you are in both so erase
1075 out << "\nNames unique to " << filename2 << ":\n";
1077 for (set<string>::iterator it = set2.begin(); it != set2.end(); it++) { out << *it << endl; }
1079 out << "****************************************" << endl << endl;
1083 catch(exception& e) {
1084 m->errorOut(e, "GetSeqsCommand", "runSanityCheck");
1088 //**********************************************************************************************************************
1090 int GetSeqsCommand::compareAccnos(){
1093 string thisOutputDir = outputDir;
1094 if (outputDir == "") { thisOutputDir += m->hasPath(accnosfile); }
1095 map<string, string> variables;
1096 variables["[filename]"] = thisOutputDir + m->getRootName(m->getSimpleName(accnosfile));
1097 string outputFileName = getOutputFileName("accnosreport", variables);
1100 m->openOutputFile(outputFileName, out);
1103 m->openInputFile(accnosfile2, in);
1106 set<string> namesAccnos2;
1107 set<string> namesDups;
1108 set<string> namesAccnos = names;
1110 map<string, int> nameCount;
1112 if (namefile != "") {
1114 m->openInputFile(namefile, inName);
1117 while(!inName.eof()){
1119 if (m->control_pressed) { inName.close(); return 0; }
1121 string thisname, repnames;
1123 inName >> thisname; m->gobble(inName); //read from first column
1124 inName >> repnames; //read from second column
1126 int num = m->getNumNames(repnames);
1127 nameCount[thisname] = num;
1137 if (namesAccnos.count(name) == 0){ //name unique to accnos2
1138 int pos = name.find_last_of('_');
1139 string tempName = name;
1140 if (pos != string::npos) { tempName = tempName.substr(pos+1); cout << tempName << endl; }
1141 if (namesAccnos.count(tempName) == 0){
1142 namesAccnos2.insert(name);
1143 }else { //you are in both so erase
1144 namesAccnos.erase(name);
1145 namesDups.insert(name);
1147 }else { //you are in both so erase
1148 namesAccnos.erase(name);
1149 namesDups.insert(name);
1156 out << "Names in both files : " + toString(namesDups.size()) << endl;
1157 m->mothurOut("Names in both files : " + toString(namesDups.size())); m->mothurOutEndLine();
1159 for (set<string>::iterator it = namesDups.begin(); it != namesDups.end(); it++) {
1161 if (namefile != "") { out << '\t' << nameCount[(*it)]; }
1165 out << "Names unique to " + accnosfile + " : " + toString(namesAccnos.size()) << endl;
1166 m->mothurOut("Names unique to " + accnosfile + " : " + toString(namesAccnos.size())); m->mothurOutEndLine();
1168 for (set<string>::iterator it = namesAccnos.begin(); it != namesAccnos.end(); it++) {
1170 if (namefile != "") { out << '\t' << nameCount[(*it)]; }
1174 out << "Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size()) << endl;
1175 m->mothurOut("Names unique to " + accnosfile2 + " : " + toString(namesAccnos2.size())); m->mothurOutEndLine();
1177 for (set<string>::iterator it = namesAccnos2.begin(); it != namesAccnos2.end(); it++) {
1179 if (namefile != "") { out << '\t' << nameCount[(*it)]; }
1185 outputNames.push_back(outputFileName); outputTypes["accnosreport"].push_back(outputFileName);
1190 catch(exception& e) {
1191 m->errorOut(e, "GetSeqsCommand", "compareAccnos");
1197 //**********************************************************************************************************************