]> git.donarmstrong.com Git - mothur.git/blob - makefastqcommand.cpp
added getCommandInfoCommand for gui
[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 MakeFastQCommand::MakeFastQCommand(){   
50         try {
51                 abort = true; calledHelp = true; 
52                 setParameters();
53                 vector<string> tempOutNames;
54                 outputTypes["fastq"] = tempOutNames;
55         }
56         catch(exception& e) {
57                 m->errorOut(e, "MakeFastQCommand", "MakeFastQCommand");
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                         vector<string> myArray = setParameters();
71                         
72                         OptionParser parser(option);
73                         map<string,string> parameters = parser.getParameters();
74                         
75                         ValidParameters validParameter;
76                         map<string,string>::iterator it;
77                         
78                         //check to make sure all parameters are valid for command
79                         for (it = parameters.begin(); it != parameters.end(); it++) { 
80                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
81                         }
82                         
83                         //initialize outputTypes
84                         vector<string> tempOutNames;
85                         outputTypes["fastq"] = tempOutNames;
86                         
87                                                 
88                         //if the user changes the input directory command factory will send this info to us in the output parameter 
89                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
90                         if (inputDir == "not found"){   inputDir = "";          }
91                         else {
92                                 string path;
93                                 it = parameters.find("fasta");
94                                 //user has given a template file
95                                 if(it != parameters.end()){ 
96                                         path = m->hasPath(it->second);
97                                         //if the user has not given a path then, add inputdir. else leave path alone.
98                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
99                                 }
100                                 
101                                 it = parameters.find("qfile");
102                                 //user has given a template file
103                                 if(it != parameters.end()){ 
104                                         path = m->hasPath(it->second);
105                                         //if the user has not given a path then, add inputdir. else leave path alone.
106                                         if (path == "") {       parameters["qfile"] = inputDir + it->second;            }
107                                 }
108                         }
109                         
110                         
111                         //check for required parameters
112                         fastafile = validParameter.validFile(parameters, "fasta", true);
113                         if (fastafile == "not open") { abort = true; fastafile = ""; }
114                         else if (fastafile == "not found") {            
115                                 fastafile = m->getFastaFile(); 
116                                 if (fastafile != "") {  m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
117                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
118                         }       
119                         
120                         qualfile = validParameter.validFile(parameters, "qfile", true);
121                         if (qualfile == "not open") { abort = true; qualfile = ""; }
122                         else if (qualfile == "not found") {                     
123                                 qualfile = m->getQualFile(); 
124                                 if (qualfile != "") {  m->mothurOut("Using " + qualfile + " as input file for the qfile parameter."); m->mothurOutEndLine(); }
125                                 else {  m->mothurOut("You have no current qualfile and the qfile parameter is required."); m->mothurOutEndLine(); abort = true; }
126                         }       
127                         
128                         //if the user changes the output directory command factory will send this info to us in the output parameter 
129                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(fastafile);              }
130
131                 }
132                 
133         }
134         catch(exception& e) {
135                 m->errorOut(e, "MakeFastQCommand", "MakeFastQCommand");
136                 exit(1);
137         }
138 }
139 //**********************************************************************************************************************
140
141 int MakeFastQCommand::execute(){
142         try {
143                 
144                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
145                 
146                 
147                 string outputFile = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "fastq";
148                 outputNames.push_back(outputFile); outputTypes["fastq"].push_back(outputFile);
149                 
150                 ofstream out;
151                 m->openOutputFile(outputFile, out);
152                 
153                 ifstream qFile;
154                 m->openInputFile(qualfile, qFile);
155                 
156                 ifstream fFile;
157                 m->openInputFile(fastafile, fFile);
158                 
159                 while (!fFile.eof() && !qFile.eof()) {
160                         
161                         if (m->control_pressed) { break; }
162                         
163                         Sequence currSeq(fFile); m->gobble(fFile);
164                         QualityScores currQual(qFile);  m->gobble(qFile);
165                         
166                         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; }
167                         else {
168                                 //print sequence
169                                 out << '@' << currSeq.getName() << endl << currSeq.getAligned() << endl;
170                                 
171                                 string qualityString = convertQual(currQual.getQualityScores());
172                                 
173                                 //print quality info
174                                 out << '+' << currQual.getName() << endl << qualityString << endl;
175                         }
176                         
177                 }
178                 
179                 fFile.close();
180                 qFile.close();
181                 out.close();
182                 
183                 if (m->control_pressed) { for (int i = 0; i < outputNames.size(); i++) {        remove(outputNames[i].c_str()); } return 0; }
184                 
185                 m->mothurOutEndLine();
186                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
187                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
188                 m->mothurOutEndLine();
189                                         
190                 return 0;
191                 
192         }
193         catch(exception& e) {
194                 m->errorOut(e, "MakeFastQCommand", "execute");
195                 exit(1);
196         }
197 }
198 //**********************************************************************************************************************
199 string MakeFastQCommand::convertQual(vector<int> qual) {
200         try {
201                 string qualScores;
202                 
203                 int controlChar = int('!');
204                 
205                 for (int i = 0; i < qual.size(); i++) { 
206                         int temp = qual[i] + controlChar;
207                         char qualChar = (char) temp;
208                         
209                         qualScores += qualChar;
210                 }
211                 
212                 return qualScores;
213         }
214         catch(exception& e) {
215                 m->errorOut(e, "MakeFastQCommand", "convertQual");
216                 exit(1);
217         }
218 }
219 //**********************************************************************************************************************
220
221
222
223
224