5 * Created by Pat Schloss on 6/6/09.
6 * Copyright 2009 Patrick D. Schloss. All rights reserved.
10 #include "trimseqscommand.h"
11 #include "needlemanoverlap.hpp"
13 //***************************************************************************************************************
15 TrimSeqsCommand::TrimSeqsCommand(string option) {
20 //allow user to run help
21 if(option == "help") { help(); abort = true; }
24 //valid paramters for this command
25 string AlignArray[] = {"fasta", "flip", "oligos", "maxambig", "maxhomop", "minlength", "maxlength", "qfile",
26 "qthreshold", "qaverage", "allfiles", "qtrim","tdiffs", "pdiffs", "bdiffs", "processors", "outputdir","inputdir"};
28 vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
30 OptionParser parser(option);
31 map<string,string> parameters = parser.getParameters();
33 ValidParameters validParameter;
34 map<string,string>::iterator it;
36 //check to make sure all parameters are valid for command
37 for (it = parameters.begin(); it != parameters.end(); it++) {
38 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; }
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 = ""; }
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; }
54 it = parameters.find("oligos");
55 //user has given a template file
56 if(it != parameters.end()){
57 path = hasPath(it->second);
58 //if the user has not given a path then, add inputdir. else leave path alone.
59 if (path == "") { parameters["oligos"] = inputDir + it->second; }
62 it = parameters.find("qfile");
63 //user has given a template file
64 if(it != parameters.end()){
65 path = hasPath(it->second);
66 //if the user has not given a path then, add inputdir. else leave path alone.
67 if (path == "") { parameters["qfile"] = inputDir + it->second; }
72 //check for required parameters
73 fastaFile = validParameter.validFile(parameters, "fasta", true);
74 if (fastaFile == "not found") { m->mothurOut("fasta is a required parameter for the screen.seqs command."); m->mothurOutEndLine(); abort = true; }
75 else if (fastaFile == "not open") { abort = true; }
77 //if the user changes the output directory command factory will send this info to us in the output parameter
78 outputDir = validParameter.validFile(parameters, "outputdir", false); if (outputDir == "not found"){
80 outputDir += hasPath(fastaFile); //if user entered a file with a path then preserve it
83 //check for optional parameter and set defaults
84 // ...at some point should added some additional type checking...
86 temp = validParameter.validFile(parameters, "flip", false);
87 if (temp == "not found"){ flip = 0; }
88 else if(isTrue(temp)) { flip = 1; }
90 temp = validParameter.validFile(parameters, "oligos", true);
91 if (temp == "not found"){ oligoFile = ""; }
92 else if(temp == "not open"){ abort = true; }
93 else { oligoFile = temp; }
95 temp = validParameter.validFile(parameters, "maxambig", false); if (temp == "not found") { temp = "-1"; }
96 convert(temp, maxAmbig);
98 temp = validParameter.validFile(parameters, "maxhomop", false); if (temp == "not found") { temp = "0"; }
99 convert(temp, maxHomoP);
101 temp = validParameter.validFile(parameters, "minlength", false); if (temp == "not found") { temp = "0"; }
102 convert(temp, minLength);
104 temp = validParameter.validFile(parameters, "maxlength", false); if (temp == "not found") { temp = "0"; }
105 convert(temp, maxLength);
107 temp = validParameter.validFile(parameters, "tdiffs", false); if (temp == "not found") { temp = "0"; }
108 convert(temp, tdiffs);
110 temp = validParameter.validFile(parameters, "bdiffs", false); if (temp == "not found") { temp = "0"; }
111 convert(temp, bdiffs);
113 temp = validParameter.validFile(parameters, "pdiffs", false); if (temp == "not found") { temp = "0"; }
114 convert(temp, pdiffs);
116 temp = validParameter.validFile(parameters, "qfile", true);
117 if (temp == "not found") { qFileName = ""; }
118 else if(temp == "not open") { abort = true; }
119 else { qFileName = temp; }
121 temp = validParameter.validFile(parameters, "qthreshold", false); if (temp == "not found") { temp = "0"; }
122 convert(temp, qThreshold);
124 temp = validParameter.validFile(parameters, "qtrim", false); if (temp == "not found") { temp = "F"; }
125 qtrim = isTrue(temp);
127 temp = validParameter.validFile(parameters, "qaverage", false); if (temp == "not found") { temp = "0"; }
128 convert(temp, qAverage);
130 temp = validParameter.validFile(parameters, "allfiles", false); if (temp == "not found") { temp = "F"; }
131 allFiles = isTrue(temp);
133 temp = validParameter.validFile(parameters, "processors", false); if (temp == "not found"){ temp = "1"; }
134 convert(temp, processors);
136 if(allFiles && oligoFile == ""){
137 m->mothurOut("You selected allfiles, but didn't enter an oligos file. Ignoring the allfiles request."); m->mothurOutEndLine();
139 if((qAverage != 0 && qThreshold != 0) && qFileName == ""){
140 m->mothurOut("You didn't provide a quality file name, quality criteria will be ignored."); m->mothurOutEndLine();
144 if(!flip && oligoFile=="" && !maxLength && !minLength && (maxAmbig==-1) && !maxHomoP && qFileName == ""){
145 m->mothurOut("You didn't set any options... quiting command."); m->mothurOutEndLine();
151 catch(exception& e) {
152 m->errorOut(e, "TrimSeqsCommand", "TrimSeqsCommand");
156 //**********************************************************************************************************************
158 void TrimSeqsCommand::help(){
160 m->mothurOut("The trim.seqs command reads a fastaFile and creates .....\n");
161 m->mothurOut("The trim.seqs command parameters are fasta, flip, oligos, maxambig, maxhomop, minlength, maxlength, qfile, qthreshold, qaverage, diffs, qtrim and allfiles.\n");
162 m->mothurOut("The fasta parameter is required.\n");
163 m->mothurOut("The flip parameter .... The default is 0.\n");
164 m->mothurOut("The oligos parameter .... The default is "".\n");
165 m->mothurOut("The maxambig parameter .... The default is -1.\n");
166 m->mothurOut("The maxhomop parameter .... The default is 0.\n");
167 m->mothurOut("The minlength parameter .... The default is 0.\n");
168 m->mothurOut("The maxlength parameter .... The default is 0.\n");
169 m->mothurOut("The tdiffs parameter is used to specify the total number of differences allowed in the sequence. The default is 0.\n");
170 m->mothurOut("The bdiffs parameter is used to specify the number of differences allowed in the barcode. The default is 0.\n");
171 m->mothurOut("The pdiffs parameter is used to specify the number of differences allowed in the primer. The default is 0.\n");
172 m->mothurOut("The qfile parameter .....\n");
173 m->mothurOut("The qthreshold parameter .... The default is 0.\n");
174 m->mothurOut("The qaverage parameter .... The default is 0.\n");
175 m->mothurOut("The allfiles parameter .... The default is F.\n");
176 m->mothurOut("The qtrim parameter .... The default is F.\n");
177 m->mothurOut("The trim.seqs command should be in the following format: \n");
178 m->mothurOut("trim.seqs(fasta=yourFastaFile, flip=yourFlip, oligos=yourOligos, maxambig=yourMaxambig, \n");
179 m->mothurOut("maxhomop=yourMaxhomop, minlength=youMinlength, maxlength=yourMaxlength) \n");
180 m->mothurOut("Example trim.seqs(fasta=abrecovery.fasta, flip=..., oligos=..., maxambig=..., maxhomop=..., minlength=..., maxlength=...).\n");
181 m->mothurOut("Note: No spaces between parameter labels (i.e. fasta), '=' and parameters (i.e.yourFasta).\n");
182 m->mothurOut("For more details please check out the wiki http://www.mothur.org/wiki/Trim.seqs .\n\n");
185 catch(exception& e) {
186 m->errorOut(e, "TrimSeqsCommand", "help");
192 //***************************************************************************************************************
194 TrimSeqsCommand::~TrimSeqsCommand(){ /* do nothing */ }
196 //***************************************************************************************************************
198 int TrimSeqsCommand::execute(){
201 if (abort == true) { return 0; }
203 numFPrimers = 0; //this needs to be initialized
206 string trimSeqFile = outputDir + getRootName(getSimpleName(fastaFile)) + "trim.fasta";
207 outputNames.push_back(trimSeqFile);
208 string scrapSeqFile = outputDir + getRootName(getSimpleName(fastaFile)) + "scrap.fasta";
209 outputNames.push_back(scrapSeqFile);
210 string groupFile = outputDir + getRootName(getSimpleName(fastaFile)) + "groups";
212 vector<string> fastaFileNames;
214 outputNames.push_back(groupFile);
215 getOligos(fastaFileNames);
218 if(qFileName != "") { setLines(qFileName, qLines); }
221 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
224 openInputFile(fastaFile, inFASTA);
225 int numSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
228 lines.push_back(new linePair(0, numSeqs));
230 driverCreateTrim(fastaFile, qFileName, trimSeqFile, scrapSeqFile, groupFile, fastaFileNames, lines[0], lines[0]);
232 for (int j = 0; j < fastaFileNames.size(); j++) {
233 rename((fastaFileNames[j] + toString(getpid()) + ".temp").c_str(), fastaFileNames[j].c_str());
237 setLines(fastaFile, lines);
238 if(qFileName == "") { qLines = lines; }
240 createProcessesCreateTrim(fastaFile, qFileName, trimSeqFile, scrapSeqFile, groupFile, fastaFileNames);
242 rename((trimSeqFile + toString(processIDS[0]) + ".temp").c_str(), trimSeqFile.c_str());
243 rename((scrapSeqFile + toString(processIDS[0]) + ".temp").c_str(), scrapSeqFile.c_str());
244 rename((groupFile + toString(processIDS[0]) + ".temp").c_str(), groupFile.c_str());
245 for (int j = 0; j < fastaFileNames.size(); j++) {
246 rename((fastaFileNames[j] + toString(processIDS[0]) + ".temp").c_str(), fastaFileNames[j].c_str());
249 for(int i=1;i<processors;i++){
250 appendFiles((trimSeqFile + toString(processIDS[i]) + ".temp"), trimSeqFile);
251 remove((trimSeqFile + toString(processIDS[i]) + ".temp").c_str());
252 appendFiles((scrapSeqFile + toString(processIDS[i]) + ".temp"), scrapSeqFile);
253 remove((scrapSeqFile + toString(processIDS[i]) + ".temp").c_str());
254 appendFiles((groupFile + toString(processIDS[i]) + ".temp"), groupFile);
255 remove((groupFile + toString(processIDS[i]) + ".temp").c_str());
256 for (int j = 0; j < fastaFileNames.size(); j++) {
257 appendFiles((fastaFileNames[j] + toString(processIDS[i]) + ".temp"), fastaFileNames[j]);
258 remove((fastaFileNames[j] + toString(processIDS[i]) + ".temp").c_str());
263 if (m->control_pressed) { return 0; }
266 openInputFile(fastafileNames[s], inFASTA);
267 numSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
270 lines.push_back(new linePair(0, numSeqs));
272 driverCreateSummary(fastafile, qFileName, trimSeqFile, scrapSeqFile, groupFile, fastaFileNames, lines[0], lines[0]);
274 if (m->control_pressed) { return 0; }
278 for(int i=0;i<fastaFileNames.size();i++){
281 openInputFile(getRootName(fastaFile) + groupVector[i] + ".fasta", inFASTA);
283 openOutputFile(outputDir + getRootName(getSimpleName(fastaFile)) + groupVector[i] + ".groups", outGroups);
284 outputNames.push_back(outputDir + getRootName(getSimpleName(fastaFile)) + groupVector[i] + ".groups");
286 while(!inFASTA.eof()){
287 if(inFASTA.get() == '>'){
289 outGroups << seqName << '\t' << groupVector[i] << endl;
291 while (!inFASTA.eof()) { char c = inFASTA.get(); if (c == 10 || c == 13){ break; } }
297 if (m->control_pressed) {
298 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
302 m->mothurOutEndLine();
303 m->mothurOut("Output File Names: "); m->mothurOutEndLine();
304 for (int i = 0; i < outputNames.size(); i++) { m->mothurOut(outputNames[i]); m->mothurOutEndLine(); }
305 m->mothurOutEndLine();
310 catch(exception& e) {
311 m->errorOut(e, "TrimSeqsCommand", "execute");
316 /**************************************************************************************/
317 int TrimSeqsCommand::driverCreateTrim(string filename, string qFileName, string trimFile, string scrapFile, string groupFile, vector<string> fastaNames, linePair* line, linePair* qline) {
321 int able = openOutputFile(trimFile, outFASTA);
324 openOutputFile(scrapFile, scrapFASTA);
327 vector<ofstream*> fastaFileNames;
328 if (oligoFile != "") {
329 openOutputFile(groupFile, outGroups);
330 for (int i = 0; i < fastaNames.size(); i++) {
331 fastaFileNames.push_back(new ofstream((fastaNames[i] + toString(getpid()) + ".temp").c_str(), ios::ate));
336 openInputFile(filename, inFASTA);
339 if(qFileName != "") { openInputFile(qFileName, qFile); }
341 qFile.seekg(qline->start);
342 inFASTA.seekg(line->start);
344 for(int i=0;i<line->num;i++){
346 if (m->control_pressed) {
350 if (oligoFile != "") { outGroups.close(); }
351 if(qFileName != "") { qFile.close(); }
352 for(int i=0;i<fastaFileNames.size();i++){
353 fastaFileNames[i]->close();
354 delete fastaFileNames[i];
356 for (int i = 0; i < outputNames.size(); i++) { remove(outputNames[i].c_str()); }
362 Sequence currSeq(inFASTA);
364 string origSeq = currSeq.getUnaligned();
367 string trashCode = "";
368 int currentSeqsDiffs = 0;
371 if(qThreshold != 0) { success = stripQualThreshold(currSeq, qFile); }
372 else if(qAverage != 0) { success = cullQualAverage(currSeq, qFile); }
373 if ((!qtrim) && (origSeq.length() != currSeq.getUnaligned().length())) {
374 success = 0; //if you don't want to trim and the sequence does not meet quality requirements, move to scrap
376 if(!success) { trashCode += 'q'; }
379 if(barcodes.size() != 0){
380 success = stripBarcode(currSeq, group);
381 if(!success){ trashCode += 'b'; }
382 else{ currentSeqsDiffs += currentSeqsTdiffs; }
385 if(numFPrimers != 0){
386 success = stripForward(currSeq);
387 if(!success){ trashCode += 'f'; }
388 else{ currentSeqsDiffs += currentSeqsTdiffs; }
391 if (currentSeqsDiffs > tdiffs) { trashCode += 't'; }
393 if(numRPrimers != 0){
394 success = stripReverse(currSeq);
395 if(!success){ trashCode += 'r'; }
398 if(minLength > 0 || maxLength > 0){
399 success = cullLength(currSeq);
400 if(!success){ trashCode += 'l'; }
403 success = cullHomoP(currSeq);
404 if(!success){ trashCode += 'h'; }
407 success = cullAmbigs(currSeq);
408 if(!success){ trashCode += 'n'; }
411 if(flip){ currSeq.reverseComplement(); } // should go last
413 if(trashCode.length() == 0){
414 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.
415 currSeq.printSequence(outFASTA);
416 if(barcodes.size() != 0){
417 outGroups << currSeq.getName() << '\t' << groupVector[group] << endl;
420 currSeq.printSequence(*fastaFileNames[group]);
425 currSeq.setName(currSeq.getName() + '|' + trashCode);
426 currSeq.setUnaligned(origSeq);
427 currSeq.printSequence(scrapFASTA);
436 if (oligoFile != "") { outGroups.close(); }
437 if(qFileName != "") { qFile.close(); }
439 for(int i=0;i<fastaFileNames.size();i++){
440 fastaFileNames[i]->close();
441 delete fastaFileNames[i];
446 catch(exception& e) {
447 m->errorOut(e, "TrimSeqsCommand", "driverCreateTrim");
451 /**************************************************************************************************/
452 int TrimSeqsCommand::createProcessesCreateTrim(string filename, string qFileName, string trimFile, string scrapFile, string groupFile, vector<string> fastaNames) {
454 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
459 //loop through and create all the processes you want
460 while (process != processors) {
464 processIDS.push_back(pid); //create map from line number to pid so you can append files in correct order later
467 driverCreateTrim(filename, qFileName, (trimFile + toString(getpid()) + ".temp"), (scrapFile + toString(getpid()) + ".temp"), (groupFile + toString(getpid()) + ".temp"), fastaNames, lines[process], qLines[process]);
469 }else { m->mothurOut("unable to spawn the necessary processes."); m->mothurOutEndLine(); exit(0); }
472 //force parent to wait until all the processes are done
473 for (int i=0;i<processors;i++) {
474 int temp = processIDS[i];
481 catch(exception& e) {
482 m->errorOut(e, "TrimSeqsCommand", "createProcessesCreateTrim");
486 /**************************************************************************************************/
488 int TrimSeqsCommand::setLines(string filename, vector<linePair*>& lines) {
493 vector<long int> positions;
496 openInputFile(filename, inFASTA);
499 while(!inFASTA.eof()){
500 input = getline(inFASTA);
502 if (input.length() != 0) {
503 if(input[0] == '>'){ long int pos = inFASTA.tellg(); positions.push_back(pos - input.length() - 1); }
508 int numFastaSeqs = positions.size();
513 //get num bytes in file
514 pFile = fopen (filename.c_str(),"rb");
515 if (pFile==NULL) perror ("Error opening file");
517 fseek (pFile, 0, SEEK_END);
522 int numSeqsPerProcessor = numFastaSeqs / processors;
524 for (int i = 0; i < processors; i++) {
526 long int startPos = positions[ i * numSeqsPerProcessor ];
527 if(i == processors - 1){
528 numSeqsPerProcessor = numFastaSeqs - i * numSeqsPerProcessor;
530 long int myEnd = positions[ (i+1) * numSeqsPerProcessor ];
532 lines.push_back(new linePair(startPos, numSeqsPerProcessor));
537 catch(exception& e) {
538 m->errorOut(e, "TrimSeqsCommand", "setLines");
542 //***************************************************************************************************************
544 void TrimSeqsCommand::getOligos(vector<string>& outFASTAVec){ //vector<ofstream*>& outFASTAVec
547 openInputFile(oligoFile, inOligos);
551 string type, oligo, group;
554 while(!inOligos.eof()){
558 while (!inOligos.eof()) { char c = inOligos.get(); if (c == 10 || c == 13){ break; } } // get rest of line if there's any crap there
563 for(int i=0;i<oligo.length();i++){
564 oligo[i] = toupper(oligo[i]);
565 if(oligo[i] == 'U') { oligo[i] = 'T'; }
568 if(type == "forward"){
569 forPrimer.push_back(oligo);
571 else if(type == "reverse"){
572 Sequence oligoRC("reverse", oligo);
573 oligoRC.reverseComplement();
574 revPrimer.push_back(oligoRC.getUnaligned());
576 else if(type == "barcode"){
578 barcodes[oligo]=index++;
579 groupVector.push_back(group);
582 //outFASTAVec.push_back(new ofstream((outputDir + getRootName(getSimpleName(fastaFile)) + group + ".fasta").c_str(), ios::ate));
583 outputNames.push_back((outputDir + getRootName(getSimpleName(fastaFile)) + group + ".fasta"));
584 outFASTAVec.push_back((outputDir + getRootName(getSimpleName(fastaFile)) + group + ".fasta"));
592 numFPrimers = forPrimer.size();
593 numRPrimers = revPrimer.size();
596 catch(exception& e) {
597 m->errorOut(e, "TrimSeqsCommand", "getOligos");
601 //***************************************************************************************************************
603 bool TrimSeqsCommand::stripBarcode(Sequence& seq, int& group){
605 string rawSequence = seq.getUnaligned();
606 bool success = 0; //guilty until proven innocent
608 //can you find the barcode
609 for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
610 string oligo = it->first;
611 if(rawSequence.length() < oligo.length()){ //let's just assume that the barcodes are the same length
616 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
618 seq.setUnaligned(rawSequence.substr(oligo.length()));
624 //if you found the barcode or if you don't want to allow for diffs
625 if ((bdiffs == 0) || (success == 1)) { return success; }
627 else { //try aligning and see if you can find it
629 Alignment* alignment;
630 if (barcodes.size() > 0) { //assumes barcodes are all the same length
631 map<string,int>::iterator it=barcodes.begin();
632 string temp = it->first;
634 alignment = new NeedlemanOverlap(-2.0, 1.0, -1.0, (temp.length()+bdiffs+1));
635 }else{ alignment = NULL; }
638 //can you find the barcode
639 for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
640 string oligo = it->first;
641 int length = oligo.length();
643 if(rawSequence.length() < oligo.length()){ //let's just assume that the barcodes are the same length
648 //use needleman to align first barcode.length()+numdiffs of sequence to each barcode
649 alignment->align(oligo, rawSequence.substr(0,length+bdiffs));
650 oligo = alignment->getSeqAAln();
651 string temp = alignment->getSeqBAln();
652 //cout << "barcode = " << oligo << " raw = " << rawSequence.substr(0,oligo.length()) << " raw aligned = " << temp << endl;
655 if(compareDNASeq(oligo, temp, length, newStart, bdiffs)){
657 seq.setUnaligned(rawSequence.substr(newStart));
663 if (alignment != NULL) { delete alignment; }
668 catch(exception& e) {
669 m->errorOut(e, "TrimSeqsCommand", "stripBarcode");
675 //***************************************************************************************************************
677 bool TrimSeqsCommand::stripForward(Sequence& seq){
679 string rawSequence = seq.getUnaligned();
680 bool success = 0; //guilty until proven innocent
682 for(int i=0;i<numFPrimers;i++){
683 string oligo = forPrimer[i];
685 if(rawSequence.length() < oligo.length()){
690 if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
691 seq.setUnaligned(rawSequence.substr(oligo.length()));
697 //if you found the primer or if you don't want to allow for diffs
698 if ((pdiffs == 0) || (success == 1)) { return success; }
700 else { //try aligning and see if you can find it
702 Alignment* alignment;
703 if (numFPrimers > 0) { alignment = new NeedlemanOverlap(-2.0, 1.0, -1.0, (forPrimer[0].length()+pdiffs+1)); }
704 else{ alignment = NULL; }
705 //can you find the primer
706 for(int i=0;i<numFPrimers;i++){
707 string oligo = forPrimer[i];
708 int length = oligo.length();
710 if(rawSequence.length() < oligo.length()){
715 //resize if neccessary
716 if ((length+pdiffs+1) > alignment->getnRows()) { alignment->resize(length+pdiffs+1); }
718 //use needleman to align first primer.length()+numdiffs of sequence to each primer
719 alignment->align(oligo, rawSequence.substr(0,length+pdiffs));
720 oligo = alignment->getSeqAAln();
721 string temp = alignment->getSeqBAln();
724 if(compareDNASeq(oligo, temp, length, newStart, pdiffs)){
725 seq.setUnaligned(rawSequence.substr(newStart));
731 if (alignment != NULL) { delete alignment; }
737 catch(exception& e) {
738 m->errorOut(e, "TrimSeqsCommand", "stripForward");
743 //***************************************************************************************************************
745 bool TrimSeqsCommand::stripReverse(Sequence& seq){
747 string rawSequence = seq.getUnaligned();
748 bool success = 0; //guilty until proven innocent
750 for(int i=0;i<numRPrimers;i++){
751 string oligo = revPrimer[i];
753 if(rawSequence.length() < oligo.length()){
758 if(compareDNASeq(oligo, rawSequence.substr(rawSequence.length()-oligo.length(),oligo.length()))){
759 seq.setUnaligned(rawSequence.substr(0,rawSequence.length()-oligo.length()));
767 catch(exception& e) {
768 m->errorOut(e, "TrimSeqsCommand", "stripReverse");
773 //***************************************************************************************************************
775 bool TrimSeqsCommand::cullLength(Sequence& seq){
778 int length = seq.getNumBases();
779 bool success = 0; //guilty until proven innocent
781 if(length >= minLength && maxLength == 0) { success = 1; }
782 else if(length >= minLength && length <= maxLength) { success = 1; }
783 else { success = 0; }
788 catch(exception& e) {
789 m->errorOut(e, "TrimSeqsCommand", "cullLength");
795 //***************************************************************************************************************
797 bool TrimSeqsCommand::cullHomoP(Sequence& seq){
799 int longHomoP = seq.getLongHomoPolymer();
800 bool success = 0; //guilty until proven innocent
802 if(longHomoP <= maxHomoP){ success = 1; }
803 else { success = 0; }
807 catch(exception& e) {
808 m->errorOut(e, "TrimSeqsCommand", "cullHomoP");
814 //***************************************************************************************************************
816 bool TrimSeqsCommand::cullAmbigs(Sequence& seq){
818 int numNs = seq.getAmbigBases();
819 bool success = 0; //guilty until proven innocent
821 if(numNs <= maxAmbig) { success = 1; }
822 else { success = 0; }
826 catch(exception& e) {
827 m->errorOut(e, "TrimSeqsCommand", "cullAmbigs");
833 //***************************************************************************************************************
835 bool TrimSeqsCommand::compareDNASeq(string oligo, string seq){
838 int length = oligo.length();
840 for(int i=0;i<length;i++){
842 if(oligo[i] != seq[i]){
843 if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C') { success = 0; }
844 else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N')) { success = 0; }
845 else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G')) { success = 0; }
846 else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T')) { success = 0; }
847 else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A')) { success = 0; }
848 else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
849 else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A')) { success = 0; }
850 else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
851 else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
852 else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G')) { success = 0; }
853 else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C')) { success = 0; }
854 else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G')) { success = 0; }
856 if(success == 0) { break; }
865 catch(exception& e) {
866 m->errorOut(e, "TrimSeqsCommand", "compareDNASeq");
871 //***************************************************************************************************************
873 bool TrimSeqsCommand::compareDNASeq(string oligo, string seq, int numBases, int& end, int diffs){
876 int length = oligo.length();
882 if ((oligo[0] == '-') || (oligo[0] == '.')) { success = 0; return success; } //no gaps allowed at beginning
885 for(int i=0;i<length;i++){
887 if ((oligo[i] != '-') && (oligo[i] != '.')) { countBases++; }
889 if(oligo[i] != seq[i]){
890 if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C' || oligo[i] == '-' || oligo[i] == '.') { countDiffs++; }
891 else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N')) { countDiffs++; }
892 else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G')) { countDiffs++; }
893 else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T')) { countDiffs++; }
894 else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A')) { countDiffs++; }
895 else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
896 else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A')) { countDiffs++; }
897 else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G')) { countDiffs++; }
898 else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
899 else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
900 else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C')) { countDiffs++; }
901 else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G')) { countDiffs++; }
903 if(countDiffs > diffs) { success = 0; break; }
909 if (countBases >= numBases) { end = countBases; break; } //stop checking after end of barcode or primer
912 //if it's a success we want to check for total diffs in driver, so save it.
913 if (success == 1) { currentSeqsTdiffs = countDiffs; }
917 catch(exception& e) {
918 m->errorOut(e, "TrimSeqsCommand", "compareDNASeq");
923 //***************************************************************************************************************
925 bool TrimSeqsCommand::stripQualThreshold(Sequence& seq, ifstream& qFile){
927 string rawSequence = seq.getUnaligned();
928 int seqLength; // = rawSequence.length();
929 string name, temp, temp2;
931 qFile >> name >> temp;
933 splitAtEquals(temp2, temp); //separates length=242, temp=length, temp2=242
934 convert(temp, seqLength); //converts string to int
936 if (name.length() != 0) { if(name.substr(1) != seq.getName()) { m->mothurOut("sequence name mismatch btwn fasta and qual file"); m->mothurOutEndLine(); } }
937 while (!qFile.eof()) { char c = qFile.get(); if (c == 10 || c == 13){ break; } }
942 for(int i=0;i<seqLength;i++){
945 if(score <= qThreshold){
950 for(int i=end+1;i<seqLength;i++){
954 seq.setUnaligned(rawSequence.substr(0,end));
958 catch(exception& e) {
959 m->errorOut(e, "TrimSeqsCommand", "stripQualThreshold");
964 //***************************************************************************************************************
966 bool TrimSeqsCommand::cullQualAverage(Sequence& seq, ifstream& qFile){
968 string rawSequence = seq.getUnaligned();
969 int seqLength = seq.getNumBases();
970 bool success = 0; //guilty until proven innocent
974 if (name[0] == '>') { if(name.substr(1) != seq.getName()) { m->mothurOut("sequence name mismatch btwn fasta: " + seq.getName() + " and qual file: " + name); m->mothurOutEndLine(); } }
976 while (!qFile.eof()) { char c = qFile.get(); if (c == 10 || c == 13){ break; } }
981 for(int i=0;i<seqLength;i++){
985 average /= seqLength;
987 if(average >= qAverage) { success = 1; }
988 else { success = 0; }
992 catch(exception& e) {
993 m->errorOut(e, "TrimSeqsCommand", "cullQualAverage");
998 //***************************************************************************************************************