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 pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
18 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
20 vector<string> myArray;
21 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
25 m->errorOut(e, "ParseFastaQCommand", "setParameters");
29 //**********************************************************************************************************************
30 string ParseFastaQCommand::getHelpString(){
32 string helpString = "";
33 helpString += "The fastq.info command reads a fastq file and creates a fasta and quality file.\n";
34 helpString += "The fastq.info command parameter is fastq, and it is required.\n";
35 helpString += "The fastq.info command should be in the following format: fastq.info(fastaq=yourFastaQFile).\n";
36 helpString += "Example fastq.info(fastaq=test.fastaq).\n";
37 helpString += "Note: No spaces between parameter labels (i.e. fastq), '=' and yourFastQFile.\n";
41 m->errorOut(e, "ParseFastaQCommand", "getHelpString");
45 //**********************************************************************************************************************
46 ParseFastaQCommand::ParseFastaQCommand(){
48 abort = true; calledHelp = true;
50 vector<string> tempOutNames;
51 outputTypes["fasta"] = tempOutNames;
52 outputTypes["qfile"] = tempOutNames;
55 m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand");
59 //**********************************************************************************************************************
60 ParseFastaQCommand::ParseFastaQCommand(string option){
62 abort = false; calledHelp = false;
64 if(option == "help") { help(); abort = true; calledHelp = true; }
67 vector<string> myArray = setParameters();
69 OptionParser parser(option);
70 map<string,string> parameters = parser.getParameters();
72 ValidParameters validParameter;
73 map<string,string>::iterator it;
75 //check to make sure all parameters are valid for command
76 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
77 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
80 //initialize outputTypes
81 vector<string> tempOutNames;
82 outputTypes["fasta"] = tempOutNames;
83 outputTypes["qfile"] = tempOutNames;
85 //if the user changes the input directory command factory will send this info to us in the output parameter
86 string inputDir = validParameter.validFile(parameters, "inputdir", false);
87 if (inputDir == "not found"){ inputDir = ""; }
90 it = parameters.find("fastq");
91 //user has given a template file
92 if(it != parameters.end()){
93 path = m->hasPath(it->second);
94 //if the user has not given a path then, add inputdir. else leave path alone.
95 if (path == "") { parameters["fastq"] = inputDir + it->second; }
99 //check for required parameters
100 fastaQFile = validParameter.validFile(parameters, "fastq", true);
101 if (fastaQFile == "not found") { m->mothurOut("fastq is a required parameter for the fastq.info command."); m->mothurOutEndLine(); abort = true; }
102 else if (fastaQFile == "not open") { fastaQFile = ""; abort = true; }
104 //if the user changes the output directory command factory will send this info to us in the output parameter
105 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(fastaQFile); }
109 catch(exception& e) {
110 m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand");
114 //**********************************************************************************************************************
116 int ParseFastaQCommand::execute(){
118 if (abort == true) { if (calledHelp) { return 0; } return 2; }
121 string fastaFile = outputDir + m->getRootName(m->getSimpleName(fastaQFile)) + "fasta";
122 string qualFile = outputDir + m->getRootName(m->getSimpleName(fastaQFile)) + "qual";
123 ofstream outFasta, outQual;
124 m->openOutputFile(fastaFile, outFasta); outputNames.push_back(fastaFile); outputTypes["fasta"].push_back(fastaFile);
125 m->openOutputFile(qualFile, outQual); outputNames.push_back(qualFile); outputTypes["qfile"].push_back(qualFile);
128 m->openInputFile(fastaQFile, in);
133 string name = m->getline(in); m->gobble(in);
134 if (name == "") { m->mothurOut("[ERROR]: Blank fasta name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
135 else if (name[0] != '@') { m->mothurOut("[ERROR]: reading " + name + " expected a name with @ as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
136 else { name = name.substr(1); }
139 string sequence = m->getline(in); m->gobble(in);
140 if (sequence == "") { m->mothurOut("[ERROR]: missing sequence for " + name); m->mothurOutEndLine(); m->control_pressed = true; break; }
143 string name2 = m->getline(in); m->gobble(in);
144 if (name2 == "") { m->mothurOut("[ERROR]: Blank quality name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
145 else if (name2[0] != '+') { m->mothurOut("[ERROR]: reading " + name2 + " expected a name with + as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
146 else { name2 = name2.substr(1); }
148 //read quality scores
149 string qual = m->getline(in); m->gobble(in);
150 if (qual == "") { m->mothurOut("[ERROR]: missing quality for " + name2); m->mothurOutEndLine(); m->control_pressed = true; break; }
152 //sanity check sequence length and number of quality scores match
153 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; } }
154 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; }
156 //convert quality scores
157 vector<int> qualScores = convertQual(qual);
159 //print sequence info to files
160 outFasta << ">" << name << endl << sequence << endl;
162 outQual << ">" << name << endl;
163 for (int i = 0; i < qualScores.size(); i++) { outQual << qualScores[i] << " "; }
171 if (m->control_pressed) { outputTypes.clear(); remove(fastaFile.c_str()); remove(qualFile.c_str()); return 0; }
173 //set fasta file as new current fastafile
175 itTypes = outputTypes.find("fasta");
176 if (itTypes != outputTypes.end()) {
177 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
180 itTypes = outputTypes.find("qfile");
181 if (itTypes != outputTypes.end()) {
182 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
185 m->mothurOutEndLine();
186 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
187 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
188 m->mothurOutEndLine();
192 catch(exception& e) {
193 m->errorOut(e, "ParseFastaQCommand", "execute");
197 //**********************************************************************************************************************
198 vector<int> ParseFastaQCommand::convertQual(string qual) {
200 vector<int> qualScores;
202 int controlChar = int('!');
204 for (int i = 0; i < qual.length(); i++) {
205 int temp = int(qual[i]);
208 qualScores.push_back(temp);
213 catch(exception& e) {
214 m->errorOut(e, "ParseFastaQCommand", "convertQual");
218 //**********************************************************************************************************************