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", "qtrim"};
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, "qtrim", false); if (temp == "not found") { temp = "F"; }
79 temp = validParameter.validFile(parameters, "qaverage", false); if (temp == "not found") { temp = "0"; }
80 convert(temp, qAverage);
82 temp = validParameter.validFile(parameters, "allfiles", false); if (temp == "not found") { temp = "F"; }
83 allFiles = isTrue(temp);
85 if(allFiles && oligoFile == ""){
86 mothurOut("You selected allfiles, but didn't enter an oligos file. Ignoring the allfiles request."); mothurOutEndLine();
88 if((qAverage != 0 && qThreshold != 0) && qFileName == ""){
89 mothurOut("You didn't provide a quality file name, quality criteria will be ignored."); mothurOutEndLine();
93 if(!flip && oligoFile=="" && !maxLength && !minLength && (maxAmbig==-1) && !maxHomoP && qFileName == ""){
94 mothurOut("You didn't set any options... quiting command."); mothurOutEndLine();
100 catch(exception& e) {
101 errorOut(e, "TrimSeqsCommand", "TrimSeqsCommand");
105 //**********************************************************************************************************************
107 void TrimSeqsCommand::help(){
109 mothurOut("The trim.seqs command reads a fastaFile and creates .....\n");
110 mothurOut("The trim.seqs command parameters are fasta, flip, oligos, maxambig, maxhomop, minlength, maxlength, qfile, qthreshold, qaverage, qtrim and allfiles.\n");
111 mothurOut("The fasta parameter is required.\n");
112 mothurOut("The flip parameter .... The default is 0.\n");
113 mothurOut("The oligos parameter .... The default is "".\n");
114 mothurOut("The maxambig parameter .... The default is -1.\n");
115 mothurOut("The maxhomop parameter .... The default is 0.\n");
116 mothurOut("The minlength parameter .... The default is 0.\n");
117 mothurOut("The maxlength parameter .... The default is 0.\n");
118 mothurOut("The qfile parameter .....\n");
119 mothurOut("The qthreshold parameter .... The default is 0.\n");
120 mothurOut("The qaverage parameter .... The default is 0.\n");
121 mothurOut("The allfiles parameter .... The default is F.\n");
122 mothurOut("The qtrim parameter .... The default is F.\n");
123 mothurOut("The trim.seqs command should be in the following format: \n");
124 mothurOut("trim.seqs(fasta=yourFastaFile, flip=yourFlip, oligos=yourOligos, maxambig=yourMaxambig, \n");
125 mothurOut("maxhomop=yourMaxhomop, minlength=youMinlength, maxlength=yourMaxlength) \n");
126 mothurOut("Example trim.seqs(fasta=abrecovery.fasta, flip=..., oligos=..., maxambig=..., maxhomop=..., minlength=..., maxlength=...).\n");
127 mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n");
128 mothurOut("For more details please check out the wiki http://www.mothur.org/wiki/Trim.seqs .\n\n");
131 catch(exception& e) {
132 errorOut(e, "TrimSeqsCommand", "help");
138 //***************************************************************************************************************
140 TrimSeqsCommand::~TrimSeqsCommand(){ /* do nothing */ }
142 //***************************************************************************************************************
144 int TrimSeqsCommand::execute(){
147 if (abort == true) { return 0; }
150 openInputFile(fastaFile, inFASTA);
153 string trimSeqFile = getRootName(fastaFile) + "trim.fasta";
154 openOutputFile(trimSeqFile, outFASTA);
157 vector<ofstream*> fastaFileNames;
159 string groupFile = getRootName(fastaFile) + "groups";
160 openOutputFile(groupFile, outGroups);
161 getOligos(fastaFileNames);
165 string scrapSeqFile = getRootName(fastaFile) + "scrap.fasta";
166 openOutputFile(scrapSeqFile, scrapFASTA);
169 if(qFileName != "") { openInputFile(qFileName, qFile); }
173 while(!inFASTA.eof()){
174 Sequence currSeq(inFASTA);
175 string origSeq = currSeq.getUnaligned();
177 string trashCode = "";
180 if(qThreshold != 0) { success = stripQualThreshold(currSeq, qFile); }
181 else if(qAverage != 0) { success = cullQualAverage(currSeq, qFile); }
182 if ((!qtrim) && (origSeq.length() != currSeq.getUnaligned().length())) {
183 success = 0; //if you don't want to trim and the sequence does not meet quality requirements, move to scrap
185 if(!success) { trashCode += 'q'; }
187 if(barcodes.size() != 0){
189 success = stripBarcode(currSeq, group);
190 if(!success){ trashCode += 'b'; }
192 if(numFPrimers != 0){
193 success = stripForward(currSeq);
194 if(!success){ trashCode += 'f'; }
196 if(numRPrimers != 0){
197 success = stripReverse(currSeq);
198 if(!success){ trashCode += 'r'; }
200 if(minLength > 0 || maxLength > 0){
201 success = cullLength(currSeq);
202 if ((currSeq.getUnaligned().length() > 300) && (success)) { cout << "too long " << currSeq.getUnaligned().length() << endl; }
203 if(!success){ trashCode += 'l'; }
206 success = cullHomoP(currSeq);
207 if(!success){ trashCode += 'h'; }
210 success = cullAmbigs(currSeq);
211 if(!success){ trashCode += 'n'; }
214 if(flip){ currSeq.reverseComplement(); } // should go last
216 if(trashCode.length() == 0){
217 currSeq.setAligned(currSeq.getUnaligned()); //this is because of a modification we made to the sequence class to fix a bug. all seqs have an aligned version, which is the version that gets printed.
218 currSeq.printSequence(outFASTA);
219 if(barcodes.size() != 0){
220 outGroups << currSeq.getName() << '\t' << groupVector[group] << endl;
223 currSeq.printSequence(*fastaFileNames[group]);
228 currSeq.setName(currSeq.getName() + '|' + trashCode);
229 currSeq.setUnaligned(origSeq);
230 currSeq.printSequence(scrapFASTA);
238 if(qFileName != "") { qFile.close(); }
240 for(int i=0;i<fastaFileNames.size();i++){
241 fastaFileNames[i]->close();
242 delete fastaFileNames[i];
245 for(int i=0;i<fastaFileNames.size();i++){
247 openInputFile(getRootName(fastaFile) + groupVector[i] + ".fasta", inFASTA);
249 openOutputFile(getRootName(fastaFile) + groupVector[i] + ".groups", outGroups);
251 while(!inFASTA.eof()){
252 if(inFASTA.get() == '>'){
254 outGroups << seqName << '\t' << groupVector[i] << endl;
256 while (!inFASTA.eof()) { char c = inFASTA.get(); if (c == 10 || c == 13){ break; } }
265 catch(exception& e) {
266 errorOut(e, "TrimSeqsCommand", "execute");
271 //***************************************************************************************************************
273 void TrimSeqsCommand::getOligos(vector<ofstream*>& outFASTAVec){
276 openInputFile(oligoFile, inOligos);
280 string type, oligo, group;
283 while(!inOligos.eof()){
287 while (!inOligos.eof()) { char c = inOligos.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
292 for(int i=0;i<oligo.length();i++){
293 oligo[i] = toupper(oligo[i]);
294 if(oligo[i] == 'U') { oligo[i] = 'T'; }
297 if(type == "forward"){
298 forPrimer.push_back(oligo);
300 else if(type == "reverse"){
301 revPrimer.push_back(oligo);
303 else if(type == "barcode"){
305 barcodes[oligo]=index++;
306 groupVector.push_back(group);
309 outFASTAVec.push_back(new ofstream((getRootName(fastaFile) + group + ".fasta").c_str(), ios::ate));
317 numFPrimers = forPrimer.size();
318 numRPrimers = revPrimer.size();
321 catch(exception& e) {
322 errorOut(e, "TrimSeqsCommand", "getOligos");
328 //***************************************************************************************************************
330 bool TrimSeqsCommand::stripBarcode(Sequence& seq, int& group){
332 string rawSequence = seq.getUnaligned();
333 bool success = 0; //guilty until proven innocent
335 for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
336 string oligo = it->first;
337 if(rawSequence.length() < oligo.length()){ //let's just assume that the barcodes are the same length
342 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
344 seq.setUnaligned(rawSequence.substr(oligo.length()));
352 catch(exception& e) {
353 errorOut(e, "TrimSeqsCommand", "stripBarcode");
359 //***************************************************************************************************************
361 bool TrimSeqsCommand::stripForward(Sequence& seq){
363 string rawSequence = seq.getUnaligned();
364 bool success = 0; //guilty until proven innocent
366 for(int i=0;i<numFPrimers;i++){
367 string oligo = forPrimer[i];
369 if(rawSequence.length() < oligo.length()){
374 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
375 seq.setUnaligned(rawSequence.substr(oligo.length()));
384 catch(exception& e) {
385 errorOut(e, "TrimSeqsCommand", "stripForward");
390 //***************************************************************************************************************
392 bool TrimSeqsCommand::stripReverse(Sequence& seq){
394 string rawSequence = seq.getUnaligned();
395 bool success = 0; //guilty until proven innocent
397 for(int i=0;i<numRPrimers;i++){
398 string oligo = revPrimer[i];
400 if(rawSequence.length() < oligo.length()){
405 if(compareDNASeq(oligo, rawSequence.substr(rawSequence.length()-oligo.length(),oligo.length()))){
406 seq.setUnaligned(rawSequence.substr(rawSequence.length()-oligo.length()));
414 catch(exception& e) {
415 errorOut(e, "TrimSeqsCommand", "stripReverse");
420 //***************************************************************************************************************
422 bool TrimSeqsCommand::cullLength(Sequence& seq){
425 int length = seq.getNumBases();
426 bool success = 0; //guilty until proven innocent
428 if(length >= minLength && maxLength == 0) { success = 1; }
429 else if(length >= minLength && length <= maxLength) { success = 1; }
430 else { success = 0; }
435 catch(exception& e) {
436 errorOut(e, "TrimSeqsCommand", "cullLength");
442 //***************************************************************************************************************
444 bool TrimSeqsCommand::cullHomoP(Sequence& seq){
446 int longHomoP = seq.getLongHomoPolymer();
447 bool success = 0; //guilty until proven innocent
449 if(longHomoP <= maxHomoP){ success = 1; }
450 else { success = 0; }
454 catch(exception& e) {
455 errorOut(e, "TrimSeqsCommand", "cullHomoP");
461 //***************************************************************************************************************
463 bool TrimSeqsCommand::cullAmbigs(Sequence& seq){
465 int numNs = seq.getAmbigBases();
466 bool success = 0; //guilty until proven innocent
468 if(numNs <= maxAmbig) { success = 1; }
469 else { success = 0; }
473 catch(exception& e) {
474 errorOut(e, "TrimSeqsCommand", "cullAmbigs");
480 //***************************************************************************************************************
482 bool TrimSeqsCommand::compareDNASeq(string oligo, string seq){
485 int length = oligo.length();
487 for(int i=0;i<length;i++){
489 if(oligo[i] != seq[i]){
490 if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C') { success = 0; }
491 else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N')) { success = 0; }
492 else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G')) { success = 0; }
493 else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T')) { success = 0; }
494 else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A')) { success = 0; }
495 else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
496 else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A')) { success = 0; }
497 else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
498 else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
499 else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
500 else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C')) { success = 0; }
501 else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
503 if(success == 0) { break; }
512 catch(exception& e) {
513 errorOut(e, "TrimSeqsCommand", "compareDNASeq");
519 //***************************************************************************************************************
521 bool TrimSeqsCommand::stripQualThreshold(Sequence& seq, ifstream& qFile){
523 string rawSequence = seq.getUnaligned();
524 int seqLength = rawSequence.length();
528 if (name.length() != 0) { if(name.substr(1) != seq.getName()) { mothurOut("sequence name mismatch btwn fasta and qual file"); mothurOutEndLine(); } }
529 while (!qFile.eof()) { char c = qFile.get(); if (c == 10 || c == 13){ break; } }
534 for(int i=0;i<seqLength;i++){
537 if(score <= qThreshold){
542 for(int i=end+1;i<seqLength;i++){
546 seq.setUnaligned(rawSequence.substr(0,end));
550 catch(exception& e) {
551 errorOut(e, "TrimSeqsCommand", "stripQualThreshold");
556 //***************************************************************************************************************
558 bool TrimSeqsCommand::cullQualAverage(Sequence& seq, ifstream& qFile){
560 string rawSequence = seq.getUnaligned();
561 int seqLength = seq.getNumBases();
562 bool success = 0; //guilty until proven innocent
566 if (name[0] == '>') { if(name.substr(1) != seq.getName()) { mothurOut("sequence name mismatch btwn fasta: " + seq.getName() + " and qual file: " + name); mothurOutEndLine(); } }
568 while (!qFile.eof()) { char c = qFile.get(); if (c == 10 || c == 13){ break; } }
573 for(int i=0;i<seqLength;i++){
577 average /= seqLength;
579 if(average >= qAverage) { success = 1; }
580 else { success = 0; }
584 catch(exception& e) {
585 errorOut(e, "TrimSeqsCommand", "cullQualAverage");
590 //***************************************************************************************************************