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.24.tar.gz (72.8 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.24-cp310-abi3-win_amd64.whl (325.9 kB view details)

Uploaded CPython 3.10+Windows x86-64

nj_py-0.0.24-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (441.8 kB view details)

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

nj_py-0.0.24-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (431.0 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

nj_py-0.0.24-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (806.9 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.24.tar.gz.

File metadata

  • Download URL: nj_py-0.0.24.tar.gz
  • Upload date:
  • Size: 72.8 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.24.tar.gz
Algorithm Hash digest
SHA256 6f6845a9687b6b8f285d4ce020f0916f199e88b82a66ca10081a04cbb0d9e577
MD5 f0dec2c1eb89c3d7fc73a730767182b6
BLAKE2b-256 479e845c466cd38ea79e3abfe28ab01e60865092676d1cfa556062e7f0844026

See more details on using hashes here.

Provenance

The following attestation bundles were made for nj_py-0.0.24.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.24-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: nj_py-0.0.24-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 325.9 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.24-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 4edda506dbb09bf6fe035c0b0b8a840cacdb2464c4099117921c54e4198c44d1
MD5 56ace1c1e29c0b8dc1ae332bc8e3f88d
BLAKE2b-256 8bcc3fa966fbf9a492aab352223538cd193c330a7deb77f9c0362a59be7d7025

See more details on using hashes here.

Provenance

The following attestation bundles were made for nj_py-0.0.24-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.24-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nj_py-0.0.24-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ef0852d87d82143dd32c298a8c56b1d188b1507c832d783b6741bebcabaa03de
MD5 4141b588a511b94200b5bd0da89c1ae5
BLAKE2b-256 d247c9b109642a9ab36b4743e4dca0da623aeca0de4400dee927c664f9603688

See more details on using hashes here.

Provenance

The following attestation bundles were made for nj_py-0.0.24-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.24-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for nj_py-0.0.24-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 0a208cecaea74e300410e3724a102819d8e24e916278abd18ee88bcd9bed7b07
MD5 0cfe757895fc88f25643b281cbe846ad
BLAKE2b-256 37dbe165498617b0ee0e399165f7ccb445ce83df17e8779a8bbe7fa804656757

See more details on using hashes here.

Provenance

The following attestation bundles were made for nj_py-0.0.24-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.24-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.24-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 65704c2178cb32343ad3da0aaded5f1cadfc171c812f1378ac4e0763ebd0b86c
MD5 d005e27243eb8e9e31cdf96079134c82
BLAKE2b-256 94016c2dbe0040c2476babb207b72e210c214f8c562eea303ebc953b413304af

See more details on using hashes here.

Provenance

The following attestation bundles were made for nj_py-0.0.24-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