2 * consensusseqscommand.cpp
5 * Created by westcott on 11/23/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "consensusseqscommand.h"
11 #include "sequence.hpp"
12 #include "inputdata.h"
14 //**********************************************************************************************************************
15 vector<string> ConsensusSeqsCommand::setParameters(){
17 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
18 CommandParameter pname("name", "InputTypes", "", "", "namecount", "none", "none",false,false); parameters.push_back(pname);
19 CommandParameter pcount("count", "InputTypes", "", "", "namecount", "none", "none",false,false); parameters.push_back(pcount);
20 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(plist);
21 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
22 CommandParameter pcutoff("cutoff", "Number", "", "100", "", "", "",false,false); parameters.push_back(pcutoff);
23 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
24 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
26 vector<string> myArray;
27 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
31 m->errorOut(e, "ConsensusSeqsCommand", "setParameters");
35 //**********************************************************************************************************************
36 string ConsensusSeqsCommand::getHelpString(){
38 string helpString = "";
39 helpString += "The consensus.seqs command can be used in 2 ways: create a consensus sequence from a fastafile, or with a listfile create a consensus sequence for each otu. Sequences must be aligned.\n";
40 helpString += "The consensus.seqs command parameters are fasta, list, name, count, cutoff and label.\n";
41 helpString += "The fasta parameter allows you to enter the fasta file containing your sequences, and is required, unless you have a valid current fasta file. \n";
42 helpString += "The list parameter allows you to enter a your list file. \n";
43 helpString += "The name parameter allows you to enter a names file associated with the fasta file. \n";
44 helpString += "The label parameter allows you to select what distance levels you would like output files for, and are separated by dashes.\n";
45 helpString += "The cutoff parameter allows you set a percentage of sequences that support the base. For example: cutoff=97 would only return a sequence that only showed ambiguities for bases that were not supported by at least 97% of sequences.\n";
46 helpString += "The consensus.seqs command should be in the following format: \n";
47 helpString += "consensus.seqs(fasta=yourFastaFile, list=yourListFile) \n";
48 helpString += "Example: consensus.seqs(fasta=abrecovery.align, list=abrecovery.fn.list) \n";
49 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n";
53 m->errorOut(e, "ConsensusSeqsCommand", "getHelpString");
57 //**********************************************************************************************************************
58 string ConsensusSeqsCommand::getOutputFileNameTag(string type, string inputName=""){
60 string outputFileName = "";
61 map<string, vector<string> >::iterator it;
63 //is this a type this command creates
64 it = outputTypes.find(type);
65 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
67 if (type == "fasta") { outputFileName = "cons.fasta"; }
68 else if (type == "name") { outputFileName = "cons.names"; }
69 else if (type == "count") { outputFileName = "cons.count_table"; }
70 else if (type == "summary") { outputFileName = "cons.summary"; }
71 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
73 return outputFileName;
76 m->errorOut(e, "ConsensusSeqsCommand", "getOutputFileNameTag");
81 //**********************************************************************************************************************
82 ConsensusSeqsCommand::ConsensusSeqsCommand(){
84 abort = true; calledHelp = true;
86 vector<string> tempOutNames;
87 outputTypes["fasta"] = tempOutNames;
88 outputTypes["name"] = tempOutNames;
89 outputTypes["count"] = tempOutNames;
90 outputTypes["summary"] = tempOutNames;
93 m->errorOut(e, "ConsensusSeqsCommand", "ConsensusSeqsCommand");
97 //***************************************************************************************************************
98 ConsensusSeqsCommand::ConsensusSeqsCommand(string option) {
100 abort = false; calledHelp = false;
103 //allow user to run help
104 if(option == "help") { help(); abort = true; calledHelp = true; }
105 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
109 vector<string> myArray = setParameters();
111 OptionParser parser(option);
112 map<string,string> parameters = parser.getParameters();
114 ValidParameters validParameter;
115 map<string,string>::iterator it;
117 //check to make sure all parameters are valid for command
118 for (it = parameters.begin(); it != parameters.end(); it++) {
119 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
122 //initialize outputTypes
123 vector<string> tempOutNames;
124 outputTypes["fasta"] = tempOutNames;
125 outputTypes["name"] = tempOutNames;
126 outputTypes["count"] = tempOutNames;
127 outputTypes["summary"] = tempOutNames;
130 //if the user changes the input directory command factory will send this info to us in the output parameter
131 string inputDir = validParameter.validFile(parameters, "inputdir", false);
132 if (inputDir == "not found"){ inputDir = ""; }
135 it = parameters.find("fasta");
136 //user has given a template file
137 if(it != parameters.end()){
138 path = m->hasPath(it->second);
139 //if the user has not given a path then, add inputdir. else leave path alone.
140 if (path == "") { parameters["fasta"] = inputDir + it->second; }
143 it = parameters.find("name");
144 //user has given a template file
145 if(it != parameters.end()){
146 path = m->hasPath(it->second);
147 //if the user has not given a path then, add inputdir. else leave path alone.
148 if (path == "") { parameters["name"] = inputDir + it->second; }
151 it = parameters.find("list");
152 //user has given a template file
153 if(it != parameters.end()){
154 path = m->hasPath(it->second);
155 //if the user has not given a path then, add inputdir. else leave path alone.
156 if (path == "") { parameters["list"] = inputDir + it->second; }
159 it = parameters.find("count");
160 //user has given a template file
161 if(it != parameters.end()){
162 path = m->hasPath(it->second);
163 //if the user has not given a path then, add inputdir. else leave path alone.
164 if (path == "") { parameters["count"] = inputDir + it->second; }
169 //check for parameters
170 fastafile = validParameter.validFile(parameters, "fasta", true);
171 if (fastafile == "not open") { abort = true; }
172 else if (fastafile == "not found") {
173 fastafile = m->getFastaFile();
174 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
175 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
176 }else { m->setFastaFile(fastafile); }
178 namefile = validParameter.validFile(parameters, "name", true);
179 if (namefile == "not open") { namefile = ""; abort = true; }
180 else if (namefile == "not found") { namefile = ""; }
181 else { m->setNameFile(namefile); }
183 countfile = validParameter.validFile(parameters, "count", true);
184 if (countfile == "not open") { abort = true; countfile = ""; }
185 else if (countfile == "not found") { countfile = ""; }
186 else { m->setCountTableFile(countfile); }
188 if ((countfile != "") && (namefile != "")) { m->mothurOut("You must enter ONLY ONE of the following: count or name."); m->mothurOutEndLine(); abort = true; }
190 listfile = validParameter.validFile(parameters, "list", true);
191 if (listfile == "not open") { abort = true; }
192 else if (listfile == "not found") { listfile = ""; }
193 else { m->setListFile(listfile); }
195 label = validParameter.validFile(parameters, "label", false);
196 if (label == "not found") { label = ""; }
198 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
199 else { allLines = 1; }
202 string temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "100"; }
203 m->mothurConvert(temp, cutoff);
205 //if the user changes the output directory command factory will send this info to us in the output parameter
206 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(fastafile); }
208 if (countfile == "") {
210 vector<string> files; files.push_back(fastafile);
211 parser.getNameFile(files);
216 catch(exception& e) {
217 m->errorOut(e, "ConsensusSeqsCommand", "ConsensusSeqsCommand");
221 //***************************************************************************************************************
223 int ConsensusSeqsCommand::execute(){
226 if (abort == true) { if (calledHelp) { return 0; } return 2; }
230 if (m->control_pressed) { return 0; }
232 if (namefile != "") { readNames(); }
233 if (countfile != "") { ct.readTable(countfile); }
235 if (m->control_pressed) { return 0; }
238 if (listfile == "") {
241 string outputSummaryFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + getOutputFileNameTag("summary");
242 m->openOutputFile(outputSummaryFile, outSummary);
243 outSummary.setf(ios::fixed, ios::floatfield); outSummary.setf(ios::showpoint);
244 outputNames.push_back(outputSummaryFile); outputTypes["summary"].push_back(outputSummaryFile);
246 outSummary << "PositioninAlignment\tA\tT\tG\tC\tGap\tNumberofSeqs\tConsensusBase" << endl;
249 string outputFastaFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + getOutputFileNameTag("fasta");
250 m->openOutputFile(outputFastaFile, outFasta);
251 outputNames.push_back(outputFastaFile); outputTypes["fasta"].push_back(outputFastaFile);
253 vector< vector<float> > percentages; percentages.resize(5);
254 for (int j = 0; j < percentages.size(); j++) { percentages[j].resize(seqLength, 0.0); }
259 for (int j = 0; j < seqLength; j++) {
261 if (m->control_pressed) { outSummary.close(); outFasta.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
263 vector<int> counts; counts.resize(5, 0); //A,T,G,C,Gap
266 for (map<string, string>::iterator it = fastaMap.begin(); it != fastaMap.end(); it++) {
268 string thisSeq = it->second;
271 if (countfile != "") { size = ct.getNumSeqs(it->first); }
273 map<string, int>::iterator itCount = nameFileMap.find(it->first);
274 if (itCount != nameFileMap.end()) {
275 size = itCount->second;
276 }else { m->mothurOut("[ERROR]: file mismatch, aborting.\n"); m->control_pressed = true; break; }
279 for (int k = 0; k < size; k++) {
280 if (thisSeq[j] == '.') { numDots++; }
282 char base = toupper(thisSeq[j]);
283 if (base == 'A') { counts[0]++; }
284 else if (base == 'T') { counts[1]++; }
285 else if (base == 'G') { counts[2]++; }
286 else if (base == 'C') { counts[3]++; }
287 else { counts[4]++; }
293 if (numDots != thisCount) { conBase = getBase(counts, thisCount); }
297 percentages[0][j] = counts[0] / (float) thisCount;
298 percentages[1][j] = counts[1] / (float) thisCount;
299 percentages[2][j] = counts[2] / (float) thisCount;
300 percentages[3][j] = counts[3] / (float) thisCount;
301 percentages[4][j] = counts[4] / (float) thisCount;
304 for (int j = 0; j < seqLength; j++) {
305 outSummary << (j+1) << '\t' << percentages[0][j] << '\t'<< percentages[1][j] << '\t'<< percentages[2][j] << '\t' << percentages[3][j] << '\t' << percentages[4][j] << '\t' << thisCount << '\t' << consSeq[j] << endl;
309 outFasta << ">conseq" << endl << consSeq << endl;
311 outSummary.close(); outFasta.close();
313 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
318 InputData* input = new InputData(listfile, "list");
319 ListVector* list = input->getListVector();
321 string lastLabel = list->getLabel();
322 set<string> processedLabels;
323 set<string> userLabels = labels;
325 //as long as you are not at the end of the file or done wih the lines you want
326 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
328 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } delete list; delete input; return 0; }
330 if(allLines == 1 || labels.count(list->getLabel()) == 1){
332 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
336 processedLabels.insert(list->getLabel());
337 userLabels.erase(list->getLabel());
340 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
341 string saveLabel = list->getLabel();
345 list = input->getListVector(lastLabel);
346 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
350 processedLabels.insert(list->getLabel());
351 userLabels.erase(list->getLabel());
353 //restore real lastlabel to save below
354 list->setLabel(saveLabel);
357 lastLabel = list->getLabel();
359 delete list; list = NULL;
361 //get next line to process
362 list = input->getListVector();
366 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } if (list != NULL) { delete list; } delete input; return 0; }
368 //output error messages about any remaining user labels
369 set<string>::iterator it;
370 bool needToRun = false;
371 for (it = userLabels.begin(); it != userLabels.end(); it++) {
372 m->mothurOut("Your file does not include the label " + *it);
373 if (processedLabels.count(lastLabel) != 1) {
374 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
377 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
381 //run last label if you need to
382 if (needToRun == true) {
383 if (list != NULL) { delete list; }
385 list = input->getListVector(lastLabel);
387 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
391 delete list; list = NULL;
394 if (list != NULL) { delete list; }
398 m->mothurOutEndLine();
399 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
400 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
401 m->mothurOutEndLine();
407 catch(exception& e) {
408 m->errorOut(e, "ConsensusSeqsCommand", "execute");
412 //***************************************************************************************************************
414 int ConsensusSeqsCommand::processList(ListVector*& list){
418 string outputSummaryFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + list->getLabel() + getOutputFileNameTag("summary");
419 m->openOutputFile(outputSummaryFile, outSummary);
420 outSummary.setf(ios::fixed, ios::floatfield); outSummary.setf(ios::showpoint);
421 outputNames.push_back(outputSummaryFile); outputTypes["summary"].push_back(outputSummaryFile);
424 string outputNameFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + list->getLabel() + getOutputFileNameTag("name");
425 m->openOutputFile(outputNameFile, outName);
426 outputNames.push_back(outputNameFile); outputTypes["name"].push_back(outputNameFile);
429 string outputFastaFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + list->getLabel() + getOutputFileNameTag("fasta");
430 m->openOutputFile(outputFastaFile, outFasta);
431 outputNames.push_back(outputFastaFile); outputTypes["fasta"].push_back(outputFastaFile);
433 outSummary << "OTU#\tPositioninAlignment\tA\tT\tG\tC\tGap\tNumberofSeqs\tConsensusBase" << endl;
435 for (int i = 0; i < list->getNumBins(); i++) {
437 if (m->control_pressed) { outSummary.close(); outName.close(); outFasta.close(); return 0; }
439 string bin = list->get(i);
440 string consSeq = getConsSeq(bin, outSummary, i);
442 outFasta << ">seq" << (i+1) << endl << consSeq << endl;
443 outName << "seq" << (i+1) << '\t' << "seq" << (i+1) << "," << bin << endl;
446 outSummary.close(); outName.close(); outFasta.close();
451 catch(exception& e) {
452 m->errorOut(e, "ConsensusSeqsCommand", "processList");
457 //***************************************************************************************************************
458 string ConsensusSeqsCommand::getConsSeq(string bin, ofstream& outSummary, int binNumber){
465 vector<string> binNames;
466 m->splitAtComma(bin, binNames);
468 vector< vector<float> > percentages; percentages.resize(5);
469 for (int j = 0; j < percentages.size(); j++) { percentages[j].resize(seqLength, 0.0); }
471 if (countfile != "") {
473 for (int j = 0; j < seqLength; j++) {
475 if (m->control_pressed) { return consSeq; }
477 vector<int> counts; counts.resize(5, 0); //A,T,G,C,Gap
480 for (int i = 0; i < binNames.size(); i++) {
481 if (m->control_pressed) { return consSeq; }
484 map<string, string>::iterator itFasta = fastaMap.find(binNames[i]);
485 if (itFasta != fastaMap.end()) {
486 thisSeq = itFasta->second;
487 }else { m->mothurOut("[ERROR]: " + binNames[i] + " is not in your fasta file, please correct."); m->mothurOutEndLine(); m->control_pressed = true; }
489 int size = ct.getNumSeqs(binNames[i]);
491 for (int k = 0; k < size; k++) {
492 if (thisSeq[j] == '.') { numDots++; }
494 char base = toupper(thisSeq[j]);
495 if (base == 'A') { counts[0]++; }
496 else if (base == 'T') { counts[1]++; }
497 else if (base == 'G') { counts[2]++; }
498 else if (base == 'C') { counts[3]++; }
499 else { counts[4]++; }
502 }else { m->mothurOut("[ERROR]: " + binNames[i] + " is not in your count file, please correct."); m->mothurOutEndLine(); m->control_pressed = true; }
505 if (numDots != totalSize) { conBase = getBase(counts, totalSize); }
509 percentages[0][j] = counts[0] / (float) totalSize;
510 percentages[1][j] = counts[1] / (float) totalSize;
511 percentages[2][j] = counts[2] / (float) totalSize;
512 percentages[3][j] = counts[3] / (float) totalSize;
513 percentages[4][j] = counts[4] / (float) totalSize;
518 //get sequence strings for each name in the bin
520 for (int i = 0; i < binNames.size(); i++) {
522 map<string, string>::iterator it;
523 it = nameMap.find(binNames[i]);
524 if (it == nameMap.end()) {
525 if (namefile == "") { m->mothurOut("[ERROR]: " + binNames[i] + " is not in your fasta file, please correct."); m->mothurOutEndLine(); error = true; }
526 else { m->mothurOut("[ERROR]: " + binNames[i] + " is not in your fasta or name file, please correct."); m->mothurOutEndLine(); error = true; }
529 //add sequence string to seqs vector to process below
530 map<string, string>::iterator itFasta = fastaMap.find(it->second);
532 if (itFasta != fastaMap.end()) {
533 string seq = itFasta->second;
535 }else { m->mothurOut("[ERROR]: file mismatch, aborting. \n"); }
539 if (error) { m->control_pressed = true; return consSeq; }
540 totalSize = seqs.size();
542 for (int j = 0; j < seqLength; j++) {
544 if (m->control_pressed) { return consSeq; }
546 vector<int> counts; counts.resize(5, 0); //A,T,G,C,Gap
549 for (int i = 0; i < seqs.size(); i++) {
551 if (seqs[i][j] == '.') { numDots++; }
553 char base = toupper(seqs[i][j]);
554 if (base == 'A') { counts[0]++; }
555 else if (base == 'T') { counts[1]++; }
556 else if (base == 'G') { counts[2]++; }
557 else if (base == 'C') { counts[3]++; }
558 else { counts[4]++; }
562 if (numDots != seqs.size()) { conBase = getBase(counts, seqs.size()); }
566 percentages[0][j] = counts[0] / (float) seqs.size();
567 percentages[1][j] = counts[1] / (float) seqs.size();
568 percentages[2][j] = counts[2] / (float) seqs.size();
569 percentages[3][j] = counts[3] / (float) seqs.size();
570 percentages[4][j] = counts[4] / (float) seqs.size();
577 for (int j = 0; j < seqLength; j++) {
578 outSummary << (binNumber + 1) << '\t' << (j+1) << '\t' << percentages[0][j] << '\t'<< percentages[1][j] << '\t'<< percentages[2][j] << '\t' << percentages[3][j] << '\t' << percentages[4][j] << '\t' << totalSize << '\t' << consSeq[j] << endl;
584 catch(exception& e) {
585 m->errorOut(e, "ConsensusSeqsCommand", "getConsSeq");
589 //***************************************************************************************************************
591 char ConsensusSeqsCommand::getBase(vector<int> counts, int size){ //A,T,G,C,Gap
598 * Y = T C (pyrimidine)
601 * S = G C (strong bonds)
602 * W = A T (weak bonds)
603 * B = G T C (all but A)
604 * D = G A T (all but C)
605 * H = A C T (all but G)
606 * V = G C A (all but T)
607 * N = A G C T (any) */
611 //zero out counts that don't make the cutoff
612 float percentage = (100.0 - cutoff) / 100.0;
613 int zeroCutoff = percentage * size;
615 for (int i = 0; i < counts.size(); i++) {
616 if (counts[i] < zeroCutoff) { counts[i] = 0; }
620 if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'n'; }
622 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'N'; }
624 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'v'; }
626 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'V'; }
628 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'h'; }
630 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'H'; }
632 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'd'; }
634 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'D'; }
636 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'b'; }
638 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'B'; }
639 //W = A T (weak bonds)
640 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'w'; }
641 //W = A T (weak bonds) no gap
642 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'W'; }
643 //S = G C (strong bonds)
644 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 's'; }
645 //S = G C (strong bonds) no gap
646 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'S'; }
648 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'm'; }
649 //M = A C (amino) no gap
650 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'M'; }
652 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'k'; }
653 //K = G T (keto) no gap
654 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'K'; }
655 //Y = T C (pyrimidine)
656 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'y'; }
657 //Y = T C (pyrimidine) no gap
658 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'Y'; }
660 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'r'; }
661 //R = G A (purine) no gap
662 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'R'; }
664 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'a'; }
666 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'A'; }
668 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 't'; }
670 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'T'; }
672 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'g'; }
674 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'G'; }
676 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'c'; }
678 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'C'; }
680 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = '-'; }
681 //cutoff removed all counts
682 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'N'; }
683 else{ m->mothurOut("[ERROR]: cannot find consensus base."); m->mothurOutEndLine(); }
688 catch(exception& e) {
689 m->errorOut(e, "ConsensusSeqsCommand", "getBase");
694 //***************************************************************************************************************
696 int ConsensusSeqsCommand::readFasta(){
700 m->openInputFile(fastafile, in);
705 if (m->control_pressed) { break; }
707 Sequence seq(in); m->gobble(in);
708 string name = seq.getName();
711 fastaMap[name] = seq.getAligned();
712 nameMap[name] = name; //set nameMap incase no names file
713 nameFileMap[name] = 1;
715 if (seqLength == 0) { seqLength = seq.getAligned().length(); }
716 else if (seqLength != seq.getAligned().length()) { m->mothurOut("[ERROR]: sequence are not the same length, please correct."); m->mothurOutEndLine(); m->control_pressed = true; break; }
725 catch(exception& e) {
726 m->errorOut(e, "ConsensusSeqsCommand", "readFasta");
730 //***************************************************************************************************************
732 int ConsensusSeqsCommand::readNames(){
734 map<string, string> temp;
735 map<string, string>::iterator it;
738 m->readNames(namefile, temp); //use central buffered read
740 for (map<string, string>::iterator itTemp = temp.begin(); itTemp != temp.end(); itTemp++) {
741 string thisname, repnames;
742 thisname = itTemp->first;
743 repnames = itTemp->second;
745 it = nameMap.find(thisname);
746 if (it != nameMap.end()) { //then this sequence was in the fastafile
747 nameFileMap[thisname] = m->getNumNames(repnames); //for later when outputting the new namesFile if the list file is unique
749 vector<string> splitRepNames;
750 m->splitAtComma(repnames, splitRepNames);
752 for (int i = 0; i < splitRepNames.size(); i++) { nameMap[splitRepNames[i]] = thisname; }
754 }else{ m->mothurOut("[ERROR]: " + thisname + " is not in the fasta file, please correct."); m->mothurOutEndLine(); error = true; }
757 if (error) { m->control_pressed = true; }
762 catch(exception& e) {
763 m->errorOut(e, "ConsensusSeqsCommand", "readNames");
768 //***************************************************************************************************************