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::setParameters(){
16 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
17 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
18 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
20 vector<string> myArray;
21 for (int i = 0; i < parameters.size(); i++) { myArray.push_back(parameters[i].name); }
25 m->errorOut(e, "DegapSeqsCommand", "setParameters");
29 //**********************************************************************************************************************
30 string DegapSeqsCommand::getHelpString(){
32 string helpString = "";
33 helpString += "The degap.seqs command reads a fastafile and removes all gap characters.\n";
34 helpString += "The degap.seqs command parameter is fasta.\n";
35 helpString += "The fasta parameter allows you to enter the fasta file containing your sequences, and is required unless you have a valid current fasta file. \n";
36 helpString += "You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n";
37 helpString += "The degap.seqs command should be in the following format: \n";
38 helpString += "degap.seqs(fasta=yourFastaFile) \n";
39 helpString += "Example: degap.seqs(fasta=abrecovery.align) \n";
40 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n";
44 m->errorOut(e, "DegapSeqsCommand", "getHelpString");
48 //**********************************************************************************************************************
49 DegapSeqsCommand::DegapSeqsCommand(){
51 abort = true; calledHelp = true;
53 vector<string> tempOutNames;
54 outputTypes["fasta"] = tempOutNames;
57 m->errorOut(e, "DegapSeqsCommand", "DegapSeqsCommand");
61 //***************************************************************************************************************
62 DegapSeqsCommand::DegapSeqsCommand(string option) {
64 abort = false; calledHelp = false;
66 //allow user to run help
67 if(option == "help") { help(); abort = true; calledHelp = true; }
70 vector<string> myArray = setParameters();
72 OptionParser parser(option);
73 map<string,string> parameters = parser.getParameters();
75 ValidParameters validParameter;
76 map<string,string>::iterator it;
78 //check to make sure all parameters are valid for command
79 for (it = parameters.begin(); it != parameters.end(); it++) {
80 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
84 //initialize outputTypes
85 vector<string> tempOutNames;
86 outputTypes["fasta"] = tempOutNames;
88 //if the user changes the input directory command factory will send this info to us in the output parameter
89 string inputDir = validParameter.validFile(parameters, "inputdir", false);
90 if (inputDir == "not found"){ inputDir = ""; }
92 //check for required parameters
93 fastafile = validParameter.validFile(parameters, "fasta", false);
94 if (fastafile == "not found") {
95 fastafile = m->getFastaFile();
96 if (fastafile != "") { fastaFileNames.push_back(fastafile); m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
97 else { m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
100 m->splitAtDash(fastafile, fastaFileNames);
102 //go through files and make sure they are good, if not, then disregard them
103 for (int i = 0; i < fastaFileNames.size(); i++) {
106 if (fastaFileNames[i] == "current") {
107 fastaFileNames[i] = m->getFastaFile();
108 if (fastaFileNames[i] != "") { m->mothurOut("Using " + fastaFileNames[i] + " as input file for the fasta parameter where you had given current."); m->mothurOutEndLine(); }
110 m->mothurOut("You have no current fastafile, ignoring current."); m->mothurOutEndLine(); ignore=true;
111 //erase from file list
112 fastaFileNames.erase(fastaFileNames.begin()+i);
118 if (inputDir != "") {
119 string path = m->hasPath(fastaFileNames[i]);
120 //if the user has not given a path then, add inputdir. else leave path alone.
121 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
125 int ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
127 //if you can't open it, try default location
128 if (ableToOpen == 1) {
129 if (m->getDefaultPath() != "") { //default path is set
130 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
131 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
133 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
135 fastaFileNames[i] = tryPath;
139 //if you can't open it, try default location
140 if (ableToOpen == 1) {
141 if (m->getOutputDir() != "") { //default path is set
142 string tryPath = m->getOutputDir() + m->getSimpleName(fastaFileNames[i]);
143 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying output directory " + tryPath); m->mothurOutEndLine();
145 ableToOpen = m->openInputFile(tryPath, in2, "noerror");
147 fastaFileNames[i] = tryPath;
153 if (ableToOpen == 1) {
154 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
155 //erase from file list
156 fastaFileNames.erase(fastaFileNames.begin()+i);
162 //make sure there is at least one valid file left
163 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
167 //if the user changes the output directory command factory will send this info to us in the output parameter
168 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
170 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it
175 catch(exception& e) {
176 m->errorOut(e, "DegapSeqsCommand", "DegapSeqsCommand");
180 //***************************************************************************************************************
181 int DegapSeqsCommand::execute(){
184 if (abort == true) { if (calledHelp) { return 0; } return 2; }
186 for (int s = 0; s < fastaFileNames.size(); s++) {
188 m->mothurOut("Degapping sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
190 m->openInputFile(fastaFileNames[s], inFASTA);
193 string tempOutputDir = outputDir;
194 if (outputDir == "") { tempOutputDir = m->hasPath(fastaFileNames[s]); }
195 string degapFile = tempOutputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "ng.fasta";
196 m->openOutputFile(degapFile, outFASTA);
198 while(!inFASTA.eof()){
199 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; }
201 Sequence currSeq(inFASTA); m->gobble(inFASTA);
202 if (currSeq.getName() != "") {
203 outFASTA << ">" << currSeq.getName() << endl;
204 outFASTA << currSeq.getUnaligned() << endl;
210 outputNames.push_back(degapFile); outputTypes["fasta"].push_back(degapFile);
212 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; }
215 //set fasta file as new current fastafile
217 itTypes = outputTypes.find("fasta");
218 if (itTypes != outputTypes.end()) {
219 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
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 //***************************************************************************************************************