Skip to content

Commit df862d5

Browse files
authored
Merge pull request #155 from deeptools/fix152
Copy over updated libBigWig
2 parents 335b04e + 2ae221d commit df862d5

File tree

9 files changed

+138
-119
lines changed

9 files changed

+138
-119
lines changed

libBigWig/bigWig.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ extern "C" {
5656
/*!
5757
* The library version number
5858
*/
59-
#define LIBBIGWIG_VERSION 0.4.6
59+
#define LIBBIGWIG_VERSION 0.4.8
6060

6161
/*!
6262
* If 1, then this library was compiled with remote file support.
@@ -279,7 +279,7 @@ void bwCleanup(void);
279279
* @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.
280280
* @return 1 if the file appears to be bigWig, otherwise 0.
281281
*/
282-
int bwIsBigWig(char *fname, CURLcode (*callBack)(CURL*));
282+
int bwIsBigWig(const char *fname, CURLcode (*callBack)(CURL*));
283283

284284
/*!
285285
* @brief Determine is a file is a bigBed file.
@@ -288,7 +288,7 @@ int bwIsBigWig(char *fname, CURLcode (*callBack)(CURL*));
288288
* @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.
289289
* @return 1 if the file appears to be bigWig, otherwise 0.
290290
*/
291-
int bbIsBigBed(char *fname, CURLcode (*callBack)(CURL*));
291+
int bbIsBigBed(const char *fname, CURLcode (*callBack)(CURL*));
292292

293293
/*!
294294
* @brief Opens a local or remote bigWig file.
@@ -298,7 +298,7 @@ int bbIsBigBed(char *fname, CURLcode (*callBack)(CURL*));
298298
* @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.
299299
* @return A bigWigFile_t * on success and NULL on error.
300300
*/
301-
bigWigFile_t *bwOpen(char *fname, CURLcode (*callBack)(CURL*), const char* mode);
301+
bigWigFile_t *bwOpen(const char *fname, CURLcode (*callBack)(CURL*), const char* mode);
302302

303303
/*!
304304
* @brief Opens a local or remote bigBed file.
@@ -307,7 +307,7 @@ bigWigFile_t *bwOpen(char *fname, CURLcode (*callBack)(CURL*), const char* mode)
307307
* @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.
308308
* @return A bigWigFile_t * on success and NULL on error.
309309
*/
310-
bigWigFile_t *bbOpen(char *fname, CURLcode (*callBack)(CURL*));
310+
bigWigFile_t *bbOpen(const char *fname, CURLcode (*callBack)(CURL*));
311311

312312
/*!
313313
* @brief Returns a string containing the SQL entry (or NULL).
@@ -339,7 +339,7 @@ void bwClose(bigWigFile_t *fp);
339339
* @param chrom A chromosome name
340340
* @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.
341341
*/
342-
uint32_t bwGetTid(bigWigFile_t *fp, char *chrom);
342+
uint32_t bwGetTid(const bigWigFile_t *fp, const char *chrom);
343343

344344
/*!
345345
* @brief Frees space allocated by `bwGetOverlappingIntervals`
@@ -367,7 +367,7 @@ void bbDestroyOverlappingEntries(bbOverlappingEntries_t *o);
367367
* @see bwDestroyOverlappingIntervals
368368
* @see bwGetValues
369369
*/
370-
bwOverlappingIntervals_t *bwGetOverlappingIntervals(bigWigFile_t *fp, char *chrom, uint32_t start, uint32_t end);
370+
bwOverlappingIntervals_t *bwGetOverlappingIntervals(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end);
371371

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

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

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

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

450450
/*!
451451
* @brief Determines per-interval bigWig statistics
@@ -459,7 +459,7 @@ bwOverlappingIntervals_t *bwGetValues(bigWigFile_t *fp, char *chrom, uint32_t st
459459
* @see bwStatsType
460460
* @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.
461461
*/
462-
double *bwStats(bigWigFile_t *fp, char *chrom, uint32_t start, uint32_t end, uint32_t nBins, enum bwStatsType type);
462+
double *bwStats(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end, uint32_t nBins, enum bwStatsType type);
463463

464464
/*!
465465
* @brief Determines per-interval bigWig statistics
@@ -473,7 +473,7 @@ double *bwStats(bigWigFile_t *fp, char *chrom, uint32_t start, uint32_t end, uin
473473
* @see bwStatsType
474474
* @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.
475475
*/
476-
double *bwStatsFromFull(bigWigFile_t *fp, char *chrom, uint32_t start, uint32_t end, uint32_t nBins, enum bwStatsType type);
476+
double *bwStatsFromFull(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end, uint32_t nBins, enum bwStatsType type);
477477

478478
//Writer functions
479479

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

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

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

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

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

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

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

602602
#ifdef __cplusplus
603603
}

libBigWig/bigWigIO.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ typedef struct {
4949
size_t bufLen; /**<The actual size of the buffer used.*/
5050
enum bigWigFile_type_enum type; /**<The connection type*/
5151
int isCompressed; /**<1 if the file is compressed, otherwise 0*/
52-
char *fname; /**<Only needed for remote connections. The original URL/filename requested, since we need to make multiple connections.*/
52+
const char *fname; /**<Only needed for remote connections. The original URL/filename requested, since we need to make multiple connections.*/
5353
} URL_t;
5454

