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