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 //**********************************************************************************************************************
14 vector<string> ChopSeqsCommand::getValidParameters(){
16 string AlignArray[] = {"fasta","numbases","countgaps","keep","outputdir","inputdir"};
17 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
21 m->errorOut(e, "ChopSeqsCommand", "getValidParameters");
25 //**********************************************************************************************************************
26 ChopSeqsCommand::ChopSeqsCommand(){
28 //initialize outputTypes
29 vector<string> tempOutNames;
30 outputTypes["fasta"] = tempOutNames;
31 outputTypes["accnos"] = tempOutNames;
34 m->errorOut(e, "ChopSeqsCommand", "ChopSeqsCommand");
38 //**********************************************************************************************************************
39 vector<string> ChopSeqsCommand::getRequiredParameters(){
41 string Array[] = {"fasta","numbases"};
42 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
46 m->errorOut(e, "ChopSeqsCommand", "getRequiredParameters");
50 //**********************************************************************************************************************
51 vector<string> ChopSeqsCommand::getRequiredFiles(){
53 vector<string> myArray;
57 m->errorOut(e, "ChopSeqsCommand", "getRequiredFiles");
61 //**********************************************************************************************************************
62 ChopSeqsCommand::ChopSeqsCommand(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","numbases","countgaps","keep","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 (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) {
82 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
85 //initialize outputTypes
86 vector<string> tempOutNames;
87 outputTypes["fasta"] = tempOutNames;
88 outputTypes["accnos"] = 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 = ""; }
95 it = parameters.find("fasta");
96 //user has given a template file
97 if(it != parameters.end()){
98 path = m->hasPath(it->second);
99 //if the user has not given a path then, add inputdir. else leave path alone.
100 if (path == "") { parameters["fasta"] = inputDir + it->second; }
104 //check for required parameters
105 fastafile = validParameter.validFile(parameters, "fasta", true);
106 if (fastafile == "not open") { abort = true; }
107 else if (fastafile == "not found") { m->mothurOut("You must provide a fasta file."); m->mothurOutEndLine(); abort = true; }
109 //if the user changes the output directory command factory will send this info to us in the output parameter
110 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){ outputDir = m->hasPath(fastafile); }
112 string temp = validParameter.validFile(parameters, "numbases", false); if (temp == "not found") { temp = "0"; }
113 convert(temp, numbases);
115 temp = validParameter.validFile(parameters, "countgaps", false); if (temp == "not found") { temp = "f"; }
116 countGaps = m->isTrue(temp);
118 keep = validParameter.validFile(parameters, "keep", false); if (keep == "not found") { keep = "front"; }
120 if (numbases == 0) { m->mothurOut("You must provide the number of bases you want to keep for the chops.seqs command."); m->mothurOutEndLine(); abort = true; }
124 catch(exception& e) {
125 m->errorOut(e, "ChopSeqsCommand", "ChopSeqsCommand");
129 //**********************************************************************************************************************
131 void ChopSeqsCommand::help(){
133 m->mothurOut("The chop.seqs command reads a fasta file and outputs a .chop.fasta containing the trimmed sequences.\n");
134 m->mothurOut("The chop.seqs command parameters are fasta, numbases, countgaps and keep. fasta and numbases are required required.\n");
135 m->mothurOut("The chop.seqs command should be in the following format: chop.seqs(fasta=yourFasta, numbases=yourNum, keep=yourKeep).\n");
136 m->mothurOut("The numbases parameter allows you to specify the number of bases you want to keep.\n");
137 m->mothurOut("The keep parameter allows you to specify whether you want to keep the front or the back of your sequence, default=front.\n");
138 m->mothurOut("The countgaps parameter allows you to specify whether you want to count gaps as bases, default=false.\n");
139 m->mothurOut("Example chop.seqs(fasta=amazon.fasta, numbases=200, keep=front).\n");
140 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
142 catch(exception& e) {
143 m->errorOut(e, "ChopSeqsCommand", "help");
148 //**********************************************************************************************************************
150 int ChopSeqsCommand::execute(){
153 if (abort == true) { return 0; }
155 string outputFileName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "chop.fasta";
156 string outputFileNameAccnos = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "chop.accnos";
159 m->openOutputFile(outputFileName, out);
162 m->openOutputFile(outputFileNameAccnos, outAcc);
165 m->openInputFile(fastafile, in);
167 bool wroteAccnos = false;
173 if (m->control_pressed) { outputTypes.clear(); in.close(); out.close(); outAcc.close(); remove(outputFileName.c_str()); remove(outputFileNameAccnos.c_str()); return 0; }
175 if (seq.getName() != "") {
176 string newSeqString = getChopped(seq);
178 //output trimmed sequence
179 if (newSeqString != "") {
180 out << ">" << seq.getName() << endl << newSeqString << endl;
182 outAcc << seq.getName() << endl;
191 m->mothurOutEndLine();
192 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
193 m->mothurOut(outputFileName); m->mothurOutEndLine(); outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
195 if (wroteAccnos) { m->mothurOut(outputFileNameAccnos); m->mothurOutEndLine(); outputNames.push_back(outputFileNameAccnos); outputTypes["accnos"].push_back(outputFileNameAccnos); }
196 else { remove(outputFileNameAccnos.c_str()); }
198 m->mothurOutEndLine();
203 catch(exception& e) {
204 m->errorOut(e, "ChopSeqsCommand", "execute");
208 //**********************************************************************************************************************
209 string ChopSeqsCommand::getChopped(Sequence seq) {
211 string temp = seq.getAligned();
212 string tempUnaligned = seq.getUnaligned();
215 //if needed trim sequence
216 if (keep == "front") {//you want to keep the beginning
217 int tempLength = temp.length();
219 if (tempLength > numbases) { //you have enough bases to remove some
222 int numBasesCounted = 0;
224 for (int i = 0; i < temp.length(); i++) {
226 if (toupper(temp[i]) == 'N') { temp[i] == '.'; }
230 if (numBasesCounted >= numbases) { stopSpot = i; break; }
233 if (stopSpot == 0) { temp = ""; }
234 else { temp = temp.substr(0, stopSpot); }
236 }else { temp = ""; } //sequence too short
238 }else { //you are keeping the back
239 int tempLength = temp.length();
240 if (tempLength > numbases) { //you have enough bases to remove some
243 int numBasesCounted = 0;
245 for (int i = (temp.length()-1); i >= 0; i--) {
247 if (toupper(temp[i]) == 'N') { temp[i] == '.'; }
251 if (numBasesCounted >= numbases) { stopSpot = i; break; }
254 if (stopSpot == 0) { temp = ""; }
255 else { temp = temp.substr(stopSpot+1); }
257 else { temp = ""; } //sequence too short
262 //if needed trim sequence
263 if (keep == "front") {//you want to keep the beginning
264 int tempLength = tempUnaligned.length();
266 if (tempLength > numbases) { //you have enough bases to remove some
269 int numBasesCounted = 0;
271 for (int i = 0; i < temp.length(); i++) {
273 if (toupper(temp[i]) == 'N') {
276 if (tempLength < numbases) { stopSpot = 0; break; }
279 if(isalpha(temp[i])) { numBasesCounted++; }
281 if (numBasesCounted >= numbases) { stopSpot = i; break; }
284 if (stopSpot == 0) { temp = ""; }
285 else { temp = temp.substr(0, stopSpot); }
287 }else { temp = ""; } //sequence too short
289 }else { //you are keeping the back
290 int tempLength = tempUnaligned.length();
291 if (tempLength > numbases) { //you have enough bases to remove some
294 int numBasesCounted = 0;
296 for (int i = (temp.length()-1); i >= 0; i--) {
298 if (toupper(temp[i]) == 'N') {
301 if (tempLength < numbases) { stopSpot = 0; break; }
304 if(isalpha(temp[i])) { numBasesCounted++; }
306 if (numBasesCounted >= numbases) { stopSpot = i; break; }
309 if (stopSpot == 0) { temp = ""; }
310 else { temp = temp.substr(stopSpot+1); }
312 else { temp = ""; } //sequence too short
318 catch(exception& e) {
319 m->errorOut(e, "ChopSeqsCommand", "getChopped");
323 //**********************************************************************************************************************