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::getValidParameters(){
16 string Array[] = {"fastq", "outputdir","inputdir"};
17 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
21 m->errorOut(e, "ParseFastaQCommand", "getValidParameters");
25 //**********************************************************************************************************************
26 ParseFastaQCommand::ParseFastaQCommand(){
28 //initialize outputTypes
29 vector<string> tempOutNames;
30 outputTypes["fasta"] = tempOutNames;
31 outputTypes["qual"] = tempOutNames;
34 m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand");
38 //**********************************************************************************************************************
39 vector<string> ParseFastaQCommand::getRequiredParameters(){
41 string Array[] = {"fastq"};
42 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
46 m->errorOut(e, "ParseFastaQCommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> ParseFastaQCommand::getRequiredFiles(){
53 vector<string> myArray;
57 m->errorOut(e, "ParseFastaQCommand", "getRequiredFiles");
61 //**********************************************************************************************************************
62 ParseFastaQCommand::ParseFastaQCommand(string option){
66 if(option == "help") { help(); abort = true; }
69 //valid paramters for this command
70 string Array[] = {"fastq", "outputdir", "inputdir"};
71 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
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 (map<string,string>::iterator 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["fasta"] = tempOutNames;
87 outputTypes["qual"] = 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("fastq");
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["fastq"] = inputDir + it->second; }
103 //check for required parameters
104 fastaQFile = validParameter.validFile(parameters, "fastq", true);
105 if (fastaQFile == "not found") { m->mothurOut("fastq is a required parameter for the fastq.info command."); m->mothurOutEndLine(); abort = true; }
106 else if (fastaQFile == "not open") { fastaQFile = ""; abort = true; }
108 //if the user changes the output directory command factory will send this info to us in the output parameter
109 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(fastaQFile); }
113 catch(exception& e) {
114 m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand");
118 //**********************************************************************************************************************
120 void ParseFastaQCommand::help(){
122 m->mothurOut("The fastq.info command reads a fastaQ file and creates a fasta and quality file.\n");
123 m->mothurOut("The fastq.info command parameter is fastq, and it is required.\n");
124 m->mothurOut("The fastq.info command should be in the following format: fastq.info(fastaq=yourFastaQFile).\n");
125 m->mothurOut("Example fastq.info(fastaq=test.fastaq).\n");
126 m->mothurOut("Note: No spaces between parameter labels (i.e. fastq), '=' and yourFastQFile.\n");
127 m->mothurOutEndLine();
129 catch(exception& e) {
130 m->errorOut(e, "ParseFastaQCommand", "help");
134 //**********************************************************************************************************************
136 ParseFastaQCommand::~ParseFastaQCommand() { /* do nothing */ }
138 //**********************************************************************************************************************
140 int ParseFastaQCommand::execute(){
142 if (abort == true) { return 0; }
145 string fastaFile = outputDir + m->getRootName(m->getSimpleName(fastaQFile)) + "fasta";
146 string qualFile = outputDir + m->getRootName(m->getSimpleName(fastaQFile)) + "qual";
147 ofstream outFasta, outQual;
148 m->openOutputFile(fastaFile, outFasta); outputNames.push_back(fastaFile); outputTypes["fasta"].push_back(fastaFile);
149 m->openOutputFile(qualFile, outQual); outputNames.push_back(qualFile); outputTypes["qual"].push_back(qualFile);
152 m->openInputFile(fastaQFile, in);
157 string name = m->getline(in); m->gobble(in);
158 if (name == "") { m->mothurOut("[ERROR]: Blank fasta name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
159 else if (name[0] != '@') { m->mothurOut("[ERROR]: reading " + name + " expected a name with @ as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
160 else { name = name.substr(1); }
163 string sequence = m->getline(in); m->gobble(in);
164 if (sequence == "") { m->mothurOut("[ERROR]: missing sequence for " + name); m->mothurOutEndLine(); m->control_pressed = true; break; }
167 string name2 = m->getline(in); m->gobble(in);
168 if (name2 == "") { m->mothurOut("[ERROR]: Blank quality name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
169 else if (name2[0] != '+') { m->mothurOut("[ERROR]: reading " + name2 + " expected a name with + as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
170 else { name2 = name2.substr(1); }
172 //read quality scores
173 string qual = m->getline(in); m->gobble(in);
174 if (qual == "") { m->mothurOut("[ERROR]: missing quality for " + name2); m->mothurOutEndLine(); m->control_pressed = true; break; }
176 //sanity check sequence length and number of quality scores match
177 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; }
178 if (qual.length() != sequence.length()) { m->mothurOut("[ERROR]: lengths do not match. read " + toString(sequence.length()) + " characters for fasta and " + toString(qual.length()) + " characters for quality scores."); m->mothurOutEndLine(); m->control_pressed = true; break; }
180 //convert quality scores
181 vector<int> qualScores = convertQual(qual);
183 //print sequence info to files
184 outFasta << ">" << name << endl << sequence << endl;
186 outQual << ">" << name << endl;
187 for (int i = 0; i < qualScores.size(); i++) { outQual << qualScores[i] << " "; }
195 if (m->control_pressed) { outputTypes.clear(); remove(fastaFile.c_str()); remove(qualFile.c_str()); return 0; }
197 m->mothurOutEndLine();
198 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
199 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
200 m->mothurOutEndLine();
204 catch(exception& e) {
205 m->errorOut(e, "ParseFastaQCommand", "execute");
209 //**********************************************************************************************************************
210 vector<int> ParseFastaQCommand::convertQual(string qual) {
212 vector<int> qualScores;
214 int controlChar = int('!');
216 for (int i = 0; i < qual.length(); i++) {
217 int temp = int(qual[i]);
220 qualScores.push_back(temp);
225 catch(exception& e) {
226 m->errorOut(e, "ParseFastaQCommand", "convertQual");
230 //**********************************************************************************************************************