5 * Created by westcott on 2/14/11.
6 * Copyright 2011 Schloss Lab. All rights reserved.
10 #include "makefastqcommand.h"
11 #include "sequence.hpp"
12 #include "qualityscores.h"
14 //**********************************************************************************************************************
15 vector<string> MakeFastQCommand::setParameters(){
17 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none","fastq",false,true,true); parameters.push_back(pfasta);
18 CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "none", "none","fastq",false,true,true); parameters.push_back(pqfile);
19 CommandParameter pformat("format", "Multiple", "sanger-illumina-illumina1.8+", "sanger", "", "", "","",false,false); 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, "MakeFastQCommand", "setParameters");
32 //**********************************************************************************************************************
33 string MakeFastQCommand::getHelpString(){
35 string helpString = "";
36 helpString += "The make.fastq command reads a fasta and quality file and creates a fastq file.\n";
37 helpString += "The make.fastq command parameters are fasta, qfile and format. fasta and qfile are required.\n";
38 helpString += "The format parameter is used to indicate whether your sequences are sanger, illumina1.8+ or illumina, default=sanger.\n";
39 helpString += "The make.fastq command should be in the following format: make.fastq(qfile=yourQualityFile, fasta=yourFasta).\n";
40 helpString += "Example make.fastq(fasta=amazon.fasta, qfile=amazon.qual).\n";
41 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
45 m->errorOut(e, "MakeFastQCommand", "getHelpString");
49 //**********************************************************************************************************************
50 string MakeFastQCommand::getOutputPattern(string type) {
54 if (type == "fastq") { pattern = "[filename],fastq"; }
55 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
60 m->errorOut(e, "MakeFastQCommand", "getOutputPattern");
64 //**********************************************************************************************************************
65 MakeFastQCommand::MakeFastQCommand(){
67 abort = true; calledHelp = true;
69 vector<string> tempOutNames;
70 outputTypes["fastq"] = tempOutNames;
73 m->errorOut(e, "MakeFastQCommand", "MakeFastQCommand");
77 //**********************************************************************************************************************
78 MakeFastQCommand::MakeFastQCommand(string option) {
80 abort = false; calledHelp = false;
82 //allow user to run help
83 if(option == "help") { help(); abort = true; calledHelp = true; }
84 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
87 vector<string> myArray = setParameters();
89 OptionParser parser(option);
90 map<string,string> parameters = parser.getParameters();
92 ValidParameters validParameter;
93 map<string,string>::iterator it;
95 //check to make sure all parameters are valid for command
96 for (it = parameters.begin(); it != parameters.end(); it++) {
97 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
100 //initialize outputTypes
101 vector<string> tempOutNames;
102 outputTypes["fastq"] = tempOutNames;
105 //if the user changes the input directory command factory will send this info to us in the output parameter
106 string inputDir = validParameter.validFile(parameters, "inputdir", false);
107 if (inputDir == "not found"){ inputDir = ""; }
110 it = parameters.find("fasta");
111 //user has given a template file
112 if(it != parameters.end()){
113 path = m->hasPath(it->second);
114 //if the user has not given a path then, add inputdir. else leave path alone.
115 if (path == "") { parameters["fasta"] = inputDir + it->second; }
118 it = parameters.find("qfile");
119 //user has given a template file
120 if(it != parameters.end()){
121 path = m->hasPath(it->second);
122 //if the user has not given a path then, add inputdir. else leave path alone.
123 if (path == "") { parameters["qfile"] = inputDir + it->second; }
128 //check for required parameters
129 fastafile = validParameter.validFile(parameters, "fasta", true);
130 if (fastafile == "not open") { abort = true; fastafile = ""; }
131 else if (fastafile == "not found") {
132 fastafile = m->getFastaFile();
133 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
134 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
135 }else { m->setFastaFile(fastafile); }
137 qualfile = validParameter.validFile(parameters, "qfile", true);
138 if (qualfile == "not open") { abort = true; qualfile = ""; }
139 else if (qualfile == "not found") {
140 qualfile = m->getQualFile();
141 if (qualfile != "") { m->mothurOut("Using " + qualfile + " as input file for the qfile parameter."); m->mothurOutEndLine(); }
142 else { m->mothurOut("You have no current qualfile and the qfile parameter is required."); m->mothurOutEndLine(); abort = true; }
143 }else { m->setQualFile(qualfile); }
145 //if the user changes the output directory command factory will send this info to us in the output parameter
146 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(fastafile); }
148 format = validParameter.validFile(parameters, "format", false); if (format == "not found"){ format = "sanger"; }
150 if ((format != "sanger") && (format != "illumina") && (format != "illumina1.8+")) {
151 m->mothurOut(format + " is not a valid format. Your format choices are sanger, illumina1.8+ and illumina, aborting." ); m->mothurOutEndLine();
159 catch(exception& e) {
160 m->errorOut(e, "MakeFastQCommand", "MakeFastQCommand");
164 //**********************************************************************************************************************
166 int MakeFastQCommand::execute(){
169 if (abort == true) { if (calledHelp) { return 0; } return 2; }
171 map<string, string> variables;
172 variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastafile));
173 string outputFile = getOutputFileName("fastq",variables);
174 outputNames.push_back(outputFile); outputTypes["fastq"].push_back(outputFile);
177 m->openOutputFile(outputFile, out);
180 m->openInputFile(qualfile, qFile);
183 m->openInputFile(fastafile, fFile);
185 while (!fFile.eof() && !qFile.eof()) {
187 if (m->control_pressed) { break; }
189 Sequence currSeq(fFile); m->gobble(fFile);
190 QualityScores currQual(qFile); m->gobble(qFile);
192 if (currSeq.getName() != currQual.getName()) { m->mothurOut("[ERROR]: mismatch between fasta and quality files. Found " + currSeq.getName() + " in fasta file and " + currQual.getName() + " in quality file."); m->mothurOutEndLine(); m->control_pressed = true; }
195 out << '@' << currSeq.getName() << endl << currSeq.getAligned() << endl;
197 string qualityString = convertQual(currQual.getQualityScores());
200 out << '+' << currQual.getName() << endl << qualityString << endl;
209 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { m->mothurRemove(outputNames[i]); } return 0; }
211 m->mothurOutEndLine();
212 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
213 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
214 m->mothurOutEndLine();
219 catch(exception& e) {
220 m->errorOut(e, "MakeFastQCommand", "execute");
224 //**********************************************************************************************************************
225 string MakeFastQCommand::convertQual(vector<int> qual) {
229 for (int i = 0; i < qual.size(); i++) {
230 int controlChar = int('!');
231 if (format == "illumina") { controlChar = int('@'); }
233 int temp = qual[i] + controlChar;
234 char qualChar = (char) temp;
236 qualScores += qualChar;
241 catch(exception& e) {
242 m->errorOut(e, "MakeFastQCommand", "convertQual");
246 //**********************************************************************************************************************