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",false,false); parameters.push_back(pfasta);
18 CommandParameter pqfile("qfile", "InputTypes", "", "", "none", "fastaQual", "none",false,false); 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::getOutputFileNameTag(string type, string inputName=""){
49 string outputFileName = "";
50 map<string, vector<string> >::iterator it;
52 //is this a type this command creates
53 it = outputTypes.find(type);
54 if (it == outputTypes.end()) { m->mothurOut("[ERROR]: this command doesn't create a " + type + " output file.\n"); }
56 if (type == "fasta") { outputFileName = "rc" + m->getExtension(inputName); }
57 else if (type == "qfile") { outputFileName = "rc" + m->getExtension(inputName); }
58 else { m->mothurOut("[ERROR]: No definition for type " + type + " output file tag.\n"); m->control_pressed = true; }
60 return outputFileName;
63 m->errorOut(e, "ReverseSeqsCommand", "getOutputFileNameTag");
67 //**********************************************************************************************************************
68 ReverseSeqsCommand::ReverseSeqsCommand(){
70 abort = true; calledHelp = true;
72 vector<string> tempOutNames;
73 outputTypes["fasta"] = tempOutNames;
74 outputTypes["qfile"] = tempOutNames;
77 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
81 //***************************************************************************************************************
83 ReverseSeqsCommand::ReverseSeqsCommand(string option) {
85 abort = false; calledHelp = false;
87 //allow user to run help
88 if(option == "help") { help(); abort = true; calledHelp = true; }
89 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
92 vector<string> myArray = setParameters();
94 OptionParser parser(option);
95 map<string,string> parameters = parser.getParameters();
97 ValidParameters validParameter;
98 map<string,string>::iterator it;
100 //check to make sure all parameters are valid for command
101 for (it = parameters.begin(); it != parameters.end(); it++) {
102 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
105 //initialize outputTypes
106 vector<string> tempOutNames;
107 outputTypes["fasta"] = tempOutNames;
108 outputTypes["qfile"] = tempOutNames;
110 //if the user changes the input directory command factory will send this info to us in the output parameter
111 string inputDir = validParameter.validFile(parameters, "inputdir", false);
112 if (inputDir == "not found"){ inputDir = ""; }
115 it = parameters.find("fasta");
116 //user has given a template file
117 if(it != parameters.end()){
118 path = m->hasPath(it->second);
119 //if the user has not given a path then, add inputdir. else leave path alone.
120 if (path == "") { parameters["fasta"] = inputDir + it->second; }
123 it = parameters.find("qfile");
124 //user has given a template file
125 if(it != parameters.end()){
126 path = m->hasPath(it->second);
127 //if the user has not given a path then, add inputdir. else leave path alone.
128 if (path == "") { parameters["qfile"] = inputDir + it->second; }
132 //check for required parameters
133 fastaFileName = validParameter.validFile(parameters, "fasta", true);
134 if (fastaFileName == "not open") { abort = true; }
135 else if (fastaFileName == "not found") { fastaFileName = "";}// m->mothurOut("fasta is a required parameter for the reverse.seqs command."); m->mothurOutEndLine(); abort = true; }
136 else { m->setFastaFile(fastaFileName); }
138 qualFileName = validParameter.validFile(parameters, "qfile", true);
139 if (qualFileName == "not open") { abort = true; }
140 else if (qualFileName == "not found") { qualFileName = ""; }//m->mothurOut("fasta is a required parameter for the reverse.seqs command."); m->mothurOutEndLine(); abort = true; }
141 else { m->setQualFile(qualFileName); }
143 if((fastaFileName == "") && (qualFileName == "")){
144 fastaFileName = m->getFastaFile();
145 if (fastaFileName != "") { m->mothurOut("Using " + fastaFileName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
147 qualFileName = m->getQualFile();
148 if (qualFileName != "") { m->mothurOut("Using " + qualFileName + " as input file for the qfile parameter."); m->mothurOutEndLine(); }
150 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();
156 //if the user changes the output directory command factory will send this info to us in the output parameter
157 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
163 catch(exception& e) {
164 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
168 //***************************************************************************************************************
171 int ReverseSeqsCommand::execute(){
174 if (abort == true) { if (calledHelp) { return 0; } return 2; }
176 string fastaReverseFileName;
178 if(fastaFileName != ""){
180 m->openInputFile(fastaFileName, inFASTA);
183 string tempOutputDir = outputDir;
184 if (outputDir == "") { tempOutputDir += m->hasPath(fastaFileName); } //if user entered a file with a path then preserve it
185 fastaReverseFileName = tempOutputDir + m->getRootName(m->getSimpleName(fastaFileName)) + getOutputFileNameTag("fasta", fastaFileName);
186 m->openOutputFile(fastaReverseFileName, outFASTA);
188 while(!inFASTA.eof()){
189 if (m->control_pressed) { inFASTA.close(); outFASTA.close(); m->mothurRemove(fastaReverseFileName); return 0; }
191 Sequence currSeq(inFASTA); m->gobble(inFASTA);
192 if (currSeq.getName() != "") {
193 currSeq.reverseComplement();
194 currSeq.printSequence(outFASTA);
199 outputNames.push_back(fastaReverseFileName); outputTypes["fasta"].push_back(fastaReverseFileName);
202 string qualReverseFileName;
204 if(qualFileName != ""){
205 QualityScores currQual;
208 m->openInputFile(qualFileName, inQual);
211 string tempOutputDir = outputDir;
212 if (outputDir == "") { tempOutputDir += m->hasPath(qualFileName); } //if user entered a file with a path then preserve it
213 string qualReverseFileName = tempOutputDir + m->getRootName(m->getSimpleName(qualFileName)) + getOutputFileNameTag("qfile", qualFileName);
214 m->openOutputFile(qualReverseFileName, outQual);
216 while(!inQual.eof()){
217 if (m->control_pressed) { inQual.close(); outQual.close(); m->mothurRemove(qualReverseFileName); return 0; }
218 currQual = QualityScores(inQual); m->gobble(inQual);
219 currQual.flipQScores();
220 currQual.printQScores(outQual);
224 outputNames.push_back(qualReverseFileName); outputTypes["qfile"].push_back(qualReverseFileName);
227 if (m->control_pressed) { m->mothurRemove(qualReverseFileName); m->mothurRemove(fastaReverseFileName); return 0; }
229 //set fasta file as new current fastafile
231 itTypes = outputTypes.find("fasta");
232 if (itTypes != outputTypes.end()) {
233 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
236 itTypes = outputTypes.find("qfile");
237 if (itTypes != outputTypes.end()) {
238 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
242 m->mothurOutEndLine();
243 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
244 for(int i=0;i<outputNames.size();i++){
245 m->mothurOut(outputNames[i]);
246 m->mothurOutEndLine();
253 catch(exception& e) {
254 m->errorOut(e, "ReverseSeqsCommand", "execute");
259 //***************************************************************************************************************