Skip to main content

CI crates.io

Needletail

Needletail is a MIT-licensed, minimal-copying FASTA/FASTQ parser and k-mer processing library for Rust.

The goal is to write a fast and well-tested set of functions that more specialized bioinformatics programs can use. Needletail's goal is to be as fast as the readfq C library at parsing FASTX files and much (i.e. 25 times) faster than equivalent Python implementations at k-mer counting.

Example

extern crate needletail;
use needletail::{parse_fastx_file, Sequence, FastxReader};

fn main() {
    let filename = "tests/data/28S.fasta";

    let mut n_bases = 0;
    let mut n_valid_kmers = 0;
    let mut reader = parse_fastx_file(&filename).expect("valid path/file");
    while let Some(record) = reader.next() {
        let seqrec = record.expect("invalid record");
        // keep track of the total number of bases
        n_bases += seqrec.num_bases();
        // normalize to make sure all the bases are consistently capitalized and
        // that we remove the newlines since this is FASTA
        let norm_seq = seqrec.normalize(false);
        // we make a reverse complemented copy of the sequence first for
        // `canonical_kmers` to draw the complemented sequences from.
        let rc = norm_seq.reverse_complement();
        // now we keep track of the number of AAAAs (or TTTTs via
        // canonicalization) in the file; note we also get the position (i.0;
        // in the event there were `N`-containing kmers that were skipped)
        // and whether the sequence was complemented (i.2) in addition to
        // the canonical kmer (i.1)
        for (_, kmer, _) in norm_seq.canonical_kmers(4, &rc) {
            if kmer == b"AAAA" {
                n_valid_kmers += 1;
            }
        }
    }
    println!("There are {} bases in your file.", n_bases);
    println!("There are {} AAAAs in your file.", n_valid_kmers);
}

Installation

Needletail requires rust and cargo to be installed. Please use either your local package manager (homebrew, apt-get, pacman, etc) or install these via rustup.

Once you have Rust set up, you can include needletail in your Cargo.toml file like:

[dependencies]
needletail = "0.6.0"

To install needletail itself for development:

git clone https://github.com/onecodex/needletail
cargo test  # to run tests

Python

Documentation

For a real example, you can refer to test_python.py.

The python library only raise one type of exception: NeedletailError.

There are 2 ways to parse a FASTA/FASTQ: one if you have a string (parse_fastx_string(content: str)) or a path to a file (parse_fastx_file(path: str)). Those functions will raise if the file is not found or if the content is invalid and will return an iterator.

from needletail import parse_fastx_file, NeedletailError, reverse_complement, normalize_seq

try:
    for record in parse_fastx_file("myfile.fastq"):
        print(record.id)
        print(record.seq)
        print(record.qual)
except NeedletailError:
    print("Invalid Fastq file")

A record has the following shape:

class Record:
    id: str
    seq: str
    qual: Optional[str]

    def is_fasta(self) -> bool
    def is_fastq(self) -> bool
    def normalize(self, iupac: bool)

