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::getValidParameters(){
17 string Array[] = {"fasta","qfile","outputdir","inputdir" };
18 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
22 m->errorOut(e, "MakeFastQCommand", "getValidParameters");
26 //**********************************************************************************************************************
27 MakeFastQCommand::MakeFastQCommand(){
29 abort = true; calledHelp = true;
30 vector<string> tempOutNames;
31 outputTypes["fastq"] = tempOutNames;
34 m->errorOut(e, "MakeFastQCommand", "MakeFastQCommand");
38 //**********************************************************************************************************************
39 vector<string> MakeFastQCommand::getRequiredParameters(){
41 string Array[] = {"fasta","qfile"};
42 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
46 m->errorOut(e, "MakeFastQCommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> MakeFastQCommand::getRequiredFiles(){
53 vector<string> myArray;
57 m->errorOut(e, "MakeFastQCommand", "getRequiredFiles");
61 //**********************************************************************************************************************
62 MakeFastQCommand::MakeFastQCommand(string option) {
64 abort = false; calledHelp = false;
66 //allow user to run help
67 if(option == "help") { help(); abort = true; calledHelp = true; }
70 //valid paramters for this command
71 string Array[] = {"fasta","qfile", "outputdir","inputdir" };
72 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
74 OptionParser parser(option);
75 map<string,string> parameters = parser.getParameters();
77 ValidParameters validParameter;
78 map<string,string>::iterator it;
80 //check to make sure all parameters are valid for command
81 for (it = parameters.begin(); it != parameters.end(); it++) {
82 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
85 //initialize outputTypes
86 vector<string> tempOutNames;
87 outputTypes["fastq"] = tempOutNames;
90 //if the user changes the input directory command factory will send this info to us in the output parameter
91 string inputDir = validParameter.validFile(parameters, "inputdir", false);
92 if (inputDir == "not found"){ inputDir = ""; }
95 it = parameters.find("fasta");
96 //user has given a template file
97 if(it != parameters.end()){
98 path = m->hasPath(it->second);
99 //if the user has not given a path then, add inputdir. else leave path alone.
100 if (path == "") { parameters["fasta"] = inputDir + it->second; }
103 it = parameters.find("qfile");
104 //user has given a template file
105 if(it != parameters.end()){
106 path = m->hasPath(it->second);
107 //if the user has not given a path then, add inputdir. else leave path alone.
108 if (path == "") { parameters["qfile"] = inputDir + it->second; }
111 it = parameters.find("list");
112 //user has given a template file
113 if(it != parameters.end()){
114 path = m->hasPath(it->second);
115 //if the user has not given a path then, add inputdir. else leave path alone.
116 if (path == "") { parameters["list"] = inputDir + it->second; }
121 //check for required parameters
122 fastafile = validParameter.validFile(parameters, "fasta", true);
123 if (fastafile == "not open") { abort = true; fastafile = ""; }
124 else if (fastafile == "not found") { fastafile = ""; m->mothurOut("You must provide a fasta file."); m->mothurOutEndLine(); abort = true; }
126 qualfile = validParameter.validFile(parameters, "qfile", true);
127 if (qualfile == "not open") { abort = true; qualfile = ""; }
128 else if (qualfile == "not found") { qualfile = ""; m->mothurOut("You must provide a quality file."); m->mothurOutEndLine(); abort = true; }
130 //if the user changes the output directory command factory will send this info to us in the output parameter
131 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(fastafile); }
136 catch(exception& e) {
137 m->errorOut(e, "MakeFastQCommand", "MakeFastQCommand");
141 //**********************************************************************************************************************
143 void MakeFastQCommand::help(){
145 m->mothurOut("The make.fastq command read a fasta and quality file and creates a fastq file.\n");
146 m->mothurOut("The make.fastq command parameters are fasta and qfile, both are required.\n");
147 m->mothurOut("You must also provide an accnos containing the list of groups to get or set the groups parameter to the groups you wish to select.\n");
148 m->mothurOut("The make.fastq command should be in the following format: make.fastq(qfile=yourQualityFile, fasta=yourFasta).\n");
149 m->mothurOut("Example make.fastq(fasta=amazon.fasta, qfile=amazon.qual).\n");
150 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
152 catch(exception& e) {
153 m->errorOut(e, "MakeFastQCommand", "help");
158 //**********************************************************************************************************************
160 int MakeFastQCommand::execute(){
163 if (abort == true) { if (calledHelp) { return 0; } return 2; }
166 string outputFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "fastq";
167 outputNames.push_back(outputFile); outputTypes["fastq"].push_back(outputFile);
170 m->openOutputFile(outputFile, out);
173 m->openInputFile(qualfile, qFile);
176 m->openInputFile(fastafile, fFile);
178 while (!fFile.eof() && !qFile.eof()) {
180 if (m->control_pressed) { break; }
182 Sequence currSeq(fFile); m->gobble(fFile);
183 QualityScores currQual(qFile); m->gobble(qFile);
185 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; }
188 out << '@' << currSeq.getName() << endl << currSeq.getAligned() << endl;
190 string qualityString = convertQual(currQual.getQualityScores());
193 out << '+' << currQual.getName() << endl << qualityString << endl;
202 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
204 m->mothurOutEndLine();
205 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
206 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
207 m->mothurOutEndLine();
212 catch(exception& e) {
213 m->errorOut(e, "MakeFastQCommand", "execute");
217 //**********************************************************************************************************************
218 string MakeFastQCommand::convertQual(vector<int> qual) {
222 int controlChar = int('!');
224 for (int i = 0; i < qual.size(); i++) {
225 int temp = qual[i] + controlChar;
226 char qualChar = (char) temp;
228 qualScores += qualChar;
233 catch(exception& e) {
234 m->errorOut(e, "MakeFastQCommand", "convertQual");
238 //**********************************************************************************************************************