]> git.donarmstrong.com Git - mothur.git/blob - parsefastaqcommand.cpp
06ea359e6740bd822691e9f8ab6e3e53454cc724
[mothur.git] / parsefastaqcommand.cpp
1 /*
2  *  parsefastaqcommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 9/30/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "parsefastaqcommand.h"
11 #include "sequence.hpp"
12
13 //**********************************************************************************************************************
14 vector<string> ParseFastaQCommand::setParameters(){     
15         try {
16                 CommandParameter pfastq("fastq", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfastq);
17                 CommandParameter pfasta("fasta", "Bool", "", "T", "", "", "",false,false); parameters.push_back(pfasta);
18                 CommandParameter pqual("qfile", "Bool", "", "T", "", "", "",false,false); parameters.push_back(pqual);
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, "ParseFastaQCommand", "setParameters");
28                 exit(1);
29         }
30 }
31 //**********************************************************************************************************************
32 string ParseFastaQCommand::getHelpString(){     
33         try {
34                 string helpString = "";
35                 helpString += "The fastq.info command reads a fastq file and creates a fasta and quality file.\n";
36                 helpString += "The fastq.info command parameters are fastq, fasta and qfile; fastq is required.\n";
37                 helpString += "The fastq.info command should be in the following format: fastq.info(fastaq=yourFastaQFile).\n";
38         helpString += "The fasta parameter allows you to indicate whether you want a fasta file generated. Default=T.\n";
39         helpString += "The qfile parameter allows you to indicate whether you want a quality file generated. Default=T.\n";
40                 helpString += "Example fastq.info(fastaq=test.fastaq).\n";
41                 helpString += "Note: No spaces between parameter labels (i.e. fastq), '=' and yourFastQFile.\n";
42                 return helpString;
43         }
44         catch(exception& e) {
45                 m->errorOut(e, "ParseFastaQCommand", "getHelpString");
46                 exit(1);
47         }
48 }
49 //**********************************************************************************************************************
50 ParseFastaQCommand::ParseFastaQCommand(){       
51         try {
52                 abort = true; calledHelp = true; 
53                 setParameters();
54                 vector<string> tempOutNames;
55                 outputTypes["fasta"] = tempOutNames;
56                 outputTypes["qfile"] = tempOutNames;
57         }
58         catch(exception& e) {
59                 m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand");
60                 exit(1);
61         }
62 }
63 //**********************************************************************************************************************
64 ParseFastaQCommand::ParseFastaQCommand(string option){
65         try {
66                 abort = false; calledHelp = false;   
67                 
68                 if(option == "help") {  help(); abort = true; calledHelp = true; }
69                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
70                 
71                 else {
72                         vector<string> myArray = setParameters();
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 (map<string,string>::iterator 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["fasta"] = tempOutNames;
88                         outputTypes["qfile"] = tempOutNames;
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("fastq");
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["fastq"] = inputDir + it->second;            }
101                                 }
102                         }
103                         
104                         //check for required parameters
105                         fastaQFile = validParameter.validFile(parameters, "fastq", true);
106                         if (fastaQFile == "not found") {        m->mothurOut("fastq is a required parameter for the fastq.info command.");      m->mothurOutEndLine();  abort = true;   }
107                         else if (fastaQFile == "not open")      {       fastaQFile = ""; abort = true;  }       
108                         
109                         //if the user changes the output directory command factory will send this info to us in the output parameter 
110                         outputDir = validParameter.validFile(parameters, "outputdir", false);   if (outputDir == "not found"){  outputDir = m->hasPath(fastaQFile);     }
111                         
112                         string temp;
113                         temp = validParameter.validFile(parameters, "fasta", false);    if(temp == "not found"){        temp = "T";     }
114                         fasta = m->isTrue(temp); 
115
116                         temp = validParameter.validFile(parameters, "qfile", false);    if(temp == "not found"){        temp = "T";     }
117                         qual = m->isTrue(temp); 
118                         
119                         if ((!fasta) && (!qual)) { m->mothurOut("[ERROR]: no outputs selected. Aborting."); m->mothurOutEndLine(); abort=true; }
120
121                 }               
122         }
123         catch(exception& e) {
124                 m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand");
125                 exit(1);
126         }
127 }
128 //**********************************************************************************************************************
129
130 int ParseFastaQCommand::execute(){
131         try {
132                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
133                 
134                 //open Output Files
135                 string fastaFile = outputDir + m->getRootName(m->getSimpleName(fastaQFile)) + "fasta";
136                 string qualFile = outputDir + m->getRootName(m->getSimpleName(fastaQFile)) + "qual";
137                 ofstream outFasta, outQual;
138                 
139                 if (fasta) { m->openOutputFile(fastaFile, outFasta);  outputNames.push_back(fastaFile); outputTypes["fasta"].push_back(fastaFile);      }
140                 if (qual) { m->openOutputFile(qualFile, outQual);       outputNames.push_back(qualFile);  outputTypes["qfile"].push_back(qualFile);             }
141                 
142                 ifstream in;
143                 m->openInputFile(fastaQFile, in);
144                 
145                 while (!in.eof()) {
146                         
147                         if (m->control_pressed) { break; }
148                 
149                         //read sequence name
150                         string name = m->getline(in); m->gobble(in);
151                         if (name == "") {  m->mothurOut("[ERROR]: Blank fasta name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
152                         else if (name[0] != '@') { m->mothurOut("[ERROR]: reading " + name + " expected a name with @ as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
153                         else { name = name.substr(1); }
154                         
155                         //read sequence
156                         string sequence = m->getline(in); m->gobble(in);
157                         if (sequence == "") {  m->mothurOut("[ERROR]: missing sequence for " + name); m->mothurOutEndLine(); m->control_pressed = true; break; }
158                         
159                         //read sequence name
160                         string name2 = m->getline(in); m->gobble(in);
161                         if (name2 == "") {  m->mothurOut("[ERROR]: Blank quality name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
162                         else if (name2[0] != '+') { m->mothurOut("[ERROR]: reading " + name2 + " expected a name with + as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
163                         else { name2 = name2.substr(1);  }
164                         
165                         //read quality scores
166                         string quality = m->getline(in); m->gobble(in);
167                         if (quality == "") {  m->mothurOut("[ERROR]: missing quality for " + name2); m->mothurOutEndLine(); m->control_pressed = true; break; }
168                         
169                         //sanity check sequence length and number of quality scores match
170                         if (name2 != "") { if (name != name2) { m->mothurOut("[ERROR]: names do not match. read " + name + " for fasta and " + name2 + " for quality."); m->mothurOutEndLine(); m->control_pressed = true; break; } }
171                         if (quality.length() != sequence.length()) { m->mothurOut("[ERROR]: Lengths do not match for sequence " + name + ". Read " + toString(sequence.length()) + " characters for fasta and " + toString(quality.length()) + " characters for quality scores."); m->mothurOutEndLine(); m->control_pressed = true; break; }
172                         
173                         //print sequence info to files
174                         if (fasta) { outFasta << ">" << name << endl << sequence << endl; }
175                         
176                         if (qual) { 
177                                 vector<int> qualScores = convertQual(quality);
178                                 outQual << ">" << name << endl;
179                                 for (int i = 0; i < qualScores.size(); i++) { outQual << qualScores[i] << " "; }
180                                 outQual << endl;
181                         }
182                 }
183                 
184                 in.close();
185                 if (fasta)      { outFasta.close();     }
186                 if (qual)       { outQual.close();      }
187                 
188                 if (m->control_pressed) { outputTypes.clear(); m->mothurRemove(fastaFile); m->mothurRemove(qualFile); return 0; }
189                 
190                 //set fasta file as new current fastafile
191                 string current = "";
192                 itTypes = outputTypes.find("fasta");
193                 if (itTypes != outputTypes.end()) {
194                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
195                 }
196                 
197                 itTypes = outputTypes.find("qfile");
198                 if (itTypes != outputTypes.end()) {
199                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
200                 }               
201                 
202                 m->mothurOutEndLine();
203                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
204                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
205                 m->mothurOutEndLine();
206
207                 return 0;
208         }
209         catch(exception& e) {
210                 m->errorOut(e, "ParseFastaQCommand", "execute");
211                 exit(1);
212         }
213 }
214 //**********************************************************************************************************************
215 vector<int> ParseFastaQCommand::convertQual(string qual) {
216         try {
217                 vector<int> qualScores;
218                 
219                 int controlChar = int('@');
220                 
221                 for (int i = 0; i < qual.length(); i++) { 
222                         int temp = int(qual[i]);
223                         temp -= controlChar;
224                         
225                         qualScores.push_back(temp);
226                 }
227                 
228                 return qualScores;
229         }
230         catch(exception& e) {
231                 m->errorOut(e, "ParseFastaQCommand", "convertQual");
232                 exit(1);
233         }
234 }
235 //**********************************************************************************************************************
236
237
238