Skip to main content

Build Status

py2bit

A python extension, written in C, for quick access to 2bit files. The extension uses lib2bit for file access.

Table of Contents

Installation

You can install the extension directly from github with:

pip install git+https://github.com/dpryan79/py2bit

Usage

Basic usage is as follows:

Load the extension

>>> import py2bit

Open a 2bit file

This will work if your working directory is the py2bit source code directory.

>>> tb = py2bit.open("test/foo.2bit")

Note that if you would like to include information about soft-masked bases, you need to manually specify that:

>>> tb = py2bit.open("test/foo.2bit", True)

Access the list of chromosomes and the lengths

TwoBit objects contain a dictionary holding the chromosome/contig lengths, which can be accessed with the chroms() method.

>>> tb.chroms()
{'chr1': 150L, 'chr2': 100L}

You can directly access a particular chromosome by specifying its name.

>>> tb.chroms('chr1')
150L

The lengths are stored as a "long" integer type, which is why there's an L suffix. If you specify a nonexistent chromosome then nothing is output.

>>> tb.chroms("foo")
>>>

Print file information

The following information about and contained within a 2bit file can be accessed with the info() method:

  • file size, in bytes (file size)
  • number of chromosomes/contigs (nChroms)
  • total sequence length, in bases (sequence length)
  • total number of hard-masked (N) bases (hard-masked length)
  • total number of soft-masked (lower case) bases(soft-masked length).

Note that soft-masked length will only be present if open("file.2bit", True) is used, since handling soft-masking increases memory requirements and decreases perfomance.

>>> tb.info()
{'file size': 161, 'nChroms': 2, 'sequence length': 250, 'hard-masked length': 150, 'soft-masked length': 8}

Fetch a sequence

The sequence of a full or partial chromosome/contig can be fetched with the sequence() method.

>>> tb.sequence("chr1")
'NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNACGTACGTACGTagctagctGATCGATCGTAGCTAGCTAGCTAGCTGATCNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN'

By default, the whole chromosome/contig is returned. A specific range can also be requested.

>>> tb.sequence("chr1", 24, 74)
NNNNNNNNNNNNNNNNNNNNNNNNNNACGTACGTACGTagctagctGATC

The first number is the (0-based) position on the chromosome/contig where the sequence should begin. The second number is the (1-based) position on the chromosome where the sequence should end.

If it was requested during file opening that soft-masking information be stored, then lower case bases may be present. If a nonexistent chromosome/contig is specified then a runtime error occurs.

Fetch per-base statistics

It's often required to compute the percentage of 1 or more bases in a chromosome. This can be done with the bases() method.

>>> tb.bases("chr1")
{'A': 0.08, 'C': 0.08, 'T': 0.08666666666666667, 'G': 0.08666666666666667}

This returns a dictionary with bases as keys and the fraction of the sequence composed of them as values. Note that this will not sum to 1 if there are any hard-masked bases (the chromosome is 2/3 N in this case). One can also request this information over a particular region.

>>> tb.bases("chr1", 24, 74)
{'A': 0.12, 'C': 0.12, 'T': 0.12, 'G': 0.12}

The start and end position are as with the sequence() method described above.

If integer counts are preferred, then they can instead be returned.

>>> tb.bases("chr1", 24, 74, False)
{'A': 6, 'C': 6, 'T': 6, 'G': 6}

Fetch masked blocks

There are two kinds of masking blocks that can be present in 2bit files: hard-masked and soft-masked. Hard-masked blocks are stretches of NNNN, as are commonly found near telomeres and centromeres. Soft-masked blocks are runs of lowercase A/C/T/G, typically indicating repeat elements or low-complexity stretches. In can sometimes be useful to query this information from 2bit files:

>>> tb.hardMaskedBlocks("chr1")
[(0, 50), (100, 150)]

In this (small) example, there are two stretches of hard-masked sequence, from 0 to 50 and again from 100 to 150 (see the note below about coordinates). If you would instead like to query all blocks overlapping with a specific region, you can specify the region bounds:

