static void addBwCorrelations(struct bbiChromInfo *chrom, struct genomeRangeTree *targetGrt, struct bigWigValsOnChrom *aVals, struct bigWigValsOnChrom *bVals, struct bbiFile *aBbi, struct bbiFile *bBbi, double aThreshold, double bThreshold, struct correlate *c, struct correlate *cInEnriched, struct correlate *cClipped) /* Find bits of a and b that overlap and also overlap with targetRanges. Do correlations there */ { struct rbTree *targetRanges = genomeRangeTreeFindRangeTree(targetGrt, chrom->name); if (bigWigValsOnChromFetchData(aVals, chrom->name, aBbi) && bigWigValsOnChromFetchData(bVals, chrom->name, bBbi) ) { double *a = aVals->valBuf, *b = bVals->valBuf; int i, end = chrom->size; for (i=0; i<end; ++i) { double aVal = a[i], bVal = b[i]; correlateNext(c, aVal, bVal); if (aVal > aThreshold) aVal = aThreshold; if (bVal > bThreshold) bVal = bThreshold; correlateNext(cClipped, aVal, bVal); } if (targetRanges != NULL) { struct range *range, *rangeList = rangeTreeList(targetRanges); for (range = rangeList; range != NULL; range = range->next) { int start = range->start, end = range->end; for (i=start; i<end; ++i) correlateNext(cInEnriched, a[i], b[i]); } } } }
double correlateArrays(double *x, double *y, int size) /* Return correlation of two arrays of doubles. */ { struct correlate *c = correlateNew(); double r; int i; for (i=0; i<size; ++i) correlateNext(c, x[i], y[i]); r = correlateResult(c); correlateFree(&c); return r; }
static void correlateChrom(struct chromGraphBin *a, struct chromGraphBin *b, struct correlate *c) /* Add a sample point to correlation for each data point in each graph. * The value for the other graph will be linearly interpolated. * In most cases the very first and very last data points won't be * included in the correlation since the interpolation there isn't * generally possible. Both chromGraphBins should be positioned at the * start of the same chromosome. */ { int aStart, bStart, aLastStart = 0, bLastStart = 0; double aLastVal = 0, bLastVal = 0; boolean gotA = FALSE, gotB = FALSE; chromGraphBinNextVal(a); chromGraphBinNextVal(b); for (;;) { aStart = a->chromStart; bStart = b->chromStart; if (aStart == bStart) { /* Correlate twice since matching both points. */ correlateNext(c, a->val, b->val); correlateNext(c, a->val, b->val); aLastStart = aStart; aLastVal = a->val; bLastStart = bStart; bLastVal = b->val; gotA = gotB = TRUE; if (!chromGraphBinNextVal(a)) break; if (!chromGraphBinNextVal(b)) break; } else if (aStart < bStart) { if (gotB) { double ratio = (aStart - bLastStart)/(bStart - bLastStart); double bInterVal = bLastVal + ratio * (b->val - bLastVal); correlateNext(c, a->val, bInterVal); } aLastStart = aStart; aLastVal = a->val; gotA = TRUE; if (!chromGraphBinNextVal(a)) break; } else { if (gotA) { double ratio = (bStart - aLastStart)/(aStart - aLastStart); double aInterVal = aLastVal + ratio * (a->val - aLastVal); correlateNext(c, aInterVal, b->val); } bLastStart = bStart; bLastVal = b->val; gotB = TRUE; if (!chromGraphBinNextVal(b)) break; } } }