Skip to main content

Neighbor-Joining phylogenetic tree inference. Auto-detects DNA/protein, supports multiple substitution models and bootstrap.

Project description

nj.rs

Neighbor-Joining phylogenetic tree inference in Rust, with Python and WASM bindings.

Release Tests

Crates.io Version PyPI - Version NPM Version

Takes a mutiple sequence alignment and returns a Newick string. Alphabet (DNA/protein) is auto-detected. Supports optional bootstrap support values on internal nodes. Optionally, only the distance matrix or average distance can be computed. Wrappers use a plugin system for implementing progress/error logging.

Substitution models: PDiff (DNA + protein), JukesCantor (DNA), Kimura2P (DNA), Poisson (protein)

CLI

cargo install nj --features cli

nj sequences.fasta
nj --substitution-model kimura2-p --n-bootstrap-samples 100 sequences.fasta > tree.nwk

# Bootstrap and distance computation run in parallel by default (the `cli`
# feature enables threading). Cap the worker count with -t/--num-threads:
nj -t 4 --n-bootstrap-samples 1000 sequences.fasta > tree.nwk

A progress bar is shown on stderr when bootstrapping.

Rust

[dependencies]
nj = "0.0.18"
use nj::{NJConfig, NJEvent, SequenceObject, nj, parse_fasta};
use nj::models::SubstitutionModel;

// Parse from a FASTA string
let sequences = parse_fasta(">A\nACGTACGT\n>B\nACCTACGT\n>C\nTCGTACGT\n")?;

// Run Neighbor-Joining
let newick = nj(
    NJConfig {
        msa: sequences,
        n_bootstrap_samples: 100,
        substitution_model: SubstitutionModel::JukesCantor,
        alphabet: None,
        num_threads: None,
    },
    Some(Box::new(|event| {
        if let NJEvent::BootstrapProgress { completed, total } = event {
            eprintln!("{completed}/{total}");
        }
    })),
)?;

Distance-only computation (no tree, no bootstrap):

use nj::{DistConfig, distance_matrix};
use nj::models::SubstitutionModel;

let result = distance_matrix(DistConfig {
    msa: sequences,
    substitution_model: SubstitutionModel::JukesCantor,
    alphabet: None,
    num_threads: None,
})?;
// result.names — Vec<String> of taxon names
// result.matrix — n×n Vec<Vec<f64>>, symmetric, diagonal zero

average_distance takes the same DistConfig and returns the mean of all pairwise distances as an f64.

Python

pip install nj_py
from nj_py import nj, distance_matrix

msa = [
    {"identifier": "A", "sequence": "ACGTACGT"},
    {"identifier": "B", "sequence": "ACCTACGT"},
    {"identifier": "C", "sequence": "TCGTACGT"},
]

def on_event(event):
    if event["type"] == "BootstrapProgress":
        print(f"{event['completed']}/{event['total']}")

newick = nj(msa, substitution_model="JukesCantor", n_bootstrap_samples=100, on_event=on_event)

Distance-only computation:

result = distance_matrix(msa, substitution_model="JukesCantor")
# result["names"] — list of taxon names
# result["matrix"] — n×n list of lists, symmetric, diagonal zero

JavaScript / WASM

npm install @holmrenser/nj
import { nj, distance_matrix } from '@holmrenser/nj';

const msa = [
    { identifier: 'A', sequence: 'ACGTACGT' },
    { identifier: 'B', sequence: 'ACCTACGT' },
    { identifier: 'C', sequence: 'TCGTACGT' },
];

const newick = nj(
    { msa, n_bootstrap_samples: 100, substitution_model: 'JukesCantor' },
    (event) => {
        if (event.type === 'BootstrapProgress') {
            progressBar.value = event.completed / event.total * 100;
        }
    }
);

const { names, matrix } = distance_matrix({ msa, substitution_model: 'JukesCantor' });

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

nj_py-0.0.25.tar.gz (76.3 kB view details)

Uploaded Source

Built Distributions

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

nj_py-0.0.25-cp310-abi3-win_amd64.whl (332.2 kB view details)

Uploaded CPython 3.10+Windows x86-64

nj_py-0.0.25-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (446.9 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

nj_py-0.0.25-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (435.1 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

nj_py-0.0.25-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (818.4 kB view details)

Uploaded CPython 3.10+macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

File details

Details for the file nj_py-0.0.25.tar.gz.

File metadata

  • Download URL: nj_py-0.0.25.tar.gz
  • Upload date:
  • Size: 76.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for nj_py-0.0.25.tar.gz
Algorithm Hash digest
SHA256 ea4d189219fcf411a0296851c1c84d2fc41f269fce3393dc48358a9473b8bd7f
MD5 a520bfd750b90a732a540cf55f1bf567
BLAKE2b-256 544256dcc3b75ce4679c08c81c39fed6d91ad6369844e2a5500aa31b874927c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for nj_py-0.0.25.tar.gz:

Publisher: release.yml on holmrenser/nj.rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nj_py-0.0.25-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: nj_py-0.0.25-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 332.2 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for nj_py-0.0.25-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 6aa493525e774ccbd49979ef9aaf90369094ed4df4559e3782dc9da87e0bb7b7
MD5 c08aca1b9b6db19620929963943d75d2
BLAKE2b-256 312912d61d3221e3fde73f45cd4c563b6749893071448dc69c735e6eee776752

See more details on using hashes here.

Provenance

The following attestation bundles were made for nj_py-0.0.25-cp310-abi3-win_amd64.whl:

Publisher: release.yml on holmrenser/nj.rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nj_py-0.0.25-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nj_py-0.0.25-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 109cba7a092aabe3e5c190c6527ff46a55ac7a5476f9dd555691c7e8cb63429b
MD5 3db85899de0bee901fe813ddf2822208
BLAKE2b-256 bf6ad60ee193e2855ebc54e4bef7766e768ee5c246e4f063acf5f8346a0f3b8e

See more details on using hashes here.

Provenance

The following attestation bundles were made for nj_py-0.0.25-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on holmrenser/nj.rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nj_py-0.0.25-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for nj_py-0.0.25-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e2428622bf6bcd9aa7103aefe240b9727823a89d9ed28785dd448a72b8c46171
MD5 ebfff8e2992026281cd12976f9cf934f
BLAKE2b-256 124c7b79261687bee5a26830462679eca19b10189b1ded9d3da3a5767d1ba6f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for nj_py-0.0.25-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on holmrenser/nj.rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nj_py-0.0.25-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for nj_py-0.0.25-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 5f5e2d5b667fd9ed3b39609024f2d563da11c9cbd886b53e011b86d607aaf4be
MD5 b188e60b2a83e2f7d691910c5baae1f7
BLAKE2b-256 250f72ad614ea1b5e69a7728045e6fdb3a6d8daa2a40e6d327653c9f0919b357

See more details on using hashes here.

Provenance

The following attestation bundles were made for nj_py-0.0.25-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl:

Publisher: release.yml on holmrenser/nj.rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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