X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=parsefastaqcommand.cpp;h=9509a20af2528392c2df8590aea61fef6a7665b4;hb=8dd3c225255d7084e3aff8740aa4f1f1cabb367a;hp=e808b9b45c7213de48a0f2ceed8a74e59f7e020f;hpb=0732c6305162ce19e861112b1e584b76a4290b67;p=mothur.git diff --git a/parsefastaqcommand.cpp b/parsefastaqcommand.cpp index e808b9b..9509a20 100644 --- a/parsefastaqcommand.cpp +++ b/parsefastaqcommand.cpp @@ -10,17 +10,64 @@ #include "parsefastaqcommand.h" #include "sequence.hpp" +//********************************************************************************************************************** +vector ParseFastaQCommand::setParameters(){ + try { + CommandParameter pfastq("fastq", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfastq); + CommandParameter pfasta("fasta", "Bool", "", "T", "", "", "",false,false); parameters.push_back(pfasta); + CommandParameter pqual("qfile", "Bool", "", "T", "", "", "",false,false); parameters.push_back(pqual); + CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir); + CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir); + + vector myArray; + for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); } + return myArray; + } + catch(exception& e) { + m->errorOut(e, "ParseFastaQCommand", "setParameters"); + exit(1); + } +} +//********************************************************************************************************************** +string ParseFastaQCommand::getHelpString(){ + try { + string helpString = ""; + helpString += "The fastq.info command reads a fastq file and creates a fasta and quality file.\n"; + helpString += "The fastq.info command parameter is fastq, and it is required.\n"; + helpString += "The fastq.info command should be in the following format: fastq.info(fastaq=yourFastaQFile).\n"; + helpString += "Example fastq.info(fastaq=test.fastaq).\n"; + helpString += "Note: No spaces between parameter labels (i.e. fastq), '=' and yourFastQFile.\n"; + return helpString; + } + catch(exception& e) { + m->errorOut(e, "ParseFastaQCommand", "getHelpString"); + exit(1); + } +} +//********************************************************************************************************************** +ParseFastaQCommand::ParseFastaQCommand(){ + try { + abort = true; calledHelp = true; + setParameters(); + vector tempOutNames; + outputTypes["fasta"] = tempOutNames; + outputTypes["qfile"] = tempOutNames; + } + catch(exception& e) { + m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand"); + exit(1); + } +} //********************************************************************************************************************** ParseFastaQCommand::ParseFastaQCommand(string option){ try { - abort = false; + abort = false; calledHelp = false; - if(option == "help") { help(); abort = true; } + if(option == "help") { help(); abort = true; calledHelp = true; } + else if(option == "citation") { citation(); abort = true; calledHelp = true;} else { - //valid paramters for this command - string Array[] = {"fastq", "outputdir", "inputdir"}; - vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + vector myArray = setParameters(); OptionParser parser(option); map parameters = parser.getParameters(); @@ -33,6 +80,11 @@ ParseFastaQCommand::ParseFastaQCommand(string option){ if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } } + //initialize outputTypes + vector tempOutNames; + outputTypes["fasta"] = tempOutNames; + outputTypes["qfile"] = tempOutNames; + //if the user changes the input directory command factory will send this info to us in the output parameter string inputDir = validParameter.validFile(parameters, "inputdir", false); if (inputDir == "not found"){ inputDir = ""; } @@ -54,6 +106,15 @@ ParseFastaQCommand::ParseFastaQCommand(string option){ //if the user changes the output directory command factory will send this info to us in the output parameter outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(fastaQFile); } + + string temp; + temp = validParameter.validFile(parameters, "fasta", false); if(temp == "not found"){ temp = "T"; } + fasta = m->isTrue(temp); + + temp = validParameter.validFile(parameters, "qfile", false); if(temp == "not found"){ temp = "T"; } + qual = m->isTrue(temp); + + if ((!fasta) && (!qual)) { m->mothurOut("[ERROR]: no outputs selected. Aborting."); m->mothurOutEndLine(); abort=true; } } } @@ -64,41 +125,24 @@ ParseFastaQCommand::ParseFastaQCommand(string option){ } //********************************************************************************************************************** -void ParseFastaQCommand::help(){ - try { - m->mothurOut("The fastq.info command reads a fastaQ file and creates a fasta and quality file.\n"); - m->mothurOut("The fastq.info command parameter is fastq, and it is required.\n"); - m->mothurOut("The fastq.info command should be in the following format: fastq.info(fastaq=yourFastaQFile).\n"); - m->mothurOut("Example fastq.info(fastaq=test.fastaq).\n"); - m->mothurOut("Note: No spaces between parameter labels (i.e. fastq), '=' and yourFastQFile.\n"); - m->mothurOutEndLine(); - } - catch(exception& e) { - m->errorOut(e, "ParseFastaQCommand", "help"); - exit(1); - } -} -//********************************************************************************************************************** - -ParseFastaQCommand::~ParseFastaQCommand() { /* do nothing */ } - -//********************************************************************************************************************** - int ParseFastaQCommand::execute(){ try { - if (abort == true) { return 0; } + if (abort == true) { if (calledHelp) { return 0; } return 2; } //open Output Files string fastaFile = outputDir + m->getRootName(m->getSimpleName(fastaQFile)) + "fasta"; string qualFile = outputDir + m->getRootName(m->getSimpleName(fastaQFile)) + "qual"; ofstream outFasta, outQual; - m->openOutputFile(fastaFile, outFasta); outputNames.push_back(fastaFile); - m->openOutputFile(qualFile, outQual); outputNames.push_back(qualFile); + + if (fasta) { m->openOutputFile(fastaFile, outFasta); outputNames.push_back(fastaFile); outputTypes["fasta"].push_back(fastaFile); } + if (qual) { m->openOutputFile(qualFile, outQual); outputNames.push_back(qualFile); outputTypes["qfile"].push_back(qualFile); } ifstream in; m->openInputFile(fastaQFile, in); while (!in.eof()) { + + if (m->control_pressed) { break; } //read sequence name string name = m->getline(in); m->gobble(in); @@ -117,29 +161,41 @@ int ParseFastaQCommand::execute(){ else { name2 = name2.substr(1); } //read quality scores - string qual = m->getline(in); m->gobble(in); - if (qual == "") { m->mothurOut("[ERROR]: missing quality for " + name2); m->mothurOutEndLine(); m->control_pressed = true; break; } + string quality = m->getline(in); m->gobble(in); + if (quality == "") { m->mothurOut("[ERROR]: missing quality for " + name2); m->mothurOutEndLine(); m->control_pressed = true; break; } //sanity check sequence length and number of quality scores match - if (name != name2) { m->mothurOut("[ERROR]: names do not match. read " + name + " for fasta and " + name2 + " for quality."); m->mothurOutEndLine(); m->control_pressed = true; break; } - if (qual.length() != sequence.length()) { m->mothurOut("[ERROR]: lengths do not match. read " + toString(sequence.length()) + " characters for fasta and " + toString(qual.length()) + " characters for quality scores."); m->mothurOutEndLine(); m->control_pressed = true; break; } - - //convert quality scores - vector qualScores = convertQual(qual); + if (name2 != "") { if (name != name2) { m->mothurOut("[ERROR]: names do not match. read " + name + " for fasta and " + name2 + " for quality."); m->mothurOutEndLine(); m->control_pressed = true; break; } } + if (quality.length() != sequence.length()) { m->mothurOut("[ERROR]: lengths do not match. read " + toString(sequence.length()) + " characters for fasta and " + toString(quality.length()) + " characters for quality scores."); m->mothurOutEndLine(); m->control_pressed = true; break; } //print sequence info to files - outFasta << ">" << name << endl << sequence << endl; + if (fasta) { outFasta << ">" << name << endl << sequence << endl; } - outQual << ">" << name << endl; - for (int i = 0; i < qualScores.size(); i++) { outQual << qualScores[i] << " "; } - outQual << endl; + if (qual) { + vector qualScores = convertQual(quality); + outQual << ">" << name << endl; + for (int i = 0; i < qualScores.size(); i++) { outQual << qualScores[i] << " "; } + outQual << endl; + } } in.close(); - outFasta.close(); - outQual.close(); + if (fasta) { outFasta.close(); } + if (qual) { outQual.close(); } + + if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(fastaFile); m->mothurRemove(qualFile); return 0; } - if (m->control_pressed) { remove(fastaFile.c_str()); remove(qualFile.c_str()); return 0; } + //set fasta file as new current fastafile + string current = ""; + itTypes = outputTypes.find("fasta"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); } + } + + itTypes = outputTypes.find("qfile"); + if (itTypes != outputTypes.end()) { + if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); } + } m->mothurOutEndLine(); m->mothurOut("Output File Names: "); m->mothurOutEndLine();