]> git.donarmstrong.com Git - mothur.git/blob - parsefastaqcommand.cpp
75d6fa01be65bcccf8f176bfd29dcce843efc0e3
[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 pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
18                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
19                 
20                 vector<string> myArray;
21                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
22                 return myArray;
23         }
24         catch(exception& e) {
25                 m->errorOut(e, "ParseFastaQCommand", "setParameters");
26                 exit(1);
27         }
28 }
29 //**********************************************************************************************************************
30 string ParseFastaQCommand::getHelpString(){     
31         try {
32                 string helpString = "";
33                 helpString += "The fastq.info command reads a fastq file and creates a fasta and quality file.\n";
34                 helpString += "The fastq.info command parameter is fastq, and it is required.\n";
35                 helpString += "The fastq.info command should be in the following format: fastq.info(fastaq=yourFastaQFile).\n";
36                 helpString += "Example fastq.info(fastaq=test.fastaq).\n";
37                 helpString += "Note: No spaces between parameter labels (i.e. fastq), '=' and yourFastQFile.\n";
38                 return helpString;
39         }
40         catch(exception& e) {
41                 m->errorOut(e, "ParseFastaQCommand", "getHelpString");
42                 exit(1);
43         }
44 }
45 //**********************************************************************************************************************
46 ParseFastaQCommand::ParseFastaQCommand(){       
47         try {
48                 abort = true; calledHelp = true; 
49                 setParameters();
50                 vector<string> tempOutNames;
51                 outputTypes["fasta"] = tempOutNames;
52                 outputTypes["qfile"] = tempOutNames;
53         }
54         catch(exception& e) {
55                 m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand");
56                 exit(1);
57         }
58 }
59 //**********************************************************************************************************************
60 ParseFastaQCommand::ParseFastaQCommand(string option){
61         try {
62                 abort = false; calledHelp = false;   
63                 
64                 if(option == "help") {  help(); abort = true; calledHelp = true; }
65                 
66                 else {
67                         vector<string> myArray = setParameters();
68                         
69                         OptionParser parser(option);
70                         map<string,string> parameters = parser.getParameters();
71                         
72                         ValidParameters validParameter;
73                         map<string,string>::iterator it;
74
75                         //check to make sure all parameters are valid for command
76                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
77                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
78                         }
79                         
80                         //initialize outputTypes
81                         vector<string> tempOutNames;
82                         outputTypes["fasta"] = tempOutNames;
83                         outputTypes["qfile"] = tempOutNames;
84                         
85                         //if the user changes the input directory command factory will send this info to us in the output parameter 
86                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
87                         if (inputDir == "not found"){   inputDir = "";          }
88                         else {
89                                 string path;
90                                 it = parameters.find("fastq");
91                                 //user has given a template file
92                                 if(it != parameters.end()){ 
93                                         path = m->hasPath(it->second);
94                                         //if the user has not given a path then, add inputdir. else leave path alone.
95                                         if (path == "") {       parameters["fastq"] = inputDir + it->second;            }
96                                 }
97                         }
98                         
99                         //check for required parameters
100                         fastaQFile = validParameter.validFile(parameters, "fastq", true);
101                         if (fastaQFile == "not found") {        m->mothurOut("fastq is a required parameter for the fastq.info command.");      m->mothurOutEndLine();  abort = true;   }
102                         else if (fastaQFile == "not open")      {       fastaQFile = ""; abort = true;  }       
103                         
104                         //if the user changes the output directory command factory will send this info to us in the output parameter 
105                         outputDir = validParameter.validFile(parameters, "outputdir", false);   if (outputDir == "not found"){  outputDir = m->hasPath(fastaQFile);     }
106
107                 }               
108         }
109         catch(exception& e) {
110                 m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand");
111                 exit(1);
112         }
113 }
114 //**********************************************************************************************************************
115
116 int ParseFastaQCommand::execute(){
117         try {
118                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
119                 
120                 //open Output Files
121                 string fastaFile = outputDir + m->getRootName(m->getSimpleName(fastaQFile)) + "fasta";
122                 string qualFile = outputDir + m->getRootName(m->getSimpleName(fastaQFile)) + "qual";
123                 ofstream outFasta, outQual;
124                 m->openOutputFile(fastaFile, outFasta);  outputNames.push_back(fastaFile); outputTypes["fasta"].push_back(fastaFile);
125                 m->openOutputFile(qualFile, outQual);   outputNames.push_back(qualFile);  outputTypes["qfile"].push_back(qualFile);
126                 
127                 ifstream in;
128                 m->openInputFile(fastaQFile, in);
129                 
130                 while (!in.eof()) {
131                 
132                         //read sequence name
133                         string name = m->getline(in); m->gobble(in);
134                         if (name == "") {  m->mothurOut("[ERROR]: Blank fasta name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
135                         else if (name[0] != '@') { m->mothurOut("[ERROR]: reading " + name + " expected a name with @ as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
136                         else { name = name.substr(1); }
137                         
138                         //read sequence
139                         string sequence = m->getline(in); m->gobble(in);
140                         if (sequence == "") {  m->mothurOut("[ERROR]: missing sequence for " + name); m->mothurOutEndLine(); m->control_pressed = true; break; }
141                         
142                         //read sequence name
143                         string name2 = m->getline(in); m->gobble(in);
144                         if (name2 == "") {  m->mothurOut("[ERROR]: Blank quality name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
145                         else if (name2[0] != '+') { m->mothurOut("[ERROR]: reading " + name2 + " expected a name with + as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
146                         else { name2 = name2.substr(1);  }
147                         
148                         //read quality scores
149                         string qual = m->getline(in); m->gobble(in);
150                         if (qual == "") {  m->mothurOut("[ERROR]: missing quality for " + name2); m->mothurOutEndLine(); m->control_pressed = true; break; }
151                         
152                         //sanity check sequence length and number of quality scores match
153                         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; } }
154                         if (qual.length() != sequence.length()) { m->mothurOut("[ERROR]: lengths do not match. read " + toString(sequence.length()) + " characters for fasta and " + toString(qual.length()) + " characters for quality scores."); m->mothurOutEndLine(); m->control_pressed = true; break; }
155                         
156                         //convert quality scores
157                         vector<int> qualScores = convertQual(qual);
158                         
159                         //print sequence info to files
160                         outFasta << ">" << name << endl << sequence << endl;
161                         
162                         outQual << ">" << name << endl;
163                         for (int i = 0; i < qualScores.size(); i++) { outQual << qualScores[i] << " "; }
164                         outQual << endl;
165                 }
166                 
167                 in.close();
168                 outFasta.close();
169                 outQual.close();
170                 
171                 if (m->control_pressed) { outputTypes.clear(); remove(fastaFile.c_str()); remove(qualFile.c_str()); return 0; }
172                 
173                 //set fasta file as new current fastafile
174                 string current = "";
175                 itTypes = outputTypes.find("fasta");
176                 if (itTypes != outputTypes.end()) {
177                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
178                 }
179                 
180                 itTypes = outputTypes.find("qfile");
181                 if (itTypes != outputTypes.end()) {
182                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
183                 }               
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         catch(exception& e) {
193                 m->errorOut(e, "ParseFastaQCommand", "execute");
194                 exit(1);
195         }
196 }
197 //**********************************************************************************************************************
198 vector<int> ParseFastaQCommand::convertQual(string qual) {
199         try {
200                 vector<int> qualScores;
201                 
202                 int controlChar = int('!');
203                 
204                 for (int i = 0; i < qual.length(); i++) { 
205                         int temp = int(qual[i]);
206                         temp -= controlChar;
207                         
208                         qualScores.push_back(temp);
209                 }
210                 
211                 return qualScores;
212         }
213         catch(exception& e) {
214                 m->errorOut(e, "ParseFastaQCommand", "convertQual");
215                 exit(1);
216         }
217 }
218 //**********************************************************************************************************************
219
220
221