Skip to content

Commit

Permalink
Copy over updated libBigWig
Browse files Browse the repository at this point in the history
  • Loading branch information
Devon Ryan committed Jan 17, 2025
1 parent 335b04e commit 2d8eb13
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 117 deletions.
40 changes: 20 additions & 20 deletions libBigWig/bigWig.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ extern "C" {
/*!
* The library version number
*/
#define LIBBIGWIG_VERSION 0.4.6
#define LIBBIGWIG_VERSION 0.4.8

/*!
* If 1, then this library was compiled with remote file support.
Expand Down Expand Up @@ -279,7 +279,7 @@ void bwCleanup(void);
* @param callBack An optional user-supplied function. This is applied to remote connections so users can specify things like proxy and password information. See `test/testRemote` for an example.
* @return 1 if the file appears to be bigWig, otherwise 0.
*/
int bwIsBigWig(char *fname, CURLcode (*callBack)(CURL*));
int bwIsBigWig(const char *fname, CURLcode (*callBack)(CURL*));

/*!
* @brief Determine is a file is a bigBed file.
Expand All @@ -288,7 +288,7 @@ int bwIsBigWig(char *fname, CURLcode (*callBack)(CURL*));
* @param callBack An optional user-supplied function. This is applied to remote connections so users can specify things like proxy and password information. See `test/testRemote` for an example.
* @return 1 if the file appears to be bigWig, otherwise 0.
*/
int bbIsBigBed(char *fname, CURLcode (*callBack)(CURL*));
int bbIsBigBed(const char *fname, CURLcode (*callBack)(CURL*));

/*!
* @brief Opens a local or remote bigWig file.
Expand All @@ -298,7 +298,7 @@ int bbIsBigBed(char *fname, CURLcode (*callBack)(CURL*));
* @param mode The mode, by default "r". Both local and remote files can be read, but only local files can be written. For files being written the callback function is ignored. If and only if the mode contains "w" will the file be opened for writing (in all other cases the file will be opened for reading.
* @return A bigWigFile_t * on success and NULL on error.
*/
bigWigFile_t *bwOpen(char *fname, CURLcode (*callBack)(CURL*), const char* mode);
bigWigFile_t *bwOpen(const char *fname, CURLcode (*callBack)(CURL*), const char* mode);

/*!
* @brief Opens a local or remote bigBed file.
Expand All @@ -307,7 +307,7 @@ bigWigFile_t *bwOpen(char *fname, CURLcode (*callBack)(CURL*), const char* mode)
* @param callBack An optional user-supplied function. This is applied to remote connections so users can specify things like proxy and password information. See `test/testRemote` for an example.
* @return A bigWigFile_t * on success and NULL on error.
*/
bigWigFile_t *bbOpen(char *fname, CURLcode (*callBack)(CURL*));
bigWigFile_t *bbOpen(const char *fname, CURLcode (*callBack)(CURL*));

/*!
* @brief Returns a string containing the SQL entry (or NULL).
Expand Down Expand Up @@ -339,7 +339,7 @@ void bwClose(bigWigFile_t *fp);
* @param chrom A chromosome name
* @return An ID, -1 will be returned on error (note that this is an unsigned value, so that's ~4 billion. bigWig/bigBed files can't store that many chromosomes anyway.
*/
uint32_t bwGetTid(bigWigFile_t *fp, char *chrom);
uint32_t bwGetTid(const bigWigFile_t *fp, const char *chrom);

/*!
* @brief Frees space allocated by `bwGetOverlappingIntervals`
Expand Down Expand Up @@ -367,7 +367,7 @@ void bbDestroyOverlappingEntries(bbOverlappingEntries_t *o);
* @see bwDestroyOverlappingIntervals
* @see bwGetValues
*/
bwOverlappingIntervals_t *bwGetOverlappingIntervals(bigWigFile_t *fp, char *chrom, uint32_t start, uint32_t end);
bwOverlappingIntervals_t *bwGetOverlappingIntervals(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end);

/*!
* @brief Return bigBed entries overlapping an interval.
Expand All @@ -381,7 +381,7 @@ bwOverlappingIntervals_t *bwGetOverlappingIntervals(bigWigFile_t *fp, char *chro
* @see bbOverlappingEntries_t
* @see bbDestroyOverlappingEntries
*/
bbOverlappingEntries_t *bbGetOverlappingEntries(bigWigFile_t *fp, char *chrom, uint32_t start, uint32_t end, int withString);
bbOverlappingEntries_t *bbGetOverlappingEntries(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end, int withString);

/*!
* @brief Creates an iterator over intervals in a bigWig file
Expand All @@ -397,7 +397,7 @@ bbOverlappingEntries_t *bbGetOverlappingEntries(bigWigFile_t *fp, char *chrom, u
* @see bwIteratorNext
* @see bwIteratorDestroy
*/
bwOverlapIterator_t *bwOverlappingIntervalsIterator(bigWigFile_t *fp, char *chrom, uint32_t start, uint32_t end, uint32_t blocksPerIteration);
bwOverlapIterator_t *bwOverlappingIntervalsIterator(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end, uint32_t blocksPerIteration);

/*!
* @brief Creates an iterator over entries in a bigBed file
Expand All @@ -415,7 +415,7 @@ bwOverlapIterator_t *bwOverlappingIntervalsIterator(bigWigFile_t *fp, char *chro
* @see bwIteratorNext
* @see bwIteratorDestroy
*/
bwOverlapIterator_t *bbOverlappingEntriesIterator(bigWigFile_t *fp, char *chrom, uint32_t start, uint32_t end, int withString, uint32_t blocksPerIteration);
bwOverlapIterator_t *bbOverlappingEntriesIterator(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end, int withString, uint32_t blocksPerIteration);

/*!
* @brief Traverses to the entries/intervals in the next group of blocks.
Expand Down Expand Up @@ -445,7 +445,7 @@ void bwIteratorDestroy(bwOverlapIterator_t *iter);
* @see bwDestroyOverlappingIntervals
* @see bwGetOverlappingIntervals
*/
bwOverlappingIntervals_t *bwGetValues(bigWigFile_t *fp, char *chrom, uint32_t start, uint32_t end, int includeNA);
bwOverlappingIntervals_t *bwGetValues(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end, int includeNA);

/*!
* @brief Determines per-interval bigWig statistics
Expand All @@ -459,7 +459,7 @@ bwOverlappingIntervals_t *bwGetValues(bigWigFile_t *fp, char *chrom, uint32_t st
* @see bwStatsType
* @return A pointer to an array of double precission floating point values. Note that bigWig files only hold 32-bit values, so this is done to help prevent overflows.
*/
double *bwStats(bigWigFile_t *fp, char *chrom, uint32_t start, uint32_t end, uint32_t nBins, enum bwStatsType type);
double *bwStats(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end, uint32_t nBins, enum bwStatsType type);

/*!
* @brief Determines per-interval bigWig statistics
Expand All @@ -473,7 +473,7 @@ double *bwStats(bigWigFile_t *fp, char *chrom, uint32_t start, uint32_t end, uin
* @see bwStatsType
* @return A pointer to an array of double precission floating point values. Note that bigWig files only hold 32-bit values, so this is done to help prevent overflows.
*/
double *bwStatsFromFull(bigWigFile_t *fp, char *chrom, uint32_t start, uint32_t end, uint32_t nBins, enum bwStatsType type);
double *bwStatsFromFull(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end, uint32_t nBins, enum bwStatsType type);

//Writer functions

Expand All @@ -494,7 +494,7 @@ int bwCreateHdr(bigWigFile_t *fp, int32_t maxZooms);
* @param n The number of chromosomes (thus, the length of `chroms` and `lengths`)
* @return A pointer to a chromList_t or NULL on error.
*/
chromList_t *bwCreateChromList(char **chroms, uint32_t *lengths, int64_t n);
chromList_t *bwCreateChromList(const char* const* chroms, const uint32_t *lengths, int64_t n);

/*!
* @brief Write a the header to a bigWig file.
Expand All @@ -521,7 +521,7 @@ int bwWriteHdr(bigWigFile_t *bw);
* @return 0 on success and another value on error.
* @see bwAppendIntervals
*/
int bwAddIntervals(bigWigFile_t *fp, char **chrom, uint32_t *start, uint32_t *end, float *values, uint32_t n);
int bwAddIntervals(bigWigFile_t *fp, const char* const* chrom, const uint32_t *start, const uint32_t *end, const float *values, uint32_t n);

/*!
* @brief Append bedGraph-like intervals to a previous block of bedGraph-like intervals in a bigWig file.
Expand All @@ -535,7 +535,7 @@ int bwAddIntervals(bigWigFile_t *fp, char **chrom, uint32_t *start, uint32_t *en
* @warning Do NOT use this after `bwAddIntervalSpanSteps()`, `bwAppendIntervalSpanSteps()`, `bwAddIntervalSpanSteps()`, or `bwAppendIntervalSpanSteps()`.
* @see bwAddIntervals
*/
int bwAppendIntervals(bigWigFile_t *fp, uint32_t *start, uint32_t *end, float *values, uint32_t n);
int bwAppendIntervals(bigWigFile_t *fp, const uint32_t *start, const uint32_t *end, const float *values, uint32_t n);

/*!
* @brief Add a new block of variable-step entries to a bigWig file
Expand All @@ -553,7 +553,7 @@ int bwAppendIntervals(bigWigFile_t *fp, uint32_t *start, uint32_t *end, float *v
* @return 0 on success and another value on error.
* @see bwAppendIntervalSpans
*/
int bwAddIntervalSpans(bigWigFile_t *fp, char *chrom, uint32_t *start, uint32_t span, float *values, uint32_t n);
int bwAddIntervalSpans(bigWigFile_t *fp, const char *chrom, const uint32_t *start, uint32_t span, const float *values, uint32_t n);

/*!
* @brief Append to a previous block of variable-step entries.
Expand All @@ -566,7 +566,7 @@ int bwAddIntervalSpans(bigWigFile_t *fp, char *chrom, uint32_t *start, uint32_t
* @warning Do NOT use this after `bwAddIntervals()`, `bwAppendIntervals()`, `bwAddIntervalSpanSteps()` or `bwAppendIntervalSpanSteps()`
* @see bwAddIntervalSpans
*/
int bwAppendIntervalSpans(bigWigFile_t *fp, uint32_t *start, float *values, uint32_t n);
int bwAppendIntervalSpans(bigWigFile_t *fp, const uint32_t *start, const float *values, uint32_t n);

/*!
* @brief Add a new block of fixed-step entries to a bigWig file
Expand All @@ -585,7 +585,7 @@ int bwAppendIntervalSpans(bigWigFile_t *fp, uint32_t *start, float *values, uint
* @return 0 on success and another value on error.
* @see bwAddIntervalSpanSteps
*/
int bwAddIntervalSpanSteps(bigWigFile_t *fp, char *chrom, uint32_t start, uint32_t span, uint32_t step, float *values, uint32_t n);
int bwAddIntervalSpanSteps(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t span, uint32_t step, const float *values, uint32_t n);

/*!
* @brief Append to a previous block of fixed-step entries.
Expand All @@ -597,7 +597,7 @@ int bwAddIntervalSpanSteps(bigWigFile_t *fp, char *chrom, uint32_t start, uint32
* @warning Do NOT use this after `bwAddIntervals()`, `bwAppendIntervals()`, `bwAddIntervalSpans()` or `bwAppendIntervalSpans()`
* @see bwAddIntervalSpanSteps
*/
int bwAppendIntervalSpanSteps(bigWigFile_t *fp, float *values, uint32_t n);
int bwAppendIntervalSpanSteps(bigWigFile_t *fp, const float *values, uint32_t n);

#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions libBigWig/bigWigIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ typedef struct {
size_t bufLen; /**<The actual size of the buffer used.*/
enum bigWigFile_type_enum type; /**<The connection type*/
int isCompressed; /**<1 if the file is compressed, otherwise 0*/
char *fname; /**<Only needed for remote connections. The original URL/filename requested, since we need to make multiple connections.*/
const char *fname; /**<Only needed for remote connections. The original URL/filename requested, since we need to make multiple connections.*/
} URL_t;

