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") { cout << "fasta is a required parameter for the screen.seqs command." << endl; 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 cout << "You selected allfiles, but didn't enter an oligos file. Ignoring the allfiles request." << endl;
85 if((qAverage != 0 && qThreshold != 0) && qFileName == ""){
86 cout << "You didn't provide a quality file name, quality criteria will be ignored." << endl;
90 if(!flip && oligoFile=="" && !maxLength && !minLength && (maxAmbig==-1) && !maxHomoP && qFileName == ""){
91 cout << "You didn't set any options... quiting command." << endl;
98 cout << "Standard Error: " << e.what() << " has occurred in the TrimSeqsCommand class Function TrimSeqsCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
102 cout << "An unknown error has occurred in the TrimSeqsCommand class function TrimSeqsCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
106 //**********************************************************************************************************************
108 void TrimSeqsCommand::help(){
110 cout << "The trim.seqs command reads a fastaFile and creates ....." << "\n";
111 cout << "The trim.seqs command parameters are fasta, flip, oligos, maxambig, maxhomop, minlength and maxlength." << "\n";
112 cout << "The fasta parameter is required." << "\n";
113 cout << "The flip parameter .... The default is 0." << "\n";
114 cout << "The oligos parameter .... The default is ""." << "\n";
115 cout << "The maxambig parameter .... The default is -1." << "\n";
116 cout << "The maxhomop parameter .... The default is 0." << "\n";
117 cout << "The minlength parameter .... The default is 0." << "\n";
118 cout << "The maxlength parameter .... The default is 0." << "\n";
119 cout << "The trim.seqs command should be in the following format: " << "\n";
120 cout << "trim.seqs(fasta=yourFastaFile, flip=yourFlip, oligos=yourOligos, maxambig=yourMaxambig, " << "\n";
121 cout << "maxhomop=yourMaxhomop, minlength=youMinlength, maxlength=yourMaxlength) " << "\n";
122 cout << "Example trim.seqs(fasta=abrecovery.fasta, flip=..., oligos=..., maxambig=..., maxhomop=..., minlength=..., maxlength=...)." << "\n";
123 cout << "Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta)." << "\n" << "\n";
126 catch(exception& e) {
127 cout << "Standard Error: " << e.what() << " has occurred in the TrimSeqsCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
131 cout << "An unknown error has occurred in the TrimSeqsCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
137 //***************************************************************************************************************
139 TrimSeqsCommand::~TrimSeqsCommand(){ /* do nothing */ }
141 //***************************************************************************************************************
143 int TrimSeqsCommand::execute(){
146 if (abort == true) { return 0; }
149 openInputFile(fastaFile, inFASTA);
152 string trimSeqFile = getRootName(fastaFile) + "trim.fasta";
153 openOutputFile(trimSeqFile, outFASTA);
156 vector<ofstream*> fastaFileNames;
158 string groupFile = getRootName(fastaFile) + "groups";
159 openOutputFile(groupFile, outGroups);
160 getOligos(fastaFileNames);
164 string scrapSeqFile = getRootName(fastaFile) + "scrap.fasta";
165 openOutputFile(scrapSeqFile, scrapFASTA);
168 if(qFileName != "") { openInputFile(qFileName, qFile); }
172 while(!inFASTA.eof()){
173 Sequence currSeq(inFASTA);
174 string origSeq = currSeq.getUnaligned();
176 string trashCode = "";
179 if(qThreshold != 0) { success = stripQualThreshold(currSeq, qFile); }
180 else if(qAverage != 0) { success = cullQualAverage(currSeq, qFile); }
181 if(!success) { trashCode += 'q'; }
183 if(barcodes.size() != 0){
184 success = stripBarcode(currSeq, group);
185 if(!success){ trashCode += 'b'; }
187 if(numFPrimers != 0){
188 success = stripForward(currSeq);
189 if(!success){ trashCode += 'f'; }
191 if(numRPrimers != 0){
192 success = stripReverse(currSeq);
193 if(!success){ trashCode += 'r'; }
195 if(minLength > 0 || maxLength > 0){
196 success = cullLength(currSeq);
197 if(!success){ trashCode += 'l'; }
200 success = cullHomoP(currSeq);
201 if(!success){ trashCode += 'h'; }
204 success = cullAmbigs(currSeq);
205 if(!success){ trashCode += 'n'; }
208 if(flip){ currSeq.reverseComplement(); } // should go last
210 if(trashCode.length() == 0){
211 currSeq.printSequence(outFASTA);
212 if(barcodes.size() != 0){
213 outGroups << currSeq.getName() << '\t' << groupVector[group] << endl;
216 currSeq.printSequence(*fastaFileNames[group]);
221 currSeq.setName(currSeq.getName() + '|' + trashCode);
222 currSeq.setUnaligned(origSeq);
223 currSeq.printSequence(scrapFASTA);
232 for(int i=0;i<fastaFileNames.size();i++){
233 fastaFileNames[i]->close();
234 delete fastaFileNames[i];
237 for(int i=0;i<fastaFileNames.size();i++){
239 openInputFile(getRootName(fastaFile) + groupVector[i] + ".fasta", inFASTA);
241 openOutputFile(getRootName(fastaFile) + groupVector[i] + ".groups", outGroups);
243 while(!inFASTA.eof()){
244 if(inFASTA.get() == '>'){
246 outGroups << seqName << '\t' << groupVector[i] << endl;
248 while (!inFASTA.eof()) { char c = inFASTA.get(); if (c == 10 || c == 13){ break; } }
257 catch(exception& e) {
258 cout << "Standard Error: " << e.what() << " has occurred in the TrimSeqsCommand class Function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
262 cout << "An unknown error has occurred in the TrimSeqsCommand class function execute. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
267 //***************************************************************************************************************
269 void TrimSeqsCommand::getOligos(vector<ofstream*>& outFASTAVec){
272 openInputFile(oligoFile, inOligos);
276 string type, oligo, group;
279 while(!inOligos.eof()){
283 while (!inOligos.eof()) { char c = inOligos.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
288 for(int i=0;i<oligo.length();i++){
289 oligo[i] = toupper(oligo[i]);
290 if(oligo[i] == 'U') { oligo[i] = 'T'; }
293 if(type == "forward"){
294 forPrimer.push_back(oligo);
296 else if(type == "reverse"){
297 revPrimer.push_back(oligo);
299 else if(type == "barcode"){
301 barcodes[oligo]=index++;
302 groupVector.push_back(group);
305 outFASTAVec.push_back(new ofstream((getRootName(fastaFile) + group + ".fasta").c_str(), ios::ate));
311 numFPrimers = forPrimer.size();
312 numRPrimers = revPrimer.size();
315 //***************************************************************************************************************
317 bool TrimSeqsCommand::stripBarcode(Sequence& seq, int& group){
319 string rawSequence = seq.getUnaligned();
320 bool success = 0; //guilty until proven innocent
322 for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
323 string oligo = it->first;
324 if(rawSequence.length() < oligo.length()){ //let's just assume that the barcodes are the same length
329 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
331 seq.setUnaligned(rawSequence.substr(oligo.length()));
340 //***************************************************************************************************************
342 bool TrimSeqsCommand::stripForward(Sequence& seq){
344 string rawSequence = seq.getUnaligned();
345 bool success = 0; //guilty until proven innocent
347 for(int i=0;i<numFPrimers;i++){
348 string oligo = forPrimer[i];
350 if(rawSequence.length() < oligo.length()){
355 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
356 seq.setUnaligned(rawSequence.substr(oligo.length()));
366 //***************************************************************************************************************
368 bool TrimSeqsCommand::stripReverse(Sequence& seq){
370 string rawSequence = seq.getUnaligned();
371 bool success = 0; //guilty until proven innocent
373 for(int i=0;i<numRPrimers;i++){
374 string oligo = revPrimer[i];
376 if(rawSequence.length() < oligo.length()){
381 if(compareDNASeq(oligo, rawSequence.substr(rawSequence.length()-oligo.length(),oligo.length()))){
382 seq.setUnaligned(rawSequence.substr(rawSequence.length()-oligo.length()));
391 //***************************************************************************************************************
393 bool TrimSeqsCommand::cullLength(Sequence& seq){
395 int length = seq.getNumBases();
396 bool success = 0; //guilty until proven innocent
398 if(length >= minLength && maxLength == 0) { success = 1; }
399 else if(length >= minLength && length <= maxLength) { success = 1; }
400 else { success = 0; }
406 //***************************************************************************************************************
408 bool TrimSeqsCommand::cullHomoP(Sequence& seq){
410 int longHomoP = seq.getLongHomoPolymer();
411 bool success = 0; //guilty until proven innocent
413 if(longHomoP <= maxHomoP){ success = 1; }
414 else { success = 0; }
420 //***************************************************************************************************************
422 bool TrimSeqsCommand::cullAmbigs(Sequence& seq){
424 int numNs = seq.getAmbigBases();
425 bool success = 0; //guilty until proven innocent
427 if(numNs <= maxAmbig) { success = 1; }
428 else { success = 0; }
434 //***************************************************************************************************************
436 bool TrimSeqsCommand::compareDNASeq(string oligo, string seq){
439 int length = oligo.length();
441 for(int i=0;i<length;i++){
443 if(oligo[i] != seq[i]){
444 if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C') { success = 0; }
445 else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N')) { success = 0; }
446 else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G')) { success = 0; }
447 else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T')) { success = 0; }
448 else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A')) { success = 0; }
449 else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
450 else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A')) { success = 0; }
451 else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
452 else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
453 else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
454 else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C')) { success = 0; }
455 else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
457 if(success == 0) { break; }
467 //***************************************************************************************************************
469 bool TrimSeqsCommand::stripQualThreshold(Sequence& seq, ifstream& qFile){
471 string rawSequence = seq.getUnaligned();
472 int seqLength = rawSequence.length();
476 if(name.substr(1) != seq.getName()) { cout << "sequence name mismatch btwn fasta and qual file" << endl; }
477 while (!qFile.eof()) { char c = qFile.get(); if (c == 10 || c == 13){ break; } }
482 for(int i=0;i<seqLength;i++){
485 if(score <= qThreshold){
490 for(int i=end+1;i<seqLength;i++){
494 seq.setUnaligned(rawSequence.substr(0,end));
499 //***************************************************************************************************************
501 bool TrimSeqsCommand::cullQualAverage(Sequence& seq, ifstream& qFile){
503 string rawSequence = seq.getUnaligned();
504 int seqLength = seq.getNumBases();
505 bool success = 0; //guilty until proven innocent
509 if(name.substr(1) != seq.getName()) { cout << "sequence name mismatch btwn fasta and qual file" << endl; }
510 while (!qFile.eof()) { char c = qFile.get(); if (c == 10 || c == 13){ break; } }
515 for(int i=0;i<seqLength;i++){
519 average /= seqLength;
521 if(average >= qAverage) { success = 1; }
522 else { success = 0; }
527 //***************************************************************************************************************