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(){
29 //initialize outputTypes
30 vector<string> tempOutNames;
31 outputTypes["fasta"] = tempOutNames;
34 m->errorOut(e, "DegapSeqsCommand", "DegapSeqsCommand");
38 //**********************************************************************************************************************
39 vector<string> DegapSeqsCommand::getRequiredParameters(){
41 string Array[] = {"fasta"};
42 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
46 m->errorOut(e, "DegapSeqsCommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> DegapSeqsCommand::getRequiredFiles(){
53 vector<string> myArray;
57 m->errorOut(e, "DegapSeqsCommand", "getRequiredFiles");
61 //***************************************************************************************************************
62 DegapSeqsCommand::DegapSeqsCommand(string option) {
66 //allow user to run help
67 if(option == "help") { help(); abort = 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; }
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 = ""; }
94 //check for required parameters
95 fastafile = validParameter.validFile(parameters, "fasta", false);
96 if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the degap.seqs command."); m->mothurOutEndLine(); abort = true; }
98 m->splitAtDash(fastafile, fastaFileNames);
100 //go through files and make sure they are good, if not, then disregard them
101 for (int i = 0; i < fastaFileNames.size(); i++) {
102 if (inputDir != "") {
103 string path = m->hasPath(fastaFileNames[i]);
104 //if the user has not given a path then, add inputdir. else leave path alone.
105 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
109 int ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
111 //if you can't open it, try default location
112 if (ableToOpen == 1) {
113 if (m->getDefaultPath() != "") { //default path is set
114 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
115 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
117 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
119 fastaFileNames[i] = tryPath;
123 //if you can't open it, try default location
124 if (ableToOpen == 1) {
125 if (m->getOutputDir() != "") { //default path is set
126 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
127 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
129 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
131 fastaFileNames[i] = tryPath;
137 if (ableToOpen == 1) {
138 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
139 //erase from file list
140 fastaFileNames.erase(fastaFileNames.begin()+i);
145 //make sure there is at least one valid file left
146 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
150 //if the user changes the output directory command factory will send this info to us in the output parameter
151 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
153 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it
158 catch(exception& e) {
159 m->errorOut(e, "DegapSeqsCommand", "DegapSeqsCommand");
163 //**********************************************************************************************************************
165 void DegapSeqsCommand::help(){
167 m->mothurOut("The degap.seqs command reads a fastafile and removes all gap characters.\n");
168 m->mothurOut("The degap.seqs command parameter is fasta.\n");
169 m->mothurOut("The fasta parameter allows you to enter the fasta file containing your sequences, and is required. \n");
170 m->mothurOut("You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n");
171 m->mothurOut("The degap.seqs command should be in the following format: \n");
172 m->mothurOut("degap.seqs(fasta=yourFastaFile) \n");
173 m->mothurOut("Example: degap.seqs(fasta=abrecovery.align) \n");
174 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
176 catch(exception& e) {
177 m->errorOut(e, "DegapSeqsCommand", "help");
182 //***************************************************************************************************************
184 DegapSeqsCommand::~DegapSeqsCommand(){ /* do nothing */ }
186 //***************************************************************************************************************
189 int DegapSeqsCommand::execute(){
192 if (abort == true) { return 0; }
194 for (int s = 0; s < fastaFileNames.size(); s++) {
196 m->mothurOut("Degapping sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
198 m->openInputFile(fastaFileNames[s], inFASTA);
201 string tempOutputDir = outputDir;
202 if (outputDir == "") { tempOutputDir = m->hasPath(fastaFileNames[s]); }
203 string degapFile = tempOutputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "ng.fasta";
204 m->openOutputFile(degapFile, outFASTA);
206 while(!inFASTA.eof()){
207 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; }
209 Sequence currSeq(inFASTA); m->gobble(inFASTA);
210 if (currSeq.getName() != "") {
211 outFASTA << ">" << currSeq.getName() << endl;
212 outFASTA << currSeq.getUnaligned() << endl;
218 outputNames.push_back(degapFile); outputTypes["fasta"].push_back(degapFile);
220 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; }
223 m->mothurOutEndLine();
224 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
225 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
226 m->mothurOutEndLine();
232 catch(exception& e) {
233 m->errorOut(e, "DegapSeqsCommand", "execute");
238 //***************************************************************************************************************