Skip to main content

Convert FASTQ and FASTA reads to binary PyTorch tensors!

Project description

nucleotorch

Convert FASTQ and FASTA reads to binary PyTorch tensors!

Nucleotorch is a lightweight, fast tool for converting raw NGS data in the form of FASTQ or FASTA files into numerical PyTorch tensors for genomic machine learning applications.

With the rise of models such as DNABERT, it's become clear that transformers have the capability to completely upend the field of genetics. More recently, encoder-only models such as InstaDeep's Nucleotide Transformer have started to appear. Models such as these have the capacity to learn realtionships between genes in an unsupervised manner, allowing for the use of unaligned NGS reads as input data.

One problem with using FASTQ/FASTA data, though, is its prohbitively large size. Nucleotorch solves this issue by converting the FASTQ/FASTA text sequences into packed bit array in C, and then turning this bitarray into a PyTorch tensor consisiting of unsigned integers. Not only does this do away with the memory-intensity and long runtimes that come with reading and parsing large text files in Python, it also allows for concise and frictionless generation of compact, unique input samples.

Nucleotorch requires Numpy and PyTorch. Nucleotorch only accepts valid FASTQ or FASTA files as arguments.

RELEASE NOTES:

Version 1.2.0 (2/16/2025) (Latest): perf improvements on unpacking algo, refactoring to isolate tokenization in anticipation of adding cuda kernels soon

Version 1.1.13 (2/14/2025): toml updates for venv

Version 1.1.11 (2/14/2025): linux support

Version 1.1.8 (2/13/2025): macOS intel support

Version 1.1.7 (2/13/2025): Distribution fixes

Version 1.1.0 (2/12/2025): FASTA and FASTQ support, new token_length parameter allows users to control the size of the tokens in the tensor

Version 1.0.3 (2/11/2025): FASTQ support

DOCUMENTATION

def process_fastq(filepath: str, token_length: int = 1) -> List[torch.Tensor]:
    """
    Processes FASTQ reads into PyTorch embedding vectors.

    Args:
        filepath (str): Path to the FASTQ file relative to the current working directory.
        token_length (int, optional): Number of desired nucleotides per token (ie, entry in the tensor). If not 
        specified, the bitarray is divided into uint8 chunks by default.

    Returns:
        List[torch.Tensor]: A list of PyTorch tensors, with each tensor being composed of uints (type of uint depends on token_length).
    """

Example usage:

import nucleotorch
tensors = nucleotorch.process_fastq("path/to/example/FASTQ/file", 6)
def process_fasta(filepath: str, token_length: int = 1) -> List[torch.Tensor]:
    """
    Processes FASTQ reads into PyTorch embedding vectors.

    Args:
        filepath (str): Path to the FASTA file relative to the current working directory.
        token_length (int, optional): Number of desired nucleotides per token (ie, entry in the tensor). If not 
        specified, the bitarray is divided into uint8 chunks by default.

    Returns:
        List[torch.Tensor]: A list of PyTorch tensors, with each tensor being composed of uints (type of uint depends on token_length).
    """

Example usage:

import nucleotorch
tensors = nucleotorch.process_fasta("path/to/example/FASTA/file", 6)

KNOWN ISSUES

2/14/2025 In some cases on intel macOS or linux, venv installation fails due to not finding numpy via the toml. This can be fixed by first installing numpy in the venv, and doing a fresh reinstall of nucleotorch, or by globally installing nucleotorch outside of a venv (assuming you have numpy installed). This allows for a cached file to be built and used for venv installation. To be fixed in v1.2.1

Source Code:

https://github.com/agwilliams201/nucleotorch

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

nucleotorch-1.2.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

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

nucleotorch-1.2.0-cp312-cp312-macosx_11_0_arm64.whl (10.7 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file nucleotorch-1.2.0.tar.gz.

File metadata

  • Download URL: nucleotorch-1.2.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for nucleotorch-1.2.0.tar.gz
Algorithm Hash digest
SHA256 52c37c2c603cad70ca957c3e84c24456844f34d635869d1fbdbee95f35d0920f
MD5 80f6dc5e5192b25b50de7ef73af3c7fc
BLAKE2b-256 02da78af2d655accf7f5f26afaff2c70e4dd9948aff37997292ffc7a3e694f2b

See more details on using hashes here.

File details

Details for the file nucleotorch-1.2.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for nucleotorch-1.2.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9dafc70d582aeae57c4395309bb539e2c82d1cd19f29504c2816b0765c44ab4b
MD5 3f99e280df484129f9e9bdc3f361b0ea
BLAKE2b-256 a75495fcbd3cf99361d67236ef235c8f443b4111e8874ad38318319c3fc048ee

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