static bits64 bbiWriteSummaryAndIndexComp(struct bbiSummary *summaryList, int blockSize, int itemsPerSlot, FILE *f) /* Write out summary and index to summary uncompressed, returning start position of * summary index. */ { bits32 i, count = slCount(summaryList); struct bbiSummary **summaryArray; AllocArray(summaryArray, count); writeOne(f, count); struct bbiSummary *summary = summaryList; /* Figure out max size of uncompressed and compressed blocks. */ bits32 itemSize = sizeof(summary->chromId) + sizeof(summary->start) + sizeof(summary->end) + sizeof(summary->validCount) + 4*sizeof(float); int uncBufSize = itemSize * itemsPerSlot; char uncBuf[uncBufSize]; int compBufSize = zCompBufSize(uncBufSize); char compBuf[compBufSize]; /* Loop through compressing and writing one slot at a time. */ bits32 itemsLeft = count; int sumIx = 0; while (itemsLeft > 0) { bits32 itemsInSlot = itemsLeft; if (itemsInSlot > itemsPerSlot) itemsInSlot = itemsPerSlot; char *writePt = uncBuf; bits64 filePos = ftell(f); for (i=0; i<itemsInSlot; ++i) { summaryArray[sumIx++] = summary; memWriteOne(&writePt, summary->chromId); memWriteOne(&writePt, summary->start); memWriteOne(&writePt, summary->end); memWriteOne(&writePt, summary->validCount); memWriteFloat(&writePt, summary->minVal); memWriteFloat(&writePt, summary->maxVal); memWriteFloat(&writePt, summary->sumData); memWriteFloat(&writePt, summary->sumSquares); summary->fileOffset = filePos; summary = summary->next; if (summary == NULL) break; } bits32 uncSize = writePt - uncBuf; int compSize = zCompress(uncBuf, uncSize, compBuf, compBufSize); mustWrite(f, compBuf, compSize); itemsLeft -= itemsInSlot; } bits64 indexOffset = ftell(f); cirTreeFileBulkIndexToOpenFile(summaryArray, sizeof(summaryArray[0]), count, blockSize, itemsPerSlot, NULL, bbiSummaryFetchKey, bbiSummaryFetchOffset, indexOffset, f); freez(&summaryArray); return indexOffset; }
static int bwgSectionWrite(struct bwgSection *section, boolean doCompress, FILE *f) /* Write out section to file, filling in section->fileOffset. */ { UBYTE type = section->type; UBYTE reserved8 = 0; int itemSize; switch (section->type) { case bwgTypeBedGraph: itemSize = 12; break; case bwgTypeVariableStep: itemSize = 8; break; case bwgTypeFixedStep: itemSize = 4; break; default: itemSize = 0; // Suppress compiler warning internalErr(); break; } int fixedSize = sizeof(section->chromId) + sizeof(section->start) + sizeof(section->end) + sizeof(section->itemStep) + sizeof(section->itemSpan) + sizeof(type) + sizeof(reserved8) + sizeof(section->itemCount); int bufSize = section->itemCount * itemSize + fixedSize; char buf[bufSize]; char *bufPt = buf; section->fileOffset = ftell(f); memWriteOne(&bufPt, section->chromId); memWriteOne(&bufPt, section->start); memWriteOne(&bufPt, section->end); memWriteOne(&bufPt, section->itemStep); memWriteOne(&bufPt, section->itemSpan); memWriteOne(&bufPt, type); memWriteOne(&bufPt, reserved8); memWriteOne(&bufPt, section->itemCount); int i; switch (section->type) { case bwgTypeBedGraph: { struct bwgBedGraphItem *item = section->items.bedGraphList; for (item = section->items.bedGraphList; item != NULL; item = item->next) { memWriteOne(&bufPt, item->start); memWriteOne(&bufPt, item->end); memWriteOne(&bufPt, item->val); } break; } case bwgTypeVariableStep: { struct bwgVariableStepPacked *items = section->items.variableStepPacked; for (i=0; i<section->itemCount; ++i) { memWriteOne(&bufPt, items->start); memWriteOne(&bufPt, items->val); items += 1; } break; } case bwgTypeFixedStep: { struct bwgFixedStepPacked *items = section->items.fixedStepPacked; for (i=0; i<section->itemCount; ++i) { memWriteOne(&bufPt, items->val); items += 1; } break; } default: internalErr(); break; } assert(bufSize == (bufPt - buf) ); if (doCompress) { size_t maxCompSize = zCompBufSize(bufSize); char compBuf[maxCompSize]; int compSize = zCompress(buf, bufSize, compBuf, maxCompSize); mustWrite(f, compBuf, compSize); } else mustWrite(f, buf, bufSize); return bufSize; }