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; }
69 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
72 vector<string> myArray = setParameters();
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;
88 outputTypes["qfile"] = 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; }
103 it = parameters.find("qfile");
104 //user has given a template file
105 if(it != parameters.end()){
106 path = m->hasPath(it->second);
107 //if the user has not given a path then, add inputdir. else leave path alone.
108 if (path == "") { parameters["qfile"] = inputDir + it->second; }
112 //check for required parameters
113 fastaFileName = validParameter.validFile(parameters, "fasta", true);
114 if (fastaFileName == "not open") { abort = true; }
115 else if (fastaFileName == "not found") { fastaFileName = "";}// m->mothurOut("fasta is a required parameter for the reverse.seqs command."); m->mothurOutEndLine(); abort = true; }
116 else { m->setFastaFile(fastaFileName); }
118 qualFileName = validParameter.validFile(parameters, "qfile", true);
119 if (qualFileName == "not open") { abort = true; }
120 else if (qualFileName == "not found") { qualFileName = ""; }//m->mothurOut("fasta is a required parameter for the reverse.seqs command."); m->mothurOutEndLine(); abort = true; }
121 else { m->setQualFile(qualFileName); }
123 if((fastaFileName == "") && (qualFileName == "")){
124 fastaFileName = m->getFastaFile();
125 if (fastaFileName != "") { m->mothurOut("Using " + fastaFileName + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
127 qualFileName = m->getQualFile();
128 if (qualFileName != "") { m->mothurOut("Using " + qualFileName + " as input file for the qfile parameter."); m->mothurOutEndLine(); }
130 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();
136 //if the user changes the output directory command factory will send this info to us in the output parameter
137 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
143 catch(exception& e) {
144 m->errorOut(e, "ReverseSeqsCommand", "ReverseSeqsCommand");
148 //***************************************************************************************************************
151 int ReverseSeqsCommand::execute(){
154 if (abort == true) { if (calledHelp) { return 0; } return 2; }
156 string fastaReverseFileName;
158 if(fastaFileName != ""){
160 m->openInputFile(fastaFileName, inFASTA);
163 string tempOutputDir = outputDir;
164 if (outputDir == "") { tempOutputDir += m->hasPath(fastaFileName); } //if user entered a file with a path then preserve it
165 fastaReverseFileName = tempOutputDir + m->getRootName(m->getSimpleName(fastaFileName)) + "rc" + m->getExtension(fastaFileName);
166 m->openOutputFile(fastaReverseFileName, outFASTA);
168 while(!inFASTA.eof()){
169 if (m->control_pressed) { inFASTA.close(); outFASTA.close(); m->mothurRemove(fastaReverseFileName); return 0; }
171 Sequence currSeq(inFASTA); m->gobble(inFASTA);
172 if (currSeq.getName() != "") {
173 currSeq.reverseComplement();
174 currSeq.printSequence(outFASTA);
179 outputNames.push_back(fastaReverseFileName); outputTypes["fasta"].push_back(fastaReverseFileName);
182 string qualReverseFileName;
184 if(qualFileName != ""){
185 QualityScores currQual;
188 m->openInputFile(qualFileName, inQual);
191 string tempOutputDir = outputDir;
192 if (outputDir == "") { tempOutputDir += m->hasPath(qualFileName); } //if user entered a file with a path then preserve it
193 string qualReverseFileName = tempOutputDir + m->getRootName(m->getSimpleName(qualFileName)) + "rc" + m->getExtension(qualFileName);
194 m->openOutputFile(qualReverseFileName, outQual);
196 while(!inQual.eof()){
197 if (m->control_pressed) { inQual.close(); outQual.close(); m->mothurRemove(qualReverseFileName); return 0; }
198 currQual = QualityScores(inQual); m->gobble(inQual);
199 currQual.flipQScores();
200 currQual.printQScores(outQual);
204 outputNames.push_back(qualReverseFileName); outputTypes["qfile"].push_back(qualReverseFileName);
207 if (m->control_pressed) { m->mothurRemove(qualReverseFileName); m->mothurRemove(fastaReverseFileName); return 0; }
209 //set fasta file as new current fastafile
211 itTypes = outputTypes.find("fasta");
212 if (itTypes != outputTypes.end()) {
213 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
216 itTypes = outputTypes.find("qfile");
217 if (itTypes != outputTypes.end()) {
218 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setQualFile(current); }
222 m->mothurOutEndLine();
223 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
224 for(int i=0;i<outputNames.size();i++){
225 m->mothurOut(outputNames[i]);
226 m->mothurOutEndLine();
233 catch(exception& e) {
234 m->errorOut(e, "ReverseSeqsCommand", "execute");
239 //***************************************************************************************************************