2 * parsefastaqcommand.cpp
5 * Created by westcott on 9/30/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "parsefastaqcommand.h"
11 #include "sequence.hpp"
13 //**********************************************************************************************************************
14 vector<string> ParseFastaQCommand::setParameters(){
16 CommandParameter pfastq("fastq", "InputTypes", "", "", "none", "none", "none","",false,true,true); parameters.push_back(pfastq);
17 CommandParameter pfasta("fasta", "Boolean", "", "T", "", "", "","fasta",false,false); parameters.push_back(pfasta);
18 CommandParameter pqual("qfile", "Boolean", "", "T", "", "", "","qfile",false,false); parameters.push_back(pqual);
19 CommandParameter pformat("format", "Multiple", "sanger-illumina-solexa", "sanger", "", "", "","",false,false,true); parameters.push_back(pformat);
20 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
21 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
23 vector<string> myArray;
24 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
28 m->errorOut(e, "ParseFastaQCommand", "setParameters");
32 //**********************************************************************************************************************
33 string ParseFastaQCommand::getHelpString(){
35 string helpString = "";
36 helpString += "The fastq.info command reads a fastq file and creates a fasta and quality file.\n";
37 helpString += "The fastq.info command parameters are fastq, fasta, qfile and format; fastq is required.\n";
38 helpString += "The fastq.info command should be in the following format: fastq.info(fastaq=yourFastaQFile).\n";
39 helpString += "The format parameter is used to indicate whether your sequences are sanger, solexa or illumina, default=sanger.\n";
40 helpString += "The fasta parameter allows you to indicate whether you want a fasta file generated. Default=T.\n";
41 helpString += "The qfile parameter allows you to indicate whether you want a quality file generated. Default=T.\n";
42 helpString += "Example fastq.info(fastaq=test.fastaq).\n";
43 helpString += "Note: No spaces between parameter labels (i.e. fastq), '=' and yourFastQFile.\n";
47 m->errorOut(e, "ParseFastaQCommand", "getHelpString");
51 //**********************************************************************************************************************
52 string ParseFastaQCommand::getOutputPattern(string type) {
56 if (type == "fasta") { pattern = "[filename],fasta"; }
57 else if (type == "qfile") { pattern = "[filename],qual"; }
58 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
63 m->errorOut(e, "ParseFastaQCommand", "getOutputPattern");
67 //**********************************************************************************************************************
68 ParseFastaQCommand::ParseFastaQCommand(){
70 abort = true; calledHelp = true;
72 vector<string> tempOutNames;
73 outputTypes["fasta"] = tempOutNames;
74 outputTypes["qfile"] = tempOutNames;
77 m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand");
81 //**********************************************************************************************************************
82 ParseFastaQCommand::ParseFastaQCommand(string option){
84 abort = false; calledHelp = false;
86 if(option == "help") { help(); abort = true; calledHelp = true; }
87 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
90 vector<string> myArray = setParameters();
92 OptionParser parser(option);
93 map<string,string> parameters = parser.getParameters();
95 ValidParameters validParameter;
96 map<string,string>::iterator it;
98 //check to make sure all parameters are valid for command
99 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
100 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
103 //initialize outputTypes
104 vector<string> tempOutNames;
105 outputTypes["fasta"] = tempOutNames;
106 outputTypes["qfile"] = tempOutNames;
108 //if the user changes the input directory command factory will send this info to us in the output parameter
109 string inputDir = validParameter.validFile(parameters, "inputdir", false);
110 if (inputDir == "not found"){ inputDir = ""; }
113 it = parameters.find("fastq");
114 //user has given a template file
115 if(it != parameters.end()){
116 path = m->hasPath(it->second);
117 //if the user has not given a path then, add inputdir. else leave path alone.
118 if (path == "") { parameters["fastq"] = inputDir + it->second; }
122 //check for required parameters
123 fastaQFile = validParameter.validFile(parameters, "fastq", true);
124 if (fastaQFile == "not found") { m->mothurOut("fastq is a required parameter for the fastq.info command."); m->mothurOutEndLine(); abort = true; }
125 else if (fastaQFile == "not open") { fastaQFile = ""; abort = true; }
127 //if the user changes the output directory command factory will send this info to us in the output parameter
128 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(fastaQFile); }
131 temp = validParameter.validFile(parameters, "fasta", false); if(temp == "not found"){ temp = "T"; }
132 fasta = m->isTrue(temp);
134 temp = validParameter.validFile(parameters, "qfile", false); if(temp == "not found"){ temp = "T"; }
135 qual = m->isTrue(temp);
137 format = validParameter.validFile(parameters, "format", false); if (format == "not found"){ format = "sanger"; }
139 if ((format != "sanger") && (format != "illumina") && (format != "solexa")) {
140 m->mothurOut(format + " is not a valid format. Your format choices are sanger, solexa and illumina, aborting." ); m->mothurOutEndLine();
144 if ((!fasta) && (!qual)) { m->mothurOut("[ERROR]: no outputs selected. Aborting."); m->mothurOutEndLine(); abort=true; }
148 catch(exception& e) {
149 m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand");
153 //**********************************************************************************************************************
155 int ParseFastaQCommand::execute(){
157 if (abort == true) { if (calledHelp) { return 0; } return 2; }
160 map<string, string> variables;
161 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastaQFile));
162 string fastaFile = getOutputFileName("fasta",variables);
163 string qualFile = getOutputFileName("qfile",variables);
164 ofstream outFasta, outQual;
166 if (fasta) { m->openOutputFile(fastaFile, outFasta); outputNames.push_back(fastaFile); outputTypes["fasta"].push_back(fastaFile); }
167 if (qual) { m->openOutputFile(qualFile, outQual); outputNames.push_back(qualFile); outputTypes["qfile"].push_back(qualFile); }
170 m->openInputFile(fastaQFile, in);
172 //fill convert table - goes from solexa to sanger. Used fq_all2std.pl as a reference.
173 for (int i = -64; i < 65; i++) {
174 char temp = (char) ((int)(33 + 10*log(1+pow(10,(i/10.0)))/log(10)+0.499));
175 convertTable.push_back(temp);
180 if (m->control_pressed) { break; }
183 string name = m->getline(in); m->gobble(in);
184 if (name == "") { m->mothurOut("[ERROR]: Blank fasta name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
185 else if (name[0] != '@') { m->mothurOut("[ERROR]: reading " + name + " expected a name with @ as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
186 else { name = name.substr(1); }
189 string sequence = m->getline(in); m->gobble(in);
190 if (sequence == "") { m->mothurOut("[ERROR]: missing sequence for " + name); m->mothurOutEndLine(); m->control_pressed = true; break; }
193 string name2 = m->getline(in); m->gobble(in);
194 if (name2 == "") { m->mothurOut("[ERROR]: Blank quality name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
195 else if (name2[0] != '+') { m->mothurOut("[ERROR]: reading " + name2 + " expected a name with + as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
196 else { name2 = name2.substr(1); }
198 //read quality scores
199 string quality = m->getline(in); m->gobble(in);
200 if (quality == "") { m->mothurOut("[ERROR]: missing quality for " + name2); m->mothurOutEndLine(); m->control_pressed = true; break; }
202 //sanity check sequence length and number of quality scores match
203 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; } }
204 if (quality.length() != sequence.length()) { m->mothurOut("[ERROR]: Lengths do not match for sequence " + name + ". Read " + toString(sequence.length()) + " characters for fasta and " + toString(quality.length()) + " characters for quality scores."); m->mothurOutEndLine(); m->control_pressed = true; break; }
206 //print sequence info to files
207 if (fasta) { outFasta << ">" << name << endl << sequence << endl; }
210 vector<int> qualScores = convertQual(quality);
211 outQual << ">" << name << endl;
212 for (int i = 0; i < qualScores.size(); i++) { outQual << qualScores[i] << " "; }
218 if (fasta) { outFasta.close(); }
219 if (qual) { outQual.close(); }
221 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(fastaFile); m->mothurRemove(qualFile); return 0; }
223 //set fasta file as new current fastafile
225 itTypes = outputTypes.find("fasta");
226 if (itTypes != outputTypes.end()) {
227 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
230 itTypes = outputTypes.find("qfile");
231 if (itTypes != outputTypes.end()) {
232 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
235 m->mothurOutEndLine();
236 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
237 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
238 m->mothurOutEndLine();
242 catch(exception& e) {
243 m->errorOut(e, "ParseFastaQCommand", "execute");
247 //**********************************************************************************************************************
248 vector<int> ParseFastaQCommand::convertQual(string qual) {
250 vector<int> qualScores;
252 for (int i = 0; i < qual.length(); i++) {
256 if (format == "illumina") {
257 temp -= 64; //char '@'
258 }else if (format == "solexa") {
259 temp = int(convertTable[temp]); //convert to sanger
260 temp -= int('!'); //char '!'
262 temp -= int('!'); //char '!'
264 qualScores.push_back(temp);
269 catch(exception& e) {
270 m->errorOut(e, "ParseFastaQCommand", "convertQual");
274 //**********************************************************************************************************************