2 * consensusseqscommand.cpp
5 * Created by westcott on 11/23/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "consensusseqscommand.h"
11 #include "sequence.hpp"
12 #include "inputdata.h"
14 //**********************************************************************************************************************
15 vector<string> ConsensusSeqsCommand::getValidParameters(){
17 string Array[] = {"fasta", "list", "name", "label","outputdir","inputdir"};
18 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
22 m->errorOut(e, "ConsensusSeqsCommand", "getValidParameters");
26 //**********************************************************************************************************************
27 ConsensusSeqsCommand::ConsensusSeqsCommand(){
30 //initialize outputTypes
31 vector<string> tempOutNames;
32 outputTypes["fasta"] = tempOutNames;
33 outputTypes["name"] = tempOutNames;
34 outputTypes["summary"] = tempOutNames;
37 m->errorOut(e, "ConsensusSeqsCommand", "ConsensusSeqsCommand");
41 //**********************************************************************************************************************
42 vector<string> ConsensusSeqsCommand::getRequiredParameters(){
44 string Array[] = {"fasta", "list"};
45 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
49 m->errorOut(e, "ConsensusSeqsCommand", "getRequiredParameters");
53 //**********************************************************************************************************************
54 vector<string> ConsensusSeqsCommand::getRequiredFiles(){
56 vector<string> myArray;
60 m->errorOut(e, "DegapSeqsCommand", "getRequiredFiles");
64 //***************************************************************************************************************
65 ConsensusSeqsCommand::ConsensusSeqsCommand(string option) {
70 //allow user to run help
71 if(option == "help") { help(); abort = true; }
74 //valid paramters for this command
75 string Array[] = {"fasta","list","name","label", "outputdir","inputdir"};
76 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
78 OptionParser parser(option);
79 map<string,string> parameters = parser.getParameters();
81 ValidParameters validParameter;
82 map<string,string>::iterator it;
84 //check to make sure all parameters are valid for command
85 for (it = parameters.begin(); it != parameters.end(); it++) {
86 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
89 //initialize outputTypes
90 vector<string> tempOutNames;
91 outputTypes["fasta"] = tempOutNames;
92 outputTypes["name"] = tempOutNames;
93 outputTypes["summary"] = tempOutNames;
96 //if the user changes the input directory command factory will send this info to us in the output parameter
97 string inputDir = validParameter.validFile(parameters, "inputdir", false);
98 if (inputDir == "not found"){ inputDir = ""; }
101 it = parameters.find("fasta");
102 //user has given a template file
103 if(it != parameters.end()){
104 path = m->hasPath(it->second);
105 //if the user has not given a path then, add inputdir. else leave path alone.
106 if (path == "") { parameters["fasta"] = inputDir + it->second; }
109 it = parameters.find("name");
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["name"] = inputDir + it->second; }
117 it = parameters.find("list");
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["list"] = inputDir + it->second; }
127 //check for parameters
128 fastafile = validParameter.validFile(parameters, "fasta", true);
129 if (fastafile == "not open") { abort = true; }
130 else if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the consensus.seqs command."); m->mothurOutEndLine(); abort = true; }
132 namefile = validParameter.validFile(parameters, "name", true);
133 if (namefile == "not open") { abort = true; }
134 else if (namefile == "not found") { namefile = ""; }
136 listfile = validParameter.validFile(parameters, "list", true);
137 if (listfile == "not open") { abort = true; }
138 else if (listfile == "not found") { listfile = ""; m->mothurOut("list is a required parameter for the consensus.seqs command."); m->mothurOutEndLine(); abort = true; }
140 label = validParameter.validFile(parameters, "label", false);
141 if (label == "not found") { label = ""; }
143 if(label != "all") { m->splitAtDash(label, labels); allLines = 0; }
144 else { allLines = 1; }
147 //if the user changes the output directory command factory will send this info to us in the output parameter
148 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(fastafile); }
152 catch(exception& e) {
153 m->errorOut(e, "ConsensusSeqsCommand", "ConsensusSeqsCommand");
157 //**********************************************************************************************************************
159 void ConsensusSeqsCommand::help(){
161 m->mothurOut("The consensus.seqs command reads a fastafile and listfile and creates a consensus sequence for each otu. Sequences must be aligned.\n");
162 m->mothurOut("The consensus.seqs command parameters are fasta, list, name and label.\n");
163 m->mothurOut("The fasta parameter allows you to enter the fasta file containing your sequences, and is required. \n");
164 m->mothurOut("The list parameter allows you to enter a your list file. \n");
165 m->mothurOut("The name parameter allows you to enter a names file associated with the fasta file. \n");
166 m->mothurOut("The label parameter allows you to select what distance levels you would like output files for, and are separated by dashes.\n");
167 m->mothurOut("The consensus.seqs command should be in the following format: \n");
168 m->mothurOut("consensus.seqs(fasta=yourFastaFile, list=yourListFile) \n");
169 m->mothurOut("Example: consensus.seqs(fasta=abrecovery.align, list=abrecovery.fn.list) \n");
170 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
172 catch(exception& e) {
173 m->errorOut(e, "ConsensusSeqsCommand", "help");
178 //***************************************************************************************************************
180 ConsensusSeqsCommand::~ConsensusSeqsCommand(){ /* do nothing */ }
182 //***************************************************************************************************************
184 int ConsensusSeqsCommand::execute(){
187 if (abort == true) { return 0; }
191 if (m->control_pressed) { return 0; }
193 if (namefile != "") { readNames(); }
195 if (m->control_pressed) { return 0; }
197 InputData* input = new InputData(listfile, "list");
198 ListVector* list = input->getListVector();
200 string lastLabel = list->getLabel();
201 set<string> processedLabels;
202 set<string> userLabels = labels;
204 //as long as you are not at the end of the file or done wih the lines you want
205 while((list != NULL) && ((allLines == 1) || (userLabels.size() != 0))) {
207 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } delete list; delete input; return 0; }
209 if(allLines == 1 || labels.count(list->getLabel()) == 1){
211 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
215 processedLabels.insert(list->getLabel());
216 userLabels.erase(list->getLabel());
219 if ((m->anyLabelsToProcess(list->getLabel(), userLabels, "") == true) && (processedLabels.count(lastLabel) != 1)) {
220 string saveLabel = list->getLabel();
224 list = input->getListVector(lastLabel);
225 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
229 processedLabels.insert(list->getLabel());
230 userLabels.erase(list->getLabel());
232 //restore real lastlabel to save below
233 list->setLabel(saveLabel);
236 lastLabel = list->getLabel();
238 delete list; list = NULL;
240 //get next line to process
241 list = input->getListVector();
245 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } if (list != NULL) { delete list; } delete input; return 0; }
247 //output error messages about any remaining user labels
248 set<string>::iterator it;
249 bool needToRun = false;
250 for (it = userLabels.begin(); it != userLabels.end(); it++) {
251 m->mothurOut("Your file does not include the label " + *it);
252 if (processedLabels.count(lastLabel) != 1) {
253 m->mothurOut(". I will use " + lastLabel + "."); m->mothurOutEndLine();
256 m->mothurOut(". Please refer to " + lastLabel + "."); m->mothurOutEndLine();
260 //run last label if you need to
261 if (needToRun == true) {
262 if (list != NULL) { delete list; }
264 list = input->getListVector(lastLabel);
266 m->mothurOut(list->getLabel()); m->mothurOutEndLine();
270 delete list; list = NULL;
273 if (list != NULL) { delete list; }
276 m->mothurOutEndLine();
277 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
278 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
279 m->mothurOutEndLine();
285 catch(exception& e) {
286 m->errorOut(e, "ConsensusSeqsCommand", "execute");
290 //***************************************************************************************************************
292 int ConsensusSeqsCommand::processList(ListVector*& list){
296 string outputSummaryFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + list->getLabel() + ".cons.summary";
297 m->openOutputFile(outputSummaryFile, outSummary);
298 outSummary.setf(ios::fixed, ios::floatfield); outSummary.setf(ios::showpoint);
299 outputNames.push_back(outputSummaryFile); outputTypes["summary"].push_back(outputSummaryFile);
302 string outputNameFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + list->getLabel() + ".cons.names";
303 m->openOutputFile(outputNameFile, outName);
304 outputNames.push_back(outputNameFile); outputTypes["name"].push_back(outputNameFile);
307 string outputFastaFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + list->getLabel() + ".cons.fasta";
308 m->openOutputFile(outputFastaFile, outFasta);
309 outputNames.push_back(outputFastaFile); outputTypes["fasta"].push_back(outputFastaFile);
311 for (int i = 0; i < list->getNumBins(); i++) {
313 if (m->control_pressed) { outSummary.close(); outName.close(); outFasta.close(); return 0; }
315 string bin = list->get(i);
318 string consSeq = getConsSeq(bin, outSummary, newName, i);
320 outFasta << ">seq" << (i+1) << endl << consSeq << endl;
321 outName << "seq" << (i+1) << '\t' << "seq" << (i+1) << "," << newName << endl;
324 outSummary.close(); outName.close(); outFasta.close();
329 catch(exception& e) {
330 m->errorOut(e, "ConsensusSeqsCommand", "processList");
335 //***************************************************************************************************************
336 //made this smart enough to owrk with unique or non unique list file
337 string ConsensusSeqsCommand::getConsSeq(string bin, ofstream& outSummary, string& name, int binNumber){
343 //the whole bin is the second column if no names file, otherwise build it
345 if (namefile != "") { name = ""; }
347 vector<string> binNames;
348 m->splitAtComma(bin, binNames);
350 //get sequence strings for each name in the bin
353 set<string> addedAlready;
355 for (int i = 0; i < binNames.size(); i++) {
357 map<string, string>::iterator it;
359 it = nameMap.find(binNames[i]);
360 if (it == nameMap.end()) {
361 if (namefile == "") { m->mothurOut("[ERROR]: " + binNames[i] + " is not in your fasta file, please correct."); m->mothurOutEndLine(); error = true; }
362 else { m->mothurOut("[ERROR]: " + binNames[i] + " is not in your fasta or name file, please correct."); m->mothurOutEndLine(); error = true; }
366 //add sequence string to seqs vector to process below
367 string seq = fastaMap[it->second];
370 if (seqLength == 0) { seqLength = seq.length(); }
371 else if (seqLength != seq.length()) { m->mothurOut("[ERROR]: sequence are not the same length, please correct."); m->mothurOutEndLine(); error = true; break; }
373 if (namefile != "") {
374 //did we add this line from name file already?
375 if (addedAlready.count(it->second) == 0) {
376 name += "," + nameFileMap[it->second];
377 addedAlready.insert(it->second);
384 if (error) { m->control_pressed = true; return consSeq; }
386 if (namefile != "") { name = name.substr(1); }
388 vector< vector<float> > percentages; percentages.resize(5);
389 for (int j = 0; j < percentages.size(); j++) { percentages[j].resize(seqLength, 0.0); }
392 for (int j = 0; j < seqLength; j++) {
394 if (m->control_pressed) { return consSeq; }
396 vector<int> counts; counts.resize(5, 0); //A,T,G,C,Gap
399 for (int i = 0; i < seqs.size(); i++) {
401 if (seqs[i][j] == '.') { numDots++; }
403 char base = toupper(seqs[i][j]);
404 if (base == 'A') { counts[0]++; }
405 else if (base == 'T') { counts[1]++; }
406 else if (base == 'G') { counts[2]++; }
407 else if (base == 'C') { counts[3]++; }
408 else { counts[4]++; }
412 if (numDots != seqs.size()) { conBase = getBase(counts); }
416 percentages[0][j] = counts[0] / (float) seqs.size();
417 percentages[1][j] = counts[1] / (float) seqs.size();
418 percentages[2][j] = counts[2] / (float) seqs.size();
419 percentages[3][j] = counts[3] / (float) seqs.size();
420 percentages[4][j] = counts[4] / (float) seqs.size();
424 outSummary << ">seq" << (binNumber + 1) << endl;
425 outSummary << "A" << '\t';
426 for (int j = 0; j < seqLength; j++) { outSummary << percentages[0][j] << '\t'; }
428 outSummary << "T" << '\t';
429 for (int j = 0; j < seqLength; j++) { outSummary << percentages[1][j] << '\t'; }
431 outSummary << "G" << '\t';
432 for (int j = 0; j < seqLength; j++) { outSummary << percentages[2][j] << '\t'; }
434 outSummary << "C" << '\t';
435 for (int j = 0; j < seqLength; j++) { outSummary << percentages[3][j] << '\t'; }
437 outSummary << "Gap" << '\t';
438 for (int j = 0; j < seqLength; j++) { outSummary << percentages[4][j] << '\t'; }
445 catch(exception& e) {
446 m->errorOut(e, "ConsensusSeqsCommand", "getConsSeq");
450 //***************************************************************************************************************
452 char ConsensusSeqsCommand::getBase(vector<int> counts){ //A,T,G,C,Gap
459 * Y = T C (pyrimidine)
462 * S = G C (strong bonds)
463 * W = A T (weak bonds)
464 * B = G T C (all but A)
465 * D = G A T (all but C)
466 * H = A C T (all but G)
467 * V = G C A (all but T)
468 * N = A G C T (any) */
473 if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'n'; }
475 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'N'; }
477 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'v'; }
479 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'V'; }
481 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'h'; }
483 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'H'; }
485 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'd'; }
487 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'D'; }
489 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'b'; }
491 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'B'; }
492 //W = A T (weak bonds)
493 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'w'; }
494 //W = A T (weak bonds) no gap
495 else if ((counts[0] != 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'W'; }
496 //S = G C (strong bonds)
497 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 's'; }
498 //S = G C (strong bonds) no gap
499 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'S'; }
501 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'm'; }
502 //M = A C (amino) no gap
503 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'M'; }
505 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'k'; }
506 //K = G T (keto) no gap
507 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'K'; }
508 //Y = T C (pyrimidine)
509 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'y'; }
510 //Y = T C (pyrimidine) no gap
511 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'Y'; }
513 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'r'; }
514 //R = G A (purine) no gap
515 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'R'; }
517 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'a'; }
519 else if ((counts[0] != 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'A'; }
521 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 't'; }
523 else if ((counts[0] == 0) && (counts[1] != 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'T'; }
525 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = 'g'; }
527 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] != 0) && (counts[3] == 0) && (counts[4] == 0)) { conBase = 'G'; }
529 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] != 0)) { conBase = 'c'; }
531 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] != 0) && (counts[4] == 0)) { conBase = 'C'; }
533 else if ((counts[0] == 0) && (counts[1] == 0) && (counts[2] == 0) && (counts[3] == 0) && (counts[4] != 0)) { conBase = '-'; }
534 else{ m->mothurOut("[ERROR]: cannot find consensus base."); m->mothurOutEndLine(); }
539 catch(exception& e) {
540 m->errorOut(e, "ConsensusSeqsCommand", "getBase");
545 //***************************************************************************************************************
547 int ConsensusSeqsCommand::readFasta(){
551 m->openInputFile(fastafile, in);
555 if (m->control_pressed) { break; }
557 Sequence seq(in); m->gobble(in);
558 string name = seq.getName();
561 fastaMap[name] = seq.getAligned();
562 nameMap[name] = name; //set nameMap incase no names file
563 nameFileMap[name] = name;
572 catch(exception& e) {
573 m->errorOut(e, "ConsensusSeqsCommand", "readFasta");
577 //***************************************************************************************************************
579 int ConsensusSeqsCommand::readNames(){
583 m->openInputFile(namefile, in);
585 string thisname, repnames;
586 map<string, string>::iterator it;
592 if (m->control_pressed) { break; }
594 in >> thisname; m->gobble(in); //read from first column
595 in >> repnames; //read from second column
597 it = nameMap.find(thisname);
598 if (it != nameMap.end()) { //then this sequence was in the fastafile
600 vector<string> splitRepNames;
601 m->splitAtComma(repnames, splitRepNames);
603 nameFileMap[thisname] = repnames; //for later when outputting the new namesFile if the list file is unique
604 for (int i = 0; i < splitRepNames.size(); i++) { nameMap[splitRepNames[i]] = thisname; }
606 }else{ m->mothurOut("[ERROR]: " + thisname + " is not in the fasta file, please correct."); m->mothurOutEndLine(); error = true; }
613 if (error) { m->control_pressed = true; }
618 catch(exception& e) {
619 m->errorOut(e, "ConsensusSeqsCommand", "readNames");
624 //***************************************************************************************************************