5 * Created by westcott on 5/10/10.
6 * Copyright 2010 Schloss Lab. All rights reserved.
10 #include "chopseqscommand.h"
11 #include "sequence.hpp"
13 //**********************************************************************************************************************
15 ChopSeqsCommand::ChopSeqsCommand(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","end","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 (map<string,string>::iterator 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 output directory command factory will send this info to us in the output parameter
39 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = ""; }
41 //if the user changes the input directory command factory will send this info to us in the output parameter
42 string inputDir = validParameter.validFile(parameters, "inputdir", false);
43 if (inputDir == "not found"){ inputDir = ""; }
46 it = parameters.find("fasta");
47 //user has given a template file
48 if(it != parameters.end()){
49 path = hasPath(it->second);
50 //if the user has not given a path then, add inputdir. else leave path alone.
51 if (path == "") { parameters["fasta"] = inputDir + it->second; }
55 //check for required parameters
56 fastafile = validParameter.validFile(parameters, "fasta", true);
57 if (fastafile == "not open") { abort = true; }
58 else if (fastafile == "not found") { m->mothurOut("You must provide a fasta file."); m->mothurOutEndLine(); abort = true; }
60 string temp = validParameter.validFile(parameters, "end", false);
61 if (temp == "not found") { m->mothurOut("You must provide an end for the chops.seqs command."); m->mothurOutEndLine(); abort = true; }
64 if (end < 0) { m->mothurOut("End must be positive."); m->mothurOutEndLine(); abort = true; }
71 m->errorOut(e, "ChopSeqsCommand", "ChopSeqsCommand");
75 //**********************************************************************************************************************
77 void ChopSeqsCommand::help(){
79 m->mothurOut("The chop.seqs command reads a fasta file and outputs a .chop.fasta with sequences trimmed to the end position.\n");
80 m->mothurOut("The chop.seqs command parameters are fasta and end, both are required.\n");
81 m->mothurOut("The chop.seqs command should be in the following format: chop.seqs(fasta=yourFasta, end=yourEnd).\n");
82 m->mothurOut("Example chop.seqs(fasta=amazon.fasta, end=200).\n");
83 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
86 m->errorOut(e, "ChopSeqsCommand", "help");
91 //**********************************************************************************************************************
93 int ChopSeqsCommand::execute(){
96 if (abort == true) { return 0; }
98 string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "chop.fasta";
101 openOutputFile(outputFileName, out);
104 openInputFile(fastafile, in);
108 Sequence seq(in, "no align");
110 if (m->control_pressed) { in.close(); out.close(); remove(outputFileName.c_str()); return 0; }
112 if (seq.getName() != "") {
113 string temp = seq.getUnaligned();
115 //output sequence name
116 out << ">" << seq.getName() << endl;
118 //if needed trim sequence
119 if (temp.length() > end) { temp = temp.substr(0, end); }
121 //output trimmed sequence
128 m->mothurOutEndLine();
129 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
130 m->mothurOut(outputFileName); m->mothurOutEndLine();
131 m->mothurOutEndLine();
136 catch(exception& e) {
137 m->errorOut(e, "ChopSeqsCommand", "execute");
142 //**********************************************************************************************************************