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; }
65 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
68 vector<string> myArray = setParameters();
70 OptionParser parser(option);
71 map<string,string> parameters = parser.getParameters();
73 ValidParameters validParameter;
74 map<string,string>::iterator it;
76 //check to make sure all parameters are valid for command
77 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
78 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
81 //initialize outputTypes
82 vector<string> tempOutNames;
83 outputTypes["fasta"] = tempOutNames;
84 outputTypes["qfile"] = tempOutNames;
86 //if the user changes the input directory command factory will send this info to us in the output parameter
87 string inputDir = validParameter.validFile(parameters, "inputdir", false);
88 if (inputDir == "not found"){ inputDir = ""; }
91 it = parameters.find("fastq");
92 //user has given a template file
93 if(it != parameters.end()){
94 path = m->hasPath(it->second);
95 //if the user has not given a path then, add inputdir. else leave path alone.
96 if (path == "") { parameters["fastq"] = inputDir + it->second; }
100 //check for required parameters
101 fastaQFile = validParameter.validFile(parameters, "fastq", true);
102 if (fastaQFile == "not found") { m->mothurOut("fastq is a required parameter for the fastq.info command."); m->mothurOutEndLine(); abort = true; }
103 else if (fastaQFile == "not open") { fastaQFile = ""; abort = true; }
105 //if the user changes the output directory command factory will send this info to us in the output parameter
106 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(fastaQFile); }
110 catch(exception& e) {
111 m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand");
115 //**********************************************************************************************************************
117 int ParseFastaQCommand::execute(){
119 if (abort == true) { if (calledHelp) { return 0; } return 2; }
122 string fastaFile = outputDir + m->getRootName(m->getSimpleName(fastaQFile)) + "fasta";
123 string qualFile = outputDir + m->getRootName(m->getSimpleName(fastaQFile)) + "qual";
124 ofstream outFasta, outQual;
125 m->openOutputFile(fastaFile, outFasta); outputNames.push_back(fastaFile); outputTypes["fasta"].push_back(fastaFile);
126 m->openOutputFile(qualFile, outQual); outputNames.push_back(qualFile); outputTypes["qfile"].push_back(qualFile);
129 m->openInputFile(fastaQFile, in);
134 string name = m->getline(in); m->gobble(in);
135 if (name == "") { m->mothurOut("[ERROR]: Blank fasta name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
136 else if (name[0] != '@') { m->mothurOut("[ERROR]: reading " + name + " expected a name with @ as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
137 else { name = name.substr(1); }
140 string sequence = m->getline(in); m->gobble(in);
141 if (sequence == "") { m->mothurOut("[ERROR]: missing sequence for " + name); m->mothurOutEndLine(); m->control_pressed = true; break; }
144 string name2 = m->getline(in); m->gobble(in);
145 if (name2 == "") { m->mothurOut("[ERROR]: Blank quality name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
146 else if (name2[0] != '+') { m->mothurOut("[ERROR]: reading " + name2 + " expected a name with + as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
147 else { name2 = name2.substr(1); }
149 //read quality scores
150 string qual = m->getline(in); m->gobble(in);
151 if (qual == "") { m->mothurOut("[ERROR]: missing quality for " + name2); m->mothurOutEndLine(); m->control_pressed = true; break; }
153 //sanity check sequence length and number of quality scores match
154 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; } }
155 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; }
157 //convert quality scores
158 vector<int> qualScores = convertQual(qual);
160 //print sequence info to files
161 outFasta << ">" << name << endl << sequence << endl;
163 outQual << ">" << name << endl;
164 for (int i = 0; i < qualScores.size(); i++) { outQual << qualScores[i] << " "; }
172 if (m->control_pressed) { outputTypes.clear(); remove(fastaFile.c_str()); remove(qualFile.c_str()); return 0; }
174 //set fasta file as new current fastafile
176 itTypes = outputTypes.find("fasta");
177 if (itTypes != outputTypes.end()) {
178 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
181 itTypes = outputTypes.find("qfile");
182 if (itTypes != outputTypes.end()) {
183 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
186 m->mothurOutEndLine();
187 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
188 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
189 m->mothurOutEndLine();
193 catch(exception& e) {
194 m->errorOut(e, "ParseFastaQCommand", "execute");
198 //**********************************************************************************************************************
199 vector<int> ParseFastaQCommand::convertQual(string qual) {
201 vector<int> qualScores;
203 int controlChar = int('!');
205 for (int i = 0; i < qual.length(); i++) {
206 int temp = int(qual[i]);
209 qualScores.push_back(temp);
214 catch(exception& e) {
215 m->errorOut(e, "ParseFastaQCommand", "convertQual");
219 //**********************************************************************************************************************