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","fasta-name",false,true,true); parameters.push_back(pfasta);
18 CommandParameter pname("name", "InputTypes", "", "", "namecount", "none", "none","name",false,false,true); parameters.push_back(pname);
19 CommandParameter pcount("count", "InputTypes", "", "", "namecount", "none", "none","count",false,false,true); parameters.push_back(pcount);
20 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none","fasta-name",false,false,true); 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::getOutputPattern(string type) {
62 if (type == "fasta") { pattern = "[filename],cons.fasta-[filename],[tag],cons.fasta"; }
63 else if (type == "name") { pattern = "[filename],cons.names-[filename],[tag],cons.names"; }
64 else if (type == "count") { pattern = "[filename],cons.count_table-[filename],[tag],cons.count_table"; }
65 else if (type == "summary") { pattern = "[filename],cons.summary-[filename],[tag],cons.summary"; }
66 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
71 m->errorOut(e, "ConsensusSeqsCommand", "getOutputPattern");
75 //**********************************************************************************************************************
76 ConsensusSeqsCommand::ConsensusSeqsCommand(){
78 abort = true; calledHelp = true;
80 vector<string> tempOutNames;
81 outputTypes["fasta"] = tempOutNames;
82 outputTypes["name"] = tempOutNames;
83 outputTypes["count"] = tempOutNames;
84 outputTypes["summary"] = tempOutNames;
87 m->errorOut(e, "ConsensusSeqsCommand", "ConsensusSeqsCommand");
91 //***************************************************************************************************************
92 ConsensusSeqsCommand::ConsensusSeqsCommand(string option) {
94 abort = false; calledHelp = false;
97 //allow user to run help
98 if(option == "help") { help(); abort = true; calledHelp = true; }
99 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
103 vector<string> myArray = setParameters();
105 OptionParser parser(option);
106 map<string,string> parameters = parser.getParameters();
108 ValidParameters validParameter;
109 map<string,string>::iterator it;
111 //check to make sure all parameters are valid for command
112 for (it = parameters.begin(); it != parameters.end(); it++) {
113 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
116 //initialize outputTypes
117 vector<string> tempOutNames;
118 outputTypes["fasta"] = tempOutNames;
119 outputTypes["name"] = tempOutNames;
120 outputTypes["count"] = tempOutNames;
121 outputTypes["summary"] = tempOutNames;
124 //if the user changes the input directory command factory will send this info to us in the output parameter
125 string inputDir = validParameter.validFile(parameters, "inputdir", false);
126 if (inputDir == "not found"){ inputDir = ""; }
129 it = parameters.find("fasta");
130 //user has given a template file
131 if(it != parameters.end()){
132 path = m->hasPath(it->second);
133 //if the user has not given a path then, add inputdir. else leave path alone.
134 if (path == "") { parameters["fasta"] = inputDir + it->second; }
137 it = parameters.find("name");
138 //user has given a template file
139 if(it != parameters.end()){
140 path = m->hasPath(it->second);
141 //if the user has not given a path then, add inputdir. else leave path alone.
142 if (path == "") { parameters["name"] = inputDir + it->second; }
145 it = parameters.find("list");
146 //user has given a template file
147 if(it != parameters.end()){
148 path = m->hasPath(it->second);
149 //if the user has not given a path then, add inputdir. else leave path alone.
150 if (path == "") { parameters["list"] = inputDir + it->second; }
153 it = parameters.find("count");
154 //user has given a template file
155 if(it != parameters.end()){
156 path = m->hasPath(it->second);
157 //if the user has not given a path then, add inputdir. else leave path alone.
158 if (path == "") { parameters["count"] = inputDir + it->second; }
163 //check for parameters
164 fastafile = validParameter.validFile(parameters, "fasta", true);
165 if (fastafile == "not open") { abort = true; }
166 else if (fastafile == "not found") {
167 fastafile = m->getFastaFile();
168 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
169 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
170 }else { m->setFastaFile(fastafile); }
172 namefile = validParameter.validFile(parameters, "name", true);
173 if (namefile == "not open") { namefile = ""; abort = true; }
174 else if (namefile == "not found") { namefile = ""; }
175 else { m->setNameFile(namefile); }
177 countfile = validParameter.validFile(parameters, "count", true);
178 if (countfile == "not open") { abort = true; countfile = ""; }
179 else if (countfile == "not found") { countfile = ""; }
180 else { m->setCountTableFile(countfile); }
182 if ((countfile != "") && (namefile != "")) { m->mothurOut("You must enter ONLY ONE of the following: count or name."); m->mothurOutEndLine(); abort = true; }
184 listfile = validParameter.validFile(parameters, "list", true);
185 if (listfile == "not open") { abort = true; }
186 else if (listfile == "not found") { listfile = ""; }
187 else { m->setListFile(listfile); }
189 label = validParameter.validFile(parameters, "label", false);
190 if (label == "not found") { label = ""; }
192 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
193 else { allLines = 1; }
196 string temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "100"; }
197 m->mothurConvert(temp, cutoff);
199 //if the user changes the output directory command factory will send this info to us in the output parameter
200 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(fastafile); }
202 if (countfile == "") {
204 vector<string> files; files.push_back(fastafile);
205 parser.getNameFile(files);
210 catch(exception& e) {
211 m->errorOut(e, "ConsensusSeqsCommand", "ConsensusSeqsCommand");
215 //***************************************************************************************************************
217 int ConsensusSeqsCommand::execute(){
220 if (abort == true) { if (calledHelp) { return 0; } return 2; }
222 int start = time(NULL);
226 if (m->control_pressed) { return 0; }
228 if (namefile != "") { readNames(); }
229 if (countfile != "") { ct.readTable(countfile, true); }
231 if (m->control_pressed) { return 0; }
234 if (listfile == "") {
237 map<string, string> variables;
238 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastafile));
239 string outputSummaryFile = getOutputFileName("summary", variables);
240 m->openOutputFile(outputSummaryFile, outSummary);
241 outSummary.setf(ios::fixed, ios::floatfield); outSummary.setf(ios::showpoint);
242 outputNames.push_back(outputSummaryFile); outputTypes["summary"].push_back(outputSummaryFile);
244 outSummary << "PositioninAlignment\tA\tT\tG\tC\tGap\tNumberofSeqs\tConsensusBase" << endl;
247 string outputFastaFile = getOutputFileName("fasta", variables);
248 m->openOutputFile(outputFastaFile, outFasta);
249 outputNames.push_back(outputFastaFile); outputTypes["fasta"].push_back(outputFastaFile);
251 vector< vector<float> > percentages; percentages.resize(5);
252 for (int j = 0; j < percentages.size(); j++) { percentages[j].resize(seqLength, 0.0); }
257 for (int j = 0; j < seqLength; j++) {
259 if (m->control_pressed) { outSummary.close(); outFasta.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
261 vector<int> counts; counts.resize(5, 0); //A,T,G,C,Gap
264 for (map<string, string>::iterator it = fastaMap.begin(); it != fastaMap.end(); it++) {
266 string thisSeq = it->second;
269 if (countfile != "") { size = ct.getNumSeqs(it->first); }
271 map<string, int>::iterator itCount = nameFileMap.find(it->first);
272 if (itCount != nameFileMap.end()) {
273 size = itCount->second;
274 }else { m->mothurOut("[ERROR]: file mismatch, aborting.\n"); m->control_pressed = true; break; }
277 for (int k = 0; k < size; k++) {
278 if (thisSeq[j] == '.') { numDots++; }
280 char base = toupper(thisSeq[j]);
281 if (base == 'A') { counts[0]++; }
282 else if (base == 'T') { counts[1]++; }
283 else if (base == 'G') { counts[2]++; }
284 else if (base == 'C') { counts[3]++; }
285 else { counts[4]++; }
291 if (numDots != thisCount) { conBase = getBase(counts, thisCount); }
295 percentages[0][j] = counts[0] / (float) thisCount;
296 percentages[1][j] = counts[1] / (float) thisCount;
297 percentages[2][j] = counts[2] / (float) thisCount;
298 percentages[3][j] = counts[3] / (float) thisCount;
299 percentages[4][j] = counts[4] / (float) thisCount;
302 for (int j = 0; j < seqLength; j++) {
303 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;
307 outFasta << ">conseq" << endl << consSeq << endl;
309 outSummary.close(); outFasta.close();
311 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
316 InputData* input = new InputData(listfile, "list");
317 ListVector* list = input->getListVector();
319 string lastLabel = list->getLabel();
320 set<string> processedLabels;
321 set<string> userLabels = labels;
323 //as long as you are not at the end of the file or done wih the lines you want
324 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
326 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } delete list; delete input; return 0; }
328 if(allLines == 1 || labels.count(list->getLabel()) == 1){
330 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
334 processedLabels.insert(list->getLabel());
335 userLabels.erase(list->getLabel());
338 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
339 string saveLabel = list->getLabel();
343 list = input->getListVector(lastLabel);
344 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
348 processedLabels.insert(list->getLabel());
349 userLabels.erase(list->getLabel());
351 //restore real lastlabel to save below
352 list->setLabel(saveLabel);
355 lastLabel = list->getLabel();
357 delete list; list = NULL;
359 //get next line to process
360 list = input->getListVector();
364 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; }
366 //output error messages about any remaining user labels
367 set<string>::iterator it;
368 bool needToRun = false;
369 for (it = userLabels.begin(); it != userLabels.end(); it++) {
370 m->mothurOut("Your file does not include the label " + *it);
371 if (processedLabels.count(lastLabel) != 1) {
372 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
375 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
379 //run last label if you need to
380 if (needToRun == true) {
381 if (list != NULL) { delete list; }
383 list = input->getListVector(lastLabel);
385 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
389 delete list; list = NULL;
392 if (list != NULL) { delete list; }
396 m->mothurOut("It took " + toString(time(NULL) - start) + " secs to find the consensus sequences.");
398 m->mothurOutEndLine();
399 m->mothurOut("Output File Names: "); 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 map<string, string> variables;
419 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastafile));
420 variables["[tag]"] = list->getLabel();
421 string outputSummaryFile = getOutputFileName("summary", variables);
422 m->openOutputFile(outputSummaryFile, outSummary);
423 outSummary.setf(ios::fixed, ios::floatfield); outSummary.setf(ios::showpoint);
424 outputNames.push_back(outputSummaryFile); outputTypes["summary"].push_back(outputSummaryFile);
427 string outputNameFile = getOutputFileName("name",variables);
428 m->openOutputFile(outputNameFile, outName);
429 outputNames.push_back(outputNameFile); outputTypes["name"].push_back(outputNameFile);
432 string outputFastaFile = getOutputFileName("fasta",variables);
433 m->openOutputFile(outputFastaFile, outFasta);
434 outputNames.push_back(outputFastaFile); outputTypes["fasta"].push_back(outputFastaFile);
436 outSummary << "OTU#\tPositioninAlignment\tA\tT\tG\tC\tGap\tNumberofSeqs\tConsensusBase" << endl;
438 string snumBins = toString(list->getNumBins());
439 for (int i = 0; i < list->getNumBins(); i++) {
441 if (m->control_pressed) { outSummary.close(); outName.close(); outFasta.close(); return 0; }
443 string bin = list->get(i);
444 string consSeq = getConsSeq(bin, outSummary, i);
446 string seqName = "Otu";
447 string sbinNumber = toString(i+1);
448 if (sbinNumber.length() < snumBins.length()) {
449 int diff = snumBins.length() - sbinNumber.length();
450 for (int h = 0; h < diff; h++) { seqName += "0"; }
452 seqName += sbinNumber;
454 outFasta << ">" << seqName << endl << consSeq << endl;
455 outName << seqName << '\t' << seqName << "," << bin << endl;
458 outSummary.close(); outName.close(); outFasta.close();
463 catch(exception& e) {
464 m->errorOut(e, "ConsensusSeqsCommand", "processList");
469 //***************************************************************************************************************
470 string ConsensusSeqsCommand::getConsSeq(string bin, ofstream& outSummary, int binNumber){
477 vector<string> binNames;
478 m->splitAtComma(bin, binNames);
480 vector< vector<float> > percentages; percentages.resize(5);
481 for (int j = 0; j < percentages.size(); j++) { percentages[j].resize(seqLength, 0.0); }
483 if (countfile != "") {
485 for (int j = 0; j < seqLength; j++) {
487 if (m->control_pressed) { return consSeq; }
489 vector<int> counts; counts.resize(5, 0); //A,T,G,C,Gap
492 for (int i = 0; i < binNames.size(); i++) {
493 if (m->control_pressed) { return consSeq; }
496 map<string, string>::iterator itFasta = fastaMap.find(binNames[i]);
497 if (itFasta != fastaMap.end()) {
498 thisSeq = itFasta->second;
499 }else { m->mothurOut("[ERROR]: " + binNames[i] + " is not in your fasta file, please correct."); m->mothurOutEndLine(); m->control_pressed = true; }
501 int size = ct.getNumSeqs(binNames[i]);
503 for (int k = 0; k < size; k++) {
504 if (thisSeq[j] == '.') { numDots++; }
506 char base = toupper(thisSeq[j]);
507 if (base == 'A') { counts[0]++; }
508 else if (base == 'T') { counts[1]++; }
509 else if (base == 'G') { counts[2]++; }
510 else if (base == 'C') { counts[3]++; }
511 else { counts[4]++; }
514 }else { m->mothurOut("[ERROR]: " + binNames[i] + " is not in your count file, please correct."); m->mothurOutEndLine(); m->control_pressed = true; }
517 if (numDots != totalSize) { conBase = getBase(counts, totalSize); }
521 percentages[0][j] = counts[0] / (float) totalSize;
522 percentages[1][j] = counts[1] / (float) totalSize;
523 percentages[2][j] = counts[2] / (float) totalSize;
524 percentages[3][j] = counts[3] / (float) totalSize;
525 percentages[4][j] = counts[4] / (float) totalSize;
530 //get sequence strings for each name in the bin
532 for (int i = 0; i < binNames.size(); i++) {
534 map<string, string>::iterator it;
535 it = nameMap.find(binNames[i]);
536 if (it == nameMap.end()) {
537 if (namefile == "") { m->mothurOut("[ERROR]: " + binNames[i] + " is not in your fasta file, please correct."); m->mothurOutEndLine(); error = true; }
538 else { m->mothurOut("[ERROR]: " + binNames[i] + " is not in your fasta or name file, please correct."); m->mothurOutEndLine(); error = true; }
541 //add sequence string to seqs vector to process below
542 map<string, string>::iterator itFasta = fastaMap.find(it->second);
544 if (itFasta != fastaMap.end()) {
545 string seq = itFasta->second;
547 }else { m->mothurOut("[ERROR]: file mismatch, aborting. \n"); }
551 if (error) { m->control_pressed = true; return consSeq; }
552 totalSize = seqs.size();
554 for (int j = 0; j < seqLength; j++) {
556 if (m->control_pressed) { return consSeq; }
558 vector<int> counts; counts.resize(5, 0); //A,T,G,C,Gap
561 for (int i = 0; i < seqs.size(); i++) {
563 if (seqs[i][j] == '.') { numDots++; }
565 char base = toupper(seqs[i][j]);
566 if (base == 'A') { counts[0]++; }
567 else if (base == 'T') { counts[1]++; }
568 else if (base == 'G') { counts[2]++; }
569 else if (base == 'C') { counts[3]++; }
570 else { counts[4]++; }
574 if (numDots != seqs.size()) { conBase = getBase(counts, seqs.size()); }
578 percentages[0][j] = counts[0] / (float) seqs.size();
579 percentages[1][j] = counts[1] / (float) seqs.size();
580 percentages[2][j] = counts[2] / (float) seqs.size();
581 percentages[3][j] = counts[3] / (float) seqs.size();
582 percentages[4][j] = counts[4] / (float) seqs.size();
589 for (int j = 0; j < seqLength; j++) {
590 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;
596 catch(exception& e) {
597 m->errorOut(e, "ConsensusSeqsCommand", "getConsSeq");
601 //***************************************************************************************************************
603 char ConsensusSeqsCommand::getBase(vector<int> counts, int size){ //A,T,G,C,Gap
610 * Y = T C (pyrimidine)
613 * S = G C (strong bonds)
614 * W = A T (weak bonds)
615 * B = G T C (all but A)
616 * D = G A T (all but C)
617 * H = A C T (all but G)
618 * V = G C A (all but T)
619 * N = A G C T (any) */
623 //zero out counts that don't make the cutoff
624 float percentage = (100.0 - cutoff) / 100.0;
626 for (int i = 0; i < counts.size(); i++) {
627 float countPercentage = counts[i] / (float) size;
628 if (countPercentage < percentage) { counts[i] = 0; }
632 if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'n'; }
634 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'N'; }
636 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'v'; }
638 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'V'; }
640 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'h'; }
642 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'H'; }
644 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'd'; }
646 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'D'; }
648 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'b'; }
650 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'B'; }
651 //W = A T (weak bonds)
652 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'w'; }
653 //W = A T (weak bonds) no gap
654 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'W'; }
655 //S = G C (strong bonds)
656 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 's'; }
657 //S = G C (strong bonds) no gap
658 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'S'; }
660 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'm'; }
661 //M = A C (amino) no gap
662 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'M'; }
664 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'k'; }
665 //K = G T (keto) no gap
666 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'K'; }
667 //Y = T C (pyrimidine)
668 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'y'; }
669 //Y = T C (pyrimidine) no gap
670 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'Y'; }
672 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'r'; }
673 //R = G A (purine) no gap
674 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'R'; }
676 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'a'; }
678 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'A'; }
680 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 't'; }
682 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'T'; }
684 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'g'; }
686 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'G'; }
688 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'c'; }
690 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'C'; }
692 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = '-'; }
693 //cutoff removed all counts
694 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'N'; }
695 else{ m->mothurOut("[ERROR]: cannot find consensus base."); m->mothurOutEndLine(); }
700 catch(exception& e) {
701 m->errorOut(e, "ConsensusSeqsCommand", "getBase");
706 //***************************************************************************************************************
708 int ConsensusSeqsCommand::readFasta(){
712 m->openInputFile(fastafile, in);
717 if (m->control_pressed) { break; }
719 Sequence seq(in); m->gobble(in);
720 string name = seq.getName();
723 fastaMap[name] = seq.getAligned();
724 nameMap[name] = name; //set nameMap incase no names file
725 nameFileMap[name] = 1;
727 if (seqLength == 0) { seqLength = seq.getAligned().length(); }
728 else if (seqLength != seq.getAligned().length()) { m->mothurOut("[ERROR]: sequence are not the same length, please correct."); m->mothurOutEndLine(); m->control_pressed = true; break; }
737 catch(exception& e) {
738 m->errorOut(e, "ConsensusSeqsCommand", "readFasta");
742 //***************************************************************************************************************
744 int ConsensusSeqsCommand::readNames(){
746 map<string, string> temp;
747 map<string, string>::iterator it;
750 m->readNames(namefile, temp); //use central buffered read
752 for (map<string, string>::iterator itTemp = temp.begin(); itTemp != temp.end(); itTemp++) {
753 string thisname, repnames;
754 thisname = itTemp->first;
755 repnames = itTemp->second;
757 it = nameMap.find(thisname);
758 if (it != nameMap.end()) { //then this sequence was in the fastafile
759 nameFileMap[thisname] = m->getNumNames(repnames); //for later when outputting the new namesFile if the list file is unique
761 vector<string> splitRepNames;
762 m->splitAtComma(repnames, splitRepNames);
764 for (int i = 0; i < splitRepNames.size(); i++) { nameMap[splitRepNames[i]] = thisname; }
766 }else{ m->mothurOut("[ERROR]: " + thisname + " is not in the fasta file, please correct."); m->mothurOutEndLine(); error = true; }
769 if (error) { m->control_pressed = true; }
774 catch(exception& e) {
775 m->errorOut(e, "ConsensusSeqsCommand", "readNames");
780 //***************************************************************************************************************