]> git.donarmstrong.com Git - mothur.git/blob - makefastqcommand.cpp
fixes while testing 1.33.0
[mothur.git] / makefastqcommand.cpp
1 /*
2  *  makefastqcommand.cpp
3  *  mothur
4  *
5  *  Created by westcott on 2/14/11.
6  *  Copyright 2011 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "makefastqcommand.h"
11 #include "sequence.hpp"
12 #include "qualityscores.h"
13
14 //**********************************************************************************************************************
15 vector<string> MakeFastQCommand::setParameters(){       
16         try {
17                 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none","fastq",false,true,true); parameters.push_back(pfasta);
18                 CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "none", "none","fastq",false,true,true); parameters.push_back(pqfile);
19                 CommandParameter pformat("format", "Multiple", "sanger-illumina-illumina1.8+", "sanger", "", "", "","",false,false); 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, "MakeFastQCommand", "setParameters");
29                 exit(1);
30         }
31 }
32 //**********************************************************************************************************************
33 string MakeFastQCommand::getHelpString(){       
34         try {
35                 string helpString = "";
36                 helpString += "The make.fastq command reads a fasta and quality file and creates a fastq file.\n";
37                 helpString += "The make.fastq command parameters are fasta, qfile and format.  fasta and qfile are required.\n";
38                 helpString += "The format parameter is used to indicate whether your sequences are sanger, illumina1.8+ or illumina, default=sanger.\n";
39                 helpString += "The make.fastq command should be in the following format: make.fastq(qfile=yourQualityFile, fasta=yourFasta).\n";
40                 helpString += "Example make.fastq(fasta=amazon.fasta, qfile=amazon.qual).\n";
41                 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
42                 return helpString;
43         }
44         catch(exception& e) {
45                 m->errorOut(e, "MakeFastQCommand", "getHelpString");
46                 exit(1);
47         }
48 }
49 //**********************************************************************************************************************
50 string MakeFastQCommand::getOutputPattern(string type) {
51     try {
52         string pattern = "";
53         
54         if (type == "fastq") {  pattern = "[filename],fastq"; } 
55         else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true;  }
56         
57         return pattern;
58     }
59     catch(exception& e) {
60         m->errorOut(e, "MakeFastQCommand", "getOutputPattern");
61         exit(1);
62     }
63 }
64 //**********************************************************************************************************************
65 MakeFastQCommand::MakeFastQCommand(){   
66         try {
67                 abort = true; calledHelp = true; 
68                 setParameters();
69                 vector<string> tempOutNames;
70                 outputTypes["fastq"] = tempOutNames;
71         }
72         catch(exception& e) {
73                 m->errorOut(e, "MakeFastQCommand", "MakeFastQCommand");
74                 exit(1);
75         }
76 }
77 //**********************************************************************************************************************
78 MakeFastQCommand::MakeFastQCommand(string option)  {
79         try {
80                 abort = false; calledHelp = false;   
81                 
82                 //allow user to run help
83                 if(option == "help") { help(); abort = true; calledHelp = true; }
84                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
85                 
86                 else {
87                         vector<string> myArray = setParameters();
88                         
89                         OptionParser parser(option);
90                         map<string,string> parameters = parser.getParameters();
91                         
92                         ValidParameters validParameter;
93                         map<string,string>::iterator it;
94                         
95                         //check to make sure all parameters are valid for command
96                         for (it = parameters.begin(); it != parameters.end(); it++) { 
97                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
98                         }
99                         
100                         //initialize outputTypes
101                         vector<string> tempOutNames;
102                         outputTypes["fastq"] = tempOutNames;
103                         
104                                                 
105                         //if the user changes the input directory command factory will send this info to us in the output parameter 
106                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
107                         if (inputDir == "not found"){   inputDir = "";          }
108                         else {
109                                 string path;
110                                 it = parameters.find("fasta");
111                                 //user has given a template file
112                                 if(it != parameters.end()){ 
113                                         path = m->hasPath(it->second);
114                                         //if the user has not given a path then, add inputdir. else leave path alone.
115                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
116                                 }
117                                 
118                                 it = parameters.find("qfile");
119                                 //user has given a template file
120                                 if(it != parameters.end()){ 
121                                         path = m->hasPath(it->second);
122                                         //if the user has not given a path then, add inputdir. else leave path alone.
123                                         if (path == "") {       parameters["qfile"] = inputDir + it->second;            }
124                                 }
125                         }
126                         
127                         
128                         //check for required parameters
129                         fastafile = validParameter.validFile(parameters, "fasta", true);
130                         if (fastafile == "not open") { abort = true; fastafile = ""; }
131                         else if (fastafile == "not found") {            
132                                 fastafile = m->getFastaFile(); 
133                                 if (fastafile != "") {  m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
134                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
135                         }else { m->setFastaFile(fastafile); }   
136                         
137                         qualfile = validParameter.validFile(parameters, "qfile", true);
138                         if (qualfile == "not open") { abort = true; qualfile = ""; }
139                         else if (qualfile == "not found") {                     
140                                 qualfile = m->getQualFile(); 
141                                 if (qualfile != "") {  m->mothurOut("Using " + qualfile + " as input file for the qfile parameter."); m->mothurOutEndLine(); }
142                                 else {  m->mothurOut("You have no current qualfile and the qfile parameter is required."); m->mothurOutEndLine(); abort = true; }
143                         }else { m->setQualFile(qualfile); }     
144                         
145                         //if the user changes the output directory command factory will send this info to us in the output parameter 
146                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(fastafile);              }
147             
148             format = validParameter.validFile(parameters, "format", false);             if (format == "not found"){     format = "sanger";      }
149             
150             if ((format != "sanger") && (format != "illumina") && (format != "illumina1.8+"))  { 
151                                 m->mothurOut(format + " is not a valid format. Your format choices are sanger, illumina1.8+ and illumina, aborting." ); m->mothurOutEndLine();
152                                 abort=true;
153                         }
154
155
156                 }
157                 
158         }
159         catch(exception& e) {
160                 m->errorOut(e, "MakeFastQCommand", "MakeFastQCommand");
161                 exit(1);
162         }
163 }
164 //**********************************************************************************************************************
165
166 int MakeFastQCommand::execute(){
167         try {
168                 
169                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
170                 
171         map<string, string> variables; 
172         variables["[filename]"] = outputDir + m->getRootName(m->getSimpleName(fastafile));
173                 string outputFile = getOutputFileName("fastq",variables);
174                 outputNames.push_back(outputFile); outputTypes["fastq"].push_back(outputFile);
175                 
176                 ofstream out;
177                 m->openOutputFile(outputFile, out);
178                 
179                 ifstream qFile;
180                 m->openInputFile(qualfile, qFile);
181                 
182                 ifstream fFile;
183                 m->openInputFile(fastafile, fFile);
184         
185                 while (!fFile.eof() && !qFile.eof()) {
186                         
187                         if (m->control_pressed) { break; }
188                         
189                         Sequence currSeq(fFile); m->gobble(fFile);
190                         QualityScores currQual(qFile);  m->gobble(qFile);
191                         
192                         if (currSeq.getName() != currQual.getName()) { m->mothurOut("[ERROR]: mismatch between fasta and quality files. Found " + currSeq.getName() + " in fasta file and " + currQual.getName() + " in quality file."); m->mothurOutEndLine(); m->control_pressed = true; }
193                         else {
194                                 //print sequence
195                                 out << '@' << currSeq.getName() << endl << currSeq.getAligned() << endl;
196                                 
197                                 string qualityString = convertQual(currQual.getQualityScores());
198                                 
199                                 //print quality info
200                                 out << '+' << currQual.getName() << endl << qualityString << endl;
201                         }
202                         
203                 }
204                 
205                 fFile.close();
206                 qFile.close();
207                 out.close();
208                 
209                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        m->mothurRemove(outputNames[i]); } return 0; }
210                 
211                 m->mothurOutEndLine();
212                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
213                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
214                 m->mothurOutEndLine();
215                                         
216                 return 0;
217                 
218         }
219         catch(exception& e) {
220                 m->errorOut(e, "MakeFastQCommand", "execute");
221                 exit(1);
222         }
223 }
224 //**********************************************************************************************************************
225 string MakeFastQCommand::convertQual(vector<int> qual) {
226         try {
227                 string qualScores;
228                 
229         for (int i = 0; i < qual.size(); i++) { 
230             int controlChar = int('!');
231             if (format == "illumina") {  controlChar = int('@');  }
232             
233                         int temp = qual[i] + controlChar;
234                         char qualChar = (char) temp;
235                         
236                         qualScores += qualChar;
237                 }
238                 
239                 return qualScores;
240         }
241         catch(exception& e) {
242                 m->errorOut(e, "MakeFastQCommand", "convertQual");
243                 exit(1);
244         }
245 }
246 //**********************************************************************************************************************
247
248
249
250
251