Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@gmod/bbi

GMOD2.2kMIT6.0.1TypeScript support: included

Parser for BigWig/BigBed files

bionode, biojs, bigwig, bigbed, ucsc, genomics

readme

bbi-js

NPM version Coverage Status Build Status

A parser for bigwig and bigbed file formats

Usage

If using locally

const { BigWig } = require('@gmod/bbi')
const file = new BigWig({
  path: 'volvox.bw',
})
;(async () => {
  await file.getHeader()
  const feats = await file.getFeatures('chr1', 0, 100, { scale: 1 })
})()

If using remotely, you can use it in combination with generic-filehandle2 or your own implementation of something like generic-filehandle2 https://github.com/GMOD/generic-filehandle2/

const { BigWig } = require('@gmod/bbi')
const { RemoteFile } = require('generic-filehandle2')

// if running in the browser or newer versions of node.js, RemoteFile will use
// the the global fetch
const file = new BigWig({
  filehandle: new RemoteFile('volvox.bw'),
})

// old versions of node.js without a global fetch, supply custom fetch function
const fetch = require('node-fetch')
const file = new BigWig({
  filehandle: new RemoteFile('volvox.bw', { fetch }),
})

;(async () => {
  await file.getHeader()
  const feats = await file.getFeatures('chr1', 0, 100, { scale: 1 })
})()

Documentation

BigWig/BigBed constructors

Accepts an object containing either

  • path - path to a local file
  • url - path to a url
  • filehandle - a filehandle instance that you can implement as a custom class yourself. path and url are based on https://www.npmjs.com/package/generic-filehandle2 but by implementing a class containing the Filehandle interface specified therein, you can pass it to this module

BigWig

getFeatures(refName, start, end, opts)

  • refName - a name of a chromosome in the file
  • start - a 0-based half open start coordinate
  • end - a 0-based half open end coordinate
  • opts.scale - indicates zoom level to use, specified as pxPerBp, e.g. being zoomed out, you might have 100bp per pixel so opts.scale would be 1/100. the zoom level that is returned is the one which has reductionLevel<=2/opts.scale (reductionLevel is a property of the zoom level structure in the bigwig file data)
  • opts.basesPerScale - optional, inverse of opts.scale e.g. bpPerPx
  • opts.signal - optional, an AbortSignal to halt processing

Returns a promise to an array of features. If an incorrect refName or no features are found the result is an empty array.

Example:

const feats = await bigwig.getFeatures('chr1', 0, 100)
// returns array of features with start, end, score
// coordinates on returned data are are 0-based half open
// no conversion to 1-based as in wig is done)
// note refseq is not returned on the object, it is clearly chr1 from the query though

Understanding scale and reductionLevel

Here is what the reductionLevel structure looks like in a file. The zoomLevel that is chosen is the first reductionLevel<2*opts.basesPerScale (or reductionLevel<2/opts.scale) when scanning backwards through this list

  [ { reductionLevel: 40, ... },
    { reductionLevel: 160, ... },
    { reductionLevel: 640, ... },
    { reductionLevel: 2560, ... },
    { reductionLevel: 10240, ... },
    { reductionLevel: 40960, ... },
    { reductionLevel: 163840, ... } ]

getFeatureStream(refName, start, end, opts)

Same as getFeatures but returns an RxJS observable stream, useful for very large queries

const observer = await bigwig.getFeatureStream('chr1', 0, 100)
observer.subscribe(
  chunk => {
    /* chunk contains array of features with start, end, score */
  },
  error => {
    /* process error */
  },
  () => {
    /* completed */
  },
)

BigBed

getFeatures(refName, start, end, opts)

  • refName - a name of a chromosome in the file
  • start - a 0-based half open start coordinate
  • end - a 0-based half open end coordinate
  • opts.signal - optional, an AbortSignal to halt processing

returns a promise to an array of features. no concept of zoom levels is used with bigwig data

getFeatureStream(refName, start, end, opts)

Similar to BigWig, returns an RxJS observable for a observable stream

searchExtraIndex(name, opts)

Specific, to bigbed files, this method searches the bigBed "extra indexes", there can be multiple indexes e.g. for the gene ID and gene name columns. See the usage of -extraIndex in bedToBigBed here https://genome.ucsc.edu/goldenpath/help/bigBed.html

