]> git.donarmstrong.com Git - mothur.git/blobdiff - trimseqscommand.cpp
modified trim.seqs to account for primers of different lengths
[mothur.git] / trimseqscommand.cpp
index ef7dfdd0f45d649cc6296d91e106e75b9a6fd167..b12da1cfe126bbdfc562172772093cf84e733457 100644 (file)
@@ -8,6 +8,7 @@
  */
 
 #include "trimseqscommand.h"
+#include "needlemanoverlap.hpp"
 
 //***************************************************************************************************************
 
@@ -22,7 +23,7 @@ TrimSeqsCommand::TrimSeqsCommand(string option)  {
                else {
                        //valid paramters for this command
                        string AlignArray[] =  {"fasta", "flip", "oligos", "maxambig", "maxhomop", "minlength", "maxlength", "qfile", 
-                                                                       "qthreshold", "qaverage", "allfiles", "qtrim", "processors", "outputdir","inputdir"};
+                                                                       "qthreshold", "qaverage", "allfiles", "qtrim","tdiffs", "pdiffs", "bdiffs", "processors", "outputdir","inputdir"};
                        
                        vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
                        
@@ -103,6 +104,15 @@ TrimSeqsCommand::TrimSeqsCommand(string option)  {
                        temp = validParameter.validFile(parameters, "maxlength", false);        if (temp == "not found") { temp = "0"; }
                        convert(temp, maxLength);
                        
+                       temp = validParameter.validFile(parameters, "tdiffs", false);           if (temp == "not found") { temp = "0"; }
+                       convert(temp, tdiffs);
+                       
+                       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, "qfile", true);     
                        if (temp == "not found")        {       qFileName = "";         }
                        else if(temp == "not open")     {       abort = true;           }
@@ -148,7 +158,7 @@ TrimSeqsCommand::TrimSeqsCommand(string option)  {
 void TrimSeqsCommand::help(){
        try {
                m->mothurOut("The trim.seqs command reads a fastaFile and creates .....\n");
-               m->mothurOut("The trim.seqs command parameters are fasta, flip, oligos, maxambig, maxhomop, minlength, maxlength, qfile, qthreshold, qaverage, qtrim and allfiles.\n");
+               m->mothurOut("The trim.seqs command parameters are fasta, flip, oligos, maxambig, maxhomop, minlength, maxlength, qfile, qthreshold, qaverage, diffs, qtrim and allfiles.\n");
                m->mothurOut("The fasta parameter is required.\n");
                m->mothurOut("The flip parameter .... The default is 0.\n");
                m->mothurOut("The oligos parameter .... The default is "".\n");
@@ -156,6 +166,9 @@ void TrimSeqsCommand::help(){
                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 tdiffs parameter is used to specify the total number of differences allowed in the sequence. The default is 0.\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");
@@ -352,6 +365,7 @@ int TrimSeqsCommand::driverCreateTrim(string filename, string qFileName, string
                        if (origSeq != "") {
                                int group;
                                string trashCode = "";
+                               int currentSeqsDiffs = 0;
                                
                                if(qFileName != ""){
                                        if(qThreshold != 0)             {       success = stripQualThreshold(currSeq, qFile);   }
@@ -365,13 +379,17 @@ int TrimSeqsCommand::driverCreateTrim(string filename, string qFileName, string
                                if(barcodes.size() != 0){
                                        success = stripBarcode(currSeq, group);
                                        if(!success){   trashCode += 'b';       }
+                                       else{ currentSeqsDiffs += currentSeqsTdiffs;  }
                                }
                        
                                if(numFPrimers != 0){
                                        success = stripForward(currSeq);
                                        if(!success){   trashCode += 'f';       }
+                                       else{ currentSeqsDiffs += currentSeqsTdiffs;  }
                                }
-                                       
+                               
+                               if (currentSeqsDiffs > tdiffs) { trashCode += 't';   }
+
                                if(numRPrimers != 0){
                                        success = stripReverse(currSeq);
                                        if(!success){   trashCode += 'r';       }
@@ -579,9 +597,7 @@ void TrimSeqsCommand::getOligos(vector<string>& outFASTAVec){ //vector<ofstream*
                m->errorOut(e, "TrimSeqsCommand", "getOligos");
                exit(1);
        }
-
 }
-
 //***************************************************************************************************************
 
 bool TrimSeqsCommand::stripBarcode(Sequence& seq, int& group){
@@ -589,6 +605,7 @@ bool TrimSeqsCommand::stripBarcode(Sequence& seq, int& group){
                string rawSequence = seq.getUnaligned();
                bool success = 0;       //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
@@ -603,6 +620,48 @@ bool TrimSeqsCommand::stripBarcode(Sequence& seq, int& group){
                                break;
                        }
                }
+               
+               //if you found the barcode or if you don't want to allow for diffs
+               if ((bdiffs == 0) || (success == 1)) { return success;  }
+               
+               else { //try aligning and see if you can find it
+                       
+                       Alignment* alignment;
+                       if (barcodes.size() > 0) { //assumes barcodes are all the same length
+                               map<string,int>::iterator it=barcodes.begin();
+                               string temp = it->first;
+                               
+                               alignment = new NeedlemanOverlap(-2.0, 1.0, -1.0, (temp.length()+bdiffs+1));  
+                       }else{ alignment = NULL; } 
+                       
+
+                       //can you find the barcode
+                       for(map<string,int>::iterator it=barcodes.begin();it!=barcodes.end();it++){
+                               string oligo = it->first;
+                               int length = oligo.length();
+                               
+                               if(rawSequence.length() < oligo.length()){      //let's just assume that the barcodes are the same length
+                                       success = 0;
+                                       break;
+                               }
+                               
+                               //use needleman to align first barcode.length()+numdiffs of sequence to each barcode
+                               alignment->align(oligo, rawSequence.substr(0,length+bdiffs));
+                               oligo = alignment->getSeqAAln();
+                               string temp = alignment->getSeqBAln();
+               //cout << "barcode = " << oligo << " raw = " << rawSequence.substr(0,oligo.length()) << " raw aligned = " << temp << endl;                      
+                               
+                               int newStart=0;
+                               if(compareDNASeq(oligo, temp, length, newStart, bdiffs)){
+                                       group = it->second;
+                                       seq.setUnaligned(rawSequence.substr(newStart));
+                                       success = 1;
+                                       break;
+                               }
+                       }
+                       
+                       if (alignment != NULL) {  delete alignment;  }
+               }
                return success;
                
        }
@@ -635,6 +694,43 @@ bool TrimSeqsCommand::stripForward(Sequence& seq){
                        }
                }
                
+               //if you found the primer or if you don't want to allow for diffs
+               if ((pdiffs == 0) || (success == 1)) { return success;  }
+               
+               else { //try aligning and see if you can find it
+                       
+                       Alignment* alignment;
+                       if (numFPrimers > 0) {  alignment = new NeedlemanOverlap(-2.0, 1.0, -1.0, (forPrimer[0].length()+pdiffs+1));  } 
+                       else{ alignment = NULL; } 
+                       //can you find the primer
+                       for(int i=0;i<numFPrimers;i++){
+                               string oligo = forPrimer[i];
+                               int length = oligo.length();
+                       
+                               if(rawSequence.length() < oligo.length()){      
+                                       success = 0;
+                                       break;
+                               }
+                       
+                               //resize if neccessary
+                               if ((length+pdiffs+1) > alignment->getnRows()) { alignment->resize(length+pdiffs+1);    }
+                               
+                               //use needleman to align first primer.length()+numdiffs of sequence to each primer
+                               alignment->align(oligo, rawSequence.substr(0,length+pdiffs));
+                               oligo = alignment->getSeqAAln();
+                               string temp = alignment->getSeqBAln();
+                       
+                               int newStart = 0;
+                               if(compareDNASeq(oligo, temp, length, newStart, pdiffs)){
+                                       seq.setUnaligned(rawSequence.substr(newStart));
+                                       success = 1;
+                                       break;
+                               }
+                       }
+                       
+                       if (alignment != NULL) {  delete alignment;  }
+               }
+               
                return success;
                
        }
@@ -744,7 +840,7 @@ bool TrimSeqsCommand::compareDNASeq(string oligo, string seq){
                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')    {       success = 0;    }
+                               if(oligo[i] == 'A' || oligo[i] == 'T' || oligo[i] == 'G' || oligo[i] == 'C')    {       success = 0;    }
                                else if((oligo[i] == 'N' || oligo[i] == 'I') && (seq[i] == 'N'))                                {       success = 0;    }
                                else if(oligo[i] == 'R' && (seq[i] != 'A' && seq[i] != 'G'))                                    {       success = 0;    }
                                else if(oligo[i] == 'Y' && (seq[i] != 'C' && seq[i] != 'T'))                                    {       success = 0;    }
@@ -757,7 +853,7 @@ bool TrimSeqsCommand::compareDNASeq(string oligo, string seq){
                                else if(oligo[i] == 'H' && (seq[i] != 'A' && seq[i] != 'T' && seq[i] != 'C'))   {       success = 0;    }
                                else if(oligo[i] == 'V' && (seq[i] != 'A' && seq[i] != 'C' && seq[i] != 'G'))   {       success = 0;    }                       
                                
-                               if(success == 0)        {       break;  }
+                               if(success == 0)        {       break;   }
                        }
                        else{
                                success = 1;
@@ -772,16 +868,71 @@ bool TrimSeqsCommand::compareDNASeq(string oligo, string seq){
        }
 
 }
+//***************************************************************************************************************
 
+bool TrimSeqsCommand::compareDNASeq(string oligo, string seq, int numBases, int& end, int diffs){
+       try {
+               bool success = 1;
+               int length = oligo.length();
+               end = numBases;
+               int countBases = 0;
+               int countDiffs = 0;
+               
+               if (length != 0) {
+                       if ((oligo[0] == '-') || (oligo[0] == '.')) {  success = 0;  return success;  } //no gaps allowed at beginning
+               }
+               
+               for(int i=0;i<length;i++){
+                       
+                       if ((oligo[i] != '-') && (oligo[i] != '.'))  { countBases++; } 
+                                       
+                       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++;   }                       
+                               
+                               if(countDiffs > diffs)  {       success = 0; break;      }
+                       }
+                       else{
+                               success = 1;
+                       }
+                       
+                       if (countBases >= numBases) { end = countBases; break; } //stop checking after end of barcode or primer
+               }
+               
+               //if it's a success we want to check for total diffs in driver, so save it.
+               if (success == 1) {  currentSeqsTdiffs = countDiffs; }
+               
+               return success;
+       }
+       catch(exception& e) {
+               m->errorOut(e, "TrimSeqsCommand", "compareDNASeq");
+               exit(1);
+       }
+
+}
 //***************************************************************************************************************
 
 bool TrimSeqsCommand::stripQualThreshold(Sequence& seq, ifstream& qFile){
        try {
                string rawSequence = seq.getUnaligned();
-               int seqLength = rawSequence.length();
-               string name;
+               int seqLength;  // = rawSequence.length();
+               string name, temp, temp2;
                
-               qFile >> name;
+               qFile >> name >> temp;
+       
+               splitAtEquals(temp2, temp); //separates length=242, temp=length, temp2=242
+               convert(temp, seqLength); //converts string to int
+       
                if (name.length() != 0) {  if(name.substr(1) != seq.getName())  {       m->mothurOut("sequence name mismatch btwn fasta and qual file"); m->mothurOutEndLine(); }  } 
                while (!qFile.eof())    {       char c = qFile.get(); if (c == 10 || c == 13){  break;  }       }