5555
/*!
@@ -94,7 +94,7 @@ CURLcode urlSeek(URL_t *URL, size_t pos);
9494
*
9595
* @return A URL_t * or NULL on error.
9696
*/
97-
URL_t *urlOpen(char *fname, CURLcode (*callBack)(CURL*), const char* mode);
97+
URL_t *urlOpen(const char *fname, CURLcode (*callBack)(CURL*), const char* mode);
9898

9999
/*!
100100
* @brief Close a local/remote file

libBigWig/bwCommon.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ void destroyBWOverlapBlock(bwOverlapBlock_t *b);
6868
* @return 0 on success
6969
*/
7070
int bwFinalize(bigWigFile_t *fp);
71+
72+
/// @cond SKIP
73+
char *bwStrdup(const char *s);
74+
/// @endcond

libBigWig/bwRead.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ static uint64_t readChromLeaf(bigWigFile_t *bw, chromList_t *cl, uint32_t valueS
194194
if(bwRead((void*) chrom, sizeof(char), valueSize, bw) != valueSize) goto error;
195195
if(bwRead((void*) &idx, sizeof(uint32_t), 1, bw) != 1) goto error;
196196
if(bwRead((void*) &(cl->len[idx]), sizeof(uint32_t), 1, bw) != 1) goto error;
197-
cl->chrom[idx] = strdup(chrom);
197+
cl->chrom[idx] = bwStrdup(chrom);
198198
if(!(cl->chrom[idx])) goto error;
199199
}
200200

@@ -299,7 +299,7 @@ void bwClose(bigWigFile_t *fp) {
299299
free(fp);
300300
}
301301

302-
int bwIsBigWig(char *fname, CURLcode (*callBack) (CURL*)) {
302+
int bwIsBigWig(const char *fname, CURLcode (*callBack) (CURL*)) {
303303
uint32_t magic = 0;
304304
URL_t *URL = NULL;
305305

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

315-
char *bbGetSQL(bigWigFile_t *bw) {
315+
char *bbGetSQL(bigWigFile_t *fp) {
316316
char *o = NULL;
317317
uint64_t len;
318-
if(!bw->hdr->sqlOffset) return NULL;
319-
len = bw->hdr->summaryOffset - bw->hdr->sqlOffset; //This includes the NULL terminator
318+
if(!fp->hdr->sqlOffset) return NULL;
319+
len = fp->hdr->summaryOffset - fp->hdr->sqlOffset; //This includes the NULL terminator
320320
o = malloc(sizeof(char) * len);
321321
if(!o) goto error;
322-
if(bwSetPos(bw, bw->hdr->sqlOffset)) goto error;
323-
if(bwRead((void*) o, len, 1, bw) != 1) goto error;
322+
if(bwSetPos(fp, fp->hdr->sqlOffset)) goto error;
323+
if(bwRead((void*) o, len, 1, fp) != 1) goto error;
324324
return o;
325325

326326
error:
@@ -329,7 +329,7 @@ char *bbGetSQL(bigWigFile_t *bw) {
329329
return NULL;
330330
}
331331

332-
int bbIsBigBed(char *fname, CURLcode (*callBack) (CURL*)) {
332+
int bbIsBigBed(const char *fname, CURLcode (*callBack) (CURL*)) {
333333
uint32_t magic = 0;
334334
URL_t *URL = NULL;
335335

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

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

397-
bigWigFile_t *bbOpen(char *fname, CURLcode (*callBack) (CURL*)) {
397+
bigWigFile_t *bbOpen(const char *fname, CURLcode (*callBack) (CURL*)) {
398398
bigWigFile_t *bb = calloc(1, sizeof(bigWigFile_t));
399399
if(!bb) {
400400
fprintf(stderr, "[bbOpen] Couldn't allocate space to create the output object!\n");
@@ -425,3 +425,14 @@ bigWigFile_t *bbOpen(char *fname, CURLcode (*callBack) (CURL*)) {
425425
bwClose(bb);
426426
return NULL;
427427
}
428+
429+
430+
//Implementation taken from musl:
431+
//https://git.musl-libc.org/cgit/musl/tree/src/string/strdup.c
432+
//License: https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
433+
char* bwStrdup(const char *s) {
434+
size_t l = strlen(s);
435+
char *d = malloc(l+1);
436+
if (!d) return NULL;
437+
return memcpy(d, s, l+1);
438+
}

libBigWig/bwStats.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

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

7171
if(sz) {
7272
compressed = 1;
73-
buf = malloc(sz);
73+
buf = malloc(sz);
7474
}
7575
sz = 0; //This is now the size of the compressed buffer
7676

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

9898
p = buf;
99-
while(((uLongf) ((void*)p-buf)) < sz) {
99+
while(((uLongf) ((char*)p - (char*)buf)) < sz) {
100100
vtid = p[0];
101101
vstart = p[1];
102102
vend = p[2];
@@ -420,7 +420,7 @@ static double intSum(bwOverlappingIntervals_t* ints, uint32_t start, uint32_t en
420420
}
421421

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

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

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

0 commit comments

Comments
 (0)