This function accepts two arguments

  • name: a string to search for in the BigBed extra indices
  • opts: an object that can optionally contain opts.signal, an abort signal

Returns a Promise to an array of Features, with an extra field indicating the field that was matched

How to parse BigBed results

The BigBed line contents are returned as a raw text line e.g. {start: 0, end:100, rest: "ENST00000456328.2\t1000\t..."} where "rest" contains tab delimited text for the fields from 4 and on in the BED format. Since BED files from BigBed format often come with autoSql (a description of all the columns) it can be useful to parse it with BED parser that can handle autoSql. The rest line can be parsed by the @gmod/bed module, which is not by default integrated with this module, but can be combined with it as follows

import { BigBed } from '@gmod/bbi'
import BED from '@gmod/bed'

const ti = new BigBed({
  filehandle: new LocalFile(require.resolve('./data/hg18.bb')),
})
const { autoSql } = await ti.getHeader()
const feats = await ti.getFeatures('chr7', 0, 100000)
const parser = new BED({ autoSql })
const lines = feats.map(f => {
  const { start, end, rest, uniqueId } = f
  return parser.parseLine(`chr7\t${start}\t${end}\t${rest}`, { uniqueId })
})
// @gmod/bbi returns features with {uniqueId, start, end, rest}
// we reconstitute this as a line for @gmod/bed with a template string
// note: the uniqueId is based on file offsets and helps to deduplicate exact feature copies if they exist

Features before parsing with @gmod/bed:

{
  "chromId": 0,
  "start": 64068,
  "end": 64107,
  "rest": "uc003sil.1\t0\t-\t64068\t64068\t255,0,0\t.\tDQ584609",
  "uniqueId": "bb-171"
}

Features after parsing with @gmod/bed:

{
  "uniqueId": "bb-0",
  "chrom": "chr7",
  "chromStart": 54028,
  "chromEnd": 73584,
  "name": "uc003sii.2",
  "score": 0,
  "strand": -1,
  "thickStart": 54028,
  "thickEnd": 54028,
  "reserved": "255,0,0",
  "spID": "AL137655"
}

Academic Use

This package was written with funding from the NHGRI as part of the JBrowse project. If you use it in an academic project that you publish, please cite the most recent JBrowse paper, which will be linked from jbrowse.org.

License

MIT © Colin Diesh

changelog

6.0.1 (2024-12-12)

6.0.0 (2024-12-12)

5.0.2 (2024-09-03)

5.0.1 (2024-08-09)

5.0.0 (2024-08-09)

4.0.6 (2024-07-23)

  • Use renamed abortable-promise-cache -> @gmod/abortable-promise-cache

4.0.5 (2024-06-19)

  • Improved linting

