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(){
29 //initialize outputTypes
30 vector<string> tempOutNames;
31 outputTypes["fasta"] = tempOutNames;
34 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
38 //**********************************************************************************************************************
39 vector<string> ReverseSeqsCommand::getRequiredParameters(){
41 string Array[] = {"fasta"};
42 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
46 m->errorOut(e, "ReverseSeqsCommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> ReverseSeqsCommand::getRequiredFiles(){
53 vector<string> myArray;
57 m->errorOut(e, "ReverseSeqsCommand", "getRequiredFiles");
61 //***************************************************************************************************************
63 ReverseSeqsCommand::ReverseSeqsCommand(string option) {
67 //allow user to run help
68 if(option == "help") { help(); abort = true; }
71 //valid paramters for this command
72 string Array[] = {"fasta", "outputdir","inputdir"};
73 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
75 OptionParser parser(option);
76 map<string,string> parameters = parser.getParameters();
78 ValidParameters validParameter;
79 map<string,string>::iterator it;
81 //check to make sure all parameters are valid for command
82 for (it = parameters.begin(); it != parameters.end(); it++) {
83 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
86 //initialize outputTypes
87 vector<string> tempOutNames;
88 outputTypes["fasta"] = tempOutNames;
90 //if the user changes the input directory command factory will send this info to us in the output parameter
91 string inputDir = validParameter.validFile(parameters, "inputdir", false);
92 if (inputDir == "not found"){ inputDir = ""; }
95 it = parameters.find("fasta");
96 //user has given a template file
97 if(it != parameters.end()){
98 path = m->hasPath(it->second);
99 //if the user has not given a path then, add inputdir. else leave path alone.
100 if (path == "") { parameters["fasta"] = inputDir + it->second; }
104 //check for required parameters
105 fasta = validParameter.validFile(parameters, "fasta", true);
106 if (fasta == "not open") { abort = true; }
107 else if (fasta == "not found") { fasta = ""; m->mothurOut("fasta is a required parameter for the reverse.seqs command."); m->mothurOutEndLine(); abort = true; }
109 //if the user changes the output directory command factory will send this info to us in the output parameter
110 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
112 outputDir += m->hasPath(fasta); //if user entered a file with a path then preserve it
117 catch(exception& e) {
118 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
122 //**********************************************************************************************************************
124 void ReverseSeqsCommand::help(){
126 m->mothurOut("The reverse.seqs command reads a fastafile and outputs a fasta file containing the reverse compliment.\n");
127 m->mothurOut("The reverse.seqs command parameter is fasta and it is required.\n");
128 m->mothurOut("The reverse.seqs command should be in the following format: \n");
129 m->mothurOut("reverse.seqs(fasta=yourFastaFile) \n");
131 catch(exception& e) {
132 m->errorOut(e, "ReverseSeqsCommand", "help");
137 //***************************************************************************************************************
139 ReverseSeqsCommand::~ReverseSeqsCommand(){ /* do nothing */ }
141 //***************************************************************************************************************
144 int ReverseSeqsCommand::execute(){
147 if (abort == true) { return 0; }
150 m->openInputFile(fasta, inFASTA);
153 string reverseFile = outputDir + m->getRootName(m->getSimpleName(fasta)) + "rc" + m->getExtension(fasta);
154 m->openOutputFile(reverseFile, outFASTA);
156 while(!inFASTA.eof()){
157 if (m->control_pressed) { inFASTA.close(); outFASTA.close(); remove(reverseFile.c_str()); return 0; }
159 Sequence currSeq(inFASTA); m->gobble(inFASTA);
160 if (currSeq.getName() != "") {
161 currSeq.reverseComplement();
162 currSeq.printSequence(outFASTA);
168 if (m->control_pressed) { remove(reverseFile.c_str()); return 0; }
170 m->mothurOutEndLine();
171 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
172 m->mothurOut(reverseFile); m->mothurOutEndLine(); outputNames.push_back(reverseFile); outputTypes["fasta"].push_back(reverseFile);
173 m->mothurOutEndLine();
179 catch(exception& e) {
180 m->errorOut(e, "ReverseSeqsCommand", "execute");
185 //***************************************************************************************************************