Skip to main content

Convert GenBank files to SQLite databases

Project description

gbk-to-sqlite

Convert GenBank files to SQLite databases with an intuitive, queryable schema.

Summary

gbk-to-sqlite is a Python tool designed to convert GenBank (.gbk or .gbk.gz) format files into SQLite databases. This enables easier programmatic access, efficient storage, and complex querying of genomic data.

Key features:

  • Fast and memory-efficient conversion of large GenBank files
  • Support for both regular and gzipped GenBank files
  • Comprehensive database schema that preserves all relevant GenBank information
  • Automatic handling of complex features including multi-value qualifiers
  • Clean Python API for programmatic usage
  • SQL query interface for powerful data analysis

Installation

Install with pip:

pip install gbk-to-sqlite

Or with uv:

uv pip install gbk-to-sqlite

Database Schema and Models

gbk-to-sqlite uses a relational model to represent GenBank data:

Genome

Represents a GenBank file:

  • id: Unique identifier (auto-generated)
  • gbk_path: Path to the source GenBank file

Record

Represents a sequence record in the GenBank file:

  • id: Unique identifier (auto-generated)
  • genome: Foreign key reference to the Genome
  • name: The LOCUS name
  • definition: The DEFINITION field (sequence description)
  • accession: The ACCESSION number
  • version: The VERSION identifier

Feature

Represents a feature annotation (gene, CDS, etc.):

  • genome, record, feature_index: Composite primary key
  • location_start: Start position (0-based)
  • location_end: End position
  • location_strand: Strand orientation ('+', '-', or null)

Qualifier

Represents a feature's qualifier (e.g., /gene="xyz"):

  • genome, record, feature_index: References to the associated Feature
  • key: Qualifier name (e.g., "gene")
  • value: Qualifier value (e.g., "xyz"), can be null for flag qualifiers

Examples

Command-line Usage

Convert a single GenBank file:

gbk-to-sqlite --genbank-files sequence.gbk --sqlite-db output.sqlite

Convert multiple files:

gbk-to-sqlite --genbank-files file1.gbk file2.gbk.gz --sqlite-db output.sqlite

Use glob patterns to convert many files:

gbk-to-sqlite --genbank-glob "data/*.gbk.gz" --sqlite-db output.sqlite

Python API Usage

from gbk_to_sqlite import convert_gbk_to_sqlite, db, Genome, Record, Feature, Qualifier

# Initialize the database
db.init("output.sqlite")
db.connect()
db.create_tables([Genome, Record, Feature, Qualifier])

# Convert GenBank files
with db.atomic():
    convert_gbk_to_sqlite("sequence.gbk")

# Query the database
records = Record.select().where(Record.accession == "NC_000001")
for record in records:
    print(f"Record: {record.name}, Definition: {record.definition}")

    # Find all genes
    genes = Feature.select().join(Qualifier).where(
        (Feature.record == record) &
        (Qualifier.key == "gene")
    )

    for gene in genes:
        gene_name = Qualifier.select().where(
            (Qualifier.feature == gene) &
            (Qualifier.key == "gene")
        ).first().value

        print(f"Gene: {gene_name}, Location: {gene.location_start}..{gene.location_end}")

# Close the database connection
db.close()

Known Limitations

  • Complex locations (join, order, complement) are stored with a simplified representation
  • Join locations may not preserve strand information
  • Some advanced GenBank features might not be fully supported

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gbk_to_sqlite-0.1.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gbk_to_sqlite-0.1.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file gbk_to_sqlite-0.1.0.tar.gz.

File metadata

  • Download URL: gbk_to_sqlite-0.1.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for gbk_to_sqlite-0.1.0.tar.gz
Algorithm Hash digest
SHA256 db90b150ca1d2f310153e83f48d2713280ab17d8bde26ddf5b2a30ca17a025d1
MD5 4999245dd1bf2634beb11ab43b11e92a
BLAKE2b-256 02b3c73ed395164015133455c4049bc6b99f9697035357db979f45d99df04420

See more details on using hashes here.

File details

Details for the file gbk_to_sqlite-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: gbk_to_sqlite-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for gbk_to_sqlite-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9addb0de7c3778f77a68fa057eb11326309659c256c04c0ddbcbc658908cfa41
MD5 c7af7172c573f6e71706173059e6a63a
BLAKE2b-256 3a8ba378d677cad3dacec6a31edf43b5f987753a6839c5a71de8309a6f828d79

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page