4.0.4 (2024-3-5)

  • Fix issue fetching data from file where refNames are not sorted (#59)

4.0.3 (2024-01-16)

Performance Improvements

  • optimize parseBigBedBlock (#58) (eb3f7a4)

  • Small perf improvement (#58)

4.0.2 (2023-07-30)

  • Fix issue with fetching headers that are large in certain cases

4.0.1 (2023-07-13)

  • Fix eslint plugin being in dependencies Accidentally

4.0.0 (2023-05-05)

  • Improve typescripting, refactoring
  • Options argument only accepts {signal}, not just signal now

3.0.1 (2023-04-21)

Features

  • explicit buffer import (#53) (2699c98)

  • Add explicit Buffer import

3.0.0 (2023-01-11)

  • Update to rxjs 7

2.0.5 (2022-12-17)

  • Cleanup package.json and README

2.0.4 (2022-10-15)

  • Use plain TextDecoder for decoding autoSql

2.0.3 (2022-10-10)

  • Add BigInt64 polyfill for older safari

2.0.2 (2022-07-18)

  • Make basesCovered a number instead of BigInt

2.0.1 (2022-07-18)

  • Bump generic-filehandle 2->3

2.0.0 (2022-07-18)

  • Use binary-parser instead of @gmod/binary-parser, with some optimizations. This uses BigInt and TextDecoder, so requires a major version bump

1.0.35 (2022-04-22)

  • Produce actual ESM module for the "module" field in package.json, was commonJS
  • Add sourceMap:true to tsconfig

1.0.34 (2022-03-11)

Reverts

1.0.33 (2022-02-25)

1.0.32 (2022-02-16)

  • Use pako to decode on command line instead of using zlib to avoid need to manually polyfill zlib

1.0.31 (2021-12-14)

  • Add esm module builds with less babelification for smaller bundle sizes

1.0.30 (2020-06-25)

  • Use abortable-promise-cache instead of abortable-memoize
  • Allow opts parameter to getHeader instead of just abortsignal

1.0.29 (2020-01-28)

  • Accidentally made the package include itself as dependency in 1.0.28, republish

1.0.28 (2020-01-28)

  • Change typescript interface to use object keys instead of Map type for refsByName, refsById
  • Typescript only release change

1.0.27 (2020-01-10)

  • Reduce number of requests needed on initial header
  • Add definedFieldCount to the returned Header

Thanks to @skinner for both of these contributions!

1.0.26 (2019-11-10)

  • Fix important bug with fixed step bigwig files not using the proper start coordinate

1.0.25 (2019-11-03)

  • Add fix for files with a large header, if autoSql is large in a bigbed file would be likely scenario

1.0.24 (2019-10-29)

  • Fix the uniqueIds generated via the bigbed features

1.0.23 (2019-10-06)

  • Small refactor of filehandle.read() to make it more robust

1.0.22 (2019-06-13)

  • Bump generic-filehandle
  • Add more checks for abort signal to allow early bailing

1.0.21 (2019-05-09)

  • Add fix for reading files with greater than 256 contigs

1.0.20 (2019-05-06)

  • Add fix that prevented accessing the lowest zoom/reduction level

1.0.19 (2019-05-02)

  • Add regression fix since 1.0.16 for uncompressed files. Thanks to @lidaof for reporting!

1.0.18 (2019-05-02)

  • Improve error handling of the observables (issue #20, pull #21)
  • Bump generic-filehandle to 1.0.9 to fix compatibility with native browser fetch

1.0.17 (2019-04-30)

  • Use some standard rxjs notions for combining operator results
  • Add parsing of the extraIndex data in BigBed, allowing you to call bigbed.searchExtraIndex(name[,opts])

1.0.16 (2019-04-23)

  • Pre-compile binary-parser instances for faster
  • Important: fixed bug that caused bigwig summary blocks to not be returned in output

1.0.15 (2019-04-18)

  • Make important performance improvement for BigWig data

1.0.14 (2019-04-17)

  • Improve documentation for integration with @gmod/bed@2
  • Fix some cases where abortSignal was passed incorrectly to filehandle

1.0.13 (2019-04-14)

  • Added uniqueId to objects returned from BigBed to avoid issue with duplicates

1.0.12 (2019-04-12)

  • Fix returning bigbed objects on empty regions

1.0.11 (2019-04-10)

  • Removed polyfill of Array.prototype.flat which modifies global scope

1.0.10 (2019-04-09)

  • Fix misinterpretation of variable step wig files in this module (the span is not variable in variable step files, only the step, use bedGraphToBigWig for variable span)
  • Improved docs

1.0.9 (2019-04-05)

  • Added caching of networking requests (thanks @rbuels for the abortable-promise-cache module!)
  • Fix some type errors on the range class
  • Correct using span on fixed size wiggle types

1.0.8 (2019-04-01)

  • Fix @babel/runtime in deployed package
  • Bugfix to the url argument to the BigWig/BigBed

1.0.7 (2019-04-01)

  • Added getFeatureStream which returns an Observable from rxjs
  • Added url option to BigWig and BigBed constructors to allow usage of RemoteFile filehandle
  • Added typescript backend for better processing

1.0.6 (2019-03-15)

  • Fix issue with fixed step and variable step bigwig files not working at all

1.0.5 (2019-03-07)

  • Fix issue with jest being in deps instead of devDeps

1.0.4 (2019-01-28)

  • Add renameRefSeqs functionality where you can apply a callback to the refseq names
  • Consistently apply start/end coordinate filters at different zoom levels

1.0.3 (2019-01-27)

  • Fix issue with properly inflating chunks (issue #1)

1.0.2 (2019-01-24)

  • Added regenerator-runtime to babel dist compilation

1.0.1 (2019-01-24)

  • Added exports for BigWig and BigBed. const {BigWig, BigBed} = require('@gmod/bbi')

1.0.0 (2019-01-23)

  • Initial version
  • Has support for bigwig and bigbed files