5 * Created by Pat Schloss on 6/6/09.
6 * Copyright 2009 Patrick D. Schloss. All rights reserved.
10 #include "trimseqscommand.h"
12 //***************************************************************************************************************
14 TrimSeqsCommand::TrimSeqsCommand(string option){
19 //allow user to run help
20 if(option == "help") { help(); abort = true; }
23 //valid paramters for this command
24 string AlignArray[] = {"fasta", "flip", "oligos", "maxambig", "maxhomop", "minlength", "maxlength", "qfile", "qthreshold", "qaverage", "allfiles"};
26 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
28 OptionParser parser(option);
29 map<string,string> parameters = parser.getParameters();
31 ValidParameters validParameter;
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 //check for required parameters
39 fastaFile = validParameter.validFile(parameters, "fasta", true);
40 if (fastaFile == "not found") { mothurOut("fasta is a required parameter for the screen.seqs command."); mothurOutEndLine(); abort = true; }
41 else if (fastaFile == "not open") { abort = true; }
44 //check for optional parameter and set defaults
45 // ...at some point should added some additional type checking...
47 temp = validParameter.validFile(parameters, "flip", false);
48 if (temp == "not found"){ flip = 0; }
49 else if(isTrue(temp)) { flip = 1; }
51 temp = validParameter.validFile(parameters, "oligos", true);
52 if (temp == "not found"){ oligoFile = ""; }
53 else if(temp == "not open"){ abort = true; }
54 else { oligoFile = temp; }
56 temp = validParameter.validFile(parameters, "maxambig", false); if (temp == "not found") { temp = "-1"; }
57 convert(temp, maxAmbig);
59 temp = validParameter.validFile(parameters, "maxhomop", false); if (temp == "not found") { temp = "0"; }
60 convert(temp, maxHomoP);
62 temp = validParameter.validFile(parameters, "minlength", false); if (temp == "not found") { temp = "0"; }
63 convert(temp, minLength);
65 temp = validParameter.validFile(parameters, "maxlength", false); if (temp == "not found") { temp = "0"; }
66 convert(temp, maxLength);
68 temp = validParameter.validFile(parameters, "qfile", true);
69 if (temp == "not found") { qFileName = ""; }
70 else if(temp == "not open") { abort = 0; }
71 else { qFileName = temp; }
73 temp = validParameter.validFile(parameters, "qthreshold", false); if (temp == "not found") { temp = "0"; }
74 convert(temp, qThreshold);
76 temp = validParameter.validFile(parameters, "qaverage", false); if (temp == "not found") { temp = "0"; }
77 convert(temp, qAverage);
79 temp = validParameter.validFile(parameters, "allfiles", false); if (temp == "not found") { temp = "F"; }
80 allFiles = isTrue(temp);
82 if(allFiles && oligoFile == ""){
83 mothurOut("You selected allfiles, but didn't enter an oligos file. Ignoring the allfiles request."); mothurOutEndLine();
85 if((qAverage != 0 && qThreshold != 0) && qFileName == ""){
86 mothurOut("You didn't provide a quality file name, quality criteria will be ignored."); mothurOutEndLine();
90 if(!flip && oligoFile=="" && !maxLength && !minLength && (maxAmbig==-1) && !maxHomoP && qFileName == ""){
91 mothurOut("You didn't set any options... quiting command."); mothurOutEndLine();
98 errorOut(e, "TrimSeqsCommand", "TrimSeqsCommand");
102 //**********************************************************************************************************************
104 void TrimSeqsCommand::help(){
106 mothurOut("The trim.seqs command reads a fastaFile and creates .....\n");
107 mothurOut("The trim.seqs command parameters are fasta, flip, oligos, maxambig, maxhomop, minlength and maxlength.\n");
108 mothurOut("The fasta parameter is required.\n");
109 mothurOut("The flip parameter .... The default is 0.\n");
110 mothurOut("The oligos parameter .... The default is "".\n");
111 mothurOut("The maxambig parameter .... The default is -1.\n");
112 mothurOut("The maxhomop parameter .... The default is 0.\n");
113 mothurOut("The minlength parameter .... The default is 0.\n");
114 mothurOut("The maxlength parameter .... The default is 0.\n");
115 mothurOut("The trim.seqs command should be in the following format: \n");
116 mothurOut("trim.seqs(fasta=yourFastaFile, flip=yourFlip, oligos=yourOligos, maxambig=yourMaxambig, \n");
117 mothurOut("maxhomop=yourMaxhomop, minlength=youMinlength, maxlength=yourMaxlength) \n");
118 mothurOut("Example trim.seqs(fasta=abrecovery.fasta, flip=..., oligos=..., maxambig=..., maxhomop=..., minlength=..., maxlength=...).\n");
119 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n\n");
122 catch(exception& e) {
123 errorOut(e, "TrimSeqsCommand", "help");
129 //***************************************************************************************************************
131 TrimSeqsCommand::~TrimSeqsCommand(){ /* do nothing */ }
133 //***************************************************************************************************************
135 int TrimSeqsCommand::execute(){
138 if (abort == true) { return 0; }
141 openInputFile(fastaFile, inFASTA);
144 string trimSeqFile = getRootName(fastaFile) + "trim.fasta";
145 openOutputFile(trimSeqFile, outFASTA);
148 vector<ofstream*> fastaFileNames;
150 string groupFile = getRootName(fastaFile) + "groups";
151 openOutputFile(groupFile, outGroups);
152 getOligos(fastaFileNames);
156 string scrapSeqFile = getRootName(fastaFile) + "scrap.fasta";
157 openOutputFile(scrapSeqFile, scrapFASTA);
160 if(qFileName != "") { openInputFile(qFileName, qFile); }
164 while(!inFASTA.eof()){
165 Sequence currSeq(inFASTA);
166 string origSeq = currSeq.getUnaligned();
168 string trashCode = "";
171 if(qThreshold != 0) { success = stripQualThreshold(currSeq, qFile); }
172 else if(qAverage != 0) { success = cullQualAverage(currSeq, qFile); }
173 if(!success) { trashCode += 'q'; }
176 if(barcodes.size() != 0){
177 success = stripBarcode(currSeq, group);
178 if(!success){ trashCode += 'b'; }
180 if(numFPrimers != 0){
181 success = stripForward(currSeq);
182 if(!success){ trashCode += 'f'; }
184 if(numRPrimers != 0){
185 success = stripReverse(currSeq);
186 if(!success){ trashCode += 'r'; }
188 if(minLength > 0 || maxLength > 0){
189 success = cullLength(currSeq);
190 if(!success){ trashCode += 'l'; }
193 success = cullHomoP(currSeq);
194 if(!success){ trashCode += 'h'; }
197 success = cullAmbigs(currSeq);
198 if(!success){ trashCode += 'n'; }
201 if(flip){ currSeq.reverseComplement(); } // should go last
203 if(trashCode.length() == 0){
204 currSeq.printSequence(outFASTA);
205 if(barcodes.size() != 0){
206 outGroups << currSeq.getName() << '\t' << groupVector[group] << endl;
209 currSeq.printSequence(*fastaFileNames[group]);
214 currSeq.setName(currSeq.getName() + '|' + trashCode);
215 currSeq.setUnaligned(origSeq);
216 currSeq.printSequence(scrapFASTA);
225 for(int i=0;i<fastaFileNames.size();i++){
226 fastaFileNames[i]->close();
227 delete fastaFileNames[i];
230 for(int i=0;i<fastaFileNames.size();i++){
232 openInputFile(getRootName(fastaFile) + groupVector[i] + ".fasta", inFASTA);
234 openOutputFile(getRootName(fastaFile) + groupVector[i] + ".groups", outGroups);
236 while(!inFASTA.eof()){
237 if(inFASTA.get() == '>'){
239 outGroups << seqName << '\t' << groupVector[i] << endl;
241 while (!inFASTA.eof()) { char c = inFASTA.get(); if (c == 10 || c == 13){ break; } }
250 catch(exception& e) {
251 errorOut(e, "TrimSeqsCommand", "execute");
256 //***************************************************************************************************************
258 void TrimSeqsCommand::getOligos(vector<ofstream*>& outFASTAVec){
261 openInputFile(oligoFile, inOligos);
265 string type, oligo, group;
268 while(!inOligos.eof()){
272 while (!inOligos.eof()) { char c = inOligos.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
277 for(int i=0;i<oligo.length();i++){
278 oligo[i] = toupper(oligo[i]);
279 if(oligo[i] == 'U') { oligo[i] = 'T'; }
282 if(type == "forward"){
283 forPrimer.push_back(oligo);
285 else if(type == "reverse"){
286 revPrimer.push_back(oligo);
288 else if(type == "barcode"){
290 barcodes[oligo]=index++;
291 groupVector.push_back(group);
294 outFASTAVec.push_back(new ofstream((getRootName(fastaFile) + group + ".fasta").c_str(), ios::ate));
302 numFPrimers = forPrimer.size();
303 numRPrimers = revPrimer.size();
306 //***************************************************************************************************************
308 bool TrimSeqsCommand::stripBarcode(Sequence& seq, int& group){
310 string rawSequence = seq.getUnaligned();
311 bool success = 0; //guilty until proven innocent
313 for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
314 string oligo = it->first;
315 if(rawSequence.length() < oligo.length()){ //let's just assume that the barcodes are the same length
320 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
322 seq.setUnaligned(rawSequence.substr(oligo.length()));
331 //***************************************************************************************************************
333 bool TrimSeqsCommand::stripForward(Sequence& seq){
335 string rawSequence = seq.getUnaligned();
336 bool success = 0; //guilty until proven innocent
338 for(int i=0;i<numFPrimers;i++){
339 string oligo = forPrimer[i];
341 if(rawSequence.length() < oligo.length()){
346 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
347 seq.setUnaligned(rawSequence.substr(oligo.length()));
357 //***************************************************************************************************************
359 bool TrimSeqsCommand::stripReverse(Sequence& seq){
361 string rawSequence = seq.getUnaligned();
362 bool success = 0; //guilty until proven innocent
364 for(int i=0;i<numRPrimers;i++){
365 string oligo = revPrimer[i];
367 if(rawSequence.length() < oligo.length()){
372 if(compareDNASeq(oligo, rawSequence.substr(rawSequence.length()-oligo.length(),oligo.length()))){
373 seq.setUnaligned(rawSequence.substr(rawSequence.length()-oligo.length()));
382 //***************************************************************************************************************
384 bool TrimSeqsCommand::cullLength(Sequence& seq){
386 int length = seq.getNumBases();
387 bool success = 0; //guilty until proven innocent
389 if(length >= minLength && maxLength == 0) { success = 1; }
390 else if(length >= minLength && length <= maxLength) { success = 1; }
391 else { success = 0; }
397 //***************************************************************************************************************
399 bool TrimSeqsCommand::cullHomoP(Sequence& seq){
401 int longHomoP = seq.getLongHomoPolymer();
402 bool success = 0; //guilty until proven innocent
404 if(longHomoP <= maxHomoP){ success = 1; }
405 else { success = 0; }
411 //***************************************************************************************************************
413 bool TrimSeqsCommand::cullAmbigs(Sequence& seq){
415 int numNs = seq.getAmbigBases();
416 bool success = 0; //guilty until proven innocent
418 if(numNs <= maxAmbig) { success = 1; }
419 else { success = 0; }
425 //***************************************************************************************************************
427 bool TrimSeqsCommand::compareDNASeq(string oligo, string seq){
430 int length = oligo.length();
432 for(int i=0;i<length;i++){
434 if(oligo[i] != seq[i]){
435 if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C') { success = 0; }
436 else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N')) { success = 0; }
437 else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G')) { success = 0; }
438 else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T')) { success = 0; }
439 else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A')) { success = 0; }
440 else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
441 else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A')) { success = 0; }
442 else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
443 else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
444 else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
445 else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C')) { success = 0; }
446 else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
448 if(success == 0) { break; }
458 //***************************************************************************************************************
460 bool TrimSeqsCommand::stripQualThreshold(Sequence& seq, ifstream& qFile){
462 string rawSequence = seq.getUnaligned();
463 int seqLength = rawSequence.length();
467 if(name.substr(1) != seq.getName()) { mothurOut("sequence name mismatch btwn fasta and qual file"); mothurOutEndLine(); }
468 while (!qFile.eof()) { char c = qFile.get(); if (c == 10 || c == 13){ break; } }
473 for(int i=0;i<seqLength;i++){
476 if(score <= qThreshold){
481 for(int i=end+1;i<seqLength;i++){
485 seq.setUnaligned(rawSequence.substr(0,end));
490 //***************************************************************************************************************
492 bool TrimSeqsCommand::cullQualAverage(Sequence& seq, ifstream& qFile){
494 string rawSequence = seq.getUnaligned();
495 int seqLength = seq.getNumBases();
496 bool success = 0; //guilty until proven innocent
500 if(name.substr(1) != seq.getName()) { mothurOut("sequence name mismatch btwn fasta and qual file"); mothurOutEndLine(); }
501 while (!qFile.eof()) { char c = qFile.get(); if (c == 10 || c == 13){ break; } }
506 for(int i=0;i<seqLength;i++){
510 average /= seqLength;
512 if(average >= qAverage) { success = 1; }
513 else { success = 0; }
518 //***************************************************************************************************************