nj.rs
Neighbor-Joining phylogenetic tree inference in Rust, with Python and WASM bindings.
Takes a multiple sequence alignment and returns a Newick tree. The alphabet (DNA/protein) is auto-detected. Features:
- Seven distance models, with optional among-site rate variation (gamma shape and/or proportion of invariant sites)
- Optional bootstrap support values on internal nodes (parallel when built with the
parallelfeature) - Distance-only modes: the full pairwise distance matrix or just the average distance
- A progress/log event callback in every binding
Substitution models
| Model | DNA | Protein | Rate variation (gamma_shape, p_invar) |
|---|---|---|---|
PDiff |
✓ | ✓ | ignored |
JukesCantor |
✓ | ✓ | |
Kimura2P |
✓ | ✓ | |
TajimaNei |
✓ | ✓ | |
Tamura |
✓ | ✓ | |
Poisson |
✓ | ✓ | |
KimuraProtein |
✓ | ✓ |
Using a model that does not match the alphabet returns an error. gamma_shape (α) must be > 0 and p_invar must be in [0, 1); they combine as +I+Γ.
CLI
cargo install nj --features cli
nj sequences.fasta
nj --substitution-model kimura2-p --n-bootstrap-samples 100 sequences.fasta > tree.nwk
cat sequences.fasta | nj -m jukes-cantor -g 0.5 -i 0.2 # reads stdin when no file is given
# 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
| Option | Description |
|---|---|
[FASTA] |
Input alignment (stdin if omitted) |
-o, --output <FILE> |
Write output to a file instead of stdout |
-b, --n-bootstrap-samples <N> |
Bootstrap replicates, 0 to disable (default 100) |
-m, --substitution-model <MODEL> |
p-diff (default), jukes-cantor, kimura2-p, tajima-nei, tamura, poisson, kimura-protein |
-a, --alphabet <ALPHABET> |
Override alphabet auto-detection |
-g, --gamma-shape <ALPHA> |
Gamma rate-heterogeneity shape |
-i, --p-invar <FRACTION> |
Proportion of invariant sites |
--distance-matrix, --average-distance |
Also compute these; output becomes a JSON object with a newick field |
-t, --num-threads <N> |
Worker thread cap |
-v, --verbose |
Print stage log messages to stderr |
A progress bar is shown on stderr when bootstrapping.
Rust
[dependencies]
nj = "0.0.25"
use nj::{NJConfig, NJEvent, 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 result = nj(
NJConfig {
msa: sequences,
n_bootstrap_samples: 100,
substitution_model: SubstitutionModel::JukesCantor,
alphabet: None,
num_threads: None,
return_distance_matrix: false,
return_average_distance: false,
gamma_shape: None,
p_invar: None,
},
Some(Box::new(|event| {
if let NJEvent::BootstrapProgress { completed, total } = event {
eprintln!("{completed}/{total}");
}
})),
)?;
println!("{}", result.newick);
// result.distance_matrix / result.average_distance are `Some` when requested
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,
gamma_shape: None,
p_invar: 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.
Cargo features: parallel (Rayon-based bootstrap and distance computation), cli (the binary; implies parallel), and simd (explicit SIMD kernel, nightly only).
Python
pip install nj_py
from nj_py import nj, distance_matrix, average_distance
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']}")
result = nj(
msa,
substitution_model="JukesCantor",
n_bootstrap_samples=100,
gamma_shape=0.5, # optional
p_invar=0.2, # optional
on_event=on_event,
)
print(result["newick"])
nj returns a dict with a newick key. Pass return_distance_matrix=True and/or return_average_distance=True to also get distance_matrix / average_distance keys.
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
mean = average_distance(msa, substitution_model="JukesCantor")
JavaScript / WASM
npm install @holmrenser/nj
import { nj, distance_matrix, average_distance } 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' });
const mean = average_distance({ msa, substitution_model: 'JukesCantor' });
gamma_shape, p_invar, return_distance_matrix and return_average_distance are accepted in the config object as in Python.
Metadata
Release files for nj-py 0.0.26
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| nj_py-0.0.26.tar.gz | 80.8 kB | With provenance | Details for nj_py-0.0.26.tar.gz |
Built distributions (wheels)
| File | Actions Reset | |||
|---|---|---|---|---|
| nj_py-0.0.26-cp310-abi3-win_amd64.whl | CPython 3.10 | abi3 | Windows x86-64 | Details for nj_py-0.0.26-cp310-abi3-win_amd64.whl |
| nj_py-0.0.26-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl | CPython 3.10 | abi3 | Linux glibc 2.17+ x86-64 | Details for nj_py-0.0.26-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
| nj_py-0.0.26-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl | CPython 3.10 | abi3 | Linux glibc 2.17+ ARM64 | Details for nj_py-0.0.26-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl |
| nj_py-0.0.26-cp310-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl | CPython 3.10 | abi3 | macOS 10.12+ x86-64, macOS 11.0+ ARM64, macOS 10.12+ universal2 (ARM64, x86-64) | Details for nj_py-0.0.26-cp310-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl |
Total release size: 2.1 MB
Release files / nj_py-0.0.26.tar.gz
| Download URL | nj_py-0.0.26.tar.gz |
|---|---|
| Size | 80.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b29819a017b38d914d3577d4120c9420be84c87694d06929573901f4e58d0edc
|
|
BLAKE2b-256 checksum How to use checksums |
30c4b6369b66ab6f92034d29d9f1e3fe449a63468b71989e6ceea18809132f8f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logRelease files / nj_py-0.0.26-cp310-abi3-win_amd64.whl
| Download URL | nj_py-0.0.26-cp310-abi3-win_amd64.whl |
|---|---|
| Size | 333.0 kB |
| Tags | CPython 3.10 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
9921775e092cd0ebbd9ed66129fbaf3c2f8998ad9330d1b9d0f1225036049fd0
|
|
BLAKE2b-256 checksum How to use checksums |
727e8e2d399b5c34276f13f08671723aa1a36e9f5f9bf3ee0623372407b83e8f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logRelease files / nj_py-0.0.26-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | nj_py-0.0.26-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 450.3 kB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
615f2d5bb0846940b10b9f3a4ec1c0eaa4890fb6427c790c76f501690fb48149
|
|
BLAKE2b-256 checksum How to use checksums |
d000c2b469f5230c82fe80b8244c8bea833e577950763b47effc30aa8c0b9510
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logRelease files / nj_py-0.0.26-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
| Download URL | nj_py-0.0.26-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl |
|---|---|
| Size | 438.6 kB |
| Tags | CPython 3.10 Linux glibc 2.17+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
9cb1b17f649040291179d383102fe1b426048c7fbf125d626c8d78aee7cefb60
|
|
BLAKE2b-256 checksum How to use checksums |
8cba583877b554e487e1a5eaaf628a2561402cb86d79058010fa67c97b3506a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logRelease files / nj_py-0.0.26-cp310-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl
| Download URL | nj_py-0.0.26-cp310-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl |
|---|---|
| Size | 817.7 kB |
| Tags | CPython 3.10 abi3 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
0601e01dce96b8f020427ae68bdf90d097b381665e4c4b25ce27d8ccb1932b95
|
|
BLAKE2b-256 checksum How to use checksums |
c5f961b9f63e93fe77074b9b26a8abac04224a3b6773af005e27630de600d40a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logSecurity for nj-py 0.0.26
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.
Full provenance
All 5 files in this release have been uploaded with provenance metadata.
Attestation for nj_py-0.0.26.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.
Transparency logA 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.