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::setParameters(){
16 CommandParameter pfastq("fastq", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfastq);
17 CommandParameter pfasta("fasta", "Bool", "", "T", "", "", "",false,false); parameters.push_back(pfasta);
18 CommandParameter pqual("qfile", "Bool", "", "T", "", "", "",false,false); parameters.push_back(pqual);
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, "ParseFastaQCommand", "setParameters");
31 //**********************************************************************************************************************
32 string ParseFastaQCommand::getHelpString(){
34 string helpString = "";
35 helpString += "The fastq.info command reads a fastq file and creates a fasta and quality file.\n";
36 helpString += "The fastq.info command parameters are fastq, fasta and qfile; fastq is required.\n";
37 helpString += "The fastq.info command should be in the following format: fastq.info(fastaq=yourFastaQFile).\n";
38 helpString += "The fasta parameter allows you to indicate whether you want a fasta file generated. Default=T.\n";
39 helpString += "The qfile parameter allows you to indicate whether you want a quality file generated. Default=T.\n";
40 helpString += "Example fastq.info(fastaq=test.fastaq).\n";
41 helpString += "Note: No spaces between parameter labels (i.e. fastq), '=' and yourFastQFile.\n";
45 m->errorOut(e, "ParseFastaQCommand", "getHelpString");
49 //**********************************************************************************************************************
50 ParseFastaQCommand::ParseFastaQCommand(){
52 abort = true; calledHelp = true;
54 vector<string> tempOutNames;
55 outputTypes["fasta"] = tempOutNames;
56 outputTypes["qfile"] = tempOutNames;
59 m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand");
63 //**********************************************************************************************************************
64 ParseFastaQCommand::ParseFastaQCommand(string option){
66 abort = false; calledHelp = false;
68 if(option == "help") { help(); abort = true; calledHelp = true; }
69 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
72 vector<string> myArray = setParameters();
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["qfile"] = 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); }
113 temp = validParameter.validFile(parameters, "fasta", false); if(temp == "not found"){ temp = "T"; }
114 fasta = m->isTrue(temp);
116 temp = validParameter.validFile(parameters, "qfile", false); if(temp == "not found"){ temp = "T"; }
117 qual = m->isTrue(temp);
119 if ((!fasta) && (!qual)) { m->mothurOut("[ERROR]: no outputs selected. Aborting."); m->mothurOutEndLine(); abort=true; }
123 catch(exception& e) {
124 m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand");
128 //**********************************************************************************************************************
130 int ParseFastaQCommand::execute(){
132 if (abort == true) { if (calledHelp) { return 0; } return 2; }
135 string fastaFile = outputDir + m->getRootName(m->getSimpleName(fastaQFile)) + "fasta";
136 string qualFile = outputDir + m->getRootName(m->getSimpleName(fastaQFile)) + "qual";
137 ofstream outFasta, outQual;
139 if (fasta) { m->openOutputFile(fastaFile, outFasta); outputNames.push_back(fastaFile); outputTypes["fasta"].push_back(fastaFile); }
140 if (qual) { m->openOutputFile(qualFile, outQual); outputNames.push_back(qualFile); outputTypes["qfile"].push_back(qualFile); }
143 m->openInputFile(fastaQFile, in);
147 if (m->control_pressed) { break; }
150 string name = m->getline(in); m->gobble(in);
151 if (name == "") { m->mothurOut("[ERROR]: Blank fasta name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
152 else if (name[0] != '@') { m->mothurOut("[ERROR]: reading " + name + " expected a name with @ as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
153 else { name = name.substr(1); }
156 string sequence = m->getline(in); m->gobble(in);
157 if (sequence == "") { m->mothurOut("[ERROR]: missing sequence for " + name); m->mothurOutEndLine(); m->control_pressed = true; break; }
160 string name2 = m->getline(in); m->gobble(in);
161 if (name2 == "") { m->mothurOut("[ERROR]: Blank quality name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
162 else if (name2[0] != '+') { m->mothurOut("[ERROR]: reading " + name2 + " expected a name with + as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
163 else { name2 = name2.substr(1); }
165 //read quality scores
166 string quality = m->getline(in); m->gobble(in);
167 if (quality == "") { m->mothurOut("[ERROR]: missing quality for " + name2); m->mothurOutEndLine(); m->control_pressed = true; break; }
169 //sanity check sequence length and number of quality scores match
170 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; } }
171 if (quality.length() != sequence.length()) { m->mothurOut("[ERROR]: Lengths do not match for sequence " + name + ". Read " + toString(sequence.length()) + " characters for fasta and " + toString(quality.length()) + " characters for quality scores."); m->mothurOutEndLine(); m->control_pressed = true; break; }
173 //print sequence info to files
174 if (fasta) { outFasta << ">" << name << endl << sequence << endl; }
177 vector<int> qualScores = convertQual(quality);
178 outQual << ">" << name << endl;
179 for (int i = 0; i < qualScores.size(); i++) { outQual << qualScores[i] << " "; }
185 if (fasta) { outFasta.close(); }
186 if (qual) { outQual.close(); }
188 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(fastaFile); m->mothurRemove(qualFile); return 0; }
190 //set fasta file as new current fastafile
192 itTypes = outputTypes.find("fasta");
193 if (itTypes != outputTypes.end()) {
194 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
197 itTypes = outputTypes.find("qfile");
198 if (itTypes != outputTypes.end()) {
199 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
202 m->mothurOutEndLine();
203 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
204 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
205 m->mothurOutEndLine();
209 catch(exception& e) {
210 m->errorOut(e, "ParseFastaQCommand", "execute");
214 //**********************************************************************************************************************
215 vector<int> ParseFastaQCommand::convertQual(string qual) {
217 vector<int> qualScores;
219 int controlChar = int('!');
221 for (int i = 0; i < qual.length(); i++) {
222 int temp = int(qual[i]);
225 qualScores.push_back(temp);
230 catch(exception& e) {
231 m->errorOut(e, "ParseFastaQCommand", "convertQual");
235 //**********************************************************************************************************************