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