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