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","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 keep = validParameter.validFile(parameters, "keep", false); if (keep == "not found") { keep = "front"; }
65 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; }
70 m->errorOut(e, "ChopSeqsCommand", "ChopSeqsCommand");
74 //**********************************************************************************************************************
76 void ChopSeqsCommand::help(){
78 m->mothurOut("The chop.seqs command reads a fasta file and outputs a .chop.fasta containing the trimmed sequences.\n");
79 m->mothurOut("The chop.seqs command parameters are fasta, numbases, and keep. fasta and numbases are required required.\n");
80 m->mothurOut("The chop.seqs command should be in the following format: chop.seqs(fasta=yourFasta, numbases=yourNum, keep=yourKeep).\n");
81 m->mothurOut("The numbases parameter allows you to specify the number of bases you want to keep.\n");
82 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");
83 m->mothurOut("Example chop.seqs(fasta=amazon.fasta, numbases=200, keep=front).\n");
84 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
87 m->errorOut(e, "ChopSeqsCommand", "help");
92 //**********************************************************************************************************************
94 int ChopSeqsCommand::execute(){
97 if (abort == true) { return 0; }
99 string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "chop.fasta";
100 string outputFileNameAccnos = outputDir + getRootName(getSimpleName(fastafile)) + "chop.accnos";
103 openOutputFile(outputFileName, out);
106 openOutputFile(outputFileNameAccnos, outAcc);
109 openInputFile(fastafile, in);
111 bool wroteAccnos = false;
117 if (m->control_pressed) { in.close(); out.close(); outAcc.close(); remove(outputFileName.c_str()); remove(outputFileNameAccnos.c_str()); return 0; }
119 if (seq.getName() != "") {
120 string newSeqString = getChopped(seq);
122 //output trimmed sequence
123 if (newSeqString != "") {
124 out << ">" << seq.getName() << endl << newSeqString << endl;
126 outAcc << seq.getName() << endl;
135 m->mothurOutEndLine();
136 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
137 m->mothurOut(outputFileName); m->mothurOutEndLine();
139 if (wroteAccnos) { m->mothurOut(outputFileNameAccnos); m->mothurOutEndLine(); }
140 else { remove(outputFileNameAccnos.c_str()); }
142 m->mothurOutEndLine();
147 catch(exception& e) {
148 m->errorOut(e, "ChopSeqsCommand", "execute");
152 //**********************************************************************************************************************
153 string ChopSeqsCommand::getChopped(Sequence seq) {
155 string temp = seq.getAligned();
156 string tempUnaligned = seq.getUnaligned();
158 //if needed trim sequence
159 if (keep == "front") {//you want to keep the beginning
160 int tempLength = tempUnaligned.length();
162 if (tempLength > numbases) { //you have enough bases to remove some
165 int numBasesCounted = 0;
167 for (int i = 0; i < temp.length(); i++) {
169 if (toupper(temp[i]) == 'N') {
172 if (tempLength < numbases) { stopSpot = 0; break; }
175 if(isalpha(temp[i])) { numBasesCounted++; }
177 if (numBasesCounted >= numbases) { stopSpot = i; break; }
180 if (stopSpot == 0) { temp = ""; }
181 else { temp = temp.substr(0, stopSpot); }
183 }else { temp = ""; } //sequence too short
185 }else { //you are keeping the back
186 int tempLength = tempUnaligned.length();
187 if (tempLength > numbases) { //you have enough bases to remove some
190 int numBasesCounted = 0;
192 for (int i = (temp.length()-1); i >= 0; i--) {
194 if (toupper(temp[i]) == 'N') {
197 if (tempLength < numbases) { stopSpot = 0; break; }
200 if(isalpha(temp[i])) { numBasesCounted++; }
202 if (numBasesCounted >= numbases) { stopSpot = i; break; }
205 if (stopSpot == 0) { temp = ""; }
206 else { temp = temp.substr(stopSpot+1); }
208 else { temp = ""; } //sequence too short
213 catch(exception& e) {
214 m->errorOut(e, "ChopSeqsCommand", "getChopped");
218 //**********************************************************************************************************************