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.23.tar.gz (69.9 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.23-cp310-abi3-win_amd64.whl (301.0 kB view details)

Uploaded CPython 3.10+Windows x86-64

nj_py-0.0.23-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (423.0 kB view details)

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

nj_py-0.0.23-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (412.3 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

nj_py-0.0.23-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (767.1 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.23.tar.gz.

File metadata

  • Download URL: nj_py-0.0.23.tar.gz
  • Upload date:
  • Size: 69.9 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.23.tar.gz
Algorithm Hash digest
SHA256 7dbc4cc9a2cf5f6fda7705343b209f993f23193795b8b71dbcde383ffe3a4f30
MD5 07b7832b3f631af73f91977c9624f689
BLAKE2b-256 965a43564f85437baf03a543904847ab503295a1de58120d6ff346f634bb66a4

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: nj_py-0.0.23-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 301.0 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.23-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 cad9aeb3d52d13cbccd3e47caa6f45e2dcbfe12d8821985b99cbb5656ee459d2
MD5 0287e85353944085b3a2a0b215bf4936
BLAKE2b-256 3b9b5bf54211b39b3cefabff3cab12026bbcacc3d695dddd59b7e98feb468296

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for nj_py-0.0.23-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6b091b01a9bd402e8cc84c63ff2895f3fc171d1202e1f5ec2c0698135e0acc03
MD5 de623fff8124e20536236521c23af5a7
BLAKE2b-256 5c3b0a42a3d269b2f8365ed20056f55c2681c657f2a0a10bb032d37648c63ee2

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for nj_py-0.0.23-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 97f78dd44cf8e2308e19650273165ea5e24018e984867f1a6179165a4143d23d
MD5 787d29afd5b51089da9103662bb7fc03
BLAKE2b-256 408a0d6990b3e87ca915b64073a147e9ca2ba5c639010e3eb845905b8938aaa7

See more details on using hashes here.

Provenance

The following attestation bundles were made for nj_py-0.0.23-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.23-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.23-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 1b46b497d8e68d63279f65b613996e2dccf1c3905ffe14cc89f96983b765c096
MD5 cbf45c876fa15b96a148de7f7bb38695
BLAKE2b-256 bfae91e33e54bb78d8e0f69986f401ac879770f617cb2fb69adbf4421d3bfa44

See more details on using hashes here.

Provenance

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