]> git.donarmstrong.com Git - mothur.git/blob - parsefastaqcommand.cpp
fixes while testing
[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::getValidParameters(){        
15         try {
16                 string Array[] =  {"fastq", "outputdir","inputdir"};
17                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
18                 return myArray;
19         }
20         catch(exception& e) {
21                 m->errorOut(e, "ParseFastaQCommand", "getValidParameters");
22                 exit(1);
23         }
24 }
25 //**********************************************************************************************************************
26 ParseFastaQCommand::ParseFastaQCommand(){       
27         try {
28                 abort = true;
29                 //initialize outputTypes
30                 vector<string> tempOutNames;
31                 outputTypes["fasta"] = tempOutNames;
32                 outputTypes["qual"] = tempOutNames;
33         }
34         catch(exception& e) {
35                 m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand");
36                 exit(1);
37         }
38 }
39 //**********************************************************************************************************************
40 vector<string> ParseFastaQCommand::getRequiredParameters(){     
41         try {
42                 string Array[] =  {"fastq"};
43                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
44                 return myArray;
45         }
46         catch(exception& e) {
47                 m->errorOut(e, "ParseFastaQCommand", "getRequiredParameters");
48                 exit(1);
49         }
50 }
51 //**********************************************************************************************************************
52 vector<string> ParseFastaQCommand::getRequiredFiles(){  
53         try {
54                 vector<string> myArray;
55                 return myArray;
56         }
57         catch(exception& e) {
58                 m->errorOut(e, "ParseFastaQCommand", "getRequiredFiles");
59                 exit(1);
60         }
61 }
62 //**********************************************************************************************************************
63 ParseFastaQCommand::ParseFastaQCommand(string option){
64         try {
65                 abort = false;
66                 
67                 if(option == "help") {  help(); abort = true; }
68                 
69                 else {
70                         //valid paramters for this command
71                         string Array[] =  {"fastq", "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 (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["qual"] = 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                 }               
113         }
114         catch(exception& e) {
115                 m->errorOut(e, "ParseFastaQCommand", "ParseFastaQCommand");
116                 exit(1);
117         }
118 }
119 //**********************************************************************************************************************
120
121 void ParseFastaQCommand::help(){
122         try {
123                 m->mothurOut("The fastq.info command reads a fastaQ file and creates a fasta and quality file.\n");
124                 m->mothurOut("The fastq.info command parameter is fastq, and it is required.\n");
125                 m->mothurOut("The fastq.info command should be in the following format: fastq.info(fastaq=yourFastaQFile).\n");
126                 m->mothurOut("Example fastq.info(fastaq=test.fastaq).\n");
127                 m->mothurOut("Note: No spaces between parameter labels (i.e. fastq), '=' and yourFastQFile.\n");
128                 m->mothurOutEndLine();
129         }
130         catch(exception& e) {
131                 m->errorOut(e, "ParseFastaQCommand", "help");
132                 exit(1);
133         }
134 }
135 //**********************************************************************************************************************
136
137 ParseFastaQCommand::~ParseFastaQCommand()       {       /*      do nothing      */      }
138
139 //**********************************************************************************************************************
140
141 int ParseFastaQCommand::execute(){
142         try {
143                 if (abort == true) {    return 0;       }
144                 
145                 //open Output Files
146                 string fastaFile = outputDir + m->getRootName(m->getSimpleName(fastaQFile)) + "fasta";
147                 string qualFile = outputDir + m->getRootName(m->getSimpleName(fastaQFile)) + "qual";
148                 ofstream outFasta, outQual;
149                 m->openOutputFile(fastaFile, outFasta);  outputNames.push_back(fastaFile); outputTypes["fasta"].push_back(fastaFile);
150                 m->openOutputFile(qualFile, outQual);   outputNames.push_back(qualFile);  outputTypes["qual"].push_back(qualFile);
151                 
152                 ifstream in;
153                 m->openInputFile(fastaQFile, in);
154                 
155                 while (!in.eof()) {
156                 
157                         //read sequence name
158                         string name = m->getline(in); m->gobble(in);
159                         if (name == "") {  m->mothurOut("[ERROR]: Blank fasta name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
160                         else if (name[0] != '@') { m->mothurOut("[ERROR]: reading " + name + " expected a name with @ as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
161                         else { name = name.substr(1); }
162                         
163                         //read sequence
164                         string sequence = m->getline(in); m->gobble(in);
165                         if (sequence == "") {  m->mothurOut("[ERROR]: missing sequence for " + name); m->mothurOutEndLine(); m->control_pressed = true; break; }
166                         
167                         //read sequence name
168                         string name2 = m->getline(in); m->gobble(in);
169                         if (name2 == "") {  m->mothurOut("[ERROR]: Blank quality name."); m->mothurOutEndLine(); m->control_pressed = true; break; }
170                         else if (name2[0] != '+') { m->mothurOut("[ERROR]: reading " + name2 + " expected a name with + as a leading character."); m->mothurOutEndLine(); m->control_pressed = true; break; }
171                         else { name2 = name2.substr(1);  }
172                         
173                         //read quality scores
174                         string qual = m->getline(in); m->gobble(in);
175                         if (qual == "") {  m->mothurOut("[ERROR]: missing quality for " + name2); m->mothurOutEndLine(); m->control_pressed = true; break; }
176                         
177                         //sanity check sequence length and number of quality scores match
178                         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; }
179                         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; }
180                         
181                         //convert quality scores
182                         vector<int> qualScores = convertQual(qual);
183                         
184                         //print sequence info to files
185                         outFasta << ">" << name << endl << sequence << endl;
186                         
187                         outQual << ">" << name << endl;
188                         for (int i = 0; i < qualScores.size(); i++) { outQual << qualScores[i] << " "; }
189                         outQual << endl;
190                 }
191                 
192                 in.close();
193                 outFasta.close();
194                 outQual.close();
195                 
196                 if (m->control_pressed) { outputTypes.clear(); remove(fastaFile.c_str()); remove(qualFile.c_str()); return 0; }
197                 
198                 m->mothurOutEndLine();
199                 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
200                 for (int i = 0; i < outputNames.size(); i++) {  m->mothurOut(outputNames[i]); m->mothurOutEndLine();    }
201                 m->mothurOutEndLine();
202
203                 return 0;
204         }
205         catch(exception& e) {
206                 m->errorOut(e, "ParseFastaQCommand", "execute");
207                 exit(1);
208         }
209 }
210 //**********************************************************************************************************************
211 vector<int> ParseFastaQCommand::convertQual(string qual) {
212         try {
213                 vector<int> qualScores;
214                 
215                 int controlChar = int('!');
216                 
217                 for (int i = 0; i < qual.length(); i++) { 
218                         int temp = int(qual[i]);
219                         temp -= controlChar;
220                         
221                         qualScores.push_back(temp);
222                 }
223                 
224                 return qualScores;
225         }
226         catch(exception& e) {
227                 m->errorOut(e, "ParseFastaQCommand", "convertQual");
228                 exit(1);
229         }
230 }
231 //**********************************************************************************************************************
232
233
234