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 //***************************************************************************************************************
15 DegapSeqsCommand::DegapSeqsCommand(string option) {
19 //allow user to run help
20 if(option == "help") { help(); abort = true; }
23 //valid paramters for this command
24 string Array[] = {"fasta", "outputdir","inputdir"};
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27 OptionParser parser(option);
28 map<string,string> parameters = parser.getParameters();
30 ValidParameters validParameter;
31 map<string,string>::iterator it;
33 //check to make sure all parameters are valid for command
34 for (it = parameters.begin(); it != parameters.end(); it++) {
35 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
38 //if the user changes the input directory command factory will send this info to us in the output parameter
39 string inputDir = validParameter.validFile(parameters, "inputdir", false);
40 if (inputDir == "not found"){ inputDir = ""; }
42 //check for required parameters
43 fastafile = validParameter.validFile(parameters, "fasta", false);
44 if (fastafile == "not found") { fastafile = ""; m->mothurOut("fasta is a required parameter for the degap.seqs command."); m->mothurOutEndLine(); abort = true; }
46 m->splitAtDash(fastafile, fastaFileNames);
48 //go through files and make sure they are good, if not, then disregard them
49 for (int i = 0; i < fastaFileNames.size(); i++) {
51 string path = m->hasPath(fastaFileNames[i]);
52 //if the user has not given a path then, add inputdir. else leave path alone.
53 if (path == "") { fastaFileNames[i] = inputDir + fastaFileNames[i]; }
57 int ableToOpen = m->openInputFile(fastaFileNames[i], in, "noerror");
59 //if you can't open it, try default location
60 if (ableToOpen == 1) {
61 if (m->getDefaultPath() != "") { //default path is set
62 string tryPath = m->getDefaultPath() + m->getSimpleName(fastaFileNames[i]);
63 m->mothurOut("Unable to open " + fastaFileNames[i] + ". Trying default " + tryPath); m->mothurOutEndLine();
64 ableToOpen = m->openInputFile(tryPath, in, "noerror");
65 fastaFileNames[i] = tryPath;
70 if (ableToOpen == 1) {
71 m->mothurOut("Unable to open " + fastaFileNames[i] + ". It will be disregarded."); m->mothurOutEndLine();
72 //erase from file list
73 fastaFileNames.erase(fastaFileNames.begin()+i);
78 //make sure there is at least one valid file left
79 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
83 //if the user changes the output directory command factory will send this info to us in the output parameter
84 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
86 outputDir += m->hasPath(fastafile); //if user entered a file with a path then preserve it
92 m->errorOut(e, "DegapSeqsCommand", "DegapSeqsCommand");
96 //**********************************************************************************************************************
98 void DegapSeqsCommand::help(){
100 m->mothurOut("The degap.seqs command reads a fastafile and removes all gap characters.\n");
101 m->mothurOut("The degap.seqs command parameter is fasta.\n");
102 m->mothurOut("The fasta parameter allows you to enter the fasta file containing your sequences, and is required. \n");
103 m->mothurOut("You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n");
104 m->mothurOut("The degap.seqs command should be in the following format: \n");
105 m->mothurOut("degap.seqs(fasta=yourFastaFile) \n");
106 m->mothurOut("Example: degap.seqs(fasta=abrecovery.align) \n");
107 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
109 catch(exception& e) {
110 m->errorOut(e, "DegapSeqsCommand", "help");
115 //***************************************************************************************************************
117 DegapSeqsCommand::~DegapSeqsCommand(){ /* do nothing */ }
119 //***************************************************************************************************************
122 int DegapSeqsCommand::execute(){
125 if (abort == true) { return 0; }
127 for (int s = 0; s < fastaFileNames.size(); s++) {
129 m->mothurOut("Degapping sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
131 m->openInputFile(fastaFileNames[s], inFASTA);
134 string tempOutputDir = outputDir;
135 if (outputDir == "") { tempOutputDir = m->hasPath(fastaFileNames[s]); }
136 string degapFile = tempOutputDir + m->getRootName(m->getSimpleName(fastaFileNames[s])) + "ng.fasta";
137 m->openOutputFile(degapFile, outFASTA);
139 while(!inFASTA.eof()){
140 if (m->control_pressed) { inFASTA.close(); outFASTA.close(); remove(degapFile.c_str()); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } return 0; }
142 Sequence currSeq(inFASTA); m->gobble(inFASTA);
143 if (currSeq.getName() != "") {
144 outFASTA << ">" << currSeq.getName() << endl;
145 outFASTA << currSeq.getUnaligned() << endl;
151 outputNames.push_back(degapFile);
153 if (m->control_pressed) { remove(degapFile.c_str()); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } return 0; }
156 m->mothurOutEndLine();
157 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
158 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
159 m->mothurOutEndLine();
165 catch(exception& e) {
166 m->errorOut(e, "DegapSeqsCommand", "execute");
171 //***************************************************************************************************************