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