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"
14 //***************************************************************************************************************
16 ReverseSeqsCommand::ReverseSeqsCommand(string option) {
20 //allow user to run help
21 if(option == "help") { help(); abort = true; }
24 //valid paramters for this command
25 string Array[] = {"fasta", "outputdir","inputdir"};
26 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
28 OptionParser parser(option);
29 map<string,string> parameters = parser.getParameters();
31 ValidParameters validParameter;
32 map<string,string>::iterator it;
34 //check to make sure all parameters are valid for command
35 for (it = parameters.begin(); it != parameters.end(); it++) {
36 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
39 //if the user changes the input directory command factory will send this info to us in the output parameter
40 string inputDir = validParameter.validFile(parameters, "inputdir", false);
41 if (inputDir == "not found"){ inputDir = ""; }
44 it = parameters.find("fasta");
45 //user has given a template file
46 if(it != parameters.end()){
47 path = m->hasPath(it->second);
48 //if the user has not given a path then, add inputdir. else leave path alone.
49 if (path == "") { parameters["fasta"] = inputDir + it->second; }
53 //check for required parameters
54 fasta = validParameter.validFile(parameters, "fasta", true);
55 if (fasta == "not open") { abort = true; }
56 else if (fasta == "not found") { fasta = ""; m->mothurOut("fasta is a required parameter for the reverse.seqs command."); m->mothurOutEndLine(); abort = true; }
58 //if the user changes the output directory command factory will send this info to us in the output parameter
59 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
61 outputDir += m->hasPath(fasta); //if user entered a file with a path then preserve it
67 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
71 //**********************************************************************************************************************
73 void ReverseSeqsCommand::help(){
75 m->mothurOut("The reverse.seqs command reads a fastafile and ....\n");
76 m->mothurOut("The reverse.seqs command parameter is fasta and it is required.\n");
77 m->mothurOut("The reverse.seqs command should be in the following format: \n");
78 m->mothurOut("reverse.seqs(fasta=yourFastaFile) \n");
81 m->errorOut(e, "ReverseSeqsCommand", "help");
86 //***************************************************************************************************************
88 ReverseSeqsCommand::~ReverseSeqsCommand(){ /* do nothing */ }
90 //***************************************************************************************************************
93 int ReverseSeqsCommand::execute(){
96 if (abort == true) { return 0; }
99 m->openInputFile(fasta, inFASTA);
102 string reverseFile = outputDir + m->getRootName(m->getSimpleName(fasta)) + "rc" + m->getExtension(fasta);
103 m->openOutputFile(reverseFile, outFASTA);
105 while(!inFASTA.eof()){
106 if (m->control_pressed) { inFASTA.close(); outFASTA.close(); remove(reverseFile.c_str()); return 0; }
108 Sequence currSeq(inFASTA); m->gobble(inFASTA);
109 if (currSeq.getName() != "") {
110 currSeq.reverseComplement();
111 currSeq.printSequence(outFASTA);
117 if (m->control_pressed) { remove(reverseFile.c_str()); return 0; }
119 m->mothurOutEndLine();
120 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
121 m->mothurOut(reverseFile); m->mothurOutEndLine();
122 m->mothurOutEndLine();
128 catch(exception& e) {
129 m->errorOut(e, "ReverseSeqsCommand", "execute");
134 //***************************************************************************************************************