Skip to main content

FASTX parsing and k-mer methods

Project description

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.4"

To install needletail itself for development:

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

Python

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"

Building binary wheels and pushing to PyPI

# The Mac build requires switching through a few different python versions
maturin build --cargo-extra-args="--features=python" --release --strip

# The linux build is automated through cross-compiling in a docker image
docker run --rm -v $(pwd):/io konstin2/maturin:master build --cargo-extra-args="--features=python" --release --strip
twine upload target/wheels/*

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.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

needletail-0.4.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (293.2 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

needletail-0.4.1-cp310-cp310-macosx_11_0_arm64.whl (285.2 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

needletail-0.4.1-cp39-cp39-manylinux2010_x86_64.whl (313.6 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

needletail-0.4.1-cp39-cp39-macosx_10_7_x86_64.whl (267.1 kB view hashes)

Uploaded CPython 3.9 macOS 10.7+ x86-64

needletail-0.4.1-cp38-cp38-manylinux2010_x86_64.whl (313.3 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

needletail-0.4.1-cp38-cp38-macosx_10_7_x86_64.whl (267.5 kB view hashes)

Uploaded CPython 3.8 macOS 10.7+ x86-64

needletail-0.4.1-cp37-cp37m-manylinux2010_x86_64.whl (313.2 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

needletail-0.4.1-cp36-cp36m-manylinux2010_x86_64.whl (313.2 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64

Supported by

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