]> git.donarmstrong.com Git - mothur.git/blob - consensusseqscommand.cpp
added count.seqs command and made some modifcations to the uchime code to allow it...
[mothur.git] / consensusseqscommand.cpp
1 /*
2  *  consensusseqscommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 11/23/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "consensusseqscommand.h"
11 #include "sequence.hpp"
12 #include "inputdata.h"
13
14 //**********************************************************************************************************************
15 vector<string> ConsensusSeqsCommand::setParameters(){   
16         try {
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);
24                 
25                 vector<string> myArray;
26                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
27                 return myArray;
28         }
29         catch(exception& e) {
30                 m->errorOut(e, "ConsensusSeqsCommand", "setParameters");
31                 exit(1);
32         }
33 }
34 //**********************************************************************************************************************
35 string ConsensusSeqsCommand::getHelpString(){   
36         try {
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";       
49                 return helpString;
50         }
51         catch(exception& e) {
52                 m->errorOut(e, "ConsensusSeqsCommand", "getHelpString");
53                 exit(1);
54         }
55 }
56
57 //**********************************************************************************************************************
58 ConsensusSeqsCommand::ConsensusSeqsCommand(){   
59         try {
60                 abort = true; calledHelp = true; 
61                 setParameters();
62                 vector<string> tempOutNames;
63                 outputTypes["fasta"] = tempOutNames;
64                 outputTypes["name"] = tempOutNames;
65                 outputTypes["summary"] = tempOutNames;
66         }
67         catch(exception& e) {
68                 m->errorOut(e, "ConsensusSeqsCommand", "ConsensusSeqsCommand");
69                 exit(1);
70         }
71 }
72 //***************************************************************************************************************
73 ConsensusSeqsCommand::ConsensusSeqsCommand(string option)  {
74         try {
75                 abort = false; calledHelp = false;   
76                 allLines = 1;
77                 
78                 //allow user to run help
79                 if(option == "help") { help(); abort = true; calledHelp = true; }
80                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
81                 
82                 else {
83                         
84                         vector<string> myArray = setParameters();
85                         
86                         OptionParser parser(option);
87                         map<string,string> parameters = parser.getParameters();
88                         
89                         ValidParameters validParameter;
90                         map<string,string>::iterator it;
91                         
92                         //check to make sure all parameters are valid for command
93                         for (it = parameters.begin(); it != parameters.end(); it++) { 
94                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
95                         }
96                         
97                         //initialize outputTypes
98                         vector<string> tempOutNames;
99                         outputTypes["fasta"] = tempOutNames;
100                         outputTypes["name"] = tempOutNames;
101                         outputTypes["summary"] = tempOutNames;
102                         
103                                                 
104                         //if the user changes the input directory command factory will send this info to us in the output parameter 
105                         string inputDir = validParameter.validFile(parameters, "inputdir", false);      
106                         if (inputDir == "not found"){   inputDir = "";          }
107                         else {
108                                 string path;
109                                 it = parameters.find("fasta");
110                                 //user has given a template file
111                                 if(it != parameters.end()){ 
112                                         path = m->hasPath(it->second);
113                                         //if the user has not given a path then, add inputdir. else leave path alone.
114                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
115                                 }
116                                 
117                                 it = parameters.find("name");
118                                 //user has given a template file
119                                 if(it != parameters.end()){ 
120                                         path = m->hasPath(it->second);
121                                         //if the user has not given a path then, add inputdir. else leave path alone.
122                                         if (path == "") {       parameters["name"] = inputDir + it->second;             }
123                                 }
124                                 
125                                 it = parameters.find("list");
126                                 //user has given a template file
127                                 if(it != parameters.end()){ 
128                                         path = m->hasPath(it->second);
129                                         //if the user has not given a path then, add inputdir. else leave path alone.
130                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
131                                 }
132                         }
133                         
134                         
135                         //check for parameters
136                         fastafile = validParameter.validFile(parameters, "fasta", true);
137                         if (fastafile == "not open") { abort = true; }
138                         else if (fastafile == "not found") {                    
139                                 fastafile = m->getFastaFile(); 
140                                 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
141                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
142                         }       
143                         
144                         namefile = validParameter.validFile(parameters, "name", true);
145                         if (namefile == "not open") { abort = true; }
146                         else if (namefile == "not found") { namefile = ""; }    
147                         
148                         listfile = validParameter.validFile(parameters, "list", true);
149                         if (listfile == "not open") { abort = true; }
150                         else if (listfile == "not found") { listfile = "";  }   
151                         
152                         label = validParameter.validFile(parameters, "label", false);                   
153                         if (label == "not found") { label = ""; }
154                         else { 
155                                 if(label != "all") {  m->splitAtDash(label, labels);  allLines = 0;  }
156                                 else { allLines = 1;  }
157                         }
158                         
159                         string temp = validParameter.validFile(parameters, "cutoff", false);  if (temp == "not found") { temp = "100"; }
160                         convert(temp, cutoff); 
161                         
162                         //if the user changes the output directory command factory will send this info to us in the output parameter 
163                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(fastafile);      }
164
165                 }
166         }
167         catch(exception& e) {
168                 m->errorOut(e, "ConsensusSeqsCommand", "ConsensusSeqsCommand");
169                 exit(1);
170         }
171 }
172 //***************************************************************************************************************
173
174 int ConsensusSeqsCommand::execute(){
175         try{
176                 
177                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
178                 
179                 readFasta();
180                 
181                 if (m->control_pressed) { return 0; }
182                 
183                 if (namefile != "") { readNames(); }
184                 
185                 if (m->control_pressed) { return 0; }
186                 
187                                 
188                 if (listfile == "") {
189                         
190                         ofstream outSummary;
191                         string outputSummaryFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "cons.summary";
192                         m->openOutputFile(outputSummaryFile, outSummary);
193                         outSummary.setf(ios::fixed, ios::floatfield); outSummary.setf(ios::showpoint);
194                         outputNames.push_back(outputSummaryFile); outputTypes["summary"].push_back(outputSummaryFile);
195                         
196                         outSummary << "PositioninAlignment\tA\tT\tG\tC\tGap\tNumberofSeqs\tConsensusBase" << endl;
197                         
198                         ofstream outFasta;
199                         string outputFastaFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "cons.fasta";
200                         m->openOutputFile(outputFastaFile, outFasta);
201                         outputNames.push_back(outputFastaFile); outputTypes["fasta"].push_back(outputFastaFile);
202                         
203                         vector<string> seqs;
204                         int seqLength = 0;
205                         for (map<string, string>::iterator it = nameMap.begin(); it != nameMap.end(); it++) {
206                                 
207                                 if (m->control_pressed) { outSummary.close(); outFasta.close(); for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); } return 0; }
208                                 
209                                 string seq = fastaMap[it->second];
210                                 seqs.push_back(seq);
211                                 
212                                 if (seqLength == 0) { seqLength = seq.length(); }
213                                 else if (seqLength != seq.length()) { m->mothurOut("[ERROR]: sequence are not the same length, please correct."); m->mothurOutEndLine(); m->control_pressed = true; }
214
215                         }
216                         
217                         vector< vector<float> > percentages; percentages.resize(5);
218                         for (int j = 0; j < percentages.size(); j++) { percentages[j].resize(seqLength, 0.0); }
219                         
220                         string consSeq = "";
221                         //get counts
222                         for (int j = 0; j < seqLength; j++) {
223                                 
224                                 if (m->control_pressed) { outSummary.close(); outFasta.close(); for (int i = 0; i < outputNames.size(); i++) {  remove(outputNames[i].c_str()); } return 0; }
225                                 
226                                 vector<int> counts; counts.resize(5, 0); //A,T,G,C,Gap
227                                 int numDots = 0;
228                                 
229                                 for (int i = 0; i < seqs.size(); i++) {
230                                         
231                                         if (seqs[i][j] == '.') { numDots++; }
232                                         
233                                         char base = toupper(seqs[i][j]);
234                                         if (base == 'A') { counts[0]++; }
235                                         else if (base == 'T') { counts[1]++; }
236                                         else if (base == 'G') { counts[2]++; }
237                                         else if (base == 'C') { counts[3]++; }
238                                         else { counts[4]++; }
239                                 }
240                                 
241                                 char conBase = '.';
242                                 if (numDots != seqs.size()) { conBase = getBase(counts, seqs.size()); }
243                                 
244                                 consSeq += conBase;
245                                 
246                                 percentages[0][j] = counts[0] / (float) seqs.size();
247                                 percentages[1][j] = counts[1] / (float) seqs.size();
248                                 percentages[2][j] = counts[2] / (float) seqs.size();
249                                 percentages[3][j] = counts[3] / (float) seqs.size();
250                                 percentages[4][j] = counts[4] / (float) seqs.size();
251                                 
252                         }
253                         
254                         for (int j = 0; j < seqLength; j++) { 
255                                 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;
256                         }
257                         
258                                 
259                         outFasta << ">conseq" << endl << consSeq << endl;
260                         
261                         outSummary.close(); outFasta.close();
262                         
263                 
264                 }else {
265                         
266                                                 
267                         InputData* input = new InputData(listfile, "list");
268                         ListVector* list = input->getListVector();
269                         
270                         string lastLabel = list->getLabel();
271                         set<string> processedLabels;
272                         set<string> userLabels = labels;
273
274                         //as long as you are not at the end of the file or done wih the lines you want
275                         while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
276                                 
277                                 if (m->control_pressed) {  for (int i = 0; i < outputNames.size(); i++) {       remove(outputNames[i].c_str()); } delete list; delete input;  return 0;  }
278                                 
279                                 if(allLines == 1 || labels.count(list->getLabel()) == 1){                       
280                                         
281                                         m->mothurOut(list->getLabel()); m->mothurOutEndLine();
282                                         
283                                         processList(list);
284                                         
285                                         processedLabels.insert(list->getLabel());
286                                         userLabels.erase(list->getLabel());
287                                 }
288                                 
289                                 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
290                                         string saveLabel = list->getLabel();
291                                         
292                                         delete list; 
293                                         
294                                         list = input->getListVector(lastLabel);
295                                         m->mothurOut(list->getLabel()); m->mothurOutEndLine();
296                                         
297                                         processList(list);
298                                         
299                                         processedLabels.insert(list->getLabel());
300                                         userLabels.erase(list->getLabel());
301                                         
302                                         //restore real lastlabel to save below
303                                         list->setLabel(saveLabel);
304                                 }
305                                 
306                                 lastLabel = list->getLabel();
307                                 
308                                 delete list; list = NULL;
309                                 
310                                 //get next line to process
311                                 list = input->getListVector();                          
312                         }
313                         
314                         
315                         if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str()); } if (list != NULL) { delete list; } delete input; return 0;  }
316                         
317                         //output error messages about any remaining user labels
318                         set<string>::iterator it;
319                         bool needToRun = false;
320                         for (it = userLabels.begin(); it != userLabels.end(); it++) {  
321                                 m->mothurOut("Your file does not include the label " + *it); 
322                                 if (processedLabels.count(lastLabel) != 1) {
323                                         m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
324                                         needToRun = true;
325                                 }else {
326                                         m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
327                                 }
328                         }
329                         
330                         //run last label if you need to
331                         if (needToRun == true)  {
332                                 if (list != NULL) { delete list; }
333                                 
334                                 list = input->getListVector(lastLabel);
335                                 
336                                 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
337                                 
338                                 processList(list);
339                                 
340                                 delete list; list = NULL;
341                         }
342                         
343                         if (list != NULL) { delete list; }
344                         delete input;
345                 }
346                 
347                 m->mothurOutEndLine();
348                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
349                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }       
350                 m->mothurOutEndLine();
351                 
352                 
353                 return 0;
354                 
355         }
356         catch(exception& e) {
357                 m->errorOut(e, "ConsensusSeqsCommand", "execute");
358                 exit(1);
359         }
360 }
361 //***************************************************************************************************************
362
363 int ConsensusSeqsCommand::processList(ListVector*& list){
364         try{
365                 
366                 ofstream outSummary;
367                 string outputSummaryFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + list->getLabel() + ".cons.summary";
368                 m->openOutputFile(outputSummaryFile, outSummary);
369                 outSummary.setf(ios::fixed, ios::floatfield); outSummary.setf(ios::showpoint);
370                 outputNames.push_back(outputSummaryFile); outputTypes["summary"].push_back(outputSummaryFile);
371                 
372                 ofstream outName;
373                 string outputNameFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + list->getLabel() + ".cons.names";
374                 m->openOutputFile(outputNameFile, outName);
375                 outputNames.push_back(outputNameFile); outputTypes["name"].push_back(outputNameFile);
376                 
377                 ofstream outFasta;
378                 string outputFastaFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + list->getLabel() + ".cons.fasta";
379                 m->openOutputFile(outputFastaFile, outFasta);
380                 outputNames.push_back(outputFastaFile); outputTypes["fasta"].push_back(outputFastaFile);
381                 
382                 outSummary << "OTU#\tPositioninAlignment\tA\tT\tG\tC\tGap\tNumberofSeqs\tConsensusBase" << endl;
383                 
384                 for (int i = 0; i < list->getNumBins(); i++) {
385                         
386                         if (m->control_pressed) { outSummary.close(); outName.close(); outFasta.close(); return 0; }
387                         
388                         string bin = list->get(i);
389                         
390                         string newName = "";
391                         string consSeq = getConsSeq(bin, outSummary, newName, i);
392                         
393                         outFasta << ">seq" << (i+1) << endl << consSeq << endl;
394                         outName << "seq" << (i+1) << '\t' << "seq" << (i+1) << "," << newName << endl;
395                 }
396                 
397                 outSummary.close(); outName.close(); outFasta.close();
398                 
399                 return 0;
400                 
401         }
402         catch(exception& e) {
403                 m->errorOut(e, "ConsensusSeqsCommand", "processList");
404                 exit(1);
405         }
406 }
407
408 //***************************************************************************************************************
409 //made this smart enough to owrk with unique or non unique list file
410 string ConsensusSeqsCommand::getConsSeq(string bin, ofstream& outSummary, string& name, int binNumber){
411         try{
412                 
413                 string consSeq = "";
414                 bool error = false;
415                 
416                 //the whole bin is the second column if no names file, otherwise build it
417                 name = bin;
418                 if (namefile != "") { name = ""; }
419                 
420                 vector<string> binNames;
421                 m->splitAtComma(bin, binNames);
422                 
423                 //get sequence strings for each name in the bin
424                 vector<string> seqs;
425                 
426                 set<string> addedAlready;
427                 int seqLength = 0;
428                 for (int i = 0; i < binNames.size(); i++) {
429                         
430                         map<string, string>::iterator it;
431                         
432                         it = nameMap.find(binNames[i]);
433                         if (it == nameMap.end()) { 
434                                 if (namefile == "") { m->mothurOut("[ERROR]: " + binNames[i] + " is not in your fasta file, please correct."); m->mothurOutEndLine(); error = true; }
435                                 else { m->mothurOut("[ERROR]: " + binNames[i] + " is not in your fasta or name file, please correct."); m->mothurOutEndLine(); error = true; }
436                                 break;
437                         }else {
438                                 
439                                 //add sequence string to seqs vector to process below
440                                 string seq = fastaMap[it->second];
441                                 seqs.push_back(seq);
442                                 
443                                 if (seqLength == 0) { seqLength = seq.length(); }
444                                 else if (seqLength != seq.length()) { m->mothurOut("[ERROR]: sequence are not the same length, please correct."); m->mothurOutEndLine(); error = true; break; }
445                                 
446                                 if (namefile != "") { 
447                                         //did we add this line from name file already?
448                                         if (addedAlready.count(it->second) == 0) {
449                                                 name += "," + nameFileMap[it->second];
450                                                 addedAlready.insert(it->second);
451                                         }
452                                 }
453                                 
454                         }
455                 }
456                 
457                 if (error) { m->control_pressed = true; return consSeq; }
458                 
459                 if (namefile != "") { name = name.substr(1); }
460                 
461                 vector< vector<float> > percentages; percentages.resize(5);
462                 for (int j = 0; j < percentages.size(); j++) { percentages[j].resize(seqLength, 0.0); }
463                 
464                 //get counts
465                 for (int j = 0; j < seqLength; j++) {
466                         
467                         if (m->control_pressed) { return consSeq; }
468                         
469                         vector<int> counts; counts.resize(5, 0); //A,T,G,C,Gap
470                         int numDots = 0;
471                         
472                         for (int i = 0; i < seqs.size(); i++) {
473                                 
474                                 if (seqs[i][j] == '.') { numDots++; }
475                                 
476                                 char base = toupper(seqs[i][j]);
477                                 if (base == 'A') { counts[0]++; }
478                                 else if (base == 'T') { counts[1]++; }
479                                 else if (base == 'G') { counts[2]++; }
480                                 else if (base == 'C') { counts[3]++; }
481                                 else { counts[4]++; }
482                         }
483                         
484                         char conBase = '.';
485                         if (numDots != seqs.size()) { conBase = getBase(counts, seqs.size()); }
486                         
487                         consSeq += conBase;
488                         
489                         percentages[0][j] = counts[0] / (float) seqs.size();
490                         percentages[1][j] = counts[1] / (float) seqs.size();
491                         percentages[2][j] = counts[2] / (float) seqs.size();
492                         percentages[3][j] = counts[3] / (float) seqs.size();
493                         percentages[4][j] = counts[4] / (float) seqs.size();
494                         
495                 }
496                 
497                 for (int j = 0; j < seqLength; j++) { 
498                         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;
499                 }
500                 
501                 return consSeq;
502                 
503         }
504         catch(exception& e) {
505                 m->errorOut(e, "ConsensusSeqsCommand", "getConsSeq");
506                 exit(1);
507         }
508 }
509 //***************************************************************************************************************
510
511 char ConsensusSeqsCommand::getBase(vector<int> counts, int size){  //A,T,G,C,Gap
512         try{
513                 /* A = adenine
514                 * C = cytosine
515                 * G = guanine
516                 * T = thymine
517                 * R = G A (purine)
518                 * Y = T C (pyrimidine)
519                 * K = G T (keto)
520                 * M = A C (amino)
521                 * S = G C (strong bonds)
522                 * W = A T (weak bonds)
523                 * B = G T C (all but A)
524                 * D = G A T (all but C)
525                 * H = A C T (all but G)
526                 * V = G C A (all but T)
527                 * N = A G C T (any) */
528                 
529                 char conBase = 'N';
530                 
531                 //zero out counts that don't make the cutoff
532                 float percentage = (100.0 - cutoff) / 100.0;
533                 int zeroCutoff = percentage * size;
534                 
535                 for (int i = 0; i < counts.size(); i++) {
536                         if (counts[i] < zeroCutoff) { counts[i] = 0; }
537                 }
538                 
539                 //any
540                 if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) {  conBase = 'n'; }
541                 //any no gap
542                 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) {  conBase = 'N'; }
543                 //all but T
544                 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) {  conBase = 'v'; }  
545                 //all but T no gap
546                 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) {  conBase = 'V'; }  
547                 //all but G
548                 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) {  conBase = 'h'; }  
549                 //all but G no gap
550                 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) {  conBase = 'H'; }  
551                 //all but C
552                 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) {  conBase = 'd'; }  
553                 //all but C no gap
554                 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) {  conBase = 'D'; }  
555                 //all but A
556                 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) {  conBase = 'b'; }  
557                 //all but A no gap
558                 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) {  conBase = 'B'; }  
559                 //W = A T (weak bonds)
560                 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) {  conBase = 'w'; }  
561                 //W = A T (weak bonds) no gap
562                 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) {  conBase = 'W'; }  
563                 //S = G C (strong bonds)
564                 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) {  conBase = 's'; }  
565                 //S = G C (strong bonds) no gap
566                 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) {  conBase = 'S'; }  
567                 //M = A C (amino)
568                 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) {  conBase = 'm'; }  
569                 //M = A C (amino) no gap
570                 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) {  conBase = 'M'; }  
571                 //K = G T (keto)
572                 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) {  conBase = 'k'; }  
573                 //K = G T (keto) no gap
574                 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) {  conBase = 'K'; }  
575                 //Y = T C (pyrimidine)
576                 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) {  conBase = 'y'; }  
577                 //Y = T C (pyrimidine) no gap
578                 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) {  conBase = 'Y'; }  
579                 //R = G A (purine)
580                 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) {  conBase = 'r'; }  
581                 //R = G A (purine) no gap
582                 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) {  conBase = 'R'; }  
583                 //only A
584                 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) {  conBase = 'a'; }  
585                 //only A no gap
586                 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) {  conBase = 'A'; }  
587                 //only T
588                 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) {  conBase = 't'; }  
589                 //only T no gap
590                 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) {  conBase = 'T'; }  
591                 //only G
592                 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) {  conBase = 'g'; }  
593                 //only G no gap
594                 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) {  conBase = 'G'; }  
595                 //only C
596                 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) {  conBase = 'c'; }  
597                 //only C no gap
598                 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) {  conBase = 'C'; }  
599                 //only gap
600                 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) {  conBase = '-'; }
601                 //cutoff removed all counts
602                 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) {  conBase = 'N'; }
603                 else{ m->mothurOut("[ERROR]: cannot find consensus base."); m->mothurOutEndLine(); }
604                 
605                 return conBase;
606                 
607         }
608         catch(exception& e) {
609                 m->errorOut(e, "ConsensusSeqsCommand", "getBase");
610                 exit(1);
611         }
612 }
613
614 //***************************************************************************************************************
615
616 int ConsensusSeqsCommand::readFasta(){
617         try{
618                 
619                 ifstream in;
620                 m->openInputFile(fastafile, in);
621                 
622                 while (!in.eof()) {
623                         
624                         if (m->control_pressed) { break; }
625                         
626                         Sequence seq(in); m->gobble(in);
627                         string name = seq.getName();
628                         
629                         if (name != "") {
630                                 fastaMap[name] = seq.getAligned();
631                                 nameMap[name] = name; //set nameMap incase no names file
632                                 nameFileMap[name] = name;
633                         }
634                 }
635                 
636                 in.close();
637                 
638                 return 0;
639                 
640         }
641         catch(exception& e) {
642                 m->errorOut(e, "ConsensusSeqsCommand", "readFasta");
643                 exit(1);
644         }
645 }
646 //***************************************************************************************************************
647
648 int ConsensusSeqsCommand::readNames(){
649          try{
650                  
651                  ifstream in;
652                  m->openInputFile(namefile, in);
653                  
654                  string thisname, repnames;
655                  map<string, string>::iterator it;
656                  
657                  bool error = false;
658                  
659                  while(!in.eof()){
660                          
661                          if (m->control_pressed) { break; }
662                          
663                          in >> thisname;                m->gobble(in);          //read from first column
664                          in >> repnames;                        //read from second column
665                          
666                          it = nameMap.find(thisname);
667                          if (it != nameMap.end()) { //then this sequence was in the fastafile
668                                  
669                                  vector<string> splitRepNames;
670                                  m->splitAtComma(repnames, splitRepNames);
671                                  
672                                  nameFileMap[thisname] = repnames;      //for later when outputting the new namesFile if the list file is unique
673                                  for (int i = 0; i < splitRepNames.size(); i++) { nameMap[splitRepNames[i]] = thisname; }
674                                  
675                          }else{ m->mothurOut("[ERROR]: " + thisname + " is not in the fasta file, please correct."); m->mothurOutEndLine(); error = true; }
676                          
677                          m->gobble(in);
678                  }
679                  
680                  in.close();
681                  
682                  if (error) { m->control_pressed = true; }
683  
684                  return 0;
685  
686         }
687          catch(exception& e) {
688                  m->errorOut(e, "ConsensusSeqsCommand", "readNames");
689                  exit(1);
690          }
691  }
692  
693 //***************************************************************************************************************
694
695