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", "", "", "none", "none", "none",false,false); parameters.push_back(pname);
19 CommandParameter plist("list", "InputTypes", "", "", "none", "none", "none",false,false); parameters.push_back(plist);
20 CommandParameter plabel("label", "String", "", "", "", "", "",false,false); parameters.push_back(plabel);
21 CommandParameter pcutoff("cutoff", "Number", "", "100", "", "", "",false,false); parameters.push_back(pcutoff);
22 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
23 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
25 vector<string> myArray;
26 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
30 m->errorOut(e, "ConsensusSeqsCommand", "setParameters");
34 //**********************************************************************************************************************
35 string ConsensusSeqsCommand::getHelpString(){
37 string helpString = "";
38 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";
39 helpString += "The consensus.seqs command parameters are fasta, list, name, cutoff and label.\n";
40 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";
41 helpString += "The list parameter allows you to enter a your list file. \n";
42 helpString += "The name parameter allows you to enter a names file associated with the fasta file. \n";
43 helpString += "The label parameter allows you to select what distance levels you would like output files for, and are separated by dashes.\n";
44 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";
45 helpString += "The consensus.seqs command should be in the following format: \n";
46 helpString += "consensus.seqs(fasta=yourFastaFile, list=yourListFile) \n";
47 helpString += "Example: consensus.seqs(fasta=abrecovery.align, list=abrecovery.fn.list) \n";
48 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n";
52 m->errorOut(e, "ConsensusSeqsCommand", "getHelpString");
56 //**********************************************************************************************************************
57 string ConsensusSeqsCommand::getOutputFileNameTag(string type, string inputName=""){
59 string outputFileName = "";
60 map<string, vector<string> >::iterator it;
62 //is this a type this command creates
63 it = outputTypes.find(type);
64 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
66 if (type == "fasta") { outputFileName = "cons.fasta"; }
67 else if (type == "name") { outputFileName = "cons.names"; }
68 else if (type == "summary") { outputFileName = "cons.summary"; }
69 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
71 return outputFileName;
74 m->errorOut(e, "ConsensusSeqsCommand", "getOutputFileNameTag");
79 //**********************************************************************************************************************
80 ConsensusSeqsCommand::ConsensusSeqsCommand(){
82 abort = true; calledHelp = true;
84 vector<string> tempOutNames;
85 outputTypes["fasta"] = tempOutNames;
86 outputTypes["name"] = tempOutNames;
87 outputTypes["summary"] = tempOutNames;
90 m->errorOut(e, "ConsensusSeqsCommand", "ConsensusSeqsCommand");
94 //***************************************************************************************************************
95 ConsensusSeqsCommand::ConsensusSeqsCommand(string option) {
97 abort = false; calledHelp = false;
100 //allow user to run help
101 if(option == "help") { help(); abort = true; calledHelp = true; }
102 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
106 vector<string> myArray = setParameters();
108 OptionParser parser(option);
109 map<string,string> parameters = parser.getParameters();
111 ValidParameters validParameter;
112 map<string,string>::iterator it;
114 //check to make sure all parameters are valid for command
115 for (it = parameters.begin(); it != parameters.end(); it++) {
116 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
119 //initialize outputTypes
120 vector<string> tempOutNames;
121 outputTypes["fasta"] = tempOutNames;
122 outputTypes["name"] = tempOutNames;
123 outputTypes["summary"] = tempOutNames;
126 //if the user changes the input directory command factory will send this info to us in the output parameter
127 string inputDir = validParameter.validFile(parameters, "inputdir", false);
128 if (inputDir == "not found"){ inputDir = ""; }
131 it = parameters.find("fasta");
132 //user has given a template file
133 if(it != parameters.end()){
134 path = m->hasPath(it->second);
135 //if the user has not given a path then, add inputdir. else leave path alone.
136 if (path == "") { parameters["fasta"] = inputDir + it->second; }
139 it = parameters.find("name");
140 //user has given a template file
141 if(it != parameters.end()){
142 path = m->hasPath(it->second);
143 //if the user has not given a path then, add inputdir. else leave path alone.
144 if (path == "") { parameters["name"] = inputDir + it->second; }
147 it = parameters.find("list");
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["list"] = inputDir + it->second; }
157 //check for parameters
158 fastafile = validParameter.validFile(parameters, "fasta", true);
159 if (fastafile == "not open") { abort = true; }
160 else if (fastafile == "not found") {
161 fastafile = m->getFastaFile();
162 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
163 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
164 }else { m->setFastaFile(fastafile); }
166 namefile = validParameter.validFile(parameters, "name", true);
167 if (namefile == "not open") { namefile = ""; abort = true; }
168 else if (namefile == "not found") { namefile = ""; }
169 else { m->setNameFile(namefile); }
171 listfile = validParameter.validFile(parameters, "list", true);
172 if (listfile == "not open") { abort = true; }
173 else if (listfile == "not found") { listfile = ""; }
174 else { m->setListFile(listfile); }
176 label = validParameter.validFile(parameters, "label", false);
177 if (label == "not found") { label = ""; }
179 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
180 else { allLines = 1; }
183 string temp = validParameter.validFile(parameters, "cutoff", false); if (temp == "not found") { temp = "100"; }
184 m->mothurConvert(temp, cutoff);
186 //if the user changes the output directory command factory will send this info to us in the output parameter
187 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(fastafile); }
190 vector<string> files; files.push_back(fastafile);
191 parser.getNameFile(files);
195 catch(exception& e) {
196 m->errorOut(e, "ConsensusSeqsCommand", "ConsensusSeqsCommand");
200 //***************************************************************************************************************
202 int ConsensusSeqsCommand::execute(){
205 if (abort == true) { if (calledHelp) { return 0; } return 2; }
209 if (m->control_pressed) { return 0; }
211 if (namefile != "") { readNames(); }
213 if (m->control_pressed) { return 0; }
216 if (listfile == "") {
219 string outputSummaryFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + getOutputFileNameTag("summary");
220 m->openOutputFile(outputSummaryFile, outSummary);
221 outSummary.setf(ios::fixed, ios::floatfield); outSummary.setf(ios::showpoint);
222 outputNames.push_back(outputSummaryFile); outputTypes["summary"].push_back(outputSummaryFile);
224 outSummary << "PositioninAlignment\tA\tT\tG\tC\tGap\tNumberofSeqs\tConsensusBase" << endl;
227 string outputFastaFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + getOutputFileNameTag("fasta");
228 m->openOutputFile(outputFastaFile, outFasta);
229 outputNames.push_back(outputFastaFile); outputTypes["fasta"].push_back(outputFastaFile);
233 for (map<string, string>::iterator it = nameMap.begin(); it != nameMap.end(); it++) {
235 if (m->control_pressed) { outSummary.close(); outFasta.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
237 string seq = fastaMap[it->second];
240 if (seqLength == 0) { seqLength = seq.length(); }
241 else if (seqLength != seq.length()) { m->mothurOut("[ERROR]: sequence are not the same length, please correct."); m->mothurOutEndLine(); m->control_pressed = true; }
245 vector< vector<float> > percentages; percentages.resize(5);
246 for (int j = 0; j < percentages.size(); j++) { percentages[j].resize(seqLength, 0.0); }
250 for (int j = 0; j < seqLength; j++) {
252 if (m->control_pressed) { outSummary.close(); outFasta.close(); for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
254 vector<int> counts; counts.resize(5, 0); //A,T,G,C,Gap
257 for (int i = 0; i < seqs.size(); i++) {
259 if (seqs[i][j] == '.') { numDots++; }
261 char base = toupper(seqs[i][j]);
262 if (base == 'A') { counts[0]++; }
263 else if (base == 'T') { counts[1]++; }
264 else if (base == 'G') { counts[2]++; }
265 else if (base == 'C') { counts[3]++; }
266 else { counts[4]++; }
270 if (numDots != seqs.size()) { conBase = getBase(counts, seqs.size()); }
274 percentages[0][j] = counts[0] / (float) seqs.size();
275 percentages[1][j] = counts[1] / (float) seqs.size();
276 percentages[2][j] = counts[2] / (float) seqs.size();
277 percentages[3][j] = counts[3] / (float) seqs.size();
278 percentages[4][j] = counts[4] / (float) seqs.size();
282 for (int j = 0; j < seqLength; j++) {
283 outSummary << (j+1) << '\t' << percentages[0][j] << '\t'<< percentages[1][j] << '\t'<< percentages[2][j] << '\t' << percentages[3][j] << '\t' << percentages[4][j] << '\t' << seqs.size() << '\t' << consSeq[j] << endl;
287 outFasta << ">conseq" << endl << consSeq << endl;
289 outSummary.close(); outFasta.close();
295 InputData* input = new InputData(listfile, "list");
296 ListVector* list = input->getListVector();
298 string lastLabel = list->getLabel();
299 set<string> processedLabels;
300 set<string> userLabels = labels;
302 //as long as you are not at the end of the file or done wih the lines you want
303 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
305 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } delete list; delete input; return 0; }
307 if(allLines == 1 || labels.count(list->getLabel()) == 1){
309 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
313 processedLabels.insert(list->getLabel());
314 userLabels.erase(list->getLabel());
317 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
318 string saveLabel = list->getLabel();
322 list = input->getListVector(lastLabel);
323 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
327 processedLabels.insert(list->getLabel());
328 userLabels.erase(list->getLabel());
330 //restore real lastlabel to save below
331 list->setLabel(saveLabel);
334 lastLabel = list->getLabel();
336 delete list; list = NULL;
338 //get next line to process
339 list = input->getListVector();
343 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; }
345 //output error messages about any remaining user labels
346 set<string>::iterator it;
347 bool needToRun = false;
348 for (it = userLabels.begin(); it != userLabels.end(); it++) {
349 m->mothurOut("Your file does not include the label " + *it);
350 if (processedLabels.count(lastLabel) != 1) {
351 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
354 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
358 //run last label if you need to
359 if (needToRun == true) {
360 if (list != NULL) { delete list; }
362 list = input->getListVector(lastLabel);
364 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
368 delete list; list = NULL;
371 if (list != NULL) { delete list; }
375 m->mothurOutEndLine();
376 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
377 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
378 m->mothurOutEndLine();
384 catch(exception& e) {
385 m->errorOut(e, "ConsensusSeqsCommand", "execute");
389 //***************************************************************************************************************
391 int ConsensusSeqsCommand::processList(ListVector*& list){
395 string outputSummaryFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + list->getLabel() + getOutputFileNameTag("summary");
396 m->openOutputFile(outputSummaryFile, outSummary);
397 outSummary.setf(ios::fixed, ios::floatfield); outSummary.setf(ios::showpoint);
398 outputNames.push_back(outputSummaryFile); outputTypes["summary"].push_back(outputSummaryFile);
401 string outputNameFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + list->getLabel() + getOutputFileNameTag("name");
402 m->openOutputFile(outputNameFile, outName);
403 outputNames.push_back(outputNameFile); outputTypes["name"].push_back(outputNameFile);
406 string outputFastaFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + list->getLabel() + getOutputFileNameTag("fasta");
407 m->openOutputFile(outputFastaFile, outFasta);
408 outputNames.push_back(outputFastaFile); outputTypes["fasta"].push_back(outputFastaFile);
410 outSummary << "OTU#\tPositioninAlignment\tA\tT\tG\tC\tGap\tNumberofSeqs\tConsensusBase" << endl;
412 for (int i = 0; i < list->getNumBins(); i++) {
414 if (m->control_pressed) { outSummary.close(); outName.close(); outFasta.close(); return 0; }
416 string bin = list->get(i);
419 string consSeq = getConsSeq(bin, outSummary, newName, i);
421 outFasta << ">seq" << (i+1) << endl << consSeq << endl;
422 outName << "seq" << (i+1) << '\t' << "seq" << (i+1) << "," << newName << endl;
425 outSummary.close(); outName.close(); outFasta.close();
430 catch(exception& e) {
431 m->errorOut(e, "ConsensusSeqsCommand", "processList");
436 //***************************************************************************************************************
437 //made this smart enough to owrk with unique or non unique list file
438 string ConsensusSeqsCommand::getConsSeq(string bin, ofstream& outSummary, string& name, int binNumber){
444 //the whole bin is the second column if no names file, otherwise build it
446 if (namefile != "") { name = ""; }
448 vector<string> binNames;
449 m->splitAtComma(bin, binNames);
451 //get sequence strings for each name in the bin
454 set<string> addedAlready;
456 for (int i = 0; i < binNames.size(); i++) {
458 map<string, string>::iterator it;
460 it = nameMap.find(binNames[i]);
461 if (it == nameMap.end()) {
462 if (namefile == "") { m->mothurOut("[ERROR]: " + binNames[i] + " is not in your fasta file, please correct."); m->mothurOutEndLine(); error = true; }
463 else { m->mothurOut("[ERROR]: " + binNames[i] + " is not in your fasta or name file, please correct."); m->mothurOutEndLine(); error = true; }
467 //add sequence string to seqs vector to process below
468 string seq = fastaMap[it->second];
471 if (seqLength == 0) { seqLength = seq.length(); }
472 else if (seqLength != seq.length()) { m->mothurOut("[ERROR]: sequence are not the same length, please correct."); m->mothurOutEndLine(); error = true; break; }
474 if (namefile != "") {
475 //did we add this line from name file already?
476 if (addedAlready.count(it->second) == 0) {
477 name += "," + nameFileMap[it->second];
478 addedAlready.insert(it->second);
485 if (error) { m->control_pressed = true; return consSeq; }
487 if (namefile != "") { name = name.substr(1); }
489 vector< vector<float> > percentages; percentages.resize(5);
490 for (int j = 0; j < percentages.size(); j++) { percentages[j].resize(seqLength, 0.0); }
493 for (int j = 0; j < seqLength; j++) {
495 if (m->control_pressed) { return consSeq; }
497 vector<int> counts; counts.resize(5, 0); //A,T,G,C,Gap
500 for (int i = 0; i < seqs.size(); i++) {
502 if (seqs[i][j] == '.') { numDots++; }
504 char base = toupper(seqs[i][j]);
505 if (base == 'A') { counts[0]++; }
506 else if (base == 'T') { counts[1]++; }
507 else if (base == 'G') { counts[2]++; }
508 else if (base == 'C') { counts[3]++; }
509 else { counts[4]++; }
513 if (numDots != seqs.size()) { conBase = getBase(counts, seqs.size()); }
517 percentages[0][j] = counts[0] / (float) seqs.size();
518 percentages[1][j] = counts[1] / (float) seqs.size();
519 percentages[2][j] = counts[2] / (float) seqs.size();
520 percentages[3][j] = counts[3] / (float) seqs.size();
521 percentages[4][j] = counts[4] / (float) seqs.size();
525 for (int j = 0; j < seqLength; j++) {
526 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' << seqs.size() << '\t' << consSeq[j] << endl;
532 catch(exception& e) {
533 m->errorOut(e, "ConsensusSeqsCommand", "getConsSeq");
537 //***************************************************************************************************************
539 char ConsensusSeqsCommand::getBase(vector<int> counts, int size){ //A,T,G,C,Gap
546 * Y = T C (pyrimidine)
549 * S = G C (strong bonds)
550 * W = A T (weak bonds)
551 * B = G T C (all but A)
552 * D = G A T (all but C)
553 * H = A C T (all but G)
554 * V = G C A (all but T)
555 * N = A G C T (any) */
559 //zero out counts that don't make the cutoff
560 float percentage = (100.0 - cutoff) / 100.0;
561 int zeroCutoff = percentage * size;
563 for (int i = 0; i < counts.size(); i++) {
564 if (counts[i] < zeroCutoff) { counts[i] = 0; }
568 if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'n'; }
570 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'N'; }
572 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'v'; }
574 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'V'; }
576 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'h'; }
578 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'H'; }
580 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'd'; }
582 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'D'; }
584 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'b'; }
586 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'B'; }
587 //W = A T (weak bonds)
588 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'w'; }
589 //W = A T (weak bonds) no gap
590 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'W'; }
591 //S = G C (strong bonds)
592 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 's'; }
593 //S = G C (strong bonds) no gap
594 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'S'; }
596 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'm'; }
597 //M = A C (amino) no gap
598 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'M'; }
600 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'k'; }
601 //K = G T (keto) no gap
602 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'K'; }
603 //Y = T C (pyrimidine)
604 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'y'; }
605 //Y = T C (pyrimidine) no gap
606 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'Y'; }
608 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'r'; }
609 //R = G A (purine) no gap
610 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'R'; }
612 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'a'; }
614 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'A'; }
616 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 't'; }
618 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'T'; }
620 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'g'; }
622 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'G'; }
624 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'c'; }
626 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'C'; }
628 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = '-'; }
629 //cutoff removed all counts
630 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'N'; }
631 else{ m->mothurOut("[ERROR]: cannot find consensus base."); m->mothurOutEndLine(); }
636 catch(exception& e) {
637 m->errorOut(e, "ConsensusSeqsCommand", "getBase");
642 //***************************************************************************************************************
644 int ConsensusSeqsCommand::readFasta(){
648 m->openInputFile(fastafile, in);
652 if (m->control_pressed) { break; }
654 Sequence seq(in); m->gobble(in);
655 string name = seq.getName();
658 fastaMap[name] = seq.getAligned();
659 nameMap[name] = name; //set nameMap incase no names file
660 nameFileMap[name] = name;
669 catch(exception& e) {
670 m->errorOut(e, "ConsensusSeqsCommand", "readFasta");
674 //***************************************************************************************************************
676 int ConsensusSeqsCommand::readNames(){
678 map<string, string> temp;
679 map<string, string>::iterator it;
682 m->readNames(namefile, temp); //use central buffered read
684 for (map<string, string>::iterator itTemp = temp.begin(); itTemp != temp.end(); itTemp++) {
685 string thisname, repnames;
686 thisname = itTemp->first;
687 repnames = itTemp->second;
689 it = nameMap.find(thisname);
690 if (it != nameMap.end()) { //then this sequence was in the fastafile
691 nameFileMap[thisname] = repnames; //for later when outputting the new namesFile if the list file is unique
693 vector<string> splitRepNames;
694 m->splitAtComma(repnames, splitRepNames);
696 for (int i = 0; i < splitRepNames.size(); i++) { nameMap[splitRepNames[i]] = thisname; }
698 }else{ m->mothurOut("[ERROR]: " + thisname + " is not in the fasta file, please correct."); m->mothurOutEndLine(); error = true; }
701 if (error) { m->control_pressed = true; }
706 catch(exception& e) {
707 m->errorOut(e, "ConsensusSeqsCommand", "readNames");
712 //***************************************************************************************************************