]> git.donarmstrong.com Git - mothur.git/blob - chopseqscommand.cpp
initialized numLinkers and numSpacers in trim.seqs and trim.flows
[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::setParameters(){        
15         try {
16                 CommandParameter pfasta("fasta", "InputTypes", "", "", "none", "none", "none",false,true); parameters.push_back(pfasta);
17                 CommandParameter pnumbases("numbases", "Number", "", "0", "", "", "",false,true); parameters.push_back(pnumbases);
18                 CommandParameter pcountgaps("countgaps", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pcountgaps);
19                 CommandParameter pshort("short", "Boolean", "", "F", "", "", "",false,false); parameters.push_back(pshort);
20                 CommandParameter pkeep("keep", "Multiple", "front-back", "front", "", "", "",false,false); parameters.push_back(pkeep);
21                 CommandParameter pinputdir("inputdir", "String", "", "", "", "", "",false,false); parameters.push_back(pinputdir);
22                 CommandParameter poutputdir("outputdir", "String", "", "", "", "", "",false,false); parameters.push_back(poutputdir);
23                 
24                 vector<string> myArray;
25                 for (int i = 0; i < parameters.size(); i++) {   myArray.push_back(parameters[i].name);          }
26                 return myArray;
27         }
28         catch(exception& e) {
29                 m->errorOut(e, "ChopSeqsCommand", "setParameters");
30                 exit(1);
31         }
32 }
33 //**********************************************************************************************************************
34 string ChopSeqsCommand::getHelpString(){        
35         try {
36                 string helpString = "";
37                 helpString += "The chop.seqs command reads a fasta file and outputs a .chop.fasta containing the trimmed sequences. Note: If a sequence is completely 'chopped', an accnos file will be created with the names of the sequences removed. \n";
38                 helpString += "The chop.seqs command parameters are fasta, numbases, countgaps and keep. fasta is required unless you have a valid current fasta file. numbases is required.\n";
39                 helpString += "The chop.seqs command should be in the following format: chop.seqs(fasta=yourFasta, numbases=yourNum, keep=yourKeep).\n";
40                 helpString += "The numbases parameter allows you to specify the number of bases you want to keep.\n";
41                 helpString += "The keep parameter allows you to specify whether you want to keep the front or the back of your sequence, default=front.\n";
42                 helpString += "The countgaps parameter allows you to specify whether you want to count gaps as bases, default=false.\n";
43                 helpString += "The short parameter allows you to specify you want to keep sequences that are too short to chop, default=false.\n";
44                 helpString += "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";
45                 helpString += "Example chop.seqs(fasta=amazon.fasta, numbases=200, keep=front).\n";
46                 helpString += "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n";
47                 return helpString;
48         }
49         catch(exception& e) {
50                 m->errorOut(e, "ChopSeqsCommand", "getHelpString");
51                 exit(1);
52         }
53 }
54
55 //**********************************************************************************************************************
56 ChopSeqsCommand::ChopSeqsCommand(){     
57         try {
58                 abort = true; calledHelp = true; 
59                 setParameters();
60                 vector<string> tempOutNames;
61                 outputTypes["fasta"] = tempOutNames;
62                 outputTypes["accnos"] = tempOutNames;
63         }
64         catch(exception& e) {
65                 m->errorOut(e, "ChopSeqsCommand", "ChopSeqsCommand");
66                 exit(1);
67         }
68 }
69 //**********************************************************************************************************************
70 ChopSeqsCommand::ChopSeqsCommand(string option)  {
71         try {
72                 abort = false; calledHelp = false;   
73                 
74                 //allow user to run help
75                 if(option == "help") { help(); abort = true; calledHelp = true; }
76                 else if(option == "citation") { citation(); abort = true; calledHelp = true;}
77                 
78                 else {
79                         vector<string> myArray = setParameters();
80                         
81                         OptionParser parser(option);
82                         map<string,string> parameters = parser.getParameters();
83                         
84                         ValidParameters validParameter;
85                         map<string,string>::iterator it;
86                         
87                         //check to make sure all parameters are valid for command
88                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
89                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
90                         }
91                         
92                         //initialize outputTypes
93                         vector<string> tempOutNames;
94                         outputTypes["fasta"] = tempOutNames;
95                         outputTypes["accnos"] = tempOutNames;
96                 
97                         //if the user changes the input directory command factory will send this info to us in the output parameter 
98                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
99                         if (inputDir == "not found"){   inputDir = "";          }
100                         else {
101                                 string path;
102                                 it = parameters.find("fasta");
103                                 //user has given a template file
104                                 if(it != parameters.end()){ 
105                                         path = m->hasPath(it->second);
106                                         //if the user has not given a path then, add inputdir. else leave path alone.
107                                         if (path == "") {       parameters["fasta"] = inputDir + it->second;            }
108                                 }
109                         }
110
111                         //check for required parameters
112                         fastafile = validParameter.validFile(parameters, "fasta", true);
113                         if (fastafile == "not open") { abort = true; }
114                         else if (fastafile == "not found") {                            //if there is a current fasta file, use it
115                                 fastafile = m->getFastaFile(); 
116                                 if (fastafile != "") { m->mothurOut("Using " + fastafile + " as input file for the fasta parameter."); m->mothurOutEndLine(); }
117                                 else {  m->mothurOut("You have no current fastafile and the fasta parameter is required."); m->mothurOutEndLine(); abort = true; }
118                         }else { m->setFastaFile(fastafile); }   
119                         
120                         //if the user changes the output directory command factory will send this info to us in the output parameter 
121                         outputDir = validParameter.validFile(parameters, "outputdir", false);           if (outputDir == "not found"){  outputDir = m->hasPath(fastafile);      }
122                         
123                         string temp = validParameter.validFile(parameters, "numbases", false);  if (temp == "not found") { temp = "0"; } 
124                         m->mothurConvert(temp, numbases);   
125                         
126                         temp = validParameter.validFile(parameters, "countgaps", false);        if (temp == "not found") { temp = "f"; } 
127                         countGaps = m->isTrue(temp);  
128                         
129                         temp = validParameter.validFile(parameters, "short", false);    if (temp == "not found") { temp = "f"; } 
130                         Short = m->isTrue(temp);   
131                 
132                         keep = validParameter.validFile(parameters, "keep", false);             if (keep == "not found") { keep = "front"; } 
133                                 
134                         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;  }
135                 }
136
137         }
138         catch(exception& e) {
139                 m->errorOut(e, "ChopSeqsCommand", "ChopSeqsCommand");
140                 exit(1);
141         }
142 }
143 //**********************************************************************************************************************
144
145 int ChopSeqsCommand::execute(){
146         try {
147                 
148                 if (abort == true) { if (calledHelp) { return 0; }  return 2;   }
149                 
150                 string outputFileName = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "chop.fasta";
151                 string outputFileNameAccnos = outputDir + m->getRootName(m->getSimpleName(fastafile)) + "chop.accnos";
152                 
153                 ofstream out;
154                 m->openOutputFile(outputFileName, out);
155                 
156                 ofstream outAcc;
157                 m->openOutputFile(outputFileNameAccnos, outAcc);
158                 
159                 ifstream in;
160                 m->openInputFile(fastafile, in);
161                 
162                 bool wroteAccnos = false;
163                 
164                 while (!in.eof()) {
165                         
166                         Sequence seq(in);
167                         
168                         if (m->control_pressed) { outputTypes.clear(); in.close(); out.close(); outAcc.close(); m->mothurRemove(outputFileName); m->mothurRemove(outputFileNameAccnos); return 0;  }
169                         
170                         if (seq.getName() != "") {
171                                 string newSeqString = getChopped(seq);
172                                 
173                                 //output trimmed sequence
174                                 if (newSeqString != "") {
175                                         out << ">" << seq.getName() << endl << newSeqString << endl;
176                                 }else{
177                                         outAcc << seq.getName() << endl;
178                                         wroteAccnos = true;
179                                 }
180                         }
181                 }
182                 in.close();
183                 out.close();
184                 outAcc.close();
185                 
186                 m->mothurOutEndLine();
187                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
188                 m->mothurOut(outputFileName); m->mothurOutEndLine();    outputNames.push_back(outputFileName); outputTypes["fasta"].push_back(outputFileName);
189                 
190                 if (wroteAccnos) { m->mothurOut(outputFileNameAccnos); m->mothurOutEndLine(); outputNames.push_back(outputFileNameAccnos); outputTypes["accnos"].push_back(outputFileNameAccnos); }
191                 else {  m->mothurRemove(outputFileNameAccnos);  }
192                 
193                 m->mothurOutEndLine();
194                 
195                 //set fasta file as new current fastafile
196                 string current = "";
197                 itTypes = outputTypes.find("fasta");
198                 if (itTypes != outputTypes.end()) {
199                         if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setFastaFile(current); }
200                 }
201                 
202                 if (wroteAccnos) { //set accnos file as new current accnosfile
203                         itTypes = outputTypes.find("accnos");
204                         if (itTypes != outputTypes.end()) {
205                                 if ((itTypes->second).size() != 0) { current = (itTypes->second)[0]; m->setAccnosFile(current); }
206                         }
207                 }
208                 
209                 
210                 return 0;               
211         }
212
213         catch(exception& e) {
214                 m->errorOut(e, "ChopSeqsCommand", "execute");
215                 exit(1);
216         }
217 }
218 //**********************************************************************************************************************
219 string ChopSeqsCommand::getChopped(Sequence seq) {
220         try {
221                 string temp = seq.getAligned();
222                 string tempUnaligned = seq.getUnaligned();
223                 
224                 if (countGaps) {
225                         //if needed trim sequence
226                         if (keep == "front") {//you want to keep the beginning
227                                 int tempLength = temp.length();
228
229                                 if (tempLength > numbases) { //you have enough bases to remove some
230                                 
231                                         int stopSpot = 0;
232                                         int numBasesCounted = 0;
233                                         
234                                         for (int i = 0; i < temp.length(); i++) {
235                                                 //eliminate N's
236                                                 if (toupper(temp[i]) == 'N') { temp[i] = '.'; }
237                                                 
238                                                 numBasesCounted++; 
239                                                 
240                                                 if (numBasesCounted >= numbases) { stopSpot = i; break; }
241                                         }
242                                         
243                                         if (stopSpot == 0) { temp = ""; }
244                                         else {  temp = temp.substr(0, stopSpot+1);  }
245                                                         
246                                 }else { 
247                                         if (!Short) { temp = ""; } //sequence too short
248                                 }
249                         }else { //you are keeping the back
250                                 int tempLength = temp.length();
251                                 if (tempLength > numbases) { //you have enough bases to remove some
252                                         
253                                         int stopSpot = 0;
254                                         int numBasesCounted = 0;
255                                         
256                                         for (int i = (temp.length()-1); i >= 0; i--) {
257                                                 //eliminate N's
258                                                 if (toupper(temp[i]) == 'N') { temp[i] = '.'; }
259                                                 
260                                                 numBasesCounted++; 
261
262                                                 if (numBasesCounted >= numbases) { stopSpot = i; break; }
263                                         }
264                                 
265                                         if (stopSpot == 0) { temp = ""; }
266                                         else {  temp = temp.substr(stopSpot+1);  }
267                                 }else { 
268                                         if (!Short) { temp = ""; } //sequence too short
269                                 }
270                         }
271
272                 }else{
273                                 
274                         //if needed trim sequence
275                         if (keep == "front") {//you want to keep the beginning
276                                 int tempLength = tempUnaligned.length();
277
278                                 if (tempLength > numbases) { //you have enough bases to remove some
279                                         
280                                         int stopSpot = 0;
281                                         int numBasesCounted = 0;
282                                         
283                                         for (int i = 0; i < temp.length(); i++) {
284                                                 //eliminate N's
285                                                 if (toupper(temp[i]) == 'N') { 
286                                                         temp[i] = '.'; 
287                                                         tempLength--;
288                                                         if (tempLength < numbases) { stopSpot = 0; break; }
289                                                 }
290                                                 
291                                                 if(isalpha(temp[i])) { numBasesCounted++; }
292                                                 
293                                                 if (numBasesCounted >= numbases) { stopSpot = i; break; }
294                                         }
295                                         
296                                         if (stopSpot == 0) { temp = ""; }
297                                         else {  temp = temp.substr(0, stopSpot+1);  }
298                                                         
299                                 }else { 
300                                         if (!Short) { temp = ""; } //sequence too short
301                                 }                               
302                         }else { //you are keeping the back
303                                 int tempLength = tempUnaligned.length();
304                                 if (tempLength > numbases) { //you have enough bases to remove some
305                                         
306                                         int stopSpot = 0;
307                                         int numBasesCounted = 0;
308                                         
309                                         for (int i = (temp.length()-1); i >= 0; i--) {
310                                                 //eliminate N's
311                                                 if (toupper(temp[i]) == 'N') { 
312                                                         temp[i] = '.'; 
313                                                         tempLength--;
314                                                         if (tempLength < numbases) { stopSpot = 0; break; }
315                                                 }
316                                                 
317                                                 if(isalpha(temp[i])) { numBasesCounted++; }
318
319                                                 if (numBasesCounted >= numbases) { stopSpot = i; break; }
320                                         }
321                                 
322                                         if (stopSpot == 0) { temp = ""; }
323                                         else {  temp = temp.substr(stopSpot);  }
324                                 }else { 
325                                         if (!Short) { temp = ""; } //sequence too short
326                                 }
327                         }
328                 }
329                 
330                 return temp;
331         }
332         catch(exception& e) {
333                 m->errorOut(e, "ChopSeqsCommand", "getChopped");
334                 exit(1);
335         }
336 }
337 //**********************************************************************************************************************
338
339