/*!
Expand Down Expand Up @@ -94,7 +94,7 @@ CURLcode urlSeek(URL_t *URL, size_t pos);
*
* @return A URL_t * or NULL on error.
*/
URL_t *urlOpen(char *fname, CURLcode (*callBack)(CURL*), const char* mode);
URL_t *urlOpen(const char *fname, CURLcode (*callBack)(CURL*), const char* mode);

/*!
* @brief Close a local/remote file
Expand Down
4 changes: 4 additions & 0 deletions libBigWig/bwCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ void destroyBWOverlapBlock(bwOverlapBlock_t *b);
* @return 0 on success
*/
int bwFinalize(bigWigFile_t *fp);

/// @cond SKIP
char *bwStrdup(const char *s);
/// @endcond
31 changes: 21 additions & 10 deletions libBigWig/bwRead.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ static uint64_t readChromLeaf(bigWigFile_t *bw, chromList_t *cl, uint32_t valueS
if(bwRead((void*) chrom, sizeof(char), valueSize, bw) != valueSize) goto error;
if(bwRead((void*) &idx, sizeof(uint32_t), 1, bw) != 1) goto error;
if(bwRead((void*) &(cl->len[idx]), sizeof(uint32_t), 1, bw) != 1) goto error;
cl->chrom[idx] = strdup(chrom);
cl->chrom[idx] = bwStrdup(chrom);
if(!(cl->chrom[idx])) goto error;
}

