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