95112ba802bfc44396278a4f583993fb1cd148ef galt Fri Aug 24 23:04:42 2012 -0700 more fixes to bbi for byteswapping diff --git src/lib/bbiRead.c src/lib/bbiRead.c index 4aa69ba..52a3cd6 100644 --- src/lib/bbiRead.c +++ src/lib/bbiRead.c @@ -145,88 +145,94 @@ /* Close down a big wig/big bed file. */ { struct bbiFile *bwf = *pBwf; if (bwf != NULL) { cirTreeFileDetach(&bwf->unzoomedCir); slFreeList(&bwf->levelList); slFreeList(&bwf->levelList); bptFileDetach(&bwf->chromBpt); udcFileClose(&bwf->udc); freeMem(bwf->fileName); freez(pBwf); } } +static void chromIdSizeHandleSwapped(boolean isSwapped, struct bbiChromIdSize *idSize) +/* Swap bytes in chromosome Id and Size as needed. */ +{ +if (isSwapped) + { + idSize->chromId = byteSwap32(idSize->chromId); + idSize->chromSize = byteSwap32(idSize->chromSize); + } +} + struct fileOffsetSize *bbiOverlappingBlocks(struct bbiFile *bbi, struct cirTreeFile *ctf, char *chrom, bits32 start, bits32 end, bits32 *retChromId) /* Fetch list of file blocks that contain items overlapping chromosome range. */ { struct bbiChromIdSize idSize; if (!bptFileFind(bbi->chromBpt, chrom, strlen(chrom), &idSize, sizeof(idSize))) return NULL; -if (bbi->isSwapped) - idSize.chromId = byteSwap32(idSize.chromId); +chromIdSizeHandleSwapped(bbi->isSwapped, &idSize); if (retChromId != NULL) *retChromId = idSize.chromId; return cirTreeFindOverlappingBlocks(ctf, idSize.chromId, start, end); } struct chromNameCallbackContext /* Some stuff that the bPlusTree traverser needs for context. */ { struct bbiChromInfo *list; /* The list we are building. */ boolean isSwapped; /* Need to byte-swap things? */ }; static void chromNameCallback(void *context, void *key, int keySize, void *val, int valSize) /* Callback that captures chromInfo from bPlusTree. */ { struct chromNameCallbackContext *c = context; struct bbiChromInfo *info; struct bbiChromIdSize *idSize = val; assert(valSize == sizeof(*idSize)); +chromIdSizeHandleSwapped(c->isSwapped, idSize); AllocVar(info); info->name = cloneStringZ(key, keySize); info->id = idSize->chromId; info->size = idSize->chromSize; -if (c->isSwapped) - { - info->id = byteSwap32(info->id); - info->size = byteSwap32(info->size); - } slAddHead(&c->list, info); } struct bbiChromInfo *bbiChromList(struct bbiFile *bbi) /* Return list of chromosomes. */ { struct chromNameCallbackContext context; context.list = NULL; context.isSwapped = bbi->isSwapped; bptFileTraverse(bbi->chromBpt, &context, chromNameCallback); slReverse(&context.list); return context.list; } bits32 bbiChromSize(struct bbiFile *bbi, char *chrom) /* Return chromosome size, or 0 if no such chromosome in file. */ { struct bbiChromIdSize idSize; if (!bptFileFind(bbi->chromBpt, chrom, strlen(chrom), &idSize, sizeof(idSize))) return 0; +chromIdSizeHandleSwapped(bbi->isSwapped, &idSize); return idSize.chromSize; } void bbiChromInfoFree(struct bbiChromInfo **pInfo) /* Free up one chromInfo */ { struct bbiChromInfo *info = *pInfo; if (info != NULL) { freeMem(info->name); freez(pInfo); } } void bbiChromInfoFreeList(struct bbiChromInfo **pList) @@ -291,46 +297,51 @@ errAbort("Unknown bbiSummaryType %d", (int)type); return NULL; } } #ifdef UNUSED static void bbiSummaryOnDiskRead(struct bbiFile *bbi, struct bbiSummaryOnDisk *sum) /* Read in summary from file. */ { struct udcFile *udc = bbi->udc; boolean isSwapped = bbi->isSwapped; sum->chromId = udcReadBits32(udc, isSwapped); sum->start = udcReadBits32(udc, isSwapped); sum->end = udcReadBits32(udc, isSwapped); sum->validCount = udcReadBits32(udc, isSwapped); +// looks like a bug to me, these should call udcReadFloat() udcMustReadOne(udc, sum->minVal); udcMustReadOne(udc, sum->maxVal); udcMustReadOne(udc, sum->sumData); udcMustReadOne(udc, sum->sumSquares); } #endif /* UNUSED */ static void bbiSummaryHandleSwapped(struct bbiFile *bbi, struct bbiSummaryOnDisk *in) /* Swap integer fields in summary as needed. */ { if (bbi->isSwapped) { in->chromId = byteSwap32(in->chromId); in->start = byteSwap32(in->start); in->end = byteSwap32(in->end); in->validCount = byteSwap32(in->validCount); + in->minVal = byteSwapFloat(in->minVal); + in->maxVal = byteSwapFloat(in->maxVal); + in->sumData = byteSwapFloat(in->sumData); + in->sumSquares = byteSwapFloat(in->sumSquares); } } static struct bbiSummary *bbiSummaryFromOnDisk(struct bbiSummaryOnDisk *in) /* Create a bbiSummary unlinked to anything from input in onDisk format. */ { struct bbiSummary *out; AllocVar(out); out->chromId = in->chromId; out->start = in->start; out->end = in->end; out->validCount = in->validCount; out->minVal = in->minVal; out->maxVal = in->maxVal; out->sumData = in->sumData; @@ -457,30 +468,31 @@ el->minVal = minVal; el->maxVal = maxVal; el->sumData = sumData; el->sumSquares = sumSquares; } } return validCount; } static int bbiChromId(struct bbiFile *bbi, char *chrom) /* Return chromosome size */ { struct bbiChromIdSize idSize; if (!bptFileFind(bbi->chromBpt, chrom, strlen(chrom), &idSize, sizeof(idSize))) return -1; +chromIdSizeHandleSwapped(bbi->isSwapped, &idSize); return idSize.chromId; } static boolean bbiSummaryArrayFromZoom(struct bbiZoomLevel *zoom, struct bbiFile *bbi, char *chrom, bits32 start, bits32 end, int summarySize, struct bbiSummaryElement *summary) /* Look up region in index and get data at given zoom level. Summarize this data * in the summary array. */ { boolean result = FALSE; int chromId = bbiChromId(bbi, chrom); if (chromId < 0) return FALSE; struct bbiSummary *sum, *sumList = bbiSummariesInRegion(zoom, bbi, chromId, start, end); if (sumList != NULL)