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