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