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 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 = 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 = openInputFile(fastaFileNames[i], in);
60 if (ableToOpen == 1) {
61 m->mothurOut(fastaFileNames[i] + " will be disregarded."); m->mothurOutEndLine();
62 //erase from file list
63 fastaFileNames.erase(fastaFileNames.begin()+i);
68 //make sure there is at least one valid file left
69 if (fastaFileNames.size() == 0) { m->mothurOut("no valid files."); m->mothurOutEndLine(); abort = true; }
73 //if the user changes the output directory command factory will send this info to us in the output parameter
74 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
76 outputDir += hasPath(fastafile); //if user entered a file with a path then preserve it
82 m->errorOut(e, "DegapSeqsCommand", "DegapSeqsCommand");
86 //**********************************************************************************************************************
88 void DegapSeqsCommand::help(){
90 m->mothurOut("The degap.seqs command reads a fastafile and removes all gap characters.\n");
91 m->mothurOut("The degap.seqs command parameter is fasta.\n");
92 m->mothurOut("The fasta parameter allows you to enter the fasta file containing your sequences, and is required. \n");
93 m->mothurOut("You may enter multiple fasta files by separating their names with dashes. ie. fasta=abrecovery.fasta-amzon.fasta \n");
94 m->mothurOut("The degap.seqs command should be in the following format: \n");
95 m->mothurOut("degap.seqs(fasta=yourFastaFile) \n");
96 m->mothurOut("Example: degap.seqs(fasta=abrecovery.align) \n");
97 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFastaFile).\n\n");
100 m->errorOut(e, "DegapSeqsCommand", "help");
105 //***************************************************************************************************************
107 DegapSeqsCommand::~DegapSeqsCommand(){ /* do nothing */ }
109 //***************************************************************************************************************
112 int DegapSeqsCommand::execute(){
115 if (abort == true) { return 0; }
117 for (int s = 0; s < fastaFileNames.size(); s++) {
119 m->mothurOut("Degapping sequences from " + fastaFileNames[s] + " ..." ); m->mothurOutEndLine();
121 openInputFile(fastaFileNames[s], inFASTA);
124 string degapFile = outputDir + getRootName(getSimpleName(fastaFileNames[s])) + "ng.fasta";
125 openOutputFile(degapFile, outFASTA);
127 while(!inFASTA.eof()){
128 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; }
130 Sequence currSeq(inFASTA); gobble(inFASTA);
131 if (currSeq.getName() != "") {
132 outFASTA << ">" << currSeq.getName() << endl;
133 outFASTA << currSeq.getUnaligned() << endl;
139 outputNames.push_back(degapFile);
141 if (m->control_pressed) { remove(degapFile.c_str()); for (int j = 0; j < outputNames.size(); j++) { remove(outputNames[j].c_str()); } return 0; }
144 m->mothurOutEndLine();
145 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
146 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
147 m->mothurOutEndLine();
153 catch(exception& e) {
154 m->errorOut(e, "DegapSeqsCommand", "execute");
159 //***************************************************************************************************************