Skip to main content

nnfasta

Lightweight Neural Net efficient FASTA dataset suitable for training.

Should be memory efficient across process boundaries. So useful as input to torch/tensorflow dataloaders with multiple workers etc. (see this issue)

Presents a list of fasta files as a simple abc.Sequence so you can inquire about len(dataset) and retrieve Records randomly with dataset[i]

Uses Python's mmap.mmap under the hood.

The underlying FASTA's should be "well formed" since there is minimal sanity checking done.

Install

Install with:

pip install nnfasta

There are no dependencies, you just need a modern (>= 3.9) python (< 12K of code).

Usage

from nnfasta import nnfastas

dataset = nnfastas(['athaliana.fasta','triticum.fasta','zmays.fasta'])

# display the number of sequences
print(len(dataset))

# get a particular record
rec = dataset[20]
print('sequence', rec.id, rec.description, rec.seq)

Warning: No checks are made for the existence of the fasta files. Also files of zero length will be rejected by mmap.

A Record mimics biopython's SeqRecord and is simply:

@dataclass
class Record:
    id: str
    """Sequence ID"""
    description: str
    """Line prefixed by '>'"""
    seq: str
    """Sequence stripped of whitespace and uppercased"""

    @property
    def name(self) -> str:
        return self.id

The major difference is that seq is just a simple str not a biopython Seq object (We just don't want the Bio dependency -- nnfasta has no dependencies).

Arguments

You can give nnfastas either a filename, a Path, the actual bytes in the file or an open file pointer (opened with mode="rb") OR a list of these things. e.g:

from nnfasta import nnfastas
my = "my.fasta"
fa = nnfastas([my, open(my mode="rb"),
            open(my, mode="rb").read()])

Encoding

The files are assumed to be encoded as "ASCII". If this is not the case then nnfastas accepts an encoding argument. All the files presented to nnfastas are assumed to be similarly encoded. You can alter the decoding with the errors keyword (default=strict).

Test and Train Split best practice

Use SubsetFasta

from nnfasta import nnfastas, SubsetFasta
from sklearn.model_selection import train_test_split

dataset = nnfastas(['athaliana.fasta','triticum.fasta','zmays.fasta'])
train_idx, test_idx = train_test_split(range(len(dataset)),test_size=.1,shuffle=True)

# these are still Sequence[Record] objects.

train_data = SubsetFasta(dataset, train_idx)
test_data = SubsetFasta(dataset, test_idx)

# *OR* ... this is basically the same
import torch
train_data, test_data = torch.utils.data.random_split(dataset, [.9, .1])

See the pytorch Subset logic here

How it works

We memory map the input files and use python's re package to scan the files for b"\r>|\n>|^>" bytes from which we compute a start, end for each record and create an array.array (in memory).

The operating system will ensure that similar mmapped pages in different process are shared.

Enjoy peps!

Metadata

Release files for nnfasta 0.1.36

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

Source distribution (sdist)

Source distribution for nnfasta 0.1.36
File Size Uploaded Provenance Actions
nnfasta-0.1.36.tar.gz 7.0 kB Without provenance Details for nnfasta-0.1.36.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nnfasta 0.1.36
File Interpreter ABI Platform Actions
nnfasta-0.1.36-py3-none-any.whl Python 3 none any Details for nnfasta-0.1.36-py3-none-any.whl

Total release size: 14.7 kB

Release files / nnfasta-0.1.36.tar.gz

Download URL nnfasta-0.1.36.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
33de0e5be3fdaf85f4f73a0f1b6b94354ae6d746cd443ed0cb3c949a88385ca2
BLAKE2b-256 checksum
How to use checksums
7cd8da91b6f48e85cd6714d97ab4d05ec2c418316539090ba3a1c36bf32ed15b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.11.0 Linux/5.15.0-106-generic

Release files / nnfasta-0.1.36-py3-none-any.whl

Download URL nnfasta-0.1.36-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
efd2c24ce89afc24862338d606ba035267b72d55c1f2208725eb8569f74011ee
BLAKE2b-256 checksum
How to use checksums
9c5a7445ac8eb605e23d32f986870c160487f8255bcd1e2a0e99954ad467f090
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.11.0 Linux/5.15.0-106-generic

Security for nnfasta 0.1.36

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.

Release history Release notifications | RSS feed

This release

0.1.36 This release

2 release files No provenance

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