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