5 * Created by westcott on 6/21/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "degapseqscommand.h"
11 #include "sequence.hpp"
13 //**********************************************************************************************************************
14 vector<string> DegapSeqsCommand::getValidParameters(){
16 string Array[] = {"fasta", "outputdir","inputdir"};
17 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
21 m->errorOut(e, "DegapSeqsCommand", "getValidParameters");
25 //**********************************************************************************************************************
26 DegapSeqsCommand::DegapSeqsCommand(){
28 abort = true; calledHelp = true;
29 vector<string> tempOutNames;
30 outputTypes["fasta"] = tempOutNames;
33 m->errorOut(e, "DegapSeqsCommand", "DegapSeqsCommand");
37 //**********************************************************************************************************************
38 vector<string> DegapSeqsCommand::getRequiredParameters(){
40 string Array[] = {"fasta"};
41 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
45 m->errorOut(e, "DegapSeqsCommand", "getRequiredParameters");
49 //**********************************************************************************************************************
50 vector<string> DegapSeqsCommand::getRequiredFiles(){
52 vector<string> myArray;
56 m->errorOut(e, "DegapSeqsCommand", "getRequiredFiles");
60 //***************************************************************************************************************
61 DegapSeqsCommand::DegapSeqsCommand(string option) {
63 abort = false; calledHelp = false;
65 //allow user to run help
66 if(option == "help") { help(); abort = true; calledHelp = true; }
69 //valid paramters for this command
70 string Array[] = {"fasta", "outputdir","inputdir"};
71 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
73 OptionParser parser(option);
74 map<string,string> parameters = parser.getParameters();
76 ValidParameters validParameter;
77 map<string,string>::iterator it;
79 //check to make sure all parameters are valid for command
80 for (it = parameters.begin(); it != parameters.end(); it++) {
81 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 = ""; }
93 //check for required parameters
94 fastafile = validParameter.validFile(parameters, "fasta", false);
95 if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the degap.seqs command."); m->mothurOutEndLine(); abort = true; }
97 m->splitAtDash(fastafile, fastaFileNames);
99 //go through files and make sure they are good, if not, then disregard them
100 for (int i = 0; i < fastaFileNames.size(); i++) {
101 if (inputDir != "") {
102 string path = m->hasPath(fastaFileNames[i]);
103 //if the user has not given a path then, add inputdir. else leave path alone.
104 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
108 int ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
110 //if you can't open it, try default location
111 if (ableToOpen == 1) {
112 if (m->getDefaultPath() != "") { //default path is set
113 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
114 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
116 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
118 fastaFileNames[i] = tryPath;
122 //if you can't open it, try default location
123 if (ableToOpen == 1) {
124 if (m->getOutputDir() != "") { //default path is set
125 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
126 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
128 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
130 fastaFileNames[i] = tryPath;
136 if (ableToOpen == 1) {
137 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
138 //erase from file list
139 fastaFileNames.erase(fastaFileNames.begin()+i);
144 //make sure there is at least one valid file left
145 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
149 //if the user changes the output directory command factory will send this info to us in the output parameter
150 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
152 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it
157 catch(exception& e) {
158 m->errorOut(e, "DegapSeqsCommand", "DegapSeqsCommand");
162 //**********************************************************************************************************************
164 void DegapSeqsCommand::help(){
166 m->mothurOut("The degap.seqs command reads a fastafile and removes all gap characters.\n");
167 m->mothurOut("The degap.seqs command parameter is fasta.\n");
168 m->mothurOut("The fasta parameter allows you to enter the fasta file containing your sequences, and is required. \n");
169 m->mothurOut("You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n");
170 m->mothurOut("The degap.seqs command should be in the following format: \n");
171 m->mothurOut("degap.seqs(fasta=yourFastaFile) \n");
172 m->mothurOut("Example: degap.seqs(fasta=abrecovery.align) \n");
173 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
175 catch(exception& e) {
176 m->errorOut(e, "DegapSeqsCommand", "help");
181 //***************************************************************************************************************
183 DegapSeqsCommand::~DegapSeqsCommand(){ /* do nothing */ }
185 //***************************************************************************************************************
188 int DegapSeqsCommand::execute(){
191 if (abort == true) { if (calledHelp) { return 0; } return 2; }
193 for (int s = 0; s < fastaFileNames.size(); s++) {
195 m->mothurOut("Degapping sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
197 m->openInputFile(fastaFileNames[s], inFASTA);
200 string tempOutputDir = outputDir;
201 if (outputDir == "") { tempOutputDir = m->hasPath(fastaFileNames[s]); }
202 string degapFile = tempOutputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "ng.fasta";
203 m->openOutputFile(degapFile, outFASTA);
205 while(!inFASTA.eof()){
206 if (m->control_pressed) { outputTypes.clear(); inFASTA.close(); outFASTA.close(); remove(degapFile.c_str()); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } return 0; }
208 Sequence currSeq(inFASTA); m->gobble(inFASTA);
209 if (currSeq.getName() != "") {
210 outFASTA << ">" << currSeq.getName() << endl;
211 outFASTA << currSeq.getUnaligned() << endl;
217 outputNames.push_back(degapFile); outputTypes["fasta"].push_back(degapFile);
219 if (m->control_pressed) { outputTypes.clear(); remove(degapFile.c_str()); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } return 0; }
222 m->mothurOutEndLine();
223 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
224 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
225 m->mothurOutEndLine();
231 catch(exception& e) {
232 m->errorOut(e, "DegapSeqsCommand", "execute");
237 //***************************************************************************************************************