bx-python

0.14.0 · active · verified Thu Apr 16

bx-python is a collection of Python tools designed for manipulating biological data, with a particular focus on operations involving multiple sequence alignments and genomic intervals. The library is currently at version 0.14.0 and follows an active release cadence, frequently updating to support newer Python versions and improve compatibility with other scientific libraries like NumPy.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to interact with `bx-python` for common bioinformatics tasks. It shows how to use `TwoBitFile` to access genomic sequences (note: a valid .2bit file is required for this part to fully run) and how to construct and write a simple MAF (Multiple Alignment Format) block to standard output using `MAFWriter`.

from bx.seq.twobit import TwoBitFile
import os

# This is a placeholder for a real .2bit file
# In a real scenario, you'd open an existing file.
# For demonstration, we'll simulate a file path.
twobit_file_path = os.environ.get('BX_TWOBIT_FILE', 'example.2bit')

# Create a dummy .2bit file for demonstration if it doesn't exist
# This part would typically not be in a quickstart but is here for runnability.
if not os.path.exists(twobit_file_path):
    try:
        # TwoBitFile constructor expects a file-like object or a path to an existing file
        # For a runnable example, we would need to *create* a valid .2bit file or skip this section.
        # Given the complexity, let's illustrate reading an assumed existing file.
        # This part of the quickstart is illustrative rather than fully runnable without a .2bit file.
        print(f"Please provide a valid .2bit file at {twobit_file_path} to run this example.")
        print("Example: Download one from UCSC genome browser, e.g., 'hg38.2bit'")
        # Example of how you would *use* it if the file exists:
        # with open(twobit_file_path, 'rb') as f:
        #     tb = TwoBitFile(f)
        #     print(f"Sequence 'chr1' length: {tb['chr1'].length}")
        #     print(f"Sequence 'chr1' first 10 bases: {tb['chr1'].get(0, 10)}")

    except Exception as e:
        print(f"Could not create or open dummy .2bit file for quickstart: {e}")
        print("Skipping TwoBitFile quickstart example.")

# Example of using MAF (Multiple Alignment Format) writer
from bx.align.maf import MAFWriter, MAFAlignment, MAFBlock, MAFComponent
import sys

print("\nDemonstrating MAFWriter:")
writer = MAFWriter(sys.stdout)

alignment = MAFAlignment()
block = MAFBlock()

# Create components for a block
comp1 = MAFComponent()
comp1.src = 'hg38.chr1'
comp1.start = 100
comp1.size = 10
comp1.strand = '+'
comp1.srcSize = 248956422
comp1.text = 'ATGCATGCAT'

comp2 = MAFComponent()
comp2.src = 'mm10.chrX'
comp2.start = 50
comp2.size = 10
comp2.strand = '-'
comp2.srcSize = 171031299
comp2.text = 'ATGCATGCAT'

block.components.append(comp1)
block.components.append(comp2)
alignment.append(block)

writer.write(alignment)
print("MAF example written to stdout.")

view raw JSON →