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","fromend","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); if (temp == "not found") { temp = "0"; }
63 temp = validParameter.validFile(parameters, "fromend", false); if (temp == "not found") { temp = "0"; }
64 convert(temp, fromend);
66 if ((end == 0) && (fromend == 0)) { m->mothurOut("You must provide either end or fromend for the chops.seqs command."); m->mothurOutEndLine(); abort = true; }
67 if ((end != 0) && (fromend != 0)) { m->mothurOut("You must provide either end or fromend for the chops.seqs command, not both."); m->mothurOutEndLine(); abort = true; }
72 m->errorOut(e, "ChopSeqsCommand", "ChopSeqsCommand");
76 //**********************************************************************************************************************
78 void ChopSeqsCommand::help(){
80 m->mothurOut("The chop.seqs command reads a fasta file and outputs a .chop.fasta with sequences trimmed to the end position.\n");
81 m->mothurOut("The chop.seqs command parameters are fasta, end and fromend, fasta is required.\n");
82 m->mothurOut("The chop.seqs command should be in the following format: chop.seqs(fasta=yourFasta, end=yourEnd).\n");
83 m->mothurOut("The end parameter allows you to specify an end base position for your sequences, default = 0.\n");
84 m->mothurOut("The fromend parameter allows you to remove the last X bases from the end of the sequence, default = 0.\n");
85 m->mothurOut("Example chop.seqs(fasta=amazon.fasta, end=200).\n");
86 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
89 m->errorOut(e, "ChopSeqsCommand", "help");
94 //**********************************************************************************************************************
96 int ChopSeqsCommand::execute(){
99 if (abort == true) { return 0; }
101 string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "chop.fasta";
102 string outputFileNameAccnos = outputDir + getRootName(getSimpleName(fastafile)) + "chop.accnos";
105 openOutputFile(outputFileName, out);
108 openOutputFile(outputFileNameAccnos, outAcc);
111 openInputFile(fastafile, in);
113 bool wroteAccnos = false;
119 if (m->control_pressed) { in.close(); out.close(); outAcc.close(); remove(outputFileName.c_str()); remove(outputFileNameAccnos.c_str()); return 0; }
121 if (seq.getName() != "") {
122 string newSeqString = "";
123 if (seq.getIsAligned()) { //sequence is aligned
124 newSeqString = getChoppedAligned(seq);
126 newSeqString = getChoppedUnaligned(seq);
129 //output trimmed sequence
130 if (newSeqString != "") {
131 out << ">" << seq.getName() << endl << newSeqString << endl;
133 outAcc << seq.getName() << endl;
142 m->mothurOutEndLine();
143 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
144 m->mothurOut(outputFileName); m->mothurOutEndLine();
146 if (wroteAccnos) { m->mothurOut(outputFileNameAccnos); m->mothurOutEndLine(); }
147 else { remove(outputFileNameAccnos.c_str()); }
149 m->mothurOutEndLine();
154 catch(exception& e) {
155 m->errorOut(e, "ChopSeqsCommand", "execute");
159 //**********************************************************************************************************************
160 string ChopSeqsCommand::getChoppedUnaligned(Sequence seq) {
162 string temp = seq.getUnaligned();
164 //if needed trim sequence
166 if (temp.length() > end) { temp = temp.substr(0, end); }
168 }else { //you are using fromend
169 if (temp.length() > fromend) { temp = temp.substr(0, (temp.length()-fromend)); }
170 else { temp = ""; } //sequence too short
175 catch(exception& e) {
176 m->errorOut(e, "ChopSeqsCommand", "getChoppedUnaligned");
180 //**********************************************************************************************************************
181 string ChopSeqsCommand::getChoppedAligned(Sequence seq) {
183 string temp = seq.getAligned();
184 string tempUnaligned = seq.getUnaligned();
186 //if needed trim sequence
188 if (tempUnaligned.length() > end) { //you have enough bases to remove some
193 for (int i = 0; i < temp.length(); i++) {
194 if(isalpha(temp[i])) { numBases++; }
196 if (numBases >= end) { stopSpot = i; break; }
199 temp = temp.substr(0, stopSpot);
200 }else { temp = ""; } //sequence too short
202 }else { //you are using fromend
204 if (tempUnaligned.length() > fromend) {
209 for (int i = (temp.length()-1); i >= 0; i--) {
210 if(isalpha(temp[i])) { numBases++; }
212 if (numBases >= fromend) { stopSpot = i; break; }
215 temp = temp.substr(0, stopSpot);
217 else { temp = ""; } //sequence too short
222 catch(exception& e) {
223 m->errorOut(e, "ChopSeqsCommand", "getChoppedUnaligned");
227 //**********************************************************************************************************************