Loading...
Searching...
No Matches
bigWig.h
Go to the documentation of this file.
#ifndef LIBBIGWIG_H
#define LIBBIGWIG_H
#include "bigWigIO.h"
#include "bwValues.h"
#include <inttypes.h>
#include <zlib.h>
#ifdef __cplusplus
extern "C" {
#endif
#define LIBBIGWIG_VERSION 0.4.8
#ifdef NOCURL
#define LIBBIGWIG_CURL 0
#ifndef CURLTYPE_DEFINED
#define CURLTYPE_DEFINED
typedef int CURLcode;
typedef void CURL;
#endif
#else
#define LIBBIGWIG_CURL 1
#endif
#define BIGWIG_MAGIC 0x888FFC26
#define BIGBED_MAGIC 0x8789F2EB
#define CIRTREE_MAGIC 0x78ca8c91
#define IDX_MAGIC 0x2468ace0
#define DEFAULT_nCHILDREN 64
#define DEFAULT_BLOCKSIZE 32768
enum bwStatsType {
doesNotExist = -1,
mean = 0,
average = 0,
stdev = 1,
dev = 1,
max = 2,
min = 3,
cov = 4,
coverage = 4,
sum = 5
};
//Should hide this from end users
typedef struct {
uint32_t *level;
//There's 4 bytes of padding between these
uint64_t *dataOffset;
uint64_t *indexOffset;
bwRTree_t **idx;
} bwZoomHdr_t;
typedef struct {
uint16_t version;
uint16_t nLevels;
uint64_t ctOffset;
uint64_t dataOffset;
uint64_t indexOffset;
uint16_t fieldCount;
uint16_t definedFieldCount;
uint64_t sqlOffset;
uint64_t summaryOffset;
uint32_t bufSize;
uint64_t extensionOffset;
bwZoomHdr_t *zoomHdrs;
//total Summary
uint64_t nBasesCovered;
double minVal;
double maxVal;
double sumData;
double sumSquared;
} bigWigHdr_t;
//Should probably replace this with a hash
typedef struct {
int64_t nKeys;
char **chrom;
uint32_t *len;
} chromList_t;
//TODO remove from bigWig.h
typedef struct bwLL bwLL;
struct bwLL {
bwRTreeNode_t *node;
struct bwLL *next;
};
typedef struct bwZoomBuffer_t bwZoomBuffer_t;
struct bwZoomBuffer_t { //each individual entry takes 32 bytes
void *p;
uint32_t l, m;
struct bwZoomBuffer_t *next;
};
typedef struct {
uint64_t nBlocks;
uint32_t blockSize;
uint64_t nEntries;
uint64_t runningWidthSum;
uint32_t tid;
uint32_t start;
uint32_t end;
uint32_t span;
uint32_t step;
uint8_t ltype;
uint32_t l;
void *p;
bwLL *firstIndexNode;
bwLL *currentIndexNode;
bwZoomBuffer_t **firstZoomBuffer;
bwZoomBuffer_t **lastZoomBuffer;
uint64_t *nNodes;
uLongf compressPsz;
void *compressP;
} bwWriteBuffer_t;
typedef struct {
URL_t *URL;
bigWigHdr_t *hdr;
chromList_t *cl;
bwRTree_t *idx;
bwWriteBuffer_t *writeBuffer;
int isWrite;
int type;
} bigWigFile_t;
typedef struct {
uint32_t l;
uint32_t m;
uint32_t *start;
uint32_t *end;
float *value;
} bwOverlappingIntervals_t;
typedef struct {
uint32_t l;
uint32_t m;
uint32_t *start;
uint32_t *end;
char **str;
} bbOverlappingEntries_t;
typedef struct {
bigWigFile_t *bw;
uint32_t tid;
uint32_t start;
uint32_t end;
uint64_t offset;
uint32_t blocksPerIteration;
int withString;
void *blocks;
bwOverlappingIntervals_t *intervals;
bbOverlappingEntries_t *entries;
void *data;
} bwOverlapIterator_t;
int bwInit(size_t bufSize);
void bwCleanup(void);
int bwIsBigWig(const char *fname, CURLcode (*callBack)(CURL*));
int bbIsBigBed(const char *fname, CURLcode (*callBack)(CURL*));
bigWigFile_t *bwOpen(const char *fname, CURLcode (*callBack)(CURL*), const char* mode);
bigWigFile_t *bbOpen(const char *fname, CURLcode (*callBack)(CURL*));
char *bbGetSQL(bigWigFile_t *fp);
void bwClose(bigWigFile_t *fp);
/*******************************************************************************
*
* The following are in bwStats.c
*
*******************************************************************************/
uint32_t bwGetTid(const bigWigFile_t *fp, const char *chrom);
void bwDestroyOverlappingIntervals(bwOverlappingIntervals_t *o);
void bbDestroyOverlappingEntries(bbOverlappingEntries_t *o);
bwOverlappingIntervals_t *bwGetOverlappingIntervals(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end);
bbOverlappingEntries_t *bbGetOverlappingEntries(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end, int withString);
bwOverlapIterator_t *bwOverlappingIntervalsIterator(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end, uint32_t blocksPerIteration);
bwOverlapIterator_t *bbOverlappingEntriesIterator(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end, int withString, uint32_t blocksPerIteration);
bwOverlapIterator_t *bwIteratorNext(bwOverlapIterator_t *iter);
void bwIteratorDestroy(bwOverlapIterator_t *iter);
bwOverlappingIntervals_t *bwGetValues(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end, int includeNA);
double *bwStats(bigWigFile_t *fp, const 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
int bwCreateHdr(bigWigFile_t *fp, int32_t maxZooms);
chromList_t *bwCreateChromList(const char* const* chroms, const uint32_t *lengths, int64_t n);
int bwWriteHdr(bigWigFile_t *bw);
int bwAddIntervals(bigWigFile_t *fp, const char* const* chrom, const uint32_t *start, const uint32_t *end, const float *values, uint32_t n);
int bwAppendIntervals(bigWigFile_t *fp, const uint32_t *start, const uint32_t *end, const 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);
int bwAppendIntervalSpans(bigWigFile_t *fp, const uint32_t *start, const 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);
int bwAppendIntervalSpanSteps(bigWigFile_t *fp, const float *values, uint32_t n);
#ifdef __cplusplus
}
#endif
#endif // LIBBIGWIG_H
bigWigIO.h
bwCreateHdr
int bwCreateHdr(bigWigFile_t *fp, int32_t maxZooms)
Create a largely empty bigWig header Every bigWig file has a header, this creates the template for on...
Definition bwWrite.c:56
bbGetSQL
char * bbGetSQL(bigWigFile_t *fp)
Returns a string containing the SQL entry (or NULL). The "auto SQL" field contains the names and valu...
Definition bwRead.c:315
bbDestroyOverlappingEntries
void bbDestroyOverlappingEntries(bbOverlappingEntries_t *o)
Frees space allocated by bbGetOverlappingEntries.
Definition bwValues.c:331
bwGetValues
bwOverlappingIntervals_t * bwGetValues(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end, int includeNA)
Return all per-base bigWig values in a given interval. Given an interval (e.g., chr1:0-100),...
Definition bwValues.c:711
bwAppendIntervalSpanSteps
int bwAppendIntervalSpanSteps(bigWigFile_t *fp, const float *values, uint32_t n)
Append to a previous block of fixed-step entries. If you previously used bwAddIntervalSpanSteps(),...
Definition bwWrite.c:552
bwInit
int bwInit(size_t bufSize)
Initializes curl and global variables. This MUST be called before other functions (at least if you wa...
Definition bwRead.c:38
bbOpen
bigWigFile_t * bbOpen(const char *fname, CURLcode(*callBack)(CURL *))
Opens a local or remote bigBed file. This will open a local or remote bigBed file....
Definition bwRead.c:397
bwOpen
bigWigFile_t * bwOpen(const char *fname, CURLcode(*callBack)(CURL *), const char *mode)
Opens a local or remote bigWig file. This will open a local or remote bigWig file....
Definition bwRead.c:345
bbIsBigBed
int bbIsBigBed(const char *fname, CURLcode(*callBack)(CURL *))
Determine is a file is a bigBed file. This function will quickly check either local or remote files t...
Definition bwRead.c:332
bwDestroyOverlappingIntervals
void bwDestroyOverlappingIntervals(bwOverlappingIntervals_t *o)
Frees space allocated by bwGetOverlappingIntervals.
Definition bwValues.c:323
bwGetOverlappingIntervals
bwOverlappingIntervals_t * bwGetOverlappingIntervals(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end)
Return bigWig entries overlapping an interval. Find all bigWig entries overlapping a range and return...
Definition bwValues.c:561
bwAddIntervals
int bwAddIntervals(bigWigFile_t *fp, const char *const *chrom, const uint32_t *start, const uint32_t *end, const float *values, uint32_t n)
Write a new block of bedGraph-like intervals to a bigWig file Adds entries of the form: chromosome st...
Definition bwWrite.c:374
bwStats
double * bwStats(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end, uint32_t nBins, enum bwStatsType type)
Determines per-interval bigWig statistics Can determine mean/min/max/coverage/standard deviation of v...
Definition bwStats.c:530
bwGetTid
uint32_t bwGetTid(const bigWigFile_t *fp, const char *chrom)
Converts between chromosome name and ID.
Definition bwValues.c:282
bwIteratorDestroy
void bwIteratorDestroy(bwOverlapIterator_t *iter)
Destroys a bwOverlapIterator_t.
Definition bwValues.c:641
bwAppendIntervalSpans
int bwAppendIntervalSpans(bigWigFile_t *fp, const uint32_t *start, const float *values, uint32_t n)
Append to a previous block of variable-step entries. If you previously used bwAddIntervalSpans(),...
Definition bwWrite.c:495
bwStatsType
bwStatsType
Definition bigWig.h:103
coverage
@ coverage
Definition bigWig.h:112
min
@ min
Definition bigWig.h:110
sum
@ sum
Definition bigWig.h:113
doesNotExist
@ doesNotExist
Definition bigWig.h:104
max
@ max
Definition bigWig.h:109
mean
@ mean
Definition bigWig.h:105
dev
@ dev
Definition bigWig.h:108
stdev
@ stdev
Definition bigWig.h:107
average
@ average
Definition bigWig.h:106
cov
@ cov
Definition bigWig.h:111
bwCreateChromList
chromList_t * bwCreateChromList(const char *const *chroms, const uint32_t *lengths, int64_t n)
Take a list of chromosome names and lengths and return a pointer to a chromList_t This MUST be run be...
Definition bwWrite.c:22
bwStatsFromFull
double * bwStatsFromFull(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end, uint32_t nBins, enum bwStatsType type)
Determines per-interval bigWig statistics Can determine mean/min/max/coverage/standard deviation of v...
Definition bwStats.c:485
bwAddIntervalSpans
int bwAddIntervalSpans(bigWigFile_t *fp, const char *chrom, const uint32_t *start, uint32_t span, const float *values, uint32_t n)
Add a new block of variable-step entries to a bigWig file Adds entries for the form chromosome start ...
Definition bwWrite.c:462
bbGetOverlappingEntries
bbOverlappingEntries_t * bbGetOverlappingEntries(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end, int withString)
Return bigBed entries overlapping an interval. Find all bigBed entries overlapping a range and return...
Definition bwValues.c:573
bwAppendIntervals
int bwAppendIntervals(bigWigFile_t *fp, const uint32_t *start, const uint32_t *end, const float *values, uint32_t n)
Append bedGraph-like intervals to a previous block of bedGraph-like intervals in a bigWig file....
Definition bwWrite.c:434
bwIsBigWig
int bwIsBigWig(const char *fname, CURLcode(*callBack)(CURL *))
Determine if a file is a bigWig file. This function will quickly check either local or remote files t...
Definition bwRead.c:302
bwAddIntervalSpanSteps
int bwAddIntervalSpanSteps(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t span, uint32_t step, const float *values, uint32_t n)
Add a new block of fixed-step entries to a bigWig file Adds entries for the form value to the file....
Definition bwWrite.c:520
bbOverlappingEntriesIterator
bwOverlapIterator_t * bbOverlappingEntriesIterator(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end, int withString, uint32_t blocksPerIteration)
Creates an iterator over entries in a bigBed file Iterators can be traversed with bwIteratorNext() an...
Definition bwValues.c:613
bwClose
void bwClose(bigWigFile_t *fp)
Closes a bigWigFile_t and frees up allocated memory This closes both bigWig and bigBed files.
Definition bwRead.c:289
bwCleanup
void bwCleanup(void)
The counterpart to bwInit, this cleans up curl.
Definition bwRead.c:52
bwWriteHdr
int bwWriteHdr(bigWigFile_t *bw)
Write a the header to a bigWig file. You must have already opened the output file,...
Definition bwWrite.c:187
bwIteratorNext
bwOverlapIterator_t * bwIteratorNext(bwOverlapIterator_t *iter)
Traverses to the entries/intervals in the next group of blocks.
Definition bwValues.c:650
bwOverlappingIntervalsIterator
bwOverlapIterator_t * bwOverlappingIntervalsIterator(bigWigFile_t *fp, const char *chrom, uint32_t start, uint32_t end, uint32_t blocksPerIteration)
Creates an iterator over intervals in a bigWig file Iterators can be traversed with bwIteratorNext() ...
Definition bwValues.c:585
bwValues.h
URL_t
This structure holds the file pointers and buffers needed for raw access to local and remote files.
Definition bigWigIO.h:38
bbOverlappingEntries_t
Holds interval:str associations.
Definition bigWig.h:234
bbOverlappingEntries_t::m
uint32_t m
Definition bigWig.h:236
bbOverlappingEntries_t::str
char ** str
Definition bigWig.h:239
bbOverlappingEntries_t::start
uint32_t * start
Definition bigWig.h:237
bbOverlappingEntries_t::end
uint32_t * end
Definition bigWig.h:238
bbOverlappingEntries_t::l
uint32_t l
Definition bigWig.h:235
bigWigFile_t
A structure that holds everything needed to access a bigWig file.
Definition bigWig.h:210
bigWigFile_t::idx
bwRTree_t * idx
Definition bigWig.h:214
bigWigFile_t::isWrite
int isWrite
Definition bigWig.h:216
bigWigFile_t::type
int type
Definition bigWig.h:217
bigWigFile_t::URL
URL_t * URL
Definition bigWig.h:211
bigWigFile_t::writeBuffer
bwWriteBuffer_t * writeBuffer
Definition bigWig.h:215
bigWigFile_t::cl
chromList_t * cl
Definition bigWig.h:213
bigWigFile_t::hdr
bigWigHdr_t * hdr
Definition bigWig.h:212
bigWigHdr_t
The header section of a bigWig file.
Definition bigWig.h:135
bigWigHdr_t::zoomHdrs
bwZoomHdr_t * zoomHdrs
Definition bigWig.h:147
bigWigHdr_t::sumData
double sumData
Definition bigWig.h:152
bigWigHdr_t::extensionOffset
uint64_t extensionOffset
Definition bigWig.h:146
bigWigHdr_t::definedFieldCount
uint16_t definedFieldCount
Definition bigWig.h:142
bigWigHdr_t::sqlOffset
uint64_t sqlOffset
Definition bigWig.h:143
bigWigHdr_t::minVal
double minVal
Definition bigWig.h:150
bigWigHdr_t::fieldCount
uint16_t fieldCount
Definition bigWig.h:141
bigWigHdr_t::indexOffset
uint64_t indexOffset
Definition bigWig.h:140
bigWigHdr_t::ctOffset
uint64_t ctOffset
Definition bigWig.h:138
bigWigHdr_t::version
uint16_t version
Definition bigWig.h:136
bigWigHdr_t::dataOffset
uint64_t dataOffset
Definition bigWig.h:139
bigWigHdr_t::nBasesCovered
uint64_t nBasesCovered
Definition bigWig.h:149
bigWigHdr_t::nLevels
uint16_t nLevels
Definition bigWig.h:137
bigWigHdr_t::summaryOffset
uint64_t summaryOffset
Definition bigWig.h:144
bigWigHdr_t::sumSquared
double sumSquared
Definition bigWig.h:153
bigWigHdr_t::bufSize
uint32_t bufSize
Definition bigWig.h:145
bigWigHdr_t::maxVal
double maxVal
Definition bigWig.h:151
bwOverlapIterator_t
A structure to hold iterations One of intervals and entries should be used to access records from big...
Definition bigWig.h:246
bwOverlapIterator_t::offset
uint64_t offset
Definition bigWig.h:251
bwOverlapIterator_t::blocksPerIteration
uint32_t blocksPerIteration
Definition bigWig.h:252
bwOverlapIterator_t::entries
bbOverlappingEntries_t * entries
Definition bigWig.h:256
bwOverlapIterator_t::withString
int withString
Definition bigWig.h:253
bwOverlapIterator_t::intervals
bwOverlappingIntervals_t * intervals
Definition bigWig.h:255
bwOverlapIterator_t::data
void * data
Definition bigWig.h:257
bwOverlapIterator_t::bw
bigWigFile_t * bw
Definition bigWig.h:247
bwOverlapIterator_t::start
uint32_t start
Definition bigWig.h:249
bwOverlapIterator_t::tid
uint32_t tid
Definition bigWig.h:248
bwOverlapIterator_t::end
uint32_t end
Definition bigWig.h:250
bwOverlapIterator_t::blocks
void * blocks
Definition bigWig.h:254
bwOverlappingIntervals_t
Holds interval:value associations.
Definition bigWig.h:223
bwOverlappingIntervals_t::m
uint32_t m
Definition bigWig.h:225
bwOverlappingIntervals_t::value
float * value
Definition bigWig.h:228
bwOverlappingIntervals_t::start
uint32_t * start
Definition bigWig.h:226
bwOverlappingIntervals_t::l
uint32_t l
Definition bigWig.h:224
bwOverlappingIntervals_t::end
uint32_t * end
Definition bigWig.h:227
bwRTreeNode_t
A node within an R-tree holding the index for data.
Definition bwValues.h:20
bwRTree_t
Definition bwValues.h:39
bwWriteBuffer_t
This is only needed for writing bigWig files (and won't be created otherwise) This should be removed ...
Definition bigWig.h:185
bwWriteBuffer_t::lastZoomBuffer
bwZoomBuffer_t ** lastZoomBuffer
Definition bigWig.h:201
bwWriteBuffer_t::nEntries
uint64_t nEntries
Definition bigWig.h:188
bwWriteBuffer_t::compressPsz
uLongf compressPsz
Definition bigWig.h:203
bwWriteBuffer_t::tid
uint32_t tid
Definition bigWig.h:190
bwWriteBuffer_t::ltype
uint8_t ltype
Definition bigWig.h:195
bwWriteBuffer_t::span
uint32_t span
Definition bigWig.h:193
bwWriteBuffer_t::firstZoomBuffer
bwZoomBuffer_t ** firstZoomBuffer
Definition bigWig.h:200
bwWriteBuffer_t::firstIndexNode
bwLL * firstIndexNode
Definition bigWig.h:198
bwWriteBuffer_t::runningWidthSum
uint64_t runningWidthSum
Definition bigWig.h:189
bwWriteBuffer_t::blockSize
uint32_t blockSize
Definition bigWig.h:187
bwWriteBuffer_t::currentIndexNode
bwLL * currentIndexNode
Definition bigWig.h:199
bwWriteBuffer_t::nNodes
uint64_t * nNodes
Definition bigWig.h:202
bwWriteBuffer_t::l
uint32_t l
Definition bigWig.h:196
bwWriteBuffer_t::step
uint32_t step
Definition bigWig.h:194
bwWriteBuffer_t::start
uint32_t start
Definition bigWig.h:191
bwWriteBuffer_t::end
uint32_t end
Definition bigWig.h:192
bwWriteBuffer_t::p
void * p
Definition bigWig.h:197
bwWriteBuffer_t::nBlocks
uint64_t nBlocks
Definition bigWig.h:186
bwWriteBuffer_t::compressP
void * compressP
Definition bigWig.h:204
bwZoomHdr_t
BigWig files have multiple "zoom" levels, each of which has its own header. This hold those headers.
Definition bigWig.h:122
bwZoomHdr_t::dataOffset
uint64_t * dataOffset
Definition bigWig.h:125
bwZoomHdr_t::level
uint32_t * level
Definition bigWig.h:123
bwZoomHdr_t::idx
bwRTree_t ** idx
Definition bigWig.h:127
bwZoomHdr_t::indexOffset
uint64_t * indexOffset
Definition bigWig.h:126
chromList_t
Holds the chromosomes and their lengths.
Definition bigWig.h:160
chromList_t::chrom
char ** chrom
Definition bigWig.h:162
chromList_t::len
uint32_t * len
Definition bigWig.h:163
chromList_t::nKeys
int64_t nKeys
Definition bigWig.h:161