]> git.donarmstrong.com Git - mothur.git/blobdiff - trimseqscommand.cpp
1.10.0
[mothur.git] / trimseqscommand.cpp
index 537992584660b29696b602f09285aff6279aea73..ae9c436440950cca6cd33eb99e85c5b9b4db1062 100644 (file)
@@ -23,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","diffs", "processors", "outputdir","inputdir"};
+                                                                       "qthreshold", "qaverage", "allfiles", "qtrim","tdiffs", "pdiffs", "bdiffs", "processors", "outputdir","inputdir"};
                        
                        vector<string> myArray (AlignArray, AlignArray+(sizeof(AlignArray)/sizeof(string)));
                        
@@ -104,8 +104,17 @@ TrimSeqsCommand::TrimSeqsCommand(string option)  {
                        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 = "";         }
@@ -160,7 +169,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 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");
@@ -255,13 +266,13 @@ int TrimSeqsCommand::execute(){
                                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
@@ -320,7 +331,11 @@ int TrimSeqsCommand::driverCreateTrim(string filename, string qFileName, string
                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
                        }
                }
                
@@ -357,6 +372,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);   }
@@ -369,14 +385,19 @@ int TrimSeqsCommand::driverCreateTrim(string filename, string qFileName, string
                        
                                if(barcodes.size() != 0){
                                        success = stripBarcode(currSeq, group);
-                                       if(!success){   trashCode += 'b';       }
+//                                     cout << "here: " << success << endl;
+                                       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';       }
@@ -587,68 +608,106 @@ void TrimSeqsCommand::getOligos(vector<string>& outFASTAVec){ //vector<ofstream*
 }
 //***************************************************************************************************************
 
-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
+               bool 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;
+                               success = bdiffs + 1;
                                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) { //assumes barcodes are all the same length
+                       if (barcodes.size() > 0) {
                                map<string,int>::iterator it=barcodes.begin();
-                               string temp = it->first;
-                               
-                               alignment = new NeedlemanOverlap(-2.0, 1.0, -1.0, (temp.length()+diffs+1));  
+
+                               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;
-                               int length = oligo.length();
+//                             int length = oligo.length();
                                
-                               if(rawSequence.length() < oligo.length()){      //let's just assume that the barcodes are the same length
-                                       success = 0;
+                               if(rawSequence.length() < maxLength){   //let's just assume that the barcodes are the same length
+                                       success = bdiffs + 1;
                                        break;
                                }
                                
                                //use needleman to align first barcode.length()+numdiffs of sequence to each barcode
-                               alignment->align(oligo, rawSequence.substr(0,length+diffs));
+                               alignment->align(oligo, rawSequence.substr(0,oligo.length()+bdiffs));
                                oligo = alignment->getSeqAAln();
                                string temp = alignment->getSeqBAln();
-               cout << "barcode = " << oligo << " raw = " << rawSequence.substr(0,oligo.length()) << " raw aligned = " << temp << endl;                        
+               
+                               int alnLength = oligo.length();
+                               
+                               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;
-                               if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()), length, newStart)){
-               cout << "found match" << endl;
-                                       group = it->second;
-                                       seq.setUnaligned(rawSequence.substr(newStart));
-                                       success = 1;
-                                       break;
+                               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 =  bdiffs + 1;  }
+                       else if(minCount > 1)   {       success =  bdiffs + 1;  }
+                       else{
+                               group = minGroup;
+                               seq.setUnaligned("*" + rawSequence.substr(minPos));
+                               success = minDiff;
                        }
                        
                        if (alignment != NULL) {  delete alignment;  }
+                       
                }
                return success;
                
@@ -662,63 +721,103 @@ bool TrimSeqsCommand::stripBarcode(Sequence& seq, int& group){
 
 //***************************************************************************************************************
 
-bool TrimSeqsCommand::stripForward(Sequence& seq){
+int TrimSeqsCommand::stripForward(Sequence& seq){
        try {
                string rawSequence = seq.getUnaligned();
-               bool success = 0;       //guilty until proven innocent
+               bool 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
-                       
-                       
+//                     cout << endl;
+
+                       int maxLength = 0;
+
                        Alignment* alignment;
-                       if (numFPrimers > 0) { alignment = new NeedlemanOverlap(-2.0, 1.0, -1.0, (forPrimer[0].length()+diffs+1));  } //assumes primers are all the same length
-                       else{ alignment = NULL; } 
+                       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;
                        
-                       //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;
+                               if(rawSequence.length() < maxLength){   
+                                       success = pdiffs + 1;
                                        break;
                                }
                                
-                               //use needleman to align first primer.length()+numdiffs of sequence to each primer
-                               alignment->align(oligo, rawSequence.substr(0,length+diffs));
+                               //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();
                                
-                               int newStart = 0;
-                               if(compareDNASeq(oligo, rawSequence.substr(0,oligo.length()), length, newStart)){
-                                       seq.setUnaligned(rawSequence.substr(newStart));
-                                       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 =  pdiffs + 1;  }
+                       else if(minCount > 1)   {       success =  pdiffs + 1;  }
+                       else{
+                               seq.setUnaligned("*" + rawSequence.substr(minPos));
+                               success = minDiff;
                        }
                        
                        if (alignment != NULL) {  delete alignment;  }
+                       
                }
-               
                return success;
-               
+
        }
        catch(exception& e) {
                m->errorOut(e, "TrimSeqsCommand", "stripForward");
@@ -856,24 +955,14 @@ bool TrimSeqsCommand::compareDNASeq(string oligo, string seq){
 }
 //***************************************************************************************************************
 
-bool TrimSeqsCommand::compareDNASeq(string oligo, string seq, int numBases, int& end){
+int TrimSeqsCommand::countDiffs(string oligo, string seq){
        try {
-               bool success = 1;
+
                int length = oligo.length();
-               end = length;
-               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++; } 
-                       
-       //cout << oligo[i] << " " << seq[i] << " diffs = " << countDiffs << " countBases = " << countBases << endl;     
-               
+                                                               
                        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++;   }
@@ -888,19 +977,13 @@ bool TrimSeqsCommand::compareDNASeq(string oligo, string seq, int numBases, int&
                                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 = i; break; } //stop checking after end of barcode or primer
                }
-       
-               return success;
+               
+               return countDiffs;
        }
        catch(exception& e) {
-               m->errorOut(e, "TrimSeqsCommand", "compareDNASeq");
+               m->errorOut(e, "TrimSeqsCommand", "countDiffs");
                exit(1);
        }