Example #1
0
static void homozygote (unsigned r, double probl, double statl, double u, double x2, COUNTTYPE * R)
{
    // If the process takes longer than `timeLimit` seconds, set
    // `tableCount` negative to signify that the job is aborted
    if(tableCount < 0) return;
    if(time(NULL) - start >= timeLimit) tableCount = -tableCount;
    
	COUNTTYPE * res, *resn;
	int lower, upper, exindix;
	unsigned i, arr;
    double arrln2;
	COUNTTYPE * Rnew = R + nAlleles;
	memcpy(Rnew, R, Rbytes);
	//Find upper and lower limits for arr.
    res = R-1;  // So res is a 1-based version of R
    resn = Rnew-1; // resn is 1 based for Rnew
	lower = res[r];
	for (i = 1; i <= r-1; i++) lower -= res[i];
    lower = lower < 2 ? 0 : lower/2;
    upper = res[r]/2;
    //For each possible value of arr, examine the heterozygote at r, r-1
    for(arr = lower; arr <= upper; arr++) {
        resn[r] = res[r] - 2*arr;
        arrln2 = arr * M_LN2;
        exindix = (r-1)*nAlleles + r - 1;  // index of homozygote
        heterozygote(r,
                     r-1,
                     probl + lnFact[arr] + arrln2,
                     statl + xlnx[arr] + arrln2,
                     u + (double)arr/mi[r],
                     x2 + R_pow_di(arr - exa[exindix],2)/exa[exindix],
                     Rnew);
    }
}
Example #2
0
void homozygote (unsigned r, COUNTTYPE * R)
{
    // If the process takes longer than `timeLimit` seconds, set
    // `tableCount` negative to signify that the job is aborted
    if(tableCount < 0) return;
    if(time(NULL) - start >= timeLimit) tableCount = -tableCount;
    if (tableCount > countLimit) tableCount = -tableCount;
	COUNTTYPE * res, *resn;
	int lower, upper;
	unsigned i, arr;
	COUNTTYPE * Rnew = R + nAlleles;
	memcpy(Rnew, R, Rbytes);
	//Find upper and lower limits for arr.
    res = R-1;  // So res is a 1-based version of R
    resn = Rnew-1; // resn is 1 based for Rnew
	lower = res[r];
	for (i = 1; i <= r-1; i++) lower -= res[i];
        lower = lower < 2 ? 0 : lower/2;
        upper = res[r]/2;
        //For each possible value of arr, examine the heterozygote at r, r-1
        for(arr = lower; arr <= upper; arr++) {
            resn[r] = res[r] - 2*arr;
            heterozygote(r, r-1, Rnew);
        }
}
Example #3
0
static void heterozygote (unsigned r, unsigned c, double probl, double statl, double u, double x2, COUNTTYPE * R)
{
    if(tableCount < 0) return;
    COUNTTYPE *res, *resn;
	int lower, upper, exindex;
	unsigned i, arc, ar1, ar2, a31, a32, a11, a21, a22;
	unsigned res1, res2, resTemp, dT;
	int hdex;
	double probl3, statl3, x23, problT, statlT, uT, x2T, prob, x=0;
    COUNTTYPE * Rnew = R + nAlleles;
    
    res = R-1; // to make res a 1-based version of R
    resn = Rnew-1; // so resn is 1-based for Rnew
	lower = res[r];
	for (i = 1; i < c; i++) lower -= res[i];
    lower = fmax(0, lower);
    upper = fmin(res[r], res[c]);
    if(c > 2) for (arc = lower; arc <= upper; arc++) {
        memcpy(Rnew, R, Rbytes); // Put a fresh set of residuals from R into Rnew
        
        // decrement residuals for the current value of arc.
        resn[r] -= arc;
        resn[c] -= arc;
        exindex = (r-1)*nAlleles + c - 1;
        heterozygote(r, c-1,
                     probl+lnFact[arc],
                     statl + xlnx[arc],
                     u,
                     x2 + R_pow_di(arc - exa[exindex], 2)/exa[exindex],
                     Rnew);
    } // for arc
    if(c==2){
		if(r > 3) for (ar2= lower; ar2 <= upper; ar2++) {
			 memcpy(Rnew, R, Rbytes); // Put a fresh set of residuals from R into Rnew
			// decrement residuals for the current value of arc.
			resn[r] -= ar2;
			resn[c] -= ar2;
			// The value of ar1 is now fixed, so no need for any more calls to heterozygote in this row
			ar1 = fmin(resn[r], resn[1]);
			resn[1] -= ar1;
			resn[r] -= ar1;
            exindex = (r-1)*nAlleles;
            homozygote(r-1,
                       probl + lnFact[ar2] + lnFact[ar1],
                       statl + xlnx[ar2] + xlnx[ar1],
                       u,
                        x2 + R_pow_di(ar1 - exa[exindex], 2)/exa[exindex]+ R_pow_di(ar2 - exa[exindex+1], 2)/exa[exindex+1] ,
                       Rnew);
        } // if r > 3
		if(r==3) // and c = 2, then we can handle a series of two-allele cases with no deeper recursion
		{
			double * uT1, *uT2, *x11, *x22;
			for(a32 = lower; a32 <= upper; a32++) {
				a31 = fmin(res[1], res[3]-a32); //Value of a31 is now fixed for each a32
				probl3 = probl + lnFact[a32] + lnFact[a31];
				statl3 = statl + xlnx[a32] + xlnx[a31];
                exindex = 2*nAlleles;
                x23 = x2 + R_pow_di(a31 - exa[exindex], 2)/exa[exindex]+ R_pow_di(a32 - exa[exindex+1], 2)/exa[exindex+1] ;
				// get residual allele counts for two-allele case
				res1 = res[1] - a31;
				res2 = res[2] - a32;
                // make pointers to lookups in case they need to be swapped
				uT1 = uTerm1;
				uT2 = uTerm2;
                x11 = x211;
                x22 = x222;
                
				if(res1 > res2) {            // make sure res1 <= res2. If they need swapping, then swap lookups too
					resTemp = res2;
					res2 = res1;
					res1 = resTemp;
					uT1 = uTerm2;
					uT2 = uTerm1;
                    x11 = x222;
                    x22 = x211;
				}
				
				// Now process two-allele case with allele counts res1 and res2
                tableCount += res1/2 + 1;
                    for(a11 = 0; a11 <= res1/2; a11++) {
					a21 = res1-a11*2; // integer arithmetic rounds down
					a22 = (res2-a21)/2;
					problT = probl3 + lnFact[a11] + lnFact[a21] + lnFact[a22];
					statlT = statl3 + xlnx[a11] + xlnx[a21] + xlnx[a22];
					dT = a11 + a22;
					
					// Here come the actual probability and LLR and X2 and U values
					problT = constProbTerm - problT -dT * M_LN2;
					prob = exp(problT);
					statlT = constLLRterm - statlT - dT * M_LN2;
					uT = 2 * ntotal * (u + uT1[a11] + uT2[a22]) - ntotal;
                    x2T = x23 + x221[a21] + x11[a11] + x22[a22];
                        
//                    umean += prob * uT;
//                    uvariance += prob * uT * uT;
                    
                    //Now process the new values of prob and stat
                    probSum += prob;
                    if(statlT <= maxLLR) pLLR += prob;
                    if(problT <= maxlPr) pPr += prob;
                    if (minmaxU < 0) {
                        if(uT <= minmaxU) pU += prob;
                    } else {
                        if(uT >= minmaxU) pU += prob;
                    }
                    if(x2T >= minX2) pX2 += prob;
                    
                    // Update histogram if needed
                    if (HN) {
                        switch (statID) {
                            case 0:
                                x = statlT;
                                break;
                            case 1:
                                x = problT;
                                break;
                            case 2:
                                x = uT;
                                break;
                            case 3:
                                x = x2T;
                            default:
                                break;
                        }
                        hdex = statSpan * (x - leftStat);
                        if ((hdex >= 0) && (hdex < HN)) {
                            hProb[hdex] += prob;
                        }
                    }
                } // for a11
			} // for a32
		} // if r == 3
	} // if c == 2
}
Example #4
0
void heterozygote (unsigned r, unsigned c, COUNTTYPE * R)
{
    if(tableCount < 0) return;  // aborted because of time limit
	COUNTTYPE *res, *resn;
	int lower, upper;
	unsigned ntables;
	unsigned i, arc, ar1, ar2, a32, a31;
	COUNTTYPE * Rnew = R + nAlleles;
	double countsSoFar;
    unsigned long long hash;
    //	NSNumber * n;
	
    res = R-1; // to make res a 1-based version of R
    resn = Rnew-1; // so resn is 1-based for Rnew
	lower = res[r];
	for (i = 1; i < c; i++) lower -= res[i];
    lower = fmax(0, lower);
    upper = fmin(res[r], res[c]);
    if(c > 2) for (arc = lower; arc <= upper; arc++) {
        memcpy(Rnew, R, Rbytes); // Put a fresh set of residuals from R into Rnew
        
        // decrement residuals for the current value of arc.
        resn[r] -= arc;
        resn[c] -= arc;
        heterozygote(r, c-1, Rnew);
    }
	if(c==2){
		if(r > 3) for (ar2= lower; ar2 <= upper; ar2++) {
            memcpy(Rnew, R, Rbytes); // Put a fresh set of residuals from R into Rnew
    // decrement residuals for the current value of arc.
			resn[r] -= ar2;
			resn[c] -= ar2;
			// The value of ar1 is now fixed, so no need for any more calls to heterozygote in this row
			ar1 = fmin(resn[r], resn[1]);
			resn[1] -= ar1;
			resn[r] -= ar1;
            // Before calling homozygote, see if we have visited this node before by comparing its hash tag.
            hash = makeHash(r-1, Rnew);
            i = 0;
            // Search list of old nodes
            while (hash != nodez[i].hash && i < nextNode) i++;
            if(i < nextNode) {
				// old node was found, no need to go any further.
				tableCount += nodez[i].count;
			} else {
				// new node
				countsSoFar =  tableCount;
                homozygote(r-1, Rnew);
                if (nextNode < MAXNODE) {
                    // Make a new node
                    nodez[i].hash = hash;
                    nodez[i].count = tableCount - countsSoFar;
                    nextNode++;
                }
  			} // new node
        }
		if(r==3) // and c = 2, then we can handle a series of two-allele cases with no deeper recursion
		{
			for(a32 = lower; a32 <= upper; a32++) {
				a31 = fmin(res[1], res[3]-a32); //Value of a31 is now fixed for each a32
				ntables = (fmin(res[1] - a31, res[2]-a32))/2 + 1;
				tableCount += ntables;
			}
		} // if r == 3
	} // if c == 2
} // heterozygote