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"
13 //**********************************************************************************************************************
14 vector<string> ReverseSeqsCommand::getValidParameters(){
16 string Array[] = {"fasta", "outputdir","inputdir"};
17 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
21 m->errorOut(e, "ReverseSeqsCommand", "getValidParameters");
25 //**********************************************************************************************************************
26 ReverseSeqsCommand::ReverseSeqsCommand(){
28 abort = true; calledHelp = true;
29 vector<string> tempOutNames;
30 outputTypes["fasta"] = tempOutNames;
33 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
37 //**********************************************************************************************************************
38 vector<string> ReverseSeqsCommand::getRequiredParameters(){
40 string Array[] = {"fasta"};
41 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
45 m->errorOut(e, "ReverseSeqsCommand", "getRequiredParameters");
49 //**********************************************************************************************************************
50 vector<string> ReverseSeqsCommand::getRequiredFiles(){
52 vector<string> myArray;
56 m->errorOut(e, "ReverseSeqsCommand", "getRequiredFiles");
60 //***************************************************************************************************************
62 ReverseSeqsCommand::ReverseSeqsCommand(string option) {
64 abort = false; calledHelp = false;
66 //allow user to run help
67 if(option == "help") { help(); abort = true; calledHelp = true; }
70 //valid paramters for this command
71 string Array[] = {"fasta", "outputdir","inputdir"};
72 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
74 OptionParser parser(option);
75 map<string,string> parameters = parser.getParameters();
77 ValidParameters validParameter;
78 map<string,string>::iterator it;
80 //check to make sure all parameters are valid for command
81 for (it = parameters.begin(); it != parameters.end(); it++) {
82 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
85 //initialize outputTypes
86 vector<string> tempOutNames;
87 outputTypes["fasta"] = 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; }
103 //check for required parameters
104 fasta = validParameter.validFile(parameters, "fasta", true);
105 if (fasta == "not open") { abort = true; }
106 else if (fasta == "not found") { fasta = ""; m->mothurOut("fasta is a required parameter for the reverse.seqs command."); m->mothurOutEndLine(); abort = true; }
108 //if the user changes the output directory command factory will send this info to us in the output parameter
109 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
111 outputDir += m->hasPath(fasta); //if user entered a file with a path then preserve it
116 catch(exception& e) {
117 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
121 //**********************************************************************************************************************
123 void ReverseSeqsCommand::help(){
125 m->mothurOut("The reverse.seqs command reads a fastafile and outputs a fasta file containing the reverse compliment.\n");
126 m->mothurOut("The reverse.seqs command parameter is fasta and it is required.\n");
127 m->mothurOut("The reverse.seqs command should be in the following format: \n");
128 m->mothurOut("reverse.seqs(fasta=yourFastaFile) \n");
130 catch(exception& e) {
131 m->errorOut(e, "ReverseSeqsCommand", "help");
136 //***************************************************************************************************************
138 ReverseSeqsCommand::~ReverseSeqsCommand(){ /* do nothing */ }
140 //***************************************************************************************************************
143 int ReverseSeqsCommand::execute(){
146 if (abort == true) { if (calledHelp) { return 0; } return 2; }
149 m->openInputFile(fasta, inFASTA);
152 string reverseFile = outputDir + m->getRootName(m->getSimpleName(fasta)) + "rc" + m->getExtension(fasta);
153 m->openOutputFile(reverseFile, outFASTA);
155 while(!inFASTA.eof()){
156 if (m->control_pressed) { inFASTA.close(); outFASTA.close(); remove(reverseFile.c_str()); return 0; }
158 Sequence currSeq(inFASTA); m->gobble(inFASTA);
159 if (currSeq.getName() != "") {
160 currSeq.reverseComplement();
161 currSeq.printSequence(outFASTA);
167 if (m->control_pressed) { remove(reverseFile.c_str()); return 0; }
169 m->mothurOutEndLine();
170 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
171 m->mothurOut(reverseFile); m->mothurOutEndLine(); outputNames.push_back(reverseFile); outputTypes["fasta"].push_back(reverseFile);
172 m->mothurOutEndLine();
178 catch(exception& e) {
179 m->errorOut(e, "ReverseSeqsCommand", "execute");
184 //***************************************************************************************************************