Expand Down Expand Up @@ -299,7 +299,7 @@ void bwClose(bigWigFile_t *fp) {
free(fp);
}

int bwIsBigWig(char *fname, CURLcode (*callBack) (CURL*)) {
int bwIsBigWig(const char *fname, CURLcode (*callBack) (CURL*)) {
uint32_t magic = 0;
URL_t *URL = NULL;

Expand All @@ -312,15 +312,15 @@ int bwIsBigWig(char *fname, CURLcode (*callBack) (CURL*)) {
return 0;
}

char *bbGetSQL(bigWigFile_t *bw) {
char *bbGetSQL(bigWigFile_t *fp) {
char *o = NULL;
uint64_t len;
if(!bw->hdr->sqlOffset) return NULL;
len = bw->hdr->summaryOffset - bw->hdr->sqlOffset; //This includes the NULL terminator
if(!fp->hdr->sqlOffset) return NULL;
len = fp->hdr->summaryOffset - fp->hdr->sqlOffset; //This includes the NULL terminator
o = malloc(sizeof(char) * len);
if(!o) goto error;
if(bwSetPos(bw, bw->hdr->sqlOffset)) goto error;
if(bwRead((void*) o, len, 1, bw) != 1) goto error;
if(bwSetPos(fp, fp->hdr->sqlOffset)) goto error;
if(bwRead((void*) o, len, 1, fp) != 1) goto error;
return o;

error:
Expand All @@ -329,7 +329,7 @@ char *bbGetSQL(bigWigFile_t *bw) {
return NULL;
}

int bbIsBigBed(char *fname, CURLcode (*callBack) (CURL*)) {
int bbIsBigBed(const char *fname, CURLcode (*callBack) (CURL*)) {
uint32_t magic = 0;
URL_t *URL = NULL;

Expand All @@ -342,7 +342,7 @@ int bbIsBigBed(char *fname, CURLcode (*callBack) (CURL*)) {
return 0;
}

bigWigFile_t *bwOpen(char *fname, CURLcode (*callBack) (CURL*), const char *mode) {
bigWigFile_t *bwOpen(const char *fname, CURLcode (*callBack) (CURL*), const char *mode) {
bigWigFile_t *bwg = calloc(1, sizeof(bigWigFile_t));
if(!bwg) {
fprintf(stderr, "[bwOpen] Couldn't allocate space to create the output object!\n");
Expand Down Expand Up @@ -394,7 +394,7 @@ bigWigFile_t *bwOpen(char *fname, CURLcode (*callBack) (CURL*), const char *mode
return NULL;
}

bigWigFile_t *bbOpen(char *fname, CURLcode (*callBack) (CURL*)) {
bigWigFile_t *bbOpen(const char *fname, CURLcode (*callBack) (CURL*)) {
bigWigFile_t *bb = calloc(1, sizeof(bigWigFile_t));
if(!bb) {
fprintf(stderr, "[bbOpen] Couldn't allocate space to create the output object!\n");
Expand Down Expand Up @@ -425,3 +425,14 @@ bigWigFile_t *bbOpen(char *fname, CURLcode (*callBack) (CURL*)) {
bwClose(bb);
return NULL;
}


//Implementation taken from musl:
//https://git.musl-libc.org/cgit/musl/tree/src/string/strdup.c
//License: https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
char* bwStrdup(const char *s) {
size_t l = strlen(s);
char *d = malloc(l+1);
if (!d) return NULL;
return memcpy(d, s, l+1);
}
12 changes: 6 additions & 6 deletions libBigWig/bwStats.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

//Returns -1 if there are no applicable levels, otherwise an integer indicating the most appropriate level.
//Like Kent's library, this divides the desired bin size by 2 to minimize the effect of blocks overlapping multiple bins
static int32_t determineZoomLevel(bigWigFile_t *fp, int basesPerBin) {
static int32_t determineZoomLevel(const bigWigFile_t *fp, int basesPerBin) {
int32_t out = -1;
int64_t diff;
uint32_t bestDiff = -1;
Expand Down Expand Up @@ -70,7 +70,7 @@ static struct vals_t *getVals(bigWigFile_t *fp, bwOverlapBlock_t *o, int i, uint

if(sz) {
compressed = 1;
buf = malloc(sz);
buf = malloc(sz);
}
sz = 0; //This is now the size of the compressed buffer

Expand All @@ -96,7 +96,7 @@ static struct vals_t *getVals(bigWigFile_t *fp, bwOverlapBlock_t *o, int i, uint
}

p = buf;
while(((uLongf) ((void*)p-buf)) < sz) {
while(((uLongf) ((char*)p - (char*)buf)) < sz) {
vtid = p[0];
vstart = p[1];
vend = p[2];
Expand Down Expand Up @@ -420,7 +420,7 @@ static double intSum(bwOverlappingIntervals_t* ints, uint32_t start, uint32_t en
}

//Returns NULL on error, otherwise a double* that needs to be free()d
double *bwStatsFromZoom(bigWigFile_t *fp, int32_t level, uint32_t tid, uint32_t start, uint32_t end, uint32_t nBins, enum bwStatsType type) {
static double *bwStatsFromZoom(bigWigFile_t *fp, int32_t level, uint32_t tid, uint32_t start, uint32_t end, uint32_t nBins, enum bwStatsType type) {
bwOverlapBlock_t *blocks = NULL;
double *output = NULL;
uint32_t pos = start, i, end2;
Expand Down Expand Up @@ -482,7 +482,7 @@ double *bwStatsFromZoom(bigWigFile_t *fp, int32_t level, uint32_t tid, uint32_t
return NULL;
}

double *bwStatsFromFull(bigWigFile_t *fp, char *chrom, uint32_t start, uint32_t end, uint32_t nBins, enum bwStatsType type) {
double *bwStatsFromFull(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end, uint32_t nBins, enum bwStatsType type) {
bwOverlappingIntervals_t *ints = NULL;
double *output = malloc(sizeof(double)*nBins);
uint32_t i, pos = start, end2;
Expand Down Expand Up @@ -527,7 +527,7 @@ double *bwStatsFromFull(bigWigFile_t *fp, char *chrom, uint32_t start, uint32_t

//Returns a list of floats of length nBins that must be free()d
//On error, NULL is returned
double *bwStats(bigWigFile_t *fp, char *chrom, uint32_t start, uint32_t end, uint32_t nBins, enum bwStatsType type) {
double *bwStats(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end, uint32_t nBins, enum bwStatsType type) {
int32_t level = determineZoomLevel(fp, ((double)(end-start))/((int) nBins));
uint32_t tid = bwGetTid(fp, chrom);
if(tid == (uint32_t) -1) return NULL;
Expand Down
Loading

0 comments on commit 2d8eb13

Please sign in to comment.