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 get.groups command selects sequences from a specfic group or set of groups from the following file types: fasta, name, group, list, taxonomy.\n");
146 m->mothurOut("It outputs a file containing the sequences in the those specified groups.\n");
147 m->mothurOut("The get.groups command parameters are accnos, fasta, name, group, list, taxonomy and groups. The group parameter is required.\n");
148 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");
149 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like. You can separate group names with dashes.\n");
150 m->mothurOut("The get.groups command should be in the following format: get.groups(accnos=yourAccnos, fasta=yourFasta, group=yourGroupFile).\n");
151 m->mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta, group=amazon.groups).\n");
152 m->mothurOut("or get.seqs(groups=pasture, fasta=amazon.fasta, amazon.groups).\n");
153 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");*/
155 catch(exception& e) {
156 m->errorOut(e, "MakeFastQCommand", "help");
161 //**********************************************************************************************************************
163 int MakeFastQCommand::execute(){
166 if (abort == true) { if (calledHelp) { return 0; } return 2; }
169 string outputFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "fastq";
170 outputNames.push_back(outputFile); outputTypes["fastq"].push_back(outputFile);
173 m->openOutputFile(outputFile, out);
176 m->openInputFile(qualfile, qFile);
179 m->openInputFile(fastafile, fFile);
181 while (!fFile.eof() && !qFile.eof()) {
183 if (m->control_pressed) { break; }
185 Sequence currSeq(fFile); m->gobble(fFile);
186 QualityScores currQual(qFile); m->gobble(qFile);
188 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; }
191 out << '@' << currSeq.getName() << endl << currSeq.getAligned() << endl;
193 string qualityString = convertQual(currQual.getQualityScores());
196 out << '+' << currQual.getName() << endl << qualityString << endl;
205 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); } return 0; }
207 m->mothurOutEndLine();
208 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
209 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
210 m->mothurOutEndLine();
215 catch(exception& e) {
216 m->errorOut(e, "MakeFastQCommand", "execute");
220 //**********************************************************************************************************************
221 string MakeFastQCommand::convertQual(vector<int> qual) {
225 int controlChar = int('!');
227 for (int i = 0; i < qual.size(); i++) {
228 int temp = qual[i] + controlChar;
229 char qualChar = (char) temp;
231 qualScores += qualChar;
236 catch(exception& e) {
237 m->errorOut(e, "MakeFastQCommand", "convertQual");
241 //**********************************************************************************************************************