]> git.donarmstrong.com Git - mothur.git/blob - parsefastaqcommand.cpp
Merge remote-tracking branch 'mothur/master'
[mothur.git] / parsefastaqcommand.cpp
1 /*
2  *  parsefastaqcommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 9/30/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "parsefastaqcommand.h"
11 #include "sequence.hpp"
12
13 //**********************************************************************************************************************
14 vector<string> ParseFastaQCommand::setParameters(){     
15         try {
16                 CommandParameter pfastq("fastq", "InputTypes", "", "", "none", "none", "none","",false,true,true); parameters.push_back(pfastq);
17                 CommandParameter pfasta("fasta", "Boolean", "", "T", "", "", "","fasta",false,false); parameters.push_back(pfasta);
18                 CommandParameter pqual("qfile", "Boolean", "", "T", "", "", "","qfile",false,false); parameters.push_back(pqual);
19                 CommandParameter pformat("format", "Multiple", "sanger-illumina-solexa-illumina1.8+", "sanger", "", "", "","",false,false,true); parameters.push_back(pformat);
20         CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
21                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
22                 
23                 vector<string> myArray;
24                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
25                 return myArray;
26         }
27         catch(exception& e) {
28                 m->errorOut(e, "ParseFastaQCommand", "setParameters");
29                 exit(1);
30         }
31 }
32 //**********************************************************************************************************************
33 string ParseFastaQCommand::getHelpString(){     
34         try {
35                 string helpString = "";
36                 helpString += "The fastq.info command reads a fastq file and creates a fasta and quality file.\n";
37                 helpString += "The fastq.info command parameters are fastq, fasta, qfile and format; fastq is required.\n";
38         helpString += "The fastq.info command should be in the following format: fastq.info(fastaq=yourFastaQFile).\n";
39                 helpString += "The format parameter is used to indicate whether your sequences are sanger, solexa, illumina1.8+ or illumina, default=sanger.\n";
40         helpString += "The fasta parameter allows you to indicate whether you want a fasta file generated. Default=T.\n";
41         helpString += "The qfile parameter allows you to indicate whether you want a quality file generated. Default=T.\n";
42                 helpString += "Example fastq.info(fastaq=test.fastaq).\n";
43                 helpString += "Note: No spaces between parameter labels (i.e. fastq), '=' and yourFastQFile.\n";
44                 return helpString;
45         }
46         catch(exception& e) {
47                 m->errorOut(e, "ParseFastaQCommand", "getHelpString");
48                 exit(1);
49         }
50 }
51 //**********************************************************************************************************************
52 string ParseFastaQCommand::getOutputPattern(string type) {
53     try {
54         string pattern = "";
55         
56         if (type == "fasta") {  pattern = "[filename],fasta"; } 
57         else if (type == "qfile") {  pattern = "[filename],qual"; } 
58         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
59         
60         return pattern;
61     }
62     catch(exception& e) {
63         m->errorOut(e, "ParseFastaQCommand", "getOutputPattern");
64         exit(1);
65     }
66 }
67 //**********************************************************************************************************************
68 ParseFastaQCommand::ParseFastaQCommand(){       
69         try {
70                 abort = true; calledHelp = true; 
71                 setParameters();
72                 vector<string> tempOutNames;
73                 outputTypes["fasta"] = tempOutNames;
74                 outputTypes["qfile"] = tempOutNames;
75         }
76         catch(exception& e) {
77                 m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand");
78                 exit(1);
79         }
80 }
81 //**********************************************************************************************************************
82 ParseFastaQCommand::ParseFastaQCommand(string option){
83         try {
84                 abort = false; calledHelp = false;   
85                 
86                 if(option == "help") {  help(); abort = true; calledHelp = true; }
87                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
88                 
89                 else {
90                         vector<string> myArray = setParameters();
91                         
92                         OptionParser parser(option);
93                         map<string,string> parameters = parser.getParameters();
94                         
95                         ValidParameters validParameter;
96                         map<string,string>::iterator it;
97
98                         //check to make sure all parameters are valid for command
99                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
100                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
101                         }
102                         
103                         //initialize outputTypes
104                         vector<string> tempOutNames;
105                         outputTypes["fasta"] = tempOutNames;
106                         outputTypes["qfile"] = tempOutNames;
107                         
108                         //if the user changes the input directory command factory will send this info to us in the output parameter 
109                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
110                         if (inputDir == "not found"){   inputDir = "";          }
111                         else {
112                                 string path;
113                                 it = parameters.find("fastq");
114                                 //user has given a template file
115                                 if(it != parameters.end()){ 
116                                         path = m->hasPath(it->second);
117                                         //if the user has not given a path then, add inputdir. else leave path alone.
118                                         if (path == "") {       parameters["fastq"] = inputDir + it->second;            }
119                                 }
120                         }
121                         
122                         //check for required parameters
123                         fastaQFile = validParameter.validFile(parameters, "fastq", true);
124                         if (fastaQFile == "not found") {        m->mothurOut("fastq is a required parameter for the fastq.info command.");      m->mothurOutEndLine();  abort = true;   }
125                         else if (fastaQFile == "not open")      {       fastaQFile = ""; abort = true;  }       
126                         
127                         //if the user changes the output directory command factory will send this info to us in the output parameter 
128                         outputDir = validParameter.validFile(parameters, "outputdir", false);   if (outputDir == "not found"){  outputDir = m->hasPath(fastaQFile);     }
129                         
130                         string temp;
131                         temp = validParameter.validFile(parameters, "fasta", false);    if(temp == "not found"){        temp = "T";     }
132                         fasta = m->isTrue(temp); 
133
134                         temp = validParameter.validFile(parameters, "qfile", false);    if(temp == "not found"){        temp = "T";     }
135                         qual = m->isTrue(temp); 
136                         
137             format = validParameter.validFile(parameters, "format", false);             if (format == "not found"){     format = "sanger";      }
138             
139             if ((format != "sanger") && (format != "illumina") && (format != "illumina1.8+") && (format != "solexa"))  { 
140                                 m->mothurOut(format + " is not a valid format. Your format choices are sanger, solexa, illumina1.8+ and illumina, aborting." ); m->mothurOutEndLine();
141                                 abort=true;
142                         }
143
144                         if ((!fasta) && (!qual)) { m->mothurOut("[ERROR]: no outputs selected. Aborting."); m->mothurOutEndLine(); abort=true; }
145
146                 }               
147         }
148         catch(exception& e) {
149                 m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand");
150                 exit(1);
151         }
152 }
153 //**********************************************************************************************************************
154
155 int ParseFastaQCommand::execute(){
156         try {
157                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
158                 
159                 //open Output Files
160         map<string, string> variables; 
161         variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastaQFile));
162                 string fastaFile = getOutputFileName("fasta",variables);
163                 string qualFile = getOutputFileName("qfile",variables);
164                 ofstream outFasta, outQual;
165                 
166                 if (fasta) { m->openOutputFile(fastaFile, outFasta);  outputNames.push_back(fastaFile); outputTypes["fasta"].push_back(fastaFile);      }
167                 if (qual) { m->openOutputFile(qualFile, outQual);       outputNames.push_back(qualFile);  outputTypes["qfile"].push_back(qualFile);             }
168                 
169                 ifstream in;
170                 m->openInputFile(fastaQFile, in);
171         
172         //fill convert table - goes from solexa to sanger. Used fq_all2std.pl as a reference.
173         for (int i = -64; i < 65; i++) { 
174             char temp = (char) ((int)(33 + 10*log(1+pow(10,(i/10.0)))/log(10)+0.499));
175             convertTable.push_back(temp);
176         }
177                 
178                 while (!in.eof()) {
179                         
180                         if (m->control_pressed) { break; }
181                 
182                         //read sequence name
183                         string name = m->getline(in); m->gobble(in);
184                         if (name == "") {  m->mothurOut("[ERROR]: Blank fasta name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
185                         else if (name[0] != '@') { m->mothurOut("[ERROR]: reading " + name + " expected a name with @ as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
186                         else { 
187                 name = name.substr(1); 
188                 for (int i = 0; i < name.length(); i++) { if (name[i] == ':') { name[i] = '_'; m->changedSeqNames = true; } }
189             }
190                         
191                         //read sequence
192                         string sequence = m->getline(in); m->gobble(in);
193                         if (sequence == "") {  m->mothurOut("[ERROR]: missing sequence for " + name); m->mothurOutEndLine(); m->control_pressed = true; break; }
194                         
195                         //read sequence name
196                         string name2 = m->getline(in); m->gobble(in);
197                         if (name2 == "") {  m->mothurOut("[ERROR]: Blank quality name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
198                         else if (name2[0] != '+') { m->mothurOut("[ERROR]: reading " + name2 + " expected a name with + as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
199                         else { 
200                 name2 = name2.substr(1);  
201                 for (int i = 0; i < name2.length(); i++) { if (name2[i] == ':') { name2[i] = '_'; m->changedSeqNames = true; } }
202             }
203                         
204                         //read quality scores
205                         string quality = m->getline(in); m->gobble(in);
206                         if (quality == "") {  m->mothurOut("[ERROR]: missing quality for " + name2); m->mothurOutEndLine(); m->control_pressed = true; break; }
207                         
208                         //sanity check sequence length and number of quality scores match
209                         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; } }
210                         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; }
211                         
212                         //print sequence info to files
213                         if (fasta) { outFasta << ">" << name << endl << sequence << endl; }
214                         
215                         if (qual) { 
216                                 vector<int> qualScores = convertQual(quality);
217                                 outQual << ">" << name << endl;
218                                 for (int i = 0; i < qualScores.size(); i++) { outQual << qualScores[i] << " "; }
219                                 outQual << endl;
220                         }
221                 }
222                 
223                 in.close();
224                 if (fasta)      { outFasta.close();     }
225                 if (qual)       { outQual.close();      }
226                 
227                 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(fastaFile); m->mothurRemove(qualFile); return 0; }
228                 
229                 //set fasta file as new current fastafile
230                 string current = "";
231                 itTypes = outputTypes.find("fasta");
232                 if (itTypes != outputTypes.end()) {
233                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
234                 }
235                 
236                 itTypes = outputTypes.find("qfile");
237                 if (itTypes != outputTypes.end()) {
238                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
239                 }               
240                 
241                 m->mothurOutEndLine();
242                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
243                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
244                 m->mothurOutEndLine();
245
246                 return 0;
247         }
248         catch(exception& e) {
249                 m->errorOut(e, "ParseFastaQCommand", "execute");
250                 exit(1);
251         }
252 }
253 //**********************************************************************************************************************
254 vector<int> ParseFastaQCommand::convertQual(string qual) {
255         try {
256                 vector<int> qualScores;
257                 
258         bool negativeScores = false;
259         
260                 for (int i = 0; i < qual.length(); i++) { 
261             
262             int temp = 0;
263             temp = int(qual[i]);
264             if (format == "illumina") {
265                 temp -= 64; //char '@'
266             }else if (format == "illumina1.8+") {
267                 temp -= int('!'); //char '!'
268             }else if (format == "solexa") {
269                 temp = int(convertTable[temp]); //convert to sanger
270                 temp -= int('!'); //char '!'
271             }else {
272                 temp -= int('!'); //char '!'
273             }
274             if (temp < -5) { negativeScores = true; }
275                         qualScores.push_back(temp);
276                 }
277                 
278         if (negativeScores) { m->mothurOut("[ERROR]: finding negative quality scores, do you have the right format selected? http://en.wikipedia.org/wiki/FASTQ_format#Encoding \n");  m->control_pressed = true;  }
279         
280                 return qualScores;
281         }
282         catch(exception& e) {
283                 m->errorOut(e, "ParseFastaQCommand", "convertQual");
284                 exit(1);
285         }
286 }
287 //**********************************************************************************************************************
288
289
290