]> git.donarmstrong.com Git - mothur.git/blob - makefastqcommand.cpp
18ca8644a2f0bfffc1035706c7a6b46df8aba043
[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::getValidParameters(){  
16         try {
17                 string Array[] =  {"fasta","qfile","outputdir","inputdir" };
18                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
19                 return myArray;
20         }
21         catch(exception& e) {
22                 m->errorOut(e, "MakeFastQCommand", "getValidParameters");
23                 exit(1);
24         }
25 }
26 //**********************************************************************************************************************
27 MakeFastQCommand::MakeFastQCommand(){   
28         try {
29                 abort = true; calledHelp = true; 
30                 vector<string> tempOutNames;
31                 outputTypes["fastq"] = tempOutNames;
32         }
33         catch(exception& e) {
34                 m->errorOut(e, "MakeFastQCommand", "MakeFastQCommand");
35                 exit(1);
36         }
37 }
38 //**********************************************************************************************************************
39 vector<string> MakeFastQCommand::getRequiredParameters(){       
40         try {
41                 string Array[] =  {"fasta","qfile"};
42                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
43                 return myArray;
44         }
45         catch(exception& e) {
46                 m->errorOut(e, "MakeFastQCommand", "getRequiredParameters");
47                 exit(1);
48         }
49 }
50 //**********************************************************************************************************************
51 vector<string> MakeFastQCommand::getRequiredFiles(){    
52         try {
53                 vector<string> myArray;
54                 return myArray;
55         }
56         catch(exception& e) {
57                 m->errorOut(e, "MakeFastQCommand", "getRequiredFiles");
58                 exit(1);
59         }
60 }
61 //**********************************************************************************************************************
62 MakeFastQCommand::MakeFastQCommand(string option)  {
63         try {
64                 abort = false; calledHelp = false;   
65                 
66                 //allow user to run help
67                 if(option == "help") { help(); abort = true; calledHelp = true; }
68                 
69                 else {
70                         //valid paramters for this command
71                         string Array[] =  {"fasta","qfile", "outputdir","inputdir" };
72                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
73                         
74                         OptionParser parser(option);
75                         map<string,string> parameters = parser.getParameters();
76                         
77                         ValidParameters validParameter;
78                         map<string,string>::iterator it;
79                         
80                         //check to make sure all parameters are valid for command
81                         for (it = parameters.begin(); it != parameters.end(); it++) { 
82                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
83                         }
84                         
85                         //initialize outputTypes
86                         vector<string> tempOutNames;
87                         outputTypes["fastq"] = tempOutNames;
88                         
89                                                 
90                         //if the user changes the input directory command factory will send this info to us in the output parameter 
91                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
92                         if (inputDir == "not found"){   inputDir = "";          }
93                         else {
94                                 string path;
95                                 it = parameters.find("fasta");
96                                 //user has given a template file
97                                 if(it != parameters.end()){ 
98                                         path = m->hasPath(it->second);
99                                         //if the user has not given a path then, add inputdir. else leave path alone.
100                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
101                                 }
102                                 
103                                 it = parameters.find("qfile");
104                                 //user has given a template file
105                                 if(it != parameters.end()){ 
106                                         path = m->hasPath(it->second);
107                                         //if the user has not given a path then, add inputdir. else leave path alone.
108                                         if (path == "") {       parameters["qfile"] = inputDir + it->second;            }
109                                 }
110                                 
111                                 it = parameters.find("list");
112                                 //user has given a template file
113                                 if(it != parameters.end()){ 
114                                         path = m->hasPath(it->second);
115                                         //if the user has not given a path then, add inputdir. else leave path alone.
116                                         if (path == "") {       parameters["list"] = inputDir + it->second;             }
117                                 }
118                         }
119                         
120                         
121                         //check for required parameters
122                         fastafile = validParameter.validFile(parameters, "fasta", true);
123                         if (fastafile == "not open") { abort = true; fastafile = ""; }
124                         else if (fastafile == "not found") {  fastafile = "";  m->mothurOut("You must provide a fasta file."); m->mothurOutEndLine(); abort = true; }   
125                         
126                         qualfile = validParameter.validFile(parameters, "qfile", true);
127                         if (qualfile == "not open") { abort = true; qualfile = ""; }
128                         else if (qualfile == "not found") {  qualfile = "";  m->mothurOut("You must provide a quality file."); m->mothurOutEndLine(); abort = true; }   
129                         
130                         //if the user changes the output directory command factory will send this info to us in the output parameter 
131                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(fastafile);              }
132
133                 }
134                 
135         }
136         catch(exception& e) {
137                 m->errorOut(e, "MakeFastQCommand", "MakeFastQCommand");
138                 exit(1);
139         }
140 }
141 //**********************************************************************************************************************
142
143 void MakeFastQCommand::help(){
144         try {
145                 m->mothurOut("The make.fastq command read a fasta and quality file and creates a fastq file.\n");
146                 m->mothurOut("The make.fastq command parameters are fasta and qfile, both are required.\n");
147                 m->mothurOut("You must also provide an accnos containing the list of groups to get or set the groups parameter to the groups you wish to select.\n");
148                 m->mothurOut("The make.fastq command should be in the following format: make.fastq(qfile=yourQualityFile, fasta=yourFasta).\n");
149                 m->mothurOut("Example make.fastq(fasta=amazon.fasta, qfile=amazon.qual).\n");
150                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
151         }
152         catch(exception& e) {
153                 m->errorOut(e, "MakeFastQCommand", "help");
154                 exit(1);
155         }
156 }
157
158 //**********************************************************************************************************************
159
160 int MakeFastQCommand::execute(){
161         try {
162                 
163                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
164                 
165                 
166                 string outputFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "fastq";
167                 outputNames.push_back(outputFile); outputTypes["fastq"].push_back(outputFile);
168                 
169                 ofstream out;
170                 m->openOutputFile(outputFile, out);
171                 
172                 ifstream qFile;
173                 m->openInputFile(qualfile, qFile);
174                 
175                 ifstream fFile;
176                 m->openInputFile(fastafile, fFile);
177                 
178                 while (!fFile.eof() && !qFile.eof()) {
179                         
180                         if (m->control_pressed) { break; }
181                         
182                         Sequence currSeq(fFile); m->gobble(fFile);
183                         QualityScores currQual(qFile);  m->gobble(qFile);
184                         
185                         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; }
186                         else {
187                                 //print sequence
188                                 out << '@' << currSeq.getName() << endl << currSeq.getAligned() << endl;
189                                 
190                                 string qualityString = convertQual(currQual.getQualityScores());
191                                 
192                                 //print quality info
193                                 out << '+' << currQual.getName() << endl << qualityString << endl;
194                         }
195                         
196                 }
197                 
198                 fFile.close();
199                 qFile.close();
200                 out.close();
201                 
202                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str()); } return 0; }
203                 
204                 m->mothurOutEndLine();
205                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
206                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
207                 m->mothurOutEndLine();
208                                         
209                 return 0;
210                 
211         }
212         catch(exception& e) {
213                 m->errorOut(e, "MakeFastQCommand", "execute");
214                 exit(1);
215         }
216 }
217 //**********************************************************************************************************************
218 string MakeFastQCommand::convertQual(vector<int> qual) {
219         try {
220                 string qualScores;
221                 
222                 int controlChar = int('!');
223                 
224                 for (int i = 0; i < qual.size(); i++) { 
225                         int temp = qual[i] + controlChar;
226                         char qualChar = (char) temp;
227                         
228                         qualScores += qualChar;
229                 }
230                 
231                 return qualScores;
232         }
233         catch(exception& e) {
234                 m->errorOut(e, "MakeFastQCommand", "convertQual");
235                 exit(1);
236         }
237 }
238 //**********************************************************************************************************************
239
240
241
242
243