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",false,true); parameters.push_back(pfasta);
18 CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pqfile);
19 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
20 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
22 vector<string> myArray;
23 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
27 m->errorOut(e, "MakeFastQCommand", "setParameters");
31 //**********************************************************************************************************************
32 string MakeFastQCommand::getHelpString(){
34 string helpString = "";
35 helpString += "The make.fastq command read a fasta and quality file and creates a fastq file.\n";
36 helpString += "The make.fastq command parameters are fasta and qfile, both are required.\n";
37 helpString += "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";
38 helpString += "The make.fastq command should be in the following format: make.fastq(qfile=yourQualityFile, fasta=yourFasta).\n";
39 helpString += "Example make.fastq(fasta=amazon.fasta, qfile=amazon.qual).\n";
40 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
44 m->errorOut(e, "MakeFastQCommand", "getHelpString");
48 //**********************************************************************************************************************
49 MakeFastQCommand::MakeFastQCommand(){
51 abort = true; calledHelp = true;
53 vector<string> tempOutNames;
54 outputTypes["fastq"] = tempOutNames;
57 m->errorOut(e, "MakeFastQCommand", "MakeFastQCommand");
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; }
68 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
71 vector<string> myArray = setParameters();
73 OptionParser parser(option);
74 map<string,string> parameters = parser.getParameters();
76 ValidParameters validParameter;
77 map<string,string>::iterator it;
79 //check to make sure all parameters are valid for command
80 for (it = parameters.begin(); it != parameters.end(); it++) {
81 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
84 //initialize outputTypes
85 vector<string> tempOutNames;
86 outputTypes["fastq"] = tempOutNames;
89 //if the user changes the input directory command factory will send this info to us in the output parameter
90 string inputDir = validParameter.validFile(parameters, "inputdir", false);
91 if (inputDir == "not found"){ inputDir = ""; }
94 it = parameters.find("fasta");
95 //user has given a template file
96 if(it != parameters.end()){
97 path = m->hasPath(it->second);
98 //if the user has not given a path then, add inputdir. else leave path alone.
99 if (path == "") { parameters["fasta"] = inputDir + it->second; }
102 it = parameters.find("qfile");
103 //user has given a template file
104 if(it != parameters.end()){
105 path = m->hasPath(it->second);
106 //if the user has not given a path then, add inputdir. else leave path alone.
107 if (path == "") { parameters["qfile"] = inputDir + it->second; }
112 //check for required parameters
113 fastafile = validParameter.validFile(parameters, "fasta", true);
114 if (fastafile == "not open") { abort = true; fastafile = ""; }
115 else if (fastafile == "not found") {
116 fastafile = m->getFastaFile();
117 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
118 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
121 qualfile = validParameter.validFile(parameters, "qfile", true);
122 if (qualfile == "not open") { abort = true; qualfile = ""; }
123 else if (qualfile == "not found") {
124 qualfile = m->getQualFile();
125 if (qualfile != "") { m->mothurOut("Using " + qualfile + " as input file for the qfile parameter."); m->mothurOutEndLine(); }
126 else { m->mothurOut("You have no current qualfile and the qfile parameter is required."); m->mothurOutEndLine(); abort = true; }
129 //if the user changes the output directory command factory will send this info to us in the output parameter
130 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(fastafile); }
135 catch(exception& e) {
136 m->errorOut(e, "MakeFastQCommand", "MakeFastQCommand");
140 //**********************************************************************************************************************
142 int MakeFastQCommand::execute(){
145 if (abort == true) { if (calledHelp) { return 0; } return 2; }
148 string outputFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "fastq";
149 outputNames.push_back(outputFile); outputTypes["fastq"].push_back(outputFile);
152 m->openOutputFile(outputFile, out);
155 m->openInputFile(qualfile, qFile);
158 m->openInputFile(fastafile, fFile);
160 while (!fFile.eof() && !qFile.eof()) {
162 if (m->control_pressed) { break; }
164 Sequence currSeq(fFile); m->gobble(fFile);
165 QualityScores currQual(qFile); m->gobble(qFile);
167 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; }
170 out << '@' << currSeq.getName() << endl << currSeq.getAligned() << endl;
172 string qualityString = convertQual(currQual.getQualityScores());
175 out << '+' << currQual.getName() << endl << qualityString << endl;
184 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
186 m->mothurOutEndLine();
187 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
188 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
189 m->mothurOutEndLine();
194 catch(exception& e) {
195 m->errorOut(e, "MakeFastQCommand", "execute");
199 //**********************************************************************************************************************
200 string MakeFastQCommand::convertQual(vector<int> qual) {
204 int controlChar = int('!');
206 for (int i = 0; i < qual.size(); i++) {
207 int temp = qual[i] + controlChar;
208 char qualChar = (char) temp;
210 qualScores += qualChar;
215 catch(exception& e) {
216 m->errorOut(e, "MakeFastQCommand", "convertQual");
220 //**********************************************************************************************************************