>>> tb.hardMaskedBlocks("chr1", 75, 101)
[(100, 150)]

If there are no overlapping regions, then an empty list is returned:

>>> tb.hardMaskedBlocks("chr1", 75, 100)
[]

Instead of hardMaskedBlocks(), one can use softMaskedBlocks() in an identical manner:

>>> tb = py2bit.open("foo.2bit", storeMasked=True)
>>> tb.softMaskedBlocks("chr1")
[(62, 70)]

As shown, you must specify storeMasked=True or you will receive a run time error.

Close a file

A TwoBit object can be closed with the close() method.

>>> tb.close()

A note on coordinates

0-based half-open coordinates are used by this python module. So to access the value for the first base on chr1, one would specify the starting position as 0 and the end position as 1. Similarly, bases 100 to 115 would have a start of 99 and an end of 115. This is simply for the sake of consistency with most other bioinformatics packages.

Metadata

Release files for py2bit 1.0.2

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

Source distribution (sdist)

Source distribution for py2bit 1.0.2
File Size Uploaded Provenance Actions
py2bit-1.0.2.tar.gz 18.8 kB Without provenance Details for py2bit-1.0.2.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for py2bit 1.0.2
File Actions
py2bit-1.0.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.17+ x86-64 Details for py2bit-1.0.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
py2bit-1.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.17+ x86-64 Details for py2bit-1.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
py2bit-1.0.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.17+ x86-64 Details for py2bit-1.0.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
py2bit-1.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.17+ x86-64 Details for py2bit-1.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
py2bit-1.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.17+ x86-64 Details for py2bit-1.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Total release size: 269.7 kB

Release files / py2bit-1.0.2.tar.gz

Download URL py2bit-1.0.2.tar.gz
Size 18.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a9ebaf734a459ff229d6ac10999a068d4826ecd18adf1a698a118b9b69278484
BLAKE2b-256 checksum
How to use checksums
f7290e51573db9a9ba86f871c5e689e94a1134b1d74fcecab01972e753b5c2ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.22

Release files / py2bit-1.0.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL py2bit-1.0.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 50.8 kB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
a9d0095714c6ee62427e9f3d9430fd015e36b8590b670645af911f3d342608be
BLAKE2b-256 checksum
How to use checksums
de0ff1e7091be665c25debfabaa60f97236bf2b017a53a200c047f73927c4138
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.22

Release files / py2bit-1.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL py2bit-1.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 50.9 kB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
b1d07e49f9372bbb5628e5ccc5041cbb2f4896610d740051b9729a9930618cde
BLAKE2b-256 checksum
How to use checksums
a04baf2dfec078767708aa27a2639df15c9f76bb8f1ffe83bbb08788d1f819ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.22

Release files / py2bit-1.0.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL py2bit-1.0.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 49.8 kB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
670a8f3e1384a0a2c48f54f4642293e8090857ce4ad1af9ffd5c19cbefcd5833
BLAKE2b-256 checksum
How to use checksums
5e86a29c8709a07a62fc32587efc84e9af534528dd181cfafe05d9d4c58016e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.22

Release files / py2bit-1.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL py2bit-1.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 49.8 kB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
e4433b131f75866ff2ee2c1a6db247ca90dac37b50498acd1ba63b7eeaa1d403
BLAKE2b-256 checksum
How to use checksums
81a0311be1bd6972a3ee15d6a3bedd27f3d3587d74208bb9b3288a2eae633dca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.22

Release files / py2bit-1.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL py2bit-1.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 49.6 kB
Tags CPython 3.9 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
3b016074b666732782d31e1010bfa5995d94cccdb3ac40653c83ff972c4e25e9
BLAKE2b-256 checksum
How to use checksums
4d12ca4bb40c4ce8784139c33cf8c043c847a74812bd941574de32e1270f3f4d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.22

Security for py2bit 1.0.2

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