src/jkOwnLib/gfPcrLib.c 1.13

1.13 2010/05/15 06:28:15 galt
Dealing with primers that match but dangle off the ends of the chrom
Index: src/jkOwnLib/gfPcrLib.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/jkOwnLib/gfPcrLib.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -b -B -U 4 -r1.12 -r1.13
--- src/jkOwnLib/gfPcrLib.c	7 Dec 2009 18:24:00 -0000	1.12
+++ src/jkOwnLib/gfPcrLib.c	15 May 2010 06:28:15 -0000	1.13
@@ -343,9 +343,8 @@
 char *endDna = fDna + seq->size;
 char *fpPerfect = fPrimer + fPrimerSize - minPerfect;
 char *rpPerfect = rPrimer;
 char *fpMatch, *rpMatch;
-int sizeLeft;
 int goodSize = minGood - minPerfect;
 struct gfPcrOutput  *out;
 int matchSize;
 
@@ -354,32 +353,61 @@
     {
     fpMatch = memMatch(fpPerfect, minPerfect, fDna, endDna - fDna);
     if (fpMatch == NULL)
         break;
-    sizeLeft = endDna - fpMatch;
     rDna = fpMatch;
     for (;;)
         {
-	int fPos, rPos, fGoodPos, rGoodPos;
+	int fPos, rPos, fGoodPos, rGoodPos, fTrim, rTrim;
 	rpMatch = memMatch(rpPerfect, minPerfect, rDna, endDna - rDna);
 	if (rpMatch == NULL)
 	    break;
 	fPos = fpMatch - (fPrimerSize - minPerfect) - seq->dna;
 	rPos = rpMatch + rPrimerSize - seq->dna;
+
+	/* deal with primers dangling off either end of the target sequence */
+	if (fPos < 0)
+	    {
+	    fTrim = 0 - fPos;
+	    fPos = 0;
+	    }
+	else
+	    fTrim = 0;
+
+	if (rPos > seq->size)
+	    {
+	    rTrim = rPos - seq->size;
+	    rPos = seq->size;
+	    }
+	else
+	    rTrim = 0;
+
 	fGoodPos = fpMatch - goodSize - seq->dna;
 	rGoodPos = rpMatch + minPerfect - seq->dna;
+
+	fGoodPos = max(fGoodPos, 0);
+	rGoodPos = min(rGoodPos, seq->size);
+
 	matchSize = rPos - fPos;
+
+	int fGoodSize = fGoodPos - fPos;
+	int rGoodSize = rPos - rGoodPos;
+	fGoodSize = min(fGoodSize, goodSize);
+	rGoodSize = min(rGoodSize, goodSize);
+
         if (rPos >= 0 && fPos >= 0 && fPos < seq->size && matchSize <= maxSize)
 	    {
 	    /* If matches well enough create output record. */
-	    if (goodMatch(seq->dna + fGoodPos, fpPerfect - goodSize, goodSize) &&
-	        goodMatch(seq->dna + rGoodPos, rpPerfect + minPerfect, goodSize))
+	    if (goodMatch(seq->dna + fGoodPos, fpPerfect - fGoodSize, fGoodSize) &&
+	        goodMatch(seq->dna + rGoodPos, rpPerfect + minPerfect, rGoodSize))
 		{
+
+		/* Truncate the copy of the primers going into the out-> record using rTrim and fTrim if needed. */
 		AllocVar(out);
 		out->name  = cloneString(pcrName);
-		out->fPrimer = cloneString(fPrimer);
-		out->rPrimer = cloneString(rPrimer);
-		reverseComplement(out->rPrimer, rPrimerSize);
+		out->fPrimer = cloneString(fPrimer + fTrim);
+		out->rPrimer = cloneStringZ(rPrimer, rPrimerSize - rTrim);
+		reverseComplement(out->rPrimer, rPrimerSize - rTrim);
 		out->seqName = cloneString(seqName);
 		out->seqSize = seqSize;
 		out->fPos = fPos + seqOffset;
 		out->rPos = rPos + seqOffset;