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","numbases","countgaps","keep","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, "numbases", false); if (temp == "not found") { temp = "0"; }
61 convert(temp, numbases);
63 temp = validParameter.validFile(parameters, "countgaps", false); if (temp == "not found") { temp = "f"; }
64 countGaps = isTrue(temp);
66 keep = validParameter.validFile(parameters, "keep", false); if (keep == "not found") { keep = "front"; }
68 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; }
73 m->errorOut(e, "ChopSeqsCommand", "ChopSeqsCommand");
77 //**********************************************************************************************************************
79 void ChopSeqsCommand::help(){
81 m->mothurOut("The chop.seqs command reads a fasta file and outputs a .chop.fasta containing the trimmed sequences.\n");
82 m->mothurOut("The chop.seqs command parameters are fasta, numbases, countgaps and keep. fasta and numbases are required required.\n");
83 m->mothurOut("The chop.seqs command should be in the following format: chop.seqs(fasta=yourFasta, numbases=yourNum, keep=yourKeep).\n");
84 m->mothurOut("The numbases parameter allows you to specify the number of bases you want to keep.\n");
85 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");
86 m->mothurOut("The countgaps parameter allows you to specify whether you want to count gaps as bases, default=false.\n");
87 m->mothurOut("Example chop.seqs(fasta=amazon.fasta, numbases=200, keep=front).\n");
88 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
91 m->errorOut(e, "ChopSeqsCommand", "help");
96 //**********************************************************************************************************************
98 int ChopSeqsCommand::execute(){
101 if (abort == true) { return 0; }
103 string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "chop.fasta";
104 string outputFileNameAccnos = outputDir + getRootName(getSimpleName(fastafile)) + "chop.accnos";
107 openOutputFile(outputFileName, out);
110 openOutputFile(outputFileNameAccnos, outAcc);
113 openInputFile(fastafile, in);
115 bool wroteAccnos = false;
121 if (m->control_pressed) { in.close(); out.close(); outAcc.close(); remove(outputFileName.c_str()); remove(outputFileNameAccnos.c_str()); return 0; }
123 if (seq.getName() != "") {
124 string newSeqString = getChopped(seq);
126 //output trimmed sequence
127 if (newSeqString != "") {
128 out << ">" << seq.getName() << endl << newSeqString << endl;
130 outAcc << seq.getName() << endl;
139 m->mothurOutEndLine();
140 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
141 m->mothurOut(outputFileName); m->mothurOutEndLine();
143 if (wroteAccnos) { m->mothurOut(outputFileNameAccnos); m->mothurOutEndLine(); }
144 else { remove(outputFileNameAccnos.c_str()); }
146 m->mothurOutEndLine();
151 catch(exception& e) {
152 m->errorOut(e, "ChopSeqsCommand", "execute");
156 //**********************************************************************************************************************
157 string ChopSeqsCommand::getChopped(Sequence seq) {
159 string temp = seq.getAligned();
160 string tempUnaligned = seq.getUnaligned();
163 //if needed trim sequence
164 if (keep == "front") {//you want to keep the beginning
165 int tempLength = temp.length();
167 if (tempLength > numbases) { //you have enough bases to remove some
170 int numBasesCounted = 0;
172 for (int i = 0; i < temp.length(); i++) {
174 if (toupper(temp[i]) == 'N') { temp[i] == '.'; }
178 if (numBasesCounted >= numbases) { stopSpot = i; break; }
181 if (stopSpot == 0) { temp = ""; }
182 else { temp = temp.substr(0, stopSpot); }
184 }else { temp = ""; } //sequence too short
186 }else { //you are keeping the back
187 int tempLength = temp.length();
188 if (tempLength > numbases) { //you have enough bases to remove some
191 int numBasesCounted = 0;
193 for (int i = (temp.length()-1); i >= 0; i--) {
195 if (toupper(temp[i]) == 'N') { temp[i] == '.'; }
199 if (numBasesCounted >= numbases) { stopSpot = i; break; }
202 if (stopSpot == 0) { temp = ""; }
203 else { temp = temp.substr(stopSpot+1); }
205 else { temp = ""; } //sequence too short
210 //if needed trim sequence
211 if (keep == "front") {//you want to keep the beginning
212 int tempLength = tempUnaligned.length();
214 if (tempLength > numbases) { //you have enough bases to remove some
217 int numBasesCounted = 0;
219 for (int i = 0; i < temp.length(); i++) {
221 if (toupper(temp[i]) == 'N') {
224 if (tempLength < numbases) { stopSpot = 0; break; }
227 if(isalpha(temp[i])) { numBasesCounted++; }
229 if (numBasesCounted >= numbases) { stopSpot = i; break; }
232 if (stopSpot == 0) { temp = ""; }
233 else { temp = temp.substr(0, stopSpot); }
235 }else { temp = ""; } //sequence too short
237 }else { //you are keeping the back
238 int tempLength = tempUnaligned.length();
239 if (tempLength > numbases) { //you have enough bases to remove some
242 int numBasesCounted = 0;
244 for (int i = (temp.length()-1); i >= 0; i--) {
246 if (toupper(temp[i]) == 'N') {
249 if (tempLength < numbases) { stopSpot = 0; break; }
252 if(isalpha(temp[i])) { numBasesCounted++; }
254 if (numBasesCounted >= numbases) { stopSpot = i; break; }
257 if (stopSpot == 0) { temp = ""; }
258 else { temp = temp.substr(stopSpot+1); }
260 else { temp = ""; } //sequence too short
266 catch(exception& e) {
267 m->errorOut(e, "ChopSeqsCommand", "getChopped");
271 //**********************************************************************************************************************