static struct bed *filterBedByOverlap(struct bed *bedListIn, boolean hasBlocks, char *op, double moreThresh, double lessThresh, Bits *bits, int chromSize) /* Return list of beds that pass overlap filter. */ { struct bed *intersectedBedList = NULL; struct bed *bed = NULL, *nextBed = NULL; /* Loop through primary bed list seeing if each one intersects * enough to keep. */ for (bed = bedListIn; bed != NULL; bed = nextBed) { int numBasesOverlap = countBasesOverlap(bed, bits, hasBlocks, chromSize); int length = 0; double pctBasesOverlap; nextBed = bed->next; if (hasBlocks) { int i; for (i=0; i < bed->blockCount; i++) length += bed->blockSizes[i]; } else length = (bed->chromEnd - bed->chromStart); if (length == 0) length = 2; pctBasesOverlap = ((numBasesOverlap * 100.0) / length); if (intersectOverlapFilter(op, moreThresh, lessThresh, pctBasesOverlap)) { slAddHead(&intersectedBedList, bed); } } slReverse(&intersectedBedList); return intersectedBedList; }
struct bbiInterval *intersectedFilteredBbiIntervalsOnRegion(struct sqlConnection *conn, struct bbiFile *bwf, struct region *region, enum wigCompare filterCmp, double filterLl, double filterUl, struct lm *lm) /* Get list of bbiIntervals (more-or-less bedGraph things from bigWig) out of bigWig file * and if necessary apply filter and intersection. Return list which is allocated in lm. */ { char *chrom = region->chrom; int chromSize = hChromSize(database, chrom); struct bbiInterval *iv, *ivList = bigWigIntervalQuery(bwf, chrom, region->start, region->end, lm); /* Run filter if necessary */ if (filterCmp != wigNoOp_e) { struct bbiInterval *next, *newList = NULL; for (iv = ivList; iv != NULL; iv = next) { next = iv->next; if (wigCompareValFilter(iv->val, filterCmp, filterLl, filterUl)) { slAddHead(&newList, iv); } } slReverse(&newList); ivList = newList; } /* Run intersection if necessary */ if (anyIntersection()) { boolean isBpWise = intersectionIsBpWise(); Bits *bits2 = bitsForIntersectingTable(conn, region, chromSize, isBpWise); struct bbiInterval *next, *newList = NULL; double moreThresh = cartCgiUsualDouble(cart, hgtaMoreThreshold, 0)*0.01; double lessThresh = cartCgiUsualDouble(cart, hgtaLessThreshold, 100)*0.01; char *op = cartString(cart, hgtaIntersectOp); for (iv = ivList; iv != NULL; iv = next) { next = iv->next; int start = iv->start; int size = iv->end - start; int overlap = bitCountRange(bits2, start, size); if (isBpWise) { if (overlap == size) { slAddHead(&newList, iv); } else if (overlap > 0) { /* Here we have to break things up. */ double val = iv->val; struct bbiInterval *partIv = iv; // Reuse memory for first interval int s = iv->start, end = iv->end; for (;;) { s = bitFindSet(bits2, s, end); if (s >= end) break; int bitsSet = bitFindClear(bits2, s, end) - s; if (partIv == NULL) lmAllocVar(lm, partIv); partIv->start = s; partIv->end = s + bitsSet; partIv->val = val; slAddHead(&newList, partIv); partIv = NULL; s += bitsSet; if (s >= end) break; } } } else { double coverage = (double)overlap/size; if (intersectOverlapFilter(op, moreThresh, lessThresh, coverage)) { slAddHead(&newList, iv); } } } slReverse(&newList); ivList = newList; bitFree(&bits2); } return ivList; }