#include "trimseqscommand.h"
#include "needlemanoverlap.hpp"
-#include "nast.hpp"
//***************************************************************************************************************
else {
//valid paramters for this command
string AlignArray[] = {"fasta", "flip", "oligos", "maxambig", "maxhomop", "minlength", "maxlength", "qfile",
- "qthreshold", "qaverage", "allfiles", "qtrim","diffs", "processors", "outputdir","inputdir"};
+ "qthreshold", "qaverage", "allfiles", "qtrim","tdiffs", "pdiffs", "bdiffs", "processors", "outputdir","inputdir"};
vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
temp = validParameter.validFile(parameters, "maxlength", false); if (temp == "not found") { temp = "0"; }
convert(temp, maxLength);
- temp = validParameter.validFile(parameters, "diffs", false); if (temp == "not found") { temp = "0"; }
- convert(temp, diffs);
+
+ temp = validParameter.validFile(parameters, "bdiffs", false); if (temp == "not found") { temp = "0"; }
+ convert(temp, bdiffs);
+
+ temp = validParameter.validFile(parameters, "pdiffs", false); if (temp == "not found") { temp = "0"; }
+ convert(temp, pdiffs);
+
+ temp = validParameter.validFile(parameters, "tdiffs", false); if (temp == "not found") { int tempTotal = pdiffs + bdiffs; temp = toString(tempTotal); }
+ convert(temp, tdiffs);
+
+ if(tdiffs == 0){ tdiffs = bdiffs + pdiffs; }
temp = validParameter.validFile(parameters, "qfile", true);
if (temp == "not found") { qFileName = ""; }
m->mothurOut("The maxhomop parameter .... The default is 0.\n");
m->mothurOut("The minlength parameter .... The default is 0.\n");
m->mothurOut("The maxlength parameter .... The default is 0.\n");
- m->mothurOut("The diffs parameter .... The default is 0.\n");
+ m->mothurOut("The tdiffs parameter is used to specify the total number of differences allowed in the sequence. The default is pdiffs + bdiffs.\n");
+ m->mothurOut("The bdiffs parameter is used to specify the number of differences allowed in the barcode. The default is 0.\n");
+ m->mothurOut("The pdiffs parameter is used to specify the number of differences allowed in the primer. The default is 0.\n");
m->mothurOut("The qfile parameter .....\n");
m->mothurOut("The qthreshold parameter .... The default is 0.\n");
m->mothurOut("The qaverage parameter .... The default is 0.\n");
if (m->control_pressed) { return 0; }
#else
ifstream inFASTA;
- openInputFile(fastafileNames[s], inFASTA);
- numSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
+ openInputFile(fastaFile, inFASTA);
+ int numSeqs=count(istreambuf_iterator<char>(inFASTA),istreambuf_iterator<char>(), '>');
inFASTA.close();
lines.push_back(new linePair(0, numSeqs));
- driverCreateSummary(fastafile, qFileName, trimSeqFile, scrapSeqFile, groupFile, fastaFileNames, lines[0], lines[0]);
+ driverCreateTrim(fastaFile, qFileName, trimSeqFile, scrapSeqFile, groupFile, fastaFileNames, lines[0], lines[0]);
if (m->control_pressed) { return 0; }
#endif
if (oligoFile != "") {
openOutputFile(groupFile, outGroups);
for (int i = 0; i < fastaNames.size(); i++) {
+ #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
fastaFileNames.push_back(new ofstream((fastaNames[i] + toString(getpid()) + ".temp").c_str(), ios::ate));
+ #else
+ fastaFileNames.push_back(new ofstream((fastaNames[i] + toString(i) + ".temp").c_str(), ios::ate));
+ #endif
}
}
return 0;
}
- bool success = 1;
+ int success = 1;
Sequence currSeq(inFASTA);
if (origSeq != "") {
int group;
string trashCode = "";
+ int currentSeqsDiffs = 0;
if(qFileName != ""){
if(qThreshold != 0) { success = stripQualThreshold(currSeq, qFile); }
if(barcodes.size() != 0){
success = stripBarcode(currSeq, group);
- if(!success){ trashCode += 'b'; }
+ if(success > bdiffs){ trashCode += 'b'; }
+ else{ currentSeqsDiffs += success; }
}
-
+
if(numFPrimers != 0){
success = stripForward(currSeq);
- if(!success){ trashCode += 'f'; }
+ if(success > pdiffs){ trashCode += 'f'; }
+ else{ currentSeqsDiffs += success; }
}
-
+
+ if (currentSeqsDiffs > tdiffs) { trashCode += 't'; }
+
if(numRPrimers != 0){
success = stripReverse(currSeq);
if(!success){ trashCode += 'r'; }
if(flip){ currSeq.reverseComplement(); } // should go last
if(trashCode.length() == 0){
- 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.
+ currSeq.setAligned(currSeq.getUnaligned());
currSeq.printSequence(outFASTA);
if(barcodes.size() != 0){
outGroups << currSeq.getName() << '\t' << groupVector[group] << endl;
else{
currSeq.setName(currSeq.getName() + '|' + trashCode);
currSeq.setUnaligned(origSeq);
+ currSeq.setAligned(origSeq);
currSeq.printSequence(scrapFASTA);
}
}
}
//***************************************************************************************************************
-bool TrimSeqsCommand::stripBarcode(Sequence& seq, int& group){
+int TrimSeqsCommand::stripBarcode(Sequence& seq, int& group){
try {
+
string rawSequence = seq.getUnaligned();
- bool success = 0; //guilty until proven innocent
+ int success = bdiffs + 1; //guilty until proven innocent
//can you find the barcode
for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
string oligo = it->first;
if(rawSequence.length() < oligo.length()){ //let's just assume that the barcodes are the same length
- success = 0;
- break;
+ success = bdiffs + 10; //if the sequence is shorter than the barcode then bail out
+ break;
}
if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
group = it->second;
seq.setUnaligned(rawSequence.substr(oligo.length()));
- success = 1;
+ success = 0;
break;
}
}
//if you found the barcode or if you don't want to allow for diffs
- if ((diffs == 0) || (success == 1)) { return success; }
+// cout << success;
+ if ((bdiffs == 0) || (success == 0)) { return success; }
else { //try aligning and see if you can find it
+// cout << endl;
+
+ int maxLength = 0;
+
+ Alignment* alignment;
+ if (barcodes.size() > 0) {
+ map<string,int>::iterator it=barcodes.begin();
+
+ for(it;it!=barcodes.end();it++){
+ if(it->first.length() > maxLength){
+ maxLength = it->first.length();
+ }
+ }
+ alignment = new NeedlemanOverlap(-2.0, 1.0, -1.0, (maxLength+bdiffs+1));
+
+ }else{ alignment = NULL; }
+
//can you find the barcode
+ int minDiff = 1e6;
+ int minCount = 1;
+ int minGroup = -1;
+ int minPos = 0;
+
for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
string oligo = it->first;
- if(rawSequence.length() < oligo.length()){ //let's just assume that the barcodes are the same length
- success = 0;
+// int length = oligo.length();
+
+ if(rawSequence.length() < maxLength){ //let's just assume that the barcodes are the same length
+ success = bdiffs + 10;
break;
}
//use needleman to align first barcode.length()+numdiffs of sequence to each barcode
- Alignment* alignment = new NeedlemanOverlap(-2.0, 1.0, -1.0, (oligo.length()+diffs+1));
- Sequence* templateSeq = new Sequence("temp", rawSequence.substr(0,(oligo.length()+diffs)));
- Sequence* candidateSeq = new Sequence("temp2", oligo);
- Nast nast(alignment, candidateSeq, templateSeq);
+ alignment->align(oligo, rawSequence.substr(0,oligo.length()+bdiffs));
+ oligo = alignment->getSeqAAln();
+ string temp = alignment->getSeqBAln();
+
+ int alnLength = oligo.length();
- oligo = candidateSeq->getAligned();
- cout << "barcode = " << oligo << " raw = " << rawSequence.substr(0,(oligo.length())) << endl;
- delete alignment;
- delete templateSeq;
- delete candidateSeq;
+ for(int i=oligo.length()-1;i>=0;i--){
+ if(oligo[i] != '-'){ alnLength = i+1; break; }
+ }
+ oligo = oligo.substr(0,alnLength);
+ temp = temp.substr(0,alnLength);
- if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
- group = it->second;
- seq.setUnaligned(rawSequence.substr(0,oligo.length()));
- success = 1;
- break;
+ int newStart=0;
+ int numDiff = countDiffs(oligo, temp);
+ if(numDiff < minDiff){
+ minDiff = numDiff;
+ minCount = 1;
+ minGroup = it->second;
+ minPos = 0;
+ for(int i=0;i<alnLength;i++){
+ if(temp[i] != '-'){
+ minPos++;
+ }
+ }
}
+ else if(numDiff == minDiff){
+ minCount++;
+ }
+
}
+
+ if(minDiff > bdiffs) { success = minDiff; } //no good matches
+ else if(minCount > 1) { success = bdiffs + 100; } //can't tell the difference between multiple barcodes
+ else{ //use the best match
+ group = minGroup;
+ seq.setUnaligned(rawSequence.substr(minPos));
+ success = minDiff;
+ }
+
+ if (alignment != NULL) { delete alignment; }
+
}
+// cout << success << endl;
+
return success;
}
//***************************************************************************************************************
-bool TrimSeqsCommand::stripForward(Sequence& seq){
+int TrimSeqsCommand::stripForward(Sequence& seq){
try {
string rawSequence = seq.getUnaligned();
- bool success = 0; //guilty until proven innocent
+ int success = pdiffs + 1; //guilty until proven innocent
+ //can you find the primer
for(int i=0;i<numFPrimers;i++){
string oligo = forPrimer[i];
-
- if(rawSequence.length() < oligo.length()){
- success = 0;
+
+ if(rawSequence.length() < oligo.length()){
+ success = pdiffs + 1;
break;
}
if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
seq.setUnaligned(rawSequence.substr(oligo.length()));
- success = 1;
+ success = 0;
break;
}
}
- //if you found the primer or if you don't want to allow for diffs
- if ((diffs == 0) || (success == 1)) { return success; }
+ //if you found the barcode or if you don't want to allow for diffs
+// cout << success;
+ if ((pdiffs == 0) || (success == 0)) { return success; }
else { //try aligning and see if you can find it
- //can you find the primer
+// cout << endl;
+
+ int maxLength = 0;
+
+ Alignment* alignment;
+ if (numFPrimers > 0) {
+
+ for(int i=0;i<numFPrimers;i++){
+ if(forPrimer[i].length() > maxLength){
+ maxLength = forPrimer[i].length();
+ }
+ }
+ alignment = new NeedlemanOverlap(-2.0, 1.0, -1.0, (maxLength+pdiffs+1));
+
+ }else{ alignment = NULL; }
+
+ //can you find the barcode
+ int minDiff = 1e6;
+ int minCount = 1;
+ int minPos = 0;
+
for(int i=0;i<numFPrimers;i++){
string oligo = forPrimer[i];
- if(rawSequence.length() < oligo.length()){
- success = 0;
+
+ if(rawSequence.length() < maxLength){
+ success = pdiffs + 100;
break;
}
- //use needleman to align first primer.length()+numdiffs of sequence to each primer
- Alignment* alignment = new NeedlemanOverlap(-2.0, 1.0, -1.0, (oligo.length()+diffs+1));
- Sequence* templateSeq = new Sequence("temp", rawSequence.substr(0,(oligo.length()+diffs)));
- Sequence* candidateSeq = new Sequence("temp2", oligo);
- Nast nast(alignment, candidateSeq, templateSeq);
-
- oligo = candidateSeq->getAligned();
-
- delete alignment;
- delete templateSeq;
- delete candidateSeq;
+ //use needleman to align first barcode.length()+numdiffs of sequence to each barcode
+ alignment->align(oligo, rawSequence.substr(0,oligo.length()+pdiffs));
+ oligo = alignment->getSeqAAln();
+ string temp = alignment->getSeqBAln();
+
+ int alnLength = oligo.length();
- if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()))){
- seq.setUnaligned(rawSequence.substr(0,oligo.length()));
- success = 1;
- break;
+ for(int i=oligo.length()-1;i>=0;i--){
+ if(oligo[i] != '-'){ alnLength = i+1; break; }
+ }
+ oligo = oligo.substr(0,alnLength);
+ temp = temp.substr(0,alnLength);
+
+ int newStart=0;
+ int numDiff = countDiffs(oligo, temp);
+ if(numDiff < minDiff){
+ minDiff = numDiff;
+ minCount = 1;
+ minPos = 0;
+ for(int i=0;i<alnLength;i++){
+ if(temp[i] != '-'){
+ minPos++;
+ }
+ }
}
+ else if(numDiff == minDiff){
+ minCount++;
+ }
+
}
+ if(minDiff > pdiffs) { success = minDiff; }
+ else if(minCount > 1) { success = pdiffs + 10; }
+ else{
+ seq.setUnaligned(rawSequence.substr(minPos));
+ success = minDiff;
+ }
+
+ if (alignment != NULL) { delete alignment; }
+
}
-
return success;
-
+
}
catch(exception& e) {
m->errorOut(e, "TrimSeqsCommand", "stripForward");
}
}
+//***************************************************************************************************************
+
+int TrimSeqsCommand::countDiffs(string oligo, string seq){
+ try {
+
+ int length = oligo.length();
+ int countDiffs = 0;
+
+ for(int i=0;i<length;i++){
+
+ if(oligo[i] != seq[i]){
+ if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C' || oligo[i] == '-' || oligo[i] == '.') { countDiffs++; }
+ else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N')) { countDiffs++; }
+ else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G')) { countDiffs++; }
+ else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T')) { countDiffs++; }
+ else if(oligo[i] == 'M' && (seq[i] != 'C' && seq[i] != 'A')) { countDiffs++; }
+ else if(oligo[i] == 'K' && (seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
+ else if(oligo[i] == 'W' && (seq[i] != 'T' && seq[i] != 'A')) { countDiffs++; }
+ else if(oligo[i] == 'S' && (seq[i] != 'C' && seq[i] != 'G')) { countDiffs++; }
+ else if(oligo[i] == 'B' && (seq[i] != 'C' && seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
+ else if(oligo[i] == 'D' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'G')) { countDiffs++; }
+ else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C')) { countDiffs++; }
+ else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G')) { countDiffs++; }
+
+ }
+ }
+
+ return countDiffs;
+ }
+ catch(exception& e) {
+ m->errorOut(e, "TrimSeqsCommand", "countDiffs");
+ exit(1);
+ }
+}
//***************************************************************************************************************
bool TrimSeqsCommand::stripQualThreshold(Sequence& seq, ifstream& qFile){