]> git.donarmstrong.com Git - mothur.git/blob - consensusseqscommand.cpp
v 19.3
[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                 
540                 //any
541                 if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) {  conBase = 'n'; }
542                 //any no gap
543                 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) {  conBase = 'N'; }
544                 //all but T
545                 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) {  conBase = 'v'; }  
546                 //all but T no gap
547                 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) {  conBase = 'V'; }  
548                 //all but G
549                 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) {  conBase = 'h'; }  
550                 //all but G no gap
551                 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) {  conBase = 'H'; }  
552                 //all but C
553                 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) {  conBase = 'd'; }  
554                 //all but C no gap
555                 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) {  conBase = 'D'; }  
556                 //all but A
557                 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) {  conBase = 'b'; }  
558                 //all but A no gap
559                 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) {  conBase = 'B'; }  
560                 //W = A T (weak bonds)
561                 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) {  conBase = 'w'; }  
562                 //W = A T (weak bonds) no gap
563                 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) {  conBase = 'W'; }  
564                 //S = G C (strong bonds)
565                 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) {  conBase = 's'; }  
566                 //S = G C (strong bonds) no gap
567                 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) {  conBase = 'S'; }  
568                 //M = A C (amino)
569                 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) {  conBase = 'm'; }  
570                 //M = A C (amino) no gap
571                 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) {  conBase = 'M'; }  
572                 //K = G T (keto)
573                 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) {  conBase = 'k'; }  
574                 //K = G T (keto) no gap
575                 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) {  conBase = 'K'; }  
576                 //Y = T C (pyrimidine)
577                 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) {  conBase = 'y'; }  
578                 //Y = T C (pyrimidine) no gap
579                 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) {  conBase = 'Y'; }  
580                 //R = G A (purine)
581                 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) {  conBase = 'r'; }  
582                 //R = G A (purine) no gap
583                 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) {  conBase = 'R'; }  
584                 //only A
585                 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) {  conBase = 'a'; }  
586                 //only A no gap
587                 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) {  conBase = 'A'; }  
588                 //only T
589                 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) {  conBase = 't'; }  
590                 //only T no gap
591                 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) {  conBase = 'T'; }  
592                 //only G
593                 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) {  conBase = 'g'; }  
594                 //only G no gap
595                 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) {  conBase = 'G'; }  
596                 //only C
597                 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) {  conBase = 'c'; }  
598                 //only C no gap
599                 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) {  conBase = 'C'; }  
600                 //only gap
601                 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) {  conBase = '-'; }
602                 //cutoff removed all counts
603                 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) {  m->mothurOut("cutoff ...."); m->mothurOutEndLine(); }
604                 else{ m->mothurOut("[ERROR]: cannot find consensus base."); m->mothurOutEndLine(); }
605                 
606                 return conBase;
607                 
608         }
609         catch(exception& e) {
610                 m->errorOut(e, "ConsensusSeqsCommand", "getBase");
611                 exit(1);
612         }
613 }
614
615 //***************************************************************************************************************
616
617 int ConsensusSeqsCommand::readFasta(){
618         try{
619                 
620                 ifstream in;
621                 m->openInputFile(fastafile, in);
622                 
623                 while (!in.eof()) {
624                         
625                         if (m->control_pressed) { break; }
626                         
627                         Sequence seq(in); m->gobble(in);
628                         string name = seq.getName();
629                         
630                         if (name != "") {
631                                 fastaMap[name] = seq.getAligned();
632                                 nameMap[name] = name; //set nameMap incase no names file
633                                 nameFileMap[name] = name;
634                         }
635                 }
636                 
637                 in.close();
638                 
639                 return 0;
640                 
641         }
642         catch(exception& e) {
643                 m->errorOut(e, "ConsensusSeqsCommand", "readFasta");
644                 exit(1);
645         }
646 }
647 //***************************************************************************************************************
648
649 int ConsensusSeqsCommand::readNames(){
650          try{
651                  
652                  ifstream in;
653                  m->openInputFile(namefile, in);
654                  
655                  string thisname, repnames;
656                  map<string, string>::iterator it;
657                  
658                  bool error = false;
659                  
660                  while(!in.eof()){
661                          
662                          if (m->control_pressed) { break; }
663                          
664                          in >> thisname;                m->gobble(in);          //read from first column
665                          in >> repnames;                        //read from second column
666                          
667                          it = nameMap.find(thisname);
668                          if (it != nameMap.end()) { //then this sequence was in the fastafile
669                                  
670                                  vector<string> splitRepNames;
671                                  m->splitAtComma(repnames, splitRepNames);
672                                  
673                                  nameFileMap[thisname] = repnames;      //for later when outputting the new namesFile if the list file is unique
674                                  for (int i = 0; i < splitRepNames.size(); i++) { nameMap[splitRepNames[i]] = thisname; }
675                                  
676                          }else{ m->mothurOut("[ERROR]: " + thisname + " is not in the fasta file, please correct."); m->mothurOutEndLine(); error = true; }
677                          
678                          m->gobble(in);
679                  }
680                  
681                  in.close();
682                  
683                  if (error) { m->control_pressed = true; }
684  
685                  return 0;
686  
687         }
688          catch(exception& e) {
689                  m->errorOut(e, "ConsensusSeqsCommand", "readNames");
690                  exit(1);
691          }
692  }
693  
694 //***************************************************************************************************************
695
696