Skip to main content

Python Bed Lookup

Allows very fast searching of a bed file of any size by gene/snp location.

For example:

from bed_lookup import BedFile
b = BedFile('my_bed.bed')
gene = b.lookup('chr3', 1000104)

This module requires cython, and should work with recent versions of python2 and python3.

It can also be used with a pandas dataframe directly:

df['new_col'] = b.lookup_df(df, 'chrom', 'pos')

Note: with large dataframes, this function can be very slow, but there is a nice trick to speed it up:

import numpy as np
import pandas as pd
from multiprocessing import Pool, cpu_count
pool = Pool()
b    = BedFile('my_bed.bed')
df   = pd.read_csv('big_table.txt.gz', sep='\t', compression='gzip')
dfs  = np.array_split(df, cpu_count())
run  = []
out  = []
# Our chromsome column is 'chrom' and position column is 'pos'.
for d in dfs:
    run.append(pool.apply_async(b.lookup_df, (d, 'chrom', 'pos')))
for r in run:
    out.append(r.get())
df['new_col'] = pd.concat(out)

Installation

Installation follows the standard python syntax:

git clone https://github.com/MikeDacre/python_bed_lookup
cd python_bed_lookup
python setup.py build
sudo python setup.py install

If you do not have root permission on you device, replace the last line with:

python setup.py install --user

Running from the command line

There is a command line script called bed_location_lookup that will be installed in /usr/bin if you install globally or in ~/.local/usr/bin if you install for your user only. The sytax for that script is:

bed_location_lookup <bed_file> chr1_1000134 chr2_1859323 ....

It will work for any number of gene coordinate arguments. Be aware, that there is a file opening delay when the script is run (for small bed files this will be very small, but for large files it can be a few seconds). It is therefore much more efficient to call a single instance of bed_location_lookup with a long list of coordinates than it is to call it once per coordinate. For a large number of coordinates this difference can be substantial.

bed_location_lookup has a few other options also, to get those run:

bed_location_lookup -h

Note: if you know the bed file is large and a database already exists, you can get considerable speed up by passing the database file instead of the raw bed file. e.g. pass bedfile.bed.db instead of bedfile.bed. This bypasses the file length check.

Backend information and customization

It makes use of a cython optimized dictionary lookup for small bed files and sqlite for larger bed files. Which backend is being used is transparent to the user, simply use the lookup() function as demonstrated in the example above. The default file size cutoff is ~5 million lines in the bed file, which results in a memory use of 1.2GB for a 5 million line long file. The memory use scales linearly, so setting the limit at 1 million lines will result in about 240MB of memory use. To change the file size cutoff edit the _max_len variable in bed_lookup/__init__.py. Be aware that the file size limit is actually measured in bytes, for speed purposes. A dictionary of size to file length maps is provided in the __init__.py file, the default should work fine on most systems.

Note that the sqlite backed is very slightly slower for lookups, however the sqlite backend requires that a database exists already. If one does not exist (the expected name is the bed file name followed by a .db) already then one is created, and this step can be very slow. Hypothetically this should only be done once.

As noted above, when creating a BedFile object, a file length lookup is performed. This lookup can be costly, particularly for gzipped files. To skip this step, simply pass the database file to BedFile(), instead of the bedfile itself.

Note: this code will work with either plain text or gzipped files, gzipped files will be slightly slower at load due to the overhead of decompression. For large files where an sqlite database already exists, there will be only a very slight delay relative to the uncompressed bed file (due to file length counting).

As the BedFile object is only generated once, any lookups after the creation of this object will be very fast (less than a second) for any length of bed file. Smaller files will obvious result in even quicker lookups.

Metadata

Release files for bed-lookup 1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bed-lookup 1.5
File Size Uploaded Provenance
bed_lookup-1.5.tar.gz 86.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for bed-lookup 1.5
File Interpreter ABI Platform
bed_lookup-1.5-cp39-cp39-macosx_11_0_arm64.whl CPython 3.9 CPython 3.9 macOS 11.0+ ARM64 Details

Total release size: 227.7 kB

Release files / bed_lookup-1.5.tar.gz

Download URL bed_lookup-1.5.tar.gz
Size 86.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b445a63fc5e431fdf262f5d9e64e18d780acc7cd6dc4912e85c7aae43a779a38
BLAKE2b-256 checksum
How to use checksums
be1cc5653952c2a0e1fe664fccde2240867f101d09945c3f1d9d4e6ced416b9e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.8

Release files / bed_lookup-1.5-cp39-cp39-macosx_11_0_arm64.whl

Download URL bed_lookup-1.5-cp39-cp39-macosx_11_0_arm64.whl
Size 141.5 kB
Tags CPython 3.9 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
258d5e0f57f9d40448e115e1edab107a10ff018b13e77e188bfeffeb52649042
BLAKE2b-256 checksum
How to use checksums
56cca1f0e679350f691564a515fde135e8a68da33a728d1d79979e1747ab40ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.8

Security for bed-lookup 1.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page