5 * Created by Pat Schloss on 6/6/09.
6 * Copyright 2009 Patrick D. Schloss. All rights reserved.
10 #include "reversecommand.h"
11 #include "sequence.hpp"
12 #include "qualityscores.h"
14 //**********************************************************************************************************************
15 vector<string> ReverseSeqsCommand::setParameters(){
17 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "fastaQual", "none","fasta",false,false,true); parameters.push_back(pfasta);
18 CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "fastaQual", "none","qfile",false,false,true); parameters.push_back(pqfile);
19 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(pinputdir);
20 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "","",false,false); parameters.push_back(poutputdir);
22 vector<string> myArray;
23 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
27 m->errorOut(e, "ReverseSeqsCommand", "setParameters");
31 //**********************************************************************************************************************
32 string ReverseSeqsCommand::getHelpString(){
34 string helpString = "";
35 helpString += "The reverse.seqs command reads a fastafile and outputs a fasta file containing the reverse compliment.\n";
36 helpString += "The reverse.seqs command parameters fasta or qfile are required.\n";
37 helpString += "The reverse.seqs command should be in the following format: \n";
38 helpString += "reverse.seqs(fasta=yourFastaFile) \n";
42 m->errorOut(e, "ReverseSeqsCommand", "getHelpString");
46 //**********************************************************************************************************************
47 string ReverseSeqsCommand::getOutputPattern(string type) {
51 if (type == "fasta") { pattern = "[filename],rc,[extension]"; }
52 else if (type == "qfile") { pattern = "[filename],rc,[extension]"; }
53 else { m->mothurOut("[ERROR]: No definition for type " + type + " output pattern.\n"); m->control_pressed = true; }
58 m->errorOut(e, "ReverseSeqsCommand", "getOutputPattern");
62 //**********************************************************************************************************************
63 ReverseSeqsCommand::ReverseSeqsCommand(){
65 abort = true; calledHelp = true;
67 vector<string> tempOutNames;
68 outputTypes["fasta"] = tempOutNames;
69 outputTypes["qfile"] = tempOutNames;
72 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
76 //***************************************************************************************************************
78 ReverseSeqsCommand::ReverseSeqsCommand(string option) {
80 abort = false; calledHelp = false;
82 //allow user to run help
83 if(option == "help") { help(); abort = true; calledHelp = true; }
84 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
87 vector<string> myArray = setParameters();
89 OptionParser parser(option);
90 map<string,string> parameters = parser.getParameters();
92 ValidParameters validParameter;
93 map<string,string>::iterator it;
95 //check to make sure all parameters are valid for command
96 for (it = parameters.begin(); it != parameters.end(); it++) {
97 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
100 //initialize outputTypes
101 vector<string> tempOutNames;
102 outputTypes["fasta"] = tempOutNames;
103 outputTypes["qfile"] = tempOutNames;
105 //if the user changes the input directory command factory will send this info to us in the output parameter
106 string inputDir = validParameter.validFile(parameters, "inputdir", false);
107 if (inputDir == "not found"){ inputDir = ""; }
110 it = parameters.find("fasta");
111 //user has given a template file
112 if(it != parameters.end()){
113 path = m->hasPath(it->second);
114 //if the user has not given a path then, add inputdir. else leave path alone.
115 if (path == "") { parameters["fasta"] = inputDir + it->second; }
118 it = parameters.find("qfile");
119 //user has given a template file
120 if(it != parameters.end()){
121 path = m->hasPath(it->second);
122 //if the user has not given a path then, add inputdir. else leave path alone.
123 if (path == "") { parameters["qfile"] = inputDir + it->second; }
127 //check for required parameters
128 fastaFileName = validParameter.validFile(parameters, "fasta", true);
129 if (fastaFileName == "not open") { abort = true; }
130 else if (fastaFileName == "not found") { fastaFileName = "";}// m->mothurOut("fasta is a required parameter for the reverse.seqs command."); m->mothurOutEndLine(); abort = true; }
131 else { m->setFastaFile(fastaFileName); }
133 qualFileName = validParameter.validFile(parameters, "qfile", true);
134 if (qualFileName == "not open") { abort = true; }
135 else if (qualFileName == "not found") { qualFileName = ""; }//m->mothurOut("fasta is a required parameter for the reverse.seqs command."); m->mothurOutEndLine(); abort = true; }
136 else { m->setQualFile(qualFileName); }
138 if((fastaFileName == "") && (qualFileName == "")){
139 fastaFileName = m->getFastaFile();
140 if (fastaFileName != "") { m->mothurOut("Using " + fastaFileName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
142 qualFileName = m->getQualFile();
143 if (qualFileName != "") { m->mothurOut("Using " + qualFileName + " as input file for the qfile parameter."); m->mothurOutEndLine(); }
145 m->mothurOut("You have no current files for fasta or qfile, and fasta or qfile is a required parameter for the reverse.seqs command."); m->mothurOutEndLine();
151 //if the user changes the output directory command factory will send this info to us in the output parameter
152 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
158 catch(exception& e) {
159 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
163 //***************************************************************************************************************
166 int ReverseSeqsCommand::execute(){
169 if (abort == true) { if (calledHelp) { return 0; } return 2; }
171 string fastaReverseFileName;
173 if(fastaFileName != ""){
175 m->openInputFile(fastaFileName, inFASTA);
178 string tempOutputDir = outputDir;
179 if (outputDir == "") { tempOutputDir += m->hasPath(fastaFileName); } //if user entered a file with a path then preserve it
180 map<string, string> variables;
181 variables["[filename]"] = tempOutputDir + m->getRootName(m->getSimpleName(fastaFileName));
182 variables["[extension]"] = m->getExtension(fastaFileName);
183 fastaReverseFileName = getOutputFileName("fasta", variables);
184 m->openOutputFile(fastaReverseFileName, outFASTA);
186 while(!inFASTA.eof()){
187 if (m->control_pressed) { inFASTA.close(); outFASTA.close(); m->mothurRemove(fastaReverseFileName); return 0; }
189 Sequence currSeq(inFASTA); m->gobble(inFASTA);
190 if (currSeq.getName() != "") {
191 currSeq.reverseComplement();
192 currSeq.printSequence(outFASTA);
197 outputNames.push_back(fastaReverseFileName); outputTypes["fasta"].push_back(fastaReverseFileName);
200 string qualReverseFileName;
202 if(qualFileName != ""){
203 QualityScores currQual;
206 m->openInputFile(qualFileName, inQual);
209 string tempOutputDir = outputDir;
210 if (outputDir == "") { tempOutputDir += m->hasPath(qualFileName); } //if user entered a file with a path then preserve it
211 map<string, string> variables;
212 variables["[filename]"] = tempOutputDir + m->getRootName(m->getSimpleName(qualFileName));
213 variables["[extension]"] = m->getExtension(qualFileName);
214 string qualReverseFileName = getOutputFileName("qfile", variables);
215 m->openOutputFile(qualReverseFileName, outQual);
217 while(!inQual.eof()){
218 if (m->control_pressed) { inQual.close(); outQual.close(); m->mothurRemove(qualReverseFileName); return 0; }
219 currQual = QualityScores(inQual); m->gobble(inQual);
220 currQual.flipQScores();
221 currQual.printQScores(outQual);
225 outputNames.push_back(qualReverseFileName); outputTypes["qfile"].push_back(qualReverseFileName);
228 if (m->control_pressed) { m->mothurRemove(qualReverseFileName); m->mothurRemove(fastaReverseFileName); return 0; }
230 //set fasta file as new current fastafile
232 itTypes = outputTypes.find("fasta");
233 if (itTypes != outputTypes.end()) {
234 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
237 itTypes = outputTypes.find("qfile");
238 if (itTypes != outputTypes.end()) {
239 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
243 m->mothurOutEndLine();
244 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
245 for(int i=0;i<outputNames.size();i++){
246 m->mothurOut(outputNames[i]);
247 m->mothurOutEndLine();
254 catch(exception& e) {
255 m->errorOut(e, "ReverseSeqsCommand", "execute");
260 //***************************************************************************************************************