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