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");
47 //**********************************************************************************************************************
48 ReverseSeqsCommand::ReverseSeqsCommand(){
50 abort = true; calledHelp = true;
52 vector<string> tempOutNames;
53 outputTypes["fasta"] = tempOutNames;
54 outputTypes["qfile"] = tempOutNames;
57 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
61 //***************************************************************************************************************
63 ReverseSeqsCommand::ReverseSeqsCommand(string option) {
65 abort = false; calledHelp = false;
67 //allow user to run help
68 if(option == "help") { help(); abort = true; calledHelp = true; }
71 vector<string> myArray = setParameters();
73 OptionParser parser(option);
74 map<string,string> parameters = parser.getParameters();
76 ValidParameters validParameter;
77 map<string,string>::iterator it;
79 //check to make sure all parameters are valid for command
80 for (it = parameters.begin(); it != parameters.end(); it++) {
81 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
84 //initialize outputTypes
85 vector<string> tempOutNames;
86 outputTypes["fasta"] = tempOutNames;
87 outputTypes["qfile"] = tempOutNames;
89 //if the user changes the input directory command factory will send this info to us in the output parameter
90 string inputDir = validParameter.validFile(parameters, "inputdir", false);
91 if (inputDir == "not found"){ inputDir = ""; }
94 it = parameters.find("fasta");
95 //user has given a template file
96 if(it != parameters.end()){
97 path = m->hasPath(it->second);
98 //if the user has not given a path then, add inputdir. else leave path alone.
99 if (path == "") { parameters["fasta"] = inputDir + it->second; }
102 it = parameters.find("qfile");
103 //user has given a template file
104 if(it != parameters.end()){
105 path = m->hasPath(it->second);
106 //if the user has not given a path then, add inputdir. else leave path alone.
107 if (path == "") { parameters["qfile"] = inputDir + it->second; }
111 //check for required parameters
112 fastaFileName = validParameter.validFile(parameters, "fasta", true);
113 if (fastaFileName == "not open") { abort = true; }
114 else if (fastaFileName == "not found") { fastaFileName = "";}// m->mothurOut("fasta is a required parameter for the reverse.seqs command."); m->mothurOutEndLine(); abort = true; }
116 qualFileName = validParameter.validFile(parameters, "qfile", true);
117 if (qualFileName == "not open") { abort = true; }
118 else if (qualFileName == "not found") { qualFileName = ""; }//m->mothurOut("fasta is a required parameter for the reverse.seqs command."); m->mothurOutEndLine(); abort = true; }
120 if((fastaFileName == "") && (qualFileName == "")){
121 fastaFileName = m->getFastaFile();
122 if (fastaFileName != "") { m->mothurOut("Using " + fastaFileName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
124 qualFileName = m->getQualFile();
125 if (qualFileName != "") { m->mothurOut("Using " + qualFileName + " as input file for the qfile parameter."); m->mothurOutEndLine(); }
127 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();
133 //if the user changes the output directory command factory will send this info to us in the output parameter
134 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
140 catch(exception& e) {
141 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
145 //***************************************************************************************************************
148 int ReverseSeqsCommand::execute(){
151 if (abort == true) { if (calledHelp) { return 0; } return 2; }
153 string fastaReverseFileName;
155 if(fastaFileName != ""){
157 m->openInputFile(fastaFileName, inFASTA);
160 string tempOutputDir = outputDir;
161 if (outputDir == "") { tempOutputDir += m->hasPath(fastaFileName); } //if user entered a file with a path then preserve it
162 fastaReverseFileName = tempOutputDir + m->getRootName(m->getSimpleName(fastaFileName)) + "rc" + m->getExtension(fastaFileName);
163 m->openOutputFile(fastaReverseFileName, outFASTA);
165 while(!inFASTA.eof()){
166 if (m->control_pressed) { inFASTA.close(); outFASTA.close(); remove(fastaReverseFileName.c_str()); return 0; }
168 Sequence currSeq(inFASTA); m->gobble(inFASTA);
169 if (currSeq.getName() != "") {
170 currSeq.reverseComplement();
171 currSeq.printSequence(outFASTA);
176 outputNames.push_back(fastaReverseFileName); outputTypes["fasta"].push_back(fastaReverseFileName);
179 string qualReverseFileName;
181 if(qualFileName != ""){
182 QualityScores currQual;
185 m->openInputFile(qualFileName, inQual);
188 string tempOutputDir = outputDir;
189 if (outputDir == "") { tempOutputDir += m->hasPath(qualFileName); } //if user entered a file with a path then preserve it
190 string qualReverseFileName = tempOutputDir + m->getRootName(m->getSimpleName(qualFileName)) + "rc" + m->getExtension(qualFileName);
191 m->openOutputFile(qualReverseFileName, outQual);
193 while(!inQual.eof()){
194 if (m->control_pressed) { inQual.close(); outQual.close(); remove(qualReverseFileName.c_str()); return 0; }
195 currQual = QualityScores(inQual); m->gobble(inQual);
196 currQual.flipQScores();
197 currQual.printQScores(outQual);
201 outputNames.push_back(qualReverseFileName); outputTypes["qfile"].push_back(qualReverseFileName);
204 if (m->control_pressed) { remove(qualReverseFileName.c_str()); remove(fastaReverseFileName.c_str()); return 0; }
206 //set fasta file as new current fastafile
208 itTypes = outputTypes.find("fasta");
209 if (itTypes != outputTypes.end()) {
210 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
213 itTypes = outputTypes.find("qfile");
214 if (itTypes != outputTypes.end()) {
215 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
219 m->mothurOutEndLine();
220 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
221 for(int i=0;i<outputNames.size();i++){
222 m->mothurOut(outputNames[i]);
223 m->mothurOutEndLine();
230 catch(exception& e) {
231 m->errorOut(e, "ReverseSeqsCommand", "execute");
236 //***************************************************************************************************************