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