]> git.donarmstrong.com Git - mothur.git/blob - chopseqscommand.cpp
added countgaps parameter to chop.seqs command
[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
15 ChopSeqsCommand::ChopSeqsCommand(string option)  {
16         try {
17                 abort = false;
18                 
19                 //allow user to run help
20                 if(option == "help") { help(); abort = true; }
21                 
22                 else {
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)));
26                         
27                         OptionParser parser(option);
28                         map<string,string> parameters = parser.getParameters();
29                         
30                         ValidParameters validParameter;
31                         map<string,string>::iterator it;
32                         
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;  }
36                         }
37                         
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 = "";         }
40                         
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 = "";          }
44                         else {
45                                 string path;
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;            }
52                                 }
53                         }
54
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; }    
59                         
60                         string temp = validParameter.validFile(parameters, "numbases", false);  if (temp == "not found") { temp = "0"; } 
61                         convert(temp, numbases);   
62                         
63                         temp = validParameter.validFile(parameters, "countgaps", false);        if (temp == "not found") { temp = "f"; } 
64                         countGaps = isTrue(temp);   
65                 
66                         keep = validParameter.validFile(parameters, "keep", false);             if (keep == "not found") { keep = "front"; } 
67                                 
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;  }
69                 }
70
71         }
72         catch(exception& e) {
73                 m->errorOut(e, "ChopSeqsCommand", "ChopSeqsCommand");
74                 exit(1);
75         }
76 }
77 //**********************************************************************************************************************
78
79 void ChopSeqsCommand::help(){
80         try {
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");
89         }
90         catch(exception& e) {
91                 m->errorOut(e, "ChopSeqsCommand", "help");
92                 exit(1);
93         }
94 }
95
96 //**********************************************************************************************************************
97
98 int ChopSeqsCommand::execute(){
99         try {
100                 
101                 if (abort == true) { return 0; }
102                 
103                 string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "chop.fasta";
104                 string outputFileNameAccnos = outputDir + getRootName(getSimpleName(fastafile)) + "chop.accnos";
105                 
106                 ofstream out;
107                 openOutputFile(outputFileName, out);
108                 
109                 ofstream outAcc;
110                 openOutputFile(outputFileNameAccnos, outAcc);
111                 
112                 ifstream in;
113                 openInputFile(fastafile, in);
114                 
115                 bool wroteAccnos = false;
116                 
117                 while (!in.eof()) {
118                         
119                         Sequence seq(in);
120                         
121                         if (m->control_pressed) { in.close(); out.close(); outAcc.close(); remove(outputFileName.c_str()); remove(outputFileNameAccnos.c_str()); return 0;  }
122                         
123                         if (seq.getName() != "") {
124                                 string newSeqString = getChopped(seq);
125                                 
126                                 //output trimmed sequence
127                                 if (newSeqString != "") {
128                                         out << ">" << seq.getName() << endl << newSeqString << endl;
129                                 }else{
130                                         outAcc << seq.getName() << endl;
131                                         wroteAccnos = true;
132                                 }
133                         }
134                 }
135                 in.close();
136                 out.close();
137                 outAcc.close();
138                 
139                 m->mothurOutEndLine();
140                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
141                 m->mothurOut(outputFileName); m->mothurOutEndLine();    
142                 
143                 if (wroteAccnos) { m->mothurOut(outputFileNameAccnos); m->mothurOutEndLine();  }
144                 else {  remove(outputFileNameAccnos.c_str());  }
145                 
146                 m->mothurOutEndLine();
147                 
148                 return 0;               
149         }
150
151         catch(exception& e) {
152                 m->errorOut(e, "ChopSeqsCommand", "execute");
153                 exit(1);
154         }
155 }
156 //**********************************************************************************************************************
157 string ChopSeqsCommand::getChopped(Sequence seq) {
158         try {
159                 string temp = seq.getAligned();
160                 string tempUnaligned = seq.getUnaligned();
161                 
162                 if (countGaps) {
163                         //if needed trim sequence
164                         if (keep == "front") {//you want to keep the beginning
165                                 int tempLength = temp.length();
166
167                                 if (tempLength > numbases) { //you have enough bases to remove some
168                                 
169                                         int stopSpot = 0;
170                                         int numBasesCounted = 0;
171                                         
172                                         for (int i = 0; i < temp.length(); i++) {
173                                                 //eliminate N's
174                                                 if (toupper(temp[i]) == 'N') { temp[i] == '.'; }
175                                                 
176                                                 numBasesCounted++; 
177                                                 
178                                                 if (numBasesCounted >= numbases) { stopSpot = i; break; }
179                                         }
180                                         
181                                         if (stopSpot == 0) { temp = ""; }
182                                         else {  temp = temp.substr(0, stopSpot);  }
183                                                         
184                                 }else { temp = ""; } //sequence too short
185                                 
186                         }else { //you are keeping the back
187                                 int tempLength = temp.length();
188                                 if (tempLength > numbases) { //you have enough bases to remove some
189                                         
190                                         int stopSpot = 0;
191                                         int numBasesCounted = 0;
192                                         
193                                         for (int i = (temp.length()-1); i >= 0; i--) {
194                                                 //eliminate N's
195                                                 if (toupper(temp[i]) == 'N') { temp[i] == '.'; }
196                                                 
197                                                 numBasesCounted++; 
198
199                                                 if (numBasesCounted >= numbases) { stopSpot = i; break; }
200                                         }
201                                 
202                                         if (stopSpot == 0) { temp = ""; }
203                                         else {  temp = temp.substr(stopSpot+1);  }
204                                 }
205                                 else {  temp = ""; } //sequence too short
206                         }
207
208                 }else{
209                                 
210                         //if needed trim sequence
211                         if (keep == "front") {//you want to keep the beginning
212                                 int tempLength = tempUnaligned.length();
213
214                                 if (tempLength > numbases) { //you have enough bases to remove some
215                                         
216                                         int stopSpot = 0;
217                                         int numBasesCounted = 0;
218                                         
219                                         for (int i = 0; i < temp.length(); i++) {
220                                                 //eliminate N's
221                                                 if (toupper(temp[i]) == 'N') { 
222                                                         temp[i] == '.'; 
223                                                         tempLength--;
224                                                         if (tempLength < numbases) { stopSpot = 0; break; }
225                                                 }
226                                                 
227                                                 if(isalpha(temp[i])) { numBasesCounted++; }
228                                                 
229                                                 if (numBasesCounted >= numbases) { stopSpot = i; break; }
230                                         }
231                                         
232                                         if (stopSpot == 0) { temp = ""; }
233                                         else {  temp = temp.substr(0, stopSpot);  }
234                                                         
235                                 }else { temp = ""; } //sequence too short
236                                 
237                         }else { //you are keeping the back
238                                 int tempLength = tempUnaligned.length();
239                                 if (tempLength > numbases) { //you have enough bases to remove some
240                                         
241                                         int stopSpot = 0;
242                                         int numBasesCounted = 0;
243                                         
244                                         for (int i = (temp.length()-1); i >= 0; i--) {
245                                                 //eliminate N's
246                                                 if (toupper(temp[i]) == 'N') { 
247                                                         temp[i] == '.'; 
248                                                         tempLength--;
249                                                         if (tempLength < numbases) { stopSpot = 0; break; }
250                                                 }
251                                                 
252                                                 if(isalpha(temp[i])) { numBasesCounted++; }
253
254                                                 if (numBasesCounted >= numbases) { stopSpot = i; break; }
255                                         }
256                                 
257                                         if (stopSpot == 0) { temp = ""; }
258                                         else {  temp = temp.substr(stopSpot+1);  }
259                                 }
260                                 else {  temp = ""; } //sequence too short
261                         }
262                 }
263                 
264                 return temp;
265         }
266         catch(exception& e) {
267                 m->errorOut(e, "ChopSeqsCommand", "getChopped");
268                 exit(1);
269         }
270 }
271 //**********************************************************************************************************************
272
273