Note that normalize (see https://docs.rs/needletail/0.4.1/needletail/sequence/fn.normalize.html for what it does) will mutate self.seq. It is also available as the normalize_seq(seq: str, iupac: bool) function which will return the normalized sequence in this case.

Lastly, there is also a reverse_complement(seq: str) that will do exactly what it says. This will not raise an error if you pass some invalid characters.

Building

To work on the Python library on a Mac OS X/Unix system (requires Python 3):

pip install maturin

# finally, install the library in the local virtualenv
maturin develop --cargo-extra-args="--features=python"

To build the binary wheels and push to PyPI

# The Mac build requires switching through a few different python versions
maturin build --features python --release --strip

# The linux build is automated through cross-compiling in a docker image
docker run --rm -v $(pwd):/io ghcr.io/pyo3/maturin:main build --features=python --release --strip -f
twine upload target/wheels/*

Releasing A New Version

There is a Github Workflow that will build Python wheels for macOS (x86 and ARM) and Ubuntu (x86). To run, create a new release.

Getting Help

Questions are best directed as GitHub issues. We plan to add more documentation soon, but in the meantime "doc" comments are included in the source.

Contributing

Please do! We're happy to discuss possible additions and/or accept pull requests.

Acknowledgements

Starting from 0.4, the parsers algorithms is taken from seq_io. While it has been slightly modified, it is mainly coming from that library. Links to the original files are available in src/parser/fast{a,q}.rs.

Metadata

Release files for needletail 0.6.3

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

Source distribution (sdist)

Source distribution for needletail 0.6.3
File Size Uploaded Provenance Actions
needletail-0.6.3.tar.gz 434.7 kB Without provenance Details for needletail-0.6.3.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for needletail 0.6.3
File Actions
needletail-0.6.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.17+ x86-64 Details for needletail-0.6.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
needletail-0.6.3-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details for needletail-0.6.3-cp312-cp312-macosx_11_0_arm64.whl
needletail-0.6.3-cp312-cp312-macosx_10_12_x86_64.whl CPython 3.12 CPython 3.12 macOS 10.12+ x86-64 Details for needletail-0.6.3-cp312-cp312-macosx_10_12_x86_64.whl
needletail-0.6.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.17+ x86-64 Details for needletail-0.6.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
needletail-0.6.3-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details for needletail-0.6.3-cp311-cp311-macosx_11_0_arm64.whl
needletail-0.6.3-cp311-cp311-macosx_10_12_x86_64.whl CPython 3.11 CPython 3.11 macOS 10.12+ x86-64 Details for needletail-0.6.3-cp311-cp311-macosx_10_12_x86_64.whl

Total release size: 3.4 MB

Release files / needletail-0.6.3.tar.gz

Download URL needletail-0.6.3.tar.gz
Size 434.7 kB
Tags Source
SHA-256 checksum
How to use checksums
93da7faa511eae7da4a3186c0d7d73ccfb3b4f14d9f9777fac20dd2bd75efb61
BLAKE2b-256 checksum
How to use checksums
33835f50f1dac42953e64588be292904fcaede10439323b0a859e1caf9b43ab1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.5

Release files / needletail-0.6.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL needletail-0.6.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 590.8 kB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
de7f60c7b61cb25c52c2bfa516cc146487a31f53702d8429ca46edb813d5cc4f
BLAKE2b-256 checksum
How to use checksums
88037c6d78a3a84344c885236401e705280bf29b664215b3a8b1e36f9221036d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.5

Release files / needletail-0.6.3-cp312-cp312-macosx_11_0_arm64.whl

Download URL needletail-0.6.3-cp312-cp312-macosx_11_0_arm64.whl
Size 423.9 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
4d46ea8d27f8a2d9c47879818a90584a50fd27937a60a52e06fdb0d20abd6592
BLAKE2b-256 checksum
How to use checksums
80b470f2ab21a23fd885605053b6da65fb409a793d3c7781351c329f0597e03f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.5

Release files / needletail-0.6.3-cp312-cp312-macosx_10_12_x86_64.whl

Download URL needletail-0.6.3-cp312-cp312-macosx_10_12_x86_64.whl
Size 488.1 kB
Tags CPython 3.12 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
9ea2536d3366b23f20de6c247a64cb5d42d640a6bad5cf04e4532fb9d6c74082
BLAKE2b-256 checksum
How to use checksums
cf95dd0ad78b6fcba1dd59c40471eee84b0c796b5d23a64cbdbd207e0fc3cdf8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.5

Release files / needletail-0.6.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL needletail-0.6.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 591.5 kB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
c090bf9f5e9e59faa951ab2f28c039d44b98a8ca302725537eeaad706ef6fa80
BLAKE2b-256 checksum
How to use checksums
403cbf64588c6117b5d5d01b1b43549110c294d99dd35e4ef225f036ddf54054
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.5

Release files / needletail-0.6.3-cp311-cp311-macosx_11_0_arm64.whl

Download URL needletail-0.6.3-cp311-cp311-macosx_11_0_arm64.whl
Size 424.5 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
0281def15e5ebc9240ca66dbcaf87aad5bf5bc77ce5ebed089680389046c512f
BLAKE2b-256 checksum
How to use checksums
690480d6216666487c022db072d51f231b3d8e682fc4f0f8fb9d2b4fb4ff6658
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.5

Release files / needletail-0.6.3-cp311-cp311-macosx_10_12_x86_64.whl

Download URL needletail-0.6.3-cp311-cp311-macosx_10_12_x86_64.whl
Size 488.1 kB
Tags CPython 3.11 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
4979272931143253e7ca1ca04dcb05e9359c9db9dcffe057d89e292476bed155
BLAKE2b-256 checksum
How to use checksums
b662ed6a2a70d310b5325fbe5faf9ceee5cb7601740f22e68152d9a3aee3a1b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.5

Security for needletail 0.6.3

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.

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