Metadata-Version: 2.4
Name: dbtk-dnabert
Version: 1.2.2
Summary: A general DNABERT implementation using the deepbio-toolkit.
Author-email: "David W. Ludwig II" <davidludwigii@gmail.com>
License: MIT License
        
        Copyright (c) 2024 DLii-Research
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/DLii-Research/dbtk-dnabert
Project-URL: Bug Tracker, https://github.com/DLii-Research/dbtk-dnabert/issues
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: deepbio-toolkit>=0.4.3
Requires-Dist: lightning
Dynamic: license-file

# dbtk-dnabert

An implementation of [DNABERT](https://academic.oup.com/bioinformatics/article/37/15/2112/6128680) using Pytorch and the [deepbio-toolkit](https://pypi.org/project/deepbio-toolkit/) library.

## Getting Started

1. Install the dbtk-dnabert package
```bash
pip install dbtk-dnabert
```

2. Pull pre-trained DNABERT model
```py
from dnabert import DnaBert

# Load the pre-trained model
model = DnaBert.from_pretrained("SirDavidLudwig/dnabert", revision="64d-silva16s-250bp")
```

## Examples

Embed DNA sequences
```py
# Sequences to embed
sequences = [
    "ACTGAATGAGAC",
    "TTGAGTAGCCAA"
]

# Tokenize sequences
sequence_tokens = torch.tensor([model.tokenizer(sequence) for sequence in sequences])

# Embed sequences
output = model(sequence_tokens)

# Sequence-level embeddings from class token
embeddings = output["class"]

# Sequence-level embeddings from averaged tokens
embeddings = output["tokens"].mean(dim=1)
```

## Pre-trained Models

| Model Name | Embedding Dim. | Maximum Length | Pre-training Dataset |
| --- | --- | --- | --- |
| `64d-silva16s-250bp` | 64 | 250bp | Silva 16S |
| `768d-silva16s-250bp` | 768 | 250bp | Silva 16S |

## Development

### 1. Model Configuration

Template model configurations can be generated using the `dbtk model config` command.

### 2. Pre-training

The model can be pre-trained using the supplied configurations with the command:

```bash
dbtk model fit \
    -c ./configs/datamodules/pretrain_silva_16s_250bp.yaml \
    -c ./configs/models/pretrain_dnabert_768d_250bp.yaml \
    -c ./configs/trainers/pretrainer.yaml \
    ./logs/dnabert_768d_250bp
```

### 3. Exporting

The trained model can be exported to a Huggingface model with the following command.

```bash
dbtk model export ./logs/dnabert_768d_250bp/last.ckpt ./exports/dnabert_768d_250bp
```
