]> git.donarmstrong.com Git - mothur.git/blob - chopseqscommand.cpp
added [ERROR] flag if command aborts
[mothur.git] / chopseqscommand.cpp
1 /*
2  *  chopseqscommand.cpp
3  *  Mothur
4  *
5  *  Created by westcott on 5/10/10.
6  *  Copyright 2010 Schloss Lab. All rights reserved.
7  *
8  */
9
10 #include "chopseqscommand.h"
11 #include "sequence.hpp"
12
13 //**********************************************************************************************************************
14 vector<string> ChopSeqsCommand::getValidParameters(){   
15         try {
16                 string AlignArray[] =  {"fasta","short","numbases","countgaps","keep","outputdir","inputdir"};
17                 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
18                 return myArray;
19         }
20         catch(exception& e) {
21                 m->errorOut(e, "ChopSeqsCommand", "getValidParameters");
22                 exit(1);
23         }
24 }
25 //**********************************************************************************************************************
26 ChopSeqsCommand::ChopSeqsCommand(){     
27         try {
28                 abort = true; calledHelp = true; 
29                 vector<string> tempOutNames;
30                 outputTypes["fasta"] = tempOutNames;
31                 outputTypes["accnos"] = tempOutNames;
32         }
33         catch(exception& e) {
34                 m->errorOut(e, "ChopSeqsCommand", "ChopSeqsCommand");
35                 exit(1);
36         }
37 }
38 //**********************************************************************************************************************
39 vector<string> ChopSeqsCommand::getRequiredParameters(){        
40         try {
41                 string Array[] =  {"fasta","numbases"};
42                 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
43                 return myArray;
44         }
45         catch(exception& e) {
46                 m->errorOut(e, "ChopSeqsCommand", "getRequiredParameters");
47                 exit(1);
48         }
49 }
50 //**********************************************************************************************************************
51 vector<string> ChopSeqsCommand::getRequiredFiles(){     
52         try {
53                 vector<string> myArray;
54                 return myArray;
55         }
56         catch(exception& e) {
57                 m->errorOut(e, "ChopSeqsCommand", "getRequiredFiles");
58                 exit(1);
59         }
60 }
61 //**********************************************************************************************************************
62 ChopSeqsCommand::ChopSeqsCommand(string option)  {
63         try {
64                 abort = false; calledHelp = false;   
65                 
66                 //allow user to run help
67                 if(option == "help") { help(); abort = true; calledHelp = true; }
68                 
69                 else {
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)));
73                         
74                         OptionParser parser(option);
75                         map<string,string> parameters = parser.getParameters();
76                         
77                         ValidParameters validParameter;
78                         map<string,string>::iterator it;
79                         
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;  }
83                         }
84                         
85                         //initialize outputTypes
86                         vector<string> tempOutNames;
87                         outputTypes["fasta"] = tempOutNames;
88                         outputTypes["accnos"] = tempOutNames;
89                 
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 = "";          }
93                         else {
94                                 string path;
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;            }
101                                 }
102                         }
103
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; }    
108                         
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);      }
111                         
112                         string temp = validParameter.validFile(parameters, "numbases", false);  if (temp == "not found") { temp = "0"; } 
113                         convert(temp, numbases);   
114                         
115                         temp = validParameter.validFile(parameters, "countgaps", false);        if (temp == "not found") { temp = "f"; } 
116                         countGaps = m->isTrue(temp);  
117                         
118                         temp = validParameter.validFile(parameters, "short", false);    if (temp == "not found") { temp = "f"; } 
119                         Short = m->isTrue(temp);   
120                 
121                         keep = validParameter.validFile(parameters, "keep", false);             if (keep == "not found") { keep = "front"; } 
122                                 
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;  }
124                 }
125
126         }
127         catch(exception& e) {
128                 m->errorOut(e, "ChopSeqsCommand", "ChopSeqsCommand");
129                 exit(1);
130         }
131 }
132 //**********************************************************************************************************************
133
134 void ChopSeqsCommand::help(){
135         try {
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");
146         }
147         catch(exception& e) {
148                 m->errorOut(e, "ChopSeqsCommand", "help");
149                 exit(1);
150         }
151 }
152
153 //**********************************************************************************************************************
154
155 int ChopSeqsCommand::execute(){
156         try {
157                 
158                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
159                 
160                 string outputFileName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "chop.fasta";
161                 string outputFileNameAccnos = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "chop.accnos";
162                 
163                 ofstream out;
164                 m->openOutputFile(outputFileName, out);
165                 
166                 ofstream outAcc;
167                 m->openOutputFile(outputFileNameAccnos, outAcc);
168                 
169                 ifstream in;
170                 m->openInputFile(fastafile, in);
171                 
172                 bool wroteAccnos = false;
173                 
174                 while (!in.eof()) {
175                         
176                         Sequence seq(in);
177                         
178                         if (m->control_pressed) { outputTypes.clear(); in.close(); out.close(); outAcc.close(); remove(outputFileName.c_str()); remove(outputFileNameAccnos.c_str()); return 0;  }
179                         
180                         if (seq.getName() != "") {
181                                 string newSeqString = getChopped(seq);
182                                 
183                                 //output trimmed sequence
184                                 if (newSeqString != "") {
185                                         out << ">" << seq.getName() << endl << newSeqString << endl;
186                                 }else{
187                                         outAcc << seq.getName() << endl;
188                                         wroteAccnos = true;
189                                 }
190                         }
191                 }
192                 in.close();
193                 out.close();
194                 outAcc.close();
195                 
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);
199                 
200                 if (wroteAccnos) { m->mothurOut(outputFileNameAccnos); m->mothurOutEndLine(); outputNames.push_back(outputFileNameAccnos); outputTypes["accnos"].push_back(outputFileNameAccnos); }
201                 else {  remove(outputFileNameAccnos.c_str());  }
202                 
203                 m->mothurOutEndLine();
204                 
205                 return 0;               
206         }
207
208         catch(exception& e) {
209                 m->errorOut(e, "ChopSeqsCommand", "execute");
210                 exit(1);
211         }
212 }
213 //**********************************************************************************************************************
214 string ChopSeqsCommand::getChopped(Sequence seq) {
215         try {
216                 string temp = seq.getAligned();
217                 string tempUnaligned = seq.getUnaligned();
218                 
219                 if (countGaps) {
220                         //if needed trim sequence
221                         if (keep == "front") {//you want to keep the beginning
222                                 int tempLength = temp.length();
223
224                                 if (tempLength > numbases) { //you have enough bases to remove some
225                                 
226                                         int stopSpot = 0;
227                                         int numBasesCounted = 0;
228                                         
229                                         for (int i = 0; i < temp.length(); i++) {
230                                                 //eliminate N's
231                                                 if (toupper(temp[i]) == 'N') { temp[i] == '.'; }
232                                                 
233                                                 numBasesCounted++; 
234                                                 
235                                                 if (numBasesCounted >= numbases) { stopSpot = i; break; }
236                                         }
237                                         
238                                         if (stopSpot == 0) { temp = ""; }
239                                         else {  temp = temp.substr(0, stopSpot);  }
240                                                         
241                                 }else { 
242                                         if (!Short) { temp = ""; } //sequence too short
243                                 }
244                         }else { //you are keeping the back
245                                 int tempLength = temp.length();
246                                 if (tempLength > numbases) { //you have enough bases to remove some
247                                         
248                                         int stopSpot = 0;
249                                         int numBasesCounted = 0;
250                                         
251                                         for (int i = (temp.length()-1); i >= 0; i--) {
252                                                 //eliminate N's
253                                                 if (toupper(temp[i]) == 'N') { temp[i] == '.'; }
254                                                 
255                                                 numBasesCounted++; 
256
257                                                 if (numBasesCounted >= numbases) { stopSpot = i; break; }
258                                         }
259                                 
260                                         if (stopSpot == 0) { temp = ""; }
261                                         else {  temp = temp.substr(stopSpot+1);  }
262                                 }else { 
263                                         if (!Short) { temp = ""; } //sequence too short
264                                 }
265                         }
266
267                 }else{
268                                 
269                         //if needed trim sequence
270                         if (keep == "front") {//you want to keep the beginning
271                                 int tempLength = tempUnaligned.length();
272
273                                 if (tempLength > numbases) { //you have enough bases to remove some
274                                         
275                                         int stopSpot = 0;
276                                         int numBasesCounted = 0;
277                                         
278                                         for (int i = 0; i < temp.length(); i++) {
279                                                 //eliminate N's
280                                                 if (toupper(temp[i]) == 'N') { 
281                                                         temp[i] == '.'; 
282                                                         tempLength--;
283                                                         if (tempLength < numbases) { stopSpot = 0; break; }
284                                                 }
285                                                 
286                                                 if(isalpha(temp[i])) { numBasesCounted++; }
287                                                 
288                                                 if (numBasesCounted >= numbases) { stopSpot = i; break; }
289                                         }
290                                         
291                                         if (stopSpot == 0) { temp = ""; }
292                                         else {  temp = temp.substr(0, stopSpot);  }
293                                                         
294                                 }else { 
295                                         if (!Short) { temp = ""; } //sequence too short
296                                 }                               
297                         }else { //you are keeping the back
298                                 int tempLength = tempUnaligned.length();
299                                 if (tempLength > numbases) { //you have enough bases to remove some
300                                         
301                                         int stopSpot = 0;
302                                         int numBasesCounted = 0;
303                                         
304                                         for (int i = (temp.length()-1); i >= 0; i--) {
305                                                 //eliminate N's
306                                                 if (toupper(temp[i]) == 'N') { 
307                                                         temp[i] == '.'; 
308                                                         tempLength--;
309                                                         if (tempLength < numbases) { stopSpot = 0; break; }
310                                                 }
311                                                 
312                                                 if(isalpha(temp[i])) { numBasesCounted++; }
313
314                                                 if (numBasesCounted >= numbases) { stopSpot = i; break; }
315                                         }
316                                 
317                                         if (stopSpot == 0) { temp = ""; }
318                                         else {  temp = temp.substr(stopSpot+1);  }
319                                 }else { 
320                                         if (!Short) { temp = ""; } //sequence too short
321                                 }
322                         }
323                 }
324                 
325                 return temp;
326         }
327         catch(exception& e) {
328                 m->errorOut(e, "ChopSeqsCommand", "getChopped");
329                 exit(1);
330         }
331 }
332 //**********************************************************************************************************************
333
334