int main() { int a = 60; int b = 13; int c = bitOr(a, b); printf("%d|%d should be %d\n", a, b, 61); printf("bitOr(%d, %d) -> %d\n", a, b, c); }
void whyConserved(char *database, char *chrom, char *homologyTrack) /* whyConserved - Try and analyse why a particular thing is conserved. */ { struct sqlConnection *conn; int chromSize; Bits *h**o = NULL; Bits *bits = NULL; Bits *once = NULL; hSetDb(database); conn = hAllocConn(); chromSize = hChromSize(chrom); h**o = bitAlloc(chromSize); bits = bitAlloc(chromSize); once = bitAlloc(chromSize); /* Get homology bitmap and set once mask to be the same. */ fbOrTableBits(h**o, homologyTrack, chrom, chromSize, conn); bitOr(once, h**o, chromSize); /* printHeader */ printf("%-21s %8s %8s %5s %6s %6s %5s %5s \n", "Track Specification", "track", "overlap", "track", "mus", "track", "new", "cum"); printf("%-21s %8s %8s %5s %6s %6s %5s %5s \n", "", "size", "size", "geno", "track", "mus", "mus", "mus"); printf("-----------------------------------------------------------------------------\n"); /* Whittle awway at homology... */ explainSome(h**o, once, bits, chrom, chromSize, conn, NULL, homologyTrack); explainSome(h**o, once, bits, chrom, chromSize, conn, "simpleRepeat", NULL); explainSome(h**o, once, bits, chrom, chromSize, conn, "rmsk", NULL); explainSome(h**o, once, bits, chrom, chromSize, conn, "sanger22:CDS:10", NULL); explainSome(h**o, once, bits, chrom, chromSize, conn, "refGene:CDS:10", NULL); explainSome(h**o, once, bits, chrom, chromSize, conn, "sanger22:exon:10", NULL); explainSome(h**o, once, bits, chrom, chromSize, conn, "refGene:exon:10", NULL); explainSome(h**o, once, bits, chrom, chromSize, conn, "ensGene:exon:10", NULL); explainSome(h**o, once, bits, chrom, chromSize, conn, "rnaGene", NULL); explainSome(h**o, once, bits, chrom, chromSize, conn, "mrna:exon:10", NULL); explainSome(h**o, once, bits, chrom, chromSize, conn, "intronEst:exon:10", NULL); explainSome(h**o, once, bits, chrom, chromSize, conn, "xenoMrna:exon:10", NULL); explainSome(h**o, once, bits, chrom, chromSize, conn, "xenoEst:exon:10", NULL); explainSome(h**o, once, bits, chrom, chromSize, conn, "genscan:exon:10", NULL); explainSome(h**o, once, bits, chrom, chromSize, conn, "genscanSubopt", NULL); explainSome(h**o, once, bits, chrom, chromSize, conn, "psu:exon:10", NULL); explainSome(h**o, once, bits, chrom, chromSize, conn, "sanger22:upstream:200", NULL); explainSome(h**o, once, bits, chrom, chromSize, conn, "refGene:upstream:200", NULL); explainSome(h**o, once, bits, chrom, chromSize, conn, "mrna:upstream:200", NULL); explainSome(h**o, once, bits, chrom, chromSize, conn, "est", NULL); hFreeConn(&conn); }
void trackOverlap(char *database, char *chrom, char *homologyTrack, char *specFile) /* trackOverlap - Correlate a track with a series of tracks specified in specFile. */ { struct lineFile *lf = NULL; char *line = NULL; struct sqlConnection *conn; int chromSize; Bits *h**o = NULL; Bits *bits = NULL; Bits *once = NULL; lf = lineFileOpen(specFile, TRUE); conn = hAllocConn(database); chromSize = hChromSize(database, chrom); h**o = bitAlloc(chromSize); bits = bitAlloc(chromSize); once = bitAlloc(chromSize); /* Get homology bitmap and set once mask to be the same. */ fbOrTableBits(database, h**o, homologyTrack, chrom, chromSize, conn); bitOr(once, h**o, chromSize); /* printHeader */ printf("%-21s %8s %8s %5s %6s %6s %5s %5s \n", "Track Specification", "track", "overlap", "track", "cov", "track", "new", "cum"); printf("%-21s %8s %8s %5s %6s %6s %5s %5s \n", "", "size", "size", "geno", "track", "cov", "cov", "cov"); printf("-----------------------------------------------------------------------------\n"); /* Whittle awway at homology... */ explainSome(database, h**o, once, bits, chrom, chromSize, conn, NULL, homologyTrack); while(lineFileNextReal(lf, &line)) { explainSome(database, h**o, once, bits, chrom, chromSize, conn, line, NULL); } lineFileClose(&lf); hFreeConn(&conn); }
void chromFeatureBits(struct sqlConnection *conn,char *database, char *chrom, int tableCount, char *tables[], FILE *bedFile, FILE *faFile, FILE *binFile, struct bed *bedRegionList, FILE *bedOutFile, int chromSize, int *retChromBits, int *retFirstTableBits, int *retSecondTableBits) /* featureBits - Correlate tables via bitmap projections and booleans * on one chromosome. */ { int i; Bits *acc = NULL; Bits *bits = NULL; char *table; acc = bitAlloc(chromSize); bits = bitAlloc(chromSize); for (i=0; i<tableCount; ++i) { boolean not = FALSE; table = tables[i]; if (table[0] == '!') { not = TRUE; ++table; } if (i == 0) { orTable(database, acc, table, chrom, chromSize, conn); if (not) bitNot(acc, chromSize); if (retFirstTableBits != NULL) *retFirstTableBits = bitCountRange(acc, 0, chromSize); } else { bitClear(bits, chromSize); orTable(database, bits, table, chrom, chromSize, conn); if (not) bitNot(bits, chromSize); if (i == 1 && retSecondTableBits != NULL) *retSecondTableBits = bitCountRange(bits, 0, chromSize); /* feature/bug - the above does not respect minSize */ if (orLogic) bitOr(acc, bits, chromSize); else bitAnd(acc, bits, chromSize); } } if (notResults) bitNot(acc, chromSize); *retChromBits = bitCountRange(acc, 0, chromSize); if (bedFile != NULL || faFile != NULL) { minSize = optionInt("minSize", minSize); bitsToBed(database, acc, chrom, chromSize, bedFile, faFile, minSize); } if (binFile != NULL) { binSize = optionInt("binSize", binSize); binOverlap = optionInt("binOverlap", binOverlap); bitsToBins(acc, chrom, chromSize, binFile, binSize, binOverlap); } if (bedOutFile != NULL) bitsToRegions(acc, chrom, chromSize, bedRegionList, bedOutFile); bitFree(&acc); bitFree(&bits); }
struct bed *getRegionAsMergedBed( char *db, char *table, /* Database and table. */ struct region *region, /* Region to get data for. */ char *filter, /* Filter to add to SQL where clause if any. */ struct hash *idHash, /* Restrict to id's in this hash if non-NULL. */ struct lm *lm, /* Where to allocate memory. */ int *retFieldCount) /* Number of fields. */ /* Return a bed list of all items in the given range in subtrack-merged table. * Cleanup result via lmCleanup(&lm) rather than bedFreeList. */ { if (! anySubtrackMerge(db, table)) return getRegionAsBed(db, table, region, filter, idHash, lm, retFieldCount); else { struct hTableInfo *hti = getHtiOnDb(database, table); int chromSize = hChromSize(database, region->chrom); Bits *bits1 = NULL; Bits *bits2 = NULL; struct bed *bedMerged = NULL; struct trackDb *subtrack = NULL; char *primaryType = findTypeForTable(database,curTrack,table, ctLookupName); char *op = cartString(cart, hgtaSubtrackMergeOp); boolean isBpWise = (sameString(op, "and") || sameString(op, "or")); double moreThresh = cartDouble(cart, hgtaSubtrackMergeMoreThreshold); double lessThresh = cartDouble(cart, hgtaSubtrackMergeLessThreshold); boolean firstTime = TRUE; if (sameString(op, "cat")) { struct bed *bedList = getRegionAsBed(db, table, region, filter, idHash, lm, retFieldCount); struct slRef *tdbRefList = trackDbListGetRefsToDescendantLeaves(curTrack->subtracks); struct slRef *tdbRef; for (tdbRef = tdbRefList; tdbRef != NULL; tdbRef = tdbRef->next) { subtrack = tdbRef->val; if (! sameString(curTable, subtrack->table) && isSubtrackMerged(subtrack->table) && sameString(subtrack->type, primaryType)) { struct bed *bedList2 = getRegionAsBed(db, subtrack->table, region, NULL, idHash, lm, retFieldCount); bedList = slCat(bedList, bedList2); } } slFreeList(&tdbRefList); return bedList; } bits1 = bitAlloc(chromSize+8); bits2 = bitAlloc(chromSize+8); /* If doing a base-pair-wise operation, then start with the primary * subtrack's ranges in bits1, and AND/OR all the selected subtracks' * ranges into bits1. If doing a non-bp-wise intersection, then * start with all bits clear in bits1, and then OR selected subtracks' * ranges into bits1. */ if (isBpWise) { struct lm *lm2 = lmInit(64*1024); struct bed *bedList1 = getRegionAsBed(db, table, region, filter, idHash, lm2, retFieldCount); bedOrBits(bits1, chromSize, bedList1, hti->hasBlocks, 0); lmCleanup(&lm2); } struct slRef *tdbRefList = trackDbListGetRefsToDescendantLeaves(curTrack->subtracks); struct slRef *tdbRef; for (tdbRef = tdbRefList; tdbRef != NULL; tdbRef = tdbRef->next) { subtrack = tdbRef->val; if (! sameString(curTable, subtrack->table) && isSubtrackMerged(subtrack->table) && sameString(subtrack->type, primaryType)) { struct hTableInfo *hti2 = getHtiOnDb(database, subtrack->table); struct lm *lm2 = lmInit(64*1024); struct bed *bedList2 = getRegionAsBed(db, subtrack->table, region, NULL, idHash, lm2, NULL); if (firstTime) firstTime = FALSE; else bitClear(bits2, chromSize); bedOrBits(bits2, chromSize, bedList2, hti2->hasBlocks, 0); if (sameString(op, "and")) bitAnd(bits1, bits2, chromSize); else bitOr(bits1, bits2, chromSize); lmCleanup(&lm2); } } slFreeList(&tdbRefList); if (isBpWise) { bedMerged = bitsToBed4List(bits1, chromSize, region->chrom, 1, region->start, region->end, lm); if (retFieldCount != NULL) *retFieldCount = 4; } else { struct bed *bedList1 = getRegionAsBed(db, table, region, filter, idHash, lm, retFieldCount); bedMerged = filterBedByOverlap(bedList1, hti->hasBlocks, op, moreThresh, lessThresh, bits1, chromSize); } bitFree(&bits1); bitFree(&bits2); return bedMerged; } }
static struct bed *intersectOnRegion( struct sqlConnection *conn, /* Open connection to database. */ struct region *region, /* Region to work inside */ char *table1, /* Table input list is from. */ struct bed *bedList1, /* List before intersection, should be * all within region. */ struct lm *lm, /* Local memory pool. */ int *retFieldCount) /* Field count. */ /* Intersect bed list, consulting CGI vars to figure out * with what table and how. Return intersected result, * which is independent from input. This potentially will * chew up bedList1. */ { /* Grab parameters for intersection from cart. */ double moreThresh = cartCgiUsualDouble(cart, hgtaMoreThreshold, 0); double lessThresh = cartCgiUsualDouble(cart, hgtaLessThreshold, 100); boolean invTable = cartCgiUsualBoolean(cart, hgtaInvertTable, FALSE); char *op = intersectOp(); /* --- TODO MIKE - replace bedList2, bits2 with baseMask stuff. */ /* Load up intersecting bedList2 (to intersect with) */ int chromSize = hChromSize(database, region->chrom); boolean isBpWise = (sameString("and", op) || sameString("or", op)); Bits *bits2 = bitsForIntersectingTable(conn, region, chromSize, isBpWise); /* Set up some other local vars. */ struct hTableInfo *hti1 = getHti(database, table1, conn); struct bed *intersectedBedList = NULL; /* Produce intersectedBedList. */ if (isBpWise) { /* --- TODO MIKE - replace, bits1 with baseMask stuff. */ /* Base-pair-wise operation: get bitmap for primary table too */ Bits *bits1 = bitAlloc(chromSize+8); boolean hasBlocks = hti1->hasBlocks; if (retFieldCount != NULL && (*retFieldCount < 12)) hasBlocks = FALSE; bedOrBits(bits1, chromSize, bedList1, hasBlocks, 0); /* invert inputs if necessary */ if (invTable) bitNot(bits1, chromSize); /* do the intersection/union */ if (sameString("and", op)) bitAnd(bits1, bits2, chromSize); else bitOr(bits1, bits2, chromSize); /* clip to region if necessary: */ if (region->start > 0) bitClearRange(bits1, 0, region->start); if (region->end < chromSize) bitClearRange(bits1, region->end, (chromSize - region->end)); /* translate back to bed */ intersectedBedList = bitsToBed4List(bits1, chromSize, region->chrom, 1, region->start, region->end, lm); if (retFieldCount != NULL) *retFieldCount = 4; bitFree(&bits1); } else intersectedBedList = filterBedByOverlap(bedList1, hti1->hasBlocks, op, moreThresh, lessThresh, bits2, chromSize); bitFree(&bits2); return intersectedBedList; }