]> git.donarmstrong.com Git - mothur.git/blob - chopseqscommand.cpp
changes to chop.seqs
[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","end","fromend","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, "end", false);       if (temp == "not found") { temp = "0"; } 
61                         convert(temp, end);   
62                 
63                         temp = validParameter.validFile(parameters, "fromend", false);          if (temp == "not found") { temp = "0"; } 
64                         convert(temp, fromend);   
65                                 
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;  }
68                 }
69
70         }
71         catch(exception& e) {
72                 m->errorOut(e, "ChopSeqsCommand", "ChopSeqsCommand");
73                 exit(1);
74         }
75 }
76 //**********************************************************************************************************************
77
78 void ChopSeqsCommand::help(){
79         try {
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");
87         }
88         catch(exception& e) {
89                 m->errorOut(e, "ChopSeqsCommand", "help");
90                 exit(1);
91         }
92 }
93
94 //**********************************************************************************************************************
95
96 int ChopSeqsCommand::execute(){
97         try {
98                 
99                 if (abort == true) { return 0; }
100                 
101                 string outputFileName = outputDir + getRootName(getSimpleName(fastafile)) + "chop.fasta";
102                 string outputFileNameAccnos = outputDir + getRootName(getSimpleName(fastafile)) + "chop.accnos";
103                 
104                 ofstream out;
105                 openOutputFile(outputFileName, out);
106                 
107                 ofstream outAcc;
108                 openOutputFile(outputFileNameAccnos, outAcc);
109                 
110                 ifstream in;
111                 openInputFile(fastafile, in);
112                 
113                 bool wroteAccnos = false;
114                 
115                 while (!in.eof()) {
116                         
117                         Sequence seq(in);
118                         
119                         if (m->control_pressed) { in.close(); out.close(); outAcc.close(); remove(outputFileName.c_str()); remove(outputFileNameAccnos.c_str()); return 0;  }
120                         
121                         if (seq.getName() != "") {
122                                 string newSeqString = "";
123                                 if (seq.getIsAligned()) { //sequence is aligned
124                                         newSeqString = getChoppedAligned(seq);
125                                 }else{
126                                         newSeqString = getChoppedUnaligned(seq);
127                                 }
128                                 
129                                 //output trimmed sequence
130                                 if (newSeqString != "") {
131                                         out << ">" << seq.getName() << endl << newSeqString << endl;
132                                 }else{
133                                         outAcc << seq.getName() << endl;
134                                         wroteAccnos = true;
135                                 }
136                         }
137                 }
138                 in.close();
139                 out.close();
140                 outAcc.close();
141                 
142                 m->mothurOutEndLine();
143                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
144                 m->mothurOut(outputFileName); m->mothurOutEndLine();    
145                 
146                 if (wroteAccnos) { m->mothurOut(outputFileNameAccnos); m->mothurOutEndLine();  }
147                 else {  remove(outputFileNameAccnos.c_str());  }
148                 
149                 m->mothurOutEndLine();
150                 
151                 return 0;               
152         }
153
154         catch(exception& e) {
155                 m->errorOut(e, "ChopSeqsCommand", "execute");
156                 exit(1);
157         }
158 }
159 //**********************************************************************************************************************
160 string ChopSeqsCommand::getChoppedUnaligned(Sequence seq) {
161         try {
162                 string temp = seq.getUnaligned();
163                                 
164                 //if needed trim sequence
165                 if (end != 0) {
166                         if (temp.length() > end) {  temp = temp.substr(0, end);         }
167                         else { temp = "";  }
168                 }else { //you are using fromend
169                         if (temp.length() > fromend) { temp = temp.substr(0, (temp.length()-fromend));  }
170                         else {  temp = ""; } //sequence too short
171                 }
172
173                 return temp;
174         }
175         catch(exception& e) {
176                 m->errorOut(e, "ChopSeqsCommand", "getChoppedUnaligned");
177                 exit(1);
178         }
179 }
180 //**********************************************************************************************************************
181 string ChopSeqsCommand::getChoppedAligned(Sequence seq) {
182         try {
183                 string temp = seq.getAligned();
184                 string tempUnaligned = seq.getUnaligned();
185                                 
186                 //if needed trim sequence
187                 if (end != 0) {
188                         if (tempUnaligned.length() > end) { //you have enough bases to remove some
189                                 
190                                 int stopSpot = 0;
191                                 int numBases = 0;
192                                 
193                                 for (int i = 0; i < temp.length(); i++) {
194                                         if(isalpha(temp[i])) { numBases++; }
195
196                                         if (numBases >= end) { stopSpot = i; break; }
197                                 }
198                                 
199                                 temp = temp.substr(0, stopSpot);                
200                         }else { temp = ""; } //sequence too short
201                         
202                 }else { //you are using fromend
203                 
204                         if (tempUnaligned.length() > fromend) { 
205                                 
206                                 int stopSpot = 0;
207                                 int numBases = 0;
208                                 
209                                 for (int i = (temp.length()-1); i >= 0; i--) {
210                                         if(isalpha(temp[i])) { numBases++; }
211
212                                         if (numBases >= fromend) { stopSpot = i; break; }
213                                 }
214                                 
215                                 temp = temp.substr(0, stopSpot);
216                         }
217                         else {  temp = ""; } //sequence too short
218                 }
219
220                 return temp;
221         }
222         catch(exception& e) {
223                 m->errorOut(e, "ChopSeqsCommand", "getChoppedUnaligned");
224                 exit(1);
225         }
226 }
227 //**********************************************************************************************************************
228
229