]> git.donarmstrong.com Git - mothur.git/blob - makefastqcommand.cpp
added make.fastq command
[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 get.groups command selects sequences from a specfic group or set of groups from the following file types: fasta, name, group, list, taxonomy.\n");
146                 m->mothurOut("It outputs a file containing the sequences in the those specified groups.\n");
147                 m->mothurOut("The get.groups command parameters are accnos, fasta, name, group, list, taxonomy and groups. The group parameter is required.\n");
148                 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");
149                 m->mothurOut("The groups parameter allows you to specify which of the groups in your groupfile you would like.  You can separate group names with dashes.\n");
150                 m->mothurOut("The get.groups command should be in the following format: get.groups(accnos=yourAccnos, fasta=yourFasta, group=yourGroupFile).\n");
151                 m->mothurOut("Example get.seqs(accnos=amazon.accnos, fasta=amazon.fasta, group=amazon.groups).\n");
152                 m->mothurOut("or get.seqs(groups=pasture, fasta=amazon.fasta, amazon.groups).\n");
153                 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");*/
154         }
155         catch(exception& e) {
156                 m->errorOut(e, "MakeFastQCommand", "help");
157                 exit(1);
158         }
159 }
160
161 //**********************************************************************************************************************
162
163 int MakeFastQCommand::execute(){
164         try {
165                 
166                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
167                 
168                 
169                 string outputFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "fastq";
170                 outputNames.push_back(outputFile); outputTypes["fastq"].push_back(outputFile);
171                 
172                 ofstream out;
173                 m->openOutputFile(outputFile, out);
174                 
175                 ifstream qFile;
176                 m->openInputFile(qualfile, qFile);
177                 
178                 ifstream fFile;
179                 m->openInputFile(fastafile, fFile);
180                 
181                 while (!fFile.eof() && !qFile.eof()) {
182                         
183                         if (m->control_pressed) { break; }
184                         
185                         Sequence currSeq(fFile); m->gobble(fFile);
186                         QualityScores currQual(qFile);  m->gobble(qFile);
187                         
188                         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; }
189                         else {
190                                 //print sequence
191                                 out << '@' << currSeq.getName() << endl << currSeq.getAligned() << endl;
192                                 
193                                 string qualityString = convertQual(currQual.getQualityScores());
194                                 
195                                 //print quality info
196                                 out << '+' << currQual.getName() << endl << qualityString << endl;
197                         }
198                         
199                 }
200                 
201                 fFile.close();
202                 qFile.close();
203                 out.close();
204                 
205                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str()); } return 0; }
206                 
207                 m->mothurOutEndLine();
208                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
209                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
210                 m->mothurOutEndLine();
211                                         
212                 return 0;
213                 
214         }
215         catch(exception& e) {
216                 m->errorOut(e, "MakeFastQCommand", "execute");
217                 exit(1);
218         }
219 }
220 //**********************************************************************************************************************
221 string MakeFastQCommand::convertQual(vector<int> qual) {
222         try {
223                 string qualScores;
224                 
225                 int controlChar = int('!');
226                 
227                 for (int i = 0; i < qual.size(); i++) { 
228                         int temp = qual[i] + controlChar;
229                         char qualChar = (char) temp;
230                         
231                         qualScores += qualChar;
232                 }
233                 
234                 return qualScores;
235         }
236         catch(exception& e) {
237                 m->errorOut(e, "MakeFastQCommand", "convertQual");
238                 exit(1);
239         }
240 }
241 //**********************************************************************************************************************
242
243
244
245
246