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","short","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 abort = true; calledHelp = true;
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) {
64 abort = false; calledHelp = false;
66 //allow user to run help
67 if(option == "help") { help(); abort = true; calledHelp = true; }
70 //valid paramters for this command
71 string Array[] = {"fasta","numbases","countgaps","keep","short","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 temp = validParameter.validFile(parameters, "short", false); if (temp == "not found") { temp = "f"; }
119 Short = m->isTrue(temp);
121 keep = validParameter.validFile(parameters, "keep", false); if (keep == "not found") { keep = "front"; }
123 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; }
127 catch(exception& e) {
128 m->errorOut(e, "ChopSeqsCommand", "ChopSeqsCommand");
132 //**********************************************************************************************************************
134 void ChopSeqsCommand::help(){
136 m->mothurOut("The chop.seqs command reads a fasta file and outputs a .chop.fasta containing the trimmed sequences.\n");
137 m->mothurOut("The chop.seqs command parameters are fasta, numbases, countgaps and keep. fasta and numbases are required required.\n");
138 m->mothurOut("The chop.seqs command should be in the following format: chop.seqs(fasta=yourFasta, numbases=yourNum, keep=yourKeep).\n");
139 m->mothurOut("The numbases parameter allows you to specify the number of bases you want to keep.\n");
140 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");
141 m->mothurOut("The countgaps parameter allows you to specify whether you want to count gaps as bases, default=false.\n");
142 m->mothurOut("The short parameter allows you to specify you want to keep sequences that are too short to chop, default=false.\n");
143 m->mothurOut("For example, if you ran chop.seqs with numbases=200 and short=t, if a sequence had 100 bases mothur would keep the sequence rather than eliminate it.\n");
144 m->mothurOut("Example chop.seqs(fasta=amazon.fasta, numbases=200, keep=front).\n");
145 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
147 catch(exception& e) {
148 m->errorOut(e, "ChopSeqsCommand", "help");
153 //**********************************************************************************************************************
155 int ChopSeqsCommand::execute(){
158 if (abort == true) { if (calledHelp) { return 0; } return 2; }
160 string outputFileName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "chop.fasta";
161 string outputFileNameAccnos = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "chop.accnos";
164 m->openOutputFile(outputFileName, out);
167 m->openOutputFile(outputFileNameAccnos, outAcc);
170 m->openInputFile(fastafile, in);
172 bool wroteAccnos = false;
178 if (m->control_pressed) { outputTypes.clear(); in.close(); out.close(); outAcc.close(); remove(outputFileName.c_str()); remove(outputFileNameAccnos.c_str()); return 0; }
180 if (seq.getName() != "") {
181 string newSeqString = getChopped(seq);
183 //output trimmed sequence
184 if (newSeqString != "") {
185 out << ">" << seq.getName() << endl << newSeqString << endl;
187 outAcc << seq.getName() << endl;
196 m->mothurOutEndLine();
197 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
198 m->mothurOut(outputFileName); m->mothurOutEndLine(); outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
200 if (wroteAccnos) { m->mothurOut(outputFileNameAccnos); m->mothurOutEndLine(); outputNames.push_back(outputFileNameAccnos); outputTypes["accnos"].push_back(outputFileNameAccnos); }
201 else { remove(outputFileNameAccnos.c_str()); }
203 m->mothurOutEndLine();
208 catch(exception& e) {
209 m->errorOut(e, "ChopSeqsCommand", "execute");
213 //**********************************************************************************************************************
214 string ChopSeqsCommand::getChopped(Sequence seq) {
216 string temp = seq.getAligned();
217 string tempUnaligned = seq.getUnaligned();
220 //if needed trim sequence
221 if (keep == "front") {//you want to keep the beginning
222 int tempLength = temp.length();
224 if (tempLength > numbases) { //you have enough bases to remove some
227 int numBasesCounted = 0;
229 for (int i = 0; i < temp.length(); i++) {
231 if (toupper(temp[i]) == 'N') { temp[i] == '.'; }
235 if (numBasesCounted >= numbases) { stopSpot = i; break; }
238 if (stopSpot == 0) { temp = ""; }
239 else { temp = temp.substr(0, stopSpot); }
242 if (!Short) { temp = ""; } //sequence too short
244 }else { //you are keeping the back
245 int tempLength = temp.length();
246 if (tempLength > numbases) { //you have enough bases to remove some
249 int numBasesCounted = 0;
251 for (int i = (temp.length()-1); i >= 0; i--) {
253 if (toupper(temp[i]) == 'N') { temp[i] == '.'; }
257 if (numBasesCounted >= numbases) { stopSpot = i; break; }
260 if (stopSpot == 0) { temp = ""; }
261 else { temp = temp.substr(stopSpot+1); }
263 if (!Short) { temp = ""; } //sequence too short
269 //if needed trim sequence
270 if (keep == "front") {//you want to keep the beginning
271 int tempLength = tempUnaligned.length();
273 if (tempLength > numbases) { //you have enough bases to remove some
276 int numBasesCounted = 0;
278 for (int i = 0; i < temp.length(); i++) {
280 if (toupper(temp[i]) == 'N') {
283 if (tempLength < numbases) { stopSpot = 0; break; }
286 if(isalpha(temp[i])) { numBasesCounted++; }
288 if (numBasesCounted >= numbases) { stopSpot = i; break; }
291 if (stopSpot == 0) { temp = ""; }
292 else { temp = temp.substr(0, stopSpot); }
295 if (!Short) { temp = ""; } //sequence too short
297 }else { //you are keeping the back
298 int tempLength = tempUnaligned.length();
299 if (tempLength > numbases) { //you have enough bases to remove some
302 int numBasesCounted = 0;
304 for (int i = (temp.length()-1); i >= 0; i--) {
306 if (toupper(temp[i]) == 'N') {
309 if (tempLength < numbases) { stopSpot = 0; break; }
312 if(isalpha(temp[i])) { numBasesCounted++; }
314 if (numBasesCounted >= numbases) { stopSpot = i; break; }
317 if (stopSpot == 0) { temp = ""; }
318 else { temp = temp.substr(stopSpot+1); }
320 if (!Short) { temp = ""; } //sequence too short
327 catch(exception& e) {
328 m->errorOut(e, "ChopSeqsCommand", "getChopped");
332 //**********************************************************************************************************************