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(){
29 //initialize outputTypes
30 vector<string> tempOutNames;
31 outputTypes["fasta"] = tempOutNames;
32 outputTypes["qual"] = tempOutNames;
35 m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand");
39 //**********************************************************************************************************************
40 vector<string> ParseFastaQCommand::getRequiredParameters(){
42 string Array[] = {"fastq"};
43 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
47 m->errorOut(e, "ParseFastaQCommand", "getRequiredParameters");
51 //**********************************************************************************************************************
52 vector<string> ParseFastaQCommand::getRequiredFiles(){
54 vector<string> myArray;
58 m->errorOut(e, "ParseFastaQCommand", "getRequiredFiles");
62 //**********************************************************************************************************************
63 ParseFastaQCommand::ParseFastaQCommand(string option){
67 if(option == "help") { help(); abort = true; }
70 //valid paramters for this command
71 string Array[] = {"fastq", "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 (map<string,string>::iterator 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["fasta"] = tempOutNames;
88 outputTypes["qual"] = 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("fastq");
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["fastq"] = inputDir + it->second; }
104 //check for required parameters
105 fastaQFile = validParameter.validFile(parameters, "fastq", true);
106 if (fastaQFile == "not found") { m->mothurOut("fastq is a required parameter for the fastq.info command."); m->mothurOutEndLine(); abort = true; }
107 else if (fastaQFile == "not open") { fastaQFile = ""; abort = true; }
109 //if the user changes the output directory command factory will send this info to us in the output parameter
110 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(fastaQFile); }
114 catch(exception& e) {
115 m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand");
119 //**********************************************************************************************************************
121 void ParseFastaQCommand::help(){
123 m->mothurOut("The fastq.info command reads a fastq file and creates a fasta and quality file.\n");
124 m->mothurOut("The fastq.info command parameter is fastq, and it is required.\n");
125 m->mothurOut("The fastq.info command should be in the following format: fastq.info(fastaq=yourFastaQFile).\n");
126 m->mothurOut("Example fastq.info(fastaq=test.fastaq).\n");
127 m->mothurOut("Note: No spaces between parameter labels (i.e. fastq), '=' and yourFastQFile.\n");
128 m->mothurOutEndLine();
130 catch(exception& e) {
131 m->errorOut(e, "ParseFastaQCommand", "help");
135 //**********************************************************************************************************************
137 ParseFastaQCommand::~ParseFastaQCommand() { /* do nothing */ }
139 //**********************************************************************************************************************
141 int ParseFastaQCommand::execute(){
143 if (abort == true) { return 0; }
146 string fastaFile = outputDir + m->getRootName(m->getSimpleName(fastaQFile)) + "fasta";
147 string qualFile = outputDir + m->getRootName(m->getSimpleName(fastaQFile)) + "qual";
148 ofstream outFasta, outQual;
149 m->openOutputFile(fastaFile, outFasta); outputNames.push_back(fastaFile); outputTypes["fasta"].push_back(fastaFile);
150 m->openOutputFile(qualFile, outQual); outputNames.push_back(qualFile); outputTypes["qual"].push_back(qualFile);
153 m->openInputFile(fastaQFile, in);
158 string name = m->getline(in); m->gobble(in);
159 if (name == "") { m->mothurOut("[ERROR]: Blank fasta name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
160 else if (name[0] != '@') { m->mothurOut("[ERROR]: reading " + name + " expected a name with @ as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
161 else { name = name.substr(1); }
164 string sequence = m->getline(in); m->gobble(in);
165 if (sequence == "") { m->mothurOut("[ERROR]: missing sequence for " + name); m->mothurOutEndLine(); m->control_pressed = true; break; }
168 string name2 = m->getline(in); m->gobble(in);
169 if (name2 == "") { m->mothurOut("[ERROR]: Blank quality name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
170 else if (name2[0] != '+') { m->mothurOut("[ERROR]: reading " + name2 + " expected a name with + as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
171 else { name2 = name2.substr(1); }
173 //read quality scores
174 string qual = m->getline(in); m->gobble(in);
175 if (qual == "") { m->mothurOut("[ERROR]: missing quality for " + name2); m->mothurOutEndLine(); m->control_pressed = true; break; }
177 //sanity check sequence length and number of quality scores match
178 if (name2 != "") { 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; } }
179 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; }
181 //convert quality scores
182 vector<int> qualScores = convertQual(qual);
184 //print sequence info to files
185 outFasta << ">" << name << endl << sequence << endl;
187 outQual << ">" << name << endl;
188 for (int i = 0; i < qualScores.size(); i++) { outQual << qualScores[i] << " "; }
196 if (m->control_pressed) { outputTypes.clear(); remove(fastaFile.c_str()); remove(qualFile.c_str()); return 0; }
198 m->mothurOutEndLine();
199 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
200 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
201 m->mothurOutEndLine();
205 catch(exception& e) {
206 m->errorOut(e, "ParseFastaQCommand", "execute");
210 //**********************************************************************************************************************
211 vector<int> ParseFastaQCommand::convertQual(string qual) {
213 vector<int> qualScores;
215 int controlChar = int('!');
217 for (int i = 0; i < qual.length(); i++) {
218 int temp = int(qual[i]);
221 qualScores.push_back(temp);
226 catch(exception& e) {
227 m->errorOut(e, "ParseFastaQCommand", "convertQual");
231 //**********************************************************************************************************************