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::getValidParameters(){
17 string Array[] = {"fasta", "qfile", "outputdir", "inputdir"};
18 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
22 m->errorOut(e, "ReverseSeqsCommand", "getValidParameters");
26 //**********************************************************************************************************************
27 ReverseSeqsCommand::ReverseSeqsCommand(){
29 abort = true; calledHelp = true;
30 vector<string> tempOutNames;
31 outputTypes["fasta"] = tempOutNames;
32 outputTypes["qfile"] = tempOutNames;
35 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
39 //**********************************************************************************************************************
41 vector<string> ReverseSeqsCommand::getRequiredParameters(){
43 string Array[] = {"fasta", "qfile"};
44 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
48 m->errorOut(e, "ReverseSeqsCommand", "getRequiredParameters");
53 //**********************************************************************************************************************
55 vector<string> ReverseSeqsCommand::getRequiredFiles(){
57 vector<string> myArray;
61 m->errorOut(e, "ReverseSeqsCommand", "getRequiredFiles");
64 }//***************************************************************************************************************
66 ReverseSeqsCommand::ReverseSeqsCommand(string option) {
68 abort = false; calledHelp = false;
70 //allow user to run help
71 if(option == "help") { help(); abort = true; calledHelp = true; }
74 //valid paramters for this command
75 string Array[] = {"fasta", "qfile", "outputdir","inputdir"};
76 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
78 OptionParser parser(option);
79 map<string,string> parameters = parser.getParameters();
81 ValidParameters validParameter;
82 map<string,string>::iterator it;
84 //check to make sure all parameters are valid for command
85 for (it = parameters.begin(); it != parameters.end(); it++) {
86 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
89 //initialize outputTypes
90 vector<string> tempOutNames;
91 outputTypes["fasta"] = tempOutNames;
92 outputTypes["qfile"] = tempOutNames;
94 //if the user changes the input directory command factory will send this info to us in the output parameter
95 string inputDir = validParameter.validFile(parameters, "inputdir", false);
96 if (inputDir == "not found"){ inputDir = ""; }
99 it = parameters.find("fasta");
100 //user has given a template file
101 if(it != parameters.end()){
102 path = m->hasPath(it->second);
103 //if the user has not given a path then, add inputdir. else leave path alone.
104 if (path == "") { parameters["fasta"] = inputDir + it->second; }
108 //check for required parameters
109 fastaFileName = validParameter.validFile(parameters, "fasta", true);
110 if (fastaFileName == "not open") { abort = true; }
111 else if (fastaFileName == "not found") { fastaFileName = "";}// m->mothurOut("fasta is a required parameter for the reverse.seqs command."); m->mothurOutEndLine(); abort = true; }
113 qualFileName = validParameter.validFile(parameters, "qfile", true);
114 if (qualFileName == "not open") { abort = true; }
115 else if (qualFileName == "not found") { qualFileName = ""; }//m->mothurOut("fasta is a required parameter for the reverse.seqs command."); m->mothurOutEndLine(); abort = true; }
117 if(fastaFileName == "not open" && qualFileName == "not open"){
118 m->mothurOut("fasta or qfile is a required parameter for the reverse.seqs command.");
119 m->mothurOutEndLine();
123 //if the user changes the output directory command factory will send this info to us in the output parameter
124 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
126 outputDir += m->hasPath(fastaFileName); //if user entered a file with a path then preserve it
131 catch(exception& e) {
132 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
136 //**********************************************************************************************************************
138 void ReverseSeqsCommand::help(){
140 m->mothurOut("The reverse.seqs command reads a fastafile and outputs a fasta file containing the reverse compliment.\n");
141 m->mothurOut("The reverse.seqs command parameters fasta or qfile are required.\n");
142 m->mothurOut("The reverse.seqs command should be in the following format: \n");
143 m->mothurOut("reverse.seqs(fasta=yourFastaFile) \n");
145 catch(exception& e) {
146 m->errorOut(e, "ReverseSeqsCommand", "help");
151 //***************************************************************************************************************
153 ReverseSeqsCommand::~ReverseSeqsCommand(){ /* do nothing */ }
155 //***************************************************************************************************************
158 int ReverseSeqsCommand::execute(){
161 if (abort == true) { if (calledHelp) { return 0; } return 2; }
163 string fastaReverseFileName;
165 if(fastaFileName != ""){
167 m->openInputFile(fastaFileName, inFASTA);
170 fastaReverseFileName = outputDir + m->getRootName(m->getSimpleName(fastaFileName)) + "rc" + m->getExtension(fastaFileName);
171 m->openOutputFile(fastaReverseFileName, outFASTA);
173 while(!inFASTA.eof()){
174 if (m->control_pressed) { inFASTA.close(); outFASTA.close(); remove(fastaReverseFileName.c_str()); return 0; }
176 Sequence currSeq(inFASTA); m->gobble(inFASTA);
177 if (currSeq.getName() != "") {
178 currSeq.reverseComplement();
179 currSeq.printSequence(outFASTA);
184 outputNames.push_back(fastaReverseFileName);
187 string qualReverseFileName;
189 if(qualFileName != ""){
190 QualityScores currQual;
193 m->openInputFile(qualFileName, inQual);
196 string qualReverseFileName = outputDir + m->getRootName(m->getSimpleName(qualFileName)) + "rc" + m->getExtension(qualFileName);
197 m->openOutputFile(qualReverseFileName, outQual);
199 while(!inQual.eof()){
200 if (m->control_pressed) { inQual.close(); outQual.close(); remove(qualReverseFileName.c_str()); return 0; }
201 currQual = QualityScores(inQual); m->gobble(inQual);
202 currQual.flipQScores();
203 currQual.printQScores(outQual);
207 outputNames.push_back(qualReverseFileName);
210 m->mothurOutEndLine();
211 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
212 for(int i=0;i<outputNames.size();i++){
213 m->mothurOut(outputNames[i]);
214 m->mothurOutEndLine();
219 catch(exception& e) {
220 m->errorOut(e, "ReverseSeqsCommand", "execute");
225 //***************************************************************************************************************