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 ParseFastaQCommand::ParseFastaQCommand(string option){
18 if(option == "help") { help(); abort = true; }
21 //valid paramters for this command
22 string Array[] = {"fastq", "outputdir", "inputdir"};
23 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
25 OptionParser parser(option);
26 map<string,string> parameters = parser.getParameters();
28 ValidParameters validParameter;
29 map<string,string>::iterator it;
31 //check to make sure all parameters are valid for command
32 for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
33 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
36 //if the user changes the input directory command factory will send this info to us in the output parameter
37 string inputDir = validParameter.validFile(parameters, "inputdir", false);
38 if (inputDir == "not found"){ inputDir = ""; }
41 it = parameters.find("fastq");
42 //user has given a template file
43 if(it != parameters.end()){
44 path = m->hasPath(it->second);
45 //if the user has not given a path then, add inputdir. else leave path alone.
46 if (path == "") { parameters["fastq"] = inputDir + it->second; }
50 //check for required parameters
51 fastaQFile = validParameter.validFile(parameters, "fastq", true);
52 if (fastaQFile == "not found") { m->mothurOut("fastq is a required parameter for the fastq.info command."); m->mothurOutEndLine(); abort = true; }
53 else if (fastaQFile == "not open") { fastaQFile = ""; abort = true; }
55 //if the user changes the output directory command factory will send this info to us in the output parameter
56 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(fastaQFile); }
61 m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand");
65 //**********************************************************************************************************************
67 void ParseFastaQCommand::help(){
69 m->mothurOut("The fastq.info command reads a fastaQ file and creates a fasta and quality file.\n");
70 m->mothurOut("The fastq.info command parameter is fastq, and it is required.\n");
71 m->mothurOut("The fastq.info command should be in the following format: fastq.info(fastaq=yourFastaQFile).\n");
72 m->mothurOut("Example fastq.info(fastaq=test.fastaq).\n");
73 m->mothurOut("Note: No spaces between parameter labels (i.e. fastq), '=' and yourFastQFile.\n");
74 m->mothurOutEndLine();
77 m->errorOut(e, "ParseFastaQCommand", "help");
81 //**********************************************************************************************************************
83 ParseFastaQCommand::~ParseFastaQCommand() { /* do nothing */ }
85 //**********************************************************************************************************************
87 int ParseFastaQCommand::execute(){
89 if (abort == true) { return 0; }
92 string fastaFile = outputDir + m->getRootName(m->getSimpleName(fastaQFile)) + "fasta";
93 string qualFile = outputDir + m->getRootName(m->getSimpleName(fastaQFile)) + "qual";
94 ofstream outFasta, outQual;
95 m->openOutputFile(fastaFile, outFasta); outputNames.push_back(fastaFile);
96 m->openOutputFile(qualFile, outQual); outputNames.push_back(qualFile);
99 m->openInputFile(fastaQFile, in);
104 string name = m->getline(in); m->gobble(in);
105 if (name == "") { m->mothurOut("[ERROR]: Blank fasta name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
106 else if (name[0] != '@') { m->mothurOut("[ERROR]: reading " + name + " expected a name with @ as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
107 else { name = name.substr(1); }
110 string sequence = m->getline(in); m->gobble(in);
111 if (sequence == "") { m->mothurOut("[ERROR]: missing sequence for " + name); m->mothurOutEndLine(); m->control_pressed = true; break; }
114 string name2 = m->getline(in); m->gobble(in);
115 if (name2 == "") { m->mothurOut("[ERROR]: Blank quality name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
116 else if (name2[0] != '+') { m->mothurOut("[ERROR]: reading " + name2 + " expected a name with + as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
117 else { name2 = name2.substr(1); }
119 //read quality scores
120 string qual = m->getline(in); m->gobble(in);
121 if (qual == "") { m->mothurOut("[ERROR]: missing quality for " + name2); m->mothurOutEndLine(); m->control_pressed = true; break; }
123 //sanity check sequence length and number of quality scores match
124 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; }
125 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; }
127 //convert quality scores
128 vector<int> qualScores = convertQual(qual);
130 //print sequence info to files
131 outFasta << ">" << name << endl << sequence << endl;
133 outQual << ">" << name << endl;
134 for (int i = 0; i < qualScores.size(); i++) { outQual << qualScores[i] << " "; }
142 if (m->control_pressed) { remove(fastaFile.c_str()); remove(qualFile.c_str()); return 0; }
144 m->mothurOutEndLine();
145 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
146 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
147 m->mothurOutEndLine();
151 catch(exception& e) {
152 m->errorOut(e, "ParseFastaQCommand", "execute");
156 //**********************************************************************************************************************
157 vector<int> ParseFastaQCommand::convertQual(string qual) {
159 vector<int> qualScores;
161 int controlChar = int('!');
163 for (int i = 0; i < qual.length(); i++) {
164 int temp = int(qual[i]);
167 qualScores.push_back(temp);
172 catch(exception& e) {
173 m->errorOut(e, "ParseFastaQCommand", "convertQual");
177 //**********************************************************************************************************************